Translation Evaluation Beyond BLEU › Tooling
Language AI HandbookAll chapters The executable examples use SacreBLEU for the classical metrics and scikit-learn for the toy metric. Run the complete notebook in the Language AI Handbook's locked book environment, which also includes NumPy, pandas, SciPy, Matplotlib and the book's plotting helper. The examples need no model or corpus download. SacreBLEU records the scoring configuration in a signature.
PythonIn [3]Expand
import re
import numpy as np
import pandas as pd
from sacrebleu.metrics import BLEU, CHRF, TER
from scipy.stats import spearmanr
from sklearn.linear_model import Ridge
from sklearn.model_selection import GroupKFold
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
bleu_metric = BLEU(effective_order=True) # sentence-level BLEU
chrf_metric = CHRF(word_order=2) # chrF++
ter_metric = TER()With effective_order=True and the default exponential smoothing used here, SacreBLEU limits the sentence score to orders for which the hypothesis contains n-grams. A three-token hypothesis can then be scored with orders one through three. When there is at least one match overall, exponential smoothing gives a positive precision to an available order with zero matches; that order stays in the product rather than being removed. A hypothesis with no matches at any order scores zero. The paired comparison uses default corpus BLEU.
For example, a c d shares the unigram a with a b e. The candidate contains two bigrams and one trigram, but none match. Exponential smoothing assigns both orders positive precision. The fourth order is absent because the candidate has only three tokens.
PythonIn [4]Expand
smoothed_probe = bleu_metric.sentence_score("a c d", ["a b e"])
unsmoothed_probe = BLEU(
effective_order=True,
smooth_method="none",
).sentence_score("a c d", ["a b e"])
print("Matched n-grams: ", smoothed_probe.counts)
print("Available n-grams:", smoothed_probe.totals)
print(f"BLEU with smoothing: {smoothed_probe.score:.2f}")
print(f"BLEU without smoothing: {unsmoothed_probe.score:.2f}")ConsoleOut [4]Expand
Matched n-grams: [1, 0, 0, 0] Available n-grams: [3, 2, 1, 0] BLEU with smoothing: 27.52 BLEU without smoothing: 0.00
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!