-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathreport_rouge.py
61 lines (50 loc) · 2.07 KB
/
report_rouge.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
"""
* Copyright (c) 2021, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
"""
# Report mean rouge scores given a file of reference summaries and one or more files of predicted summaries
import argparse
from collections import defaultdict
from statistics import mean
import stanza
from summ_eval import rouge_metric
stanza.download('en')
nlp = stanza.Pipeline(lang='en', processors='tokenize')
def preprocess(text):
doc = nlp(text)
return '\n'.join(
' '.join(token.text for token in sentence.tokens)
for sentence in doc.sentences
)
def report_mean_rouge(ref_path, pred_paths):
metric = rouge_metric.RougeMetric()
with open(ref_path) as f:
refs = [preprocess(line) for line in f]
print('First ref')
print(refs[0])
all_scores = defaultdict(list)
for i, pred_path in enumerate(pred_paths):
with open(pred_path) as f:
preds = [preprocess(line) for line in f]
if i == 0:
print('First pred')
print(preds[0])
results = metric.evaluate_batch(preds, refs, aggregate=True)
# print(results)
all_scores['rouge1'].append(results['rouge']['rouge_1_f_score'] * 100)
all_scores['rouge2'].append(results['rouge']['rouge_2_f_score'] * 100)
all_scores['rougeL'].append(results['rouge']['rouge_l_f_score'] * 100)
for metric_name, scores in sorted(all_scores.items()):
print()
print('*' * 10)
print(metric_name)
print('Individual scores:', ', '.join(f'{score:.2f}' for score in scores))
print(f'Mean: {mean(scores):.2f}')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--ref-path', help='path to file with reference summaries')
parser.add_argument('--pred-paths', nargs='+', help='paths to prediction files')
args = parser.parse_args()
report_mean_rouge(args.ref_path, args.pred_paths)