-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvisualize.py
More file actions
136 lines (97 loc) · 3.84 KB
/
Copy pathvisualize.py
File metadata and controls
136 lines (97 loc) · 3.84 KB
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
import pandas as pd
import pickle
from sklearn.metrics.pairwise import cosine_similarity
import plotly
import plotly.graph_objs as go
import numpy as np
from scipy.stats.stats import pearsonr
import argparse
import os
def get_shifts(input_path):
shifts_dict = {}
df_shifts = pd.read_csv(input_path, sep=',', encoding='utf8')
for idx, row in df_shifts.iterrows():
shifts_dict[row['word']] = row['shift_index']
return shifts_dict
def get_cos_dist(words, shifts_dict, path, years):
cds = []
shifts = []
word_list = []
with open(path, 'rb') as f:
vocab_vectors = pickle.load(f)
for w in words:
words_emb = []
for year in years:
year_word = w + '_' + year
if year_word in vocab_vectors:
words_emb.append(vocab_vectors[year_word])
print(w)
cs = cosine_similarity(words_emb[0], words_emb[1])[0][0]
cds.append(1 - cs)
shifts.append(shifts_dict[w])
word_list.append(w)
return cds, shifts, word_list
def visualize(x,y, words):
coef = np.polyfit(x, y, 1)
poly1d_fn = np.poly1d(coef)
trace0 = go.Scatter(
x=x,
y=y,
name='Words',
mode='markers+text',
marker=dict(
size=12,
line=dict(
width=0.5,
),
opacity=0.75,
),
textfont=dict(color="black", size=19),
text=words,
textposition='bottom center'
)
trace1 = go.Scatter(
x=x,
y=poly1d_fn(x),
mode='lines',
name='logistic regression',
)
layout = dict(title='Correlation between gs semantic shifts and calculated shifts',
yaxis=dict(zeroline=False, title= 'Semantic shift index', title_font = {"size": 20},),
xaxis=dict(zeroline=False, title= 'Cosine distance', title_font = {"size": 20},),
hovermode='closest',
)
data = [trace0, trace1]
fig = go.Figure(data=data, layout=layout)
if not os.path.exists('visualizations'):
os.makedirs('visualizations')
plotly.offline.plot(fig, filename='visualizations/liverpool.html')
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--embeddings_path', type=str,
help='Path to output time embeddings',
default='embeddings/liverpool.pickle')
parser.add_argument('--shifts_path', type=str,
help='Path to gold standard semantic shifts path',
default='data/liverpool/liverpool_shift.csv')
args = parser.parse_args()
years = ['2013', '2017']
shifts_dict = get_shifts(args.shifts_path)
words = list(shifts_dict.keys())
cds, shifts, words = get_cos_dist(words, shifts_dict, args.embeddings_path, years)
#don't add text to the graph for these words, makes graph less messy
dont_draw_list = ['stubbornness', 'tourists', 'semifinals', 'desert', 'talents', 'scorpion',
'seeded', 'vomit', 'naked', 'strings', 'alternatives', 'leaks', 'bait', 'erect', 'graduate',
'travel', 'determine', 'explaining', 'soak', 'mouthpuiece', 'congestion', 'revisionism', 'slave',
'revisonist', 'emotion', 'behaviour', 'listen', 'sentence', 'voice', 'relieved', 'mouthpiece', 'astonishing',
'participate', 'implied', 'astonishing', 'revisionist', 'patient', 'preventing', 'accomplish', 'narrative',
'listened', 'egyptian', 'clenched', 'croatian']
filtered_words = []
for w in words:
if w in dont_draw_list:
filtered_words.append('')
else:
filtered_words.append(w)
words = filtered_words
print("Pearson coefficient: ", pearsonr(cds, shifts))
visualize(cds, shifts, words)