amharic-speech / eval.py
Chapimenge's picture
Add files using upload-large-folder tool
8c6d529 verified
Raw
History Blame Contribute Delete
4.89 kB
#!/usr/bin/env python3
"""Official evaluation script for this dataset. Shipped inside the release.
Word error rate is only comparable between systems if everyone computes it the
same way, and for Ge'ez script that is not the default. Amharic and Tigrinya
write several distinct characters for one sound -- ሀ, ሐ and ኀ are all /hä/; ሰ and
ሠ are both /sä/; አ and ዐ are both /ʾä/; ጸ and ፀ are both /ṣä/. Which one a writer
picks is orthographic convention, not pronunciation, and different systems settle
on different variants. Scoring raw strings therefore charges a model for spelling
a sound the way its training data spelt it.
This script is the reference implementation. Quote the normalised figure; the raw
one is printed too, because normalising is a judgement call and hiding it would
make the benchmark unauditable.
pip install jiwer
python eval.py --predictions preds.jsonl
`preds.jsonl` has one JSON object per line: {"clip_id": ..., "text": ...}
Clip ids come from the test split of this dataset.
"""
from __future__ import annotations
import argparse
import json
import re
import sys
import unicodedata
# Each pair shifts one consonant family onto its homophone base. The Ethiopic
# block lays every consonant out as a contiguous run of vowel orders from a fixed
# base, so shifting the base and keeping the offset folds all orders at once --
# including the rare ones a hand-written character list would miss.
_GEEZ_FOLD_BASES = [
(0x1210, 0x1200), # ሐ -> ሀ
(0x1280, 0x1200), # ኀ -> ሀ
(0x1220, 0x1230), # ሠ -> ሰ
(0x12D0, 0x12A0), # ዐ -> አ
(0x1340, 0x1338), # ፀ -> ጸ
]
_ORDERS = 8
_GEEZ_MAP = {src + o: dst + o for src, dst in _GEEZ_FOLD_BASES for o in range(_ORDERS)}
_WS = re.compile(r"\s+")
def normalize(text: str, *, fold_geez: bool = True) -> str:
text = unicodedata.normalize("NFC", text)
if fold_geez:
text = text.translate(_GEEZ_MAP)
# Ethiopic punctuation (። ፣ ፤) sits inside the same Unicode block as Ethiopic
# letters, so a character-range test cannot separate them. Ask Unicode.
text = "".join(" " if unicodedata.category(c)[0] in ("P", "S") else c for c in text)
return _WS.sub(" ", text.casefold()).strip()
def evaluate(
references: dict[str, str], predictions: dict[str, str], *, fold_geez: bool = True
) -> dict:
import jiwer
missing = [k for k in references if k not in predictions]
empty = [k for k, v in predictions.items() if not (v or "").strip()]
# A clip with no prediction scores as a full deletion rather than being
# skipped. Dropping it would let a system improve its WER by declining to
# answer on the clips it finds hard.
ids = list(references)
refs = [references[i] for i in ids]
hyps = [predictions.get(i, "") for i in ids]
def measure(r, h):
pairs = [(a, b) for a, b in zip(r, h, strict=True) if a.strip()]
return (
jiwer.wer([p[0] for p in pairs], [p[1] for p in pairs]),
jiwer.cer([p[0] for p in pairs], [p[1] for p in pairs]),
)
raw_wer, raw_cer = measure(refs, hyps)
n_wer, n_cer = measure(
[normalize(r, fold_geez=fold_geez) for r in refs],
[normalize(h, fold_geez=fold_geez) for h in hyps],
)
return {
"clips": len(ids),
"wer": round(n_wer * 100, 2),
"cer": round(n_cer * 100, 2),
"wer_raw": round(raw_wer * 100, 2),
"cer_raw": round(raw_cer * 100, 2),
"missing_predictions": len(missing),
"empty_predictions": len(empty),
}
def main() -> int:
p = argparse.ArgumentParser(description=__doc__)
p.add_argument(
"--predictions", required=True, help='JSONL of {"clip_id": ..., "text": ...}'
)
p.add_argument("--dataset", default="snapwre/amharic-speech")
p.add_argument("--split", default="test")
p.add_argument(
"--no-fold",
action="store_true",
help="score without homophone folding (not comparable)",
)
args = p.parse_args()
from datasets import load_dataset
ds = load_dataset(args.dataset, split=args.split)
references = {r["clip_id"]: r["sentence"] for r in ds}
predictions = {}
with open(args.predictions, encoding="utf-8") as fh:
for line in fh:
if line.strip():
row = json.loads(line)
predictions[row["clip_id"]] = row.get("text", "")
result = evaluate(references, predictions, fold_geez=not args.no_fold)
print(json.dumps(result, indent=2))
if result["missing_predictions"]:
print(
f"\nWARNING: {result['missing_predictions']} of {result['clips']} test "
f"clips had no prediction and were scored as full deletions.",
file=sys.stderr,
)
return 0
if __name__ == "__main__":
raise SystemExit(main())