forked from argosopentech/argos-translate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_sacrebleu_score.py
56 lines (42 loc) · 1.58 KB
/
model_sacrebleu_score.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
import argparse
import os
from pathlib import Path
import sacrebleu
from argostranslate.translate import translate
def main():
parser = argparse.ArgumentParser(description="sacreBLEU score for a model")
parser.add_argument(
"--test-set",
"--t",
default="wmt17",
metavar="TEST_SET",
help="The test set",
)
parser.add_argument(
"--lang-pair",
"--l",
metavar="LANG_PAIR",
help="The lang pair of the model to test",
)
args = parser.parse_args()
test_set = args.test_set
lang_pair = args.lang_pair
splitted_lang_pair = lang_pair.split('-')
from_lang = splitted_lang_pair[0]
to_lang = splitted_lang_pair[1]
sacrebleu.download_test_set(test_set, lang_pair)
source_file_path = Path(sacrebleu.get_source_file(test_set, lang_pair))
output_file_path = test_set + '.output.' + lang_pair
output_file = open(output_file_path, "a+", encoding="utf-8")
with open(source_file_path, "r") as source:
lines = source.readlines()
source_lines = [line.strip() for line in lines]
source_lines_length = len(source_lines)
for i, line in enumerate(source_lines):
translated = translate(line, from_lang, to_lang)
print('Translated ' + str(i) + ' lines of ' + str(source_lines_length) + ' lines')
output_file.write(translated.strip() + "\n")
output_file.flush()
os.system(
"sacrebleu -i " + output_file_path + " -t " + test_set + " -l " + lang_pair + ' >> ' + lang_pair + '.sacrebleu-score.json')
main()