-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathstemmer.py
300 lines (280 loc) · 9.98 KB
/
stemmer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
class PorterStemmer:
def isCons(self, letter):
if letter == 'a' or letter == 'e' or letter == 'i' or letter == 'o'
or letter == 'u':
return False
else:
return True
def isConsonant(self, word, i):
letter = word[i]
if self.isCons(letter):
if letter == 'y' and isCons(word[i-1]):
return False
else:
return True
else:
return False
def isVowel(self, word, i):
return not(isConsonant(word, i))
# *S
def endsWith(self, stem, letter):
if stem.endswith(letter):
return True
else:
return False
# *v*
def containsVowel(self, stem):
for i in stem:
if not self.isCons(i):
return True
return False
# *d
def doubleCons(self, stem):
if len(stem) >= 2:
if self.isConsonant(stem, -1) and self.isConsonant(stem, -2):
return True
else:
return False
else:
return False
def getForm(self, word):
form = []
formStr = ''
for i in range(len(word)):
if self.isConsonant(word, i):
if i != 0:
prev = form[-1]
if prev != 'C':
form.append('C')
else:
form.append('C')
else:
if i != 0:
prev = form[-1]
if prev != 'V':
form.append('V')
else:
form.append('V')
for j in form:
formStr += j
return formStr
def getM(self, word):
form = self.getForm(word)
m = form.count('VC')
return m
# *o
def cvc(self, word):
if len(word) >= 3:
f = -3
s = -2
t = -1
third = word[t]
if self.isConsonant(word, f) and self.isVowel(word, s)
and self.isConsonant(word, t):
if third != 'w' and third != 'x' and third != 'y':
return True
else:
return False
else:
return False
else:
return False
def replace(self, orig, rem, rep):
result = orig.rfind(rem)
base = orig[:result]
replaced = base + rep
return replaced
def replaceM0(self, orig, rem, rep):
result = orig.rfind(rem)
base = orig[:result]
if self.getM(base) > 0:
replaced = base + rep
return replaced
else:
return orig
def replaceM1(self, orig, rem, rep):
result = orig.rfind(rem)
base = orig[:result]
if self.getM(base) > 1:
replaced = base + rep
return replaced
else:
return orig
def step1a(self, word):
if word.endswith('sses'):
word = self.replace(word, 'sses', 'ss')
elif word.endswith('ies'):
word = self.replace(word, 'ies', 'i')
elif word.endswith('ss'):
word = self.replace(word, 'ss', 'ss')
elif word.endswith('s'):
word = self.replace(word, 's', '')
else:
pass
return word
def step1b(self, word):
flag = False
if word.endswith('eed'):
result = word.rfind('eed')
base = word[:result]
if self.getM(base) > 0:
word = base
word += 'ee'
elif word.endswith('ed'):
result = word.rfind('ed')
base = word[:result]
if self.containsVowel(base):
word = base
flag = True
elif word.endswith('ing'):
result = word.rfind('ing')
base = word[:result]
if self.containsVowel(base):
word = base
flag = True
if flag:
if word.endswith('at') or word.endswith('bl')
or word.endswith('iz'):
word += 'e'
elif self.doubleCons(word) and not self.endsWith(word, 'l')
and not self.endsWith(word, 's') and not self.endsWith(word, 'z'):
word = word[:-1]
elif self.getM(word) == 1 and self.cvc(word):
word += 'e'
else:
pass
else:
pass
return word
def step1c(self, word):
if word.endswith('y'):
result = word.rfind('y')
base = word[:result]
if self.containsVowel(base):
word = base
word += 'i'
return word
def step2(self, word):
if word.endswith('ational'):
word = self.replaceM0(word, 'ational', 'ate')
elif word.endswith('tional'):
word = self.replaceM0(word, 'tional', 'tion')
elif word.endswith('enci'):
word = self.replaceM0(word, 'enci', 'ence')
elif word.endswith('anci'):
word = self.replaceM0(word, 'anci', 'ance')
elif word.endswith('izer'):
word = self.replaceM0(word, 'izer', 'ize')
elif word.endswith('abli'):
word = self.replaceM0(word, 'abli', 'able')
elif word.endswith('alli'):
word = self.replaceM0(word, 'alli', 'al')
elif word.endswith('entli'):
word = self.replaceM0(word, 'entli', 'ent')
elif word.endswith('eli'):
word = self.replaceM0(word, 'eli', 'e')
elif word.endswith('ousli'):
word = self.replaceM0(word, 'ousli', 'ous')
elif word.endswith('ization'):
word = self.replaceM0(word, 'ization', 'ize')
elif word.endswith('ation'):
word = self.replaceM0(word, 'ation', 'ate')
elif word.endswith('ator'):
word = self.replaceM0(word, 'ator', 'ate')
elif word.endswith('alism'):
word = self.replaceM0(word, 'alism', 'al')
elif word.endswith('iveness'):
word = self.replaceM0(word, 'iveness', 'ive')
elif word.endswith('fulness'):
word = self.replaceM0(word, 'fulness', 'ful')
elif word.endswith('ousness'):
word = self.replaceM0(word, 'ousness', 'ous')
elif word.endswith('aliti'):
word = self.replaceM0(word, 'aliti', 'al')
elif word.endswith('iviti'):
word = self.replaceM0(word, 'iviti', 'ive')
elif word.endswith('biliti'):
word = self.replaceM0(word, 'biliti', 'ble')
return word
def step3(self, word):
if word.endswith('icate'):
word = self.replaceM0(word, 'icate', 'ic')
elif word.endswith('ative'):
word = self.replaceM0(word, 'ative', '')
elif word.endswith('alize'):
word = self.replaceM0(word, 'alize', 'al')
elif word.endswith('iciti'):
word = self.replaceM0(word, 'iciti', 'ic')
elif word.endswith('ful'):
word = self.replaceM0(word, 'ful', '')
elif word.endswith('ness'):
word = self.replaceM0(word, 'ness', '')
return word
def step4(self, word):
if word.endswith('al'):
word = self.replaceM1(word, 'al', '')
elif word.endswith('ance'):
word = self.replaceM1(word, 'ance', '')
elif word.endswith('ence'):
word = self.replaceM1(word, 'ence', '')
elif word.endswith('er'):
word = self.replaceM1(word, 'er', '')
elif word.endswith('ic'):
word = self.replaceM1(word, 'ic', '')
elif word.endswith('able'):
word = self.replaceM1(word, 'able', '')
elif word.endswith('ible'):
word = self.replaceM1(word, 'ible', '')
elif word.endswith('ant'):
word = self.replaceM1(word, 'ant', '')
elif word.endswith('ement'):
word = self.replaceM1(word, 'ement', '')
elif word.endswith('ment'):
word = self.replaceM1(word, 'ment', '')
elif word.endswith('ent'):
word = self.replaceM1(word, 'ent', '')
elif word.endswith('ou'):
word = self.replaceM1(word, 'ou', '')
elif word.endswith('ism'):
word = self.replaceM1(word, 'ism', '')
elif word.endswith('ate'):
word = self.replaceM1(word, 'ate', '')
elif word.endswith('iti'):
word = self.replaceM1(word, 'iti', '')
elif word.endswith('ous'):
word = self.replaceM1(word, 'ous', '')
elif word.endswith('ive'):
word = self.replaceM1(word, 'ive', '')
elif word.endswith('ize'):
word = self.replaceM1(word, 'ize', '')
elif word.endswith('ion'):
result = word.rfind('ion')
base = word[:result]
if self.getM(base) > 1
and (self.endsWith(base, 's') or self.endsWith(base, 't')):
word = base
word = self.replaceM1(word, '', '')
return word
def step5a(self, word):
if word.endswith('e'):
base = word[:-1]
if self.getM(base) > 1:
word = base
elif self.getM(base) == 1 and not self.cvc(base):
word = base
return word
def step5b(self, word):
if self.getM(word) > 1 and self.doubleCons(word)
and self.endsWith(word, 'l'):
word = word[:-1]
return word
def stem(self, word):
word = self.step1a(word)
word = self.step1b(word)
word = self.step1c(word)
word = self.step2(word)
word = self.step3(word)
word = self.step4(word)
word = self.step5a(word)
word = self.step5b(word)
return word