public function testGetPrevWhitespace()
 {
     $this->assertSame(false, StringUtil::getPrevWhitespace('Saya'));
     $this->assertSame(0, StringUtil::getPrevWhitespace(' '));
     $this->assertEquals(10, StringUtil::getPrevWhitespace('segmentasi kalimat'));
     $this->assertEquals(12, StringUtil::getPrevWhitespace('Saya belajar segmentasi kalimat.', 15));
     // exclusive current position
     $this->assertEquals(4, StringUtil::getPrevWhitespace('Saya belajar segmentasi kalimat.', 12));
 }
 private function getToken($text, $position)
 {
     if ($position >= strlen($text) - 1) {
         return '';
     }
     $nextWs = StringUtil::getNextWhitespace($text, $position);
     $prevWs = StringUtil::getPrevWhitespace($text, $position);
     $tokenStart = $prevWs === false ? 0 : $prevWs + 1;
     $tokenEnd = ($nextWs === false ? strlen($text) : $nextWs) - 1;
     $token = substr($text, $tokenStart, $tokenEnd - $tokenStart);
     return $token;
 }
Beispiel #3
0
 private function getToken($text, $position)
 {
     if ($position < strlen($text) - 1 && !StringUtil::isWhitespace(substr($text, $position + 1, 1))) {
         $nextWs = StringUtil::getNextWhitespace($text, $position);
         $prevWs = StringUtil::getPrevWhitespace($text, $position);
         $tokenStart = $prevWs === false ? 0 : $prevWs + 1;
         $tokenEnd = $nextWs === false ? strlen($text) : $nextWs;
         $token = substr($text, $tokenStart, $tokenEnd - $tokenStart);
         // strip trailing .
         if (!empty($token) && in_array($token[strlen($token) - 1], $this->eosChars)) {
             $token = substr($token, 0, strlen($token) - 1);
         }
         return $token;
     } else {
         return '';
     }
 }