bench: 扩到 230 张 1-6 年级样本;口算卡支持三步连算;补算式级人工金标

This commit is contained in:
2026-11-19 16:09:46 +08:00
parent 35e3f08b2a
commit ba7c35d538
247 changed files with 1910 additions and 5 deletions
+5 -2
View File
@@ -77,6 +77,7 @@ def detect(image_bytes: bytes, filename: str = "page.jpg") -> dict:
"judge_type": q.get("detected_as") or q.get("judge_type"),
"options": q.get("options"),
"page_answer": q.get("page_answer"),
"subs": q.get("subs") or [],
})
if len(blocks) >= MAX_BLOCKS:
break
@@ -135,10 +136,12 @@ def recognize_block(src: Path, block: dict) -> dict:
if _score(q["stem"]) >= _score(page_stem) * 0.9:
return {"stem": q["stem"], "judge_type": q.get("detected_as") or q["judge_type"],
"options": q.get("options"), "answer": q.get("answer"),
"page_answer": q.get("page_answer"), "source": "crop"}
"page_answer": q.get("page_answer"), "subs": q.get("subs") or [],
"source": "crop"}
return {"stem": block["stem"], "judge_type": block.get("judge_type"),
"options": block.get("options"), "answer": block.get("page_answer"),
"page_answer": block.get("page_answer"), "source": "page"}
"page_answer": block.get("page_answer"), "subs": block.get("subs") or [],
"source": "page"}
def _score(stem: str) -> float:
+11 -1
View File
@@ -1716,7 +1716,17 @@ def import_scan_picks(conn, gid: str, cid: str, subject: str, draft_id: str,
if len(chosen) > 20:
raise ApiError(400, "TOO_MANY", "一次最多入库 20 道")
questions = [scanflow.recognize_block(src, b) for b in chosen]
# 口算卡(一块里十几道)先展开成多道,再往库里写
questions = []
for b in chosen:
r = scanflow.recognize_block(src, b)
subs = r.get("subs") or []
if subs:
questions.extend({"stem": x.get("stem") or "", "judge_type": "calc",
"options": None, "answer": x.get("answer"),
"page_answer": None, "source": r.get("source")} for x in subs)
else:
questions.append(r)
pid = new_id("p")
folder = UPLOAD_DIR / cid
+64 -2
View File
@@ -221,6 +221,30 @@ def _pick_answer(text: str) -> str:
_EXPR = None
def _chain_answer(stem: str):
"""三步以上的连算(6+5-10、82-6×7):只要整段是纯算式,就整串求值。
比逐个两数相加强的地方是优先级和结合律都对(先乘除后加减、从左到右)。
"""
from . import judging
t = clean_text(stem or "")
t = re.sub(r"^\s*\d{1,3}\s*[.、.))]\s*", "", t) # 去掉题号
t = re.sub(r"\s+", "", t)
if not t.endswith("=") or t.count("=") != 1:
return None
expr = t[:-1]
if not re.fullmatch(r"[0-9+\-×÷*/().]+", expr or ""):
return None
if len(re.findall(r"[+\-×÷*/]", expr)) < 2: # 两数运算走原来的路
return None
expr = expr.replace("×", "*").replace("÷", "/")
if expr.startswith(("+", "*", "/")) or expr.endswith(("+", "-", "*", "/")):
return None
val = judging.safe_eval(expr)
return None if val is None else judging._fmt(val)
def _derive_answer(stem: str):
"""卷面上没印答案时,纯算式题自己算一遍——口算占大头,不能全推给孩子自评。
@@ -235,6 +259,9 @@ def _derive_answer(stem: str):
if _EXPR is None:
_EXPR = re.compile(
r"(?<![\d/])(-?\d+(?:\.\d+)?)\s*([+\-×÷xX*])\s*(-?\d+(?:\.\d+)?)\s*=(?![\d/])")
chain = _chain_answer(stem)
if chain is not None:
return chain
frac = _frac_chain_answer(stem)
if frac is not None:
return frac
@@ -289,6 +316,33 @@ def _frac_chain_answer(stem: str):
return judging._fmt(val)
_CARD = re.compile(r"(-?\d+(?:\.\d+)?(?:\s*[+\-×÷*/]\s*-?\d+(?:\.\d+)?)+)\s*=")
def split_card(stem: str) -> list[dict]:
"""口算卡:一页十几道「a op b =」。切成小题,每道自己算答案。
只在「至少 3 道、且这些算式占了题干大头」时才切——不然会把一道竖式题
(中间有多步等号)误当成卡片。
"""
from . import judging
hits = list(_CARD.finditer(stem or ""))
if len(hits) < 3:
return []
covered = sum(m.end() - m.start() for m in hits)
if covered < len(stem) * 0.45:
return []
subs = []
for m in hits:
expr = m.group(1)
val = judging.safe_eval(expr)
if val is None:
continue
subs.append({"stem": f"{expr} =", "answer": judging._fmt(val), "judge_type": "calc"})
return subs if len(subs) >= 3 else []
def _guess_type(stem: str, options: list[str], answer: str) -> str:
if len(options) >= 2:
return "choice"
@@ -388,11 +442,19 @@ def parse(resp: dict) -> list[dict]:
jt = "choice"
derived = None
subs = []
if jt != "choice":
derived = _derive_answer(stem)
subs = split_card(stem)
if not subs:
derived = _derive_answer(stem)
box = _bbox(rl)
if derived is not None:
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,