You are here

function snowball_stemmer_search_preprocess in Snowball Stemmer 8

Same name and namespace in other branches
  1. 2.x snowball_stemmer.module \snowball_stemmer_search_preprocess()

Implements hook_search_preprocess().

1 call to snowball_stemmer_search_preprocess()
CoreSearchTest::testSearchStemming in tests/src/Kernel/CoreSearchTest.php
Tests the hook stemming alone.

File

./snowball_stemmer.module, line 29
Core hooks for Snowball Stemmer.

Code

function snowball_stemmer_search_preprocess($text, $langcode = NULL) {

  // If the language is not set, get it from the language manager.
  if (!isset($langcode)) {
    $langcode = \Drupal::languageManager()
      ->getCurrentLanguage()
      ->getId();
  }
  $stemmer = \Drupal::service('snowball_stemmer.stemmer');
  if ($stemmer
    ->setLanguage($langcode)) {

    // Core's tokenization occurs after this hook. It's HTML processing and
    // removal has already happened. We need to separate out
    // words to stem, but then return into context for tokenizing.
    $words = preg_split('/([^\\p{L}\\p{N}]+)+/u', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
    $stemmed = [];
    $is_word = !preg_match('/[^\\p{L}\\p{N}]/u', $words[0]);
    foreach ($words as $key => $word) {
      if ($is_word && strlen($word)) {
        $words[$key] = $stemmer
          ->stem($word);
      }
      $is_word = !$is_word;
    }
    $text = implode('', $words);
  }
  return $text;
}