- QuestionSplitOCR 的 Answer 区是「卷面上写的」,可能正是孩子写的答案: 只能当参考(page_answer),不能拿去判对错 - 纯算式(a op b =)自己算一遍再判 calc,口算仍然机器判 - 题干按 Coord.LeftTop.Y 排序,题号自上而下 - tencent_ocr 加命令行自检:python -m backend.tencent_ocr <图片> - 单测 115 例 + 冒烟 39 项全过
184 lines
6.7 KiB
Python
184 lines
6.7 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""腾讯云「试卷切题」的解析与调用(全部打桩,不花钱、不联网)。"""
|
||
import unittest
|
||
from pathlib import Path
|
||
|
||
import sys
|
||
|
||
ROOT = Path(__file__).resolve().parent.parent
|
||
sys.path.insert(0, str(ROOT))
|
||
|
||
from tests._tmpdir import workdir # noqa: E402
|
||
from backend import tencent_ocr as tq # noqa: E402
|
||
|
||
WORK = workdir("tencent")
|
||
|
||
|
||
def fake_resp(items):
|
||
"""造一份 QuestionSplitOCR 的 Response。"""
|
||
return {"QuestionInfo": [{"ResultList": items}], "RequestId": "req-1"}
|
||
|
||
|
||
class ParseTest(unittest.TestCase):
|
||
def test_choice_question(self):
|
||
resp = fake_resp([{
|
||
"Question": [{"Index": 0, "Text": "下面加点字读音正确的是( )"}],
|
||
"Option": [
|
||
{"Index": 1, "Text": "A.快乐(lè)"},
|
||
{"Index": 2, "Text": "B.音乐(yuè)"},
|
||
{"Index": 3, "Text": "C.长大(cháng)"},
|
||
{"Index": 4, "Text": "D.银行(háng)"},
|
||
],
|
||
"Answer": [{"Text": "答案:B"}],
|
||
"GroupType": "multiple-choice",
|
||
}])
|
||
qs = tq.parse(resp)
|
||
self.assertEqual(len(qs), 1)
|
||
q = qs[0]
|
||
# 卷面上的答案可能是孩子写的,不能拿来判分 → manual + 参考答案
|
||
self.assertEqual(q["judge_type"], "manual")
|
||
self.assertEqual(q["detected_as"], "choice")
|
||
self.assertEqual(q["page_answer"], "B")
|
||
self.assertEqual(q["answer"], "B")
|
||
self.assertEqual(q["options"], ["快乐(lè)", "音乐(yuè)", "长大(cháng)", "银行(háng)"])
|
||
self.assertIn("读音", q["stem"])
|
||
|
||
def test_calc_question(self):
|
||
resp = fake_resp([{
|
||
"Question": [{"Index": 0, "Text": "1.计算:36 + 47 ="}],
|
||
"Answer": [{"Text": "83"}],
|
||
}])
|
||
q = tq.parse(resp)[0]
|
||
self.assertEqual(q["judge_type"], "calc")
|
||
self.assertEqual(q["answer"], "83")
|
||
self.assertIsNone(q["options"])
|
||
|
||
def test_manual_when_no_answer(self):
|
||
resp = fake_resp([{
|
||
"Question": [{"Index": 0, "Text": "4.用一个成语形容秋天的景色。"}],
|
||
"Answer": [],
|
||
}])
|
||
q = tq.parse(resp)[0]
|
||
self.assertEqual(q["judge_type"], "manual")
|
||
self.assertIsNone(q["answer"])
|
||
self.assertIsNone(q["page_answer"])
|
||
|
||
def test_multi_part_stem_joined_in_index_order(self):
|
||
resp = fake_resp([{
|
||
"Question": [{"Index": 1, "Text": "(2)再算十位。"},
|
||
{"Index": 0, "Text": "(1)先算个位;"}],
|
||
"Answer": [{"Text": "答案:4"}],
|
||
}])
|
||
q = tq.parse(resp)[0]
|
||
self.assertTrue(q["stem"].startswith("(1)先算个位;"), q["stem"])
|
||
self.assertEqual(q["answer"], "4")
|
||
|
||
def test_choice_answer_outside_options_dropped(self):
|
||
resp = fake_resp([{
|
||
"Question": [{"Index": 0, "Text": "选一个"}],
|
||
"Option": [{"Text": "A.甲"}, {"Text": "B.乙"}],
|
||
"Answer": [{"Text": "答案:D"}],
|
||
}])
|
||
q = tq.parse(resp)[0]
|
||
self.assertEqual(q["judge_type"], "manual")
|
||
self.assertEqual(q["detected_as"], "choice")
|
||
self.assertEqual(q["page_answer"], "D") # 原样保留,但不参与判分
|
||
|
||
def test_calc_derived_when_no_answer_printed(self):
|
||
resp = fake_resp([{"Question": [{"Text": "1.计算:2.5x4=()"}], "Answer": []}])
|
||
q = tq.parse(resp)[0]
|
||
self.assertEqual(q["judge_type"], "calc")
|
||
self.assertEqual(q["answer"], "10") # 自己算出来的,可以机器判
|
||
self.assertIsNone(q["page_answer"])
|
||
|
||
def test_empty_or_bad_payload(self):
|
||
self.assertEqual(tq.parse({}), [])
|
||
self.assertEqual(tq.parse({"QuestionInfo": None}), [])
|
||
self.assertEqual(tq.parse({"QuestionInfo": [{"ResultList": [{"Question": []}]}]}), [])
|
||
|
||
|
||
class SignTest(unittest.TestCase):
|
||
def setUp(self):
|
||
self._id, self._key = tq.secret_id, tq.secret_key
|
||
tq.secret_id = lambda: "AKIDEXAMPLE"
|
||
tq.secret_key = lambda: "SECRETEXAMPLE"
|
||
|
||
def tearDown(self):
|
||
tq.secret_id, tq.secret_key = self._id, self._key
|
||
|
||
def test_signature_shape_and_determinism(self):
|
||
body = b'{"ImageBase64":"abc"}'
|
||
a = tq._sign(body, 1700000000)
|
||
b = tq._sign(body, 1700000000)
|
||
self.assertEqual(a, b)
|
||
self.assertTrue(a.startswith("TC3-HMAC-SHA256 Credential=AKIDEXAMPLE/2023-11-14/ocr/tc3_request"))
|
||
self.assertIn("SignedHeaders=content-type;host", a)
|
||
self.assertIn("Signature=", a)
|
||
|
||
def test_signature_changes_with_body(self):
|
||
self.assertNotEqual(tq._sign(b'{"a":1}', 1700000000), tq._sign(b'{"a":2}', 1700000000))
|
||
|
||
|
||
class SplitTest(unittest.TestCase):
|
||
def setUp(self):
|
||
self._post = tq._post
|
||
self._avail = tq.available
|
||
tq.available = lambda: True
|
||
|
||
def tearDown(self):
|
||
tq._post = self._post
|
||
tq.available = self._avail
|
||
|
||
def test_split_ok(self):
|
||
tq._post = lambda payload, timeout=25: {
|
||
"ok": True, "request_id": "r1",
|
||
"data": fake_resp([{
|
||
"Question": [{"Text": "2.计算:72 - 38 ="}],
|
||
"Answer": [{"Text": "34"}],
|
||
}]),
|
||
}
|
||
r = tq.split_questions(b"img")
|
||
self.assertTrue(r["ok"])
|
||
self.assertEqual(r["questions"][0]["answer"], "34")
|
||
self.assertEqual(r["request_id"], "r1")
|
||
|
||
def test_split_api_error(self):
|
||
tq._post = lambda payload, timeout=25: {
|
||
"ok": False, "code": "TENCENT_FailedOperation.UnOpenError",
|
||
"message": "服务未开通", "request_id": "r2",
|
||
}
|
||
r = tq.split_questions(b"img")
|
||
self.assertFalse(r["ok"])
|
||
self.assertEqual(r["code"], "TENCENT_FailedOperation.UnOpenError")
|
||
self.assertEqual(r["questions"], [])
|
||
|
||
def test_empty_image_short_circuits(self):
|
||
called = []
|
||
tq._post = lambda payload, timeout=25: called.append(1) or {"ok": True, "data": {}}
|
||
r = tq.split_questions(b"")
|
||
self.assertFalse(r["ok"])
|
||
self.assertEqual(r["code"], "EMPTY_IMAGE")
|
||
self.assertEqual(called, [])
|
||
|
||
def test_post_without_credentials(self):
|
||
tq.available = lambda: False
|
||
r = tq._post({"x": 1})
|
||
self.assertFalse(r["ok"])
|
||
self.assertEqual(r["code"], "TENCENT_NOT_CONFIGURED")
|
||
|
||
def test_post_network_error_is_swallowed(self):
|
||
import urllib.request
|
||
|
||
self._open = urllib.request.urlopen
|
||
urllib.request.urlopen = lambda *a, **k: (_ for _ in ()).throw(OSError("no route"))
|
||
try:
|
||
r = tq._post({"x": 1})
|
||
finally:
|
||
urllib.request.urlopen = self._open
|
||
self.assertFalse(r["ok"])
|
||
self.assertEqual(r["code"], "TENCENT_UNREACHABLE")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
unittest.main()
|