Path: blob/master/externals/porter-stemmer/src/Porter.php
12241 views
<?php12# vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4:34/**5* Copyright (c) 2005-2016 Richard Heyes (http://www.phpguru.org/)6*7* Portions Copyright 2003-2007 Jon Abernathy <[email protected]>8*9* Originally available under the GPL 2 or greater. Relicensed with permission10* of original authors under the MIT License in 2016.11*12* All rights reserved.13*14* @package PorterStemmer15* @author Richard Heyes16* @author Jon Abernathy <[email protected]>17* @copyright 2005-2016 Richard Heyes (http://www.phpguru.org/)18* @license http://www.opensource.org/licenses/mit-license.html MIT License19*/2021/**22* PHP 5 Implementation of the Porter Stemmer algorithm. Certain elements23* were borrowed from the (broken) implementation by Jon Abernathy.24*25* See http://tartarus.org/~martin/PorterStemmer/ for a description of the26* algorithm.27*28* Usage:29*30* $stem = PorterStemmer::Stem($word);31*32* How easy is that?33*34* @package PorterStemmer35* @author Richard Heyes36* @author Jon Abernathy <[email protected]>37* @copyright 2005-2016 Richard Heyes (http://www.phpguru.org/)38* @license http://www.opensource.org/licenses/mit-license.html MIT License39*/40class Porter41{42/**43* Regex for matching a consonant44*45* @var string46*/47private static $regex_consonant = '(?:[bcdfghjklmnpqrstvwxz]|(?<=[aeiou])y|^y)';4849/**50* Regex for matching a vowel51*52* @var string53*/54private static $regex_vowel = '(?:[aeiou]|(?<![aeiou])y)';5556/**57* Stems a word. Simple huh?58*59* @param string $word Word to stem60*61* @return string Stemmed word62*/63public static function Stem($word)64{65if (strlen($word) <= 2) {66return $word;67}6869$word = self::step1ab($word);70$word = self::step1c($word);71$word = self::step2($word);72$word = self::step3($word);73$word = self::step4($word);74$word = self::step5($word);7576return $word;77}7879/**80* Step 181*/82private static function step1ab($word)83{84// Part a85if (substr($word, -1) == 's') {8687self::replace($word, 'sses', 'ss')88OR self::replace($word, 'ies', 'i')89OR self::replace($word, 'ss', 'ss')90OR self::replace($word, 's', '');91}9293// Part b94if (substr($word, -2, 1) != 'e' OR !self::replace($word, 'eed', 'ee', 0)) { // First rule95$v = self::$regex_vowel;9697// ing and ed98if ( preg_match("#$v+#", substr($word, 0, -3)) && self::replace($word, 'ing', '')99OR preg_match("#$v+#", substr($word, 0, -2)) && self::replace($word, 'ed', '')) { // Note use of && and OR, for precedence reasons100101// If one of above two test successful102if ( !self::replace($word, 'at', 'ate')103AND !self::replace($word, 'bl', 'ble')104AND !self::replace($word, 'iz', 'ize')) {105106// Double consonant ending107if ( self::doubleConsonant($word)108AND substr($word, -2) != 'll'109AND substr($word, -2) != 'ss'110AND substr($word, -2) != 'zz') {111112$word = substr($word, 0, -1);113114} elseif (self::m($word) == 1 AND self::cvc($word)) {115$word .= 'e';116}117}118}119}120121return $word;122}123124/**125* Step 1c126*127* @param string $word Word to stem128*/129private static function step1c($word)130{131$v = self::$regex_vowel;132133if (substr($word, -1) == 'y' && preg_match("#$v+#", substr($word, 0, -1))) {134self::replace($word, 'y', 'i');135}136137return $word;138}139140/**141* Step 2142*143* @param string $word Word to stem144*/145private static function step2($word)146{147switch (substr($word, -2, 1)) {148case 'a':149self::replace($word, 'ational', 'ate', 0)150OR self::replace($word, 'tional', 'tion', 0);151break;152153case 'c':154self::replace($word, 'enci', 'ence', 0)155OR self::replace($word, 'anci', 'ance', 0);156break;157158case 'e':159self::replace($word, 'izer', 'ize', 0);160break;161162case 'g':163self::replace($word, 'logi', 'log', 0);164break;165166case 'l':167self::replace($word, 'entli', 'ent', 0)168OR self::replace($word, 'ousli', 'ous', 0)169OR self::replace($word, 'alli', 'al', 0)170OR self::replace($word, 'bli', 'ble', 0)171OR self::replace($word, 'eli', 'e', 0);172break;173174case 'o':175self::replace($word, 'ization', 'ize', 0)176OR self::replace($word, 'ation', 'ate', 0)177OR self::replace($word, 'ator', 'ate', 0);178break;179180case 's':181self::replace($word, 'iveness', 'ive', 0)182OR self::replace($word, 'fulness', 'ful', 0)183OR self::replace($word, 'ousness', 'ous', 0)184OR self::replace($word, 'alism', 'al', 0);185break;186187case 't':188self::replace($word, 'biliti', 'ble', 0)189OR self::replace($word, 'aliti', 'al', 0)190OR self::replace($word, 'iviti', 'ive', 0);191break;192}193194return $word;195}196197/**198* Step 3199*200* @param string $word String to stem201*/202private static function step3($word)203{204switch (substr($word, -2, 1)) {205case 'a':206self::replace($word, 'ical', 'ic', 0);207break;208209case 's':210self::replace($word, 'ness', '', 0);211break;212213case 't':214self::replace($word, 'icate', 'ic', 0)215OR self::replace($word, 'iciti', 'ic', 0);216break;217218case 'u':219self::replace($word, 'ful', '', 0);220break;221222case 'v':223self::replace($word, 'ative', '', 0);224break;225226case 'z':227self::replace($word, 'alize', 'al', 0);228break;229}230231return $word;232}233234/**235* Step 4236*237* @param string $word Word to stem238*/239private static function step4($word)240{241switch (substr($word, -2, 1)) {242case 'a':243self::replace($word, 'al', '', 1);244break;245246case 'c':247self::replace($word, 'ance', '', 1)248OR self::replace($word, 'ence', '', 1);249break;250251case 'e':252self::replace($word, 'er', '', 1);253break;254255case 'i':256self::replace($word, 'ic', '', 1);257break;258259case 'l':260self::replace($word, 'able', '', 1)261OR self::replace($word, 'ible', '', 1);262break;263264case 'n':265self::replace($word, 'ant', '', 1)266OR self::replace($word, 'ement', '', 1)267OR self::replace($word, 'ment', '', 1)268OR self::replace($word, 'ent', '', 1);269break;270271case 'o':272if (substr($word, -4) == 'tion' OR substr($word, -4) == 'sion') {273self::replace($word, 'ion', '', 1);274} else {275self::replace($word, 'ou', '', 1);276}277break;278279case 's':280self::replace($word, 'ism', '', 1);281break;282283case 't':284self::replace($word, 'ate', '', 1)285OR self::replace($word, 'iti', '', 1);286break;287288case 'u':289self::replace($word, 'ous', '', 1);290break;291292case 'v':293self::replace($word, 'ive', '', 1);294break;295296case 'z':297self::replace($word, 'ize', '', 1);298break;299}300301return $word;302}303304/**305* Step 5306*307* @param string $word Word to stem308*/309private static function step5($word)310{311// Part a312if (substr($word, -1) == 'e') {313if (self::m(substr($word, 0, -1)) > 1) {314self::replace($word, 'e', '');315316} elseif (self::m(substr($word, 0, -1)) == 1) {317318if (!self::cvc(substr($word, 0, -1))) {319self::replace($word, 'e', '');320}321}322}323324// Part b325if (self::m($word) > 1 AND self::doubleConsonant($word) AND substr($word, -1) == 'l') {326$word = substr($word, 0, -1);327}328329return $word;330}331332/**333* Replaces the first string with the second, at the end of the string334*335* If third arg is given, then the preceding string must match that m336* count at least.337*338* @param string $str String to check339* @param string $check Ending to check for340* @param string $repl Replacement string341* @param int $m Optional minimum number of m() to meet342*343* @return bool Whether the $check string was at the end of the $str344* string. True does not necessarily mean that it was345* replaced.346*/347private static function replace(&$str, $check, $repl, $m = null)348{349$len = 0 - strlen($check);350351if (substr($str, $len) == $check) {352$substr = substr($str, 0, $len);353if (is_null($m) OR self::m($substr) > $m) {354$str = $substr . $repl;355}356357return true;358}359360return false;361}362363/**364* What, you mean it's not obvious from the name?365*366* m() measures the number of consonant sequences in $str. if c is367* a consonant sequence and v a vowel sequence, and <..> indicates arbitrary368* presence,369*370* <c><v> gives 0371* <c>vc<v> gives 1372* <c>vcvc<v> gives 2373* <c>vcvcvc<v> gives 3374*375* @param string $str The string to return the m count for376*377* @return int The m count378*/379private static function m($str)380{381$c = self::$regex_consonant;382$v = self::$regex_vowel;383384$str = preg_replace("#^$c+#", '', $str);385$str = preg_replace("#$v+$#", '', $str);386387preg_match_all("#($v+$c+)#", $str, $matches);388389return count($matches[1]);390}391392/**393* Returns true/false as to whether the given string contains two394* of the same consonant next to each other at the end of the string.395*396* @param string $str String to check397*398* @return bool Result399*/400private static function doubleConsonant($str)401{402$c = self::$regex_consonant;403404return preg_match("#$c{2}$#", $str, $matches) AND $matches[0][0] == $matches[0][1];405}406407/**408* Checks for ending CVC sequence where second C is not W, X or Y409*410* @param string $str String to check411*412* @return bool Result413*/414private static function cvc($str)415{416$c = self::$regex_consonant;417$v = self::$regex_vowel;418419return preg_match("#($c$v$c)$#", $str, $matches)420AND strlen($matches[1]) == 3421AND $matches[1][2] != 'w'422AND $matches[1][2] != 'x'423AND $matches[1][2] != 'y';424}425}426427428