Sub topic 10
Measuring accuracy with word error rate
This chapter assumes you can run the openai-whisper examples from the earlier chapters and that you are comfortable with Python lists and dictionaries. You need pip install jiwer openai-whisper. By the end you will be able to compute word error rate (WER), normalize text so the number means something, and compare Whisper model sizes and settings fairly.
What WER measures
Word error rate compares a reference (the correct transcript, usually written by a human) with a hypothesis (what the model produced). It counts the minimum number of word edits needed to turn the hypothesis into the reference:
- A substitution (S) is a word replaced by a different word.
- A deletion (D) is a reference word missing from the hypothesis.
- An insertion (I) is an extra word in the hypothesis.
The formula divides the edit count by the number of words in the reference, N:
WER = (S + D + I) / NThe alignment that gives the smallest S + D + I is found with Levenshtein edit distance, computed over words instead of characters. Words that match are called hits. N equals hits + S + D.
The following example has one of each error type:
| Reference word | Hypothesis word | Result |
|---|---|---|
| the, quick, brown | the, quick, brown | hits |
| fox | box | substitution |
| jumps, over | jumps, over | hits |
| the | (missing) | deletion |
| lazy, dog | lazy, dog | hits |
| (nothing) | today | insertion |
The reference has 9 words and there are 3 errors, so WER is 3/9, about 0.333.
Computing WER with jiwer
The jiwer library implements the alignment. jiwer.wer returns the score. jiwer.process_words returns the full breakdown.
import jiwer
reference = "the quick brown fox jumps over the lazy dog"
hypothesis = "the quick brown box jumps over lazy dog today"
out = jiwer.process_words(reference, hypothesis)
print(f"WER: {out.wer:.3f}")
print("hits:", out.hits)
print("substitutions:", out.substitutions)
print("deletions:", out.deletions)
print("insertions:", out.insertions)This prints a WER of 0.333, with 7 hits, 1 substitution, 1 deletion, and 1 insertion.
Always look at the breakdown, not just the total. The error types point to different causes:
- Many substitutions usually mean misheard words, names, or accents.
- Many deletions often mean dropped speech, such as fast talk, quiet speakers, or overlapping voices.
- Many insertions often mean hallucinated text, repeated loops, or transcribed noise.
Properties to remember
- WER can exceed 1.0. A hypothesis with many insertions can have more errors than the reference has words. A model that loops on one phrase can score 3.0 or worse.
- WER is not a percentage of correct words. Lower is better, and 0.0 is a perfect match.
- All words count equally. Missing "not" costs the same as missing "the", although it changes the meaning.
- The reference must not be empty. The score divides by N, and
jiwerraises an error for an empty reference string. - WER depends on the reference. Human transcribers disagree with each other, so a WER of zero is not always reachable or even desirable.
Normalize before you score
Raw WER punishes differences that a listener would not call errors. Compare these two scores:
import jiwer
from whisper.normalizers import BasicTextNormalizer
reference = "Hello, world!"
hypothesis = "hello world"
print("raw:", jiwer.wer(reference, hypothesis))
normalizer = BasicTextNormalizer()
print("normalized:", jiwer.wer(normalizer(reference), normalizer(hypothesis)))The raw score is 1.0, because "Hello," and "world!" do not equal "hello" and "world" as strings. After normalization it is 0.0.
The whisper.normalizers module ships with openai-whisper and offers two classes:
BasicTextNormalizeris language-neutral. It lowercases text and strips punctuation and symbols.EnglishTextNormalizerdoes more for English. It standardizes spelling variants (such as British and American forms), expands contractions and number words, and removes filler words like "um".
Whisper's output includes capitalization, punctuation, and digits. Human references may use none of these, or a different style. Without normalization you measure formatting disagreement along with recognition quality.
Scoring a Whisper transcription
The next example transcribes one file and scores it against a reference transcript stored in a text file.
import jiwer
import whisper
from whisper.normalizers import EnglishTextNormalizer
model = whisper.load_model("base.en")
result = model.transcribe("sample.wav", language="en", fp16=False)
with open("sample_reference.txt", encoding="utf-8") as f:
reference = f.read()
normalizer = EnglishTextNormalizer()
ref = normalizer(reference)
hyp = normalizer(result["text"])
out = jiwer.process_words(ref, hyp)
print(f"WER: {out.wer:.3f} S={out.substitutions} D={out.deletions} I={out.insertions}")Adjust the file names to your own data. The base.en checkpoint is the English-only base model, so setting language="en" matches it.
Scoring many files
For a test set, pass lists of strings. jiwer pools all the errors and divides by the total number of reference words:
import jiwer
references = ["turn the lights off", "what time is it"]
hypotheses = ["turn the light off", "what time is it now"]
print(jiwer.wer(references, hypotheses))There are 9 reference words and 2 errors (one substitution and one insertion), so the result is 2/9, about 0.222.
This differs from averaging the WER of each file. A long file contributes more words, so it influences the pooled score more. This is usually what you want. It is wrong to average per-file scores when file lengths differ widely, because a three-word clip would count as much as a one-hour lecture.
Comparing models and settings fairly
To choose a model size or a decoding option, hold everything else constant:
- Use the same audio and the same references for every run.
- Use the same normalizer.
- Change one variable at a time, such as model size,
beam_size, orlanguage. - Record the S, D, and I counts, and also the runtime.
The next example compares model sizes on a small list of files:
import jiwer
import whisper
from whisper.normalizers import EnglishTextNormalizer
normalizer = EnglishTextNormalizer()
dataset = [
("clip1.wav", "reference transcript for clip one"),
("clip2.wav", "reference transcript for clip two"),
]
for size in ["tiny.en", "base.en", "small.en"]:
model = whisper.load_model(size)
refs, hyps = [], []
for path, text in dataset:
result = model.transcribe(path, language="en", fp16=False)
refs.append(normalizer(text))
hyps.append(normalizer(result["text"]))
print(size, round(jiwer.wer(refs, hyps), 3))Replace the placeholder file names and reference text with your own data before running. Make sure no reference becomes empty after normalization, because jiwer rejects empty references.
Reading results with care
- Sample size. A handful of short clips gives a noisy WER. A difference of a point or two between models on a small set is not evidence that one is better.
- Domain match. A model that scores well on clean read speech may do worse on phone calls, accented speech, or technical vocabulary. Test on audio like your own.
- Hallucinations. Whisper can produce fluent text for silence or noise. These show up as insertions. Counting insertions separately helps you spot this failure mode, which the limitations chapter covers.
- Languages without spaces. WER splits on whitespace, so it is a poor fit for Chinese, Japanese, or Thai. Use character error rate (
jiwer.cer) for these. - Numbers and names. "twenty five", "25", and "25th" differ as words. The English normalizer handles many cases, but check a sample of alignments by eye.
Limits of WER
WER says how many words differ. It does not say whether the meaning was preserved, whether the transcript is readable, or whether punctuation and casing are correct. It also ignores timestamps and speaker labels. Use it as one measure alongside a read-through of the actual errors, especially when the transcripts feed a downstream task like search or summarization.
Exercise
A reference has 20 words. After alignment against a Whisper hypothesis there are 2 substitutions, 1 deletion, and 3 insertions. What is the WER, and what does the insertion count suggest you check?
Show answer
WER = (2 + 1 + 3) / 20 = 6/20 = 0.30. The relatively high number of insertions suggests checking for hallucinated or repeated text, such as output generated over silence or noise. Another possibility is a normalization mismatch that splits words, such as hyphenated or contracted forms.
Exercise
Why is jiwer.wer(["a b", "c d e f g h"], hyps) not the same as the mean of the two per-file WERs, and which is usually better for a test set?
Show answer
jiwer pools errors across all files and divides by the total reference word count, so the 6-word file counts three times as much as the 2-word file. The mean of per-file WERs weights both files equally. The pooled score is usually better because it reflects the error rate per spoken word and is not distorted by very short clips.