-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathqvec.py
executable file
·207 lines (172 loc) · 6.45 KB
/
qvec.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
#!/usr/bin/env python3
"""
./qvec.py --in_vectors <filename> --in_oracle <filename> --interpret
"""
import argparse
import json
import numpy as np
from scipy.stats.stats import pearsonr
from scipy import spatial
import time
import gzip
import sys
parser = argparse.ArgumentParser()
parser.add_argument("--in_vectors", default="vectors/w2v_sg_1b_100.txt")
parser.add_argument("--in_oracle", default="oracles/semcor_noun_verb.supersenses.en", help="comma-separated list of linguistic annotation files, each is in format word \\t json dictionary of linguistic features")
parser.add_argument("--distance_metric", default="correlation",
help="correlation, abs_correlation, cosine")
parser.add_argument("--interpret", action='store_true')
parser.add_argument("--top", type=int, default=100)
parser.add_argument("--verbose", action='store_true')
args = parser.parse_args()
class TopK(object):
def __init__(self, k):
self.k = k
self.elements = []
self.sorted = False
def Push(self, word, value):
if len(self.elements) < self.k:
self.elements.append((word, value))
else:
self.Sort()
if self.elements[-1][1] < value:
self.elements[-1] = (word, value)
self.sorted = False
def Sort(self):
if not self.sorted:
self.elements = sorted(self.elements, key=lambda x: x[1], reverse=True)
self.sorted = True
def GetSortedElements(self):
self.Sort()
return self.elements
class Matrix(object):
def __init__(self):
self.matrix = {} #key - word; value = dict with key - column num, value - val
self.number_of_columns = 0
def Column(self, dim, vocab):
#return dimension based on vocab
column = []
for word in sorted(vocab):
column.append(self.matrix[word].get(dim, 0.0))
return column
def __repr__(self):
result = []
for word in sorted(self.matrix):
line = [word]
features = self.matrix[word]
for col in range(self.number_of_columns):
line.append(str(features.get(col, 0.0)))
result.append(" ".join(line))
return "\n".join(result)
class OracleMatrix(Matrix):
def __init__(self):
super().__init__()
self.column_names = []
def AddMatrix(self, filename):
#filename format: headache {"WN_noun.cognition": 0.5, "WN_noun.state": 0.5}
for line in open(filename):
word, json_line = line.strip().split("\t")
json_features = json.loads(json_line)
features = {}
if word in self.matrix:
features = self.matrix[word]
for feature_name, feature_val in json_features.items():
if feature_name in self.column_names:
column_num = self.column_names.index(feature_name)
else:
column_num = len(self.column_names)
self.column_names.append(feature_name)
self.number_of_columns += 1
if args.verbose:
print(" Added new oracle column:", feature_name, "at index", column_num )
features[column_num] = feature_val
self.matrix[word] = features
class VectorMatrix(Matrix):
def AddMatrix(self, filename, top_k=0):
#filename format: biennials -0.11809 0.089522 -0.026722 0.075579 -0.02453
binary_file = False
if filename.endswith(".gz"):
f = gzip.open(filename, "rb")
binary_file = True
else:
f = open(filename)
self.best_in_column = []
for line in f:
tokens = line.strip().split()
if len(tokens) == 2: #ignore w2v first line
continue
word = tokens[0]
if binary_file:
word = word.decode("utf-8")
self.number_of_columns = len(tokens)-1
if top_k and len(self.best_in_column) == 0:
self.best_in_column = [TopK(top_k) for _ in range(self.number_of_columns)]
features = {}
for dim, val in enumerate(tokens[1:]):
val = float(val)
features[dim] = val
if top_k:
self.best_in_column[dim].Push(word, val)
self.matrix[word] = features
def Similarity(v1, v2, metric="correlation"):
def IsZero(v):
return all(n == 0 for n in v)
if metric == "correlation":
if IsZero(v1) or IsZero(v2):
return 0.0
return pearsonr(v1, v2)[0]
if metric == "abs_correlation":
if IsZero(v1) or IsZero(v2):
return 0.0
return abs(pearsonr(v1, v2)[0])
if metric == "cosine":
return spatial.distance.cosine(v1, v2)
def SimilarityMatrix(vsm_matrix, oracle_matrix, distance_metric="correlation"):
similarity_matrix = np.zeros((vsm_matrix.number_of_columns, oracle_matrix.number_of_columns))
vocabulary = vsm_matrix.matrix.keys() & oracle_matrix.matrix.keys()
for i in range(vsm_matrix.number_of_columns):
for j in range(oracle_matrix.number_of_columns):
similarity_matrix[i,j] = Similarity(vsm_matrix.Column(i, vocabulary),
oracle_matrix.Column(j, vocabulary), distance_metric)
return similarity_matrix
def AlignColumns(vsm_matrix, oracle_matrix, distance_metric):
similarity_matrix = SimilarityMatrix(vsm_matrix, oracle_matrix,
distance_metric=distance_metric)
total_score = 0
alignments = []
for i in range(vsm_matrix.number_of_columns):
best_oracle_column = np.argmax(similarity_matrix[i])
similarity = similarity_matrix[i, best_oracle_column]
alignments.append((best_oracle_column, similarity))
total_score += similarity
return alignments, total_score
def main():
start = time.time()
distance_metric = args.distance_metric
oracle_matrix = OracleMatrix()
for filename in args.in_oracle.strip().split(","):
if args.verbose:
print("Loading oracle matrix:", filename)
oracle_matrix.AddMatrix(filename)
vsm_matrix = VectorMatrix()
if args.verbose:
print("Loading VSM file:", args.in_vectors)
top_k = args.top if args.interpret else 0
vsm_matrix.AddMatrix(args.in_vectors, top_k)
alignments, score = AlignColumns(vsm_matrix, oracle_matrix, distance_metric)
print("QVEC score: %g" % score)
if args.interpret:
print("\t".join(["Dimension", "Aligned_oracle_column", "Similarity", "Top-N_words"]))
for i in range(vsm_matrix.number_of_columns):
top_words = []
if vsm_matrix.best_in_column:
top_words = [word for (word, value) in vsm_matrix.best_in_column[i].GetSortedElements()]
print("{}\t{}\t{}\t{}".format(
i,
oracle_matrix.column_names[alignments[i][0]],
alignments[i][1],
" ".join(top_words)))
if args.verbose:
print("Computation time: ", time.time() - start)
if __name__ == '__main__':
main()