Sub topic 2
Tokens and n-grams: the raw material of overlap metrics
Prerequisites and the evaluation setup
Assume you know basic Python, collections.Counter, and precision, recall, and F1 score. You should also have a rough idea of word embeddings and transformer language models. This chapter focuses on the text representation used by BLEU and ROUGE. BERTScore uses a different representation, which appears later.
Automatic metrics usually compare a generated text, called the candidate, with one or more human-written references. They do not directly measure whether the candidate is useful or factually correct. They measure selected patterns of agreement.
Tokens: how text becomes a sequence
A token is a unit produced by a tokenization rule. For simple word-level evaluation, the tokens might be lowercase words:
The cat sat on the mat.
→ the | cat | sat | on | the | matReal tokenizers must decide what to do with punctuation, capitalization, contractions, numbers, and subwords. These decisions affect every later score. For example, cat and Cat match under lowercase tokenization but do not match under case-sensitive tokenization.
A tokenizer can split punctuation into separate tokens:
The cat's bowl.
→ the | cat | 's | bowl | .Another tokenizer might keep cat's as one token. Neither choice is universally correct. The important requirement is to use the same documented process for candidates and references.
N-grams: local sequences of tokens
An n-gram is a consecutive sequence of n tokens. A unigram has one token, a bigram has two, and a trigram has three.
For this sequence:
the cat sat on the matthe bigrams are:
the cat
cat sat
sat on
on the
the matOrder matters. The bigram cat sat differs from sat cat. Counts also matter. The unigram the occurs twice in this sentence.
The following function creates n-gram counts:
from collections import Counter
def ngrams(tokens, n):
return Counter(
tuple(tokens[i:i + n])
for i in range(len(tokens) - n + 1)
)
tokens = "the cat sat on the mat".split()
print(ngrams(tokens, 1))
print(ngrams(tokens, 2))A sequence containing fewer than n tokens has no n-grams of order n. This matters for short candidates: a one-word candidate cannot produce a bigram.
Why tokenization choices affect scores
Consider these candidate and reference texts:
Candidate: The cat sat.
Reference: A cat was sitting.They communicate a related idea, but exact word overlap is limited. The word cat matches, while sat and sitting do not. A word-overlap metric may therefore assign a low score even when a reader sees a reasonable paraphrase.
Common choices that change overlap include:
- lowercase versus case-sensitive comparison
- word tokens versus subword tokens
- punctuation removal or retention
- stemming, which reduces words to approximate roots
- treating repeated n-grams as separate occurrences
- selecting one reference or combining multiple references
> callout note > Tokenization is part of the metric definition. A score is not fully reproducible unless the tokenizer, normalization rules, n-gram orders, and reference handling are reported.
In the next steps, n-gram counts become precision, recall, and F1 values. BLEU emphasizes modified n-gram precision, while ROUGE commonly emphasizes n-gram recall. Both depend on the same raw token sequences.
Exercise
For the tokens ["a", "b", "a"], how many distinct bigrams are there, and what are their counts?
Show answer
There are two distinct bigrams: ("a", "b") occurs once and ("b", "a") occurs once.