Merge pull request #2093 for Markdown selection grounding

This commit is contained in:
Bingxi Zhao (Frank)
2026-10-29 00:59:07 +08:00
2 changed files with 68 additions and 1 deletions

View File

@@ -1529,6 +1529,37 @@ def _latex_selection_is_grounded(source: str, selected: str) -> bool:
return body in normalized_source
def _strip_markdown_markup(text: str) -> str:
"""Strip Markdown structural markup to match browser-rendered selections."""
t = str(text or "")
# link [text](url) -> text
t = re.sub(r"\[([^\]]+)\]\([^)]+\)", r"\1", t)
# image ![alt](url) -> alt
t = re.sub(r"!\[([^\]]*)\]\([^)]+\)", r"\1", t)
# table separator rows |--|--|
t = re.sub(r"\|(?:\s*[:-]+[-| :]*)\|", " ", t)
# table pipes
t = t.replace("|", " ")
# bold/italic markers: ***, **, *, __, _
t = re.sub(r"(\*{1,3}|_{1,3})(.*?)\1", r"\2", t)
# inline code backticks
t = re.sub(r"`+([^`]+)`+", r"\1", t)
# blockquote / headers / list markers at line start or whitespace
t = re.sub(r"(?m)^\s*(?:>\s*|[0-9]+[.)]\s+|[-*+]\s+|#{1,6}\s+)", " ", t)
t = re.sub(r"\s+[0-9]+[.)]\s+", " ", t)
t = re.sub(r"\s+[-*+]\s+", " ", t)
return " ".join(t.split())
def _markdown_selection_is_grounded(source: str, selected: str) -> bool:
"""Whether selected text matches source with Markdown formatting normalized."""
clean_selected = _strip_markdown_markup(selected)
if not clean_selected:
return False
clean_source = _strip_markdown_markup(source)
return clean_selected in clean_source
def _selection_is_grounded(source_text: str, selected_text: str) -> bool:
"""Whether the claimed selection occurs in its containing message."""
source = str(source_text or "")
@@ -1544,7 +1575,9 @@ def _selection_is_grounded(source_text: str, selected_text: str) -> bool:
normalized_selected = " ".join(selected.split())
if normalized_selected and normalized_selected in normalized_source:
return True
return _latex_selection_is_grounded(normalized_source, normalized_selected)
if _latex_selection_is_grounded(normalized_source, normalized_selected):
return True
return _markdown_selection_is_grounded(normalized_source, normalized_selected)
async def _resolve_selection_tutor_context(

View File

@@ -0,0 +1,34 @@
import pytest
from deeptutor.services.session._turn_runtime_shared import _selection_is_grounded
def test_selection_grounding_plain_text():
source = "This is a simple plain text paragraph."
assert _selection_is_grounded(source, "simple plain text")
assert not _selection_is_grounded(source, "fabricated sentence")
def test_selection_grounding_table_row():
source = "| 阈值 | 0.5 |\n| 步长 | 0.01 |"
assert _selection_is_grounded(source, "阈值 0.5")
assert _selection_is_grounded(source, "步长 0.01")
assert not _selection_is_grounded(source, "阈值 1.0")
def test_selection_grounding_list_and_bold():
source = "1. **先建模**\n2. 再绑定\n3. 最后渲染"
assert _selection_is_grounded(source, "先建模 再绑定")
assert _selection_is_grounded(source, "先建模")
assert not _selection_is_grounded(source, "先绑定 再建模")
def test_selection_grounding_blockquote_and_code():
source = "> Note that `_selection_is_grounded` verifies authoritative content."
assert _selection_is_grounded(source, "Note that _selection_is_grounded verifies authoritative content.")
assert not _selection_is_grounded(source, "Note that non_existent_func verifies")
def test_selection_grounding_empty_or_whitespace():
assert not _selection_is_grounded("", "something")
assert not _selection_is_grounded("something", "")