$word) { if ($odd) { $words[$k] = Stem($word); } $odd = !$odd; } // Put it all back together return implode('', $words); } /** * Implementation of hook_help(). */ function porterstemmer_help($section = 'admin/help#search') { switch ($section) { case 'admin/modules#description': return t('Implements the Porter-Stemmer algorithm to improve English searching.'); } } /** * Regex for matching a consonant */ define('regex_consonant', '(?:[bcdfghjklmnpqrstvwxz]|(?<=[aeiou])y|^y)'); /** * Regex for matching a vowel */ define('regex_vowel', '(?:[aeiou]|(? 1) { replace($word, 'e', ''); } else if (m(substr($word, 0, -1)) == 1) { if (!cvc(substr($word, 0, -1))) { replace($word, 'e', ''); } } } // Part b if (m($word) > 1 AND doubleConsonant($word) AND substr($word, -1) == 'l') { $word = substr($word, 0, -1); } return $word; } /** * Replaces the first string with the second, at the end of the string. If third * arg is given, then the preceding string must match that m count at least. * * @param string $str String to check * @param string $check Ending to check for * @param string $repl Replacement string * @param int $m Optional minimum number of m() to meet * @return bool Whether the $check string was at the end * of the $str string. True does not necessarily mean * that it was replaced. */ 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; } /** * What, you mean it's not obvious from the name? * * m() measures the number of consonant sequences in $str. if c is * a consonant sequence and v a vowel sequence, and <..> indicates arbitrary * presence, * * gives 0 * vc gives 1 * vcvc gives 2 * vcvcvc gives 3 * * @param string $str The string to return the m count for * @return int The m count */ 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]); } /** * Returns true/false as to whether the given string contains two * of the same consonant next to each other at the end of the string. * * @param string $str String to check * @return bool Result */ function doubleConsonant($str) { $c = regex_consonant; return preg_match("#$c{2}$#", $str, $matches) AND $matches[0]{0} == $matches[0]{1}; } /** * Checks for ending CVC sequence where second C is not W, X or Y * * @param string $str String to check * @return bool Result */ 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'; } ?>