-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMuncher.py
More file actions
295 lines (150 loc) · 5.92 KB
/
Copy pathMuncher.py
File metadata and controls
295 lines (150 loc) · 5.92 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import types
import string
import json
class Muncher():
def __init__(self):
self.affix = "/root/FlaskProj/out.aff"
self.aff = "/root/FlaskProj/temp.aff"
def unmunch(self, wordList):
with open(self.affix, "r") as f:
lines = f.readlines()
filtered_lines = lines[1:]
with open(self.aff, "w") as w:
w.write(''.join(filtered_lines))
with open(self.aff, "r") as r:
# count the number of lines
count = -1
for count, line in enumerate(open(self.aff, 'rU')):
pass
count += 1
# count now equals to 3
# print count
line = r.readlines()
# print len(line)
i = 0
j = 0
test = []
content = []
content4 = []
content3 = []
content5 = []
for l in line:
l = " ".join(l.split())
# print l
length = len(l.split())
# print l
test1 = []
for i in range(length):
test1.append(l.split()[i])
# print test1
if test1[3].isdigit() or test1[0] == "SET":
continue
else:
content.append(l)
# This helps divide words to characters
# content = list(l.strip(" "))
print "This is content, all the input affix: "
# print test
print content
# working on th fifth row
content5 = [i.split(' ')[4] for i in content]
fifth_row = [x for x in content5 if not (x == "")]
# fifth_row gives the presituation of root word
print "The fifth row is "
print fifth_row
# working on the fourth row
content4 = [i.split(' ')[3] for i in content]
fourth_row = [x for x in content4 if not (x.isdigit())]
# fourth_row is a list giving all the SFX added in the suffix
print "The fourth row is "
print fourth_row
# working on the third row
content3 = [i.split(' ')[2] for i in content]
third_row = [x for x in content3 if not (x == "Y")]
# third_row is a list giving all the SFX stripped in the suffix
print "The third row is "
print third_row
# working on the second row
content2 = [i.split(' ')[1] for i in content]
second_row = [x for x in content2 if not (x == "Y")]
# second_row is a list giving the kind of affix
print "The second row is "
print second_row
# working on the first row
content1 = [i.split(' ')[0] for i in content]
first_row = [x for x in content1 if not (x == "Y")]
# first_row is a list giving if its SFX or PFX in the suffix
print "The third row is "
print first_row
lines = wordList
# start comparing:
length_list = []
for t in range(len(fourth_row)):
length_list.append(len(fourth_row[t]))
print "This is how many characters are there in each added suffix"
print length_list
# when the suffix's length longer than word itself, it will output index out of range
result = []
all = []
for l in lines:
# l = l.replace("\n", "")
all.append(l)
result.append(l)
print l
check = 1
for j in range(len(length_list)):
# print type(l[-(length_list[j]+1):]) is types.StringType
# print type(fourth_row[j]) is types.StringType
if len(l) < len(fourth_row[j]):
print " insufficient length!"
pass
else:
print l + " compare to " + fourth_row[j]
portion = l[-(length_list[j]):].replace("\n", "")
if portion == fourth_row[j] and check == 1:
result.remove(l)
check = 0
print "yes"
else:
print portion + "'s suffix does not equal to " + fourth_row[j]
print "All the root word founds are: " + str(result)
number = []
forms = []
temp = []
for l in range(len(result)):
t = 1
print result[l]
# for each paradigm word
for j in range(len(all)):
print "This is all: " + str(all[j])
# for each random word
for s in range(len(content)):
# for each affix rule
if all[j][-len(fourth_row[s]):] == fourth_row[s] and all[j][:-len(fourth_row[s])] + third_row[
s].rstrip(string.digits) == result[l]:
t = t + 1
temp.append(all[j])
print "!!!!!" + str(all[j][:-len(fourth_row[s])]) + " + " + str(
third_row[s].rstrip(string.digits)) + " = " + str(result[l])
else:
print "*****" + str(fourth_row[s]) + str(all[j][:-len(fourth_row[s])] + third_row[s])
temp = list(set(temp))
number.append(len(temp))
forms.append(temp)
temp = []
print number
print forms
tempObj = {}
newList = []
for r in range(len(result)):
tempObj["root"] = result[r]
tempObj["score"] = number[r]
tempObj["slots"] = forms[r]
newList.append(tempObj)
tempObj = {}
myResult = {"candidate_words": newList}
print myResult
return myResult