"""RULE-4: pooled word error rates with voices, paired bootstrap over sentences, runaway counts. `check` reproduces RULE-3's numbers.""" import json, sys; from pathlib import Path import numpy as np sys.path.insert(0, str(Path(__file__).parent)); import texts, texts4 from scipy.stats import fisher_exact import re src = open(Path(__file__).parent / "compare2.py").read(); exec(src[src.index("ONES = "):src.index("ROOT = Path")]) # the same norm() and dist() ROOT = Path(__file__).resolve().parents[1] / "runs" check = sys.argv[1:] == ["check"] T, SEEDS, NAME = (texts.ALL, (8000, 9000, 10000), "words3") if check else (texts4.TEXTS, (20000, 21000, 22000, 23000), "words4") REF = [norm(t) for t in T]; N = np.array([len(r) for r in REF]); S, K = len(T), len(SEEDS); U = S * K def load(tag): E, run = np.zeros((K, S), int), np.zeros((K, S), int) for si, s in enumerate(SEEDS): d = ROOT / f"{NAME}-{tag}-{s}"; t = json.load(open(d / "text.json")); assert len(t) == S for i in range(S): E[si, i] = dist(REF[i], norm(t[f"{i:03d}"])); run[si, i] = np.load(d / f"{i:03d}.npy").shape[0] >= 600 return E, run eo, ro = load("ref"); ep, rp = load("decoder.int8"); W = K * N.sum() rng = np.random.default_rng(0); d = [] for _ in range(10000): k = rng.integers(0, S, S); d.append(100 * (ep[:, k].sum() - eo[:, k].sum()) / (K * N[k].sum())) bound = float(np.percentile(d, 95)); a, b = int(ro.sum()), int(rp.sum()); fp = float(fisher_exact([[b, U - b], [a, U - a]], alternative="greater")[1]) pct = lambda E: float(100 * E.sum() / W) r = dict(utterances=U, words=int(W), wer_original=pct(eo), wer_port=pct(ep), diff=pct(ep) - pct(eo), bound95=bound, runaways_original=a, runaways_port=b, fisher_p=fp, per_seed_original=[round(float(100 * e.sum() / N.sum()), 2) for e in eo], per_seed_port=[round(float(100 * e.sum() / N.sum()), 2) for e in ep], no_errors_original=int((eo == 0).sum()), no_errors_port=int((ep == 0).sum()), over100_original=int((eo / N > 1).sum()), over100_port=int((ep / N > 1).sum()), pass1=bool(bound <= 1.0), pass2=bool(fp >= 0.05)) print(json.dumps(r, indent=1)) if not check: json.dump(r, open(ROOT / "compare4-decoder.int8.json", "w"), indent=1)