"""Build the stage-1 text pairs and the stage-2 audio rows from the teacher transcripts. python -m tk.build_data # -> data/text/{train,valid}.jsonl, data/audio-{train,valid}.jsonl, data/stats.json Targets are always made from the TEACHER's own words (never copied from human transcripts, which contain words the teacher heard differently): A DisfluencySpeech: deletion-only target (tk/align.py, ported from LvW-Dictate), skipped if teacher vs transcript_a WER > 0.35 (alignment unreliable) B fluent speech (FLEURS, VoxPopuli, LibriSpeech train-clean-100): target = teacher text minus fillers; skipped if teacher vs reference WER > 0.4 or < 3 words D spoken prompts (macOS say, OASST2 train prompts): target = LvW-Dictate part-D target (cleaned, never answered) Text mode (stage 1) adds LvW-Dictate's text pairs B/C/D (fluent pass-through, synthetic disfluencies, prompts). """ from __future__ import annotations import json import random import sys from collections import Counter import jiwer from tk import paths from tk.align import deletion_target, join_kept, norm_word, FILLERS from tk.wer import read_jsonl, n_basic FLUENT = ["fleurs-en-train", "vox-en-train", "ls-train"] TEXT_PER_PART = {"B": 500, "C": 600, "D": 900} def wer(ref: str, hyp: str) -> float: r, h = n_basic(ref), n_basic(hyp) if not r: return 1.0 if h else 0.0 return jiwer.wer(r, h) def strip_fillers(text: str) -> str: toks = text.split() return join_kept(toks, [norm_word(t) not in FILLERS for t in toks]) if toks else text def joined(s: str, teacher_dir=paths.TEACHER) -> list[dict]: man = {r["id"]: r for r in read_jsonl(paths.MANIFEST / f"{s}.jsonl")} return [{**man[t["id"]], "hyp": t["hyp"]} for t in read_jsonl(teacher_dir / f"{s}.jsonl") if t["id"] in man] def part_a(rows, st: Counter, tag: str) -> list[dict]: out = [] for r in rows: if not r["hyp"].strip(): st[f"A-{tag}-empty-hyp"] += 1 continue if wer(r["transcript_a"], r["hyp"]) > 0.35: st[f"A-{tag}-high-wer"] += 1 continue tgt, s = deletion_target(r["hyp"], r["transcript_a"], r["transcript_c"]) if not tgt.strip(): st[f"A-{tag}-empty-target"] += 1 continue out.append({"id": r["id"], "set": r["set"], "part": "A", "input": r["hyp"], "target": tgt, "transcript_c": r["transcript_c"], "dropped": s["dropped"]}) return out def part_b(rows, st: Counter) -> list[dict]: out = [] for r in rows: hyp = r["hyp"].strip() if len(hyp.split()) < 3: st[f"B-{r['set']}-short"] += 1 continue if r.get("ref") and wer(r["ref"], hyp) > 0.4: st[f"B-{r['set']}-high-wer"] += 1 continue out.append({"id": r["id"], "set": r["set"], "part": "B", "input": hyp, "target": strip_fillers(hyp)}) return out def write(path, rows): path.parent.mkdir(parents=True, exist_ok=True) path.write_text("".join(json.dumps(r, ensure_ascii=False) + "\n" for r in rows)) def main() -> None: st: Counter = Counter() rng = random.Random(1234) lvw = paths.DATA / "lvw" / "pairs" lvw_tr, lvw_va = read_jsonl(lvw / "train.jsonl"), read_jsonl(lvw / "valid.jsonl") a_tr = part_a(joined("dis-train"), st, "train") a_va = part_a(joined("dis-val"), st, "valid") # ---- stage 1 (text) t_tr = [{"id": r["id"], "part": "A", "input": r["input"], "target": r["target"]} for r in a_tr] for part, n in TEXT_PER_PART.items(): # balanced subset of LvW-Dictate's text pairs (M4 time budget) rs = [r for r in lvw_tr if r["part"] == part] random.Random(part).shuffle(rs) t_tr += [{"id": r["id"], "part": part, "input": r["input"], "target": r["target"]} for r in rs[:n]] t_va = [{"id": r["id"], "part": "A", "input": r["input"], "target": r["target"]} for r in a_va] t_va += [{"id": r["id"], "part": r["part"], "input": r["input"], "target": r["target"]} for r in lvw_va if r["part"] in "BCD"] rng.shuffle(t_tr) write(paths.DATA / "text" / "train.jsonl", t_tr) write(paths.DATA / "text" / "valid.jsonl", t_va) st["text-train"], st["text-valid"] = len(t_tr), len(t_va) # ---- stage 2 (audio) au_tr = [{**r, "audio": f"dis-train/{r['id']}.flac"} for r in a_tr] for s in FLUENT: rows = joined(s) st[f"teacher-{s}"] = len(rows) au_tr += [{**r, "audio": f"{s}/{r['id']}.flac"} for r in part_b(rows, st)] for r in read_jsonl(paths.MANIFEST / "say-train.jsonl"): au_tr.append({"id": r["id"], "set": "say-train", "part": "D", "input": r["ref"], "target": r["target"], "audio": f"say-train/{r['id']}.flac"}) au_va = [{**r, "audio": f"dis-val/{r['id']}.flac"} for r in a_va] dev = joined("fleurs-en-dev", paths.DATA / "teacher-q8_0") au_va += [{**r, "audio": f"fleurs-en-dev/{r['id']}.flac"} for r in part_b(dev, st)] rng.shuffle(au_tr) for r in au_tr + au_va: assert (paths.AUDIO / r["audio"]).exists(), r["audio"] # ---- leak checks: no train id or text in any evaluation set eval_ids, eval_txt = set(), set() for s in ["dis-test", "fleurs-en-test", "ls-clean-300", "fleurs-multi", "say-test"]: for r in read_jsonl(paths.MANIFEST / f"{s}.jsonl"): eval_ids.add(r["id"]) eval_txt.add(n_basic(r["ref"])) for r in read_jsonl(lvw / "test.jsonl"): eval_ids.add(r["id"]) eval_txt.add(n_basic(r["input"])) before = len(au_tr), len(t_tr) au_tr = [r for r in au_tr if r["id"] not in eval_ids and n_basic(r["input"]) not in eval_txt] t_tr = [r for r in t_tr if r["id"] not in eval_ids and n_basic(r["input"]) not in eval_txt] st["leak-dropped-audio"], st["leak-dropped-text"] = before[0] - len(au_tr), before[1] - len(t_tr) write(paths.DATA / "text" / "train.jsonl", t_tr) write(paths.DATA / "audio-train.jsonl", au_tr) write(paths.DATA / "audio-valid.jsonl", au_va) st["audio-train"], st["audio-valid"] = len(au_tr), len(au_va) st.update({f"audio-train-{k}": v for k, v in Counter(r["set"] for r in au_tr).items()}) (paths.DATA / "stats.json").write_text(json.dumps(dict(sorted(st.items())), indent=1)) print(json.dumps(dict(sorted(st.items())), indent=1)) if __name__ == "__main__": sys.exit(main())