forked from clips/clicr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.py
176 lines (140 loc) · 6.06 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
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
""" Evaluation script for CliCR. Exact match and F1 are based on the evaluation script of the SQuAD dataset, v1.1. """
import argparse
from collections import Counter
import string
import re
import sys
import pexpect
from describe_data import *
from build_json_dataset import to_id_answertxt
def normalize_answer(s, lemmatizer_comm=None):
"""Lower text and remove punctuation, articles and extra whitespace."""
def remove_articles(text):
return re.sub(r'\b(a|an|the)\b', ' ', text)
def white_space_fix(text):
return ' '.join(text.split())
def remove_punc(text):
exclude = set(string.punctuation)
return ''.join(ch for ch in text if ch not in exclude)
def lower(text):
return text.lower()
def lemma(text):
if lemmatizer_comm is None:
lemmatized = text
else:
lemmatized = lemmatize(text, lemmatizer_comm)
return lemmatized
return white_space_fix(remove_articles(remove_punc(lower(lemma(s)))))
def lemmatize(s, c):
"""
Use luiNorm to lemmatize.
:param c: spawned command (luiNorm)
"""
def extract(s):
"""
:param s: output string, e.g. '\r\ndrs|dr\r\n'
"""
return s.split("|")[-1][:-len(eol)]
if s.strip() == "" or s.strip() == "-":
return s
eol = "\r\n"
c.sendline(s)
c.expect(eol+".*"+eol)
lemma = extract(c.after)
return lemma
def lemmatizer(path="/mnt/b5320167-5dbd-4498-bf34-173ac5338c8d/Tools/lvg2017/bin/luiNorm"):
return pexpect.spawnu(path)
def f1_score(prediction, ground_truth, comm=None):
prediction_tokens = normalize_answer(prediction, comm).split()
ground_truth_tokens = normalize_answer(ground_truth, comm).split()
common = Counter(prediction_tokens) & Counter(ground_truth_tokens)
num_same = sum(common.values())
if num_same == 0:
return 0
precision = 1.0 * num_same / len(prediction_tokens)
recall = 1.0 * num_same / len(ground_truth_tokens)
f1 = (2 * precision * recall) / (precision + recall)
return f1
def exact_match_score(prediction, ground_truth, comm=None):
return normalize_answer(prediction, comm) == normalize_answer(ground_truth, comm)
def metric_max_over_ground_truths(metric_fn, prediction, ground_truths, comm=None):
scores_for_ground_truths = []
for ground_truth in ground_truths:
score = metric_fn(prediction, ground_truth, comm)
scores_for_ground_truths.append(score)
return max(scores_for_ground_truths)
def evaluate(dataset, predictions, lemmatizer_path=None, extended=False, embeddings_file=None, downcase=False):
data = dataset[DATA_KEY]
comm = lemmatizer(lemmatizer_path) if lemmatizer_path else None
f1 = exact_match = total = 0
n_unanswered = 0
datum_count = 0
for datum in data:
for qa in datum[DOC_KEY][QAS_KEY]:
total += 1
if qa[ID_KEY] not in predictions:
n_unanswered += 1
continue
ground_truths = list(map(lambda x: x[TXT_KEY], qa[ANS_KEY]))
prediction = predictions[qa[ID_KEY]]
exact_match += metric_max_over_ground_truths(
exact_match_score, prediction, ground_truths, comm=comm)
f1 += metric_max_over_ground_truths(
f1_score, prediction, ground_truths, comm=comm)
datum_count += 1
print("There were {} unanswered instances".format(n_unanswered))
exact_match = 100.0 * exact_match / total
f1 = 100.0 * f1 / total
assert exact_match <= f1
scores = {'exact_match': exact_match, 'f1': f1}
if extended:
from pycocoevalcap.eval import COCOEvalCap
from embedding_eval import EmbeddingEval
# COCO
ground = {}
for id, ans in to_id_answertxt(dataset).items():
normalized_ans = []
for a in ans:
normalized_ans.append(normalize_answer(a, comm))
ground[id] = normalized_ans
_predictions = {id: [normalize_answer(ans, comm)] for id, ans in predictions.items()}
cocoEval = COCOEvalCap(ground, _predictions)
cocoEval.evaluate()
# Embeddings evaluation
embEval = EmbeddingEval(ground, _predictions, embeddings_file, downcase)
embEval.evaluate()
#scores = {**scores, **cocoEval.eval, **embEval.eval} # only python3.5
scores.update(cocoEval.eval)
scores.update(embEval.eval)
return scores
def print_scores(scores):
"""
:param scores: {"method1": score, ...}
"""
print("{}\t{:.1f}".format("exact_match", scores["exact_match"]))
print("{}\t{:.1f}".format("f1", scores["f1"]))
for method, score in sorted(scores.items()):
if method == "exact_match" or method == "f1":
continue
else:
print("{}\t{:.3f}".format(method, score))
if __name__ == '__main__':
expected_version = '1.0'
parser = argparse.ArgumentParser(
description='Evaluation for CliCR ' + expected_version)
parser.add_argument('-test_file', help='Dataset file with ground truth.')
parser.add_argument('-prediction_file', help='Prediction file of the format {"id1234": "Answer", ...}')
parser.add_argument("-lemmatizer_path", help="Use luiNorm if path provided.")
parser.add_argument("-extended", help="Whether to use extended evaluation using Bleu, Rouge and embeddings.",
action="store_true")
parser.add_argument('-embeddings_file', help='Embeddings in w2v txt format. Only needed for extended evaluation.')
parser.add_argument('-downcase',
help="Only for extended evaluation. Should be set to true if the embedding vocabulary is lowercased.",
action="store_true")
args = parser.parse_args()
if args.extended:
assert args.embeddings_file is not None
dataset = load_json(args.test_file)
predictions = load_json(args.prediction_file)
print_scores(evaluate(dataset, predictions, lemmatizer_path=args.lemmatizer_path, extended=args.extended,
embeddings_file=args.embeddings_file, downcase=args.downcase))