# -*- coding: utf-8 -*- """Rule-layer coverage: 規則樹/資訊句輸出.solve on every GSM8K-train development question (real_set_audit.rows, the same loader 資訊句輸出.measure uses, then 範圍守門.過濾 → 7,066). Run against a source tree (mainline or the Space copy hs/) to compare. GSM8K test is never read. python -B tools/measure_rule_layer.py <手搓算術 root> out.json""" import json import sys import time from collections import Counter from fractions import Fraction from pathlib import Path sys.dont_write_bytecode = True root = Path(sys.argv[1]) sys.path.insert(0, r"H:\Codex用\work\gsm8k-g1m3-wave68-20261125") import real_set_audit as RS # noqa: E402 import evaluate as EV # noqa: E402 sys.path.insert(0, r"H:\Agent共用\專案\手搓算術\語法模組\資訊句") import 範圍守門 # noqa: E402 rows = list(RS.rows("GSM8K-train", EV.TRAIN)) gold = {a: str(c) for a, b, c in rows} qs = 範圍守門.過濾([(a, b) for a, b, c in rows]) sys.path.remove(r"H:\Agent共用\專案\手搓算術\語法模組\資訊句") sys.path.insert(0, str(root / "規則樹")) import 資訊句輸出 as T # noqa: E402 t0 = time.time() cnt, out, ex = Counter(), {}, [] for pid, q in qs: try: r = T.solve(q) v = r["value"] if not r["拒答"] else None except Exception: # noqa: BLE001 v = None cnt["例外"] += 1 cnt["n"] += 1 if v is None: cnt["拒"] += 1 else: ok = Fraction(str(v)) == Fraction(gold[pid].replace(",", "")) cnt["對" if ok else "錯"] += 1 ex.append({"id": pid, "q": q, "value": str(v), "gold": gold[pid], "判": "對" if ok else "錯"}) out[pid] = None if v is None else str(v) print(dict(cnt), round(time.time() - t0, 1), "s", T.__file__) json.dump({"統計": dict(cnt), "讀出": ex, "值": out}, open(sys.argv[2], "w", encoding="utf-8"), ensure_ascii=False)