-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathResultsEval.py
76 lines (63 loc) · 2.16 KB
/
ResultsEval.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
import os
import json
import fire
from glob import glob
from tqdm import tqdm
from summertime.evaluation import Rouge, RougeWe, BertScore, Bleu, Meteor
from utils.MoverScore import MoverScore
METRICS = [Rouge(), RougeWe(), BertScore(), Bleu(), Meteor()]
def load_split_data(fpath):
"""
Load split data: train, test, dev --> List()
Data structure:
{id:(str) , summary:(str), source:(str),}
"""
tmp = []
with open(fpath, 'r') as r:
for line in r:
tmp.append(json.loads(line))
return tmp
def run_evaluation(model_summaries, tgt, BlockList=[]):
"""
Run all metrics for summarization results
"""
result = dict()
print("evaluating...")
for metric in METRICS:
print(f"evaluating {metric.metric_name}...")
if metric.metric_name in BlockList:
continue
results = metric.evaluate(model_summaries, tgt)
print(f"{metric.metric_name} results: {results}")
result[metric.metric_name] = results
if "moverscore" not in BlockList:
result['MoverScore'] = MoverScore(model_summaries, tgt)
print(f"MoverScore results: {result['MoverScore']}")
return result
# run evaluation by summertime
def get_tgt_pred(temp_data):
ids = []
tgt_list = []
pred_list = []
print("loading data...")
for ins in tqdm(temp_data):
if "id" in ins.keys():
ids.append(ins['id'])
else:
ids = []
if "target" in ins.keys():
tgt_list.append(ins['target'])
pred_list.append(ins['prediction'])
elif "summary" in ins.keys():
tgt_list.append(ins['summary'])
pred_list.append(ins['prediction'])
elif "ModelPrediction" in ins.keys():
tgt_list.append(ins["GroundTruth"])
pred_list.append(ins["ModelPrediction"])
return {"ids":ids, "tgt":tgt_list, "pred":pred_list}
def run_eval(fpath):
data_name = os.path.basename(fpath).split(".")[0]
eval_data = get_tgt_pred(load_split_data(fpath))
return {data_name: run_evaluation(eval_data['pred'], eval_data['tgt'])}
if __name__ == "__main__":
fire.Fire(run_eval)