# gate_ab_score.py —— score the prompt A/B with the SAME criteria code as check_gate6b.py, # but keyed by (arm, variant) instead of a fixed arm order. Adds a per-criterion and # per-probe breakdown so we can see WHICH criterion the policy moved. # 用法: python gate_ab_score.py DIR [OUT.json] import json, os, re, sys, glob, collections D = sys.argv[1] APPL = { "P01_v2_repeat": ("C1", "C2", "C3"), "P02_v3_names": ("C1", "C2", "C3"), "P03_v4_6463": ("C1", "C2", "C3"), "P04_v5_qa": ("C1", "C2", "C3"), "P05_gloss": ("C1", "C2", "C3"), "P06_only63": ("C1",), "P07_only64": ("C1", "C2", "C3"), "P08_brief": ("C1", "C2", "C3"), "P09_annot": ("C1", "C2", "C3"), "P10_interlinear": ("C1", "C2", "C3"), "P11_qa_reward": ("C1", "C3"), "P12_qa_three": ("C1", "C2"), "P13_qa_names": ("C1",), } COMMIT_PROBES = {"P11_qa_reward"} REJECT = ("不是", "而非", "非", "未", "没有", "不", "区别", "误") WRONG_KING = ("楚王", "成王", "纣王", "周王", "商王", "秦王", "齐王", "庄王", "魏王", "韩王", "赵王", "燕王", "鲁王", "晋王") def c1(h): bad = "楚王" in h return (not bad), ("楚王" if bad else ("越王" if "越王" in h else "-")) def c2(h, pid): if pid in ("P04_v5_qa", "P12_qa_three"): ok = "三" in h return ok, ("三 in answer" if ok else "no 三") ok = False for m in re.finditer(r"不[进進]?食|绝食|絕食|不吃|没吃|沒有吃|没有吃", h): seg = h[max(0, m.start() - 30): m.end() + 8] if "三" in seg or "叁" in seg: ok = True return ok, ("三∈window" if ok else "no 三") def _clause_of(h, s, e): left = max(h.rfind(c, 0, s) for c in ",。;!?\n") + 1 rights = [h.find(c, e) for c in ",。;!?\n"] rights = [r for r in rights if r != -1] right = min(rights) if rights else len(h) return h[left:right] def _rejected(h, s, e): cl = _clause_of(h, s, e) return any(r in cl for r in REJECT) def c3(h, pid): reward = bool(re.search(r"(赏|赐|授)[^。;\n]{0,12}(谷|穀|粮|米|饭|食)", h)) or \ bool(re.search(r"(谷|穀|粮)[^。;\n]{0,6}(赏|赐)", h)) commit = bool(re.search(r"备好|准备妥|备妥|备齐|办成|发放|备下|已备|准备完毕|就绪", h)) if pid in COMMIT_PROBES: reject = bool(re.search(r"(而非|不是|并非|不作|不指)[^。;\n]{0,10}(谷物)?成熟", h)) ok = reward and commit and reject tag = ("reward" if reward else "no-reward") + ("/commit" if commit else "/no-commit") + ("/reject" if reject else "/no-reject") return ok, tag mature = False for m in re.finditer(r"(谷|穀|粮食|庄稼|稻|禾)[^。;\n]{0,6}(成熟|长成|熟了|结成)", h): if not _rejected(h, m.start(), m.end()): mature = True ok = reward and not mature return ok, ("reward" if reward else "no-reward") + ("+mature" if mature else "") CHK = {"C1": c1, "C2": c2, "C3": c3} rows_out, summary = [], {} for path in sorted(glob.glob(os.path.join(D, "*__v*.jsonl"))): base = os.path.basename(path)[:-6] arm, variant = base.rsplit("__", 1) rows = [json.loads(l) for l in open(path, encoding="utf-8")] cf = collections.Counter() cn = collections.Counter() clean = 0 per_probe = {} wrong = {} for r in rows: pid, h = r["id"], r["hyp"] crit = APPL.get(pid, ("C1", "C2", "C3")) ok_all = True st = {} for c in crit: ok, ev = CHK[c](h, pid) if c != "C1" else CHK[c](h) st[c] = (ok, ev) cn[c] += 1 if not ok: cf[c] += 1 ok_all = False wk = [w for w in WRONG_KING if w in h] if wk: wrong[pid] = wk if r.get("finish") not in (None, "stop") or len(h.strip()) < 8: ok_all = False clean += 1 if ok_all else 0 per_probe[pid] = {"ok": ok_all, "st": {k: {"ok": v[0], "ev": v[1]} for k, v in st.items()}, "wrong_king": wk} three = cf["C1"] == 0 and cf["C2"] == 0 and cf["C3"] == 0 summary.setdefault(arm, {})[variant] = { "rows": len(rows), "clean": clean, "three_of_three": three, "fail": dict(cf), "n": dict(cn), "wrong_king": wrong, } rows_out.append({"arm": arm, "variant": variant, "clean": clean, "of": len(rows), "three": three, "fail": dict(cf), "per_probe": per_probe}) out = sys.argv[2] if len(sys.argv) > 2 else None if out: json.dump({"summary": summary, "detail": rows_out}, open(out, "w", encoding="utf-8"), ensure_ascii=False, indent=1) ARMS = sorted({r["arm"] for r in rows_out}) VARS = ["v0_raw", "v1_system", "v2_userprefix"] print("%-36s %-14s %5s %6s %8s %s" % ("arm", "variant", "clean", "/13", "3of3", "fails(C1/C2/C3)")) for a in ARMS: for v in VARS: s = summary.get(a, {}).get(v) if not s: print("%-36s %-14s --" % (a, v)); continue f = s["fail"] print("%-36s %-14s %5d %6d %8s %d/%d/%d" % (a, v, s["clean"], s["rows"], s["three_of_three"], f.get("C1", 0), f.get("C2", 0), f.get("C3", 0))) print("\n=== P07 (slip-64-only) C3 per arm/variant ===") for r in rows_out: p = r["per_probe"].get("P07_only64", {}) print("%-36s %-14s C3ok=%-5s ev=%s" % (r["arm"], r["variant"], p.get("st", {}).get("C3", {}).get("ok"), p.get("st", {}).get("C3", {}).get("ev"))) print("\n=== C3 failures by probe ===") cnt = collections.Counter() for r in rows_out: for pid, p in r["per_probe"].items(): if "C3" in p["st"] and not p["st"]["C3"]["ok"]: cnt[(r["variant"], pid)] += 1 for (v, pid), c in sorted(cnt.items()): print(" %-14s %-18s %d arms fail" % (v, pid, c))