Sub topic 12
Measuring transcription quality with word error rate
This chapter assumes you can call the transcription endpoint from Python, as in the earlier chapters, and that you can install a package with pip. You don't need any statistics background.
Why measure
Transcripts that look fine on one sample can fail on your real audio. Accents, background noise, jargon, and phone-quality recordings all change accuracy. If you change a prompt, a language setting, or the provider, you need a number to compare against. Word error rate (WER) is the standard number for this.
What WER counts
WER compares a human-written reference transcript with the hypothesis, which is the text the API returned. It counts three kinds of mistakes:
- Substitutions (S): a word was replaced by another word.
- Deletions (D): a word in the reference is missing.
- Insertions (I): the hypothesis has an extra word.
The formula is:
WER = (S + D + I) / N
N is the number of words in the reference. A lower score is better, and 0 means a perfect match.
Computing WER in Python
The jiwer package implements the calculation. Install it with pip install jiwer.
import jiwer
reference = "please schedule the review for friday morning"
hypothesis = "please schedule the review for friday mourning"
print(f"WER: {jiwer.wer(reference, hypothesis):.3f}")The reference has seven words, and one was substituted ("morning" became "mourning"). So WER is 1 / 7, which prints as 0.143.
Normalize before you compare
WER treats "Friday," and "friday" as different words. Without cleanup, capitalization and punctuation inflate the score and hide real errors. Apply the same normalization to both texts: lowercase, strip punctuation, and collapse extra spaces. Decide how to treat numbers too, since "10" and "ten" will count as a mismatch unless you convert them.
import jiwer
normalize = jiwer.Compose([
jiwer.ToLowerCase(),
jiwer.RemovePunctuation(),
jiwer.RemoveMultipleSpaces(),
jiwer.Strip(),
])
reference = "Please schedule the review for Friday morning."
hypothesis = "please schedule the review for friday morning"
print(jiwer.wer(normalize(reference), normalize(hypothesis)))This prints 0.0, because the two texts match once formatting is removed.
Building a small reference set
- Pick 10 to 30 clips that look like your production audio, including some hard ones.
- Transcribe each clip by hand, or correct an API transcript carefully.
- Run the API on every clip with a fixed set of options.
- Normalize both sides and score each clip.
- Report the average and the worst clips, not only the average.
Change one option at a time, such as adding a prompt with domain vocabulary, then score the same clips again. Keep the references fixed so the scores stay comparable.
Common errors
- Reading WER as a percentage of correct words. A WER of 0.143 does not mean 85.7% accuracy in any strict sense, and WER can exceed 1.0 when there are many insertions.
- Comparing scores across different normalization rules.
- Scoring only clean audio and expecting the same result in production.
- Ignoring which words matter. One wrong name or number can break your app, while several wrong filler words may not.
- Using a tiny test set. With five clips, one bad recording swings the average.
Exercise
A reference transcript has 20 words. The API output has 2 substituted words, 1 missing word, and 1 extra word. What is the WER?
Show answer
S = 2, D = 1, I = 1, so the errors total 4. WER = 4 / 20 = 0.2.