Sub topic 1
Why Generated Text Is Hard to Score
Prerequisites
You should be comfortable with Python lists, dictionaries, and collections.Counter. You should also understand precision, recall, and F1. A rough idea of word embeddings and transformer language models will help in later chapters.
Why generated text is hard to score
A model can write many good answers to one prompt. "The cat sat on the mat" and "A cat was sitting on the rug" mean nearly the same thing but share few words. Automatic metrics such as BLEU, ROUGE, and BERTScore compare generated text with one or more human-written references. They are proxies. A high score means the output resembles the reference, not that it is good.
Tokens and n-grams
A token is a unit of text, here a whitespace-separated word. An n-gram is a run of n consecutive tokens. Overlap metrics count how many n-grams the candidate shares with the reference.
Precision, recall, and F1 for text overlap
Let m be the number of matching n-grams. Precision is m divided by the number of n-grams in the candidate. Recall is m divided by the number of n-grams in the reference. F1 is the harmonic mean of the two. BLEU is built on precision, and ROUGE is built on recall.
BLEU part 1: clipped n-gram precision
Plain precision rewards repetition. The candidate "the the the the the the the" scores 7/7 because every word appears in the reference. BLEU fixes this by clipping each candidate n-gram count to its highest count in any single reference.
from collections import Counter
def ngrams(tokens, n):
return [tuple(tokens[i:i + n]) for i in range(len(tokens) - n + 1)]
def clipped_precision(candidate, references, n):
cand = Counter(ngrams(candidate, n))
max_ref = Counter()
for ref in references:
for gram, count in Counter(ngrams(ref, n)).items():
max_ref[gram] = max(max_ref[gram], count)
clipped = sum(min(count, max_ref[gram]) for gram, count in cand.items())
total = sum(cand.values())
return clipped / total if total else 0.0
candidate = "the the the the the the the".split()
references = ["the cat is on the mat".split(), "there is a cat on the mat".split()]
print(clipped_precision(candidate, references, 1)) # 0.2857...By hand: "the" appears 7 times in the candidate, but at most 2 times in any reference. The clipped count is 2, so the unigram precision is 2/7.
BLEU part 2: the brevity penalty
Precision alone favors short outputs that contain only safe words. BLEU multiplies the precision by a brevity penalty (BP). Let c be the candidate length and r the reference length. BP is 1 when c > r, and exp(1 - r/c) otherwise. For a candidate "the cat" (c = 2) and a reference of six words (r = 6), BP is exp(1 - 3), about 0.135.
Check your understanding
Exercise
Candidate "the cat" is scored against reference "the cat sat". What are the clipped unigram precision and the brevity penalty?
Show answer
Both words appear in the reference, so the clipped count is 2 and the precision is 2/2 = 1.0. The candidate length is c = 2 and the reference length is r = 3, so BP = exp(1 - 3/2) = exp(-0.5), about 0.607.