Source for file class.porterstemmer.php
Documentation is available at class.porterstemmer.php
* Copyright (c) 2005 Richard Heyes (http://www.phpguru.org/)
* This script is free software.
* PHP5 Implementation of the Porter Stemmer algorithm. Certain elements
* were borrowed from the (broken) implementation by Jon Abernathy.
* $stem = PorterStemmer::Stem($word);
* 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)';
* Stems a word. Simple huh?
* @param string $word Word to stem
* @return string Stemmed word
public static function Stem($word)
$word =
self::step1ab($word);
$word =
self::step1c($word);
$word =
self::step2($word);
$word =
self::step3($word);
$word =
self::step4($word);
$word =
self::step5($word);
private static function step1ab($word)
if (substr($word, -
1) ==
's') {
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)) { // First rule
if ( preg_match("#$v+#", substr($word, 0, -
3)) &&
self::replace($word, 'ing', '')
OR preg_match("#$v+#", substr($word, 0, -
2)) &&
self::replace($word, 'ed', '')) { // Note use of && and OR, for precedence reasons
// 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) !=
'zz') {
} else if (self::m($word) ==
1 AND self::cvc($word)) {
* @param string $word Word to stem
private static function step1c($word)
self::replace($word, 'y', 'i');
* @param string $word Word to stem
private static function step2($word)
switch (substr($word, -
2, 1)) {
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 String to stem
private static function step3($word)
switch (substr($word, -
2, 1)) {
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 Word to stem
private static function step4($word)
switch (substr($word, -
2, 1)) {
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 Word to stem
private static function step5($word)
if (substr($word, -
1) ==
'e') {
if (self::m(substr($word, 0, -
1)) >
1) {
self::replace($word, 'e', '');
} else if (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 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
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) {
* 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
* @param string $str The string to return the m count for
* @return int The m count
private static function m($str)
$c =
self::$regex_consonant;
$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
$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;
return preg_match("#($c$v$c)$#", $str, $matches)
AND $matches[1]{2} !=
'w'
AND $matches[1]{2} !=
'x'
AND $matches[1]{2} !=
'y';
Documentation generated on Sun, 13 Dec 2009 19:39:33 +0000 by phpDocumentor 1.4.3