PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/t/askeet/lib/PorterStemmer.class.php

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