chore: 首次纳入版本控制(审查前基线,v1.2.0)
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""举一反三:给一道错题生成同类变式题。
|
||||
|
||||
小学版的变式只做两件事,刻意不碰「大模型乱出题」:
|
||||
1) 算式题:换数字、不换结构(同一错因,换一层皮),答案用 judging.safe_eval 现算;
|
||||
2) 其他题型:从同年级同科目的固定题库里取结构相同的题。
|
||||
这样每道变式都有确定答案,孩子做完能立刻判对错。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
from . import content, judging
|
||||
|
||||
NUM = re.compile(r"\d+(?:\.\d+)?")
|
||||
|
||||
# 换数字的几种「手感」:加大、减小、进位、退位
|
||||
DELTAS = [(2, 1), (3, -2), (-4, 5), (5, 4), (-6, 3), (8, -5)]
|
||||
|
||||
|
||||
def _rebuild(expr: str, nums: list[str]) -> str:
|
||||
out = []
|
||||
last = 0
|
||||
for m, v in zip(NUM.finditer(expr), nums):
|
||||
out.append(expr[last:m.start()])
|
||||
out.append(v)
|
||||
last = m.end()
|
||||
out.append(expr[last:])
|
||||
return "".join(out)
|
||||
|
||||
|
||||
def _fmt(x: float) -> str:
|
||||
"""把 10.0 写成 10、把 4.50 写成 4.5——小学生看到的答案不该拖一串零。"""
|
||||
s = f"{x:.4f}".rstrip("0").rstrip(".")
|
||||
return s or "0"
|
||||
|
||||
|
||||
def _place(num: str) -> int:
|
||||
return len(num.split(".")[1]) if "." in num else 0
|
||||
|
||||
|
||||
def _shift(num: str, delta: float) -> str | None:
|
||||
"""按原来那一位小数平移:2.5 +2 → 4.5;整数还是整数。"""
|
||||
places = _place(num)
|
||||
val = round(float(num) + delta, places)
|
||||
if val <= 0:
|
||||
return None
|
||||
return _fmt(float(int(val)) if places == 0 else val)
|
||||
|
||||
|
||||
def _math_variants(stem: str, n: int) -> list[dict]:
|
||||
"""换数字不换结构——**小数位要跟着走**。
|
||||
|
||||
原来的实现 int(float("2.5")) → 2,于是「2.5 × 4」的变式变成「4 × 5」,
|
||||
等于换了道题。现在按原数的小数位平移:2.5 × 4 → 3.5 × 4 / 4.5 × 4 / 1.5 × 4。
|
||||
"""
|
||||
head = stem.split("=")[0]
|
||||
nums = NUM.findall(head)
|
||||
if len(nums) < 2:
|
||||
return []
|
||||
m_op = re.search(r"[+\-×xX*÷/]", head)
|
||||
op = m_op.group(0) if m_op else "+"
|
||||
# 乘除只换被乘数/被除数:换掉乘数会连「一位小数 × 一位整数」的结构也变了
|
||||
change_both = op in "+-"
|
||||
results: list[dict] = []
|
||||
seen = {stem.strip()}
|
||||
for d1, d2 in DELTAS:
|
||||
if len(results) >= n:
|
||||
break
|
||||
a = _shift(nums[0], d1)
|
||||
b = _shift(nums[1], d2) if (change_both and d2) else nums[1]
|
||||
if a is None or b is None or a == nums[0]:
|
||||
continue
|
||||
new_nums = [a, b] + nums[2:]
|
||||
expr = _rebuild(head, new_nums).strip()
|
||||
val = judging.safe_eval(expr)
|
||||
if val is None or val < 0:
|
||||
continue
|
||||
# 除法换数字很容易算出 2.2857……,小学生看到会懵;只留两位以内
|
||||
if _place(_fmt(val)) > 2:
|
||||
continue
|
||||
new_stem = f"{expr} ="
|
||||
if new_stem in seen:
|
||||
continue
|
||||
seen.add(new_stem)
|
||||
results.append({"stem": new_stem, "answer": _fmt(val), "judge_type": "calc",
|
||||
"options": None, "tag": "同类变式 · 换数字"})
|
||||
return results
|
||||
|
||||
|
||||
def similar(stem: str, answer: str, judge_type: str, subject: str, grade: int, n: int = 3) -> list[dict]:
|
||||
"""返回 n 道同类题;每道都带确定答案。"""
|
||||
items: list[dict] = []
|
||||
if judge_type == "calc":
|
||||
items = _math_variants(stem, n)
|
||||
if len(items) < n:
|
||||
pool = [q for q in content.papers_for(grade or 2, subject)
|
||||
if q["stem"].strip() != stem.strip() and (q["judge_type"] == judge_type or judge_type == "calc")]
|
||||
pool.sort(key=lambda q: abs(len(q["stem"]) - len(stem)))
|
||||
for q in pool:
|
||||
if len(items) >= n:
|
||||
break
|
||||
if any(i["stem"] == q["stem"] for i in items):
|
||||
continue
|
||||
items.append({"stem": q["stem"], "answer": q["answer"], "judge_type": q["judge_type"],
|
||||
"options": q.get("options"), "tag": "同知识点变式"})
|
||||
for it in items:
|
||||
it.setdefault("subject", subject)
|
||||
it.setdefault("tag", "同类变式")
|
||||
return items[:n]
|
||||
Reference in New Issue
Block a user