-
Notifications
You must be signed in to change notification settings - Fork 6
/
metrics.py
251 lines (205 loc) · 8.9 KB
/
metrics.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 jsonlines
import sys
from scorer import fever_score
from metrics.claim import Claim
claims = []
train_file = []
train_relevant_file = []
train_concatenate_file = []
train_predictions_file = []
if len(sys.argv) - 1 == 1:
type_file = sys.argv[1]
if type_file == 'train':
train_file = "data/subsample_train.jsonl"
train_relevant_file = "data/subsample_train_relevant_docs.jsonl"
train_concatenate_file = "data/subsample_train_concatenation.jsonl"
train_predictions_file = "predictions/predictions_train.jsonl"
else: # type_file == 'dev':
train_file = "data/dev.jsonl"
train_relevant_file = "data/dev_relevant_docs.jsonl"
train_concatenate_file = "data/dev_sentence_selection.jsonl"
train_predictions_file = "predictions/new_dev_bert_test.jsonl"
else:
print("Needs to have one argument. Choose:")
print("train")
print("dev")
print("test")
exit(0)
train_file = jsonlines.open(train_file)
train_relevant_file = jsonlines.open(train_relevant_file)
train_concatenate_file = jsonlines.open(train_concatenate_file)
train_predictions_file = jsonlines.open(train_predictions_file)
train_set = []
train_relevant = []
train_concatenate = []
train_prediction = []
for lines in train_file:
lines['claim'] = lines['claim'].replace("-LRB-", " ( ")
lines['claim'] = lines['claim'].replace("-RRB-", " ) ")
train_set.append(lines)
for lines in train_relevant_file:
lines['claim'] = lines['claim'].replace("-LRB-", " ( ")
lines['claim'] = lines['claim'].replace("-RRB-", " ) ")
train_relevant.append(lines)
for lines in train_concatenate_file:
lines['claim'] = lines['claim'].replace("-LRB-", " ( ")
lines['claim'] = lines['claim'].replace("-RRB-", " ) ")
train_concatenate.append(lines)
for lines in train_predictions_file:
train_prediction.append(lines)
for claim in train_set:
_claim = Claim(claim['id'], claim['claim'], claim['verifiable'])
_claim.add_gold_evidences(claim['evidence'])
_claim.add_gold_line(claim)
_claim.label = claim['label']
claims.append(_claim)
# print(_claim.get_gold_documents())
# print(len(_claim.gold_evidence))
for claim in train_relevant:
_id = claim['id']
_claim = Claim.find_by_id(_id)[0]
# no search is needed... no information on gold about retrieval
if not _claim.verifiable:
continue
_claim.add_predicted_docs(claim['predicted_pages'])
_claim.add_predicted_sentences(claim['predicted_sentences'])
#_claim.add_predicted_sentences_bert(claim['predicted_sentences'])
for claim in train_concatenate:
_id = claim['id']
_claim = Claim.find_by_id(_id)[0]
if not _claim.verifiable:
continue
if "predicted_pages_ner" in claim:
_claim.add_predicted_docs_ner(claim['predicted_pages_ner'])
if "predicted_sentences_ner" in claim:
print("")
_claim.add_predicted_sentences_ner(claim['predicted_sentences_ner'])
if "predicted_sentences_bert" in claim:
_claim.add_predicted_sentences_bert(claim['predicted_sentences_bert'])
else:
if "predicted_sentences_triple" in claim:
_claim.add_predicted_sentences_bert(claim['predicted_sentences_triple'])
else:
_claim.add_predicted_sentences_bert(claim['predicted_sentences'])
# _claim.add_predicted_docs_ner(claim['predicted_pages_ner'])
# _claim.add_predicted_sentences_ner(claim['predicted_sentences_ner'])
if "predicted_pages_oie" in claim:
_claim.add_predicted_docs_oie(claim['predicted_pages_oie'])
# if not _claim.check_evidence_found_doc(_type="all"):
# print(str(_claim.get_gold_documents()) + " -- " + str(_claim.get_predicted_documents(_type="all")))
results = Claim.document_retrieval_stats(claims, _type="tfidf")
print("\n########################")
print("# Documents Only TFIDF #")
print("########################")
print("Precision (Document Retrieved): \t" + str(results[0]))
print("Recall (Relevant Documents): \t\t" + str(results[1]))
print("At least one Doc Found: \t\t" + str(results[2]))
results = Claim.document_retrieval_stats(claims, _type="ner")
print("\n######################")
print("# Documents Only NER #")
print("########################")
print("Precision (Document Retrieved): \t" + str(results[0]))
print("Recall (Relevant Documents): \t\t" + str(results[1]))
print("At least one Doc Found: \t\t" + str(results[2]))
results = Claim.document_retrieval_stats(claims, _type="oie")
print("\n######################")
print("# Documents Only OIE #")
print("########################")
print("Precision (Document Retrieved): \t" + str(results[0]))
print("Recall (Relevant Documents): \t\t" + str(results[1]))
print("At least one Doc Found: \t\t" + str(results[2]))
results = Claim.document_retrieval_stats(claims, _type="all")
print("\n######################")
print("# Documents for All #")
print("######################")
print("Precision (Document Retrieved): \t" + str(results[0]))
print("Recall (Relevant Documents): \t\t" + str(results[1]))
print("At least one Doc Found: \t\t" + str(results[2]))
results = Claim.evidence_extraction_stats(claims, _type="tfidf")
print("\n#################################")
print("# Possible Sentences Only TFIDF #")
print("#################################")
print("Precision (Sentences Retrieved): \t" + str(results[0]))
print("Recall (Relevant Sentences): \t\t" + str(results[1]))
print("\nIF DOCUMENT WAS FOUND CORRECTLY:")
print("Precision (Sentences Retrieved): \t" + str(results[2]))
print("Recall (Relevant Sentences): \t\t" + str(results[3]))
results = Claim.evidence_extraction_stats(claims, _type="ner")
print("\n###############################")
print("# Possible Sentences Only NER #")
print("###############################")
print("Precision (Sentences Retrieved): \t" + str(results[0]))
print("Recall (Relevant Sentences): \t\t" + str(results[1]))
print("\nIF DOCUMENT WAS FOUND CORRECTLY:")
print("Precision (Sentences Retrieved): \t" + str(results[2]))
print("Recall (Relevant Sentences): \t\t" + str(results[3]))
results = Claim.evidence_extraction_stats(claims, _type="bert")
print("\n################################")
print("# Possible Sentences Only BERT #")
print("################################")
print("Precision (Sentences Retrieved): \t" + str(results[0]))
print("Recall (Relevant Sentences): \t\t" + str(results[1]))
print("\nIF DOCUMENT WAS FOUND CORRECTLY:")
print("Precision (Sentences Retrieved): \t" + str(results[2]))
print("Recall (Relevant Sentences): \t\t" + str(results[3]))
results = Claim.evidence_extraction_stats(claims, _type="all")
print("\n###############################")
print("# Possible Sentences For BOTH #")
print("###############################")
print("Precision (Sentences Retrieved): \t" + str(results[0]))
print("Recall (Relevant Sentences): \t\t" + str(results[1]))
print("\nIF DOCUMENT WAS FOUND CORRECTLY:")
print("Precision (Sentences Retrieved): \t" + str(results[2]))
print("Recall (Relevant Sentences): \t\t" + str(results[3]))
# scores from fever
new_train_set = []
for claim in train_prediction:
_id = claim['id']
_claim = Claim.find_by_id(_id)[0]
new_train_set.append(_claim.line)
print(len(new_train_set))
results = fever_score(train_prediction, actual=new_train_set)
print("\n#########")
print("# FEVER #")
print("#########")
print("Strict_score: \t\t\t" + str(results[0]))
print("Acc_score: \t\t\t" + str(results[1]))
print("Precision: \t\t\t" + str(results[2]))
print("Recall: \t\t\t" + str(results[3]))
print("F1-Score: \t\t\t" + str(results[4]))
predictions_if_doc_found = []
claims_if_doc_found = []
for claim in train_prediction:
_id = claim['id']
_claim = Claim.find_by_id(_id)[0]
if _claim.check_evidence_found_doc(_type="all"):
claims_if_doc_found.append(_claim.line)
predictions_if_doc_found.append(claim)
# scores from fever
results = fever_score(predictions_if_doc_found, actual=claims_if_doc_found)
print("\n#######################")
print("# FEVER If Doc Found! #")
print("#######################")
print("Strict_score: \t\t\t" + str(results[0]))
print("Acc_score: \t\t\t" + str(results[1]))
print("Precision: \t\t\t" + str(results[2]))
print("Recall: \t\t\t" + str(results[3]))
print("F1-Score: \t\t\t" + str(results[4]))
predictions_if_evidence_found = []
claims_if_evidence_found = []
for claim in train_prediction:
_id = claim['id']
_claim = Claim.find_by_id(_id)[0]
if _claim.check_evidence_was_found(_type="bert"):
claims_if_evidence_found.append(_claim.line)
predictions_if_evidence_found.append(claim)
# scores from fever
results = fever_score(predictions_if_evidence_found, actual=claims_if_evidence_found)
print("\n############################")
print("# FEVER If Sentence Found! #")
print("############################")
print("Strict_score: \t\t\t" + str(results[0]))
print("Acc_score: \t\t\t" + str(results[1]))
print("Precision: \t\t\t" + str(results[2]))
print("Recall: \t\t\t" + str(results[3]))
print("F1-Score: \t\t\t" + str(results[4]))