Пример #1
0
 /**
  * Searches the current stem for the longest suffix and performs the 
  * supplied closure on it if found. Supplied suffixes should be of the
  * form:
  * 
  * array("suffix" => function($word){ ... })
  *
  * @param string $word 
  * @param array $suffixes 
  * @param boolean $onlyIfInR1 
  * @param boolean $onlyIfInR2 
  * @return string
  * @author John Anderson
  */
 protected function performOnLongestSuffix($word, $suffixes = array(), $onlyIfInR1 = false, $onlyIfInR2 = false)
 {
     $maxMatch = 0;
     foreach ($suffixes as $suffix => $replacement) {
         $suffixLength = strlen($suffix);
         if (substr($word, -$suffixLength) === $suffix && $suffixLength > $maxMatch) {
             $matchedSuffix = $suffix;
             $matchedReplacement = $replacement;
             $maxMatch = $suffixLength;
         }
     }
     if (isset($matchedSuffix) && $matchedSuffix) {
         if ($onlyIfInR1) {
             if (strstr(Stemmer::getR1($word), $matchedSuffix) !== false) {
                 return $matchedReplacement($word);
             } else {
                 return $word;
             }
         } else {
             if ($onlyIfInR2) {
                 if (strstr(Stemmer::getR2($word), $matchedSuffix) !== false) {
                     return $matchedReplacement($word);
                 } else {
                     return $word;
                 }
             } else {
                 return $matchedReplacement($word);
             }
         }
     }
     return $word;
 }