-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalc-bleu.py
38 lines (30 loc) · 1.06 KB
/
calc-bleu.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
import argparse
import datasets
import json
from sacrebleu.metrics import BLEU, CHRF, TER
import sys
def parse_command_line():
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', required=True)
parser.add_argument('--reference-field', required=True)
parser.add_argument('--translation-field', required=True)
return parser.parse_args()
def split_to_score(reference, system):
refs = [[ref] for ref in reference]
bleu = BLEU(tokenize='none', force=True)
return {
'bleu': str(bleu.corpus_score(system, refs)),
'chrf': str(CHRF().corpus_score(system, refs)),
'ter': str(TER().corpus_score(system, refs)),
}
def main():
args = parse_command_line()
ds = datasets.load_from_disk(args.dataset)
report = {
name: split_to_score(
reference=[s.lower() for s in split[args.reference_field]],
system=split[args.translation_field])
for name, split in ds.items()
}
json.dump(report, sys.stdout, indent=2)
main()