-
Notifications
You must be signed in to change notification settings - Fork 0
/
QWSD.py
204 lines (171 loc) · 5.42 KB
/
QWSD.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
# -*- coding: utf-8 -*-
import sys
import numpy as np
import cmath
import lxml.etree as et
from nltk.corpus import wordnet as wn
from scipy.linalg import orth
pos_dic = { 'ADJ': u'a', 'ADV': u'r', 'NOUN': u'n', 'VERB': u'v', }
POS_LIST = pos_dic.values() # ['a', 'r', 'n', 'v']
def load_all_words_data(data_path):
# Partially taken from
# https://github.com/luofuli/word-sense-disambiguation/blob/master/utils/data.py
print('LOADING:',data_path,file=sys.stderr)
context = et.iterparse(data_path, tag='sentence')
data = []
poss = set()
for event, elem in context:
sent_list = []
pos_list = []
for child in elem:
word = child.get('lemma').lower()
sent_list.append(word)
pos = child.get('pos')
pos_list.append(pos)
poss.add(pos)
i = -1
for child in elem:
if child.tag == 'wf':
i += 1
elif child.tag == 'instance':
i += 1
id = child.get('id')
lemma = child.get('lemma').lower()
if '(' in lemma:
print(id)
pos = child.get('pos')
word = lemma + '#' + pos_dic[pos]
context = sent_list[:]
if context[i] != lemma:
print('/'.join(context))
print(i)
print(lemma)
context[i] = '<target>'
x = {
'id': id,
'context': context,
'target_word': word,
'poss': pos_list,
}
data.append(x)
return data
def load_embeddings(fname):
print('LOADING BINARY EMBEDDING from',fname,file=sys.stderr)
word_vecs = {}
with open(fname, "rb") as f:
header = f.readline()
vocab_size, layer1_size = map(int, header.split())
print(vocab_size, int(layer1_size/2), file=sys.stderr)
binary_len = np.dtype('float32').itemsize * layer1_size
for line in range(vocab_size):
if (line%10000 == 0):
print('.',end='',file=sys.stderr)
sys.stderr.flush()
word = []
while True:
ch = f.read(1)
if ch == b' ':
word = b''.join(word)
break
if ch != b'\n':
word.append(ch)
word = word.decode("utf-8")
temp = np.fromstring(f.read(binary_len), dtype='float32')
word_vecs[word] = temp.view(np.complex64)
# NORMALIZE EMBEDDING
word_vecs[word] = (word_vecs[word]/np.linalg.norm(word_vecs[word])).astype(np.complex128)
print('',file=sys.stderr)
return word_vecs, int(layer1_size/2)
def GetWNSenses(word, p, min_sense_freq):
n_hyper = 0
n_hypo = 0
posmap = {'n': wn.NOUN, 'v': wn.VERB, 'a':wn.ADJ, 'r': wn.ADV}
s=wn.synsets(word, pos=posmap[p])
mword = wn.morphy(word, posmap[p])
senses = {}
for lemma in wn.lemmas(word, posmap[p]):
if (lemma.count() > min_sense_freq):
senses[lemma.key()] = lemma.count()
return senses
def BuildSubspacePrj(s,cembs):
# FIND ALL VECTORS CONNECTED WITH A SPECIFIC SENSE
vects = []
for k, cemb in cembs.items():
if (k.find('#')):
sk = k[0:k.find('#')]
if (sk == s):
vects.append(cemb)
if (len(vects) == 0):
print('ERROR 3: id not found',s)
sys.exit(1)
# COMPUTE AN ORTHONORMAL BASIS OF THE SPANNED SPACE
esize = vects[0].size
usedV = min(len(vects),esize-1);
m = np.zeros((esize,usedV), dtype=np.complex64)
for j in range(usedV):
m[:,j] = vects[j]
basis = orth(m)
# COMPUTE THE PROJECTOR ONTO THE SENSE SPACE
mrank = basis.shape[1]
prj = np.zeros((esize,esize), dtype=np.complex64)
basis = np.matrix(basis)
for j in range(mrank):
prj = prj + basis[:,j] * basis[:,j].getH()
return prj, usedV
if __name__ == "__main__":
cembs, edim = load_embeddings(sys.argv[1])
insts = load_all_words_data(sys.argv[2])
print('\n-------------------------------------------------------------',file=sys.stderr)
for i in range(len(insts)):
print('TEST_INSTANCE_ID:',insts[i]['id'],file=sys.stderr)
print('TEST_INSTANCE_TARGET_WORD:',insts[i]['target_word'],file=sys.stderr)
# RETRIEVE ALL POSSIBLE SENSES FOT THE TARGET
tw, pos = insts[i]['target_word'].split('#')
msf = -1
senses = GetWNSenses(tw,pos,msf)
while ((len(senses) == 0) and (msf > -2)):
msf -= 1
senses = GetWNSenses(tw,pos,msf)
if (len(senses) == 0):
print('ERROR 0: No senses into wordnet for instance',insts[i]['id'],file=sys.stderr)
sys.exit(1)
print('Senses:',senses, file=sys.stderr)
if (len(senses) == 1):
print('Only one sense available.', file=sys.stderr)
print(insts[i]['id'], list(senses.keys())[0])
else:
# GET COMPLEX EMBEDDING VECTOR FOR THE TARGET
if (tw in cembs):
wV = np.matrix(cembs.get(tw)).T
else:
print('Warning: Word vector not found. Defaulting...', file=sys.stderr)
wV = np.ones(edim, dtype=np.complex128)
wV = np.matrix(wV / np.linalg.norm(wV)).T
# GET THE PROJECTOR FOR THE CONTEXT
#cntxV = np.matrix(cembs.get(insts[i]['id'])).T
prjC, usedV = BuildSubspacePrj(insts[i]['id'],cembs)
# PROJECTION OVER THE CONTEXT SUBSPACE
#prjC = cntxV * cntxV.getH()
wC = prjC * wV / np.sqrt(wV.getH() * prjC.getH() * prjC * wV)
# COMPUTE SENSE WITH THE BEST PROBABILITY
maxprob = -1.0
for s in senses:
prjS, usedV = BuildSubspacePrj(s,cembs)
prob = np.asscalar(np.real(wC.getH() * prjS.getH() * prjS * wC))
print(s,'->',prob, file=sys.stderr)
if (prob > maxprob):
maxprob = prob
imaxprob = s
if (maxprob == -1.0):
# BACKOFF ON MFS
maxc = -1
for s, c in senses:
if (c > maxc):
maxc = c
imaxprob = s
print('CASE2: Recovering on MFS',imaxprob, file=sys.stderr)
print(insts[i]['id'], imaxprob, file=sys.stderr)
print(insts[i]['id'], imaxprob)
print('-------------------------------------------------------------',file=sys.stderr)
sys.stdout.flush()
sys.stderr.flush()