Skip to content

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

fix: normalize quote matching in mastery source

nandanadileepb824e69bb058
diff --git a/deeptutor/learning/semantic_assessment.py b/deeptutor/learning/semantic_assessment.py
index 04255086e..34e305b2b 100644
--- a/deeptutor/learning/semantic_assessment.py
+++ b/deeptutor/learning/semantic_assessment.py
@@ -9,10 +9,26 @@ from __future__ import annotations
import asyncio
import hashlib
import json
+import re
+import unicodedata
from typing import Literal
from pydantic import BaseModel, ConfigDict, Field
+_MARKUP = re.compile(r"<[^>]+>")
+_FORMATTING = re.compile(r"[\s*_`#|]+")
+
+
+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))
+
class AssessmentUnavailable(ValueError):
pass
@@ -288,7 +304,12 @@ def _attach_support(question, sources, data):
if not isinstance(quote, str) or not quote.strip():
raise AssessmentUnavailable("Invalid question support citation.")
if quote not in sources[index]["text"] and quote not in sources[index]["quote"]:
- raise AssessmentUnavailable("Question support quote is absent from the source.")
+ needle = _comparable(quote)
+ if not needle or (
+ needle not in _comparable(sources[index]["text"])
+ and needle not in _comparable(sources[index]["quote"])
+ ):
+ raise AssessmentUnavailable("Question support quote is absent from the source.")
# A choice label occurring somewhere on a page cannot prove the original
# answer-key relationship. Conservatively label such questions adapted.
exact_original = not question.choice_map and any(
diff --git a/tests/capabilities/test_mastery_semantic_assessment.py b/tests/capabilities/test_mastery_semantic_assessment.py
index 4b90f4143..6c437182c 100644
--- a/tests/capabilities/test_mastery_semantic_assessment.py
+++ b/tests/capabilities/test_mastery_semantic_assessment.py
@@ -410,6 +410,69 @@ async def test_question_support_must_quote_real_evidence(service, monkeypatch):
await semantic.bind_question(service.store, progress, leaf())
+ "source_text, quote",
+ [
+ # A PDF line wrap and indentation split the parsed words.
+ (
+ "…看出如何組織、表達與連結數學想\n 法;…",
+ "…看出如何組織、表達與連結數學想法;…",
+ ),
+ # HTML table markup is present only in the parsed text.
+ (
+ "<tr><td>alpha</td><td>is</td></tr> the first letter",
+ "alpha is the first letter",
+ ),
+ # Full-width parentheses in the parsed text vs. half-width in the quote.
+ (
+ "The set (alpha, beta) is ordered",
+ "The set (alpha, beta) is ordered",
+ ),
+ ],
+)
+async def test_faithful_citation_tolerates_formatting_only_differences(
+ 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}],
+ "provenance": "adapted",
+ }
+
+ monkeypatch.setattr(semantic, "_request", request)
+ question = leaf()
+ await semantic.bind_question(service.store, progress, question)
+ assert question.source_evidence["assessment_contract"]["citations"] == [
+ {"index": 0, "quote": quote}
+ ]
+
+
+async def test_whitespace_only_quote_still_rejected_after_normalization(service, monkeypatch):
+ 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": "alpha and beta"}]}
+ monkeypatch.setattr(lesson_evidence, "validate_evidence", lambda *args, **kwargs: None)
+
+ async def request(*args, **kwargs):
+ return {"supported": True, "citations": [{"index": 0, "quote": " \n\t*_`| "}]}
+
+ monkeypatch.setattr(semantic, "_request", request)
+ with pytest.raises(semantic.AssessmentUnavailable, match="absent"):
+ await semantic.bind_question(service.store, progress, leaf())
+
+
@pytest.mark.asyncio
async def test_group_source_binding_is_one_call_with_individual_contracts(service, monkeypatch):
from deeptutor.learning import lesson_evidence