Download code/tk/wer.py from Lucasgitguy-co/Turbo-Kev-Dictate-1.4B: direct link, hf CLI and curl.
- Browser
- Download file 1.2 kB
-
https://huggingface.co/Lucasgitguy-co/Turbo-Kev-Dictate-1.4B/resolve/main/code/tk/wer.py
- Command line
-
hf download hf://Lucasgitguy-co/Turbo-Kev-Dictate-1.4B/code/tk/wer.py
-
curl -L -o wer.py https://huggingface.co/Lucasgitguy-co/Turbo-Kev-Dictate-1.4B/resolve/main/code/tk/wer.py
1.2 kB
| """Shared WER helpers (same normalisers as LvW-Dictate: G1 keeps fillers, G2 uses Whisper's English normaliser).""" | |
| from __future__ import annotations | |
| import json | |
| from functools import lru_cache | |
| from pathlib import Path | |
| import jiwer | |
| def _norms(): | |
| from whisper_normalizer.basic import BasicTextNormalizer | |
| from whisper_normalizer.english import EnglishTextNormalizer | |
| return EnglishTextNormalizer(), BasicTextNormalizer() | |
| def n_basic(t: str) -> str: | |
| """Lower-case, no punctuation, fillers KEPT.""" | |
| return " ".join(_norms()[1](t or "").split()) | |
| def n_en(t: str) -> str: | |
| return " ".join(_norms()[0](t or "").split()) | |
| def corpus_wer(refs: list[str], hyps: list[str], norm) -> float: | |
| pairs = [(norm(r), norm(h)) for r, h in zip(refs, hyps)] | |
| pairs = [(r, h) for r, h in pairs if r] | |
| o = jiwer.process_words([r for r, _ in pairs], [h for _, h in pairs]) | |
| n = sum(len(r.split()) for r, _ in pairs) | |
| return round(100 * (o.substitutions + o.deletions + o.insertions) / max(1, n), 3) | |
| def read_jsonl(p: Path) -> list[dict]: | |
| p = Path(p) | |
| return [json.loads(l) for l in p.read_text().splitlines() if l.strip()] if p.exists() else [] | |