Bilingual Evaluation Understudy (BLEU) evaluates the similarity between a target sentence and a generated sentence. It counts the number of n-grams that appear in both the generated sentence and the target sentence, and it is mostly used in machine translation applications.
- common n-gram sizes used are 1, 2, 3 or 4
- BLEU calculates the precision of matching n-grams
- the target sentence is the human reference, and the predicted sentence is the model output
Precision
Working with 1-grams, which are single words:
- Predicted Sentence:
He eats an apple - Target Sentence:
He ate an apple - the precision is , because
"eats"has no match
Clipped Precision
Plain precision can be gamed by repetition. A predicted sentence of "He He He" scores well because every word appears in the target, even though the sentence is useless.
To avoid repetitions, clipped precision is used instead of precision:
- each word from the predicted sentence is compared with all the target sentences, and if the word has a match in any target sentence it is considered correct
- the count for each correct word is limited to the maximum number of times that word occurs across all target sentences
For a predicted sentence of "He He He eats tasty fruit" against 2 target sentences, the clipped precision is 2/6, because the repeated instances of "He" are not counted.
Geometric Average Precision
The precisions of the separate n-gram sizes are combined into 1 number:
For the default of 4 n-gram sizes with equal weights:
- is the clipped precision for that n-gram size
- is the weight given to that n-gram size
- a geometric average is used, so a score of 0 at any n-gram size drives the whole product to 0
Brevity Penalty
A brevity penalty is then added to penalise sentences that are too short, since shorter sentences can generate a misleading probability.
Where is the predicted length and is the target length.
- a prediction longer than the target is not penalised, because precision already punishes extra words
- a prediction shorter than the target is scaled down, and the shorter it is the harder the penalty
Scoring
Code
import nltk
from nltk import word_tokenize
from nltk.translate.bleu_score import SmoothingFunction
ref = 'The guard arrived late because it was raining.'
cand = 'The guard arrived late because of the rain.'
smoothie = SmoothingFunction().method1
reference = word_tokenize(ref)
candidate = word_tokenize(cand)
weights = (0.25, 0.25, 0.25, 0.25)
BLEUscore = nltk.translate.bleu_score.sentence_bleu([reference], candidate, weights, smoothing_function=smoothie)
print(BLEUscore)- the reference is passed inside a list, because a sentence can be scored against several target sentences at once
weightsof 4 values at 0.25 each is the default 4-gram setting, and it matches the geometric average aboveweightshere shows0.25for 1-gram precision, 2-gram precision, 3-gram precision, and 4-gram precision- All 4 grams' scores receives equal importance to how much they affect the final BLEU score, and the weights add up to
1 - a
SmoothingFunctionavoids a score of 0 when a higher order n-gram has no match at all
BLEU is precision oriented, and its recall oriented counterpart is Recall-Oriented Understudy for Gisting Evaluation (ROUGE).