-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTesting-Random.py
More file actions
291 lines (260 loc) · 9.31 KB
/
Testing-Random.py
File metadata and controls
291 lines (260 loc) · 9.31 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
import sys
from Similarity import *
from local import *
from TrainingDataProcessing import *
from random import *
# import glob
import time
def read_data(input):
in_file = open(input, "r")
texts = []
labels = []
for i,example in enumerate(in_file):
label, text = example.strip().split('\t')
texts.append(text)
labels.append(label)
return labels,texts
in_file.close()
def read_data_random(input):
f = open(input)
texts = []
for line in f:
if line.strip() != '':
texts.append(line.strip())
f.close()
return texts
def DocToDoc_Similarity(label, text,categories,remove_stopword_flag,alignment_type_flag,similarity_type,two_glove_flag):
result=[]
count=0
for example in text:
# indexofchoosen = 0
max=0
output=0
# indexx=0
for i in range(0, len(categories) ):
count+=1
print count
index = randint(i*50, i*50+49)
print index
if(alignment_type_flag == '1'):
if Debug: print "global"
DPResult= global_alignment(example,text[index],remove_stopword_flag,similarity_type,two_glove_flag)
else:
if Debug: print "local"
DPResult=local_alignment(example,text[index], 0, remove_stopword_flag,similarity_type,two_glove_flag)
if(i==0):
indexx = index
max = DPResult
print "awel mara" , max , index , label[index]
elif(DPResult>max):
indexofchoosen = index
max = DPResult
print "Max at3'yar" , max , i , index , label[index]
output=i
if(output != 0):
print "Example : " , example
print "Right answer: " , text[indexx]
print "Choosen answer: " , text[indexofchoosen]
# print "count kam isa? " , count
# if(count > 50):
# break
result.append(categories[output])
return result
def DocToDoc_Similarity_Random(text, remove_stopword_flag, alignment_type_flag, similarity_type, two_glove_flag):
result = []
example_num = len(text)/4
print 'Document_num =', example_num
for i in range(example_num):
example = text[4*i]
print 'Doc #', (i+1)
for j in range(1, 4):
print j,
if(alignment_type_flag == '1'):
if Debug: print "global"
DPResult= global_alignment(example,text[4*i+j],remove_stopword_flag,similarity_type,two_glove_flag)
else:
if Debug: print "local"
DPResult=local_alignment(example,text[4*i+j], 0, remove_stopword_flag,similarity_type,two_glove_flag)
if (j == 1):
current_max = DPResult
current_idx = j
elif(DPResult>current_max):
current_max = DPResult
current_idx = j
result.append(4*i+current_idx)
print 'predicted:', current_idx
return result
def local_alignment(s1, s2, p_gap , remove_stopword_flag,similarity_type , two_glove_flag):
# tic = time.time()
s1 = s1.lower()
s2 = s2.lower()
# len1 = len(s1)+1
# len2 = len(s2)+1
# # print len1, len2
s1 = s1.split(' ')
s2 = s2.split(' ')
if(remove_stopword_flag):
if Debug: print "remove_stopword_flag"
s1 = removeStopwords(s1,Stopwords)
s2 = removeStopwords(s2,Stopwords)
len1 = len(s1)+1
len2 = len(s2)+1
table = numpy.zeros([len1, len2])
Gaptimes=0
Dtimes = 0
for i in range(1, len1):
for j in range(1, len2):
score1 = table[i, j-1] + p_gap
if(similarity_type == '1'):
if Debug: print "glove"
if(two_glove_flag):
if Debug: print "2 glove"
score2 =table[i-1, j-1] + normalized (-1,1,-10,10,get_similarity_from_glove(s1[i-1], s2[j-1], dictionary,glove,secondary_dictionary,secondary_glove))
else:
if Debug: print "1 glove"
score2 = table[i-1, j-1]+ normalized (-1,1,-10,10,get_similarity_from_glove(s1[i-1], s2[j-1], dictionary,glove))
else:
if Debug: print "wordnet"
wordnet_score = get_similarity_from_wordnet(s1[i-1], s2[j-1])
if wordnet_score == -1 :
wordnet_score = 0
score2 = table[i-1, j-1]+normalized (0,1,-10,10,wordnet_score)
score4 = 0
score3 = table[i-1, j] + p_gap
table[i, j] = max(score1, score2, score3 , score4)
if(table[i, j] ==score4):
Dtimes+=1
else:
Gaptimes+=1
print "Dtimes, Gaptimes : " , Dtimes, Gaptimes
score = table.max()
# toc = time.time()
# print('Processing time: %r'
# % (toc - tic))
return score
def global_alignment(s1,s2,remove_stopword_flag,similarity_type , two_glove_flag):
p_gap = 0
# tic = time.time()
s1 = s1.lower()
s2 = s2.lower()
# len1 = len(s1)+1
# len2 = len(s2)+1
# # print len1, len2
s1 = s1.split(' ')
s2 = s2.split(' ')
if(remove_stopword_flag):
if Debug: print "remove_stopword_flag"
s1 = removeStopwords(s1,Stopwords)
s2 = removeStopwords(s2,Stopwords)
else:
if Debug: print "dont remove_stopword_flag"
len1 = len(s1)+1
len2 = len(s2)+1
table = numpy.zeros([len1, len2])
# print len1, len2
for i in range(len2):
table[0, i] = i*p_gap;
for i in range(len1):
table[i, 0] = i*p_gap;
# Gaptimes=0
# Dtimes = 0
for i in range(1, len1):
for j in range(1, len2):
score1 = table[i, j-1] + p_gap
# tic = time.time()
if(similarity_type == '1'):
if Debug: print "glove"
if(two_glove_flag):
if Debug: print "2 glove"
score2 = table[i-1, j-1]+normalized (-1,1,-10,10,get_similarity_from_glove(s1[i-1], s2[j-1], dictionary,glove,secondary_dictionary,secondary_glove))
else:
if Debug: print "1 glove"
score2 = table[i-1, j-1]+normalized (-1,1,-10,10,get_similarity_from_glove(s1[i-1], s2[j-1], dictionary,glove))
else:
if Debug: print "wordnet"
wordnet_score = get_similarity_from_wordnet(s1[i-1], s2[j-1])
if wordnet_score == -1 :
wordnet_score = 0
score2 = table[i-1, j-1]+normalized (0,1,-10,10,wordnet_score)
# toc = time.time()
#print ("time for glove similarity: %r" % (toc-tic))
score3 = table[i-1, j] + p_gap
table[i, j] = max(score1, score2, score3)
# if(table[i, j] ==score2 ):
# Dtimes+=1
# else:
# Gaptimes+=1
# print "Dtimes, Gaptimes : " , Dtimes, Gaptimes
score = table[len1-1, len2-1]
# toc = time.time()
# print('Processing time: %r'
# % (toc - tic))
return score
# return global_alignment(Doc1, Doc2, get_similarity_from_glove, glove, dictionary, p_gap)
def eval(actual,test):
print test
total = len(test)
print total
correct = 0
for i in range(total):
if actual[i] == test[i]:
correct += 1
print "%f%% (%d/%d)" % (float(correct)/total*100, correct, total)
if __name__ == "__main__":
global Stopwords
global dictionary
global secondary_dictionary
global glove
global secondary_glove
global Debug
#Debug = True
Debug = False
test = raw_input('Enter test path: ')
alignment_type_flag =raw_input('Type 1 for global_alignment and 2 for local_alignment: ')
similarity_type = raw_input('Type 1 for glove and 2 for wordnet: ')
if similarity_type == '1':
dic = raw_input('Enter dictionary path: ')
_glove = raw_input('Enter glove path: ')
double_glove_flag = raw_input('Type True is you will be using 2 gloves: ')
if(double_glove_flag== 'True'):
double_glove_flag = True
dic2=raw_input('Enter second dictionary path: ')
_glove2= raw_input('Enter second glove path: ')
secondary_dictionary = read_dictionary(dic2)
secondary_glove = read_glove(_glove2)
else:
double_glove_flag = False
remove_stopword_flag =raw_input('Type True is you will be want to remove stopwords: ')
if(remove_stopword_flag == 'True'):
remove_stopword_flag = True
Stopwords = read_dictionary("Stopwords.txt")
else:
remove_stopword_flag = False
# test=sys.argv[1]
# dic=sys.argv[2]
# _glove=sys.argv[3]
# dic2=sys.argv[4]
# _glove2=sys.argv[5]
# remove_stopword_flag =sys.argv[6]
# categories_flag = sys.argv[7]
# double_glove_flag = sys.argv[8]
# alignment_type_flag = sys.argv[9]
# similarity_type = sys.argv[10]
if similarity_type != '1':
dictionary = None
glove = None
double_glove_flag = False
else:
dictionary = read_dictionary(dic)
glove = read_glove(_glove)
# label, text = read_data(test)
text = read_data_random(test)
tic = time.time()
# result = DocToDoc_Similarity(label,text,categories,remove_stopword_flag,alignment_type_flag ,similarity_type , double_glove_flag)
result = DocToDoc_Similarity_Random(text,remove_stopword_flag,alignment_type_flag ,similarity_type , double_glove_flag)
correct_answer = range(1, len(text), 4)
toc = time.time()
print('Processing time: %r'
% (toc - tic))
# eval(label,result)
eval(correct_answer, result)