Comparing Generated Responses to Reference Data
Lexical similarity measures how much two texts overlap, most simply by counting how many tokens they share with a reference.
The Shortcut of Matching Surface Words
Suppose you need to judge whether a generated response is good. A tempting shortcut is to hold it up against a reference response, a trusted answer written in advance, and ask how much the two texts look alike on the surface. In evaluation work this shortcut has a name: lexical similarity. It is one of the most common ways to score generated text, and it is easy to apply. It is also easy to misread.
The belief many learners start with is this: if a generated response shares more words with the reference, it must be the better response. The reasoning sounds sensible. Overlap seems like evidence of correctness, and a score is a number that can be ranked. The trouble is that the measurement only sees matching surface material. It does not see whether the meaning is right, whether the answer is useful, or whether a different but correct wording was simply phrased differently from the reference.
This article follows one reference sentence through several measurements, and it ends by showing where those measurements break. Keep the reference sentence in mind: My cats scare the mice.
Counting Shared Tokens
Lexical similarity measures how much two texts overlap. The first step is to break each text into smaller units called tokens. In the simplest form of the measure, you count how many tokens two texts have in common, and you express that count relative to the reference. In the source's example, each token is a word, so the reference My cats scare the mice has 5 tokens.
Overlap is the number of tokens that appear in both the generated response and the reference, divided by the number of tokens in the reference. The reference is the denominator because it is the yardstick the response is measured against. Shared tokens are the numerator. The result is usually written as a percentage.
Scoring two responses against one reference
The reference is 'My cats scare the mice', which has 5 tokens. Response A shares 4 of those tokens. Response B shares 3. What is the overlap score for each response?
- 1Count the reference tokens
My, cats, scare, the, mice. The reference has 5 tokens, so the denominator is 5.
- 2Score Response A
Response A shares 4 of the 5 tokens. Overlap is 4 divided by 5, which is 80 percent.
- 3Score Response B
Response B shares 3 of the 5 tokens. Overlap is 3 divided by 5, which is 60 percent.
- 4Read the ranking
Response A has the higher score. The score alone only says that A shares more surface words with the reference. It does not yet say that A is better.
Response A scores 80 percent and Response B scores 60 percent.
The diagram uses a generated response, Cats frighten the mice with my friends, to show the split. Only scare is missing from the response's match. Here the response has the words my, cats, the, and mice in common with the reference, so the overlap count comes from those four shared tokens. Any token that appears only in the response, like frighten, adds nothing to the count, and any reference token the response lacks, like scare, reduces it.
Approximate Matching With Edit Distance
Counting whole shared words is coarse. A response that spells a word slightly differently gets no credit for that word at all. Approximate string matching, sometimes called fuzzy matching, handles this by measuring similarity through the number of edits needed to turn one text into another. That number is called the edit distance. A smaller edit distance means the texts are more similar.
The three usual edit operations are deletion, which removes a character, insertion, which adds a character, and substitution, which replaces one character with another. Each operation changes the text by exactly one character. Edit distance counts how many of these operations are needed to get from the first text to the second.
Applying each edit operation once
Find the edit distance between the pairs cats and cat, cat and cart, and cat and bat, using one operation at a time.
- 1Deletion
Turning cats into cat removes the final s. That is one deletion, so the edit distance is 1.
- 2Insertion
Turning cat into cart adds an r between the a and the t. That is one insertion, so the edit distance is 1.
- 3Substitution
Turning cat into bat replaces the c with a b. That is one substitution, so the edit distance is 1.
- 4Compare with word counting
Word counting treats cats and cat as two different tokens with no credit at all. Edit distance records that they differ by one character, so the near match still registers as similar.
Each pair differs by one edit, so each has edit distance 1. A smaller edit distance means the texts are more similar.
Scoring Word Order With N-Grams
N-gram similarity changes the unit of comparison. Instead of checking single tokens, it checks sequences of tokens, called n-grams. A 1-gram, also called a unigram, is a single token. A 2-gram, or bigram, is a set of two consecutive tokens. Larger n-grams follow the same pattern with longer windows. Because the unit is a sequence, n-gram similarity notices when words appear in the same order, which single-word counting does not.
To find the bigrams of a sentence, slide a two-token window across it one position at a time. Each position produces one bigram. The reference sentence My cats scare the mice has five tokens, so the window produces four bigrams in order.
The measure works by checking what percentage of the n-grams in the reference also appear in the generated response. For the reference sentence My cats scare the mice, the four bigrams are my cats, cats scare, scare the, and the mice. Setting the single-word units beside these two-word units shows the difference in granularity: five unigrams against four bigrams, with each bigram carrying information about which word came next.
What do you think happens?
A generated response uses exactly the same five words as the reference but in the order: the mice the scare cats my. How many of the reference's four bigrams does it share?
Reveal answer
One bigram, the mice.
The scrambled response produces the bigrams the mice, mice the, the scare, scare cats, and cats my. Only the mice matches a reference bigram (the reference bigram is the mice). It still shares all five unigrams with the reference, which shows that unigram counting cannot see the reordering.
This is the main gain from bigrams. The scrambled response would still share all five unigrams with the reference, but it shares few bigrams. Bigram overlap therefore rewards phrases that match the reference's local word order. That is useful, but it still counts surface sequences rather than meaning.
Named Metrics Built on These Ideas
Several named metrics apply these ideas in different ways, including BLEU, ROUGE, METEOR++, TER, and CIDEr. They differ in exactly how the overlapping is calculated. BLEU in particular is a common lexical similarity metric, especially for translation tasks.
Before foundation models became prevalent, BLEU, ROUGE, and their relatives were common. Since then, fewer benchmarks use lexical similarity, though some still do, such as WMT, COCO Captions, and GEMv2.
Why a Higher Score Can Mislead
The misconception breaks down for two separate reasons, and both come from the reference data rather than from the generated text. Lexical similarity can only reward resemblance to the references that were written. If a correct answer uses different words, it has nothing to match against.
A correct caption that scored low
Adept's model Fuyu produced a correct image caption that named Big Ben and Parliament. How did the lexical score treat it?
- 1Read the reference captions
The reference captions described a fast moving image of cars on a busy street with a tower clock, and a city building lit up with traffic.
- 2Check for the named landmarks
None of the reference captions mentioned Big Ben or Parliament, so those words had no match in the reference set.
- 3Read the score
The correct caption received a CIDEr score of 0.4. The low score reflects the missing wording in the references, not an error in the caption.
The caption was correct, and the reference set simply did not contain its wording.
Common mistake Treating a higher overlap score as proof of a better response
What it looks likeResponse A scores 80 percent and Response B scores 60 percent, so A is assumed to be the better answer.
Why it failsThe score only counts shared surface tokens. It does not check whether the meaning is correct or whether the answer is useful.
Do this insteadRead the score as a measure of resemblance to the reference, then check meaning and correctness separately.
Common mistake Assuming the reference is always correct
What it looks likeA response that disagrees with a reference is marked down even when the reference is the faulty one.
Why it failsThe WMT 2023 Metrics shared task organizers reported that they found many bad reference translations in their data.
Do this insteadInspect the references before trusting a low score, especially when the score conflicts with careful human review.
Common mistake Expecting unigram counts to notice word order
What it looks likeA scrambled sentence with the same five words gets the same unigram overlap as the original.
Why it failsUnigram counting treats tokens as a set, so order is invisible to it.
Do this insteadAdd bigram or other n-gram comparison when local word order matters.
Common mistake Assuming textual closeness means working code
What it looks likeA code solution that looks close to the reference is assumed to run correctly.
Why it failsOn HumanEval, OpenAI found that BLEU scores for incorrect and correct solutions were similar, so optimizing for BLEU is not the same as optimizing for functional correctness.
Do this insteadTest code for functional correctness directly rather than relying on overlap.
Checking Your Understanding
The reference is 'The dog chased the ball'. It has 5 tokens. Response X shares 3 tokens with it, and Response Y shares 4. Compute each overlap score, then name one reason the higher score might still not identify the better response.
Hints
- Overlap equals shared tokens divided by the number of tokens in the reference.
- Response X gives 3 divided by 5, and Response Y gives 4 divided by 5.
- Think about whether the reference could be incomplete or whether a correct answer could use different words.
List the bigrams of the sentence 'Cats chase mice at night'. Then explain which bigram would be lost if a generated response wrote 'Mice chase cats at night'.
Hints
- Slide a two-token window across the sentence one position at a time.
- Compare the bigram lists of the two sentences, looking for pairs that change.
Key Takeaways
- Lexical similarity measures how much two texts overlap, most simply by counting the tokens they share and dividing by the reference's token count.
- Edit distance scores near matches by counting single-character deletions, insertions, and substitutions, so a slightly misspelled word still earns partial similarity.
- N-gram similarity, such as bigram overlap, captures local word order that single-token counts miss.
- A higher overlap score does not always mean a better response, because a correct answer can score low when the references lack its wording, and references themselves can be wrong.
- Overlap does not measure functional correctness, as the HumanEval finding shows, so pair lexical scores with a check of meaning or of whether code works.