/web/concrete/libraries/3rdparty/StandardAnalyzer/TokenFilter/EnglishStemmer/PorterStemmer.php

https://github.com/shin2/concrete5 · PHP · 411 lines · 228 code · 76 blank · 107 comment · 40 complexity · b32a6a997a4928407a6bcfcdb7f303df MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright (c) 2005 Richard Heyes (http://www.phpguru.org/)
  4. *
  5. * All rights reserved.
  6. *
  7. * This script is free software.
  8. */
  9. /**
  10. * PHP5 Implementation of the Porter Stemmer algorithm. Certain elements
  11. * were borrowed from the (broken) implementation by Jon Abernathy.
  12. *
  13. * Usage:
  14. *
  15. * $stem = PorterStemmer::Stem($word);
  16. *
  17. * How easy is that?
  18. */
  19. // Porter Stemmer
  20. class PorterStemmer
  21. {
  22. /**
  23. * Regex for matching a consonant
  24. * @var string
  25. */
  26. private static $regex_consonant = '(?:[bcdfghjklmnpqrstvwxz]|(?<=[aeiou])y|^y)';
  27. /**
  28. * Regex for matching a vowel
  29. * @var string
  30. */
  31. private static $regex_vowel = '(?:[aeiou]|(?<![aeiou])y)';
  32. /**
  33. * Stems a word. Simple huh?
  34. *
  35. * @param string $word Word to stem
  36. * @return string Stemmed word
  37. */
  38. public static function Stem($word)
  39. {
  40. if (strlen($word) <= 2) {
  41. return $word;
  42. }
  43. $word = self::step1ab($word);
  44. $word = self::step1c($word);
  45. $word = self::step2($word);
  46. $word = self::step3($word);
  47. $word = self::step4($word);
  48. $word = self::step5($word);
  49. return $word;
  50. }
  51. /**
  52. * Step 1
  53. */
  54. private static function step1ab($word)
  55. {
  56. // Part a
  57. if (substr($word, -1) == 's') {
  58. self::replace($word, 'sses', 'ss')
  59. OR self::replace($word, 'ies', 'i')
  60. OR self::replace($word, 'ss', 'ss')
  61. OR self::replace($word, 's', '');
  62. }
  63. // Part b
  64. if (substr($word, -2, 1) != 'e' OR !self::replace($word, 'eed', 'ee', 0)) { // First rule
  65. $v = self::$regex_vowel;
  66. // ing and ed
  67. if ( preg_match("#$v+#", substr($word, 0, -3)) && self::replace($word, 'ing', '')
  68. OR preg_match("#$v+#", substr($word, 0, -2)) && self::replace($word, 'ed', '')) { // Note use of && and OR, for precedence reasons
  69. // If one of above two test successful
  70. if ( !self::replace($word, 'at', 'ate')
  71. AND !self::replace($word, 'bl', 'ble')
  72. AND !self::replace($word, 'iz', 'ize')) {
  73. // Double consonant ending
  74. if ( self::doubleConsonant($word)
  75. AND substr($word, -2) != 'll'
  76. AND substr($word, -2) != 'ss'
  77. AND substr($word, -2) != 'zz') {
  78. $word = substr($word, 0, -1);
  79. } else if (self::m($word) == 1 AND self::cvc($word)) {
  80. $word .= 'e';
  81. }
  82. }
  83. }
  84. }
  85. return $word;
  86. }
  87. /**
  88. * Step 1c
  89. *
  90. * @param string $word Word to stem
  91. */
  92. private static function step1c($word)
  93. {
  94. $v = self::$regex_vowel;
  95. if (substr($word, -1) == 'y' && preg_match("#$v+#", substr($word, 0, -1))) {
  96. self::replace($word, 'y', 'i');
  97. }
  98. return $word;
  99. }
  100. /**
  101. * Step 2
  102. *
  103. * @param string $word Word to stem
  104. */
  105. private static function step2($word)
  106. {
  107. switch (substr($word, -2, 1)) {
  108. case 'a':
  109. self::replace($word, 'ational', 'ate', 0)
  110. OR self::replace($word, 'tional', 'tion', 0);
  111. break;
  112. case 'c':
  113. self::replace($word, 'enci', 'ence', 0)
  114. OR self::replace($word, 'anci', 'ance', 0);
  115. break;
  116. case 'e':
  117. self::replace($word, 'izer', 'ize', 0);
  118. break;
  119. case 'g':
  120. self::replace($word, 'logi', 'log', 0);
  121. break;
  122. case 'l':
  123. self::replace($word, 'entli', 'ent', 0)
  124. OR self::replace($word, 'ousli', 'ous', 0)
  125. OR self::replace($word, 'alli', 'al', 0)
  126. OR self::replace($word, 'bli', 'ble', 0)
  127. OR self::replace($word, 'eli', 'e', 0);
  128. break;
  129. case 'o':
  130. self::replace($word, 'ization', 'ize', 0)
  131. OR self::replace($word, 'ation', 'ate', 0)
  132. OR self::replace($word, 'ator', 'ate', 0);
  133. break;
  134. case 's':
  135. self::replace($word, 'iveness', 'ive', 0)
  136. OR self::replace($word, 'fulness', 'ful', 0)
  137. OR self::replace($word, 'ousness', 'ous', 0)
  138. OR self::replace($word, 'alism', 'al', 0);
  139. break;
  140. case 't':
  141. self::replace($word, 'biliti', 'ble', 0)
  142. OR self::replace($word, 'aliti', 'al', 0)
  143. OR self::replace($word, 'iviti', 'ive', 0);
  144. break;
  145. }
  146. return $word;
  147. }
  148. /**
  149. * Step 3
  150. *
  151. * @param string $word String to stem
  152. */
  153. private static function step3($word)
  154. {
  155. switch (substr($word, -2, 1)) {
  156. case 'a':
  157. self::replace($word, 'ical', 'ic', 0);
  158. break;
  159. case 's':
  160. self::replace($word, 'ness', '', 0);
  161. break;
  162. case 't':
  163. self::replace($word, 'icate', 'ic', 0)
  164. OR self::replace($word, 'iciti', 'ic', 0);
  165. break;
  166. case 'u':
  167. self::replace($word, 'ful', '', 0);
  168. break;
  169. case 'v':
  170. self::replace($word, 'ative', '', 0);
  171. break;
  172. case 'z':
  173. self::replace($word, 'alize', 'al', 0);
  174. break;
  175. }
  176. return $word;
  177. }
  178. /**
  179. * Step 4
  180. *
  181. * @param string $word Word to stem
  182. */
  183. private static function step4($word)
  184. {
  185. switch (substr($word, -2, 1)) {
  186. case 'a':
  187. self::replace($word, 'al', '', 1);
  188. break;
  189. case 'c':
  190. self::replace($word, 'ance', '', 1)
  191. OR self::replace($word, 'ence', '', 1);
  192. break;
  193. case 'e':
  194. self::replace($word, 'er', '', 1);
  195. break;
  196. case 'i':
  197. self::replace($word, 'ic', '', 1);
  198. break;
  199. case 'l':
  200. self::replace($word, 'able', '', 1)
  201. OR self::replace($word, 'ible', '', 1);
  202. break;
  203. case 'n':
  204. self::replace($word, 'ant', '', 1)
  205. OR self::replace($word, 'ement', '', 1)
  206. OR self::replace($word, 'ment', '', 1)
  207. OR self::replace($word, 'ent', '', 1);
  208. break;
  209. case 'o':
  210. if (substr($word, -4) == 'tion' OR substr($word, -4) == 'sion') {
  211. self::replace($word, 'ion', '', 1);
  212. } else {
  213. self::replace($word, 'ou', '', 1);
  214. }
  215. break;
  216. case 's':
  217. self::replace($word, 'ism', '', 1);
  218. break;
  219. case 't':
  220. self::replace($word, 'ate', '', 1)
  221. OR self::replace($word, 'iti', '', 1);
  222. break;
  223. case 'u':
  224. self::replace($word, 'ous', '', 1);
  225. break;
  226. case 'v':
  227. self::replace($word, 'ive', '', 1);
  228. break;
  229. case 'z':
  230. self::replace($word, 'ize', '', 1);
  231. break;
  232. }
  233. return $word;
  234. }
  235. /**
  236. * Step 5
  237. *
  238. * @param string $word Word to stem
  239. */
  240. private static function step5($word)
  241. {
  242. // Part a
  243. if (substr($word, -1) == 'e') {
  244. if (self::m(substr($word, 0, -1)) > 1) {
  245. self::replace($word, 'e', '');
  246. } else if (self::m(substr($word, 0, -1)) == 1) {
  247. if (!self::cvc(substr($word, 0, -1))) {
  248. self::replace($word, 'e', '');
  249. }
  250. }
  251. }
  252. // Part b
  253. if (self::m($word) > 1 AND self::doubleConsonant($word) AND substr($word, -1) == 'l') {
  254. $word = substr($word, 0, -1);
  255. }
  256. return $word;
  257. }
  258. /**
  259. * Replaces the first string with the second, at the end of the string. If third
  260. * arg is given, then the preceding string must match that m count at least.
  261. *
  262. * @param string $str String to check
  263. * @param string $check Ending to check for
  264. * @param string $repl Replacement string
  265. * @param int $m Optional minimum number of m() to meet
  266. * @return bool Whether the $check string was at the end
  267. * of the $str string. True does not necessarily mean
  268. * that it was replaced.
  269. */
  270. private static function replace(&$str, $check, $repl, $m = null)
  271. {
  272. $len = 0 - strlen($check);
  273. if (substr($str, $len) == $check) {
  274. $substr = substr($str, 0, $len);
  275. if (is_null($m) OR self::m($substr) > $m) {
  276. $str = $substr . $repl;
  277. }
  278. return true;
  279. }
  280. return false;
  281. }
  282. /**
  283. * What, you mean it's not obvious from the name?
  284. *
  285. * m() measures the number of consonant sequences in $str. if c is
  286. * a consonant sequence and v a vowel sequence, and <..> indicates arbitrary
  287. * presence,
  288. *
  289. * <c><v> gives 0
  290. * <c>vc<v> gives 1
  291. * <c>vcvc<v> gives 2
  292. * <c>vcvcvc<v> gives 3
  293. *
  294. * @param string $str The string to return the m count for
  295. * @return int The m count
  296. */
  297. private static function m($str)
  298. {
  299. $c = self::$regex_consonant;
  300. $v = self::$regex_vowel;
  301. $str = preg_replace("#^$c+#", '', $str);
  302. $str = preg_replace("#$v+$#", '', $str);
  303. preg_match_all("#($v+$c+)#", $str, $matches);
  304. return count($matches[1]);
  305. }
  306. /**
  307. * Returns true/false as to whether the given string contains two
  308. * of the same consonant next to each other at the end of the string.
  309. *
  310. * @param string $str String to check
  311. * @return bool Result
  312. */
  313. private static function doubleConsonant($str)
  314. {
  315. $c = self::$regex_consonant;
  316. return preg_match("#$c{2}$#", $str, $matches) AND $matches[0]{0} == $matches[0]{1};
  317. }
  318. /**
  319. * Checks for ending CVC sequence where second C is not W, X or Y
  320. *
  321. * @param string $str String to check
  322. * @return bool Result
  323. */
  324. private static function cvc($str)
  325. {
  326. $c = self::$regex_consonant;
  327. $v = self::$regex_vowel;
  328. return preg_match("#($c$v$c)$#", $str, $matches)
  329. AND strlen($matches[1]) == 3
  330. AND $matches[1]{2} != 'w'
  331. AND $matches[1]{2} != 'x'
  332. AND $matches[1]{2} != 'y';
  333. }
  334. }
  335. ?>