public function testEndsWith() { $this->assertTrue(Text::endsWith('lunches', 's')); $this->assertTrue(Text::endsWith('lunches', 'es')); $this->assertTrue(Text::endsWith('lunches', 'hes')); $this->assertFalse(Text::endsWith('joe', 'is')); }
/** * Lower case the word and return it * @param string $word * @return string */ public function transform($word) { if (Text::endsWith($word, "'s")) { return substr($word, 0, -2); } else { return $word; } }
/** * Concept taken from nltk * Find a possible base form for the given form, with the given * part of speech, by checking WordNet's list of exceptional * forms, and by recursively stripping affixes for this part of * speech until a form in WordNet is found. * @todo improve the algorithm, it is really slow * @param string $word * @param string|null $pos * @return string return the base word */ public function getMorph($word, $pos = '') { if (mb_strlen($word) < 3) { return ""; } $searchForFuncWithPos = function (ExceptionMap $exceptionMap) use($word, $pos) { return $exceptionMap->getPos() === $pos && in_array($word, $exceptionMap->getExceptionList()); }; $searchForFuncWithoutPos = function (ExceptionMap $exceptionMap) use($word) { return in_array($word, $exceptionMap->getExceptionList()); }; $found = []; if (!empty($pos)) { $found = array_filter($this->getWordnetCorpus()->getExceptionsMap(), $searchForFuncWithPos); } else { $found = array_filter($this->getWordnetCorpus()->getExceptionsMap(), $searchForFuncWithoutPos); } // found a match in the exceptions data if (!empty($found)) { return array_values($found)[0]->getTarget(); } foreach ($this->getMorphilogicalSubstitutions() as $keyPos => $keyValues) { foreach ($keyValues as $key => $value) { if (Text::endsWith($word, $key)) { $morphedWord = substr($word, 0, -strlen($key)) . $value; $r = $this->getLemma($morphedWord, $keyPos); if (!empty($r)) { $found += array_map(function ($lemma) { return $lemma->getWord(); }, $r); return $found[0]; } } } } if (empty($found)) { return ""; } return $found[0]; }