| #!/usr/bin/env python | |
| # sacrebleu format to jsonlines | |
| import io | |
| import json | |
| import re | |
| src_lang, tgt_lang = ["en", "ro"] | |
| for split in ["train", "val", "test"]: | |
| recs = [] | |
| fout = f"{split}.json" | |
| with io.open(fout, "w", encoding="utf-8") as f: | |
| for type in ["source", "target"]: | |
| fin = f"{split}.{type}" | |
| recs.append([line.strip() for line in open(fin)]) | |
| for src, tgt in zip(*recs): | |
| out = {"translation": { src_lang: src, tgt_lang: tgt } } | |
| x = json.dumps(out, indent=0, ensure_ascii=False) | |
| x = re.sub(r'\n', ' ', x, 0, re.M) | |
| f.write(x + "\n") | |