-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathvectorspace.py
executable file
·192 lines (142 loc) · 6.03 KB
/
vectorspace.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
# import mkl
# mkl.set_num_threads(8)
from collections import defaultdict
import numpy as np
def get_sk_type(sensekey):
return int(sensekey.split('%')[1].split(':')[0])
def get_sk_pos(sk, tagtype='long'):
# merges ADJ with ADJ_SAT
if tagtype == 'long':
type2pos = {1: 'NOUN', 2: 'VERB', 3: 'ADJ', 4: 'ADV', 5: 'ADJ'}
return type2pos[get_sk_type(sk)]
elif tagtype == 'short':
type2pos = {1: 'n', 2: 'v', 3: 's', 4: 'r', 5: 's'}
return type2pos[get_sk_type(sk)]
def get_sk_lemma(sensekey):
return sensekey.split('%')[0]
class SensesVSM(object):
def __init__(self, vecs_path, normalize=True):
self.vecs_path = vecs_path
self.labels = []
self.vectors = np.array([], dtype=np.float32)
self.indices = {}
self.ndims = 0
if self.vecs_path.endswith('.txt'):
self.load_txt(self.vecs_path)
elif self.vecs_path.endswith('.npz'):
self.load_npz(self.vecs_path)
self.load_aux_senses()
if normalize:
self.normalize()
def load_txt(self, txt_vecs_path):
self.vectors = []
with open(txt_vecs_path, encoding='utf-8') as vecs_f:
for line_idx, line in enumerate(vecs_f):
elems = line.split()
self.labels.append(elems[0])
self.vectors.append(np.array(list(map(float, elems[1:])), dtype=np.float32))
self.vectors = np.vstack(self.vectors)
self.labels_set = set(self.labels)
self.indices = {l: i for i, l in enumerate(self.labels)}
self.ndims = self.vectors.shape[1]
def load_npz(self, npz_vecs_path):
loader = np.load(npz_vecs_path)
self.labels = loader['labels'].tolist()
self.vectors = loader['vectors']
self.labels_set = set(self.labels)
self.indices = {l: i for i, l in enumerate(self.labels)}
self.ndims = self.vectors.shape[1]
def load_aux_senses(self):
self.sk_lemmas = {sk: get_sk_lemma(sk) for sk in self.labels}
self.sk_postags = {sk: get_sk_pos(sk) for sk in self.labels}
self.lemma_sks = defaultdict(list)
for sk, lemma in self.sk_lemmas.items():
self.lemma_sks[lemma].append(sk)
self.known_lemmas = set(self.lemma_sks.keys())
self.sks_by_pos = defaultdict(list)
for s in self.labels:
self.sks_by_pos[self.sk_postags[s]].append(s)
self.known_postags = set(self.sks_by_pos.keys())
def save_npz(self):
npz_path = self.vecs_path.replace('.txt', '.npz')
np.savez_compressed(npz_path,
labels=self.labels,
vectors=self.vectors)
def normalize(self, norm='l2'):
norms = np.linalg.norm(self.vectors, axis=1)
self.vectors = (self.vectors.T / norms).T
def get_vec(self, label):
return self.vectors[self.indices[label]]
def similarity(self, label1, label2):
v1 = self.get_vec(label1)
v2 = self.get_vec(label2)
return np.dot(v1, v2).tolist()
def match_senses(self, vec, lemma=None, postag=None, topn=100):
relevant_sks = []
for sk in self.labels:
if (lemma is None) or (self.sk_lemmas[sk] == lemma):
if (postag is None) or (self.sk_postags[sk] == postag):
relevant_sks.append(sk)
relevant_sks_idxs = [self.indices[sk] for sk in relevant_sks]
sims = np.dot(self.vectors[relevant_sks_idxs], np.array(vec))
matches = list(zip(relevant_sks, sims))
matches = sorted(matches, key=lambda x: x[1], reverse=True)
return matches[:topn]
def most_similar_vec(self, vec, topn=10):
sims = np.dot(self.vectors, vec).astype(np.float32)
sims_ = sims.tolist()
r = []
for top_i in sims.argsort().tolist()[::-1][:topn]:
r.append((self.labels[top_i], sims_[top_i]))
return r
def sims(self, vec):
return np.dot(self.vectors, np.array(vec)).tolist()
class VSM(object):
# def __init__(self, vecs_path, normalize=True):
def __init__(self):
self.labels = []
self.vectors = np.array([], dtype=np.float32)
self.indices = {}
self.ndims = 0
# self.load_txt(vecs_path)
# if normalize:
# self.normalize()
def load(self, vectors, labels):
self.vectors = vectors
self.labels = labels
self.indices = {l: i for i, l in enumerate(self.labels)}
self.ndims = self.vectors.shape[1]
def load_txt(self, vecs_path):
self.vectors = []
with open(vecs_path, encoding='utf-8') as vecs_f:
for line_idx, line in enumerate(vecs_f):
elems = line.split()
self.labels.append(elems[0])
self.vectors.append(np.array(list(map(float, elems[1:])), dtype=np.float32))
self.labels_set = set(self.labels)
self.vectors = np.vstack(self.vectors)
self.indices = {l: i for i, l in enumerate(self.labels)}
self.ndims = self.vectors.shape[1]
def normalize(self, norm='l2'):
self.vectors = (self.vectors.T / np.linalg.norm(self.vectors, axis=1)).T
def get_vec(self, label):
return self.vectors[self.indices[label]]
def similarity(self, label1, label2):
v1 = self.get_vec(label1)
v2 = self.get_vec(label2)
return np.dot(v1, v2).tolist()
def most_similar_vec(self, vec, topn=10):
sims = np.dot(self.vectors, vec).astype(np.float32)
sims_ = sims.tolist()
r = []
for top_i in sims.argsort().tolist()[::-1][:topn]:
r.append((self.labels[top_i], sims_[top_i]))
return r
def most_similar(self, label, topn=10):
return self.most_similar_vec(self.get_vec(label), topn=topn)
def sims(self, vec):
return np.dot(self.vectors, np.array(vec)).tolist()
if __name__ == '__main__':
vecs_path = 'data/vectors/bert-large-cased/lmms-sp-wsd.bert-large-cased.vectors.txt'
vsm = SensesVSM(vecs_path)
print(len(np.unique(vsm.vectors, axis=0)))