View source
<?php
function porterstemmer_search_preprocess(&$text) {
$words = preg_split('/([^a-zA-Z]+)/', str_replace("'", '', $text), -1, PREG_SPLIT_DELIM_CAPTURE);
$odd = true;
foreach ($words as $k => $word) {
if ($odd) {
$words[$k] = Stem($word);
}
$odd = !$odd;
}
return implode('', $words);
}
function porterstemmer_help($section = 'admin/help#search') {
switch ($section) {
case 'admin/modules#description':
return t('Implements the Porter-Stemmer algorithm to improve English searching.');
}
}
define('regex_consonant', '(?:[bcdfghjklmnpqrstvwxz]|(?<=[aeiou])y|^y)');
define('regex_vowel', '(?:[aeiou]|(?<![aeiou])y)');
function stem($word) {
if (strlen($word) <= 2) {
return $word;
}
$word = step1ab($word);
$word = step1c($word);
$word = step2($word);
$word = step3($word);
$word = step4($word);
$word = step5($word);
return $word;
}
function step1ab($word) {
if (substr($word, -1) == 's') {
replace($word, 'sses', 'ss') or replace($word, 'ies', 'i') or replace($word, 'ss', 'ss') or replace($word, 's', '');
}
if (substr($word, -2, 1) != 'e' or !replace($word, 'eed', 'ee', 0)) {
$v = regex_vowel;
if (preg_match("#{$v}+#", substr($word, 0, -3)) && replace($word, 'ing', '') or preg_match("#{$v}+#", substr($word, 0, -2)) && replace($word, 'ed', '')) {
if (!replace($word, 'at', 'ate') and !replace($word, 'bl', 'ble') and !replace($word, 'iz', 'ize')) {
if (doubleConsonant($word) and substr($word, -2) != 'll' and substr($word, -2) != 'ss' and substr($word, -2) != 'zz') {
$word = substr($word, 0, -1);
}
else {
if (m($word) == 1 and cvc($word)) {
$word .= 'e';
}
}
}
}
}
return $word;
}
function step1c($word) {
$v = regex_vowel;
if (substr($word, -1) == 'y' && preg_match("#{$v}+#", substr($word, 0, -1))) {
replace($word, 'y', 'i');
}
return $word;
}
function step2($word) {
switch (substr($word, -2, 1)) {
case 'a':
replace($word, 'ational', 'ate', 0) or replace($word, 'tional', 'tion', 0);
break;
case 'c':
replace($word, 'enci', 'ence', 0) or replace($word, 'anci', 'ance', 0);
break;
case 'e':
replace($word, 'izer', 'ize', 0);
break;
case 'g':
replace($word, 'logi', 'log', 0);
break;
case 'l':
replace($word, 'entli', 'ent', 0) or replace($word, 'ousli', 'ous', 0) or replace($word, 'alli', 'al', 0) or replace($word, 'bli', 'ble', 0) or replace($word, 'eli', 'e', 0);
break;
case 'o':
replace($word, 'ization', 'ize', 0) or replace($word, 'ation', 'ate', 0) or replace($word, 'ator', 'ate', 0);
break;
case 's':
replace($word, 'iveness', 'ive', 0) or replace($word, 'fulness', 'ful', 0) or replace($word, 'ousness', 'ous', 0) or replace($word, 'alism', 'al', 0);
break;
case 't':
replace($word, 'biliti', 'ble', 0) or replace($word, 'aliti', 'al', 0) or replace($word, 'iviti', 'ive', 0);
break;
}
return $word;
}
function step3($word) {
switch (substr($word, -2, 1)) {
case 'a':
replace($word, 'ical', 'ic', 0);
break;
case 's':
replace($word, 'ness', '', 0);
break;
case 't':
replace($word, 'icate', 'ic', 0) or replace($word, 'iciti', 'ic', 0);
break;
case 'u':
replace($word, 'ful', '', 0);
break;
case 'v':
replace($word, 'ative', '', 0);
break;
case 'z':
replace($word, 'alize', 'al', 0);
break;
}
return $word;
}
function step4($word) {
switch (substr($word, -2, 1)) {
case 'a':
replace($word, 'al', '', 1);
break;
case 'c':
replace($word, 'ance', '', 1) or replace($word, 'ence', '', 1);
break;
case 'e':
replace($word, 'er', '', 1);
break;
case 'i':
replace($word, 'ic', '', 1);
break;
case 'l':
replace($word, 'able', '', 1) or replace($word, 'ible', '', 1);
break;
case 'n':
replace($word, 'ant', '', 1) or replace($word, 'ement', '', 1) or replace($word, 'ment', '', 1) or replace($word, 'ent', '', 1);
break;
case 'o':
if (substr($word, -4) == 'tion' or substr($word, -4) == 'sion') {
replace($word, 'ion', '', 1);
}
else {
replace($word, 'ou', '', 1);
}
break;
case 's':
replace($word, 'ism', '', 1);
break;
case 't':
replace($word, 'ate', '', 1) or replace($word, 'iti', '', 1);
break;
case 'u':
replace($word, 'ous', '', 1);
break;
case 'v':
replace($word, 'ive', '', 1);
break;
case 'z':
replace($word, 'ize', '', 1);
break;
}
return $word;
}
function step5($word) {
if (substr($word, -1) == 'e') {
if (m(substr($word, 0, -1)) > 1) {
replace($word, 'e', '');
}
else {
if (m(substr($word, 0, -1)) == 1) {
if (!cvc(substr($word, 0, -1))) {
replace($word, 'e', '');
}
}
}
}
if (m($word) > 1 and doubleConsonant($word) and substr($word, -1) == 'l') {
$word = substr($word, 0, -1);
}
return $word;
}
function replace(&$str, $check, $repl, $m = null) {
$len = 0 - strlen($check);
if (substr($str, $len) == $check) {
$substr = substr($str, 0, $len);
if (is_null($m) or m($substr) > $m) {
$str = $substr . $repl;
}
return true;
}
return false;
}
function m($str) {
$c = regex_consonant;
$v = regex_vowel;
$str = preg_replace("#^{$c}+#", '', $str);
$str = preg_replace("#{$v}+\$#", '', $str);
preg_match_all("#({$v}+{$c}+)#", $str, $matches);
return count($matches[1]);
}
function doubleConsonant($str) {
$c = regex_consonant;
return preg_match("#{$c}{2}\$#", $str, $matches) and $matches[0][0] == $matches[0][1];
}
function cvc($str) {
$c = regex_consonant;
$v = regex_vowel;
return preg_match("#({$c}{$v}{$c})\$#", $str, $matches) and strlen($matches[1]) == 3 and $matches[1][2] != 'w' and $matches[1][2] != 'x' and $matches[1][2] != 'y';
}