tts_robust_normalizer_single_script.py
14.2 KB · 347 lines · python Raw
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 """
5 TTS 输入鲁棒性正则化器(非语义 TN)
6
7 目标
8 ----
9 1. 只做“鲁棒性清洗”,不做数字/单位/日期/金额等语义展开。
10 2. 优先保护高风险 token,避免把 `.map`、`app.js.map`、`v2.3.1`、URL、Email、@mention、#hashtag 清坏。
11 3. 保留 `[]` / `{}` 的内容,为后续声音事件、控制标签留接口。
12 4. 对结构性符号做“替换而非删除”:
13 - `【】 / 〖〗 / 『』 / 「」` 在结构位置转成句边界。
14 - `《》` 只在“独立标题/栏目名”场景拆开;嵌入式标题保持不变。
15 - `—— / -- / ——...` 转成句边界。
16 - `-> / => / →` 等流程连接符转成中文逗号,避免 TTS 读入崩溃。
17 5. 对社交平台常见噪声做弱归一化:
18 - `...... / ……` -> `。`
19 - `???!!!` -> `?!`
20 - `!!!` -> `!`
21 6. 空格按脚本类型处理:
22 - 西文片段内部:连续空格压缩为 1 个。
23 - 汉字 / 日文假名片段内部:删除空格。
24 - 汉字 / 日文假名 与“拉丁字母类 token / 受保护 token”相邻:保留或补 1 个空格。
25 - 汉字 / 日文假名 与纯数字相邻:不强行补空格。
26 7. 轻量处理 Markdown 与换行:
27 - `[text](url)` -> `text url`
28 - 去掉标题 `#`、引用 `>`、列表前缀
29 - 换行转句边界 `。`
30
31 非目标
32 ------
33 1. 不决定“应该怎么读”。
34 2. 不删除 `[] / {}` 内容。
35 3. 不做 HTML/SSML/语义标签解释。
36 """
37
38 from __future__ import annotations
39
40 import re
41 import unicodedata
42
43
44 # ---------------------------
45 # 基础常量与正则
46 # ---------------------------
47
48 # 不依赖空格分词的脚本:汉字 + 日文假名
49 _CJK_CHARS = r"\u3400-\u4dbf\u4e00-\u9fff\u3040-\u30ff"
50 _CJK = f"[{_CJK_CHARS}]"
51
52 # 保护占位符
53 _PROT = r"___PROT\d+___"
54
55 # 需要保护的高风险 token
56 _URL_RE = re.compile(r"https?://[^\s\u3000,。!?;、)】》〉」』]+")
57 _EMAIL_RE = re.compile(r"(?<![\w.+-])[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}(?![\w.-])")
58 _MENTION_RE = re.compile(r"(?<![A-Za-z0-9_])@[A-Za-z0-9_]{1,32}")
59 _REDDIT_RE = re.compile(r"(?<![A-Za-z0-9_])(?:u|r)/[A-Za-z0-9_]+")
60 _HASHTAG_RE = re.compile(r"(?<![A-Za-z0-9_])#(?!\s)[^\s#]+")
61
62 # `.map` / `.env` / `.gitignore`
63 _DOT_TOKEN_RE = re.compile(r"(?<![A-Za-z0-9_])\.(?=[A-Za-z0-9._-]*[A-Za-z0-9])[A-Za-z0-9._-]+")
64
65 # `app.js.map` / `index.d.ts` / `v2.3.1` / `foo/bar-baz.py` 等
66 _FILELIKE_RE = re.compile(
67 r"(?<![A-Za-z0-9_])"
68 r"(?=[A-Za-z0-9._/+:-]*[A-Za-z])"
69 r"(?=[A-Za-z0-9._/+:-]*[._/+:-])"
70 r"[A-Za-z0-9](?:[A-Za-z0-9._/+:-]*[A-Za-z0-9])?"
71 r"(?![A-Za-z0-9_])"
72 )
73
74 # 参与“中英混排边界补空格”的 token:必须至少含 1 个拉丁字母,或本身就是受保护 token
75 _LATINISH = rf"(?:{_PROT}|(?=[A-Za-z0-9._/+:-]*[A-Za-z])[A-Za-z0-9][A-Za-z0-9._/+:-]*)"
76
77 # 零宽字符
78 _ZERO_WIDTH_RE = re.compile(r"[\u200b-\u200d\ufeff]")
79
80
81 # ---------------------------
82 # 主函数
83 # ---------------------------
84
85 def normalize_tts_text(text: str) -> str:
86 """对 TTS 输入做鲁棒性正则化。"""
87 text = _base_cleanup(text)
88 text = _normalize_markdown_and_lines(text)
89 text, protected = _protect_spans(text)
90
91 text = _normalize_spaces(text)
92 text = _normalize_structural_punctuation(text)
93 text = _normalize_repeated_punctuation(text)
94 text = _normalize_spaces(text)
95
96 text = _restore_spans(text, protected)
97 return text.strip()
98
99
100 # ---------------------------
101 # 具体规则
102 # ---------------------------
103
104 def _base_cleanup(text: str) -> str:
105 text = text.replace("\r\n", "\n").replace("\r", "\n").replace("\u3000", " ")
106 text = _ZERO_WIDTH_RE.sub("", text)
107
108 cleaned = []
109 for ch in text:
110 cat = unicodedata.category(ch)
111 if ch in "\n\t " or not cat.startswith("C"):
112 cleaned.append(ch)
113 return "".join(cleaned)
114
115
116 def _normalize_markdown_and_lines(text: str) -> str:
117 # Markdown 链接:[text](url) -> text url
118 text = re.sub(r"\[([^\[\]]+?)\]\((https?://[^)\s]+)\)", r"\1 \2", text)
119
120 lines = []
121 for raw in text.splitlines():
122 line = raw.strip()
123 if not line:
124 continue
125
126 line = re.sub(r"^#{1,6}\s+", "", line) # 标题
127 line = re.sub(r"^>\s+", "", line) # 引用
128 line = re.sub(r"^[-*+]\s+", "", line) # 无序列表
129 line = re.sub(r"^\d+[.)]\s+", "", line) # 有序列表
130 lines.append(line)
131
132 return "。".join(lines) if lines else ""
133
134
135 def _protect_spans(text: str) -> tuple[str, list[str]]:
136 protected: list[str] = []
137
138 def repl(match: re.Match[str]) -> str:
139 idx = len(protected)
140 protected.append(match.group(0))
141 return f"___PROT{idx}___"
142
143 for pattern in (
144 _URL_RE,
145 _EMAIL_RE,
146 _MENTION_RE,
147 _REDDIT_RE,
148 _HASHTAG_RE,
149 _DOT_TOKEN_RE,
150 _FILELIKE_RE,
151 ):
152 text = pattern.sub(repl, text)
153
154 return text, protected
155
156
157 def _restore_spans(text: str, protected: list[str]) -> str:
158 for idx, original in enumerate(protected):
159 text = text.replace(f"___PROT{idx}___", original)
160 return text
161
162
163 def _normalize_spaces(text: str) -> str:
164 # 统一空白
165 text = re.sub(r"[ \t\r\f\v]+", " ", text)
166
167 # 汉字 / 日文片段内部:删除空格
168 text = re.sub(rf"({_CJK})\s+(?={_CJK})", r"\1", text)
169
170 # 汉字 / 日文 与纯数字之间:删除空格(不强行保留)
171 text = re.sub(rf"({_CJK})\s+(?=\d)", r"\1", text)
172 text = re.sub(rf"(\d)\s+(?={_CJK})", r"\1", text)
173
174 # 汉字 / 日文 与拉丁字母类 token / protected token 相邻:保留或补 1 个空格
175 text = re.sub(rf"({_CJK})(?=({_LATINISH}))", r"\1 ", text)
176 text = re.sub(rf"(({_LATINISH}))(?={_CJK})", r"\1 ", text)
177
178 # 再压一遍连续空格
179 text = re.sub(r" {2,}", " ", text)
180
181 # 中文标点前后不保留空格
182 text = re.sub(r"\s+([,。!?;:、”’」』】)》])", r"\1", text)
183 text = re.sub(r"([(【「『《“‘])\s+", r"\1", text)
184 text = re.sub(r"([,。!?;:、])\s*", r"\1", text)
185
186 # ASCII 标点前不留空格;后面的英文空格不强改
187 text = re.sub(r"\s+([,.;!?])", r"\1", text)
188
189 return re.sub(r" {2,}", " ", text).strip()
190
191
192 def _normalize_structural_punctuation(text: str) -> str:
193 # 结构性括号:在“结构位置”解包并转成句边界
194 # 连续块要支持收敛,因此做两轮
195 for _ in range(2):
196 text = re.sub(
197 r"(^|[。!?!?;;]\s*)[【〖『「]([^】〗』」]+)[】〗』」]\s*",
198 r"\1\2。",
199 text,
200 )
201
202 # 《》只处理独立标题,不处理嵌入式标题
203 # 例:重磅。《新品发布》——现在开始! -> 重磅。新品发布。现在开始!
204 text = re.sub(
205 r"(^|[。!?!?;;]\s*)《([^》]+)》(?=\s*(?:___PROT\d+___|[—–―-]{2,}|$|[。!?!?;;,,]))",
206 r"\1\2",
207 text,
208 )
209
210 # 流程 / 映射箭头:转成中文逗号,保留链路结构但避免把 `->` 原样喂给 TTS。
211 text = re.sub(
212 r"\s*(?:<[-=]+>|[-=]+>|<[-=]+|[→←↔⇒⇐⇔⟶⟵⟷⟹⟸⟺↦↤↪↩])\s*",
213 ",",
214 text,
215 )
216
217 # 长破折号 / 多连字符:转句边界
218 text = re.sub(r"\s*(?:—|–|―|-){2,}\s*", "。", text)
219
220 return text
221
222
223 def _normalize_repeated_punctuation(text: str) -> str:
224 # 省略号 / 连续句点
225 text = re.sub(r"(?:\.{3,}|…{2,}|……+)", "。", text)
226
227 # 同类重复标点
228 text = re.sub(r"[。.]{2,}", "。", text)
229 text = re.sub(r"[,,]{2,}", ",", text)
230 text = re.sub(r"[!!]{2,}", "!", text)
231 text = re.sub(r"[??]{2,}", "?", text)
232
233 # 混合问叹号:收敛到 ?!
234 def _mixed_qe(match: re.Match[str]) -> str:
235 s = match.group(0)
236 has_q = any(ch in s for ch in "??")
237 has_e = any(ch in s for ch in "!!")
238 if has_q and has_e:
239 return "?!"
240 return "?" if has_q else "!"
241
242 text = re.sub(r"[!?!?]{2,}", _mixed_qe, text)
243 return text
244
245
246 # ---------------------------
247 # 测试
248 # ---------------------------
249
250 TEST_CASES = [
251 # 1) .map / dot-leading token / 文件名 / 版本号
252 (
253 "dot_map_sentence",
254 "2026 年 3 月 31 日,安全研究员 Chaofan Shou (@Fried_rice) 发现 Anthropic 的 npm 包中暴露了 .map 文件,",
255 "2026年3月31日,安全研究员 Chaofan Shou (@Fried_rice) 发现 Anthropic 的 npm 包中暴露了 .map 文件,",
256 ),
257 ("dot_tokens", "别把 .env、.npmrc、.gitignore 提交上去。", "别把 .env、.npmrc、.gitignore 提交上去。"),
258 ("file_names", "请检查 bundle.min.js、package.json 和 processing_moss_tts.py。", "请检查 bundle.min.js、package.json 和 processing_moss_tts.py。"),
259 ("index_d_ts", "index.d.ts 里也有同样的问题。", "index.d.ts 里也有同样的问题。"),
260 ("version_build", "Bug 的讨论可以精确到 v2.3.1 (Build 15)。", "Bug 的讨论可以精确到 v2.3.1 (Build 15)。"),
261 ("version_rc", "3.0.0-rc.1 还不能上生产。", "3.0.0-rc.1 还不能上生产。"),
262 ("jar_name", "fabric-api-0.91.3+1.20.2.jar 需要单独下载。", "fabric-api-0.91.3+1.20.2.jar 需要单独下载。"),
263
264 # 2) URL / Email / mention / hashtag / Reddit
265 ("url", "仓库地址是 https://github.com/instructkr/claude-code", "仓库地址是 https://github.com/instructkr/claude-code"),
266 ("email", "联系邮箱:ops+tts@example.ai", "联系邮箱:ops+tts@example.ai"),
267 ("mention", "@Fried_rice 说这是 source map 暴露。", "@Fried_rice 说这是 source map 暴露。"),
268 ("reddit", "去 r/singularity 看讨论。", "去 r/singularity 看讨论。"),
269 ("hashtag_chain", "#张雪峰#张雪峰[话题]#张雪峰事件", "#张雪峰#张雪峰[话题]#张雪峰事件"),
270 ("mention_hashtag_boundary", "关注@biscuit0228_并转发#thetime_tbs", "关注 @biscuit0228_ 并转发 #thetime_tbs"),
271
272 # 3) bracket / 控制 token:保留,不删除
273 ("speaker_bracket", "[S1]你好。[S2]收到。", "[S1]你好。[S2]收到。"),
274 ("event_bracket", "请模仿 {whisper} 的语气说“别出声”。", "请模仿 {whisper} 的语气说“别出声”。"),
275 ("order_bracket", "订单号:[AB-1234-XYZ]", "订单号:[AB-1234-XYZ]"),
276
277 # 4) 结构性符号:替换成边界,而不是直接删除
278 ("struct_headline", "〖重磅〗《新品发布》——现在开始!", "重磅。新品发布。现在开始!"),
279 ("struct_notice", "【公告】今天 20:00 维护——预计 30 分钟。", "公告。今天20:00维护。预计30分钟。"),
280 ("struct_quote_chain", "『特别提醒』「不要外传」", "特别提醒。不要外传。"),
281 ("flow_arrow_chain", "请求接入 -> 身份与策略判定 -> 域服务处理", "请求接入,身份与策略判定,域服务处理"),
282 ("flow_arrow_no_space", "A->B", "A,B"),
283 ("flow_arrow_unicode", "配置中心→推理编排→运行时执行", "配置中心,推理编排,运行时执行"),
284 (
285 "flow_arrow_maas_example",
286 "MaaS 主链遵循请求接入 -> 身份与策略判定 -> 域服务处理 -> 推理编排 -> 运行时执行的在线数据面结构。Dashboard 不直接承载单次在线推理请求,而是负责统一配置、统一治理、统一运营和统一展示。",
287 "MaaS 主链遵循请求接入,身份与策略判定,域服务处理,推理编排,运行时执行的在线数据面结构。Dashboard 不直接承载单次在线推理请求,而是负责统一配置、统一治理、统一运营和统一展示。",
288 ),
289
290 # 5) 嵌入式标题:保留
291 ("embedded_title", "我喜欢《哈姆雷特》这本书。", "我喜欢《哈姆雷特》这本书。"),
292
293 # 6) 重复标点 / 社交噪声
294 ("noise_qe", "真的假的???!!!", "真的假的?!"),
295 ("noise_ellipsis", "这个包把 app.js.map 也发上去了......太离谱了!!!", "这个包把 app.js.map 也发上去了。太离谱了!"),
296 ("noise_ellipsis_cn", "【系统提示】请模仿{sad}低沉语气,说“今天下雨了……”", "系统提示。请模仿{sad}低沉语气,说“今天下雨了。”"),
297
298 # 7) 空格规则:英文压缩、中文删除、中英混排保留边界
299 ("english_spaces", "This is a test.", "This is a test."),
300 ("chinese_spaces", "这 是 一 段 含有多种空白的文本。", "这是一段含有多种空白的文本。"),
301 ("mixed_spaces_1", "这是Anthropic的npm包", "这是 Anthropic 的 npm 包"),
302 ("mixed_spaces_2", "今天update到v2.3.1了", "今天 update 到 v2.3.1 了"),
303 ("mixed_spaces_3", "处理app.js.map文件", "处理 app.js.map 文件"),
304
305 # 8) Markdown / 列表 / 换行
306 ("markdown_link", "详情见 [release note](https://github.com/example/release)", "详情见 release note https://github.com/example/release"),
307 ("markdown_heading", "# I made a free open source app to help with markdown files", "I made a free open source app to help with markdown files"),
308 ("list_lines", "- 修复 .map 泄露\n- 发布 v2.3.1", "修复 .map 泄露。发布 v2.3.1"),
309 ("numbered_lines", "1. 安装依赖\n2. 运行测试\n3. 发布 v2.3.1", "安装依赖。运行测试。发布 v2.3.1"),
310 ("newlines", "第一行\n第二行\n第三行", "第一行。第二行。第三行"),
311
312 # 9) 零宽字符 / 幂等性
313 ("zero_width_url", "详见 https://x.com/\u200bSafety", "详见 https://x.com/Safety"),
314 ]
315
316
317 def run_tests(verbose: bool = True) -> None:
318 failed = []
319
320 for name, text, expected in TEST_CASES:
321 actual = normalize_tts_text(text)
322 if actual != expected:
323 failed.append((name, text, expected, actual))
324 continue
325
326 # 幂等性:第二次归一化不应继续改动结果
327 second = normalize_tts_text(actual)
328 if second != actual:
329 failed.append((name + "_idempotence", actual, actual, second))
330
331 if failed:
332 lines = ["\nTEST FAILED:\n"]
333 for name, text, expected, actual in failed:
334 lines.append(f"[{name}]")
335 lines.append(f"input : {text}")
336 lines.append(f"expected: {expected}")
337 lines.append(f"actual : {actual}")
338 lines.append("")
339 raise AssertionError("\n".join(lines))
340
341 if verbose:
342 print(f"All {len(TEST_CASES)} tests passed.")
343
344
345 if __name__ == "__main__":
346 run_tests()
347