ek.metrics.strings#
String metrics: Character/Word Error Rate (CER/WER).
CER and WER are graded edit-distance metrics – the right baseline for OCR/ASR and free-text fields, far less brittle than exact match. Two correctness rules from the research drive the design:
Accumulate globally. A corpus error rate is
sum(edits) / sum(reference length)over the whole corpus – never the mean of per-item rates (variable lengths make that mathematically wrong). So eachScorecarries raweditsandref_lenin itsdetail, andStringMetric.aggregate()sums them.evaluate()uses this.CER/WER are error rates, not bounded similarities. A hypothesis longer than the reference pushes the rate above 1.0 – itself a useful hallucination signal.
Score.valueis the error rate (lower is better;detail["higher_is_better"] is False).
The canonical backend is jiwer (Apache-2.0, rapidfuzz C++ core), which also
yields the substitution/deletion/insertion/hit breakdown. ek prefers it,
falls back to rapidfuzz directly, then to a pure-Python edit distance, so the
metric works even in a bare environment.
Classes
|
CER ( |
- class ek.metrics.strings.StringMetric(mode='cer', *, canonicalizer=None)[source]#
Bases:
objectCER (
mode='cer') or WER (mode='wer') as aMetric.- Parameters:
mode (
str) –"cer"(character) or"wer"(word) error rate.canonicalizer – Optional
str -> strapplied to both sides before scoring (seeek.canonicalize). Usually supplied by the facade instead.