mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 09:29:39 +08:00
48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
"""Unified grading function for guided learning."""
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
from difflib import SequenceMatcher
|
|
|
|
|
|
def grade_answer(user_answer: str, expected_answer: str, question_type: str = "short") -> bool:
|
|
"""Grade user answer against expected answer.
|
|
|
|
Args:
|
|
user_answer: The user's submitted answer.
|
|
expected_answer: The stored expected answer.
|
|
question_type: One of "choice", "short", "open".
|
|
|
|
Returns:
|
|
True if answer is correct.
|
|
"""
|
|
user = user_answer.strip().lower()
|
|
expected = expected_answer.strip().lower()
|
|
|
|
if not expected:
|
|
return False
|
|
|
|
if question_type == "choice":
|
|
user_norm = user.replace(" ", "")
|
|
expected_norm = expected.replace(" ", "")
|
|
return user_norm == expected_norm
|
|
|
|
if question_type == "short":
|
|
if user == expected:
|
|
return True
|
|
if len(expected) <= 30:
|
|
return SequenceMatcher(None, user, expected).ratio() >= 0.85
|
|
return False
|
|
|
|
if question_type == "open":
|
|
keywords = [k.strip() for k in re.split(r"[,;,;。\n]+", expected) if k.strip()]
|
|
if not keywords:
|
|
return False
|
|
matched = sum(1 for kw in keywords if kw in user)
|
|
return matched / len(keywords) >= 0.6
|
|
|
|
return False
|
|
|
|
|
|
__all__ = ["grade_answer"]
|