-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloaddoc2vec.py
More file actions
239 lines (204 loc) · 5.5 KB
/
loaddoc2vec.py
File metadata and controls
239 lines (204 loc) · 5.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
import logging
import gensim, os
import xml.etree.cElementTree
from nltk.corpus import stopwords
from string import ascii_lowercase
from collections import namedtuple
from gensim.models.doc2vec import TaggedDocument, Doc2Vec
from sklearn import svm
import pickle
import networkx as nx
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
dictt = {}
corpora_documents = []
st1 = "edges.txt"
st2 = "merger2.txt"
st3 = "rank_nodes_pagerank_without_weight.txt"
st4 = "inverse_rankings.txt"
st5 = "time_diff.txt"
st6 = "ans_count.txt"
st7 = "nlp_score.txt"
nooffeatures = 12
directed_graph = nx.DiGraph()
pagerank_dict = {}
leader_fol_dict = {}
bw_centrality = {}
accepted_answer_id = {}
time_diff_of_accepted = {}
nlp_dict = {}
degree = {}
degsu = 1
graph_file = open(st1)
for line in graph_file:
try:
v1 = int(line.split(" ")[0])
v2 = int(line.split(" ")[1])
directed_graph.add_edge(v1, v2)
if( v1 in degree ):
degree[v1] += 1
else:
degree[v1] = 1
if( v2 in degree ):
degree[v2] += 1
degsu = max(degsu, degree[v2])
else:
degree[v2] = 1
except Exception:
pass
for key, value in degree.items():
degree[key] = float(value)/degsu
pagerank_file = open(st3)
for line in pagerank_file:
try:
v1 = int(line.split(" ")[0])
pagerankk = float(line.split(" ")[1])
#pagerankk = "%.8f" % ( float(line.split(" ")[1]) )
pagerank_dict[ v1 ] = pagerankk
except Exception:
pass
max1 = 0
leader_follower_file = open(st4)
for line in leader_follower_file:
try:
v1 = int(line.split(" ")[0])
leader_fol_score = float(line.split(" ")[1])
max1 = max(max1, leader_fol_score)
except Exception:
pass
leader_follower_file = open(st4)
for line in leader_follower_file:
try:
v1 = int(line.split(" ")[0])
leader_fol_score = float(line.split(" ")[1])
leader_fol_dict[ v1 ] = float(leader_fol_score)/max1
except Exception:
pass
max1 = 0
time_diff_acc = open(st5)
for line in time_diff_acc:
try:
v1 = int(line.split(" ")[0])
v2 = float(line.split(" ")[1])
max1 = max(max1, v2)
except Exception:
pass
time_diff_acc = open(st5)
for line in time_diff_acc:
v1 = int(line.split(" ")[0])
v2 = float(line.split(" ")[1])
time_diff_of_accepted[ v1 ] = float(v2)/max1
max1 = 0
answer_count = open(st6)
for line in answer_count:
v1 = int(line.split(" ")[0])
v2 = int(line.split(" ")[1])
max1 = max(max1, v2)
answer_count = open(st6)
for line in answer_count:
v1 = int(line.split(" ")[0])
v2 = int(line.split(" ")[1])
accepted_answer_id[ v1 ] = float(v2)/max1
max1 = 0
nlpp = open(st7)
for line in nlpp:
v1 = int(line.split(" ")[0])
v2 = float(line.split(" ")[1])
max1 = max(max1, v2)
nlpp = open(st7)
for line in nlpp:
v1 = int(line.split(" ")[0])
v2 = float(line.split(" ")[1])
nlp_dict[ v1 ] = float(v2)/max1
def check2( vert ):
if( vert in accepted_answer_id ):
return accepted_answer_id[vert]
return 0
def check3( vert ):
if( vert in time_diff_of_accepted ):
return time_diff_of_accepted[vert]
return 1
def check4( vert ):
if( vert in nlp_dict ):
return nlp_dict[vert]
return 0
def getfeatures(vert1, vert2):
vect = []
#vect.append( bw_centrality.get(vert1) )
#vect.append( bw_centrality.get(vert2) )
if( vert1 in leader_fol_dict ):
vect.append( leader_fol_dict.get(vert1) )
else:
vect.append(0)
if( vert2 in leader_fol_dict ):
vect.append( leader_fol_dict.get(vert2) )
else:
vect.append(0)
if( vert1 in pagerank_dict ):
vect.append( pagerank_dict.get(vert1) )
else:
vect.append(0)
if( vert2 in pagerank_dict ):
vect.append( pagerank_dict.get(vert2) )
else:
vect.append(0)
if( vert1 is degree ):
vect.append( degree[vert1] )
else:
vect.append(0)
if( vert2 is degree ):
vect.append( degree[vert2] )
else:
vect.append(0)
vect.append( check2(vert1) )
vect.append( check2(vert2) )
vect.append( check3(vert1) )
vect.append( check3(vert2) )
vect.append( check4(vert1) )
vect.append( check4(vert2) )
return vect
with open("dictidbody.dump", "rb") as fp: # Unpickling
dictt = pickle.load(fp)
# load the model back
model_loaded = Doc2Vec.load('mymodel2')
with open("SVM_Classifierso3.dump", "rb") as fp: # Unpickling
clf2 = pickle.load(fp)
print('Checking')
correct = 0
noofval = 0
rejected = 0
tested_graph_file = open(st2)
for line in tested_graph_file:
v1 = int(line.split(" ")[0])
v2 = int(line.split(" ")[1])
v3 = int(line.split(" ")[2])
token1 = dictt[v1].split()
new_vector1 = model.infer_vector(token1)
token2 = dictt[v2].split()
new_vector2 = model.infer_vector(token2)
sims1 = model.docvecs.most_similar([new_vector1]) # gives you top 10 document tags and their cosine similarity
sims2 = model.docvecs.most_similar([new_vector2]) # gives you top 10 document tags and their cosine similarity
itoj = 0
jtoi = 0
for i in sims1:
for j in sims2:
img = getfeatures(i[0], j[0])
try:
labell = clf2.predict([img])
if( labell==j[0] ):
itoj += 1
else:
jtoi += 1
except Exception:
rejected += 1
pass
if( v3==v2 ):
if( itoj>jtoi ):
correct += 1
else:
if( jtoi>itoj ):
correct += 1
noofval += 1
print( correct, noofval, rejected )
print( (float(correct)/noofval) )