Sub topic 7
Computing BLEU Reproducibly with SacreBLEU
Prerequisites and the reproducibility problem
This chapter assumes you understand tokens, n-grams, modified precision, the brevity penalty, and corpus-level BLEU. You should also know basic Python and how lists represent sentences.
A BLEU number depends on more than the model output. Tokenization, lowercasing, smoothing, reference files, and aggregation rules can all change the result. Two teams can evaluate the same translations and report different scores if they use different preprocessing.
SacreBLEU is a Python package and command-line tool that provides fixed evaluation settings. It accepts raw, detokenized text and applies a documented tokenizer internally. It also reports a metric signature, which records important configuration choices.
Install and run SacreBLEU
Install the package in the environment used for evaluation:
python -m pip install sacrebleuThe Python API expects:
hypotheses: one generated string per example.references: a list of reference streams. Each stream contains one reference string per example.
Thus, one reference set has the shape [[reference_1, reference_2, ...]]. Two reference sets have the shape [[ref_a1, ref_a2, ...], [ref_b1, ref_b2, ...]].
import sacrebleu
hypotheses = [
"The cat sat on the mat.",
"A dog is in the garden.",
]
references = [[
"The cat is on the mat.",
"There is a dog in the garden.",
]]
bleu = sacrebleu.corpus_bleu(
hypotheses,
references,
lowercase=False,
tokenize="13a",
smooth_method="exp",
)
print(f"BLEU: {bleu.score:.2f}")
print(bleu.format())
print(f"Signature: {bleu.signature}")SacreBLEU reports BLEU on a 0–100 scale. A score of 35.42 means the formatted BLEU score is 35.42, not 0.3542. The internal calculation still uses proportions between zero and one.
The call above makes several choices explicit. lowercase=False preserves case. tokenize="13a" selects SacreBLEU's standard tokenizer for many Western-language translation tasks. smooth_method="exp" specifies how short or sparse n-gram counts are handled. Record these choices instead of relying on defaults.
Report the evaluation clearly
| Item to report | Example |
|---|---|
| Metric and version | SacreBLEU BLEU, package version |
| Data | Test split and language direction |
| Text form | Raw detokenized hypotheses and references |
| Settings | Tokenizer, case handling, smoothing |
| Aggregation | Corpus BLEU, not an average of sentence scores |
A reproducible report should preserve the exact hypothesis file, reference files, SacreBLEU version, command or Python settings, and metric signature. If another researcher has those inputs, they should be able to regenerate the same number.
Exercise
Why should a BLEU result include the SacreBLEU signature rather than only the numeric score?
Show answer
The signature records evaluation choices such as tokenization, case handling, smoothing, and effective n-gram order, so another person can reproduce and interpret the score.