feat: 整页拍 + 点选出错题(scanflow + 工作台界面)
This commit is contained in:
+69
-6
@@ -22,6 +22,7 @@ import hashlib
|
||||
import hmac
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
@@ -164,6 +165,37 @@ def _post(payload: dict, timeout: int = TIMEOUT) -> dict:
|
||||
_LETTERS = "ABCDEFGH"
|
||||
|
||||
|
||||
_LATEX_MAP = [("\\times", "×"), ("\\div", "÷"), ("\\cdot", "·"),
|
||||
("\\bigcirc", "○"), ("\\leq", "≤"), ("\\geq", "≥"),
|
||||
("\\neq", "≠"), ("\\pm", "±"), ("\\text", ""), ("\\,", " "),
|
||||
("\\quad", " "), ("\\left", ""), ("\\right", "")]
|
||||
|
||||
|
||||
def clean_text(text: str) -> str:
|
||||
"""把 OCR 返回的 LaTeX 片段变成人能读的样子。
|
||||
|
||||
腾讯切题对分数/公式会给 `$\\frac{3}{4}$` 这种,前端直接显示是一串乱码。
|
||||
这里做最小变换:\\frac{a}{b} → a/b,\\times → ×,去掉 $ 与多余空格。
|
||||
"""
|
||||
if not text:
|
||||
return ""
|
||||
t = str(text)
|
||||
for _ in range(6): # 处理嵌套(分子分母里还有 frac)
|
||||
new_t = re.sub(r"\\frac\s*\{([^{}]*)\}\s*\{([^{}]*)\}", r"\1/\2", t)
|
||||
if new_t == t:
|
||||
break
|
||||
t = new_t
|
||||
for a, b in _LATEX_MAP:
|
||||
t = t.replace(a, b)
|
||||
for junk in ("\\begin{matrix}", "\\end{matrix}", "\\begin{array}", "\\end{array}"):
|
||||
t = t.replace(junk, " ")
|
||||
t = re.sub(r"\{?\b(?:begin|end)\{[a-z*]+\}\}?", " ", t)
|
||||
t = t.replace("$", "").replace("\\", "")
|
||||
t = re.sub(r"[ \t]+", " ", t)
|
||||
t = re.sub(r"\n{3,}", "\n\n", t)
|
||||
return t.strip()
|
||||
|
||||
|
||||
def _strip_label(text: str) -> str:
|
||||
"""把选项自带的「A.」「A、」「A.」前缀去掉,只留内容。"""
|
||||
t = (text or "").strip()
|
||||
@@ -201,7 +233,8 @@ def _derive_answer(stem: str):
|
||||
from . import judging
|
||||
|
||||
if _EXPR is None:
|
||||
_EXPR = re.compile(r"(-?\d+(?:\.\d+)?)\s*([+\-*/×÷xX])\s*(-?\d+(?:\.\d+)?)\s*=")
|
||||
_EXPR = re.compile(
|
||||
r"(?<![\d/])(-?\d+(?:\.\d+)?)\s*([+\-×÷xX*])\s*(-?\d+(?:\.\d+)?)\s*=(?![\d/])")
|
||||
m = _EXPR.search(stem or "")
|
||||
if not m:
|
||||
return None
|
||||
@@ -229,6 +262,34 @@ def _guess_type(stem: str, options: list[str], answer: str) -> str:
|
||||
return "manual"
|
||||
|
||||
|
||||
def _bbox(rl: dict) -> dict | None:
|
||||
"""题块的外接框(像素):优先用 rl.Coord,退到题干各段的联合框。"""
|
||||
xs0, ys0, xs1, ys1 = [], [], [], []
|
||||
|
||||
def eat(c):
|
||||
if isinstance(c, list):
|
||||
for one in c:
|
||||
eat(one)
|
||||
return
|
||||
if not isinstance(c, dict):
|
||||
return
|
||||
lt, rb = c.get("LeftTop") or {}, c.get("RightBottom") or {}
|
||||
try:
|
||||
xs0.append(float(lt["X"])); ys0.append(float(lt["Y"]))
|
||||
xs1.append(float(rb["X"])); ys1.append(float(rb["Y"]))
|
||||
except (KeyError, TypeError, ValueError):
|
||||
pass
|
||||
|
||||
eat(rl.get("Coord"))
|
||||
if not xs0:
|
||||
for q in (rl.get("Question") or []):
|
||||
eat((q or {}).get("Coord"))
|
||||
if not xs0:
|
||||
return None
|
||||
x0, y0, x1, y1 = min(xs0), min(ys0), max(xs1), max(ys1)
|
||||
return {"x": int(x0), "y": int(y0), "w": int(max(1, x1 - x0)), "h": int(max(1, y1 - y0))}
|
||||
|
||||
|
||||
def parse(resp: dict) -> list[dict]:
|
||||
"""把 QuestionSplitOCR 的返回拆成我们的题目结构。
|
||||
|
||||
@@ -275,15 +336,15 @@ def parse(resp: dict) -> list[dict]:
|
||||
|
||||
ordered = sorted([r for r in results if isinstance(r, dict)], key=_top_y)
|
||||
for rl in ordered:
|
||||
stem = _join(rl.get("Question"))
|
||||
stem = clean_text(_join(rl.get("Question")))
|
||||
if not stem:
|
||||
continue
|
||||
opts = [_strip_label(o) for o in
|
||||
opts = [_strip_label(clean_text(o)) for o in
|
||||
(str((x or {}).get("Text") or "") for x in (rl.get("Option") or []))]
|
||||
opts = [o for o in opts if o]
|
||||
# 注意:Answer 区拿到的是「卷面上写的东西」。孩子做完的卷子上,那就是他写的答案,
|
||||
# 不是标准答案——拿它判对错会把错的判成对的。所以只当参考存着,不参与判分。
|
||||
page_answer = _pick_answer(_join(rl.get("Answer")))
|
||||
page_answer = clean_text(_pick_answer(_join(rl.get("Answer"))))
|
||||
jt = rl.get("GroupType") if isinstance(rl.get("GroupType"), str) else ""
|
||||
if len(opts) >= 2:
|
||||
jt = "choice"
|
||||
@@ -292,10 +353,12 @@ def parse(resp: dict) -> list[dict]:
|
||||
if jt != "choice":
|
||||
derived = _derive_answer(stem)
|
||||
|
||||
box = _bbox(rl)
|
||||
if derived is not None:
|
||||
# 纯算式:自己算一遍,可以机器判
|
||||
item = {"stem": stem, "judge_type": "calc", "options": None,
|
||||
"answer": derived, "kp": None, "page_answer": page_answer or None}
|
||||
"answer": derived, "kp": None, "page_answer": page_answer or None,
|
||||
"box": box}
|
||||
else:
|
||||
# 其余(选择/填空/问答):没有可信的标准答案 → 孩子自评,
|
||||
# 但把卷面上写的答案留给孩子和家长对照。
|
||||
@@ -303,7 +366,7 @@ def parse(resp: dict) -> list[dict]:
|
||||
item = {"stem": stem, "judge_type": "manual", "options": opts or None,
|
||||
"answer": page_answer or None, "kp": None,
|
||||
"page_answer": page_answer or None,
|
||||
"detected_as": jt}
|
||||
"detected_as": jt, "box": box}
|
||||
out.append(item)
|
||||
return out
|
||||
|
||||
|
||||
Reference in New Issue
Block a user