-
Notifications
You must be signed in to change notification settings - Fork 4
/
evaluate_marmot_relaxed.py
executable file
·251 lines (199 loc) · 8.49 KB
/
evaluate_marmot_relaxed.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
import random
from sklearn.metrics import f1_score, confusion_matrix, accuracy_score
from collections import Counter
import itertools
def load_word_dataset(f_name):
"Loads word level Universal Dependencies dataset"
with open(f_name) as f:
contents = f.readlines()
x_word = []
y_word = []
sent_x = []
sent_y = []
all_sent_x = []
all_sent_y = []
for i, line in enumerate(contents):
data = line.split()
if not data:
all_sent_x.append(sent_x)
all_sent_y.append(sent_y)
sent_x = []
sent_y = []
continue
word = data[1]
label = data[3]
x_word.append(word)
y_word.append(label)
sent_x.append(word)
sent_y.append(label)
return all_sent_x, all_sent_y, x_word, y_word
def load_char_dataset(f_name):
contents = []
with open(f_name) as f:
contents = f.readlines()
x_char = []
y_char = []
sent_x = []
sent_y = []
all_sent_x = []
all_sent_y = []
all_sent_segs = []
seg_flags = []
skip = False
for i, line in enumerate(contents[:-1]):
if (skip):
skip = False
continue
data = line.split('\t')
next_data = contents[i+1].split()
char, label, flag = data
flag = flag.strip('\n')
if (label != 'SPACE'):
seg_flags.append(int(flag))
x_char.append(char)
y_char.append(label)
sent_x.append(char)
sent_y.append(label)
if not next_data:
all_sent_x.append(sent_x)
all_sent_y.append(sent_y)
all_sent_segs.append(seg_flags)
sent_x = []
sent_y = []
seg_flags = []
skip = True
continue
return all_sent_x, all_sent_y, all_sent_segs, x_char, y_char
def load_marmot_dataset(f_name):
with open(f_name, 'r') as f:
all_x = []
sent_x = []
all_labels = []
sent_y = []
for line in f:
data = line.split('\t')
if data[0] == '\n':
all_x.append(sent_x)
sent_x = []
all_labels.append(sent_y)
sent_y = []
continue
x = data[1]
sent_x.append(x)
label = data[2].rstrip()
sent_y.append(label)
return all_x, all_labels
def load_predictions(f_pred):
with open(f_pred, 'r') as f:
all_x = []
sent_x = []
all_labels = []
sent_y = []
for line in f:
data = line.split('\t')
if data[0] == '\n':
all_x.append(sent_x)
sent_x = []
all_labels.append(sent_y)
sent_y = []
continue
x = data[1]
sent_x.append(x)
label = data[5]
sent_y.append(label)
return all_x, all_labels
def get_wrong_label(clean_tag, labels_str):
wrong_label = random.choice(labels_str)
while wrong_label == clean_tag:
wrong_label = random.choice(labels_str)
return wrong_label
def calculate_merged(reconstr_sent,cor_pred, clean_sent, clean_idx, merged_tok_num):
for i in range(merged_tok_num):
if cor_pred == clean_sent[clean_idx+i]:
reconstr_sent.append(cor_pred)
else:
wrong_label = get_wrong_label(clean_sent[clean_idx+i], labels_str)
reconstr_sent.append(wrong_label)
return reconstr_sent
if __name__ == '__main__':
f_clean_test = 'data/words/clean/en-ud-test1.2_marmot_clean_labels.txt'
_ , clean_original = load_marmot_dataset(f_clean_test)
f_clean_preds = 'data/words/clean/marmot_predictions_clean.txt'
all_sent_x_test, clean_preds = load_predictions(f_clean_preds)
clean_labels_flat = list(itertools.chain.from_iterable(clean_original))
clean_preds_flat = list(itertools.chain.from_iterable(clean_preds))
word_acc = accuracy_score( clean_labels_flat, clean_preds_flat)
print ("NON UDPIPE-TOKENIZED ACCURACY (Table 1): \n", word_acc)
print("\n")
labels_str = list(set(clean_labels_flat))
f_name_test = 'data/char/en1.2/en-ud-test1.2.conllu'
_, _, seg_flags_test, _, _ = load_char_dataset(f_name_test)
f_marmot_preds = ['data/words/udpipe_files/ud_clean_tokenized_marmot_output.txt',
'data/words/udpipe_files/ud_corr_low_tokenized_marmot_output.txt',
'data/words/udpipe_files/ud_corr_med_tokenized_marmot_output.txt',
'data/words/udpipe_files/ud_corr_high_tokenized_marmot_output.txt']
corruption_level = ['CLEAN', 'LOW', 'MED', 'HIGH']
print ("Marmot acc POS (Table 4)")
#Outer for is about each corruption level
for f_marmot_pred, corr_level in zip(f_marmot_preds,corruption_level):
corrupt_x , corrupt_preds = load_predictions(f_marmot_pred)
#curr_flags is a binary flag used to indicate the number
#of clean tokens on each corrupted token pass
reconstr_all = []
for i, sentence in enumerate(corrupt_x):
flag_idx = 0
clean_idx = 0
reconstr_sent = []
temp_token_label = []
clean_sent = clean_original[i]
corrupt_sent = corrupt_preds[i]
segmented_token = False
for j, token in enumerate(sentence):
curr_flags = seg_flags_test[i][flag_idx : flag_idx+ len(token)]
flag_idx += len(token)
#Case where we have a splitted token e.g Wo-nd-er-ful...
if sum(curr_flags) == 0:
temp_token_label.append( corrupt_sent[j])
segmented_token = True
continue
else:
#Case where an end of a splited token from previously is detected e.g Wo-nder-ful
if segmented_token == True:
temp_token_label.append( corrupt_sent[j])
if ( clean_sent[clean_idx] in temp_token_label):
reconstr_sent.append(clean_sent[clean_idx])
else:
wrong_label = get_wrong_label(clean_sent[clean_idx], labels_str)
reconstr_sent.append(wrong_label)
clean_idx += 1
temp_token_label = []
segmented_token = False
#Subcase where we have a second half split token - full token merge e.g Wo-nder-fulworld
if sum(curr_flags) == 2:
reconstr_sent.append(corrupt_sent[j])
clean_idx += 1
continue
#Subcase where there are more merges after a splitted token e.g Wo-nder-fulworldtoday
if sum(curr_flags) > 2:
reconstr_sent = calculate_merged(reconstr_sent, corrupt_sent[j],
clean_sent, clean_idx, sum(curr_flags)-1)
clean_idx += sum(curr_flags)-1
continue
else:
#Case where we have a correct tokenization
if sum(curr_flags) == 1:
reconstr_sent.append(corrupt_sent[j])
clean_idx += 1
#Case where we have a merging of two or more tokens e.g Don'tgothere
elif sum(curr_flags) > 1:
reconstr_sent = calculate_merged(reconstr_sent, corrupt_sent[j],
clean_sent, clean_idx, sum(curr_flags) )
clean_idx += sum(curr_flags)
reconstr_all.append(reconstr_sent)
clean_labels_flat = list(itertools.chain.from_iterable(clean_original))
corrupt_preds_flat = list(itertools.chain.from_iterable(reconstr_all))
print('=======================')
print('Corruption level', corr_level)
word_acc_corr = accuracy_score( clean_labels_flat, corrupt_preds_flat)
print (word_acc_corr)
print('=======================\n')