-
Notifications
You must be signed in to change notification settings - Fork 1
/
compute_indices.py
318 lines (238 loc) · 9.85 KB
/
compute_indices.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import argparse
import heapq
import json
import os
import pickle
from itertools import repeat
from multiprocessing import Pool, Process
import enchant
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from gensim.models import KeyedVectors
from nltk.corpus import wordnet
from scipy.spatial import procrustes
from scipy.stats import linregress
from sklearn.metrics.pairwise import cosine_similarity
en_us = enchant.Dict("en_US")
en_uk = enchant.Dict("en_UK")
MODEL_NAMES = [
'Level 1.model',
'Level 2.model',
'Level 3.model',
'Level 4.model',
'Level 5.model',
'Level 6.model',
'Level 7.model',
'Level 8.model',
'Level 9.model',
'Level 10.model'
]
BASELINE = MODEL_NAMES[-1]
def load_model(filepath):
return KeyedVectors.load(filepath)
def keys(model):
return list(model.wv.vocab.keys())
def check_if_english_word(word):
return en_us.check(word) or en_uk.check(word)
def get_vector_space(model, words):
vector_space = np.zeros((len(words), len(model[keys(model)[0]])))
for i, word in enumerate(words):
if hasattr(model, 'wv'):
# Word2Vec
wv = model.wv
vector_size = model.vector_size
else:
wv = model
vector_size = model[list(model.keys())[0]].shape
if word not in wv:
vector_space[i] = np.zeros(vector_size)
else:
vector_space[i] = wv[word]
return vector_space
def top_k_cosines(word, k, pw_cosines, word_indices):
top_k = []
word_idx = word_indices[word]
for w in word_indices:
if w == word:
continue
idx = word_indices[w]
cos_sim = float(pw_cosines[word_idx, idx])
if len(top_k) < k:
heapq.heappush(top_k, (cos_sim, w))
else:
heapq.heappushpop(top_k, (cos_sim, w))
top_k.sort(key=lambda t: t[0], reverse=True)
return [(w, c) for c, w in top_k]
def thresholded_cosines(word, pw_cosines, word_indices, thresh=.3):
word_idx = word_indices[word]
cosines = 0
cosine_sum = 0
for w in word_indices:
if w == word:
continue
idx = word_indices[w]
cos_sim = float(pw_cosines[word_idx, idx])
if cos_sim >= thresh:
cosines += 1
cosine_sum += cos_sim
if cosines > 0:
cosine_sum /= cosines
return cosines, cosine_sum
def get_intermediate_values(word, mature_vs, rotations, word_indices):
values = []
mature_arr = mature_vs[word_indices[BASELINE][word]].reshape((1, -1))
for model in rotations:
if model == BASELINE:
continue
if word not in word_indices[model]:
values.append(0)
continue
int_arr = rotations[model][word_indices[model][word]].reshape((1, -1))
cos_sim = float(cosine_similarity(mature_arr, int_arr))
values.append(cos_sim)
return values
def inverse_average(word, mature_vs, rotations, word_indices):
values = get_intermediate_values(word, mature_vs, rotations, word_indices)
return 1 - np.mean(values)
def inverse_slope(word, mature_vs, rotations, word_indices):
values = get_intermediate_values(word, mature_vs, rotations, word_indices)
slope, _, _, _, _ = linregress(np.arange(len(values)), values)
if slope == 0:
return (1 << 31) - 1
return 1 / slope
def consecutive_index_above_threshold(word, mature_vs, rotations, word_indices, thresh=.4):
values = get_intermediate_values(word, mature_vs, rotations, word_indices)
for i in range(len(values) - 1):
if values[i] >= thresh and values[i + 1] >= thresh:
return i
return len(values)
def index_above_threshold(word, mature_vs, rotations, word_indices, thresh=.4):
values = get_intermediate_values(word, mature_vs, rotations, word_indices)
for i, value in enumerate(values):
if value >= thresh:
return i
return len(values)
def align_models(models, top_words, disparities_file):
rotations = {}
rotation_word_indices = {}
disparities = []
for model in MODEL_NAMES[:-1]:
intermediate_model = models[model]
mature_model = models[BASELINE]
supplementary_words = list(
set(keys(intermediate_model)) - set(top_words))
supplementary_words = list(
filter(check_if_english_word, supplementary_words))
vs_intermediate = get_vector_space(
intermediate_model, top_words + supplementary_words)
vs_mature = get_vector_space(mature_model, top_words)
# Add zeros to the mature model to only rotate on top words
vs_mature = np.vstack([vs_mature, np.zeros(
(len(supplementary_words), vs_mature.shape[1]))])
mtx1, mtx2, disparity = procrustes(vs_mature, vs_intermediate)
rotations[model] = mtx2
rotation_word_indices[model] = {
word: index for index, word in enumerate(top_words + supplementary_words)}
disparities.append(disparity)
with open(disparities_file, 'wt') as fout:
json.dump(disparities, fout)
return rotations, rotation_word_indices
def process_word(word):
top3 = top_k_cosines(
word, 3, pw_cosines[BASELINE], rotation_word_indices[BASELINE])
inv_avg = inverse_average(
word, mature_vs, rotations, rotation_word_indices)
avg = 1 - inv_avg
inv_slope = inverse_slope(
word, mature_vs, rotations, rotation_word_indices)
slope = 1 / inv_slope
idx = index_above_threshold(
word, mature_vs, rotations, rotation_word_indices, thresh=.4)
cidxs = [
consecutive_index_above_threshold(
word, mature_vs, rotations, rotation_word_indices, thresh=thresh)
for thresh in [.3, .35, .4, .45, .5, .55, .6, .65, .7]]
cosines_above_thresh, avg_cosine_above_thresh = thresholded_cosines(
word, pw_cosines[BASELINE], rotation_word_indices[BASELINE], thresh=0.3)
intermediate_values = get_intermediate_values(
word, mature_vs, rotations, rotation_word_indices)
result = []
result.append(word)
for w, c in top3:
result.append(w)
result.append(c)
result.append(float(np.mean([e[1] for e in top3])))
result.append(inv_avg)
result.append(1 - inv_avg)
result.append(inv_slope)
result.append(1 / inv_slope)
result.append(idx)
result.extend(cidxs)
result.append(cosines_above_thresh)
result.append(avg_cosine_above_thresh)
result.extend(intermediate_values)
return result
def compute_indices(all_words, output_file, workers=4):
global pw_cosines, mature_vs, rotations, rotation_word_indices
columns = ['lemmatized word',
'highest cosine word', 'highest cosine word similarity',
'2nd highest cosine word', '2nd highest cosine word similarity',
'3rd highest cosine word', '3rd highest cosine word similarity',
'average top3 cosine', 'inverse average', 'average',
'inverse slope', 'slope', 'index above .4 threshold',
'continuous index above .3 threshold', 'continuous index above .35 threshold',
'continuous index above .4 threshold', 'continuous index above .45 threshold',
'continuous index above .5 threshold', 'continuous index above .55 threshold',
'continuous index above .6 threshold', 'continuous index above .65 threshold',
'continuous index above .7 threshold', 'number of cosines above .3 threshold',
'average cosine above .3 threshold',
*[f'intermediate cosine similarity {i + 1}' for i in range(len(models) - 1)]]
data = {column: [] for column in columns}
pool = Pool(processes=workers)
results = pool.map_async(process_word, all_words)
results.wait()
for result in results.get():
for i, column in enumerate(data):
data[column].append(result[i])
df = pd.DataFrame.from_records(data)[columns]
df.sort_values(by='lemmatized word', inplace=True)
df.to_csv(output_file)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--input_models_dir', '-i', type=str,
default='quantile_models_cds_sorted')
parser.add_argument('--output_file', '-o', type=str,
default='quantiles_tasa_coca_cds.csv')
parser.add_argument('--frequencies_file', '-f', type=str,
default='term_frequencies.json')
parser.add_argument('--disparities_output_file', '-d', type=str,
default='disparities_tasa_coca_cds.json')
parser.add_argument('--workers', type=int, default=8)
return parser.parse_args()
if __name__ == '__main__':
global pw_cosines, mature_vs, rotations, rotation_word_indices
args = parse_args()
WV_MODELS_DIR = args.input_models_dir
OUTPUT_FILE = args.output_file
FREQUENCIES_FILE = args.frequencies_file
DISPARITIES_OUTPUT_FILE = args.disparities_output_file
workers = args.workers
models = {model_name: load_model(os.path.join(WV_MODELS_DIR, model_name))
for model_name in MODEL_NAMES}
top_words = keys(models[BASELINE])
with open(FREQUENCIES_FILE, 'rt') as fin:
frequencies = json.load(fin)
rotations, rotation_word_indices = align_models(models,
top_words,
DISPARITIES_OUTPUT_FILE)
all_words = keys(models[BASELINE])
all_words = list(filter(check_if_english_word, all_words))
all_words.sort() # lexicographic ordering
mature_vs = get_vector_space(models[BASELINE], all_words)
rotation_word_indices[BASELINE] = {
word: index for index, word in enumerate(all_words)}
pw_cosines = {BASELINE: cosine_similarity(mature_vs)}
print("Number of words to analyze:", len(all_words))
compute_indices(all_words, OUTPUT_FILE, workers=workers)