Files
cuotibiye/backend/scheduler.py
T

213 lines
9.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""错题状态机与复习调度(SM-2 简化版)。
状态(7 个):
observing 观察中 刚入库,错因暂定 E4;不进队列
queued 待复习 到期了就进今日队列
unstable 会但不稳 今天错过、当天又做对了——不算数,明天从头再来
passed_once 已对 1 次 再做对一次就进「待讲清」
to_explain 待讲清 等孩子讲一遍(不进刷题队列)
graduated 已毕业 讲清通过 → 进「已消灭」档案,永不再推
careless 粗心小记 不进队列,只做 30 秒检查训练
间隔:1 → 3 → 7 → 15 → 30 天;答错或讲不清,间隔归零,明天再来。
"""
from __future__ import annotations
from datetime import date, datetime, timedelta
from .db import add_days, now, today
INTERVALS = [1, 3, 7, 15, 30]
DAILY_CAP = 5
CLEAR_CARELESS_WINDOW_HOURS = 24
STATUS_ZH = {
"observing": "观察中",
"queued": "待复习",
"unstable": "会但不稳",
"passed_once": "已对 1 次",
"variant_pending": "换一道",
"to_explain": "待讲清",
"graduated": "已毕业",
"careless": "粗心小记",
}
# 讲清的三维 rubric:错因 / 关键步骤 / 下次怎么办
RUBRIC_KEYWORDS = {
"wrong_reason": ["粗心", "看错", "抄错", "没看清", "算错", "进位", "退位", "借位",
"公式", "概念", "没懂", "不懂", "不会", "忘了", "背错", "漏", "审题",
"单位", "符号", "方向", "多一步", "少一步"],
"key_steps": ["先", "再", "然后", "接着", "因为", "所以", "等于", "=", "第一步", "第二步"],
"next_time": ["下次", "以后", "再遇到", "要注意", "先看", "先算", "检查", "验算", "记住"],
}
def is_due(mistake: dict, on: str | None = None) -> bool:
"""到期 = 该出现在今天列表里。
观察中的题也会在次日回来「复核」一次:做对就是粗心,做错才确认没懂。
不稳的题同样回到队列:今天错、今天对的,明天必须再做对一次才算过。
"""
if mistake.get("status") not in ("observing", "queued", "unstable", "passed_once",
"variant_pending"):
return False
due = mistake.get("next_due")
return bool(due) and due <= (on or today())
def due_today(mistakes: list[dict], on: str | None = None, cap: int = DAILY_CAP) -> list[dict]:
"""今日队列:到期的最多 5 道,按到期日、入库时间排序(固定 5 道是产品约束)。"""
items = [m for m in mistakes if is_due(m, on)]
items.sort(key=lambda m: (m.get("next_due") or "", m.get("created_at") or ""))
return items[:cap]
def on_attempt(mistake: dict, correct: bool, *, created_at: str | None = None,
same_day_wrong: bool = False, strict: bool = False,
is_variant: bool = False) -> dict:
"""做了一次(重做/口答)。返回要写回 mistakes 的字段。
:param same_day_wrong: 这道题「今天」已经错过一次。看完解析当场再做对,
只能说明「听完会了」,说明不了「记住了」——所以不推进档位,
只记一道「会但不稳」,明天再从头做对一次才算第一次。
:param strict: 严档。打开后,第一次做对不算数——先换一道**没见过**的变式
(`variant_pending`),变式做对才算第一次连对。默认关闭。
:param is_variant: 这一次做的是变式题(不是原题)。
"""
ts = created_at or now()
stage = int(mistake.get("stage") or 0)
wrong_count = int(mistake.get("wrong_count") or 1)
status = mistake.get("status")
if correct:
if status == "observing":
# 24 小时内就能做对 → 确认是粗心,进粗心小记(不进队列)
if _within_hours(mistake.get("created_at"), ts, CLEAR_CARELESS_WINDOW_HOURS):
return {"status": "careless", "wrong_code": "E4", "stage": 0,
"interval_days": 0, "next_due": None}
# 隔了更久还能做对 → 不是没懂,是「会但不熟」,拉开间隔进队列
return {"status": "queued", "stage": 1, "interval_days": 3,
"next_due": add_days(3, base=_date_of(ts)), "wrong_code": "E3"}
if same_day_wrong:
# 当天错了又对:不算数,明天重来;连错两次以上的是「没懂」,不是「不稳」
return {
"status": "unstable",
"stage": 0,
"interval_days": 1,
"next_due": add_days(1, base=_date_of(ts)),
"unstable_count": int(mistake.get("unstable_count") or 0) + 1,
"wrong_code": mistake.get("wrong_code") or "E4",
}
if strict:
if status == "variant_pending":
# 变式做对了:这才是「换一层皮还会」,算第一次连对
stage = max(stage, 1)
interval = INTERVALS[min(stage - 1, len(INTERVALS) - 1)]
return {"status": "passed_once", "stage": stage, "interval_days": interval,
"next_due": add_days(interval, base=_date_of(ts)),
"wrong_code": mistake.get("wrong_code") or "E4"}
if is_variant:
# 变式做错了:说明还是没懂,回队列(间隔归零)
return {"status": "queued", "stage": 0, "interval_days": 1,
"next_due": add_days(1, base=_date_of(ts)),
"wrong_count": wrong_count + 1, "wrong_code": "E2"}
# 原题做对但还没验过变式:今天就给一道,别等到明天
return {"status": "variant_pending", "stage": 0, "interval_days": 0,
"next_due": _date_of(ts), "wrong_code": mistake.get("wrong_code") or "E4"}
stage += 1
interval = INTERVALS[min(stage - 1, len(INTERVALS) - 1)]
if stage >= 2:
return {"status": "to_explain", "stage": stage, "interval_days": 0, "next_due": None}
return {
"status": "passed_once",
"stage": stage,
"interval_days": interval,
"next_due": add_days(interval, base=_date_of(ts)),
"wrong_code": mistake.get("wrong_code") or "E4",
}
# 答错:间隔归零,明天再来;错第 2 次起把错因从「做错」改成「没懂」
wrong_count += 1
return {
"status": "queued",
"stage": 0,
"interval_days": 1,
"next_due": add_days(1, base=_date_of(ts)),
"wrong_count": wrong_count,
"wrong_code": "E2" if wrong_count >= 2 else (mistake.get("wrong_code") or "E4"),
}
def on_explain(mistake: dict, passed: bool, created_at: str | None = None) -> dict:
"""讲清验收结果。通过 → 毕业;不通过 → 回到队列,明天再讲。"""
ts = created_at or now()
if passed:
return {"status": "graduated", "graduated_at": ts, "next_due": None,
"interval_days": 0, "explain_fails": int(mistake.get("explain_fails") or 0)}
fails = int(mistake.get("explain_fails") or 0) + 1
return {"status": "queued", "stage": 0, "next_due": add_days(1, base=_date_of(ts)),
"interval_days": 1, "explain_fails": fails,
# 讲不清 = 没懂,错因降档
"wrong_code": "E2"}
def on_import(existing: dict | None, wrong_code: str = "E4") -> dict:
"""入库规则:同一个孩子同一道题,错到第 3 次才进复习队列。
- 首次:观察中(不进队列);
- 第 2 次:观察中,错因改 E2(不是粗心,是没懂);
- 第 3 次起:进队列,今天就开始。
粗心(E4)单独走:调用方在确认后置为 careless。
"""
if existing is None:
# 次日回来复核一次:做对就是粗心,做错才确认没懂
return {"status": "observing", "wrong_count": 1, "wrong_code": wrong_code,
"stage": 0, "interval_days": 1, "next_due": add_days(1)}
wrong_count = int(existing.get("wrong_count") or 1) + 1
if wrong_count >= 3:
return {"status": "queued", "wrong_count": wrong_count, "wrong_code": "E2",
"stage": 0, "interval_days": 1, "next_due": today()}
return {"status": "observing", "wrong_count": wrong_count,
"wrong_code": "E2" if wrong_count >= 2 else wrong_code,
"stage": 0, "interval_days": 1, "next_due": add_days(1)}
def score_explain(text: str, channel: str = "ai_voice") -> dict:
"""讲清 rubric:错因说对了吗 / 关键步骤说全了吗 / 下次遇到先做什么。"""
s = (text or "").strip()
hit = {}
for dim, words in RUBRIC_KEYWORDS.items():
hit[dim] = any(w in s for w in words)
# 三句话填空只要 3 个空都填了就算过(最轻的通道)
if channel == "three_lines":
hit["key_steps"] = hit["key_steps"] or len(s) >= 8
min_len = 8 if channel == "three_lines" else 10
passed = all(hit.values()) and len(s) >= min_len
return {
"rubric_wrong_reason": int(hit["wrong_reason"]),
"rubric_key_steps": int(hit["key_steps"]),
"rubric_next_time": int(hit["next_time"]),
"passed": passed,
"length": len(s),
}
def _date_of(ts: str) -> str:
try:
return datetime.fromisoformat(ts).date().isoformat()
except ValueError:
return today()
def _within_hours(start: str | None, end: str, hours: int) -> bool:
if not start:
return False
try:
a = datetime.fromisoformat(start)
b = datetime.fromisoformat(end)
except ValueError:
return False
return timedelta(0) <= (b - a) <= timedelta(hours=hours)