lawmaluki's picture
Rename normalize.py to kambabench/scoring/normalize.py
5b693a2 verified
Raw
History Blame Contribute Delete
2.18 kB
"""
normalize.py — Kamba text normalization for KambaBench-ASR.
Versioned deliberately: bump __version__ and log the change in NORMALIZATION_CHANGELOG
whenever behavior changes, so a benchmark result is always reproducible against a specific
normalization version rather than silently drifting.
STATUS (v0.1): only safe, language-agnostic normalization is implemented — NFKC, code-switch /
disfluency tag stripping, punctuation stripping, whitespace collapse, lowercasing. No
Kamba-specific orthographic collapsing (e.g. documenting and merging accepted spelling variants)
has been added yet. Per the benchmark's orthography principle, that layer will only be added
after examining real transcripts from the candidate corpora and consulting native Kamba
speakers — see docs/ORTHOGRAPHY.md.
"""
import re
import unicodedata
__version__ = "0.1"
NORMALIZATION_CHANGELOG = {
"0.1": (
"Initial version. NFKC normalization; strip [cs]/[cs:en]/[cs:sw]-style code-switch "
"markers while keeping the inner words; strip [pause]/[sigh]/[laugh]/[breath]/[noise]/"
"[silence]-style disfluency tags; strip standard punctuation; collapse whitespace; "
"lowercase. No Kamba-specific orthographic rules yet."
),
}
_CS = re.compile(r"\[cs(?::(?:en|sw))?\]")
_DISFLUENCY = re.compile(r"\[(?:pause|sigh|laugh|breath|noise|silence)\]")
# TODO(orthography, v0.2+): confirm with native speakers whether Kamba orthography has any
# character that punctuation-stripping should NOT touch (some other Kenyan languages' writing
# systems use the apostrophe or hyphen to represent a real sound, not punctuation). Until
# confirmed, standard punctuation below is stripped uniformly. This does not affect the ĩ/ũ
# diacritics used in Kamba orthography — NFKC does not strip them, and they are not in this set.
_PUNCT = re.compile(r"[.,;?!\"/\\]")
_WS = re.compile(r"\s+")
def normalize(s: str) -> str:
"""Apply KambaBench-ASR normalization v{__version__} to a transcript string."""
s = unicodedata.normalize("NFKC", s)
s = _CS.sub(" ", s)
s = _DISFLUENCY.sub(" ", s)
s = _PUNCT.sub(" ", s)
return _WS.sub(" ", s).strip().lower()