Source for file porter_en.php
Documentation is available at porter_en.php
* @package Joomla.Administrator
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* Porter English stemmer class for the Finder indexer package.
* This class was adapted from one written by Richard Heyes.
* See copyright and link information above.
* @package Joomla.Administrator
* Regex for matching a consonant.
private static $_regex_consonant =
'(?:[bcdfghjklmnpqrstvwxz]|(?<=[aeiou])y|^y)';
* Regex for matching a vowel
private static $_regex_vowel =
'(?:[aeiou]|(?<![aeiou])y)';
* Method to stem a token and return the root.
* @param string $token The token to stem.
* @param string $lang The language of the token.
* @return string The root token.
public function stem($token, $lang)
// Check if the token is long enough to merit stemming.
// Check if the language is English or All.
if ($lang !==
'en' &&
$lang !=
'*')
// Stem the token if it is not in the cache.
if (!isset
($this->cache[$lang][$token]))
$result =
self::_step1ab($result);
$result =
self::_step1c($result);
$result =
self::_step2($result);
$result =
self::_step3($result);
$result =
self::_step4($result);
$result =
self::_step5($result);
// Add the token to the cache.
$this->cache[$lang][$token] =
$result;
return $this->cache[$lang][$token];
* @param string $word The token to stem.
private static function _step1ab($word)
self::_replace($word, 'sses', 'ss')
or self::_replace($word, 'ies', 'i')
or self::_replace($word, 'ss', 'ss')
or self::_replace($word, 's', '');
if (substr($word, -
2, 1) !=
'e' or !self::_replace($word, 'eed', 'ee', 0))
$v =
self::$_regex_vowel;
// Words ending with ing and ed
// Note use of && and OR, for precedence reasons
if (preg_match("#$v+#", substr($word, 0, -
3)) &&
self::_replace($word, 'ing', '')
or preg_match("#$v+#", substr($word, 0, -
2)) &&
self::_replace($word, 'ed', ''))
// If one of above two test successful
if (!self::_replace($word, 'at', 'ate') and !self::_replace($word, 'bl', 'ble') and !self::_replace($word, 'iz', 'ize'))
// Double consonant ending
if (self::_doubleConsonant($word) and substr($word, -
2) !=
'll' and substr($word, -
2) !=
'ss' and substr($word, -
2) !=
'zz')
elseif (self::_m($word) ==
1 and self::_cvc($word))
* @param string $word The token to stem.
private static function _step1c($word)
$v =
self::$_regex_vowel;
self::_replace($word, 'y', 'i');
* @param string $word The token to stem.
private static function _step2($word)
self::_replace($word, 'ational', 'ate', 0)
or self::_replace($word, 'tional', 'tion', 0);
self::_replace($word, 'enci', 'ence', 0)
or self::_replace($word, 'anci', 'ance', 0);
self::_replace($word, 'izer', 'ize', 0);
self::_replace($word, 'logi', 'log', 0);
self::_replace($word, 'entli', 'ent', 0)
or self::_replace($word, 'ousli', 'ous', 0)
or self::_replace($word, 'alli', 'al', 0)
or self::_replace($word, 'bli', 'ble', 0)
or self::_replace($word, 'eli', 'e', 0);
self::_replace($word, 'ization', 'ize', 0)
or self::_replace($word, 'ation', 'ate', 0)
or self::_replace($word, 'ator', 'ate', 0);
self::_replace($word, 'iveness', 'ive', 0)
or self::_replace($word, 'fulness', 'ful', 0)
or self::_replace($word, 'ousness', 'ous', 0)
or self::_replace($word, 'alism', 'al', 0);
self::_replace($word, 'biliti', 'ble', 0)
or self::_replace($word, 'aliti', 'al', 0)
or self::_replace($word, 'iviti', 'ive', 0);
* @param string $word The token to stem.
private static function _step3($word)
self::_replace($word, 'ical', 'ic', 0);
self::_replace($word, 'ness', '', 0);
self::_replace($word, 'icate', 'ic', 0)
or self::_replace($word, 'iciti', 'ic', 0);
self::_replace($word, 'ful', '', 0);
self::_replace($word, 'ative', '', 0);
self::_replace($word, 'alize', 'al', 0);
* @param string $word The token to stem.
private static function _step4($word)
self::_replace($word, 'al', '', 1);
self::_replace($word, 'ance', '', 1)
or self::_replace($word, 'ence', '', 1);
self::_replace($word, 'er', '', 1);
self::_replace($word, 'ic', '', 1);
self::_replace($word, 'able', '', 1)
or self::_replace($word, 'ible', '', 1);
self::_replace($word, 'ant', '', 1)
or self::_replace($word, 'ement', '', 1)
or self::_replace($word, 'ment', '', 1)
or self::_replace($word, 'ent', '', 1);
if (substr($word, -
4) ==
'tion' or substr($word, -
4) ==
'sion')
self::_replace($word, 'ion', '', 1);
self::_replace($word, 'ou', '', 1);
self::_replace($word, 'ism', '', 1);
self::_replace($word, 'ate', '', 1)
or self::_replace($word, 'iti', '', 1);
self::_replace($word, 'ous', '', 1);
self::_replace($word, 'ive', '', 1);
self::_replace($word, 'ize', '', 1);
* @param string $word The token to stem.
private static function _step5($word)
if (self::_m(substr($word, 0, -
1)) >
1)
self::_replace($word, 'e', '');
elseif (self::_m(substr($word, 0, -
1)) ==
1)
if (!self::_cvc(substr($word, 0, -
1)))
self::_replace($word, 'e', '');
if (self::_m($word) >
1 and self::_doubleConsonant($word) and substr($word, -
1) ==
'l')
* 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 integer $m Optional minimum number of m() to meet
* @return boolean Whether the $check string was at the end
* of the $str string. True does not necessarily mean
private static function _replace(&$str, $check, $repl, $m =
null)
if (substr($str, $len) ==
$check)
$substr =
substr($str, 0, $len);
if (is_null($m) or self::_m($substr) >
$m)
* m() measures the number of consonant sequences in $str. if c is
* a consonant sequence and v a vowel sequence, and <..> indicates arbitrary
* @param string $str The string to return the m count for
* @return integer The m count
private static function _m($str)
$c =
self::$_regex_consonant;
$v =
self::$_regex_vowel;
$str =
preg_replace("#^$c+#", '', $str);
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
private static function _doubleConsonant($str)
$c =
self::$_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
private static function _cvc($str)
$c =
self::$_regex_consonant;
$v =
self::$_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';
Documentation generated on Tue, 19 Nov 2013 15:10:58 +0100 by phpDocumentor 1.4.3