155 lines
6.6 KiB
Python
155 lines
6.6 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""错题状态机(初中版)。
|
||
|
||
与小学版最大的不同:**第二档是「做对一道变式」,不是「同题再做对」**。
|
||
同题第二次做对靠的是记忆;变式做对才算迁移。
|
||
|
||
observing ──(24h 内又做对)──► careless(粗心小记,不进队列)
|
||
│
|
||
└─(复核错 / 第 3 次错)──► queued ──对──► recalled ──对──► variant_pending
|
||
▲ │
|
||
│ 变式对
|
||
答错/讲不清 ▼
|
||
└──────────────────────────── to_explain ──讲清──► graduated
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
from datetime import datetime, timedelta
|
||
|
||
from .db import add_days, now, today
|
||
|
||
DAILY_CAP = 6 # 每天只推 6 道
|
||
PUSH_HOUR = 21 # 21:00 推送
|
||
INTERVALS = [1, 3, 7, 15, 30]
|
||
CARELESS_HOURS = 24 # 24 小时内还能做对 → 粗心
|
||
SPRINT_DAYS = 30 # 考前 30 天进入冲刺模式
|
||
|
||
STATUS_ZH = {
|
||
"observing": "观察中",
|
||
"queued": "待复习",
|
||
"recalled": "已对 1 次",
|
||
"variant_pending": "待变式",
|
||
"to_explain": "待讲清",
|
||
"graduated": "已毕业",
|
||
"careless": "粗心小记",
|
||
"backlog": "待补(先不收)",
|
||
}
|
||
|
||
ERROR_ZH = {"execution": "会做但做错了", "careless": "粗心", "gap": "真不会"}
|
||
|
||
|
||
def is_due(m: dict, on: str) -> bool:
|
||
if m["status"] == "variant_pending":
|
||
return True
|
||
if m["status"] not in ("queued", "recalled"):
|
||
return False
|
||
nd = m.get("next_due")
|
||
return bool(nd) and nd <= on
|
||
|
||
|
||
def due_today(mistakes: list[dict], on: str, cap: int = DAILY_CAP):
|
||
"""返回 (复习队列, 待讲清, 待变式)。复习队列按到期日排序并截断到 cap。"""
|
||
queue = [m for m in mistakes if m["status"] in ("queued", "recalled") and is_due(m, on)]
|
||
queue.sort(key=lambda m: (m.get("next_due") or "", m["created_at"]))
|
||
variants = [m for m in mistakes if m["status"] == "variant_pending"]
|
||
explains = [m for m in mistakes if m["status"] == "to_explain"]
|
||
return queue[:cap], explains[:3], variants[:3]
|
||
|
||
|
||
def _hours_since(ts: str) -> float:
|
||
try:
|
||
t = datetime.fromisoformat(ts)
|
||
except Exception: # noqa: BLE001
|
||
return 999.0
|
||
return (datetime.now(t.tzinfo) - t).total_seconds() / 3600.0
|
||
|
||
|
||
def on_import(m: dict | None, repeat: int, error_type: str, on: str | None = None) -> dict:
|
||
"""入库。第 1、2 次只观察,第 3 次才进队列;「真不会」先不收(backlog)。"""
|
||
on = on or today()
|
||
if error_type == "gap":
|
||
return {"status": "backlog", "stage": 0, "interval": 0, "next_due": None}
|
||
if repeat < 3:
|
||
return {"status": "observing", "stage": 0, "interval": 1, "next_due": add_days(1, on)}
|
||
return {"status": "queued", "stage": 0, "interval": 1, "next_due": on}
|
||
|
||
|
||
def on_attempt(m: dict, correct, on: str | None = None, source: str = "daily") -> dict:
|
||
"""一次作答后的状态转移。correct=None(拒答)时不改状态。"""
|
||
on = on or today()
|
||
st = m["status"]
|
||
if correct is None:
|
||
return {"status": st, "refused": True}
|
||
|
||
if st == "variant_pending":
|
||
return on_variant(m, correct, on)
|
||
|
||
if correct:
|
||
if st == "observing":
|
||
if _hours_since(m["created_at"]) <= CARELESS_HOURS:
|
||
return {"status": "careless", "interval": 0, "next_due": None, "stage": 0}
|
||
return {"status": "queued", "stage": 1, "interval": 3, "next_due": add_days(3, on)}
|
||
if st == "queued":
|
||
return {"status": "recalled", "stage": 1, "interval": 3, "next_due": add_days(3, on)}
|
||
if st == "recalled":
|
||
return {"status": "variant_pending", "stage": 2, "interval": 1, "next_due": on}
|
||
if st == "to_explain":
|
||
return {"status": "to_explain", "note": "这题已经做对了,还差讲一遍"}
|
||
return {"status": st}
|
||
|
||
# 答错:间隔归零,明天再来
|
||
return {"status": "queued", "stage": 0, "interval": 1, "next_due": add_days(1, on)}
|
||
|
||
|
||
def on_variant(m: dict, correct, on: str | None = None) -> dict:
|
||
on = on or today()
|
||
if correct is None:
|
||
return {"status": "variant_pending", "refused": True}
|
||
if correct:
|
||
return {"status": "to_explain", "stage": 2, "interval": 1, "next_due": on,
|
||
"variant_passed": 1}
|
||
return {"status": "recalled", "stage": 1, "interval": 1, "next_due": add_days(1, on)}
|
||
|
||
|
||
def on_explain(m: dict, passed: bool, on: str | None = None) -> dict:
|
||
on = on or today()
|
||
if passed:
|
||
return {"status": "graduated", "graduated_at": now(), "next_due": None}
|
||
return {"status": "queued", "stage": 0, "interval": 1, "next_due": add_days(1, on),
|
||
"wrong_code": "E2", "explain_fails": int(m.get("explain_fails") or 0) + 1}
|
||
|
||
|
||
# ------------------------------------------------------------------ 讲清 rubric
|
||
|
||
RUBRIC = {
|
||
"wrong": ["错在", "因为", "把", "忘了", "看错", "漏", "符号", "单位", "公式", "概念", "没注意", "算错"],
|
||
"steps": ["先", "然后", "再", "最后", "第一步", "第二步", "代入", "移项", "合并", "约分", "列式", "画图"],
|
||
"next": ["下次", "以后", "遇到", "记得", "注意", "先看", "先判断"],
|
||
}
|
||
MIN_LEN = 10
|
||
MIN_LEN_THREE_LINES = 8
|
||
|
||
|
||
def score_explain(text: str, channel: str = "ai_voice") -> dict:
|
||
t = (text or "").strip()
|
||
min_len = MIN_LEN_THREE_LINES if channel == "three_lines" else MIN_LEN
|
||
if len(t) < min_len:
|
||
return {"ok": False, "reason": "太短了,再说两句:错在哪、怎么做的、下次怎么办",
|
||
"wrong": False, "steps": False, "next": False}
|
||
hit = {k: any(w in t for w in ws) for k, ws in RUBRIC.items()}
|
||
passed = all(hit.values())
|
||
miss = [zh for k, zh in (("wrong", "错因"), ("steps", "关键步骤"), ("next", "下次怎么做"))
|
||
if not hit[k]]
|
||
return {"ok": passed, "wrong": hit["wrong"], "steps": hit["steps"], "next": hit["next"],
|
||
"reason": "" if passed else "还差:" + "、".join(miss)}
|
||
|
||
|
||
CHANNELS = {
|
||
"ai_voice": "讲给 AI 听(AI 扮演没听懂的同学,追问 2 轮)",
|
||
"voice": "语音条(30–90 秒)",
|
||
"steps": "写步骤(拍照,判完整性)",
|
||
"find_bug": "找错(给一个别人的错解,让你挑错)",
|
||
"make_question": "反向出题(自己出一道同类型题)",
|
||
"three_lines": "三句话填空(错在__/关键是__/下次遇到__)",
|
||
}
|