-
Notifications
You must be signed in to change notification settings - Fork 1
/
evaluate.py
77 lines (56 loc) · 1.8 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
import json
import argparse
from fuzzywuzzy import fuzz
from pathlib import Path
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--input_file', type=str, required=True)
parser.add_argument('--output_file', type=str, required=False)
return parser.parse_args()
def cal_edit_sim(references, hypotheses):
total = len(references)
edit_sim = 0.0
for pred, gt in zip(hypotheses, references):
pred = pred.strip()
gt = gt.strip()
edit_sim += fuzz.ratio(pred, gt)
return edit_sim / total
def evaluate(data):
outputs = []
throughput = 0.0
passed_count, edit_sim, total = 0, 0.0, len(data)
for d in data:
pred, ref = d['completion'].strip(), d['groundtruth'].strip()
passed = True if pred == ref else False
es = fuzz.ratio(pred, ref)
outputs.append({
'task_id': d['task_id'],
'file': d['file'],
'passed': passed,
'completion': d['completion'],
'groundtruth': d['groundtruth'],
'edit_similarity': es,
'throughput': d['throughput'],
})
throughput += d['throughput']
if passed:
passed_count += 1
edit_sim += es
results = {
"EM": 100 * passed_count / total,
"ES": edit_sim / total,
"Throughput": throughput / total,
"Details": outputs,
}
return results
if __name__ == '__main__':
args = parse_args()
with open(args.input_file) as f:
data = f.readlines()
data = [json.loads(line) for line in data]
input_file = Path(args.input_file)
print('-' * 60)
print(f'> Evaluating {input_file.name}...')
results = evaluate(data)
with open(args.output_file, 'w') as f:
json.dump(results, f, indent=4)