-
Notifications
You must be signed in to change notification settings - Fork 1
/
bleu.py
27 lines (21 loc) · 920 Bytes
/
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
import argparse
from sacremoses import MosesDetokenizer
import sacrebleu
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--translation_file', type=str, help='File with translations (must be desegmented)')
parser.add_argument('--reference_file', type=str, help='Reference file for target language')
parser.add_argument('--tgt_lang', type=str, default='de', help='Target language code')
args = parser.parse_args()
# Detokenize the files
detok = MosesDetokenizer(lang=args.tgt_lang)
with open(args.translation_file, 'r') as f:
translations = [detok.detokenize(l.strip().split()) for l in f]
with open(args.reference_file, 'r') as f:
refs = [detok.detokenize(l.strip().split()) for l in f]
refs = [refs]
# Compute BLEU score
bleu = sacrebleu.corpus_bleu(translations, refs)
print(bleu.score)
if __name__ == '__main__':
main()