Download scripts/compare2.py from matthiastl/gepard-onnx: direct link, hf CLI and curl.
- Browser
- Download file 3.18 kB
-
https://huggingface.co/matthiastl/gepard-onnx/resolve/main/scripts/compare2.py
- Command line
-
hf download hf://matthiastl/gepard-onnx/scripts/compare2.py
-
curl -L -o compare2.py https://huggingface.co/matthiastl/gepard-onnx/resolve/main/scripts/compare2.py
3.18 kB
| """RULE-2 2 and 3: pooled word error rates, paired bootstrap over sentences, and runaway counts.""" | |
| import json, re, sys; from pathlib import Path | |
| import numpy as np | |
| sys.path.insert(0, str(Path(__file__).parent)); import texts | |
| from scipy.stats import fisher_exact | |
| ONES = "zero one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen".split(); TENS = "x x twenty thirty forty fifty sixty seventy eighty ninety".split() | |
| def words(n): | |
| if n < 20: return ONES[n] | |
| if n < 100: return TENS[n // 10] + (" " + ONES[n % 10] if n % 10 else "") | |
| if n < 1000: return ONES[n // 100] + " hundred" + (" " + words(n % 100) if n % 100 else "") | |
| return words(n // 1000) + " thousand" + (" " + words(n % 1000) if n % 1000 else "") if n < 10000 else str(n) | |
| ORD = {"first": "1st", "second": "2nd", "third": "3rd", "fifteenth": "15th"} | |
| def norm(s): | |
| s = s.lower().replace("'", " ").replace("’", " ") | |
| s = re.sub(r"\$\s?(\d[\d,]*)", lambda m: m.group(1).replace(",", "") + " dollars", s) | |
| s = re.sub(r"(\d),(\d)", r"\1\2", s); s = re.sub(r"(\d+)[:.](\d\d)\b", r"\1 \2", s) | |
| s = re.sub(r"\b(\d+)(st|nd|rd|th)\b", lambda m: {"1": "first", "2": "second", "3": "third", "15": "fifteenth"}.get(m.group(1), m.group(1)), s) | |
| s = re.sub(r"\d+", lambda m: words(int(m.group())) if int(m.group()) < 10000 else m.group(), s) | |
| s = re.sub(r"[^a-z ]", " ", s); return [w for w in s.split() if w not in ("and", "um", "uh")] | |
| def dist(a, b): | |
| d = np.arange(len(b) + 1) | |
| for i, x in enumerate(a, 1): | |
| p = d.copy(); d[0] = i | |
| for j, y in enumerate(b, 1): d[j] = min(p[j] + 1, d[j - 1] + 1, p[j - 1] + (x != y)) | |
| return int(d[-1]) | |
| ROOT = Path(__file__).resolve().parents[1] / "runs"; SEEDS = (4000, 5000, 6000) | |
| REF = [norm(t) for t in texts.ALL]; N = np.array([len(r) for r in REF]) | |
| def load(tag): | |
| E, run = np.zeros((3, 100), int), np.zeros((3, 100), int) | |
| for si, s in enumerate(SEEDS): | |
| t = json.load(open(ROOT / f"words-{tag}-{s}" / "text.json")) | |
| for i in range(100): | |
| E[si, i] = dist(REF[i], norm(t[f"{i:03d}"])); run[si, i] = np.load(ROOT / f"words-{tag}-{s}" / f"{i:03d}.npy").shape[0] >= 600 | |
| return E, run | |
| eo, ro = load("ref"); ep, rp = load(sys.argv[1]) | |
| wer = lambda E: 100 * E.sum() / (3 * N.sum()) | |
| rng = np.random.default_rng(0); diffs = [] | |
| for _ in range(10000): | |
| k = rng.integers(0, 100, 100); diffs.append(100 * (ep[:, k].sum() - eo[:, k].sum()) / (3 * N[k].sum())) | |
| bound = float(np.percentile(diffs, 95)); fp = fisher_exact([[int(rp.sum()), 300 - int(rp.sum())], [int(ro.sum()), 300 - int(ro.sum())]], alternative="greater")[1] | |
| r = dict(system=sys.argv[1], words=int(3 * N.sum()), wer_original=float(wer(eo)), wer_port=float(wer(ep)), diff=float(wer(ep) - wer(eo)), bound95=bound, runaways_original=int(ro.sum()), runaways_port=int(rp.sum()), fisher_p=float(fp), per_seed_original=[round(100 * e.sum() / N.sum(), 2) for e in eo], per_seed_port=[round(100 * e.sum() / N.sum(), 2) for e in ep]) | |
| r["pass2"], r["pass3"] = bool(bound <= 3.0), bool(fp >= 0.05); print(json.dumps(r)); json.dump(r, open(ROOT / f"compare2-{sys.argv[1]}.json", "w")) | |