-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.py
125 lines (102 loc) · 4.35 KB
/
evaluate.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
"""
First, you need to compile the Bouma baseline:
>>> gcc -o hyphen hyphenate_mnl.c
"""
import subprocess
import argparse
import os
import numpy as np
from sklearn.metrics import accuracy_score, f1_score
import Levenshtein
import stresser.utils as u
def simplify(old):
new = []
for old_word in old:
new_word = []
for char in old_word:
if char == 2:
new_word.append(1)
else:
new_word.append(0)
new.append(new_word)
return new
def main():
def eval(silver_file, gold_file, syllab_only=False):
silver = u.load_file(silver_file)
_, silver_y = u.x_and_y(silver)
gold = u.load_file(gold_file)
_, gold_y = u.x_and_y(gold)
if syllab_only:
silver_y = simplify(silver_y)
gold_y = simplify(gold_y)
acc_syll = accuracy_score([i for s in gold_y for i in s],
[i for s in silver_y for i in s])
f1_syll = f1_score([i for s in gold_y for i in s],
[i for s in silver_y for i in s],
average='macro')
acc_token = accuracy_score([str(s) for s in gold_y],
[str(s) for s in silver_y])
lev = np.mean([Levenshtein.distance(''.join([str(_) for _ in g]), \
''.join([str(_) for _ in s])) \
for g, s in zip(gold_y, silver_y)])
return acc_syll, f1_syll, acc_token, lev
print('Full eval: syllab + stress')
print('Plain CRF baseline:')
acc_syll, f1_syll, acc_token, lev = eval('model_b/silver_dev.json', 'data/splits/dev.json')
print('- dev scores:')
print(' - acc (char):', acc_syll)
print(' - f1 (char):', f1_syll)
print(' - acc (token):', acc_token)
print(' - Levenshtein (token):', lev)
acc_syll, f1_syll, acc_token, lev = eval('model_b/silver_test.json', 'data/splits/test.json')
print('- test scores:')
print(' - acc (char):', acc_syll)
print(' - f1 (char):', f1_syll)
print(' - acc (token):', acc_token)
print(' - Levenshtein (token):', lev)
print('Our system (LSTM + CRF):')
acc_syll, f1_syll, acc_token, lev = eval('model_s/silver_dev.json', 'data/splits/dev.json')
print('- dev scores:')
print(' - acc (char):', acc_syll)
print(' - f1 (char):', f1_syll)
print(' - acc (token):', acc_token)
print(' - Levenshtein (token):', lev)
acc_syll, f1_syll, acc_token, lev = eval('model_s/silver_test.json', 'data/splits/test.json')
print('- test scores:')
print(' - acc (char):', acc_syll)
print(' - f1 (char):', f1_syll)
print(' - acc (token):', acc_token)
print(' - Levenshtein (token):', lev)
###############################################################################################
print('Full eval: syllab only')
acc_syll, f1_syll, acc_token, lev = eval('model_b/silver_dev.json', 'data/splits/dev.json',
syllab_only=True)
print('- dev scores:')
print(' - acc (char):', acc_syll)
print(' - f1 (char):', f1_syll)
print(' - acc (token):', acc_token)
print(' - Levenshtein (token):', lev)
acc_syll, f1_syll, acc_token, lev = eval('model_b/silver_test.json', 'data/splits/test.json',
syllab_only=True)
print('- test scores:')
print(' - acc (char):', acc_syll)
print(' - f1 (char):', f1_syll)
print(' - acc (token):', acc_token)
print(' - Levenshtein (token):', lev)
print('Our system (LSTM + CRF):')
acc_syll, f1_syll, acc_token, lev = eval('model_s/silver_dev.json', 'data/splits/dev.json',
syllab_only=True)
print('- dev scores:')
print(' - acc (char):', acc_syll)
print(' - f1 (char):', f1_syll)
print(' - acc (token):', acc_token)
print(' - Levenshtein (token):', lev)
acc_syll, f1_syll, acc_token, lev = eval('model_s/silver_test.json', 'data/splits/test.json',
syllab_only=True)
print('- test scores:')
print(' - acc (char):', acc_syll)
print(' - f1 (char):', f1_syll)
print(' - acc (token):', acc_token)
print(' - Levenshtein (token):', lev)
if __name__ == '__main__':
main()