feat(quality): clean LaTeX accents/ties + word-boundary truncation in section titles

Polish _clean_title for R-F: strip accent/escape macros (backslash-quote o to o, backslash-amp), convert tilde ties to spaces, and truncate at a word boundary so stored section titles read cleanly (mobius invariance, colin de verdiere). Tests added; ruff + mypy clean.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Tarik Moussa
2026-06-18 09:17:43 +02:00
parent c70ff9aa1e
commit 1a879d4827
2 changed files with 22 additions and 5 deletions

View File

@@ -113,14 +113,18 @@ def classify_section(text: str) -> str:
def _clean_title(title: str) -> str:
"""Normalize a LaTeX ``\\section`` title for use as a section label.
Strips LaTeX commands (``\\emph`` …) and ``{}$``, drops leading section
numbering (``3.2 ``), collapses whitespace, lower-cases, and truncates to
60 chars so the stored ``section`` value is a clean, comparable string.
Strips LaTeX word-commands (``\\emph`` …), accent/escape macros (``\\"o`` →
``o``, ``\\&``), ``~`` ties and ``{}$``, drops leading section numbering
(``3.2 ``), collapses whitespace, lower-cases, and truncates to 60 chars at a
word boundary so the stored ``section`` value is a clean, comparable string.
"""
t = re.sub(r"\\[a-zA-Z]+\*?", " ", title) # LaTeX commands
t = title.replace("~", " ") # LaTeX non-breaking tie → space
t = re.sub(r"\\[a-zA-Z]+\*?", " ", t) # word-commands: \emph, \mathcal …
t = re.sub(r"\\[^a-zA-Z]", "", t) # accent/escape macros: \"o → o, \', \`, \&
t = re.sub(r"[{}$]", "", t) # braces / math delimiters
t = re.sub(r"^\s*\d+(?:\.\d+)*\.?\s*", "", t) # leading numbering "3.2 "
return " ".join(t.split()).strip().lower()[:60]
t = " ".join(t.split()).strip().lower()
return t[:60].rsplit(" ", 1)[0] if len(t) > 60 else t
def section_label(title: str | None, content: str) -> str: