#!/usr/bin/env python3"""Score a Meetings transcript against a synthetic meeting's truth. score.py <truth.json> <transcript.json> (or an RTTM truth for speakers only)Prints JSON: * wer: word error rate of the whole meeting's words in time order, and per language (truth turns of that language against the lines overlapping them); * der: diarization error rate (missed speech + false alarm + speaker confusion over reference speech, 10 ms frames, no collar, speakers mapped one to one; scripts/voice/measure_speakers.py's scoring); * language: the share of lines whose language is the truth's at their middle, and, for every switch of language in the truth, what the line nearest it did (kept apart, or ran across it, and in which language)."""import jsonimport sysfrom pathlib importPathROOT = Path(__file__).resolve().parent.parent.parentsys.path.insert(0, str(ROOT / "scripts" / "voice"))def words(text: str, lang: str = "en"):import measurereturn measure.units(measure.normalise(text), lang)def wer(ref_text: str, hyp_text: str, lang: str = "en"):import measure r, h = words(ref_text, lang), words(hyp_text, lang)return measure.edit_distance(r, h), len(r)def der(turns, lines, duration_s):import measure_speakers as ms ref = [(t["start_ms"] / 1000, t["end_ms"] / 1000, t["speaker"]) for t in turns] hyp = [(l["start_ms"] / 1000, l["end_ms"] / 1000, l["speaker"]) for l in lines]return ms.der(ref, hyp, duration_s)def main(): truth_path, hyp_path = Path(sys.argv[1]), Path(sys.argv[2]) hyp = json.loads(hyp_path.read_text()) transcript = hyp.get("transcript", hyp) lines = transcript["lines"]if truth_path.suffix == ".rttm":import measure_speakers as ms ref = ms.rttm(truth_path) dur = max(b for _, b, _ in ref) + 1 print(json.dumps({"der": der([{"start_ms": a * 1000, "end_ms": b * 1000, "speaker": s} for a, b, s in ref], lines, dur)}, indent=1))return truth = json.loads(truth_path.read_text()) turns = truth["turns"] out = {"meeting": truth["name"], "audio_s": truth["duration_ms"] / 1000, "lines": len(lines), "speakers": {"truth": len({t["speaker"] for t in turns}), "heard": len(transcript.get("speakers", []))}} e, n = wer(" ".join(t["text"] for t in turns), " ".join(l["text"] for l in lines)) out["wer"] = {"all": round(e / max(n, 1), 4), "words": n}for lang in sorted({t["language"] for t in turns}): mine = [t for t in turns if t["language"] == lang] hyp_text = " ".join(l["text"] for l in lines if any(min(l["end_ms"], t["end_ms"]) - max(l["start_ms"], t["start_ms"]) > (l["end_ms"] - l["start_ms"]) / 2for t in mine)) e, n = wer(" ".join(t["text"] for t in mine), hyp_text, lang) out["wer"][lang] = round(e / max(n, 1), 4) out["der"] = der(turns, lines, truth["duration_ms"] / 1000 + 1)def truth_lang(ms): best = min(turns, key=lambda t: 0if t["start_ms"] <= ms <= t["end_ms"] else min(abs(ms - t["start_ms"]), abs(ms - t["end_ms"])))return best["language"] right = sum(1for l in lines if l.get("language") == truth_lang((l["start_ms"] + l["end_ms"]) // 2)) out["language"] = {"lines_right": round(right / max(len(lines), 1), 4)} switches = []for a, b in zip(turns, turns[1:]):if a["language"] != b["language"]: at = (a["end_ms"] + b["start_ms"]) // 2 across = [l for l in lines if l["start_ms"] < a["end_ms"] - 200and l["end_ms"] > b["start_ms"] + 200] after = next((l for l in lines if l["start_ms"] >= at - 300), None) switches.append({"at_ms": at, "from": a["language"], "to": b["language"],"a_line_runs_across": bool(across),"next_line_language": after.get("language") if after elseNone, }) out["language"]["switches"] = len(switches) out["language"]["switch_followed"] = round(sum(1for s in switches if s["next_line_language"] == s["to"] andnot s["a_line_runs_across"]) / max(len(switches), 1), 4) out["language"]["runs_across"] = sum(1for s in switches if s["a_line_runs_across"]) out["took"] = hyp.get("took") print(json.dumps(out, indent=1, ensure_ascii=False))if __name__ == "__main__": main()