You are here

class Stemmer in Snowball Stemmer 8

Same name and namespace in other branches
  1. 2.x src/Stemmer.php \Drupal\snowball_stemmer\Stemmer

Service wrapper class for stemmer.

Hierarchy

  • class \Drupal\snowball_stemmer\Stemmer

Expanded class hierarchy of Stemmer

2 files declare their use of Stemmer
SnowballStemmer.php in src/Plugin/search_api/processor/SnowballStemmer.php
StemmerTest.php in tests/src/Unit/StemmerTest.php
1 string reference to 'Stemmer'
snowball_stemmer.services.yml in ./snowball_stemmer.services.yml
snowball_stemmer.services.yml
1 service uses Stemmer
snowball_stemmer.stemmer in ./snowball_stemmer.services.yml
Drupal\snowball_stemmer\Stemmer

File

src/Stemmer.php, line 10

Namespace

Drupal\snowball_stemmer
View source
class Stemmer {

  /**
   * Language stemmer classes.
   *
   * @var \Wamania\Snowball\Stem[]
   */
  protected $stemmers = [];

  /**
   * Current stemming language.
   *
   * @var string
   */
  protected $language;

  /**
   * Temporary storage for lookups.
   *
   * @var string[]
   */
  protected $cache = [];

  /**
   * Language keyed array of overridden word arrays.
   *
   * @var array
   */
  protected $overrides = [];

  /**
   * Set language.
   *
   * @param string $language
   *   Two character language code.
   *
   * @return bool
   *   True if able to set, false if not supported.
   */
  public function setLanguage($language) {
    $supported = [
      'da' => 'Danish',
      'nl' => 'Dutch',
      'en' => 'English',
      'fr' => 'French',
      'de' => 'German',
      'it' => 'Italian',
      'no' => 'Norwegian',
      'pt' => 'Portuguese',
      'ro' => 'Romanian',
      'ru' => 'Russian',
      'es' => 'Spanish',
      'sv' => 'Swedish',
    ];
    if (isset($supported[$language])) {
      $this->language = $language;
      if (empty($this->stemmers[$language])) {
        $class = 'Wamania\\Snowball\\' . $supported[$language];
        $this->stemmers[$language] = new $class();
      }
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Set overridden strings not to be automatically stemmed and return values.
   *
   * @param array $overrides
   *   Array with key as overridden string and 'stemmed' return string as value.
   * @param string $language
   *   Language code. Default will apply to all languages, but an override with
   *   language will take preference.
   */
  public function setOverrides(array $overrides, $language = LanguageInterface::LANGCODE_NOT_SPECIFIED) {
    $this->overrides[$language] = $overrides;
  }

  /**
   * Check for an overridden string.
   *
   * @param string $word
   *   Word to be checked.
   *
   * @return bool|string
   *   The value to use if overridden. FALSE if not an exception.
   */
  public function hasOverride($word) {
    if (isset($this->overrides[$this->language][$word])) {
      return $this->overrides[$this->language][$word];
    }
    if (isset($this->overrides[LanguageInterface::LANGCODE_NOT_SPECIFIED][$word])) {
      return $this->overrides[LanguageInterface::LANGCODE_NOT_SPECIFIED][$word];
    }
    return FALSE;
  }

  /**
   * Stem a word.
   *
   * @param string $word
   *   Word to stem.
   *
   * @return string
   *   Stemmed word.
   *
   * @throws \Drupal\snowball_stemmer\LanguageNotSetException
   *   If the language has not been set with self::setLanguage().
   */
  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];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Stemmer::$cache protected property Temporary storage for lookups.
Stemmer::$language protected property Current stemming language.
Stemmer::$overrides protected property Language keyed array of overridden word arrays.
Stemmer::$stemmers protected property Language stemmer classes.
Stemmer::hasOverride public function Check for an overridden string.
Stemmer::setLanguage public function Set language.
Stemmer::setOverrides public function Set overridden strings not to be automatically stemmed and return values.
Stemmer::stem public function Stem a word.