-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror_analysis.py
155 lines (126 loc) · 6.48 KB
/
error_analysis.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
import json
import os
from collections import defaultdict
def get_correct_predictions(path):
"""
Print name of .json test file, example of gold labels, all correctly resolved examples for each model.
:param str path: directory to the folder with .json test files with models' predictions
:return: None
"""
for filename in os.listdir(path):
if filename.endswith('.json'):
y_test, allenbert_correct, allenbilstm_correct = [], [], []
print(filename)
with open(f'{path}/{filename}') as infile:
for line in infile.readlines():
line = line.strip('\n')
example_info = json.loads(line)
example = example_info['example']
example = ' '.join(example[:-1]) + example[-1]
gold = example_info['BIO']
y_test.append(gold)
allenbert_pred = example_info['allenBERT_pred']
allenbilstm_pred = example_info['allenBiLSTM_pred']
if allenbert_pred == gold:
allenbert_correct.append(example)
if allenbilstm_pred == gold:
allenbilstm_correct.append(example)
print(f'gold: {y_test[0]}')
print(f'''allenBERT correct:
{allenbert_correct}''')
print('-----------------------------')
print(f'''allenBiLSTM correct:
{allenbilstm_correct}''')
print('=============================')
def get_mistakes_in_Benefactive(path):
"""
Print all incorrectly resolved examples and predictions for each model for Benefactive.
:param str path: directory to the folder with .json test files with models' predictions
:return: None
"""
for filename in os.listdir(path):
if filename == 'pred_Benefactive.json':
allenbert_examples, allenbert_incorrect_pred, allenbilstm_examples, allenbilstm_incorrect_pred = [], [], [], []
allenbert_verbs, allenbilstm_verbs = [], []
with open(f'{path}/{filename}') as infile:
for line in infile.readlines():
line = line.strip('\n')
example_info = json.loads(line)
example = example_info['example']
example = ' '.join(example[:-1]) + example[-1]
gold = example_info['BIO']
allenbert_pred = example_info['allenBERT_pred']
allenbilstm_pred = example_info['allenBiLSTM_pred']
if allenbert_pred != gold:
allenbert_examples.append(example)
allenbert_incorrect_pred.append(allenbert_pred)
allenbert_verbs.append(example[1])
if allenbilstm_pred != gold:
allenbilstm_examples.append(example)
allenbilstm_incorrect_pred.append(allenbilstm_pred)
allenbilstm_verbs.append(example[1])
print(f'allenBERT mistakes in {filename}:')
count_dict_allenbert = defaultdict(int)
for item in allenbert_verbs:
count_dict_allenbert[item] += 1
print(f'Predicates difficult for allenBERT: {count_dict_allenbert}')
for example, pred in zip(allenbert_examples, allenbert_incorrect_pred):
print(example)
print(pred)
print('-----------------------------')
print('=============================')
print(f'allenBiLSTM mistakes in {filename}:')
count_dict_allenbilstm = defaultdict(int)
for item in allenbilstm_verbs:
count_dict_allenbilstm[item] += 1
print(f'Predicates difficult for allenBiLSTM: {count_dict_allenbilstm}')
for example, pred in zip(allenbilstm_examples, allenbilstm_incorrect_pred):
print(example)
print(pred)
print('-----------------------------')
def get_mistakes_in_Passive_voice(path):
"""
Print all incorrectly resolved examples and predictions for each model for Passive voice.
:param str path: directory to the folder with .json test files with models' predictions
:return: None
"""
for filename in os.listdir(path):
if filename == 'pred_Passive voice.json':
allenbert_examples, allenbert_preds, allenbilstm_examples, allenbilstm_preds = [], [], [], []
with open(f'{path}/{filename}') as infile:
for line in infile.readlines():
line = line.strip('\n')
example_info = json.loads(line)
example = example_info['example']
example = ' '.join(example[:-1]) + example[-1]
gold = example_info['BIO']
allenbert_pred = example_info['allenBERT_pred']
allenbilstm_pred = example_info['allenBiLSTM_pred']
if allenbert_pred != ["O", "O", "O", "O", "O", "O", "O", "O"]:
allenbert_examples.append(example)
allenbert_preds.append(allenbert_pred)
if allenbilstm_pred != ["O", "O", "O", "O", "O", "O", "O", "O"]:
allenbilstm_examples.append(example)
allenbilstm_preds.append(allenbilstm_pred)
print(f'allenBERT nonzero predictions for {filename}:')
for example, pred in zip(allenbert_examples, allenbert_preds):
print(example)
print(pred)
print('-----------------------------')
print('=============================')
print(f'allenBiLSTM nonzero predictions for {filename}:')
for example, pred in zip(allenbilstm_examples, allenbilstm_preds):
print(example)
print(pred)
print('-----------------------------')
if __name__ == '__main__':
path = r'Output'
get_correct_predictions(path)
print('=============================')
print('=============================')
print('=============================')
get_mistakes_in_Benefactive(path)
print('=============================')
print('=============================')
print('=============================')
get_mistakes_in_Passive_voice(path)