-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
405 lines (322 loc) · 10.9 KB
/
util.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import matplotlib.pyplot as plt
from itertools import accumulate
from collections import defaultdict
import numpy as np
def write_list(aer, path):
f = open(path, 'w')
for rate in aer:
f.write(str(rate) + '\n')
f.close()
def read_list(path):
f = open(path, 'r')
return [float(line.split('\n')[0]) for line in f]
def plot_aer(aers, path):
plt.plot(range(len(aers)), aers)
plt.savefig(path + 'AER-plot.pdf')
plt.clf()
def plot_jump(jump, max_jump, path):
xs = list(map(lambda x : x - max_jump, list(range(len(jump[0])))))
plt.plot(xs, jump[0])
plt.savefig(path + 'jump-plot.pdf')
plt.clf()
def draw_alignment_null(naacl_path, french_path, english_path, fig_path, sure=False, sentence=1):
"""
"""
i = 1
f = open(french_path, 'r')
while i <= sentence:
french = f.readline().split()
i += 1
f.close()
i = 1
e = open(english_path, 'r')
while i <= sentence:
english = e.readline().split()
i += 1
english = ['NULL']+english
e.close()
n = open(naacl_path, 'r')
alignments = defaultdict(list)
line = n.readline()
while line[0] != str(sentence):
line = n.readline().split()
while line[0] != str(sentence+1):
if sure:
if line[-1] != 'P':
alignments[int(line[2])-1].append(int(line[1]))
else:
alignments[int(line[2])-1].append(int(line[1]))
line = n.readline().split()
bitext = ([(french, english)], alignments)
fig = plt.figure(figsize=(8, 4))
ax = plt.axes()
plt.axis('off')
coordinates = get_coordinates(bitext)
lines = [ax.plot(xy[0], xy[1],alpha=0.9,linestyle='-',color='#1a75ff',lw=1.0, solid_capstyle='round')[0] for xy in coordinates['edges']]
ax.scatter(coordinates['x_f']+coordinates['x_e'], coordinates['y_f']+coordinates['y_e'],
s=30, c='white', marker='o', lw=0, alpha=1)
plot_words(ax, coordinates['x_f'], coordinates['y_f'], coordinates['w_f'], 'top')
plot_words(ax, coordinates['x_e'], coordinates['y_e'], coordinates['w_e'], 'bottom')
plt.savefig(fig_path)
def draw_all_alignments(french_path, english_path, fig_path, sure=False, sentence=1):
"""
Draws an alignment that is weighted according to probs of alignment.
"""
i = 1
f = open(french_path, 'r')
while i <= sentence:
french = f.readline().split()
i += 1
f.close()
i = 1
e = open(english_path, 'r')
while i <= sentence:
english = e.readline().split()
i += 1
english = ['NULL']+english
e.close()
alignments = defaultdict(list)
bitext = ([(french, english)], alignments)
fig = plt.figure(figsize=(8, 4))
ax = plt.axes()
plt.axis('off')
coordinates = get_coordinates(bitext, draw_all=True)
raw_line_weights = []
for j, f in enumerate(french):
# for i, e in enumerate(english):
for i, e in enumerate(english):
# for j, f in enumerate(french):
raw_line_weights.append(1./len(english))
# print(len(raw_line_weights))
line_weights = [w*10 for w in raw_line_weights]
print(line_weights)
edge_coords = coordinates['edges']
print(len(edge_coords))
lines = [ax.plot(xy[0], xy[1], alpha=0.9, linewidth=w, linestyle='-', color='#1a75ff', solid_capstyle='round')[0] for xy,w in zip(coordinates['edges'], line_weights)]
ax.scatter(coordinates['x_f']+coordinates['x_e'], coordinates['y_f']+coordinates['y_e'],
s=30, c='white', marker='o', lw=0, alpha=1)
plot_words(ax, coordinates['x_f'], coordinates['y_f'], coordinates['w_f'], 'top')
plot_words(ax, coordinates['x_e'], coordinates['y_e'], coordinates['w_e'], 'bottom')
plt.savefig(fig_path + '.pdf')
plt.clf()
def draw_weighted_alignment_null(model, naacl_path, french_path, english_path, fig_path, sure=False, sentence=1, all_ones=False):
"""
Draws an alignment that is weighted according to probs of alignment.
"""
i = 1
f = open(french_path, 'r')
while i <= sentence:
french = f.readline().split()
i += 1
f.close()
i = 1
e = open(english_path, 'r')
while i <= sentence:
english = e.readline().split()
i += 1
english = ['NULL']+english
e.close()
alignments = defaultdict(list)
bitext = ([(french, english)], alignments)
fig = plt.figure(figsize=(8, 4))
ax = plt.axes()
plt.axis('off')
coordinates = get_coordinates(bitext, draw_all=True)
# get weights of the edges:
F_indices = []
for i in french:
try:
f = model.V_f_indices[i]
except KeyError:
f = model.V_f_indices['-UNK-']
F_indices.append(f)
E_indices = []
for i in english:
try:
e = model.V_e_indices[i]
except KeyError:
e = model.V_e_indices['-UNK-']
E_indices.append(e)
raw_line_weights = []
for j, f in enumerate(F_indices):
# for i, e in enumerate(E_indices):
posterior = model.posterior(f, j, E_indices, F_indices)
for i, e in enumerate(E_indices):
# for j, f in enumerate(F_indices):
p = posterior[i]
raw_line_weights.append(p)
print(len(raw_line_weights))
line_weights = [w*10 for w in raw_line_weights]
print(line_weights)
edge_coords = coordinates['edges']
print(len(edge_coords))
lines = [ax.plot(xy[0], xy[1], alpha=0.9, linewidth=w, linestyle='-', color='#1a75ff', solid_capstyle='round')[0] for xy,w in zip(coordinates['edges'], line_weights)]
ax.scatter(coordinates['x_f']+coordinates['x_e'], coordinates['y_f']+coordinates['y_e'],
s=30, c='white', marker='o', lw=0, alpha=1)
plot_words(ax, coordinates['x_f'], coordinates['y_f'], coordinates['w_f'], 'top')
plot_words(ax, coordinates['x_e'], coordinates['y_e'], coordinates['w_e'], 'bottom')
plt.savefig(fig_path + '.pdf')
plt.clf()
def draw_alignment(naacl_path, french_path, english_path, fig_path, sure=False, sentence=1):
"""
"""
i = 1
f = open(french_path, 'r')
while i <= sentence:
french = f.readline().split()
i += 1
f.close()
i = 1
e = open(english_path, 'r')
while i <= sentence:
english = e.readline().split()
i += 1
e.close()
n = open(naacl_path, 'r')
alignments = defaultdict(list)
line = n.readline()
while line[0] != str(sentence):
line = n.readline().split()
while line[0] != str(sentence+1):
if sure:
if line[-1] != 'P':
alignments[int(line[2])-1].append(int(line[1])-1)
else:
alignments[int(line[2])-1].append(int(line[1])-1)
line = n.readline().split()
bitext = ([(french, english)], alignments)
fig = plt.figure(figsize=(8, 4))
ax = plt.axes()
plt.axis('off')
coordinates = get_coordinates(bitext)
lines = [ax.plot(xy[0], xy[1],alpha=0.9,linestyle='-',color='#1a75ff',linewidth=1.0, solid_capstyle='round')[0] for xy in coordinates['edges']]
ax.scatter(coordinates['x_f']+coordinates['x_e'], coordinates['y_f']+coordinates['y_e'],
s=30, c='white', marker='o', lw=0, alpha=1)
plot_words(ax, coordinates['x_f'], coordinates['y_f'], coordinates['w_f'], 'top')
plot_words(ax, coordinates['x_e'], coordinates['y_e'], coordinates['w_e'], 'bottom')
plt.savefig(fig_path + '.pdf')
def draw_weighted_alignment(model, naacl_path, french_path, english_path, fig_path, sure=False, sentence=1):
"""
Draws an alignment that is weighted according to probs of alignment.
"""
i = 1
f = open(french_path, 'r')
while i <= sentence:
french = f.readline().split()
i += 1
f.close()
i = 1
e = open(english_path, 'r')
while i <= sentence:
english = e.readline().split()
i += 1
e.close()
n = open(naacl_path, 'r')
alignments = defaultdict(list)
line = n.readline()
while line[0] != str(sentence):
line = n.readline().split()
while line[0] != str(sentence+1):
if sure:
if line[-1] != 'P':
alignments[int(line[2])-1].append(int(line[1])-1)
else:
alignments[int(line[2])-1].append(int(line[1])-1)
line = n.readline().split()
bitext = ([(french, english)], alignments)
fig = plt.figure(figsize=(8, 4))
ax = plt.axes()
plt.axis('off')
coordinates = get_coordinates(bitext)
# get weights of the edges:
# F_indices = [model.V_f_indices[i] for i in french]
# E_indices = [model.V_e_indices[i] for i in english]
F_indices = []
for i in french:
try:
f = model.V_f_indices[i]
except KeyError:
f = model.V_f_indices['-UNK-']
F_indices.append(f)
E_indices = []
for i in english:
try:
e = model.V_e_indices[i]
except KeyError:
e = model.V_e_indices['-UNK-']
F_indices.append(e)
raw_line_weights = []
for i, e in enumerate(E_indices):
for f in F_indices:
try:
p = model.posterior(f, E_indices)[i]
except IndexError:
p = 0
raw_line_weights.append(p)
line_weights = [w*10 for w in raw_line_weights]
edge_coords = coordinates['edges']
lines = [ax.plot(xy[0], xy[1], alpha=0.9, linewidth=w, linestyle='-', color='#1a75ff', solid_capstyle='round')[0] for xy,w in zip(coordinates['edges'], line_weights)]
ax.scatter(coordinates['x_f']+coordinates['x_e'], coordinates['y_f']+coordinates['y_e'],
s=30, c='white', marker='o', lw=0, alpha=1)
plot_words(ax, coordinates['x_f'], coordinates['y_f'], coordinates['w_f'], 'top')
plot_words(ax, coordinates['x_e'], coordinates['y_e'], coordinates['w_e'], 'bottom')
plt.savefig(fig_path)
plt.clf()
def plot_words(axes, xs, ys, words, vertical_position, weight='bold', color='black'):
"""
Code borrowed from: https://github.com/INFR11133/lab1
Plots a pair of sentences at given positions, on a given axes object.
Args:
axes (pyplotlib axes object): axes on which to plot
xs (list of floats): x coordinates
ys (list of floats): y coordinates
words (list of strings): words to be displayed
vertical_position (string): where words should be displayed relative to point coordinates
weight (string): font weight
color (string or list of strings): color/s to be used for displaying words
"""
for n in range(0, len(words)):
axes.text(xs[n], ys[n], words[n], size=9, family='sans-serif',
weight=weight, color=color,
horizontalalignment='center',
verticalalignment=vertical_position)
def get_coordinates(bitext, draw_all=False, one_sent=False, sent_index=0, word_index=0):
"""
Code borrowed from: https://github.com/INFR11133/lab1
Generates x and y coordinates to be used for plotting sentence pairs
and alignment links.
Args:
bitext (list of tuples): list of translation pairs
one_sent (Boolean): whether coordinates ahould be generated for one
selected sentence pair and one word in it, or for the whole bitext
sent_index (int): index of the selected sentence pair
word_index (int): index of the target foreign word
"""
x_positions_f = []
y_positions_f = []
x_positions_e = []
y_positions_e = []
edge_pos = []
words_f = []
words_e = []
sents, alignments = bitext
for (n, (f, e)) in enumerate(sents):
for j in range(0, len(f)):
x_positions_f.append(j+1)
y_positions_f.append((3*n)-2)
words_f.append(f[j])
if (not one_sent) or (one_sent and word_index==j):
for i in range(0, len(e)):
if draw_all:
edge_pos.append([[j+1, i+1], [(3*n)-1.9, (3*n)-1.1]])
else:
if i in alignments[j]:
edge_pos.append([[j+1, i+1], [(3*n)-1.9, (3*n)-1.1]])
for i in range(0, len(e)):
x_positions_e.append(i+1)
y_positions_e.append((3*n)-1)
words_e.append(e[i])
coord_dict = {'x_f': x_positions_f, 'x_e': x_positions_e,
'y_f': y_positions_f, 'y_e': y_positions_e,
'edges': edge_pos, 'w_f': words_f, 'w_e': words_e}
return coord_dict