Skip to content

Instantly share code, notes, and snippets.

@ymoslem
Last active February 4, 2021 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ymoslem/f9e4df761f527996115387a2144912c0 to your computer and use it in GitHub Desktop.
Save ymoslem/f9e4df761f527996115387a2144912c0 to your computer and use it in GitHub Desktop.
Compute BLEU Score for Machine Translation
import sacrebleu
from sacremoses import MosesDetokenizer
md = MosesDetokenizer(lang='en')
# Open the test dataset human translation file and detokenize the references
refs = []
with open("target.test") as test:
for line in test:
line = line.strip().split()
line = md.detokenize(line)
refs.append(line)
print("Reference 1st sentence:", refs[0])
refs = [refs] # Yes, it is a list of list(s) as required by sacreBLEU
# Open the translation file by the NMT model and detokenize the predictions
preds = []
with open("target.pred") as pred:
for line in pred:
line = line.strip().split()
line = md.detokenize(line)
preds.append(line)
print("MTed 1st sentence:", preds[0])
# Calculate and print the BLEU score
bleu = sacrebleu.corpus_bleu(preds, refs)
print(bleu.score)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment