Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix: preserve mathematical meaning when validating citations

Bingxi Zhao (Frank)6cb3d5367794
diff --git a/deeptutor/learning/semantic_assessment.py b/deeptutor/learning/semantic_assessment.py
index 46dbd653d..f77bac39a 100644
--- a/deeptutor/learning/semantic_assessment.py
+++ b/deeptutor/learning/semantic_assessment.py
@@ -15,19 +15,22 @@ import unicodedata
from pydantic import BaseModel, ConfigDict, Field
-_MARKUP = re.compile(r"<[^>]+>")
-_FORMATTING = re.compile(r"[\s*_`#|]+")
+# Only strip known table layout tags. A generic <...> pattern also consumes
+# mathematical inequalities, and Markdown characters may be math operators.
+_TABLE_MARKUP = re.compile(
+ r"</?(?:table|thead|tbody|tfoot|tr|td|th)(?:\s+[^<>]*)?\s*/?>", re.IGNORECASE
+)
+_CJK_WRAP = re.compile(r"(?<=[\u3400-\u9fff])\s+(?=[\u3400-\u9fff])")
+_FULL_WIDTH_ASCII = str.maketrans({chr(c): chr(c - 0xFEE0) for c in range(0xFF01, 0xFF5F)})
def _comparable(text: str) -> str:
- """Fold formatting-only differences so faithful citations still match.
-
- Verifier quotes are transcribed from page pixels, so line wraps,
- indentation, HTML table markup and full-width/half-width variants differ
- from the parsed text even though the words are identical.
- """
- text = unicodedata.normalize("NFKC", str(text or ""))
- return _FORMATTING.sub("", _MARKUP.sub("", text))
+ """Fold layout differences without erasing mathematical or word boundaries."""
+ # NFC preserves subscripts/superscripts that NFKC would turn into digits.
+ text = unicodedata.normalize("NFC", str(text or "")).translate(_FULL_WIDTH_ASCII)
+ text = _TABLE_MARKUP.sub(" ", text)
+ text = _CJK_WRAP.sub("", text)
+ return " ".join(text.split())
class AssessmentUnavailable(ValueError):
diff --git a/tests/capabilities/test_mastery_semantic_assessment.py b/tests/capabilities/test_mastery_semantic_assessment.py
index 6c437182c..9ee821223 100644
--- a/tests/capabilities/test_mastery_semantic_assessment.py
+++ b/tests/capabilities/test_mastery_semantic_assessment.py
@@ -681,3 +681,34 @@ async def test_scoped_group_clarification_resume_and_exactly_once_credit(service
assert [attempt.question_id for attempt in progress.quiz_attempts] == ["q1", "q2"]
replay, _, replayed = await service.grade_interaction_semantic("p", answer="", question_id="g")
assert replayed and len(replay.learning_evidence) == 2 and len(calls) == 1
+
+
+ "source_text, quote",
+ [
+ ("x_1", "x1"),
+ ("a*b", "ab"),
+ ("x < 0 and y > 1", "x1"),
+ ("x₂", "x2"),
+ ("x²", "x2"),
+ ("not able", "notable"),
+ ("|x|", "||"),
+ ],
+)
+async def test_citation_normalization_does_not_accept_changed_math_or_words(
+ service, monkeypatch, source_text, quote
+):
+ from deeptutor.learning import lesson_evidence
+ from deeptutor.learning import semantic_assessment as semantic
+
+ progress = service.store.load("p")
+ progress.lesson_evidence["kp1"] = {"sources": [{"ref": {}, "text": source_text}]}
+ monkeypatch.setattr(lesson_evidence, "validate_evidence", lambda *args, **kwargs: None)
+
+ async def request(*args, **kwargs):
+ return {"supported": True, "citations": [{"index": 0, "quote": quote}]}
+
+ monkeypatch.setattr(semantic, "_request", request)
+ with pytest.raises(semantic.AssessmentUnavailable, match="absent"):
+ await semantic.bind_question(service.store, progress, leaf())