Sub topic 10
Measuring Accuracy with Word Error Rate
Prerequisites and goal
This chapter assumes that you can run a Python script, install packages with pip, and produce text with Whisper. You should also understand that a reference transcript is the text judged correct, while a hypothesis is the text produced by the speech-to-text system.
You will learn how to compute word error rate (WER). WER is a score that compares a hypothesis with a reference by counting word-level edits. It helps you compare Whisper model sizes, decoding settings, and audio preprocessing choices.
How WER is calculated
WER uses three types of edits:
- Substitution: one word is replaced by another.
- Deletion: a word from the reference is missing from the hypothesis.
- Insertion: the hypothesis contains an extra word.
The formula is:
WER = (S + D + I) / N
Here, S is the number of substitutions, D is the number of deletions, I is the number of insertions, and N is the number of words in the reference.
For example:
| Reference | Hypothesis | Edit |
|---|---|---|
| the quick brown fox | the quick brown box | one substitution |
There are four reference words and one substitution, so the WER is 1 / 4, or 0.25. A score of 0 means an exact match. Lower scores are better. WER can be greater than 1 when a hypothesis contains many insertions.
The comparison uses alignment, which is the process of matching words between the two texts while minimizing the number of edits.
Normalizing transcripts before comparison
Raw transcripts often differ for reasons that are not meaningful. One transcript might use uppercase letters, punctuation, or a different representation of a number. Decide on normalization rules before calculating scores.
Common steps include:
- Convert text to lowercase.
- Remove punctuation.
- Collapse repeated whitespace.
- Decide whether numbers such as
10andtenshould match. - Remove non-speech labels if the reference does not contain them.
Use the same normalization rules for every system you compare. If you change the rules between experiments, the scores are not directly comparable.
Computing WER with `jiwer`
The jiwer package implements alignment and WER calculation.
Install it with:
pip install jiwerThen run this complete example:
import jiwer
reference = "the quick brown fox"
hypothesis = "the quick brown box"
score = jiwer.wer(reference, hypothesis)
print(f"WER: {score:.2f}")The output is:
WER: 0.25For a real evaluation, keep a fixed test set of audio files and reference transcripts. Transcribe every file with each model or setting, then calculate one score over the complete collection. This is more reliable than judging a single short recording.
Interpreting results carefully
A lower overall WER does not guarantee better performance for every user. Inspect scores by language, speaker, accent, recording environment, and topic. Background noise and overlapping speakers can produce different error patterns.
Also check whether the reference transcript itself is consistent. A transcript with missing words, inconsistent names, or different number conventions can make a good hypothesis appear incorrect.
Exercise
The reference contains 20 words. The hypothesis has two substitutions and one deletion, with no insertions. What is the WER?
Show answer
WER = (2 + 1 + 0) / 20 = 0.15, or 15%.