-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscore.py
223 lines (199 loc) · 5.9 KB
/
score.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
import collections
import numpy as np
import math
import os
import random
import nltk
from six.moves import urllib
from six.moves import xrange # pylint: disable=redefined-builtin
from pyparsing import StringEnd, oneOf, FollowedBy, Optional, ZeroOrMore, SkipTo
import json, csv
from scipy.stats import spearmanr
import math
def cosine_similarity(v1,v2):
"compute cosine similarity of v1 to v2: (v1 dot v2)/{||v1||*||v2||)"
sumxx, sumxy, sumyy = 0, 0, 0
for i in range(len(v1)):
x = v1[i]; y = v2[i]
sumxx += x*x
sumyy += y*y
sumxy += x*y
return sumxy/max(1e-8,math.sqrt(sumxx*sumyy))
def generatedict():
f = open('./tmp/vocab.txt')
line = f.readline()
vocab = []
wordindex = dict()
index = 0
while line:
word = line.strip().split()[0]
wordindex[word] = index
index = index +1
line = f.readline()
f.close()
wordslist = []
with open('./wordsim353/combined.csv') as csvfile:
filein = csv.reader(csvfile)
index = 0
consim = []
humansim = []
for eles in filein:
if index==0:
index = 1
continue
if (eles[0] not in wordindex) or (eles[1] not in wordindex):
continue
word1 = int(wordindex[eles[0]])
word2 = int(wordindex[eles[1]])
wordslist.append(word1)
wordslist.append(word2)
lines = open('./rw/rw.txt','r').readlines()
index = 0
consim = []
humansim = []
for line in lines:
eles = line.strip().split()
if (eles[0] not in wordindex) or (eles[1] not in wordindex):
continue
word1 = int(wordindex[eles[0]])
word2 = int(wordindex[eles[1]])
wordslist.append(word1)
wordslist.append(word2)
return wordindex, wordslist
def scorefunction(wordindex,embed):
ze = []
with open('./testdata/wordsim353/combined.csv') as csvfile:
filein = csv.reader(csvfile)
index = 0
consim = []
humansim = []
for eles in filein:
if index==0:
index = 1
continue
if (eles[0] not in wordindex) or (eles[1] not in wordindex):
continue
word1 = wordindex[eles[0]]
word2 = wordindex[eles[1]]
humansim.append(float(eles[2]))
value1 = embed[word1]
value2 = embed[word2]
index =index + 1
score = cosine_similarity(value1, value2)
consim.append(score)
cor1, pvalue1 = spearmanr(humansim, consim)
if 1==1:
lines = open('./testdata/rw/rw.txt','r').readlines()
index = 0
consim = []
humansim = []
for line in lines:
eles = line.strip().split()
if (eles[0] not in wordindex) or (eles[1] not in wordindex):
continue
word1 = wordindex[eles[0]]
word2 = wordindex[eles[1]]
humansim.append(float(eles[2]))
value1 = embed[word1]
value2 = embed[word2]
index =index + 1
score = cosine_similarity(value1, value2)
consim.append(score)
cor2, pvalue2 = spearmanr(humansim, consim)
if 1==1:
lines = open('./testdata/rg.csv','r').readlines()
index = 0
consim = []
humansim = []
for line in lines:
eles = line.strip().split(';')
if (eles[0] not in wordindex) or (eles[1] not in wordindex):
continue
word1 = wordindex[eles[0]]
word2 = wordindex[eles[1]]
humansim.append(float(eles[2]))
value1 = embed[word1]
value2 = embed[word2]
index =index + 1
score = cosine_similarity(value1, value2)
consim.append(score)
cor3, pvalue3 = spearmanr(humansim, consim)
if 1==1:
lines = open('./testdata/mc.csv','r').readlines()
index = 0
consim = []
humansim = []
for line in lines:
eles = line.strip().split(';')
if (eles[0] not in wordindex) or (eles[1] not in wordindex):
continue
word1 = wordindex[eles[0]]
word2 = wordindex[eles[1]]
humansim.append(float(eles[2]))
value1 = embed[word1]
value2 = embed[word2]
index =index + 1
score = cosine_similarity(value1, value2)
consim.append(score)
cor4, pvalue3 = spearmanr(humansim, consim)
if 1==1:
lines = open('./testdata/SimLex-999/SimLex-999.txt','r').readlines()
index = 0
consim = []
humansim = []
for line in lines:
eles = line.strip().split('\t')
if (eles[0] not in wordindex) or (eles[1] not in wordindex):
continue
word1 = wordindex[eles[0]]
word2 = wordindex[eles[1]]
humansim.append(float(eles[3]))
value1 = embed[word1]
value2 = embed[word2]
index =index + 1
score = cosine_similarity(value1, value2)
consim.append(score)
cor5, pvalue3 = spearmanr(humansim, consim)
if 1==1:
lines = open('./testdata/MEN/MEN_dataset_natural_form_full','r').readlines()
index = 0
consim = []
humansim = []
for line in lines:
eles = line.strip().split(' ')
if (eles[0] not in wordindex) or (eles[1] not in wordindex):
continue
word1 = wordindex[eles[0]]
word2 = wordindex[eles[1]]
humansim.append(float(eles[2]))
value1 = embed[word1]
value2 = embed[word2]
index =index + 1
score = cosine_similarity(value1, value2)
consim.append(score)
cor6, pvalue3 = spearmanr(humansim, consim)
if 1==1:
lines = open('./testdata/SCWS/ratings.txt','r').readlines()
index = 0
consim = []
humansim = []
for line in lines:
eles = line.strip().split(' ')
ele1 = eles[0].split('\t')[1]
ele2 = eles[0].split('\t')[3]
hscore = eles[-1].split('\t')[1]
if index==0:
index = 1
continue
if (ele1 not in wordindex) or (ele2 not in wordindex):
continue
word1 = wordindex[ele1]
word2 = wordindex[ele2]
humansim.append(float(hscore))
value1 = embed[word1]
value2 = embed[word2]
index =index + 1
score = cosine_similarity(value1, value2)
consim.append(score)
cor7, pvalue1 = spearmanr(humansim, consim)
return cor1,cor2, cor3, cor4,cor5,cor6, cor7