-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_utils.py
More file actions
71 lines (56 loc) · 1.92 KB
/
Copy pathtext_utils.py
File metadata and controls
71 lines (56 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Strip manuscript markdown so TTS reads prose, not markup."""
from __future__ import annotations
import re
def strip_markdown(text: str) -> str:
"""Light cleanup for pasted chapter fragments."""
if not text or not text.strip():
return ""
t = text.replace("\r\n", "\n")
# YAML frontmatter
if t.startswith("---"):
end = t.find("\n---", 3)
if end != -1:
t = t[end + 4 :]
# ATX headings -> plain line
t = re.sub(r"^#{1,6}\s+", "", t, flags=re.MULTILINE)
# Bold/italic
t = re.sub(r"\*\*([^*]+)\*\*", r"\1", t)
t = re.sub(r"\*([^*]+)\*", r"\1", t)
t = re.sub(r"_([^_]+)_", r"\1", t)
# Links [text](url) -> text
t = re.sub(r"\[([^\]]+)\]\([^)]+\)", r"\1", t)
# Inline code
t = re.sub(r"`([^`]+)`", r"\1", t)
# Blockquotes
t = re.sub(r"^>\s?", "", t, flags=re.MULTILINE)
# Horizontal rules
t = re.sub(r"^[-*_]{3,}\s*$", "", t, flags=re.MULTILINE)
# Collapse excessive blank lines
t = re.sub(r"\n{3,}", "\n\n", t)
return t.strip()
def split_paragraphs(text: str) -> list[str]:
"""Split into speakable chunks (paragraphs)."""
cleaned = strip_markdown(text)
if not cleaned:
return []
parts = re.split(r"\n\s*\n+", cleaned)
chunks: list[str] = []
for part in parts:
part = part.strip()
if not part:
continue
# Very long paragraphs: split on sentence boundaries
if len(part) > 1200:
sentences = re.split(r"(?<=[.!?])\s+", part)
buf = ""
for sent in sentences:
if len(buf) + len(sent) + 1 > 900 and buf:
chunks.append(buf.strip())
buf = sent
else:
buf = f"{buf} {sent}".strip() if buf else sent
if buf.strip():
chunks.append(buf.strip())
else:
chunks.append(part)
return chunks