def evaluate1Word(wv, reference):
"""Evaluate wv against reference, return (rho, count) where rwo is
Spearman's rho and count is the number of reference word pairs
that could be evaluated against.
"""
count=0
gold, predicted = [], []
for words, sim in sorted(reference, key=lambda ws: ws[1]):
if " " not in words[0] and " " not in words[1]:
#print words[0],words[1]
try:
v1, v2 = wv[words[0]], wv[words[1]]
except KeyError:
count+=1
continue
#print words
gold.append((words, sim))
predicted.append((words, cosine(v1, v2)))
simlist = lambda ws: [s for w,s in ws]
rho, p = spearmanr(simlist(gold), simlist(predicted))
print "Word not found in WordVector",count
return (rho, len(gold))
评论列表
文章目录