-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTestingForACM.py
More file actions
410 lines (378 loc) · 14.5 KB
/
TestingForACM.py
File metadata and controls
410 lines (378 loc) · 14.5 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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import AutoCompModule
from AutoCompModule import AutoCompModule
import pymongo, os, sys, re
import ACM_LDA
from ACM_LDA import ACM_LDA
# Testing for the AutoCompModule
def simpleTestSingle(ACM, testFile, num):
with open(testFile,'r',encoding='utf-8') as test:
numOfChecks1 = numOfChecks2 = succ1 = succ2 = 0
i = num
for line in test:
pprev = prev = None
for word in line.split():
if re.match("[.,\"\(\);']",word):
pprev = prev = word = None
i = num
continue
if i!= 0:
i-=1
pprev = prev
prev = word
else:
a,b = ACM.suggest(pprev,prev)
if a is not None:
if a is word:
succ1+=1
numOfChecks1+=1
if b is not None:
if b is word:
succ2+=1
numOfChecks2+=1
i=num
pprev=prev
prev=word
numOfChecks1 = numOfChecks1 if numOfChecks1 > 0 else 1
numOfChecks2 = numOfChecks2 if numOfChecks2 > 0 else 1
return succ1/numOfChecks1, succ2/numOfChecks2
def simpleTest(ACM,inputDir,num):
sum1 = sum2 = 0
numOfFiles = 0
size = len(os.listdir(inputDir))
i=1
if os.path.isdir(inputDir):
for f in sorted(os.listdir(inputDir)):
x1,x2 = simpleTestSingle(ACM,inputDir + '/' + f,num)
sys.stdout.flush()
print(str(int((i*100)/size))+"%",end="\r")
i+=1
sum1+=x1
sum2+=x2
numOfFiles+=1
return sum1/numOfFiles, sum2/numOfFiles
return "input Error"
def impSimTestSingle(ACM, testFile, num, numOfElemList=5):
with open(testFile,'r',encoding='utf-8') as test:
numOfChecks = succ = 0
i = num
def alphaFunc(lst1,lst2,numOfElemList):
if lst1 is None:
return None
if lst2 is None:
lst1.sort()
return [lst1[i][1] for i in range(len(lst1)) if i <= (numOfElemList-1)]
resLst = []
for a in lst1:
for b in lst2:
if a[1]==b[1]:
a[0]+=b[0]
break
resLst.append(a)
for b in lst2:
flag = False
for a in resLst:
if a[1] == b[1]:
flag = True
break
if not flag:
resLst.append(b)
resLst.sort(reverse=True)
return [resLst[i][1] for i in range(len(resLst)) if i <= (numOfElemList-1)]
for line in test:
pprev = prev = None
for word in line.split():
if re.match("[.,\"\(\);']",word):
pprev = prev = word = None
i = num
continue
if i!= 0:
i-=1
pprev = prev
prev = word
else:
lstBy2,lstBy3 = ACM.suggest2(pprev,prev,2*numOfElemList)
a = alphaFunc(lstBy2,lstBy3,numOfElemList)
if a is not None:
if word in a:
succ+=1
numOfChecks+=1
i=num
pprev=prev
prev=word
numOfChecks = numOfChecks if numOfChecks > 0 else 1
return succ/numOfChecks
def impSimTest(ACM,inputDir,num,x):
sum = 0
numOfFiles = 0
size = len(os.listdir(inputDir))
i=1
if os.path.isdir(inputDir):
for f in sorted(os.listdir(inputDir)):
res = impSimTestSingle(ACM,inputDir + '/' + f,num,x)
sys.stdout.flush()
print(str(int((i*100)/size))+"%",end="\r")
i+=1
sum+=res
numOfFiles+=1
return sum/numOfFiles
return "input Error"
def probTestSingle(ACM, testFile, num):
with open(testFile,'r',encoding='utf-8') as test:
biScore = triScore = 0.0
biChecks = triChecks = 0
i = num
def smooth(ACM,pprev,prev,word):
if pprev is None:
res = ACM.dictBy2.find_one({"first":prev,"second":word})
if res is None:
res = 1
else:
res = ACM.dictBy2.find_one({"first":prev,"second":word})["grade"]+1
else:
res = ACM.dictBy3.find_one({"first":pprev,"second":prev,"third":word})
if res is None:
res = 1
else:
res = ACM.dictBy3.find_one({"first":pprev,"second":prev,"third":word})["grade"]+1
return res
for line in test:
pprev = prev = None
for word in line.split():
if re.match("[.,\"\(\);']",word):
pprev = prev = word = None
i = num
continue
if i != 0:
i -= 1
pprev = prev
prev = word
else:
a,b = ACM.suggest(pprev,prev)
if a is not None:
biScore += (smooth(ACM,None,prev,word)/(smooth(ACM,None,prev,a)))
biChecks += 1
if b is not None:
triScore += (smooth(ACM,pprev,prev,word))/(smooth(ACM,pprev,prev,b))
triChecks += 1
i=num
pprev=prev
prev=word
biChecks = biChecks if biChecks >0 else 1
triChecks = triChecks if triChecks >0 else 1
return biScore/biChecks, triScore/triChecks
def probTest(ACM,inputDir,num):
sum1 = sum2 = 0
numOfFiles = 0
size = len(os.listdir(inputDir))
i=1
if os.path.isdir(inputDir):
for f in sorted(os.listdir(inputDir)):
x1,x2 = probTestSingle(ACM,inputDir + '/' + f,num)
sys.stdout.flush()
print(str(int((i*100)/size))+"%",end="\r")
i+=1
sum1+=x1
sum2+=x2
numOfFiles+=1
return sum1/numOfFiles, sum2/numOfFiles
return "input Error"
def simTopicTest(ACM,testFile,num,numTopics,numOfElemList1,numOfElemList2,numLastTopics,numOfBestTopics):
def topicFunc(lst1,lst2,numOfElemList):
if lst1 is None:
if lst2 is None:
return None
else:
return [lst2[i][4] for i in range(len(lst2)) if i <= (numOfElemList-1)]
if lst2 is None:
return [lst1[i][4] for i in range(len(lst1)) if i <= (numOfElemList-1)]
resLst = []
for a in lst1:
for b in lst2:
if a[4]==b[4]:
a[3]+=b[3]
break
resLst.append(a)
for b in lst2:
flag = False
for a in resLst:
if a[4] == b[4]:
flag = True
break
if not flag:
resLst.append(b)
resLst.sort(reverse=True)
return [resLst[i][4] for i in range(len(resLst)) if i <= (numOfElemList-1)]
def fBestTopic(vtopics,numOfBestTopics):
vtlist = []
for t in vtopics:
tmp = vtopics[t] + [t]
vtlist += [tmp]
vtlist.sort(reverse=True)
return [vtlist[i][3] for i in range(numOfBestTopics)]
def fBestTopic2(lastTopics,numOfBestTopics):
tmpV = {i:[0,0,0] for i in range(numTopics)}
for winfo in lastTopics:
for t in winfo:
tmpV[t[0]][0] += 1
tmpV[t[0]][1] += t[1]
if t[3] == True:
tmpV[t[0]][2] += 1
return fBestTopic(tmpV,numOfBestTopics)
with open(testFile,'r',encoding='utf-8') as test:
vtopics = {i:[0,0,0] for i in range(numTopics)}
lastTopics = []
numInLastTopics = 0
i = num
numOfChecks = succ = numOfChecks2 = succ2 = 0
for line in test:
pprev = prev = None
for word in line.split():
if re.match("[.,\"\(\);']",word):
pprev = prev = word = None
i = num
continue
if i!= 0:
i-=1
pprev = prev
prev = word
else:
lstBy2,lstBy3 = ACM.suggestTopTopic(pprev,prev,fBestTopic(vtopics,numOfBestTopics),numOfElemList2)
a = topicFunc(lstBy2,lstBy3,numOfElemList1)
if a is not None:
if word in a:
succ+=1
numOfChecks+=1
lstBy2,lstBy3 = ACM.suggestTopTopic(pprev,prev,fBestTopic2(lastTopics,numOfBestTopics),numOfElemList2)
a = topicFunc(lstBy2,lstBy3,numOfElemList1)
if a is not None:
if word in a:
succ2+=1
numOfChecks2+=1
i=num
pprev=prev
prev=word
wordInDict = ACM.dict.find_one({"word":prev})
if wordInDict is not None:
wordInfo=wordInDict["info"]
if wordInfo is not None:
for a in wordInfo:
vtopics[a[0]][0]+=1
vtopics[a[0]][1]+=a[1]
if a[3] is True:
vtopics[a[0]][2]+=1
if len(lastTopics) < numLastTopics:
lastTopics +=[wordInfo]
else:
lastTopics.pop(0)
lastTopics+=[wordInfo]
return succ/numOfChecks, succ2/numOfChecks2
def topicTest(ACM,inputDir,num,numTopics,numOfElemList1,numOfElemList2,numLastTopics,numOfBestTopics):
sum1 = sum2 = 0
numOfFiles = 0
size = len(os.listdir(inputDir))
i=1
if os.path.isdir(inputDir):
for f in sorted(os.listdir(inputDir)):
x1,x2 = simTopicTest(ACM,inputDir + '/' + f,num,numTopics,numOfElemList1,numOfElemList2,numLastTopics,numOfBestTopics)
sys.stdout.flush()
print(str(int((i*100)/size))+"%",end="\r")
i+=1
sum1+=x1
sum2+=x2
numOfFiles+=1
return sum1/numOfFiles, sum2/numOfFiles
return "input Error"
def diffTopicTest(ACM,testFile,num,numElmCut,numElmX,maxBuff=float('inf')):
with open(testFile,'r',encoding='utf-8') as test:
numOfChecks1 = numOfChecks2 = succ1 = succ2 = 0
i = num
buff = []
for line in test:
pprev = prev = None
for word in line.split():
buff += [word]
if len(buff) > maxBuff:
buff.pop(0)
if re.match("[.,\"\(\);']",word):
pprev = prev = word = None
i = num
continue
if i!= 0:
i-=1
pprev = prev
prev = word
else:
a = ACM.suggest(pprev,prev,buff,numElmX)
if a is not None:
if word in a[:numElmCut]:
succ1+=1
numOfChecks1+=1
i=num
pprev=prev
prev=word
numOfChecks1 = numOfChecks1 if numOfChecks1 > 0 else 1
return succ1/numOfChecks1
def diffTest(ACM,inputDir,num,numElmCut,numElmX,maxBuff=float('inf')):
print ('Start testing')
sum1 = 0
size = len(os.listdir(inputDir))
i=1
if os.path.isdir(inputDir):
for f in sorted(os.listdir(inputDir)):
x1 = diffTopicTest(ACM,inputDir + '/' + f,num,numElmCut,numElmX,maxBuff)
sys.stdout.flush()
print(str(int((i*100)/size))+"%",end="\r")
i+=1
sum1+=x1
return sum1/size
return "input Error"
# Input for the testing::
# 1 - the database name we want to check
# 2 - the directory we want to test
# 3 - the test type
# 4 - num of checks
def main():
#testing 1 2 3
if len(sys.argv) > 6:
print ("Error - too few arguments. Arguments are: Database Name, Inpurt Directory, Test Type, Number of words to see\n")
DBname = sys.argv[1]
Indir = sys.argv[2]
action = sys.argv[3]
if action != 'learn':
numOfChecks = int(sys.argv[4])
ACM = ACM_LDA(DBname)
if action == 'learn':
ACM.learn(Indir)
if action == 'simple':
(x,y) = simpleTest(ACM,Indir,numOfChecks)
print (round(x*100,2),round(y*100,2))
if action == 'prob':
(x,y) = probTest(ACM,Indir,numOfChecks)
print (round(x*100,2),round(y*100,2))
if action == 'isimple':
# 5 - num of elements to choose from table
ACM.dropDicts(DBname)
ACM.learn('corpus')
numElem = int(sys.argv[5])
x = impSimTest(ACM,Indir,numOfChecks,numElem)
print (round(x*100,2))
if action == 'sTopic':
ACM = ACM_LDA(DBname)
(x,y) = topicTest(ACM,Indir,numOfChecks,15,5,7,15,3)
print (round(x*100,2),round(y*100,2))
if action == 'best100':
i=0
with open('ilaiOut.txt','w') as outfile:
for a in ACM.dict.find().sort([("grade",-1)]):
if i < 100:
outfile.write(str(a)+'\n')
i+=1
else:
break
if action == 'diff':
ACM.dropDicts(DBname)
ACM.learn(Indir)
print ("done learning. starting test.")
print (diffTest(ACM, Indir+'test', numOfChecks, 5, 5))
if __name__ == '__main__':
main()