| 1 |
""" |
| 2 |
Implementation of Porter Stemming Algorithm from |
| 3 |
https://tartarus.org/martin/PorterStemmer/python.txt |
| 4 |
|
| 5 |
Note: The Python implementation returns different results than the JS |
| 6 |
version: |
| 7 |
- Stemming "lay" returns "lai" in Python, but "lay" in JS |
| 8 |
- Stemming "try" returns "try" in Python, but "tri" in JS |
| 9 |
|
| 10 |
|
| 11 |
Porter Stemming Algorithm |
| 12 |
This is the Porter stemming algorithm, ported to Python from the |
| 13 |
version coded up in ANSI C by the author. It may be be regarded |
| 14 |
as canonical, in that it follows the algorithm presented in |
| 15 |
|
| 16 |
Porter, 1980, An algorithm for suffix stripping, Program, Vol. 14, |
| 17 |
no. 3, pp 130-137, |
| 18 |
|
| 19 |
only differing from it at the points maked --DEPARTURE-- below. |
| 20 |
|
| 21 |
See also http://www.tartarus.org/~martin/PorterStemmer |
| 22 |
|
| 23 |
The algorithm as described in the paper could be exactly replicated |
| 24 |
by adjusting the points of DEPARTURE, but this is barely necessary, |
| 25 |
because (a) the points of DEPARTURE are definitely improvements, and |
| 26 |
(b) no encoding of the Porter stemmer I have seen is anything like |
| 27 |
as exact as this version, even with the points of DEPARTURE! |
| 28 |
|
| 29 |
Vivake Gupta ([email protected]) |
| 30 |
|
| 31 |
Release 1: January 2001 |
| 32 |
|
| 33 |
Further adjustments by Santiago Bruno ([email protected]) |
| 34 |
to allow word input not restricted to one word per line, leading |
| 35 |
to: |
| 36 |
|
| 37 |
release 2: July 2008 |
| 38 |
""" |
| 39 |
|
| 40 |
from lunr.pipeline import Pipeline |
| 41 |
|
| 42 |
|
| 43 |
class PorterStemmer: |
| 44 |
def __init__(self): |
| 45 |
"""The main part of the stemming algorithm starts here. |
| 46 |
b is a buffer holding a word to be stemmed. The letters are in b[k0], |
| 47 |
b[k0+1] ... ending at b[k]. In fact k0 = 0 in this demo program. k is |
| 48 |
readjusted downwards as the stemming progresses. Zero termination is |
| 49 |
not in fact used in the algorithm. |
| 50 |
|
| 51 |
Note that only lower case sequences are stemmed. Forcing to lower case |
| 52 |
should be done before stem(...) is called. |
| 53 |
""" |
| 54 |
|
| 55 |
self.b = "" # buffer for word to be stemmed |
| 56 |
self.k = 0 |
| 57 |
self.k0 = 0 |
| 58 |
self.j = 0 # j is a general offset into the string |
| 59 |
|
| 60 |
def cons(self, i): |
| 61 |
"""cons(i) is TRUE <=> b[i] is a consonant.""" |
| 62 |
if ( |
| 63 |
self.b[i] == "a" |
| 64 |
or self.b[i] == "e" |
| 65 |
or self.b[i] == "i" |
| 66 |
or self.b[i] == "o" |
| 67 |
or self.b[i] == "u" |
| 68 |
): |
| 69 |
return 0 |
| 70 |
if self.b[i] == "y": |
| 71 |
if i == self.k0: |
| 72 |
return 1 |
| 73 |
else: |
| 74 |
return not self.cons(i - 1) |
| 75 |
return 1 |
| 76 |
|
| 77 |
def m(self): |
| 78 |
"""m() measures the number of consonant sequences between k0 and j. |
| 79 |
if c is a consonant sequence and v a vowel sequence, and <..> |
| 80 |
indicates arbitrary presence, |
| 81 |
|
| 82 |
<c><v> gives 0 |
| 83 |
<c>vc<v> gives 1 |
| 84 |
<c>vcvc<v> gives 2 |
| 85 |
<c>vcvcvc<v> gives 3 |
| 86 |
.... |
| 87 |
""" |
| 88 |
n = 0 |
| 89 |
i = self.k0 |
| 90 |
while 1: |
| 91 |
if i > self.j: |
| 92 |
return n |
| 93 |
if not self.cons(i): |
| 94 |
break |
| 95 |
i = i + 1 |
| 96 |
i = i + 1 |
| 97 |
while 1: |
| 98 |
while 1: |
| 99 |
if i > self.j: |
| 100 |
return n |
| 101 |
if self.cons(i): |
| 102 |
break |
| 103 |
i = i + 1 |
| 104 |
i = i + 1 |
| 105 |
n = n + 1 |
| 106 |
while 1: |
| 107 |
if i > self.j: |
| 108 |
return n |
| 109 |
if not self.cons(i): |
| 110 |
break |
| 111 |
i = i + 1 |
| 112 |
i = i + 1 |
| 113 |
|
| 114 |
def vowelinstem(self): |
| 115 |
"""vowelinstem() is TRUE <=> k0,...j contains a vowel""" |
| 116 |
for i in range(self.k0, self.j + 1): |
| 117 |
if not self.cons(i): |
| 118 |
return 1 |
| 119 |
return 0 |
| 120 |
|
| 121 |
def doublec(self, j): |
| 122 |
"""doublec(j) is TRUE <=> j,(j-1) contain a double consonant.""" |
| 123 |
if j < (self.k0 + 1): |
| 124 |
return 0 |
| 125 |
if self.b[j] != self.b[j - 1]: |
| 126 |
return 0 |
| 127 |
return self.cons(j) |
| 128 |
|
| 129 |
def cvc(self, i): |
| 130 |
"""cvc(i) is TRUE <=> i-2,i-1,i has the form consonant - vowel - consonant |
| 131 |
and also if the second c is not w,x or y. this is used when trying to |
| 132 |
restore an e at the end of a short e.g. |
| 133 |
|
| 134 |
cav(e), lov(e), hop(e), crim(e), but |
| 135 |
snow, box, tray. |
| 136 |
""" |
| 137 |
if ( |
| 138 |
i < (self.k0 + 2) |
| 139 |
or not self.cons(i) |
| 140 |
or self.cons(i - 1) |
| 141 |
or not self.cons(i - 2) |
| 142 |
): |
| 143 |
return 0 |
| 144 |
ch = self.b[i] |
| 145 |
if ch == "w" or ch == "x" or ch == "y": |
| 146 |
return 0 |
| 147 |
return 1 |
| 148 |
|
| 149 |
def ends(self, s): |
| 150 |
"""ends(s) is TRUE <=> k0,...k ends with the string s.""" |
| 151 |
length = len(s) |
| 152 |
if s[length - 1] != self.b[self.k]: # tiny speed-up |
| 153 |
return 0 |
| 154 |
if length > (self.k - self.k0 + 1): |
| 155 |
return 0 |
| 156 |
if self.b[self.k - length + 1 : self.k + 1] != s: |
| 157 |
return 0 |
| 158 |
self.j = self.k - length |
| 159 |
return 1 |
| 160 |
|
| 161 |
def setto(self, s): |
| 162 |
"""setto(s) sets (j+1),...k to the characters in the string s, readjusting k.""" |
| 163 |
length = len(s) |
| 164 |
self.b = self.b[: self.j + 1] + s + self.b[self.j + length + 1 :] |
| 165 |
self.k = self.j + length |
| 166 |
|
| 167 |
def r(self, s): |
| 168 |
"""r(s) is used further down.""" |
| 169 |
if self.m() > 0: |
| 170 |
self.setto(s) |
| 171 |
|
| 172 |
def step1ab(self): |
| 173 |
"""step1ab() gets rid of plurals and -ed or -ing. e.g. |
| 174 |
|
| 175 |
caresses -> caress |
| 176 |
ponies -> poni |
| 177 |
ties -> ti |
| 178 |
caress -> caress |
| 179 |
cats -> cat |
| 180 |
|
| 181 |
feed -> feed |
| 182 |
agreed -> agree |
| 183 |
disabled -> disable |
| 184 |
|
| 185 |
matting -> mat |
| 186 |
mating -> mate |
| 187 |
meeting -> meet |
| 188 |
milling -> mill |
| 189 |
messing -> mess |
| 190 |
|
| 191 |
meetings -> meet |
| 192 |
""" |
| 193 |
if self.b[self.k] == "s": |
| 194 |
if self.ends("sses"): |
| 195 |
self.k = self.k - 2 |
| 196 |
elif self.ends("ies"): |
| 197 |
self.setto("i") |
| 198 |
elif self.b[self.k - 1] != "s": |
| 199 |
self.k = self.k - 1 |
| 200 |
if self.ends("eed"): |
| 201 |
if self.m() > 0: |
| 202 |
self.k = self.k - 1 |
| 203 |
elif (self.ends("ed") or self.ends("ing")) and self.vowelinstem(): |
| 204 |
self.k = self.j |
| 205 |
if self.ends("at"): |
| 206 |
self.setto("ate") |
| 207 |
elif self.ends("bl"): |
| 208 |
self.setto("ble") |
| 209 |
elif self.ends("iz"): |
| 210 |
self.setto("ize") |
| 211 |
elif self.doublec(self.k): |
| 212 |
self.k = self.k - 1 |
| 213 |
ch = self.b[self.k] |
| 214 |
if ch == "l" or ch == "s" or ch == "z": |
| 215 |
self.k = self.k + 1 |
| 216 |
elif self.m() == 1 and self.cvc(self.k): |
| 217 |
self.setto("e") |
| 218 |
|
| 219 |
def step1c(self): |
| 220 |
"""step1c() turns terminal y to i when there is another vowel in the stem.""" |
| 221 |
if self.ends("y") and self.vowelinstem(): |
| 222 |
self.b = self.b[: self.k] + "i" + self.b[self.k + 1 :] |
| 223 |
|
| 224 |
def step2(self): |
| 225 |
"""step2() maps double suffices to single ones. |
| 226 |
so -ization ( = -ize plus -ation) maps to -ize etc. note that the |
| 227 |
string before the suffix must give m() > 0. |
| 228 |
""" |
| 229 |
if self.b[self.k - 1] == "a": |
| 230 |
if self.ends("ational"): |
| 231 |
self.r("ate") |
| 232 |
elif self.ends("tional"): |
| 233 |
self.r("tion") |
| 234 |
elif self.b[self.k - 1] == "c": |
| 235 |
if self.ends("enci"): |
| 236 |
self.r("ence") |
| 237 |
elif self.ends("anci"): |
| 238 |
self.r("ance") |
| 239 |
elif self.b[self.k - 1] == "e": |
| 240 |
if self.ends("izer"): |
| 241 |
self.r("ize") |
| 242 |
elif self.b[self.k - 1] == "l": |
| 243 |
if self.ends("bli"): |
| 244 |
self.r("ble") # --DEPARTURE-- |
| 245 |
# To match the published algorithm, replace this phrase with |
| 246 |
# if self.ends("abli"): self.r("able") |
| 247 |
elif self.ends("alli"): |
| 248 |
self.r("al") |
| 249 |
elif self.ends("entli"): |
| 250 |
self.r("ent") |
| 251 |
elif self.ends("eli"): |
| 252 |
self.r("e") |
| 253 |
elif self.ends("ousli"): |
| 254 |
self.r("ous") |
| 255 |
elif self.b[self.k - 1] == "o": |
| 256 |
if self.ends("ization"): |
| 257 |
self.r("ize") |
| 258 |
elif self.ends("ation"): |
| 259 |
self.r("ate") |
| 260 |
elif self.ends("ator"): |
| 261 |
self.r("ate") |
| 262 |
elif self.b[self.k - 1] == "s": |
| 263 |
if self.ends("alism"): |
| 264 |
self.r("al") |
| 265 |
elif self.ends("iveness"): |
| 266 |
self.r("ive") |
| 267 |
elif self.ends("fulness"): |
| 268 |
self.r("ful") |
| 269 |
elif self.ends("ousness"): |
| 270 |
self.r("ous") |
| 271 |
elif self.b[self.k - 1] == "t": |
| 272 |
if self.ends("aliti"): |
| 273 |
self.r("al") |
| 274 |
elif self.ends("iviti"): |
| 275 |
self.r("ive") |
| 276 |
elif self.ends("biliti"): |
| 277 |
self.r("ble") |
| 278 |
elif self.b[self.k - 1] == "g": # --DEPARTURE-- |
| 279 |
if self.ends("logi"): |
| 280 |
self.r("log") |
| 281 |
# To match the published algorithm, delete this phrase |
| 282 |
|
| 283 |
def step3(self): |
| 284 |
"""step3() dels with -ic-, -full, -ness etc. similar strategy to step2.""" |
| 285 |
if self.b[self.k] == "e": |
| 286 |
if self.ends("icate"): |
| 287 |
self.r("ic") |
| 288 |
elif self.ends("ative"): |
| 289 |
self.r("") |
| 290 |
elif self.ends("alize"): |
| 291 |
self.r("al") |
| 292 |
elif self.b[self.k] == "i": |
| 293 |
if self.ends("iciti"): |
| 294 |
self.r("ic") |
| 295 |
elif self.b[self.k] == "l": |
| 296 |
if self.ends("ical"): |
| 297 |
self.r("ic") |
| 298 |
elif self.ends("ful"): |
| 299 |
self.r("") |
| 300 |
elif self.b[self.k] == "s": |
| 301 |
if self.ends("ness"): |
| 302 |
self.r("") |
| 303 |
|
| 304 |
def step4(self): |
| 305 |
"""step4() takes off -ant, -ence etc., in context <c>vcvc<v>.""" |
| 306 |
if self.b[self.k - 1] == "a": |
| 307 |
if self.ends("al"): |
| 308 |
pass |
| 309 |
else: |
| 310 |
return |
| 311 |
elif self.b[self.k - 1] == "c": |
| 312 |
if self.ends("ance"): |
| 313 |
pass |
| 314 |
elif self.ends("ence"): |
| 315 |
pass |
| 316 |
else: |
| 317 |
return |
| 318 |
elif self.b[self.k - 1] == "e": |
| 319 |
if self.ends("er"): |
| 320 |
pass |
| 321 |
else: |
| 322 |
return |
| 323 |
elif self.b[self.k - 1] == "i": |
| 324 |
if self.ends("ic"): |
| 325 |
pass |
| 326 |
else: |
| 327 |
return |
| 328 |
elif self.b[self.k - 1] == "l": |
| 329 |
if self.ends("able"): |
| 330 |
pass |
| 331 |
elif self.ends("ible"): |
| 332 |
pass |
| 333 |
else: |
| 334 |
return |
| 335 |
elif self.b[self.k - 1] == "n": |
| 336 |
if self.ends("ant"): |
| 337 |
pass |
| 338 |
elif self.ends("ement"): |
| 339 |
pass |
| 340 |
elif self.ends("ment"): |
| 341 |
pass |
| 342 |
elif self.ends("ent"): |
| 343 |
pass |
| 344 |
else: |
| 345 |
return |
| 346 |
elif self.b[self.k - 1] == "o": |
| 347 |
if self.ends("ion") and (self.b[self.j] == "s" or self.b[self.j] == "t"): |
| 348 |
pass |
| 349 |
elif self.ends("ou"): |
| 350 |
pass |
| 351 |
# takes care of -ous |
| 352 |
else: |
| 353 |
return |
| 354 |
elif self.b[self.k - 1] == "s": |
| 355 |
if self.ends("ism"): |
| 356 |
pass |
| 357 |
else: |
| 358 |
return |
| 359 |
elif self.b[self.k - 1] == "t": |
| 360 |
if self.ends("ate"): |
| 361 |
pass |
| 362 |
elif self.ends("iti"): |
| 363 |
pass |
| 364 |
else: |
| 365 |
return |
| 366 |
elif self.b[self.k - 1] == "u": |
| 367 |
if self.ends("ous"): |
| 368 |
pass |
| 369 |
else: |
| 370 |
return |
| 371 |
elif self.b[self.k - 1] == "v": |
| 372 |
if self.ends("ive"): |
| 373 |
pass |
| 374 |
else: |
| 375 |
return |
| 376 |
elif self.b[self.k - 1] == "z": |
| 377 |
if self.ends("ize"): |
| 378 |
pass |
| 379 |
else: |
| 380 |
return |
| 381 |
else: |
| 382 |
return |
| 383 |
if self.m() > 1: |
| 384 |
self.k = self.j |
| 385 |
|
| 386 |
def step5(self): |
| 387 |
"""step5() removes a final -e if m() > 1, and changes -ll to -l if |
| 388 |
m() > 1. |
| 389 |
""" |
| 390 |
self.j = self.k |
| 391 |
if self.b[self.k] == "e": |
| 392 |
a = self.m() |
| 393 |
if a > 1 or (a == 1 and not self.cvc(self.k - 1)): |
| 394 |
self.k = self.k - 1 |
| 395 |
if self.b[self.k] == "l" and self.doublec(self.k) and self.m() > 1: |
| 396 |
self.k = self.k - 1 |
| 397 |
|
| 398 |
def stem(self, p, metadata=None): |
| 399 |
"""In stem(p,i,j), p is a char pointer, and the string to be stemmed |
| 400 |
is from p[i] to p[j] inclusive. Typically i is zero and j is the |
| 401 |
offset to the last character of a string, (p[j+1] == '\0'). The |
| 402 |
stemmer adjusts the characters p[i] ... p[j] and returns the new |
| 403 |
end-point of the string, k. Stemming never increases word length, so |
| 404 |
i <= k <= j. To turn the stemmer into a module, declare 'stem' as |
| 405 |
extern, and delete the remainder of this file. |
| 406 |
""" |
| 407 |
# TODO: removed i and j from the original implementation |
| 408 |
# to comply with the `token.update` API |
| 409 |
i = 0 |
| 410 |
j = len(p) - 1 |
| 411 |
# copy the parameters into statics |
| 412 |
self.b = p |
| 413 |
self.k = j |
| 414 |
self.k0 = i |
| 415 |
if self.k <= self.k0 + 1: |
| 416 |
return self.b # --DEPARTURE-- |
| 417 |
|
| 418 |
# With this line, strings of length 1 or 2 don't go through the |
| 419 |
# stemming process, although no mention is made of this in the |
| 420 |
# published algorithm. Remove the line to match the published |
| 421 |
# algorithm. |
| 422 |
|
| 423 |
self.step1ab() |
| 424 |
self.step1c() |
| 425 |
self.step2() |
| 426 |
self.step3() |
| 427 |
self.step4() |
| 428 |
self.step5() |
| 429 |
return self.b[self.k0 : self.k + 1] |
| 430 |
|
| 431 |
|
| 432 |
porter_stemmer = PorterStemmer() |
| 433 |
|
| 434 |
|
| 435 |
def stemmer(token, i=None, tokens=None): |
| 436 |
"""Wrapper around the PorterStemmer for inclusion in pipeline. |
| 437 |
|
| 438 |
Args: |
| 439 |
language (str): ISO-639-1 code of the language. |
| 440 |
token (lunr.Token): The token to stem. |
| 441 |
i (int): The index of the token in a set. |
| 442 |
tokens (list): A list of tokens representing the set. |
| 443 |
""" |
| 444 |
return token.update(porter_stemmer.stem) |
| 445 |
|
| 446 |
|
| 447 |
Pipeline.register_function(stemmer, "stemmer") |