JiWER uses a pipeline pattern to transform input strings (sentences) into a specific format required for calculating Word Error Rate (WER) or Character Error Rate (CER).
To use transformations, you chain them using Compose.
Crucial Requirement: Every pipeline must end with a specific 'reduction' transform depending on your goal:
- For WER: The pipeline must end with
ReduceToListOfListOfWords. This converts sentences into a list of lists, where each inner list contains individual words. - For CER: The pipeline must end with
ReduceToListOfListOfChars. This converts sentences into a list of lists, where each inner list contains individual characters.
If you do not end with one of these, the output will not be in the format expected by the error rate calculation functions.
import jiwer
# Example pipeline for WER
pipeline = jiwer.Compose([
jiwer.ToLowerCase(),
jiwer.RemovePunctuation(),
jiwer.RemoveMultipleSpaces(),
jiwer.ReduceToListOfListOfWords()
])
result = pipeline(["Hello, World!"])
# Result: [['hello', 'world']]