function porterstemmer_search_preprocess in Porter-Stemmer 8
Same name and namespace in other branches
- 5 porterstemmer.module \porterstemmer_search_preprocess()
- 6.2 porterstemmer.module \porterstemmer_search_preprocess()
- 6 porterstemmer.module \porterstemmer_search_preprocess()
- 7 porterstemmer.module \porterstemmer_search_preprocess()
Implements hook_search_preprocess().
Stems the words in $text, using the Porter 2 (English) stemming algorithm.
File
- ./
porterstemmer.module, line 39 - Contains porterstemmer.module.
Code
function porterstemmer_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();
}
if ($langcode == 'en') {
// Convert text to lower case, and replace special apostrophes with regular
// apostrophes.
$text = 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();
// 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] = Porter2::stem($word);
}
}
$isword = !$isword;
}
// Put it all back together (note that delimiters are in $words).
return implode('', $words);
}
return $text;
}