supermemo-toolkit 0.2.12__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- supermemo_toolkit/Icon.png +0 -0
- supermemo_toolkit/__init__.py +1 -0
- supermemo_toolkit/ansm_conv/__init__.py +0 -0
- supermemo_toolkit/ansm_conv/anki2sm.py +109 -0
- supermemo_toolkit/ansm_conv/ankinet.py +43 -0
- supermemo_toolkit/ansm_conv/sm2anki.py +138 -0
- supermemo_toolkit/autotts/__init__.py +0 -0
- supermemo_toolkit/autotts/autotts.py +326 -0
- supermemo_toolkit/autotts/replace.py +1024 -0
- supermemo_toolkit/autotts/switcher.py +335 -0
- supermemo_toolkit/autotts/threader.py +129 -0
- supermemo_toolkit/autotts/ui.py +156 -0
- supermemo_toolkit/database/__init__.py +0 -0
- supermemo_toolkit/database/control_click.py +575 -0
- supermemo_toolkit/database/element_no.py +446 -0
- supermemo_toolkit/database/force_reload.py +373 -0
- supermemo_toolkit/database/htmtext.py +193 -0
- supermemo_toolkit/database/origin_window.py +260 -0
- supermemo_toolkit/database/registry.py +380 -0
- supermemo_toolkit/database/trace.py +283 -0
- supermemo_toolkit/database/trigger_menu.py +186 -0
- supermemo_toolkit/database/window_focus.py +397 -0
- supermemo_toolkit/epub2sm/__init__.py +0 -0
- supermemo_toolkit/epub2sm/epub_convert.py +613 -0
- supermemo_toolkit/epub2sm/format_ascii.py +89 -0
- supermemo_toolkit/epub2sm/toc_check.py +68 -0
- supermemo_toolkit/epub2sm/toc_orgnize.py +200 -0
- supermemo_toolkit/latex2img/__init__.py +0 -0
- supermemo_toolkit/latex2img/bridge.py +46 -0
- supermemo_toolkit/latex2img/formula_to_png.py +174 -0
- supermemo_toolkit/latex2img/renderer.py +364 -0
- supermemo_toolkit/launcher.py +1799 -0
- supermemo_toolkit/pathpix/LICENSE.txt +21 -0
- supermemo_toolkit/pathpix/__init__.py +0 -0
- supermemo_toolkit/pathpix/epub_imgfit.py +254 -0
- supermemo_toolkit/pathpix/gui.py +271 -0
- supermemo_toolkit/pathpix/im_sort_out.py +1134 -0
- supermemo_toolkit/smtk.py +1212 -0
- supermemo_toolkit/smtk_shell.py +491 -0
- supermemo_toolkit/time_rewind/__init__.py +0 -0
- supermemo_toolkit/time_rewind/time_rewind.py +2794 -0
- supermemo_toolkit/title_xref/__init__.py +0 -0
- supermemo_toolkit/title_xref/complete_title.py +209 -0
- supermemo_toolkit/utilscripts/__init__.py +0 -0
- supermemo_toolkit/utilscripts/config.py +200 -0
- supermemo_toolkit/utilscripts/connector.py +336 -0
- supermemo_toolkit/utilscripts/launcher.vbs +838 -0
- supermemo_toolkit/utilscripts/query_menu.py +475 -0
- supermemo_toolkit/utilscripts/shortcut.py +136 -0
- supermemo_toolkit/utilscripts/ulils.py +108 -0
- supermemo_toolkit-0.2.12.dist-info/METADATA +259 -0
- supermemo_toolkit-0.2.12.dist-info/RECORD +55 -0
- supermemo_toolkit-0.2.12.dist-info/WHEEL +4 -0
- supermemo_toolkit-0.2.12.dist-info/entry_points.txt +5 -0
- supermemo_toolkit-0.2.12.dist-info/licenses/LICENSE +339 -0
|
@@ -0,0 +1,1024 @@
|
|
|
1
|
+
import html
|
|
2
|
+
import unicodedata
|
|
3
|
+
|
|
4
|
+
import regex
|
|
5
|
+
|
|
6
|
+
# ===== 纯文本 / 符号替换(无需正则,固定字面量) =====
|
|
7
|
+
replace_list: list[tuple[str, str]] = [
|
|
8
|
+
# 图片/插图占位符
|
|
9
|
+
("[...]", ",什么,"),
|
|
10
|
+
# 注释/脚注标记(固定写法)
|
|
11
|
+
("[footnote]", ",注,"),
|
|
12
|
+
("[/footnote]", ""),
|
|
13
|
+
("[note]", ",注释,"),
|
|
14
|
+
("[/note]", ""),
|
|
15
|
+
# 引用标记
|
|
16
|
+
("[quote]", ",引用,"),
|
|
17
|
+
("[/quote]", ""),
|
|
18
|
+
("[blockquote]", ",引用,"),
|
|
19
|
+
("[/blockquote]", ""),
|
|
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
|
+
# 花朵 / 装饰符(TTS 无意义,直接删)
|
|
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
|
+
("↑", ""),
|
|
72
|
+
("↓", ""),
|
|
73
|
+
("⇒", " 因此 "),
|
|
74
|
+
("∀", ""),
|
|
75
|
+
("∃", ""),
|
|
76
|
+
("∈", ""),
|
|
77
|
+
("⊂", ""),
|
|
78
|
+
("∞", " 无穷 "),
|
|
79
|
+
("±", " 正负 "),
|
|
80
|
+
("×", " 乘 "),
|
|
81
|
+
("÷", " 除 "),
|
|
82
|
+
("≤", " 小于等于 "),
|
|
83
|
+
("≥", " 大于等于 "),
|
|
84
|
+
("≠", " 不等于 "),
|
|
85
|
+
("∫", ""),
|
|
86
|
+
("∑", ""),
|
|
87
|
+
("∏", ""),
|
|
88
|
+
# 希腊字母
|
|
89
|
+
("α", "阿尔法"),
|
|
90
|
+
("β", "贝塔"),
|
|
91
|
+
("γ", "伽马"),
|
|
92
|
+
("δ", "德尔塔"),
|
|
93
|
+
("π", "派"),
|
|
94
|
+
("φ", "斐"),
|
|
95
|
+
("ω", "欧米伽"),
|
|
96
|
+
("θ", "西塔"),
|
|
97
|
+
("λ", "拉姆达"),
|
|
98
|
+
("μ", "缪"),
|
|
99
|
+
("σ", "西格玛"),
|
|
100
|
+
("τ", "陶"),
|
|
101
|
+
("η", "伊塔"),
|
|
102
|
+
("ξ", "克西"),
|
|
103
|
+
("ζ", "泽塔"),
|
|
104
|
+
("ν", "纽"),
|
|
105
|
+
("ρ", "罗"),
|
|
106
|
+
("υ", "宇普西龙"),
|
|
107
|
+
("κ", "卡帕"),
|
|
108
|
+
("χ", "希"),
|
|
109
|
+
("ψ", "普西"),
|
|
110
|
+
("ε", "艾普西龙"),
|
|
111
|
+
# 版权/前言信息(中文,直接替换的短词已移出行首锚定规则)
|
|
112
|
+
("©", ""),
|
|
113
|
+
("®", ""),
|
|
114
|
+
("™", ""),
|
|
115
|
+
("§", ""),
|
|
116
|
+
("¶", ""),
|
|
117
|
+
("´", ""),
|
|
118
|
+
("¨", ""),
|
|
119
|
+
("ˆ", ""),
|
|
120
|
+
("˜", ""),
|
|
121
|
+
("•", ""),
|
|
122
|
+
("·", ""),
|
|
123
|
+
# 版权/前言信息(英文长词,正文误命中概率低,保持直接替换)
|
|
124
|
+
("Copyright", ""),
|
|
125
|
+
("All rights reserved", ""),
|
|
126
|
+
("LCCN", ""),
|
|
127
|
+
("DOI", ""),
|
|
128
|
+
("Library of Congress", ""),
|
|
129
|
+
("British Library", ""),
|
|
130
|
+
("Cataloguing in Publication", ""),
|
|
131
|
+
("Printed in", ""),
|
|
132
|
+
("Published by", ""),
|
|
133
|
+
("First edition", ""),
|
|
134
|
+
("Reprint", ""),
|
|
135
|
+
(" Paperback", ""),
|
|
136
|
+
("Hardcover", ""),
|
|
137
|
+
# 目录相关(英文长词/全大写,直接替换)
|
|
138
|
+
("CONTENTS", ""),
|
|
139
|
+
("Table of Contents", ""),
|
|
140
|
+
("CONTINUE", ""),
|
|
141
|
+
("NEXT", ""),
|
|
142
|
+
("PREVIOUS", ""),
|
|
143
|
+
("BACK", ""),
|
|
144
|
+
("HOME", ""),
|
|
145
|
+
# 页眉页脚(全大写/首字母大写,直接替换)
|
|
146
|
+
("HEADER", ""),
|
|
147
|
+
("FOOTER", ""),
|
|
148
|
+
# 广告/推荐(英文长词,直接替换)
|
|
149
|
+
("ADVERTISEMENT", ""),
|
|
150
|
+
("Also by", ""),
|
|
151
|
+
("Excerpt from", ""),
|
|
152
|
+
("Look inside", ""),
|
|
153
|
+
("Preview", ""),
|
|
154
|
+
# HTML 实体(固定小写字面量,直接替换即可)
|
|
155
|
+
("&", "和"),
|
|
156
|
+
("<", ""),
|
|
157
|
+
(">", ""),
|
|
158
|
+
(""", ""),
|
|
159
|
+
("'", ""),
|
|
160
|
+
(" ", ""),
|
|
161
|
+
("—", "——"),
|
|
162
|
+
("–", "–"),
|
|
163
|
+
("…", "…"),
|
|
164
|
+
("«", ""),
|
|
165
|
+
("»", ""),
|
|
166
|
+
("‘", ""),
|
|
167
|
+
("’", ""),
|
|
168
|
+
("“", ""),
|
|
169
|
+
("&rdquo", ""),
|
|
170
|
+
("•", ""),
|
|
171
|
+
("·", ""),
|
|
172
|
+
("©", ""),
|
|
173
|
+
("®", ""),
|
|
174
|
+
("™", ""),
|
|
175
|
+
("€", "欧元"),
|
|
176
|
+
("£", "英镑"),
|
|
177
|
+
("¥", "日元"),
|
|
178
|
+
("¢", "分"),
|
|
179
|
+
("§", ""),
|
|
180
|
+
("¶", ""),
|
|
181
|
+
("°", "度"),
|
|
182
|
+
("´", ""),
|
|
183
|
+
("¨", ""),
|
|
184
|
+
("ˆ", ""),
|
|
185
|
+
("˜", ""),
|
|
186
|
+
(" ", ""),
|
|
187
|
+
(" ", ""),
|
|
188
|
+
(" ", ""),
|
|
189
|
+
# ---- 分数符号 ----
|
|
190
|
+
("½", "二分之一"),
|
|
191
|
+
("⅓", "三分之一"),
|
|
192
|
+
("⅔", "三分之二"),
|
|
193
|
+
("¼", "四分之一"),
|
|
194
|
+
("¾", "四分之三"),
|
|
195
|
+
("⅛", "八分之一"),
|
|
196
|
+
("⅜", "八分之三"),
|
|
197
|
+
("⅝", "八分之五"),
|
|
198
|
+
("⅞", "八分之七"),
|
|
199
|
+
# ---- 温度符号(中文行兜底;英文行由函数优先处理) ----
|
|
200
|
+
("°C", "摄氏度"),
|
|
201
|
+
("°F", "华氏度"),
|
|
202
|
+
("℃", "摄氏度"),
|
|
203
|
+
("℉", "华氏度"),
|
|
204
|
+
("°", "度"),
|
|
205
|
+
# ---- 拉丁缩写展开 ----
|
|
206
|
+
("e.g.", "for example"),
|
|
207
|
+
("i.e.", "that is"),
|
|
208
|
+
("etc.", "et cetera"),
|
|
209
|
+
("vs.", "versus"),
|
|
210
|
+
("cf.", "compare"),
|
|
211
|
+
("et al.", "and others"),
|
|
212
|
+
("viz.", "namely"),
|
|
213
|
+
("ca.", "circa"),
|
|
214
|
+
("vol.", "volume"),
|
|
215
|
+
("Dr.", "Doctor"),
|
|
216
|
+
("Mr.", "Mister"),
|
|
217
|
+
("Mrs.", "Missus"),
|
|
218
|
+
("Ms.", "Miss"),
|
|
219
|
+
("Jr.", "Junior"),
|
|
220
|
+
("Sr.", "Senior"),
|
|
221
|
+
# ---- 常见度量衡缩写(英文) ----
|
|
222
|
+
(" km", " kilometers"),
|
|
223
|
+
(" cm", " centimeters"),
|
|
224
|
+
(" mm", " millimeters"),
|
|
225
|
+
(" µm", " micrometers"),
|
|
226
|
+
(" nm", " nanometers"),
|
|
227
|
+
(" kg", " kilograms"),
|
|
228
|
+
(" mg", " milligrams"),
|
|
229
|
+
(" ml", " milliliters"),
|
|
230
|
+
(" mL", " milliliters"),
|
|
231
|
+
(" Hz", " hertz"),
|
|
232
|
+
(" kHz", " kilohertz"),
|
|
233
|
+
(" MHz", " megahertz"),
|
|
234
|
+
(" GB", " gigabytes"),
|
|
235
|
+
(" MB", " megabytes"),
|
|
236
|
+
(" KB", " kilobytes"),
|
|
237
|
+
(" TB", " terabytes"),
|
|
238
|
+
]
|
|
239
|
+
|
|
240
|
+
# ===== 正则替换(仅标签类 / HTML,含字母的忽略大小写) =====
|
|
241
|
+
regex_replace_list: list[tuple[regex.Pattern[str], str]] = [
|
|
242
|
+
# ===== HTML 文档头 / 注释 / 脚本整段删除(最优先) =====
|
|
243
|
+
(regex.compile(r"(?is)<!DOCTYPE[^>]*>"), ""),
|
|
244
|
+
(regex.compile(r"(?is)<!--.*?-->"), ""),
|
|
245
|
+
(regex.compile(r"(?is)<head\b[^>]*>.*?</head>"), ""),
|
|
246
|
+
(regex.compile(r"(?is)<title\b[^>]*>.*?</title>"), ""),
|
|
247
|
+
(regex.compile(r"(?is)<meta\b[^>]*/?>"), ""),
|
|
248
|
+
(regex.compile(r"(?is)<style\b[^>]*>.*?</style>"), ""),
|
|
249
|
+
(regex.compile(r"(?is)<script\b[^>]*>.*?</script>"), ""),
|
|
250
|
+
(regex.compile(r"(?is)<link\b[^>]*/?>"), ""),
|
|
251
|
+
# ===== 中文页眉/目录/广告等短词:只删除独立成行的,避免误伤正文 =====
|
|
252
|
+
(regex.compile(r"(?m)^\s*版权信息\s*$"), ""),
|
|
253
|
+
(regex.compile(r"(?m)^\s*版权所有\s*$"), ""),
|
|
254
|
+
(regex.compile(r"(?m)^\s*目\s*录\s*$"), ""),
|
|
255
|
+
(regex.compile(r"(?m)^\s*广告\s*$"), ""),
|
|
256
|
+
(regex.compile(r"(?m)^\s*推荐\s*$"), ""),
|
|
257
|
+
(regex.compile(r"(?m)^\s*节选自\s*$"), ""),
|
|
258
|
+
# ===== 英文页码/页眉:只删除独立成行的 =====
|
|
259
|
+
(regex.compile(r"(?im)^\s*Page\s+\d+\s*$"), ""),
|
|
260
|
+
(regex.compile(r"(?im)^\s*Page\s*$"), ""),
|
|
261
|
+
# ===== 拼音注音删除(命中条件:括号内含带声调的拉丁元音) =====
|
|
262
|
+
(
|
|
263
|
+
regex.compile(
|
|
264
|
+
r"[((][^(())\n]*[āáǎàēéěèīíǐìōóǒòūúǔùǖǘǚǜêńňǹ][^(())\n]*[))]"
|
|
265
|
+
),
|
|
266
|
+
"",
|
|
267
|
+
),
|
|
268
|
+
# ===== 行内格式标签(strong/b/i/em/u/font/small/big/mark/del/ins/strike) =====
|
|
269
|
+
(regex.compile(r"(?i)</?strong\b[^>]*>"), ""),
|
|
270
|
+
(regex.compile(r"(?i)</?b\b[^>]*>"), ""),
|
|
271
|
+
(regex.compile(r"(?i)</?i\b[^>]*>"), ""),
|
|
272
|
+
(regex.compile(r"(?i)</?em\b[^>]*>"), ""),
|
|
273
|
+
(regex.compile(r"(?i)</?u\b[^>]*>"), ""),
|
|
274
|
+
(regex.compile(r"(?i)</?font\b[^>]*>"), ""),
|
|
275
|
+
(regex.compile(r"(?i)</?small\b[^>]*>"), ""),
|
|
276
|
+
(regex.compile(r"(?i)</?big\b[^>]*>"), ""),
|
|
277
|
+
(regex.compile(r"(?i)</?mark\b[^>]*>"), ""),
|
|
278
|
+
(regex.compile(r"(?i)</?del\b[^>]*>"), ""),
|
|
279
|
+
(regex.compile(r"(?i)</?ins\b[^>]*>"), ""),
|
|
280
|
+
(regex.compile(r"(?i)</?strike\b[^>]*>"), ""),
|
|
281
|
+
# ===== 空段落处理(必须在 <p> / <br/> 规则之前,避免产生 "。," 噪音) =====
|
|
282
|
+
(regex.compile(r"(?i)<p\b[^>]*>\s*<br\s*/?>\s*</p>"), "。"),
|
|
283
|
+
# ===== 带圈/带括号/带点数字(注释标记 ① ② ③ …)=====
|
|
284
|
+
(regex.compile(r"[\u2460-\u24FF\u2776-\u2793\u3251-\u325F\u32B1-\u32BF]"), ""),
|
|
285
|
+
# ===== 方括号标签 =====
|
|
286
|
+
(regex.compile(r"(?i)\[img\]"), ",插图,"),
|
|
287
|
+
(regex.compile(r"(?i)\[/img\]"), ""),
|
|
288
|
+
(regex.compile(r"(?i)\[image\]"), ",图片,"),
|
|
289
|
+
(regex.compile(r"(?i)\[/image\]"), ""),
|
|
290
|
+
(regex.compile(r"(?i)\[figure\]"), ",插图,"),
|
|
291
|
+
(regex.compile(r"(?i)\[/figure\]"), ""),
|
|
292
|
+
(regex.compile(r"(?i)\[picture\]"), ",图片,"),
|
|
293
|
+
(regex.compile(r"(?i)\[/picture\]"), ""),
|
|
294
|
+
# 超链接
|
|
295
|
+
(regex.compile(r"(?i)\[url\]"), ""),
|
|
296
|
+
(regex.compile(r"(?i)\[/url\]"), ""),
|
|
297
|
+
(regex.compile(r"(?i)\[link\]"), ""),
|
|
298
|
+
(regex.compile(r"(?i)\[/link\]"), ""),
|
|
299
|
+
# 脚注(固定写法 + 变体)
|
|
300
|
+
(regex.compile(r"(?i)\[fn\]"), ""),
|
|
301
|
+
(regex.compile(r"(?i)\[/fn\]"), ""),
|
|
302
|
+
(regex.compile(r"\[\^[^\]]*\]"), ""), # [^1]、[^abc]、[^*]
|
|
303
|
+
(regex.compile(r"\^\[[^\]]*\]"), ""), # ^[1]、^[abc]
|
|
304
|
+
(regex.compile(r"\[\d+\]"), ""), # [1]、[2]、[123] 上标引用
|
|
305
|
+
# 元数据/标题
|
|
306
|
+
(regex.compile(r"(?i)\[title\]"), ""),
|
|
307
|
+
(regex.compile(r"(?i)\[/title\]"), ""),
|
|
308
|
+
(regex.compile(r"(?i)\[author\]"), ""),
|
|
309
|
+
(regex.compile(r"(?i)\[/author\]"), ""),
|
|
310
|
+
(regex.compile(r"(?i)\[date\]"), ""),
|
|
311
|
+
(regex.compile(r"(?i)\[/date\]"), ""),
|
|
312
|
+
(regex.compile(r"(?i)\[publisher\]"), ""),
|
|
313
|
+
(regex.compile(r"(?i)\[/publisher\]"), ""),
|
|
314
|
+
# 目录/导航/分页
|
|
315
|
+
(regex.compile(r"(?i)\[toc\]"), ""),
|
|
316
|
+
(regex.compile(r"(?i)\[/toc\]"), ""),
|
|
317
|
+
(regex.compile(r"(?i)\[nav\]"), ""),
|
|
318
|
+
(regex.compile(r"(?i)\[/nav\]"), ""),
|
|
319
|
+
(regex.compile(r"(?i)\[pagebreak\]"), "。"),
|
|
320
|
+
(regex.compile(r"(?i)\[/pagebreak\]"), ""),
|
|
321
|
+
(regex.compile(r"(?i)\[pb\]"), ""),
|
|
322
|
+
(regex.compile(r"(?i)\[/pb\]"), ""),
|
|
323
|
+
(regex.compile(r"(?i)\[page\]"), ""),
|
|
324
|
+
(regex.compile(r"(?i)\[/page\]"), ""),
|
|
325
|
+
# 章节/段落
|
|
326
|
+
(regex.compile(r"(?i)\[chapter\]"), ""),
|
|
327
|
+
(regex.compile(r"(?i)\[/chapter\]"), ""),
|
|
328
|
+
(regex.compile(r"(?i)\[section\]"), ""),
|
|
329
|
+
(regex.compile(r"(?i)\[/section\]"), ""),
|
|
330
|
+
(regex.compile(r"(?i)\[paragraph\]"), ""),
|
|
331
|
+
(regex.compile(r"(?i)\[/paragraph\]"), ""),
|
|
332
|
+
# 公式/代码
|
|
333
|
+
(regex.compile(r"(?i)\[math\]"), ""),
|
|
334
|
+
(regex.compile(r"(?i)\[/math\]"), ""),
|
|
335
|
+
(regex.compile(r"(?i)\[latex\]"), ""),
|
|
336
|
+
(regex.compile(r"(?i)\[/latex\]"), ""),
|
|
337
|
+
(regex.compile(r"(?i)\[formula\]"), ""),
|
|
338
|
+
(regex.compile(r"(?i)\[/formula\]"), ""),
|
|
339
|
+
(regex.compile(r"(?i)\[code\]"), ",代码,"),
|
|
340
|
+
(regex.compile(r"(?i)\[/code\]"), ""),
|
|
341
|
+
(regex.compile(r"(?i)\[sourcecode\]"), ""),
|
|
342
|
+
(regex.compile(r"(?i)\[/sourcecode\]"), ""),
|
|
343
|
+
# 表格/列表
|
|
344
|
+
(regex.compile(r"(?i)\[table\]"), ",表格,"),
|
|
345
|
+
(regex.compile(r"(?i)\[/table\]"), ""),
|
|
346
|
+
# SuperMemo 内部标记(全部统一归零)
|
|
347
|
+
(regex.compile(r"(?i)\[clip\]"), ""),
|
|
348
|
+
(regex.compile(r"(?i)\[/clip\]"), ""),
|
|
349
|
+
(regex.compile(r"(?i)\[sound\]"), ""),
|
|
350
|
+
(regex.compile(r"(?i)\[/sound\]"), ""),
|
|
351
|
+
(regex.compile(r"(?i)\[video\]"), ""),
|
|
352
|
+
(regex.compile(r"(?i)\[/video\]"), ""),
|
|
353
|
+
(regex.compile(r"(?i)\[email\]"), ""),
|
|
354
|
+
(regex.compile(r"(?i)\[/email\]"), ""),
|
|
355
|
+
(regex.compile(r"(?i)\[attachment\]"), ""),
|
|
356
|
+
(regex.compile(r"(?i)\[/attachment\]"), ""),
|
|
357
|
+
(regex.compile(r"(?i)\[file\]"), ""),
|
|
358
|
+
(regex.compile(r"(?i)\[/file\]"), ""),
|
|
359
|
+
(regex.compile(r"(?i)\[object\]"), ""),
|
|
360
|
+
(regex.compile(r"(?i)\[/object\]"), ""),
|
|
361
|
+
(regex.compile(r"(?i)\[meta\]"), ""),
|
|
362
|
+
(regex.compile(r"(?i)\[/meta\]"), ""),
|
|
363
|
+
(regex.compile(r"(?i)\[info\]"), ""),
|
|
364
|
+
(regex.compile(r"(?i)\[/info\]"), ""),
|
|
365
|
+
(regex.compile(r"(?i)\[key\]"), ""),
|
|
366
|
+
(regex.compile(r"(?i)\[/key\]"), ""),
|
|
367
|
+
(regex.compile(r"(?i)\[answer\]"), ""),
|
|
368
|
+
(regex.compile(r"(?i)\[/answer\]"), ""),
|
|
369
|
+
(regex.compile(r"(?i)\[question\]"), ""),
|
|
370
|
+
(regex.compile(r"(?i)\[/question\]"), ""),
|
|
371
|
+
(regex.compile(r"(?i)\[template\]"), ""),
|
|
372
|
+
(regex.compile(r"(?i)\[/template\]"), ""),
|
|
373
|
+
(regex.compile(r"(?i)\[element\]"), ""),
|
|
374
|
+
(regex.compile(r"(?i)\[/element\]"), ""),
|
|
375
|
+
(regex.compile(r"(?i)\[component\]"), ""),
|
|
376
|
+
(regex.compile(r"(?i)\[/component\]"), ""),
|
|
377
|
+
(regex.compile(r"(?i)\[condition\]"), ""),
|
|
378
|
+
(regex.compile(r"(?i)\[/condition\]"), ""),
|
|
379
|
+
(regex.compile(r"(?i)\[if\]"), ""),
|
|
380
|
+
(regex.compile(r"(?i)\[/if\]"), ""),
|
|
381
|
+
(regex.compile(r"(?i)\[then\]"), ""),
|
|
382
|
+
(regex.compile(r"(?i)\[/then\]"), ""),
|
|
383
|
+
(regex.compile(r"(?i)\[else\]"), ""),
|
|
384
|
+
(regex.compile(r"(?i)\[/else\]"), ""),
|
|
385
|
+
(regex.compile(r"(?i)\[for\]"), ""),
|
|
386
|
+
(regex.compile(r"(?i)\[/for\]"), ""),
|
|
387
|
+
(regex.compile(r"(?i)\[while\]"), ""),
|
|
388
|
+
(regex.compile(r"(?i)\[/while\]"), ""),
|
|
389
|
+
(regex.compile(r"(?i)\[do\]"), ""),
|
|
390
|
+
(regex.compile(r"(?i)\[/do\]"), ""),
|
|
391
|
+
(regex.compile(r"(?i)\[end\]"), ""),
|
|
392
|
+
(regex.compile(r"(?i)\[/end\]"), ""),
|
|
393
|
+
(regex.compile(r"(?i)\[macro\]"), ""),
|
|
394
|
+
(regex.compile(r"(?i)\[/macro\]"), ""),
|
|
395
|
+
(regex.compile(r"(?i)\[execute\]"), ""),
|
|
396
|
+
(regex.compile(r"(?i)\[/execute\]"), ""),
|
|
397
|
+
(regex.compile(r"(?i)\[script\]"), ""),
|
|
398
|
+
(regex.compile(r"(?i)\[/script\]"), ""),
|
|
399
|
+
(regex.compile(r"(?i)\[javascript\]"), ""),
|
|
400
|
+
(regex.compile(r"(?i)\[/javascript\]"), ""),
|
|
401
|
+
(regex.compile(r"(?i)\[vbscript\]"), ""),
|
|
402
|
+
(regex.compile(r"(?i)\[/vbscript\]"), ""),
|
|
403
|
+
(regex.compile(r"(?i)\[activex\]"), ""),
|
|
404
|
+
(regex.compile(r"(?i)\[/activex\]"), ""),
|
|
405
|
+
(regex.compile(r"(?i)\[ocx\]"), ""),
|
|
406
|
+
(regex.compile(r"(?i)\[/ocx\]"), ""),
|
|
407
|
+
(regex.compile(r"(?i)\[activexcontrol\]"), ""),
|
|
408
|
+
(regex.compile(r"(?i)\[/activexcontrol\]"), ""),
|
|
409
|
+
(regex.compile(r"(?i)\[webbrowser\]"), ""),
|
|
410
|
+
(regex.compile(r"(?i)\[/webbrowser\]"), ""),
|
|
411
|
+
(regex.compile(r"(?i)\[internet\]"), ""),
|
|
412
|
+
(regex.compile(r"(?i)\[/internet\]"), ""),
|
|
413
|
+
(regex.compile(r"(?i)\[network\]"), ""),
|
|
414
|
+
(regex.compile(r"(?i)\[/network\]"), ""),
|
|
415
|
+
(regex.compile(r"(?i)\[database\]"), ""),
|
|
416
|
+
(regex.compile(r"(?i)\[/database\]"), ""),
|
|
417
|
+
(regex.compile(r"(?i)\[spreadsheet\]"), ""),
|
|
418
|
+
(regex.compile(r"(?i)\[/spreadsheet\]"), ""),
|
|
419
|
+
(regex.compile(r"(?i)\[presentation\]"), ""),
|
|
420
|
+
(regex.compile(r"(?i)\[/presentation\]"), ""),
|
|
421
|
+
(regex.compile(r"(?i)\[document\]"), ""),
|
|
422
|
+
(regex.compile(r"(?i)\[/document\]"), ""),
|
|
423
|
+
(regex.compile(r"(?i)\[folder\]"), ""),
|
|
424
|
+
(regex.compile(r"(?i)\[/folder\]"), ""),
|
|
425
|
+
(regex.compile(r"(?i)\[group\]"), ""),
|
|
426
|
+
(regex.compile(r"(?i)\[/group\]"), ""),
|
|
427
|
+
(regex.compile(r"(?i)\[category\]"), ""),
|
|
428
|
+
(regex.compile(r"(?i)\[/category\]"), ""),
|
|
429
|
+
(regex.compile(r"(?i)\[tag\]"), ""),
|
|
430
|
+
(regex.compile(r"(?i)\[/tag\]"), ""),
|
|
431
|
+
(regex.compile(r"(?i)\[label\]"), ""),
|
|
432
|
+
(regex.compile(r"(?i)\[/label\]"), ""),
|
|
433
|
+
(regex.compile(r"(?i)\[comment\]"), ""),
|
|
434
|
+
(regex.compile(r"(?i)\[/comment\]"), ""),
|
|
435
|
+
(regex.compile(r"(?i)\[reminder\]"), ""),
|
|
436
|
+
(regex.compile(r"(?i)\[/reminder\]"), ""),
|
|
437
|
+
(regex.compile(r"(?i)\[alarm\]"), ""),
|
|
438
|
+
(regex.compile(r"(?i)\[/alarm\]"), ""),
|
|
439
|
+
(regex.compile(r"(?i)\[calendar\]"), ""),
|
|
440
|
+
(regex.compile(r"(?i)\[/calendar\]"), ""),
|
|
441
|
+
(regex.compile(r"(?i)\[task\]"), ""),
|
|
442
|
+
(regex.compile(r"(?i)\[/task\]"), ""),
|
|
443
|
+
(regex.compile(r"(?i)\[project\]"), ""),
|
|
444
|
+
(regex.compile(r"(?i)\[/project\]"), ""),
|
|
445
|
+
# ===== HTML 标签 =====
|
|
446
|
+
(regex.compile(r"(?i)<html\b[^>]*>"), ""),
|
|
447
|
+
(regex.compile(r"(?i)</html>"), ""),
|
|
448
|
+
(regex.compile(r"(?i)<body\b[^>]*>"), ""),
|
|
449
|
+
(regex.compile(r"(?i)</body>"), ""),
|
|
450
|
+
(regex.compile(r"(?i)<p\b[^>]*>"), "\n"),
|
|
451
|
+
(regex.compile(r"(?i)</p>"), "。"),
|
|
452
|
+
(regex.compile(r"(?i)<br\s*/?>"), ","),
|
|
453
|
+
(regex.compile(r"(?i)<hr\s*/?>"), "……"),
|
|
454
|
+
(regex.compile(r"(?i)<h[1-6]\b[^>]*>"), "\n"),
|
|
455
|
+
(regex.compile(r"(?i)</h[1-6]>"), "。"),
|
|
456
|
+
(regex.compile(r"(?i)<div\b[^>]*>"), "\n"),
|
|
457
|
+
(regex.compile(r"(?i)</div>"), ""),
|
|
458
|
+
(regex.compile(r"(?i)<span\b[^>]*>"), ""),
|
|
459
|
+
(regex.compile(r"(?i)</span>"), ""),
|
|
460
|
+
(regex.compile(r"(?i)<blockquote\b[^>]*>"), ",引用,"),
|
|
461
|
+
(regex.compile(r"(?i)</blockquote>"), ""),
|
|
462
|
+
(regex.compile(r"(?i)<q\b[^>]*>"), "「"),
|
|
463
|
+
(regex.compile(r"(?i)</q>"), "」"),
|
|
464
|
+
(regex.compile(r"(?i)<math\b[^>]*>"), ""),
|
|
465
|
+
(regex.compile(r"(?i)</math>"), ""),
|
|
466
|
+
(regex.compile(r"(?i)<mml:math\b[^>]*>"), ""),
|
|
467
|
+
(regex.compile(r"(?i)</mml:math>"), ""),
|
|
468
|
+
(regex.compile(r"(?i)<code\b[^>]*>"), ",代码,"),
|
|
469
|
+
(regex.compile(r"(?i)</code>"), ""),
|
|
470
|
+
(regex.compile(r"(?i)<pre\b[^>]*>"), ",代码块,"),
|
|
471
|
+
(regex.compile(r"(?i)</pre>"), ""),
|
|
472
|
+
(regex.compile(r"(?i)<table\b[^>]*>"), ",表格开始,"),
|
|
473
|
+
(regex.compile(r"(?i)</table>"), ",表格结束,"),
|
|
474
|
+
(regex.compile(r"(?i)<tr\b[^>]*>"), ""),
|
|
475
|
+
(regex.compile(r"(?i)</tr>"), ","),
|
|
476
|
+
(regex.compile(r"(?i)<t[dh]\b[^>]*>"), ","),
|
|
477
|
+
(regex.compile(r"(?i)</t[dh]>"), ""),
|
|
478
|
+
(regex.compile(r"(?i)<caption\b[^>]*>"), ",表题,"),
|
|
479
|
+
(regex.compile(r"(?i)</caption>"), ","),
|
|
480
|
+
(regex.compile(r"(?i)<ul\b[^>]*>"), ""),
|
|
481
|
+
(regex.compile(r"(?i)</ul>"), ""),
|
|
482
|
+
(regex.compile(r"(?i)<ol\b[^>]*>"), ""),
|
|
483
|
+
(regex.compile(r"(?i)</ol>"), ""),
|
|
484
|
+
(regex.compile(r"(?i)<li\b[^>]*>"), ""),
|
|
485
|
+
(regex.compile(r"(?i)</li>"), "。\n"),
|
|
486
|
+
(regex.compile(r"(?i)<dl\b[^>]*>"), ""),
|
|
487
|
+
(regex.compile(r"(?i)</dl>"), ""),
|
|
488
|
+
(regex.compile(r"(?i)<dt\b[^>]*>"), ""),
|
|
489
|
+
(regex.compile(r"(?i)</dt>"), ":"),
|
|
490
|
+
(regex.compile(r"(?i)<dd\b[^>]*>"), ""),
|
|
491
|
+
(regex.compile(r"(?i)</dd>"), ""),
|
|
492
|
+
(regex.compile(r"(?i)<page\b[^>]*>"), ""),
|
|
493
|
+
(regex.compile(r"(?i)</page>"), ""),
|
|
494
|
+
# ===== 页码标注 =====
|
|
495
|
+
(regex.compile(r"【第\d+页】"), ""),
|
|
496
|
+
(regex.compile(r"第\s*\d+\s*页"), ""),
|
|
497
|
+
# ===== HTML 上标 / 下标标签 =====
|
|
498
|
+
(regex.compile(r"(?i)<SUP>[^<]*</SUP>"), ""),
|
|
499
|
+
(regex.compile(r"(?i)</?SUP>"), ""),
|
|
500
|
+
(regex.compile(r"(?i)<SUB>[^<]*</SUB>"), ""),
|
|
501
|
+
(regex.compile(r"(?i)</?SUB>"), ""),
|
|
502
|
+
# ===== ISBN 整段删除 =====
|
|
503
|
+
(regex.compile(r"(?i)(?<![A-Za-z0-9])ISBN(?:[- :]*[\dXx]){9,}"), ""),
|
|
504
|
+
# ===== 重复标点折叠 =====
|
|
505
|
+
(regex.compile(r"[??]{2,}"), "?"),
|
|
506
|
+
(regex.compile(r"[!!]{2,}"), "!"),
|
|
507
|
+
(regex.compile(r"[,,]{2,}"), ","),
|
|
508
|
+
(regex.compile(r"[.。]{3,}"), "……"),
|
|
509
|
+
(regex.compile(r"…{2,}"), "……"),
|
|
510
|
+
(regex.compile(r"[~~]{2,}"), "~"),
|
|
511
|
+
(regex.compile(r"[。.]{2,}"), "。"),
|
|
512
|
+
# ===== Markdown 粗体 / 斜体 / 行内代码 =====
|
|
513
|
+
(regex.compile(r"\*\*([^*\n]+)\*\*"), r"\1"),
|
|
514
|
+
(regex.compile(r"(?<!\*)\*([^*\n]+)\*(?!\*)"), r"\1"),
|
|
515
|
+
(regex.compile(r"__([^_\n]+)__"), r"\1"),
|
|
516
|
+
(regex.compile(r"`([^`\n]+)`"), r"\1"),
|
|
517
|
+
# ===== Markdown 标题 / 图片 / 链接 =====
|
|
518
|
+
(regex.compile(r"(?m)^#{1,6}\s+"), ""),
|
|
519
|
+
(regex.compile(r"!\[([^\]]*)\]\([^)]*\)"), ""),
|
|
520
|
+
(regex.compile(r"\[([^\]]+)\]\([^)]*\)"), r"\1"),
|
|
521
|
+
# ===== URL / Email =====
|
|
522
|
+
(regex.compile(r"(?i)https?://\S+"), ""),
|
|
523
|
+
(regex.compile(r"(?i)(?<![A-Za-z0-9])www\.\S+"), ""),
|
|
524
|
+
(regex.compile(r"(?i)\b[\w.+-]+@[\w-]+\.[\w.-]+\b"), ""),
|
|
525
|
+
# ===== 拉丁缩写(带 lookaround + 后跟数字才展开) =====
|
|
526
|
+
(regex.compile(r"(?i)(?<![A-Za-z])p\.\s+(?=\d)"), "page "),
|
|
527
|
+
(regex.compile(r"(?i)(?<![A-Za-z])pp\.\s+(?=\d)"), "pages "),
|
|
528
|
+
(regex.compile(r"(?i)(?<![A-Za-z])No\.\s*(?=\d)"), "Number "),
|
|
529
|
+
(regex.compile(r"(?i)(?<![A-Za-z])no\.\s*(?=\d)"), "number "),
|
|
530
|
+
# ===== 全角空格 → 逗号(诗句四字句之间停顿) =====
|
|
531
|
+
(regex.compile(r"\u3000+"), ","),
|
|
532
|
+
# ===== 半角空白压缩 =====
|
|
533
|
+
(regex.compile(r"[ \t]+"), " "),
|
|
534
|
+
]
|
|
535
|
+
|
|
536
|
+
# ============================================================
|
|
537
|
+
# 辅助函数(数字 / 罗马 / 日期 / 时间 / 序数 / 货币 / 温度)
|
|
538
|
+
# ============================================================
|
|
539
|
+
|
|
540
|
+
_ONES = [
|
|
541
|
+
"zero",
|
|
542
|
+
"one",
|
|
543
|
+
"two",
|
|
544
|
+
"three",
|
|
545
|
+
"four",
|
|
546
|
+
"five",
|
|
547
|
+
"six",
|
|
548
|
+
"seven",
|
|
549
|
+
"eight",
|
|
550
|
+
"nine",
|
|
551
|
+
"ten",
|
|
552
|
+
"eleven",
|
|
553
|
+
"twelve",
|
|
554
|
+
"thirteen",
|
|
555
|
+
"fourteen",
|
|
556
|
+
"fifteen",
|
|
557
|
+
"sixteen",
|
|
558
|
+
"seventeen",
|
|
559
|
+
"eighteen",
|
|
560
|
+
"nineteen",
|
|
561
|
+
]
|
|
562
|
+
_TENS = [
|
|
563
|
+
"",
|
|
564
|
+
"",
|
|
565
|
+
"twenty",
|
|
566
|
+
"thirty",
|
|
567
|
+
"forty",
|
|
568
|
+
"fifty",
|
|
569
|
+
"sixty",
|
|
570
|
+
"seventy",
|
|
571
|
+
"eighty",
|
|
572
|
+
"ninety",
|
|
573
|
+
]
|
|
574
|
+
_SCALES = [(10**9, "billion"), (10**6, "million"), (10**3, "thousand"), (1, "")]
|
|
575
|
+
|
|
576
|
+
|
|
577
|
+
def _int_to_words(n: int) -> str:
|
|
578
|
+
"""整数转英文词(支持到十亿级,负数加 minus)。"""
|
|
579
|
+
if n < 0:
|
|
580
|
+
return "minus " + _int_to_words(-n)
|
|
581
|
+
if n < 20:
|
|
582
|
+
return _ONES[n]
|
|
583
|
+
if n < 100:
|
|
584
|
+
return _TENS[n // 10] + ("" if n % 10 == 0 else "-" + _ONES[n % 10])
|
|
585
|
+
if n < 1000:
|
|
586
|
+
rest = n % 100
|
|
587
|
+
return (
|
|
588
|
+
_ONES[n // 100]
|
|
589
|
+
+ " hundred"
|
|
590
|
+
+ ("" if rest == 0 else " and " + _int_to_words(rest))
|
|
591
|
+
)
|
|
592
|
+
for value, name in _SCALES:
|
|
593
|
+
if n >= value:
|
|
594
|
+
rest = n % value
|
|
595
|
+
head = _int_to_words(n // value) + (" " + name if name else "")
|
|
596
|
+
return head + ("" if rest == 0 else " " + _int_to_words(rest))
|
|
597
|
+
return str(n)
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
def _number_to_words(s: str) -> str:
|
|
601
|
+
"""字符串数字(可能含逗号/小数点)转英文词。"""
|
|
602
|
+
s = s.replace(",", "")
|
|
603
|
+
if "." in s:
|
|
604
|
+
int_part, dec_part = s.split(".", 1)
|
|
605
|
+
return (
|
|
606
|
+
_int_to_words(int(int_part or 0))
|
|
607
|
+
+ " point "
|
|
608
|
+
+ " ".join(_ONES[int(d)] for d in dec_part)
|
|
609
|
+
)
|
|
610
|
+
return _int_to_words(int(s))
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
_ORDINAL_IRREGULAR = {
|
|
614
|
+
1: "first",
|
|
615
|
+
2: "second",
|
|
616
|
+
3: "third",
|
|
617
|
+
4: "fourth",
|
|
618
|
+
5: "fifth",
|
|
619
|
+
6: "sixth",
|
|
620
|
+
7: "seventh",
|
|
621
|
+
8: "eighth",
|
|
622
|
+
9: "ninth",
|
|
623
|
+
10: "tenth",
|
|
624
|
+
11: "eleventh",
|
|
625
|
+
12: "twelfth",
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
_ORDINAL_LAST = {
|
|
629
|
+
"zero": "zeroth",
|
|
630
|
+
"one": "first",
|
|
631
|
+
"two": "second",
|
|
632
|
+
"three": "third",
|
|
633
|
+
"four": "fourth",
|
|
634
|
+
"five": "fifth",
|
|
635
|
+
"six": "sixth",
|
|
636
|
+
"seven": "seventh",
|
|
637
|
+
"eight": "eighth",
|
|
638
|
+
"nine": "ninth",
|
|
639
|
+
"ten": "tenth",
|
|
640
|
+
"eleven": "eleventh",
|
|
641
|
+
"twelve": "twelfth",
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
|
|
645
|
+
def _ordinal_word(w: str) -> str:
|
|
646
|
+
if w in _ORDINAL_LAST:
|
|
647
|
+
return _ORDINAL_LAST[w]
|
|
648
|
+
if w.endswith("y"):
|
|
649
|
+
return w[:-1] + "ieth"
|
|
650
|
+
if w.endswith("t"):
|
|
651
|
+
return w + "h"
|
|
652
|
+
return w + "th"
|
|
653
|
+
|
|
654
|
+
|
|
655
|
+
def _int_to_ordinal(n: int) -> str:
|
|
656
|
+
if n in _ORDINAL_IRREGULAR:
|
|
657
|
+
return _ORDINAL_IRREGULAR[n]
|
|
658
|
+
w = _int_to_words(n)
|
|
659
|
+
parts = w.split(" ")
|
|
660
|
+
last = parts[-1]
|
|
661
|
+
if "-" in last:
|
|
662
|
+
head, tail = last.rsplit("-", 1)
|
|
663
|
+
parts[-1] = head + "-" + _ordinal_word(tail)
|
|
664
|
+
else:
|
|
665
|
+
parts[-1] = _ordinal_word(last)
|
|
666
|
+
return " ".join(parts)
|
|
667
|
+
|
|
668
|
+
|
|
669
|
+
# ---- 罗马数字 ----
|
|
670
|
+
_ROMAN_MAP = [
|
|
671
|
+
(1000, "M"),
|
|
672
|
+
(900, "CM"),
|
|
673
|
+
(500, "D"),
|
|
674
|
+
(400, "CD"),
|
|
675
|
+
(100, "C"),
|
|
676
|
+
(90, "XC"),
|
|
677
|
+
(50, "L"),
|
|
678
|
+
(40, "XL"),
|
|
679
|
+
(10, "X"),
|
|
680
|
+
(9, "IX"),
|
|
681
|
+
(5, "V"),
|
|
682
|
+
(4, "IV"),
|
|
683
|
+
(1, "I"),
|
|
684
|
+
]
|
|
685
|
+
_ROMAN_VALID = {v for _, v in _ROMAN_MAP}
|
|
686
|
+
# 扩充排除表:这些大写组合在正文/英文语境中很常见,不宜当成罗马数字
|
|
687
|
+
_ROMAN_EXCLUDE = {
|
|
688
|
+
"MIX",
|
|
689
|
+
"DIV",
|
|
690
|
+
"CD",
|
|
691
|
+
"DC",
|
|
692
|
+
"MM",
|
|
693
|
+
"CM",
|
|
694
|
+
"XC",
|
|
695
|
+
"LX",
|
|
696
|
+
"XL",
|
|
697
|
+
}
|
|
698
|
+
# 用 lookaround 替代 \b,避免中文字符旁 \b 失效
|
|
699
|
+
_ROMAN_RE = regex.compile(r"(?<![A-Za-z])([IVXLCDM]{2,15})(?![A-Za-z])")
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
def _roman_to_int(s: str) -> int:
|
|
703
|
+
total, i = 0, 0
|
|
704
|
+
while i < len(s):
|
|
705
|
+
if i + 1 < len(s) and s[i : i + 2] in _ROMAN_VALID:
|
|
706
|
+
total += next(n for n, v in _ROMAN_MAP if v == s[i : i + 2])
|
|
707
|
+
i += 2
|
|
708
|
+
else:
|
|
709
|
+
total += next(n for n, v in _ROMAN_MAP if v == s[i])
|
|
710
|
+
i += 1
|
|
711
|
+
return total
|
|
712
|
+
|
|
713
|
+
|
|
714
|
+
def _int_to_roman(n: int) -> str:
|
|
715
|
+
"""标准罗马数字表示(与 _roman_to_int 互逆)。"""
|
|
716
|
+
result = ""
|
|
717
|
+
for val, sym in _ROMAN_MAP:
|
|
718
|
+
while n >= val:
|
|
719
|
+
result += sym
|
|
720
|
+
n -= val
|
|
721
|
+
return result
|
|
722
|
+
|
|
723
|
+
|
|
724
|
+
def _expand_roman_numerals(text: str, *, to_digits: bool = False) -> str:
|
|
725
|
+
"""
|
|
726
|
+
罗马数字展开。
|
|
727
|
+
- to_digits=False:英文行,转英文单词(IV -> four)
|
|
728
|
+
- to_digits=True:中文行,转阿拉伯数字(第IV章 -> 第4章),交给 TTS/中文 TN 读
|
|
729
|
+
"""
|
|
730
|
+
|
|
731
|
+
def _repl(m):
|
|
732
|
+
s = m.group(1)
|
|
733
|
+
if s in _ROMAN_EXCLUDE:
|
|
734
|
+
return s
|
|
735
|
+
n = _roman_to_int(s)
|
|
736
|
+
if not (1 <= n <= 3999):
|
|
737
|
+
return s
|
|
738
|
+
if _int_to_roman(n) != s:
|
|
739
|
+
return s
|
|
740
|
+
return str(n) if to_digits else _int_to_words(n)
|
|
741
|
+
|
|
742
|
+
return _ROMAN_RE.sub(_repl, text)
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
# ---- 货币 ----
|
|
746
|
+
# 注意:¥ 在中国书籍里基本是人民币,不是日元
|
|
747
|
+
_CURRENCY_NAMES = {
|
|
748
|
+
"$": ("dollar", "dollars", "cent", "cents"),
|
|
749
|
+
"€": ("euro", "euros", "cent", "cents"),
|
|
750
|
+
"£": ("pound", "pounds", "penny", "pence"),
|
|
751
|
+
"¥": ("yuan", "yuan", "fen", "fen"),
|
|
752
|
+
"¥": ("yuan", "yuan", "fen", "fen"),
|
|
753
|
+
"₩": ("won", "won", "jeon", "jeon"),
|
|
754
|
+
"₹": ("rupee", "rupees", "paisa", "paise"),
|
|
755
|
+
}
|
|
756
|
+
_CURRENCY_RE = regex.compile(r"([$€£¥¥₩₹])\s*([\d,]+)(?:\.(\d{1,2}))?")
|
|
757
|
+
|
|
758
|
+
|
|
759
|
+
def _expand_currencies(text: str) -> str:
|
|
760
|
+
def _repl(m):
|
|
761
|
+
sym = m.group(1)
|
|
762
|
+
names = _CURRENCY_NAMES.get(sym)
|
|
763
|
+
if not names:
|
|
764
|
+
return m.group(0)
|
|
765
|
+
major_s, major_p, minor_s, minor_p = names
|
|
766
|
+
n = int(m.group(2).replace(",", ""))
|
|
767
|
+
dec = m.group(3)
|
|
768
|
+
major = major_s if n == 1 else major_p
|
|
769
|
+
if dec:
|
|
770
|
+
cents = int(dec.ljust(2, "0"))
|
|
771
|
+
if cents:
|
|
772
|
+
minor = minor_s if cents == 1 else minor_p
|
|
773
|
+
return f"{_int_to_words(n)} {major} and {_int_to_words(cents)} {minor}"
|
|
774
|
+
return f"{_int_to_words(n)} {major}"
|
|
775
|
+
|
|
776
|
+
return _CURRENCY_RE.sub(_repl, text)
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
# ---- 温度(支持 °C / °F / ℃ / ℉,允许负号和小数) ----
|
|
780
|
+
_TEMP_RE = regex.compile(r"(-?\d+(?:\.\d+)?)\s*(°\s*[CFcf]|℃|℉)(?![A-Za-z0-9])")
|
|
781
|
+
|
|
782
|
+
|
|
783
|
+
def _expand_temperatures(text: str) -> str:
|
|
784
|
+
def _repl(m):
|
|
785
|
+
v = m.group(1)
|
|
786
|
+
unit = m.group(2)
|
|
787
|
+
is_c = "C" in unit.upper() or "℃" in unit
|
|
788
|
+
name = "degrees Celsius" if is_c else "degrees Fahrenheit"
|
|
789
|
+
return f"{_number_to_words(v)} {name}"
|
|
790
|
+
|
|
791
|
+
return _TEMP_RE.sub(_repl, text)
|
|
792
|
+
|
|
793
|
+
|
|
794
|
+
# ---- 日期(MM/DD/YYYY 与 YYYY-MM-DD,年份限定 1000–2999) ----
|
|
795
|
+
_MONTHS = [
|
|
796
|
+
"January",
|
|
797
|
+
"February",
|
|
798
|
+
"March",
|
|
799
|
+
"April",
|
|
800
|
+
"May",
|
|
801
|
+
"June",
|
|
802
|
+
"July",
|
|
803
|
+
"August",
|
|
804
|
+
"September",
|
|
805
|
+
"October",
|
|
806
|
+
"November",
|
|
807
|
+
"December",
|
|
808
|
+
]
|
|
809
|
+
_DATE_SLASH_RE = regex.compile(r"(?<!\d)(\d{1,2})/(\d{1,2})/(\d{2,4})(?!\d)")
|
|
810
|
+
_DATE_ISO_RE = regex.compile(r"(?<!\d)(\d{4})-(\d{2})-(\d{2})(?!\d)")
|
|
811
|
+
|
|
812
|
+
|
|
813
|
+
def _expand_dates(text: str) -> str:
|
|
814
|
+
def _repl_slash(m):
|
|
815
|
+
mo, dy, yr = int(m.group(1)), int(m.group(2)), int(m.group(3))
|
|
816
|
+
if yr < 100:
|
|
817
|
+
yr += 2000 if yr < 50 else 1900
|
|
818
|
+
if not (1 <= mo <= 12 and 1 <= dy <= 31):
|
|
819
|
+
return m.group(0)
|
|
820
|
+
if not (1000 <= yr <= 2999):
|
|
821
|
+
return m.group(0)
|
|
822
|
+
return f"{_MONTHS[mo - 1]} {_int_to_ordinal(dy)}, {_int_to_words(yr)}"
|
|
823
|
+
|
|
824
|
+
def _repl_iso(m):
|
|
825
|
+
yr, mo, dy = int(m.group(1)), int(m.group(2)), int(m.group(3))
|
|
826
|
+
if not (1 <= mo <= 12 and 1 <= dy <= 31):
|
|
827
|
+
return m.group(0)
|
|
828
|
+
if not (1000 <= yr <= 2999):
|
|
829
|
+
return m.group(0)
|
|
830
|
+
return f"{_MONTHS[mo - 1]} {_int_to_ordinal(dy)}, {_int_to_words(yr)}"
|
|
831
|
+
|
|
832
|
+
text = _DATE_SLASH_RE.sub(_repl_slash, text)
|
|
833
|
+
return _DATE_ISO_RE.sub(_repl_iso, text)
|
|
834
|
+
|
|
835
|
+
|
|
836
|
+
# ---- 时间 ----
|
|
837
|
+
_TIME_AMPM_RE = regex.compile(
|
|
838
|
+
r"(?<!\d)(\d{1,2}):(\d{2})\s*([AaPp])\.?[Mm]\.?(?![A-Za-z])"
|
|
839
|
+
)
|
|
840
|
+
_TIME_24_RE = regex.compile(r"(?<!\d)([01]?\d|2[0-3]):([0-5]\d)(?!\d)")
|
|
841
|
+
|
|
842
|
+
|
|
843
|
+
def _expand_times(text: str) -> str:
|
|
844
|
+
def _repl_ampm(m):
|
|
845
|
+
h, mi = int(m.group(1)), int(m.group(2))
|
|
846
|
+
ampm = "A M" if m.group(3).upper() == "A" else "P M"
|
|
847
|
+
return (
|
|
848
|
+
f"{_int_to_words(h)} {_int_to_words(mi)} {ampm}"
|
|
849
|
+
if mi
|
|
850
|
+
else f"{_int_to_words(h)} {ampm}"
|
|
851
|
+
)
|
|
852
|
+
|
|
853
|
+
def _repl_24(m):
|
|
854
|
+
h, mi = int(m.group(1)), int(m.group(2))
|
|
855
|
+
if h == 0:
|
|
856
|
+
return f"zero {_int_to_words(mi)}" if mi else "midnight"
|
|
857
|
+
return f"{_int_to_words(h)} {_int_to_words(mi)}" if mi else _int_to_words(h)
|
|
858
|
+
|
|
859
|
+
text = _TIME_AMPM_RE.sub(_repl_ampm, text)
|
|
860
|
+
return _TIME_24_RE.sub(_repl_24, text)
|
|
861
|
+
|
|
862
|
+
|
|
863
|
+
# ---- 序数词(1st / 2nd / 3rd / 4th ...) ----
|
|
864
|
+
_ORDINAL_RE = regex.compile(
|
|
865
|
+
r"(?<![A-Za-z0-9])(\d+)(st|nd|rd|th)(?![A-Za-z])", regex.IGNORECASE
|
|
866
|
+
)
|
|
867
|
+
|
|
868
|
+
|
|
869
|
+
def _expand_ordinals(text: str) -> str:
|
|
870
|
+
return _ORDINAL_RE.sub(lambda m: _int_to_ordinal(int(m.group(1))), text)
|
|
871
|
+
|
|
872
|
+
|
|
873
|
+
# ---- 纯基数词 / 小数(最后执行) ----
|
|
874
|
+
_NUMBER_RE = regex.compile(
|
|
875
|
+
r"(?<![A-Za-z0-9.])(\d{1,3}(?:,\d{3})+|\d+)(\.\d+)?(?![A-Za-z0-9]|\.\d)"
|
|
876
|
+
)
|
|
877
|
+
|
|
878
|
+
|
|
879
|
+
def _expand_numbers(text: str) -> str:
|
|
880
|
+
def _repl(m):
|
|
881
|
+
return _number_to_words(m.group(1) + (m.group(2) or ""))
|
|
882
|
+
|
|
883
|
+
return _NUMBER_RE.sub(_repl, text)
|
|
884
|
+
|
|
885
|
+
|
|
886
|
+
# ============================================================
|
|
887
|
+
# 语言分流:中文行不把数字/日期/货币/温度展开成英文
|
|
888
|
+
# ============================================================
|
|
889
|
+
|
|
890
|
+
_CJK_CHAR_RE = regex.compile(r"[\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff]")
|
|
891
|
+
_LATIN_CHAR_RE = regex.compile(r"[A-Za-z]")
|
|
892
|
+
|
|
893
|
+
|
|
894
|
+
def _is_cjk_dominant(segment: str) -> bool:
|
|
895
|
+
"""
|
|
896
|
+
判断一行/一段是否以中文为主。
|
|
897
|
+
纯英文返回 False;中文行即使夹杂英文书名,只要中文字符不太少也按中文处理。
|
|
898
|
+
"""
|
|
899
|
+
cjk = len(_CJK_CHAR_RE.findall(segment))
|
|
900
|
+
latin = len(_LATIN_CHAR_RE.findall(segment))
|
|
901
|
+
if cjk == 0:
|
|
902
|
+
return False
|
|
903
|
+
return cjk * 2 >= latin
|
|
904
|
+
|
|
905
|
+
|
|
906
|
+
def _apply_language_sensitive_expansions(text: str, expand_digits: bool) -> str:
|
|
907
|
+
"""
|
|
908
|
+
按行分流:
|
|
909
|
+
- 中文行:只把罗马数字转成阿拉伯数字(第IV章 -> 第4章),数字/日期保留给中文 TTS 或后续 TN。
|
|
910
|
+
- 英文行:走原来的英文数字、日期、时间、序数、货币、温度展开。
|
|
911
|
+
"""
|
|
912
|
+
lines = text.split("\n")
|
|
913
|
+
out: list[str] = []
|
|
914
|
+
for line in lines:
|
|
915
|
+
if _is_cjk_dominant(line):
|
|
916
|
+
line = _expand_roman_numerals(line, to_digits=True)
|
|
917
|
+
else:
|
|
918
|
+
line = _expand_currencies(line)
|
|
919
|
+
line = _expand_temperatures(line)
|
|
920
|
+
line = _expand_dates(line)
|
|
921
|
+
line = _expand_times(line)
|
|
922
|
+
line = _expand_ordinals(line)
|
|
923
|
+
line = _expand_roman_numerals(line, to_digits=False)
|
|
924
|
+
if expand_digits:
|
|
925
|
+
line = _expand_numbers(line)
|
|
926
|
+
out.append(line)
|
|
927
|
+
return "\n".join(out)
|
|
928
|
+
|
|
929
|
+
|
|
930
|
+
# ============================================================
|
|
931
|
+
# 排版收尾(换行统一 + 段落断句)
|
|
932
|
+
# ============================================================
|
|
933
|
+
|
|
934
|
+
|
|
935
|
+
def _finalize_layout(text: str) -> str:
|
|
936
|
+
"""把结构换行转成 TTS 可识别的标点。
|
|
937
|
+
|
|
938
|
+
段落之间(\\n\\n)→ 句号;行末无标点 → 逗号。
|
|
939
|
+
目的是让 TTS 在诗句行、段落、题解之间有正确的停顿,
|
|
940
|
+
而不是把 \\r\\n 当成空白忽略、导致全文连读。
|
|
941
|
+
"""
|
|
942
|
+
# 换行符统一:Windows \r\n、Mac 老式 \r → \n
|
|
943
|
+
text = text.replace("\r\n", "\n").replace("\r", "\n")
|
|
944
|
+
# 去掉首尾空白(必须在 \n\n → 。\n 之前)
|
|
945
|
+
text = text.strip()
|
|
946
|
+
# 压缩连续空行
|
|
947
|
+
text = regex.sub(r"\n{3,}", "\n\n", text)
|
|
948
|
+
# 行首缩进:半角/全角空格都删掉,避免句首产生孤立逗号
|
|
949
|
+
text = regex.sub(r"(?m)^[ \t\u3000]+", "", text)
|
|
950
|
+
# 行内全角空格 → 逗号(诗句四字句之间停顿)
|
|
951
|
+
text = regex.sub(r"\u3000+", ",", text)
|
|
952
|
+
# 半角空白压缩
|
|
953
|
+
text = regex.sub(r"[ \t]+", " ", text)
|
|
954
|
+
# 清理行首孤立标点(删标签后可能残留 ,、;:)
|
|
955
|
+
text = regex.sub(r"(?m)^[,、;:]+", "", text)
|
|
956
|
+
# 段落之间 → 句号(长停顿)
|
|
957
|
+
text = regex.sub(r"\n\n", "。\n", text)
|
|
958
|
+
# 兜底:连续句号压成一个
|
|
959
|
+
text = regex.sub(r"。{2,}", "。", text)
|
|
960
|
+
# 行末无标点 → 逗号(短停顿)
|
|
961
|
+
text = regex.sub(r"(?<=[^\s,。!?;:、,.!?;:…])\n", ",\n", text)
|
|
962
|
+
return text
|
|
963
|
+
|
|
964
|
+
|
|
965
|
+
# ============================================================
|
|
966
|
+
# 主流程(顺序敏感,勿随意调换)
|
|
967
|
+
# ============================================================
|
|
968
|
+
|
|
969
|
+
|
|
970
|
+
def normalize_text(text: str, *, expand_digits: bool = True) -> str:
|
|
971
|
+
"""
|
|
972
|
+
TTS 文本规范化主入口。
|
|
973
|
+
|
|
974
|
+
参数:
|
|
975
|
+
text: 原始文本
|
|
976
|
+
expand_digits: 英文行是否展开纯基数词(默认 True)
|
|
977
|
+
"""
|
|
978
|
+
# 1. NFC 归一化(必须最先)
|
|
979
|
+
text = unicodedata.normalize("NFC", text)
|
|
980
|
+
|
|
981
|
+
# 2. & 特殊处理:必须在 unescape 之前,
|
|
982
|
+
# 否则 "&" 会变成孤立的 "&",TTS 可能读成 "and" 或静默。
|
|
983
|
+
# text = text.replace("&", "和")
|
|
984
|
+
|
|
985
|
+
# 3. HTML 实体解码:一次性处理 增 增 < " 等所有实体。
|
|
986
|
+
# SuperMemo 的 htm 文件通常全部用数字实体存储,这一步是整个流程的关键。
|
|
987
|
+
# text = html.unescape(text)
|
|
988
|
+
|
|
989
|
+
# 4. 不可见 / 控制字符(含 U+00A0 nbsp,unescape 后会出现)
|
|
990
|
+
text = regex.sub(
|
|
991
|
+
r"[\u00a0\u00ad\u180e\u2000-\u200f\u202a-\u202e\u202f\u205f\u2060-\u2064\ufeff]",
|
|
992
|
+
" ",
|
|
993
|
+
text,
|
|
994
|
+
)
|
|
995
|
+
|
|
996
|
+
# 5. 标签 / Markdown / 缩略 / 标点 / 空白压缩(regex_replace_list)
|
|
997
|
+
for pat, rep in regex_replace_list:
|
|
998
|
+
text = pat.sub(rep, text)
|
|
999
|
+
|
|
1000
|
+
# 6. 按语言分流:中文行不把数字/日期转英文;
|
|
1001
|
+
# 英文行执行货币 / 温度 / 日期 / 时间 / 序数 / 罗马 / 基数展开。
|
|
1002
|
+
# 这一步必须在 replace_list 的 "°C" 等兜底替换之前,
|
|
1003
|
+
# 否则英文行的 20°C 会被提前拆坏。
|
|
1004
|
+
text = _apply_language_sensitive_expansions(text, expand_digits)
|
|
1005
|
+
|
|
1006
|
+
# 7. 字面量替换(符号、希腊字母、拉丁缩写、度量衡、带圈数字)
|
|
1007
|
+
for a, b in replace_list:
|
|
1008
|
+
text = text.replace(a, b)
|
|
1009
|
+
|
|
1010
|
+
# 8. 排版收尾(换行 → 标点)
|
|
1011
|
+
text = _finalize_layout(text)
|
|
1012
|
+
return text.strip()
|
|
1013
|
+
|
|
1014
|
+
|
|
1015
|
+
if __name__ == "__main__":
|
|
1016
|
+
sample = (
|
|
1017
|
+
"泮水\r\n"
|
|
1018
|
+
"鲁僖公饮于泮宫并举行受俘仪式,此诗赞颂他有德有威。\r\n\r\n\r\n"
|
|
1019
|
+
"思乐泮水,薄采其芹\r\n"
|
|
1020
|
+
"鲁侯戾止,言观其旂\r\n"
|
|
1021
|
+
"第IV章 2024年10月22日\r\n"
|
|
1022
|
+
"English line: On 10/22/2024, the temperature was 20°C."
|
|
1023
|
+
)
|
|
1024
|
+
print(normalize_text(sample))
|