Files
cuotibiye/tools/check_variants.py
T

67 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""变式自校验抽样:跑 100 道题,看 practice.verify 拦下多少、留下来的有没有算错的。
独立重算不调用 judging:直接按 "a op b" 用 Fraction 算,和变式自己带的答案比。
"""
import json
import re
from fractions import Fraction
import backend.practice as practice
OP = {"+": 1, "-": -1, "×": 0, "x": 0, "X": 0, "*": 0, "÷": 2, "/": 2}
TERM = re.compile(r"(-?\d+(?:\.\d+)?)\s*([+\-×xX*÷/])\s*(-?\d+(?:\.\d+)?)")
def independent(expr):
m = TERM.fullmatch(expr.strip())
if not m:
return None
a, op, b = Fraction(m.group(1)), m.group(2), Fraction(m.group(3))
if op == "+":
return a + b
if op == "-":
return a - b
if op in "×xX*":
return a * b
return None if b == 0 else a / b
def stems():
out = []
p = "data/bench/results.jsonl"
for line in open(p, encoding="utf-8"):
try:
rec = json.loads(line)
except ValueError:
continue
for b in rec.get("blocks") or []:
if b.get("type") == "calc" and b.get("stem") and b.get("derived"):
out.append((b["stem"], b.get("answer") or ""))
return out
def main():
pool = stems()[:100]
kept = rejected = 0
bad = []
for stem, ans in pool:
for it in practice.similar(stem, ans, "calc", "math", 2, n=3):
kept += 1
want = independent(it["stem"])
if want is None:
continue
if abs(float(want) - float(it["answer"])) > 1e-9:
bad.append((it["stem"], it["answer"], float(want)))
# 反向:被 verify 拦掉的不统计(它们没进来)
# 再跑一遍原始生成(不经 verify),看 verify 总共拦了多少
raw = 0
for stem, ans in pool:
raw += len(practice._math_variants(stem, 3)) if "×" in stem or True else 0
print(f"题样 {len(pool)} 道;生成变式 {raw} 道;通过自校验 {kept} 道;独立重算不符 {len(bad)} 道")
for x in bad[:5]:
print(" 不符:", x)
if __name__ == "__main__":
main()