解析:竖式区按行拆成小题(split_multi),修括号算式算错的老问题

- tencent_ocr.split_multi:多行纯算式块(竖式/脱式)一行一道、逐道自算;
  只要有一行带汉字就整块放弃,应用题不受影响
- 剥题号正则加 (?!\\d),避免把 1.8+0.6 的 1. 当成题号(会算成 8.6)
- judging.safe_eval 不再走 normalize:strip_punct 会剥掉括号,
  34×(90-46) 会静默变成 34*90-46;等号改为取最后一段
- _tidy:小数题答案给 2.4 而不是 12/5
- 金标 330 道:认对 312->318/330(96.4%),误判 0(95% CI 0-1.19%),漏检 12
- tests/test_split_multi.py 9 例;172 单测 + 4 套冒烟全绿
This commit is contained in:
2026-11-19 21:21:20 +08:00
parent 3387ee73bc
commit 73b75bf8de
14 changed files with 226 additions and 79 deletions
+55 -3
View File
@@ -345,10 +345,60 @@ def split_card(stem: str) -> list[dict]:
val = judging.safe_eval(expr)
if val is None:
continue
subs.append({"stem": f"{expr} =", "answer": judging._fmt(val), "judge_type": "calc"})
ans = _tidy(expr, judging._fmt(val))
subs.append({"stem": f"{expr} =", "answer": ans, "judge_type": "calc"})
return subs if len(subs) >= 3 else []
_LINE_NO = re.compile(r"^\s*(?:第)?[\d一二三四五六七八九十]{1,3}\s*[.、))::](?!\d)\s*")
_HAS_HAN = re.compile(r"[\u4e00-\u9fff]")
def _tidy(expr: str, ans: str) -> str:
"""按算式本身的写法给答案:小数题给小数(`12/5` → `2.4`),分数题给分数。"""
s = (ans or "").strip()
if "." in (expr or "") and "/" in s and len(s.split("/")) == 2:
try:
num, den = s.split("/")
v = float(num) / float(den)
out = f"{v:g}"
return out
except (ValueError, ZeroDivisionError):
return s
return s
def split_multi(stem: str) -> list[dict]:
"""竖式 / 脱式:一块里几行纯算式,一行一道,逐行自己算。
和 split_card 的区别:卡片用「=」认边界,竖式往往**没有等号**(卷面上是让孩子竖着算),
只能按换行拆。为了不把应用题拆坏,这里有一条硬线——**任何一行只要带汉字就整块放弃**。
应用题几乎总会带汉字(「小明有 14 个」),竖式区则是一行一个纯算式。
"""
from . import judging
lines = [x.strip() for x in re.split(r"[\n\r;;]+", stem or "") if x.strip()]
if len(lines) < 2:
return []
subs = []
for raw in lines:
s = _LINE_NO.sub("", raw).strip()
s = s.strip("**").strip()
if not s or _HAS_HAN.search(s):
return [] # 有一行是文字 → 不是竖式区
s = s.replace("×", "*").replace("÷", "/").replace("x", "*").replace("X", "*")
s = s.replace("=", "=").rstrip("=").strip()
if not s or not re.fullmatch(r"[0-9+\-*/().\s]+", s):
return []
if not re.search(r"[+\-*/]", s):
return []
val = judging.safe_eval(s)
if val is None:
return []
subs.append({"stem": f"{s} =", "answer": _tidy(s, judging._fmt(val)), "judge_type": "calc"})
return subs if len(subs) >= 2 else []
def _guess_type(stem: str, options: list[str], answer: str) -> str:
if len(options) >= 2:
return "choice"
@@ -451,19 +501,21 @@ def parse(resp: dict) -> list[dict]:
subs = []
if jt != "choice":
subs = split_card(stem)
if not subs:
subs = split_multi(stem)
if not subs:
derived = _derive_answer(stem)
box = _bbox(rl)
if subs:
# 口算卡:一块里十几道,展开成多个小题(共用同一个框)
# 一块里好几道(口算卡 / 竖式区),展开成多个小题(共用同一个框)
item = {"stem": stem, "judge_type": "card", "options": None,
"answer": None, "kp": None, "page_answer": page_answer or None,
"subs": subs, "box": box}
elif derived is not None:
# 纯算式:自己算一遍,可以机器判
item = {"stem": stem, "judge_type": "calc", "options": None,
"answer": derived, "kp": None, "page_answer": page_answer or None,
"answer": _tidy(stem, derived), "kp": None, "page_answer": page_answer or None,
"box": box}
else:
# 其余(选择/填空/问答):没有可信的标准答案 → 孩子自评,