-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstemmer.py
361 lines (297 loc) · 12.7 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
class Stemmer:
#Initialize
suf_sub_rule,suffix_del,suffix_exist, suffix_desc, suffix_ins , suffix_morph, suffix_type, \
suffix_ign, dub_suffix ,dub_suffix_morph,dub_suffix_desc, pre_sub_rule, prefix_del,prefix_desc,\
prefix_ins, prefix_morph, prefix_type,ht_root, suffix_ht, alt_root, ht_suffix, prefix_tm,\
ht_prefix=dict(),dict(),dict(),dict(),dict(),dict(),dict(),dict(),dict(),dict(),dict(),dict(),\
dict(),dict(),dict(),dict(),dict(),dict(),dict(),dict(),dict(),dict(),dict()
parsetwo = 0
sdes,smorph, subsmorph, smorph_rulenum, pdes, repsuff, pmorph=['']*10,['']*10,[0]*10,[0]*10,['']*10,['']*10,['']*10
dspresent=True
rulenumber, smorph_number, pmorph_number, dub_len, spnt, i,k=0, 0, 0, 0, 0, 0, 0
go, repcom, rec=0, 0, 0
suffix, prefix, suffix_index, prefix_index, suffix_rule,prefix_rule, root,\
tmpgen, isSuffixRoot, suffix_root, root_pos = "","","","","","","","","","",""
present = "no"
def __init__(self):
with open('datasets/stemmer/rootwords',encoding="utf8") as f:
lines = f.readlines()
lines = [x.strip() for x in lines]
for line in lines:
data={line:line}
self.ht_root.update(data)
with open('datasets/stemmer/alt_root.txt',encoding="UTF8") as f:
lines = f.readlines()
lines = [x.strip() for x in lines]
for line in lines:
roots = line.split(" ")
data={roots[0]:roots[1]}
self.alt_root.update(data)
# data={roots[0]:"null"}
# self.suffix_exist.update(data)
with open('datasets/stemmer/suffix.txt',encoding="UTF8") as f:
lines = f.readlines()
lines = [x.strip() for x in lines]
for line in lines:
suffix = line.split("|")
data={suffix[0]:suffix[0]}
self.ht_suffix.update(data)
if len(suffix)>1:
data={suffix[0]:suffix[1]}
self.suffix_ht.update(data)
with open('datasets/stemmer/suffix_rule.txt',encoding="UTF8") as f:
lines = f.readlines()
lines = [x.strip() for x in lines]
for i in range(len(lines)):
nexti=i
suffix_rules = lines[nexti].split(" ")
if suffix_rules[0].isdigit():
data={suffix_rules[0]:suffix_rules[1]}
self.suffix_type.update(data) #whether the rule is of SFXX or SFX
data={suffix_rules[0]:suffix_rules[2]}
self.suf_sub_rule.update(data) #number of sub rule
data={suffix_rules[0]:suffix_rules[3]}
self.suffix_morph.update(data) #the morph
data={suffix_rules[0]:suffix_rules[4]}
self.suffix_desc.update(data) #the tag of the morph
data={suffix_rules[0]:suffix_rules[5]}
self.suffix_ign.update(data) #To ignore in the second parse ('Y'or'N')
num_sub = int(self.suf_sub_rule.get(suffix_rules[0])) #geting the sub rule
if (self.suffix_type.get(suffix_rules[0])=="SFX"):
for b in range(num_sub):
nexti+=1
subrule = lines[nexti].split(" ")
data={str(suffix_rules[0]) + str(b): subrule[0]}
self.suffix_del.update(data)
data={str(suffix_rules[0]) + str(b): subrule[1]}
self.suffix_ins.update(data)
with open('datasets/stemmer/prefix.txt',encoding="UTF8") as f:
lines = f.readlines()
lines = [x.strip() for x in lines]
for line in lines:
prefix = line.split("|")
data={prefix[0]:prefix[0]}
self.ht_prefix.update(data)
if len(prefix)>1:
data={prefix[0]:prefix[1]}
self.prefix_tm.update(data)
with open('datasets/stemmer/prefix_rule.txt',encoding="UTF8") as f:
lines = f.readlines()
lines = [x.strip() for x in lines]
for i in range(len(lines)):
nexti=i
prefix_rules = lines[nexti].split(" ")
if prefix_rules[0].isdigit():
data={prefix_rules[0]:prefix_rules[1]}
self.prefix_type.update(data) #whether the rule is of SFXX or SFX
data={prefix_rules[0]:prefix_rules[2]}
self.pre_sub_rule.update(data) #number of sub rule
data={prefix_rules[0]:prefix_rules[3]}
self.prefix_morph.update(data) #the morph
data={prefix_rules[0]:prefix_rules[4]}
self.prefix_desc.update(data) #the tag of the morph
num_sub = int(self.suf_sub_rule.get(prefix_rules[0])) #geting the sub rule
if (self.prefix_type.get(prefix_rules[0])=="PFX"):
for b in range(num_sub):
nexti+=1
subrule = lines[nexti].split(" ")
data={str(prefix_rules[0]) + str(b): subrule[0]}
self.prefix_del.update(data)
data={str(prefix_rules[0]) + str(b): subrule[1]}
self.prefix_ins.update(data)
with open('datasets/stemmer/dub_suffix.txt',encoding="UTF8") as f:
lines = f.readlines()
lines = [x.strip() for x in lines]
for line in lines:
suffix = line.split(" ")
data={suffix[0]:suffix[0]}
self.dub_suffix.update(data)
data={suffix[0]:suffix[1]}
self.dub_suffix_morph.update(data)
data={suffix[0]:suffix[2]}
self.dub_suffix_desc.update(data)
def isAltRoot(self,word):
if word in self.alt_root:
return True
else:
return False
# get the alternate root
def getAltRoot(self,word):
#print("\nalternate root found:"+self.alt_root.get(word))
return self.alt_root.get(word)
def getSuffixExist(self):
return self.suffix_exist.get()
def getSDes(self):
return self.sdes
def getSDesat(self,i):
return self.sdes[i]
def getPDes(self):
return self.pdes
def getSMorph(self):
return self.smorph
def getSMorphat(self,i):
return self.smorph[i]
def getPMorph(self) :
return self.pmorph
def getSMorph_number(self) :
return self.smorph_number
def getPMorph_number(self) :
return self.pmorph_number
def getRoot(self) :
return self.root
def getRuleNumber(self) :
return self.rulenumber
def getSuffix_rule(self) :
return self.suffix_rule
def isRoot(self,string):
if string==(self.ht_root.get(string)):
return True
return False
def setSDes(self,d, i):
self.sdes[i] = d
def setSMorph( self,mor, i):
self.smorph[i] = mor
def setSuffixRoot(self, m):
self.isSuffixRoot = m
def getSuffixRoot(self) :
return self.isSuffixRoot
def setSMorph_rulenum(self,i, j):
self.smorph_rulenum[i] = j
def getSMorph_rulenum(self,i):
return self.smorph_rulenum[i]
def setSubSMorph(self,subnum, i):
self.subsmorph[i] = subnum
def getSubSMorph(self,i):
return self.subsmorph[i]
def setSecParse( self,i):
self.parsetwo = i
def setSMorph_number( self,number):
self.smorph_number = number
def setPDes(self, d, i):
self.pdes[i] = d
def setPMorph( self,mor, i):
self.pmorph[i] = mor
def setPMorph_number( self,number):
self.pmorph_number = number
def setRoot(self, string):
self.root = string
def setRuleNumber( self,i):
self.rulenumber = i
def setSuffix_rule(self, suffix_rule):
self.suffix_rule = suffix_rule
def generateWord(self,token, mn):
#print("\ngenerating word")
q =token
t = str(self.smorph_rulenum[mn])
a = str(self.suffix_del.get(t + str(self.subsmorph[mn])))
l = len(token)
b = int(str(self.suf_sub_rule.get(t)))
if (self.suffix_type.get(t)==("SFX")):
for s in range(b):
if (self.subsmorph[mn] == s) :
dot = str(self.suffix_ins.get(t + str(s)))
if (dot!=(".")):
length = len(dot)
q=q.replace(dot,"")
q+=a
tmpgen = str(q)
break
else:
q+=a
tmpgen = str(q)
return tmpgen
def isASuffix(self,word):
#print("\nVerbs ending with halanta")
word+="\u094d"
c = 9998
i = self.getSMorph_number()
self.setSMorph_number(i)
self.setSMorph(str(self.suffix_morph.get(str(c))), i)
self.setSDes(str(self.suffix_desc.get(str(c))), i)
self.setRoot(word)
def isRepeat(self, rn):
if (str(self.suffix_ign.get(rn))==("Y")):
return True
else:
return False
def suffixPresent(self,string, o):
#print("\nChecking suffix present or not\n")
present = "no"
for rn in range(1,len(string)):
tmp = string[rn:]
#print ("tmp_suffix:"+tmp)
if (tmp==(self.ht_suffix.get(tmp))):
#print("\nsuffix found")
if (self.parsetwo == 1) :
if (self.isRepeat(str(self.suffix_ht.get(tmp)))):
for j in range(rec):
if (self.repsuff[j]==(tmp)) :
present = "yes"
if (self.present==("no")):
self.setRuleNumber(str(int(self.suffix_ht.get(tmp))))
return True
else :
self.setRuleNumber(str(int(self.suffix_ht.get(tmp))))
return True
else:
rec = o
self.setRuleNumber(str(int(self.suffix_ht.get(tmp))))
self.repsuff[rec] = tmp
return True
#print("\nsuffix not found")
return False
def prefixPresent(self,string) :
#print("\nChecking prefix present or not\n")
for rn in range(len(string)):
tmp = string[:rn + 1]
#print ("tmp_prefix:"+tmp)
if (tmp==(self.ht_prefix.get(tmp))) :
#print("\nprefix found")
self.setRuleNumber(str(int(self.prefix_tm.get(tmp))))
return True
#print("\nprefix not found")
return False
def stripPrefix(self,word):
#print("\nStripping prefix")
w = word
b = int(str(self.pre_sub_rule.get(str(self.getRuleNumber()))))
rulenumber = str(self.getRuleNumber())
#print("rule:"+rulenumber)
self.setPMorph(str(self.prefix_morph.get(rulenumber)), self.getPMorph_number())
self.setPDes(str(self.prefix_desc.get(rulenumber)), self.getPMorph_number())
if (self.prefix_type.get(rulenumber)==("PFX")) :
for s in range(b):
#print("\nPrefix_del:"+str(self.prefix_del.get(rulenumber + str(s))))
if (word.startswith(str(self.prefix_del.get(rulenumber + str(s))))):
tmp = str(self.prefix_del.get(rulenumber + str(s)))
w=w.replace(tmp, "")
#print("\nfinal stiped word: "+w)
break
self.setRoot(str(w))
def stripSuffix(self,word):
#print("\nStripping suffix")
w = word
b = int(str(self.suf_sub_rule.get(str(self.getRuleNumber()))))
rulenumber = str(self.getRuleNumber())
self.setSMorph(str(self.suffix_morph.get(rulenumber)), self.getSMorph_number())
self.setSMorph_rulenum(self.getSMorph_number(), int(rulenumber))
self.setSDes(str(self.suffix_desc.get(rulenumber)), self.getSMorph_number())
if (self.suffix_type.get(rulenumber)==("SFX")) :
#print("rule:"+rulenumber)
for s in range(b):
lengthw = len(w)
# #print("length:"+str(lengthw))
#print("\nSuffix_del:"+str(self.suffix_del.get(rulenumber + str(s))))
if (word.endswith(str(self.suffix_del.get(rulenumber + str(s))))):
self.setSubSMorph(s, self.getSMorph_number())
tmp1 = str(self.suffix_del.get(rulenumber + str(s)))
tmp2 = str(self.suffix_ins.get(rulenumber +str(s)))
#print(tmp1+" "+tmp2)
w=w.replace(tmp1, "")
if (tmp2!=(".")):
w+=tmp2
#print("not_dot "+tmp2)
#print("\nfinal stiped word: "+w)
break;
self.setRoot(str(w))
self.setRoot(str(w))