-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
655 lines (581 loc) · 24.3 KB
/
utils.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
import re
import random
from collections import Counter
from itertools import groupby
import numpy as np
import torch
import torch.nn as nn
from torch.nn.init import *
class ConllEntry:
def __init__(self, id, form, lemma, cpos, pos, feats=None, parent_id=None, relation=None, deps=None, misc=None):
self.id = id
self.form = form
self.norm = normalize(form)
self.cpos = cpos.upper()
self.pos = pos.upper()
self.parent_id = parent_id
self.relation = relation
self.lemma = lemma
self.feats = feats
self.deps = deps
self.misc = misc
self.pred_parent_id = None
self.pred_relation = None
def __str__(self):
values = [str(self.id), self.form, self.lemma, self.cpos, self.pos, self.feats,
str(self.pred_parent_id) if self.pred_parent_id is not None else None, self.pred_relation, self.deps,
self.misc]
return '\t'.join(['_' if v is None else v for v in values])
numberRegex = re.compile("[0-9]+|[0-9]+\\.[0-9]+|[0-9]+[0-9,]+");
def normalize(word):
return 'NUM' if numberRegex.match(word) else word.lower()
def memoize(func):
mem = {}
def helper(*args, **kwargs):
key = (args, frozenset(kwargs.items()))
if key not in mem:
mem[key] = func(*args, **kwargs)
return mem[key]
return helper
def construct_update_batch_data(data_list, batch_size):
random.shuffle(data_list)
batch_data = []
len_datas = len(data_list)
num_batch = len_datas // batch_size
if not len_datas % batch_size == 0:
num_batch += 1
for i in range(num_batch):
start_idx = i * batch_size
end_idx = min(len_datas, (i + 1) * batch_size)
batch_data.append(data_list[start_idx:end_idx])
return batch_data
@memoize
def constituent_index(sentence_length, multiroot):
counter_id = 0
basic_span = []
id_2_span = {}
for left_idx in range(sentence_length):
for right_idx in range(left_idx, sentence_length):
for dir in range(2):
id_2_span[counter_id] = (left_idx, right_idx, dir)
counter_id += 1
span_2_id = {s: id for id, s in id_2_span.items()}
for i in range(sentence_length):
if i != 0:
id = span_2_id.get((i, i, 0))
basic_span.append(id)
id = span_2_id.get((i, i, 1))
basic_span.append(id)
ijss = []
ikcs = [[] for _ in range(counter_id)]
ikis = [[] for _ in range(counter_id)]
kjcs = [[] for _ in range(counter_id)]
kjis = [[] for _ in range(counter_id)]
for l in range(1, sentence_length):
for i in range(sentence_length - l):
j = i + l
for dir in range(2):
ids = span_2_id[(i, j, dir)]
for k in range(i, j + 1):
if dir == 0:
if k < j:
# two complete spans to form an incomplete span
idli = span_2_id[(i, k, dir + 1)]
ikis[ids].append(idli)
idri = span_2_id[(k + 1, j, dir)]
kjis[ids].append(idri)
# one complete span,one incomplete span to form a complete span
idlc = span_2_id[(i, k, dir)]
ikcs[ids].append(idlc)
idrc = span_2_id[(k, j, dir)]
kjcs[ids].append(idrc)
else:
if k < j and ((not (i == 0 and k != 0) and not multiroot) or multiroot):
# two complete spans to form an incomplete span
idli = span_2_id[(i, k, dir)]
ikis[ids].append(idli)
idri = span_2_id[(k + 1, j, dir - 1)]
kjis[ids].append(idri)
if k > i:
# one incomplete span,one complete span to form a complete span
idlc = span_2_id[(i, k, dir)]
ikcs[ids].append(idlc)
idrc = span_2_id[(k, j, dir)]
kjcs[ids].append(idrc)
ijss.append(ids)
return span_2_id, id_2_span, ijss, ikcs, ikis, kjcs, kjis, basic_span
class data_sentence:
def __init__(self, id, entry_list):
self.id = id
self.entries = entry_list
self.size = len(entry_list)
def set_data_list(self, words, pos):
word_list = list()
pos_list = list()
for entry in self.entries:
if words is not None:
if entry.norm in words.keys():
word_list.append(words[entry.norm])
else:
word_list.append(words['<UNKNOWN>'])
if entry.pos in pos.keys():
pos_list.append(pos[entry.pos])
# else:
# pos_list.append(pos['<UNKNOWN-POS>'])
return word_list, pos_list
# def set_data_list(self, pos):
# pos_list = list()
# for entry in self.entries:
# if entry.pos in pos.keys():
# pos_list.append(pos[entry.pos])
# elif entry.pos == 'PRP':
# pos_list.append(pos['PRON'])
# else:
# print('some pos tag i dont know: ' + entry.pos)
# pos_list.append(pos['<UNKNOWN-POS>'])
# return pos_list
def __str__(self):
return '\t'.join([e for e in self.entries])
def read_conll(fh):
root = ConllEntry(0, '*root*', '*root*', 'ROOT-CPOS', 'ROOT-POS', '_', -1, 'rroot', '_', '_')
tokens = [root]
for line in fh:
tok = line.decode('utf8').strip().split('\t')
if len(tok) == 1 and tok[0] == '':
continue
elif not tok or line.strip() == '':
if len(tokens) > 1: yield tokens
tokens = [root]
else:
if line[0] == '#' or '-' in tok[0] or '.' in tok[0]:
tokens.append(line.strip())
else:
# if tok[3][0] == 'V':
# tok[3] = "V"
tokens.append(ConllEntry(int(float(tok[0])), tok[1], tok[2], tok[4], tok[3], tok[5],
int(float(tok[6])) if tok[6] != '_' else -1, tok[7], tok[8], tok[9]))
if len(tokens) > 1:
yield tokens
def read_data(conll_path, isPredict):
sentences = []
if not isPredict:
wordsCount = Counter()
posCount = Counter()
s_counter = 0
with open(conll_path, 'r') as conllFP:
for sentence in read_conll(conllFP):
wordsCount.update([node.norm for node in sentence if isinstance(node, ConllEntry)])
posCount.update([node.pos for node in sentence if isinstance(node, ConllEntry)])
ds = data_sentence(s_counter, sentence)
sentences.append(ds)
s_counter += 1
wordsCount['<UNKNOWN>'] = 0
posCount['<UNKNOWN-POS>'] = 0
return {w: i for i, w in enumerate(wordsCount.keys())}, {p: i for i, p in enumerate(
posCount.keys())}, sentences
else:
with open(conll_path, 'r') as conllFP:
s_counter = 0
for sentence in read_conll(conllFP):
ds = data_sentence(s_counter, sentence)
sentences.append(ds)
s_counter += 1
return sentences
def construct_batch_data(data_list, batch_size):
data_list.sort(key=lambda x: len(x[0]))
grouped = [list(g) for k, g in groupby(data_list, lambda s: len(s[0]))]
batch_data = []
for group in grouped:
sub_batch_data = get_batch_data(group, batch_size)
batch_data.extend(sub_batch_data)
return batch_data
def get_batch_data(grouped_data, batch_size):
batch_data = []
len_datas = len(grouped_data)
num_batch = len_datas // batch_size
if not len_datas % batch_size == 0:
num_batch += 1
for i in range(num_batch):
start_idx = i * batch_size
end_idx = min(len_datas, (i + 1) * batch_size)
batch_data.append(grouped_data[start_idx:end_idx])
return batch_data
def get_index(b, id):
id_a = id // b
id_b = id % b
return (id_a, id_b)
def init_weight(layer):
if isinstance(layer, nn.Linear):
xavier_uniform_(layer.weight.data)
constant_(layer.bias, 0)
if isinstance(layer, nn.Embedding):
xavier_uniform_(layer.weight.data)
if isinstance(layer, nn.LSTM):
for p in layer.parameters():
if len(p.data.shape) > 1:
xavier_uniform_(p.data)
else:
constant_(p, 0)
def eval(predicted, gold, test_path, log_path, epoch):
correct_counter = 0
total_counter = 0
for s in range(len(gold)):
ps = predicted[s][0]
gs = gold[s]
for i, e in enumerate(gs.entries):
if i == 0:
continue
if ps[i] == e.parent_id:
correct_counter += 1
total_counter += 1
accuracy = float(correct_counter) / total_counter
print ('UAS is ' + str(accuracy * 100) + '%')
f_w = open(test_path, 'w')
for s, sentence in enumerate(gold):
for entry in sentence.entries:
f_w.write(str(entry.norm) + ' ')
f_w.write('\n')
for entry in sentence.entries:
f_w.write(str(entry.pos) + ' ')
f_w.write('\n')
for i in range(len(sentence.entries)):
f_w.write(str(sentence.entries[i].parent_id) + ' ')
f_w.write('\n')
for i in range(len(sentence.entries)):
f_w.write(str(int(predicted[s][1][i])) + ' ')
f_w.write('\n')
for i in range(len(sentence.entries)):
f_w.write(str(int(predicted[s][0][i])) + ' ')
f_w.write('\n')
f_w.write('\n')
f_w.close()
if epoch == 0:
log = open(log_path, 'w')
# log.write("UAS for epoch " + str(epoch))
# log.write('\n')
# log.write('\n')
log.write(str(accuracy))
log.write('\n')
log.write('\n')
else:
log = open(log_path, 'a')
# log.write("UAS for epoch " + str(epoch))
# log.write('\n')
# log.write('\n')
log.write(str(accuracy))
log.write('\n')
log.write('\n')
def write_distribution(dmv_model):
path = "output/dis_log"
lex_path = "output/lex_log"
for t in range(dmv_model.tag_num):
log_path = path + str(t)
head_idx = dmv_model.pos["VB"]
writer = open(log_path, 'w')
dist = dmv_model.trans_param[head_idx, :, t, :, :, :]
for c in range(len(dmv_model.pos)):
for ct in range(dmv_model.tag_num):
for dir in range(2):
for cv in range(dmv_model.cvalency):
writer.write(str(dist[c, ct, dir, cv]))
writer.write('\n')
if dmv_model.tag_num > 1:
lex_log_path = lex_path + str(t)
lex_writer = open(lex_log_path, 'w')
lex_dist = dmv_model.lex_param[head_idx, t, :]
min = np.min(np.array(lex_dist))
for w in range(len(dmv_model.vocab)):
if lex_dist[w] > min:
lex_writer.write(str(lex_dist[w]))
lex_writer.write('\n')
def construct_input_data(rule_samples, decision_samples, batch_size, em_type):
batch_input_data = {}
batch_target_data = {}
batch_decision_data = {}
batch_target_decision_data = {}
batch_rule_samples = construct_update_batch_data(rule_samples, batch_size)
batch_decision_samples = construct_update_batch_data(decision_samples, batch_size)
batch_input_pos_list = list()
batch_input_dir_list = list()
batch_cvalency_list = list()
batch_dvalency_list = list()
batch_target_pos_list = list()
batch_decision_pos_list = list()
batch_decision_dir_list = list()
batch_target_decision_list = list()
if em_type == 'em':
batch_target_count_list = list()
batch_target_decision_count_list = list()
for i in range(len(batch_rule_samples)):
one_batch = np.array(batch_rule_samples[i])
one_batch_input_pos = one_batch[:, 0]
one_batch_input_dir = one_batch[:, 4]
one_batch_cvalency = one_batch[:, 5]
one_batch_target_pos = one_batch[:, 1]
batch_input_pos_list.append(one_batch_input_pos)
batch_input_dir_list.append(one_batch_input_dir)
batch_cvalency_list.append(one_batch_cvalency)
batch_target_pos_list.append(one_batch_target_pos)
if em_type == 'em':
one_batch_target_count = one_batch[:, 6]
batch_target_count_list.append(one_batch_target_count)
batch_input_data['input_pos'] = batch_input_pos_list
batch_input_data['input_dir'] = batch_input_dir_list
batch_input_data['cvalency'] = batch_cvalency_list
batch_target_data['target_pos'] = batch_target_pos_list
if em_type == 'em':
batch_target_data['target_count'] = batch_target_count_list
for i in range(len(batch_decision_samples)):
one_batch = np.array(batch_decision_samples[i])
one_batch_decision_pos = one_batch[:, 0]
one_batch_decision_dir = one_batch[:, 2]
one_batch_dvalency = one_batch[:, 3]
one_batch_target_decision = one_batch[:, 4]
batch_decision_pos_list.append(one_batch_decision_pos)
batch_decision_dir_list.append(one_batch_decision_dir)
batch_dvalency_list.append(one_batch_dvalency)
batch_target_decision_list.append(one_batch_target_decision)
if em_type == 'em':
one_batch_target_decision_count = one_batch[:, 5]
batch_target_decision_count_list.append(one_batch_target_decision_count)
batch_decision_data['decision_pos'] = batch_decision_pos_list
batch_decision_data['dvalency'] = batch_dvalency_list
batch_decision_data['decision_dir'] = batch_decision_dir_list
batch_target_decision_data['decision_target'] = batch_target_decision_list
if em_type == 'em':
batch_target_decision_data['decision_target_count'] = batch_target_decision_count_list
return batch_input_data, batch_target_data, batch_decision_data, batch_target_decision_data
def read_language_list(language_path):
ll = open(language_path, 'r')
language_set = set()
for l in ll:
l = l[:-1]
language_set.add(l)
return language_set
def get_file_set(file_list, language_set, is_train):
file_set = set()
for file in file_list:
if not file[0].isalpha():
continue
language_key, counter = get_language_key(file)
function_key = file[counter + 6]
if is_train and function_key == "a" and language_key in language_set:
file_set.add(file)
elif not is_train and function_key == "s" and language_key in language_set:
file_set.add(file)
return file_set
def get_language_key(file):
key = ""
counter = 0
for c in file:
if c == "/":
key = ""
if c == "-":
break
key += c
counter += 1
return key, counter
def read_multiple_data(data_path, file_set, isPredict):
sentences = []
language_map = {}
s_counter = 0
if not isPredict:
posCount = Counter()
lanCounter = Counter()
for file in file_set:
one_data_path = data_path + "/" + file
language_key, _ = get_language_key(file)
with open(one_data_path, 'rb') as conllFP:
for sentence in read_conll(conllFP):
# wordsCount.update([node.norm for node in sentence if isinstance(node, ConllEntry)])
posCount.update([node.pos for node in sentence if isinstance(node, ConllEntry)])
ds = data_sentence(s_counter, sentence)
sentences.append(ds)
language_map[s_counter] = language_key
lanCounter.update([language_key])
s_counter += 1
# wordsCount['<UNKNOWN>'] = 0
# posCount['<UNKNOWN-POS>'] = 0
# return {w: i for i, w in enumerate(wordsCount.keys())}, {p: i for i, p in enumerate(
return {p: i for i, p in enumerate(posCount.keys())}, sentences, {l: i for i, l in
enumerate(lanCounter.keys())}, language_map
else:
for file in file_set:
one_data_path = data_path + "/" + file
language_key, _ = get_language_key(file)
with open(one_data_path, 'r') as conllFP:
for sentence in read_conll(conllFP):
ds = data_sentence(s_counter, sentence)
sentences.append(ds)
language_map[s_counter] = language_key
s_counter += 1
return sentences, language_map
def construct_ml_batch_data(data_list, sentence_map, batch_size, sen_dim):
data_list.sort(key=lambda x: len(sentence_map[x[sen_dim]]))
grouped = [list(g) for k, g in groupby(data_list, lambda s: len(sentence_map[s[sen_dim]]))]
batch_data = []
for group in grouped:
sub_batch_data = get_batch_data(group, batch_size)
batch_data.extend(sub_batch_data)
random.shuffle(batch_data)
return batch_data
def construct_ml_input_data(rule_samples, decision_samples, sentence_map, sample_batch_size, em_type):
batch_input_data = {}
batch_target_data = {}
batch_decision_data = {}
batch_target_decision_data = {}
batch_rule_samples = construct_ml_batch_data(rule_samples, sentence_map, sample_batch_size, 4)
batch_decision_samples = construct_ml_batch_data(decision_samples, sentence_map, sample_batch_size, 4)
batch_input_pos_list = list()
batch_input_dir_list = list()
batch_input_sen_list = list()
batch_cvalency_list = list()
batch_dvalency_list = list()
batch_lan_list = list()
batch_target_pos_list = list()
batch_decision_pos_list = list()
batch_decision_dir_list = list()
batch_decision_sen_list = list()
batch_decision_lan_list = list()
batch_target_decision_list = list()
if em_type == 'em':
batch_target_count_list = list()
batch_target_decision_count_list = list()
for i in range(len(batch_rule_samples)):
one_batch = np.array(batch_rule_samples[i])
one_batch_input_pos = one_batch[:, 0]
one_batch_input_dir = one_batch[:, 2]
one_batch_cvalency = one_batch[:, 3]
one_batch_sentence = one_batch[:, 4]
one_batch_target_pos = one_batch[:, 1]
one_batch_target_lan = one_batch[:, 5]
batch_input_pos_list.append(one_batch_input_pos)
batch_input_dir_list.append(one_batch_input_dir)
batch_cvalency_list.append(one_batch_cvalency)
batch_input_sen_list.append(one_batch_sentence)
batch_target_pos_list.append(one_batch_target_pos)
batch_lan_list.append(one_batch_target_lan)
if em_type == 'em':
one_batch_target_count = one_batch[:, 6]
batch_target_count_list.append(one_batch_target_count)
batch_input_data['input_pos'] = batch_input_pos_list
batch_input_data['input_dir'] = batch_input_dir_list
batch_input_data['cvalency'] = batch_cvalency_list
batch_input_data['sentence'] = batch_input_sen_list
batch_target_data['target_pos'] = batch_target_pos_list
batch_target_data['target_lan'] = batch_lan_list
if em_type == 'em':
batch_target_data['target_count'] = batch_target_count_list
for i in range(len(batch_decision_samples)):
one_batch = np.array(batch_decision_samples[i])
one_batch_decision_pos = one_batch[:, 0]
one_batch_decision_dir = one_batch[:, 1]
one_batch_dvalency = one_batch[:, 2]
one_batch_decision_sentence = one_batch[:, 3]
one_batch_decision_lan = one_batch[:, 4]
one_batch_target_decision = one_batch[:, 5]
batch_decision_pos_list.append(one_batch_decision_pos)
batch_decision_dir_list.append(one_batch_decision_dir)
batch_dvalency_list.append(one_batch_dvalency)
batch_decision_sen_list.append(one_batch_decision_sentence)
batch_decision_lan_list.append(one_batch_decision_lan)
batch_target_decision_list.append(one_batch_target_decision)
if em_type == 'em':
one_batch_target_decision_count = one_batch[:, 6]
batch_target_decision_count_list.append(one_batch_target_decision_count)
batch_decision_data['decision_pos'] = batch_decision_pos_list
batch_decision_data['dvalency'] = batch_dvalency_list
batch_decision_data['decision_dir'] = batch_decision_dir_list
batch_decision_data['decision_sentence'] = batch_decision_sen_list
batch_decision_data['decision_language'] = batch_decision_lan_list
batch_target_decision_data['decision_target'] = batch_target_decision_list
if em_type == 'em':
batch_target_decision_data['decision_target_count'] = batch_target_decision_count_list
return batch_input_data, batch_target_data, batch_decision_data, batch_target_decision_data
def construct_ml_predict_data(rule_samples):
batch_predict_data = {}
batch_input_pos_list = rule_samples[:, 0]
batch_input_dir_list = rule_samples[:, 1]
batch_cvalency_list = rule_samples[:, 2]
batch_lan_list = rule_samples[:, 3]
batch_input_sen_list = rule_samples[:, 4]
batch_predict_data['pos'] = batch_input_pos_list
batch_predict_data['dir'] = batch_input_dir_list
batch_predict_data['cvalency'] = batch_cvalency_list
batch_predict_data['languages'] = batch_lan_list
batch_predict_data['sentence'] = batch_input_sen_list
return batch_predict_data
def construct_ml_pos_data(sentences, pos, languages, language_map):
data_list = list()
sen_idx = 0
sentence_map = {}
data_pos = []
for s in sentences:
_, s_pos = s.set_data_list(None, pos)
s_data_list = list()
s_data_list.append(s_pos)
data_pos.append(s_pos)
s_data_list.append(languages[language_map[sen_idx]])
s_data_list.append([sen_idx])
data_list.append(s_data_list)
sentence_map[sen_idx] = s_pos
sen_idx += 1
data_pos = np.array(data_pos)
return data_list, data_pos, sentence_map
def eval_ml(predicted, gold, test_path, log_path, language_map, languages, epoch):
correct_counter = np.zeros(len(languages))
total_counter = np.zeros(len(languages))
for s in range(len(gold)):
ps = predicted[s][0]
gs = gold[s]
lan_id = languages[language_map[s]]
for i, e in enumerate(gs.entries):
if i == 0:
continue
if ps[i] == e.parent_id:
correct_counter[lan_id] += 1
total_counter[lan_id] += 1
accuracy = correct_counter / total_counter
for l in languages.keys():
print ('UAS is ' + str(accuracy[languages[l]] * 100) + '% for ' + l)
print ('UAS is '+ str(np.mean(accuracy)*100)+ '% for average')
# f_w = open(test_path, 'w')
# for s, sentence in enumerate(gold):
# for entry in sentence.entries:
# f_w.write(str(entry.norm) + ' ')
# f_w.write('\n')
# for entry in sentence.entries:
# f_w.write(str(entry.pos) + ' ')
# f_w.write('\n')
# for i in range(len(sentence.entries)):
# f_w.write(str(sentence.entries[i].parent_id) + ' ')
# f_w.write('\n')
# for i in range(len(sentence.entries)):
# f_w.write(str(int(predicted[s][1][i])) + ' ')
# f_w.write('\n')
# for i in range(len(sentence.entries)):
# f_w.write(str(int(predicted[s][0][i])) + ' ')
# f_w.write('\n')
# f_w.write('\n')
# f_w.close()
if epoch == 0:
log = open(log_path, 'w')
log.write("UAS for epoch " + str(epoch))
log.write('\n')
for l in languages.keys():
log.write('\n')
log.write(str(accuracy[languages[l]])+" "+l)
log.write('\n')
log.write('\n')
log.write(str(np.mean(accuracy))+" average")
log.write('\n')
else:
log = open(log_path, 'a')
log.write("UAS for epoch " + str(epoch))
log.write('\n')
for l in languages.keys():
log.write('\n')
log.write(str(accuracy[languages[l]])+" "+l)
log.write('\n')
log.write(str(np.mean(accuracy)) + " average")
log.write('\n')