Files
item_bank/app/services/grading.py
T
2026-08-01 16:50:55 +08:00

25 lines
910 B
Python

"""客观题本地判分:精确比对答案集合。"""
from app.models.question import OBJECTIVE_TYPES, Question
def is_objective(q: Question) -> bool:
return q.type in OBJECTIVE_TYPES
def can_grade_locally(q: Question) -> bool:
"""客观题且录了标准答案,才能本地判。"""
return is_objective(q) and bool(q.correct_answer)
def grade_objective(q: Question, user_answer: list[str]) -> bool | None:
"""选择/判断题:忽略顺序与大小写,比对答案集合。
没录标准答案时返回 None(无法判定),而不是 False —— 否则一条只是
随手记下、还没填答案的笔记会被静默算错,并污染复习清单。
"""
correct = {str(a).strip().upper() for a in (q.correct_answer or [])}
if not correct:
return None
given = {str(a).strip().upper() for a in (user_answer or [])}
return correct == given