You are here

function porterstemmer_search_preprocess in Porter-Stemmer 7

Same name and namespace in other branches
  1. 8 porterstemmer.module \porterstemmer_search_preprocess()
  2. 5 porterstemmer.module \porterstemmer_search_preprocess()
  3. 6.2 porterstemmer.module \porterstemmer_search_preprocess()
  4. 6 porterstemmer.module \porterstemmer_search_preprocess()

Implements hook_search_preprocess().

Stems the words in $text, using the Porter Stemmer 2 algorithm.

File

./porterstemmer.module, line 23
Porter 2 Stemming for Drupal.

Code

function porterstemmer_search_preprocess($text) {

  // Convert text to lower case, and replace special apostrophes with regular
  // apostrophes.
  $text = drupal_strtolower(str_replace('’', "'", $text));

  // Split into words.
  $words = preg_split('/(' . PORTERSTEMMER_BOUNDARY . '+)/', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
  if (!count($words)) {
    return $text;
  }
  $has_pecl_stem = _porterstemmer_pecl_loaded();
  if (!$has_pecl_stem) {
    module_load_include('inc', 'porterstemmer', 'includes/standard-stemmer');
  }

  // Process each word, skipping delimiters.
  $isword = !preg_match('/' . PORTERSTEMMER_BOUNDARY . '/', $words[0]);
  foreach ($words as $k => $word) {
    if ($isword) {
      if ($has_pecl_stem) {
        $words[$k] = stem_english($word);
      }
      else {
        $words[$k] = porterstemmer_stem($word);
      }
    }
    $isword = !$isword;
  }

  // Put it all back together (note that delimiters are in $words).
  return implode('', $words);
}