-
Notifications
You must be signed in to change notification settings - Fork 0
/
bless.py
68 lines (66 loc) · 2.69 KB
/
bless.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
from collections import defaultdict
from scipy import sparse, io, spatial
import math
import numpy
class Bless:
def __init__(self):
self.correctlist = []
self.unknown_target = []
self.unknown_answer = []
self.unknown_cosine = []
self.dubbles = []
self.incorrect = []
self.wordlist = []
self.dictyp = {}
self.t = ''
def run(matrix,dic):
correctlist = []
unknown_target = []
unknown_answer = []
unknown_cosine = []
dubbles = []
incorrect = []
wordlist = []
dictyp = {}
t = ''
with open('BLESS.txt') as bless:
lines = bless.readlines()
while len(lines)>=1:
target, klass, typ, word = lines.pop().split()
target = target.replace('-',' ').split()[0]
word = word.replace('-',' ').split()[0]
typ = typ.replace('-',' ').split()[0]
if(target != t):
if(t!=''):
if dic.has_key(target):
targetvec = matrix[dic[target],:]
highest = 0
answer = ''
for alt in wordlist:
if alt!=target and dic.has_key(alt):
altvec = matrix[dic[alt],:]
altlen = 1 - spatial.distance.cosine(targetvec,altvec)
if math.isnan(altlen):
unknown_cosine.append((target,alt))
if(altlen>highest):
highest = altlen
answer = alt
else:
dubbles.append(alt)
typen = dictyp[answer]
if(typen == 'hyper' or typen == 'coord'):
correctlist.append((target,answer,highest))
else:
incorrect.append((target,answer,highest))
else:
unknown_target.append(target)
t = target
wordlist = []
dictyp = {}
wordlist.append(word)
dictyp[word] = typ
print ('correct:', correctlist)
print ('incorect:', incorrect)
print ('unknowns:', unknown_target, unknown_cosine)
print float(len(correctlist))/float((len(correctlist)+len(incorrect)))
return float(len(correctlist))/float((len(correctlist)+len(incorrect)))