You are here

public function Stemmer::stem in Snowball Stemmer 2.x

Same name and namespace in other branches
  1. 8 src/Stemmer.php \Drupal\snowball_stemmer\Stemmer::stem()

Stem a word.

Parameters

string $word: Word to stem.

Return value

string Stemmed word.

Throws

\Drupal\snowball_stemmer\LanguageNotSetException If the language has not been set with self::setLanguage().

File

src/Stemmer.php, line 108

Class

Stemmer
Service wrapper class for stemmer.

Namespace

Drupal\snowball_stemmer

Code

public function stem($word) {
  if (empty($this->language)) {
    throw new LanguageNotSetException('Stemmer has no language set.');
  }
  if ($override = $this
    ->hasOverride($word)) {
    return $override;
  }
  if (!isset($this->cache[$this->language][$word])) {
    try {
      $this->cache[$this->language][$word] = $this->stemmers[$this->language]
        ->stem($word);
    } catch (\Exception $e) {

      // Class throws a standard exception if the string is not UTF8.
      watchdog_exception('snowball_stemmer', $e);

      // Be nice, at least leave the word alone.
      $this->cache[$this->language][$word] = $word;
    }
  }
  return $this->cache[$this->language][$word];
}