/** * Parse the text by filtering through the tokens. * * @param array $tokens * * @return array */ public function parseTokens($tokens) { $registry = POSRegistry::getInstance(); $length = count($tokens); for ($i = 0; $i < $length; ++$i) { if (!isset($tokens[$i]['partOfSpeech1'])) { continue; } $previousWord = end($this->words); $previous = $i > 0 ? $tokens[$i - 1] : null; $current = $tokens[$i]; $next = $i + 1 < $length ? $tokens[$i + 1] : null; $properties = $this->getProperties($registry, $previousWord, $previous, $current, $next); if ($properties['attachToPrevious'] && count($this->words) > 0) { $this->appendWordToLast($current, $properties, $previousWord); } else { $this->makeNewWord($current, $properties); } if ($properties['eatNext']) { $this->eatNextToken($current, $next, $properties); $i += 1; } } return $this->words; }
/** * @test */ public function it_sets_a_class_in_the_registry_and_gets_same() { $registry = POSRegistry::getInstance(); $class1 = $registry->getClass('Meishi'); $hash1 = spl_object_hash($class1); $class2 = $registry->getClass('Meishi'); $hash2 = spl_object_hash($class1); $this->AssertEquals($hash1, $hash2); }
/** * Handle the parsing request. * * @param array $properties * @param array $previousWord [previous word] * @param array $previous [previous token] * @param array $current [current token] * @param array $next [next token] * * @return array */ public function handle(array $properties, $previousWord, $previous, array $current, $next) { $properties['partOfSpeech'] = 'noun'; $registry = POSRegistry::getInstance(); if (array_key_exists($current['partOfSpeech2'], $this->overrides)) { $className = 'Meishi' . ucfirst($this->overrides[$current['partOfSpeech2']]); } else { $className = 'Meishi' . ucfirst($current['partOfSpeech2']); } if (class_exists('Limelight\\Parse\\PartOfSpeech\\Classes\\' . $className)) { $POSClass = $registry->getClass($className); $properties = $POSClass->handle($properties, $previousWord, $previous, $current, $next); } return $properties; }