Skip to content

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

Fix TTS math prose handling around Windows paths

Bingxi Zhao (Frank)46cd81539246
diff --git a/deeptutor/services/voice/speech_text.py b/deeptutor/services/voice/speech_text.py
index baa061d85..00e61633e 100644
--- a/deeptutor/services/voice/speech_text.py
+++ b/deeptutor/services/voice/speech_text.py
@@ -199,7 +199,6 @@ _SPACING_DROP = {
"notag": "",
"tag": "",
"label": "",
- "quad": " ",
}
_BLACKBOARD = {
@@ -347,6 +346,7 @@ _LOOSE_TEX = re.compile(
)
+ r")(?![a-zA-Z])"
)
+_WINDOWS_PATH = re.compile(r"(?<![A-Za-z0-9])(?:[A-Za-z]:\\|\\\\)[^\s]*")
def _skip_ws(s: str, i: int) -> int:
@@ -705,19 +705,30 @@ def _replace_unicode(text: str) -> str:
def _verbalize_loose_commands(text: str) -> str:
"""Convert leftover TeX commands in prose; never touch bare ``_`` / ``^``.
- Gated on known TeX names so Windows paths like ``C:\\Users`` stay intact.
+ Windows paths are passed through even when nearby prose contains TeX.
"""
if "\\" not in text or not _LOOSE_TEX.search(text):
return text
- out = _replace_blackboard_sets(text)
- out = _until_stable(_unwrap_style_once, out)
- out = _until_stable(_replace_frac_once, out)
- out = _until_stable(_replace_sqrt_once, out)
- out = _until_stable(_replace_binom_once, out)
- out = _until_stable(_replace_limits_once, out)
- out = _replace_named_commands(out, {**_GREEK, **_OPERATORS, **_FUNCTIONS, **_SPACING_DROP})
- out = _drop_unknown_commands(out)
- return out
+
+ def speak(segment: str) -> str:
+ out = _replace_blackboard_sets(segment)
+ out = _until_stable(_unwrap_style_once, out)
+ out = _until_stable(_replace_frac_once, out)
+ out = _until_stable(_replace_sqrt_once, out)
+ out = _until_stable(_replace_binom_once, out)
+ out = _until_stable(_replace_limits_once, out)
+ # Unknown commands in prose may be path segments or product names.
+ # Only an explicit math island is safe to clean up destructively.
+ return _replace_named_commands(out, {**_GREEK, **_OPERATORS, **_FUNCTIONS, **_SPACING_DROP})
+
+ parts: list[str] = []
+ offset = 0
+ for match in _WINDOWS_PATH.finditer(text):
+ parts.append(speak(text[offset : match.start()]))
+ parts.append(match.group())
+ offset = match.end()
+ parts.append(speak(text[offset:]))
+ return "".join(parts)
def _emit_island(inner: str, *, math_speak: bool) -> str:
diff --git a/tests/services/test_voice.py b/tests/services/test_voice.py
index cf3b2d695..8c6908215 100644
--- a/tests/services/test_voice.py
+++ b/tests/services/test_voice.py
@@ -147,9 +147,7 @@ def test_strip_markdown_drops_unpaired_dollars() -> None:
def test_strip_markdown_verbalizes_fractions_roots_and_greek() -> None:
- out = strip_markdown_for_speech(
- r"Take $\frac{1}{2}$ of $\sqrt{x}$ and $\alpha + \beta$."
- )
+ out = strip_markdown_for_speech(r"Take $\frac{1}{2}$ of $\sqrt{x}$ and $\alpha + \beta$.")
assert "$" not in out
assert "\\" not in out
assert "{" not in out and "}" not in out
@@ -193,6 +191,15 @@ def test_strip_markdown_leaves_windows_paths_alone() -> None:
assert r"C:\Users\antmi\notes.md" in out
+def test_strip_markdown_keeps_windows_paths_beside_loose_tex() -> None:
+ out = strip_markdown_for_speech(
+ r"Saved at C:\Users\alpha\notes.md and \\server\share\beta.txt; use \frac{1}{2}."
+ )
+ assert r"C:\Users\alpha\notes.md" in out
+ assert r"\\server\share\beta.txt" in out
+ assert "1 over 2" in out
+
+
def test_strip_markdown_math_speak_off_keeps_inner_tex() -> None:
out = strip_markdown_for_speech(
r"The identity is $E = mc^2$ and $\frac{1}{2}$.",