-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplacer.py
432 lines (375 loc) · 15.6 KB
/
replacer.py
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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
from collections import Counter, defaultdict
from collections.abc import Iterator, Callable, Sequence
from typing import Optional, T
import re
# CC descriptions and YouTube censored words:
YT_CENSORED = '[ __ ]'
RE_CC_DESC_HW = r'\[(?=[^\[\]]*[\w♪~])[^\[\].?!.。?!]+\]'
RE_CC_DESC_FW = r'【(?=[^【】]*[\w♪~])[^【】.?!.。?!]+】'
RE_CC_DESC = (
# May be either:
# - "[X]" or "【X】", where X doesn't contain the respective type of brackets,
# and contains at least one word-forming character, "♪" or "~".
rf'{RE_CC_DESC_HW}|{RE_CC_DESC_FW}'
)
RE_CENSORED = re.escape(YT_CENSORED)
NORMALIZE_CC: dict[int, int] = {
0x09: 0x005F, # tab to underscore
0x0A: 0x005F, # LF to underscore
0x0D: 0x005F, # CR to underscore
0x20: 0x005F, # space to underscore
# 【】 to []:
0x3010: 0x005B,
0x3011: 0x005D
}
# HTTP(S)URLs and INFORMAL WEB ADDRESSES
#
# Accept most IDNA domain names/internationalized URL:
CJ_RANGES = (
# Based on http://www.localizingjapan.com/blog/2012/01/20/regular-expressions-for-\
# japanese-text/
# We give it a little benefit of doubt by including all kanji and even radicals,
# which still could be indicative of Japanese or Chinese text.
r'\u3041-\u3096' # Hiragana
r'\u30A0-\u30FF' # Katakana (full-width)
r'\u3400-\u4DB5\u4E00-\u9FCB\uF900-\uFA6A' # {Han} (Kanji incl. Chinese-only)
r'\u2E80-\u2FD5' # Han/Kanji radicals
r'\u31F0-\u31FF\u3220-\u3243\u3280-\u337F' # Misc. Japanese Symbols and Characters
)
# 1. Common (not all) TLDs:
# Note: We accept either all upper case or all lower case, to avoid false positives,
# e.g. "sentence ends.It starts another sentence"
RE_TLD = (
r'(?:(?:'
# common longer/non-country TLDs:
r'com|net|org|info|xyz|biz|online|club|pro|'
r'site|shop|store|tech|dev|gov|edu|mil|org|'
r'COM|NET|ORG|INFO|XYZ|BIZ|ONLINE|CLUB|PRO|'
r'SITE|SHOP|STORE|TECH|DEV|GOV|EDU|MIL|ORG|'
# common 2-letter/country TLDs:
r'ac|ad|ae|af|ag|ai|al|am|ao|ar|as|at|au|az|ba|bd|be|bf|bg|bh|'
r'bn|bo|br|bt|bw|by|bz|ca|cc|cd|c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|UK|US|UY|UZ|VC|VE|VG|VN|'
r'WS|ZA|ZM|ZW'
# ensure it's not followed by another alpha character:
r')(?![A-Za-z])|'
# Some East Asian internationalized TLDs
r'(?:中国|中國|香港|澳门|澳門|新加坡|台湾|台灣|网络|公司|日本|コム)'
# ensure it's not followed by another CJ character:
rf'(?![{CJ_RANGES}]))'
)
# 2. Host names (without TLD):
# - can contain letter, digit, hyphen (not underscore),
# - cannot start or end with a hyphen
# \w (perfectly?) matches characters allowed by IDNA allowing different alphabets,
# accents, digits (\W is the negation of \w)
# We allow either a name consisting entirely of CJ characters,
# or not containing any CJ characters (e.g. '日本', 'nihon', but not '日本nihon':
RE_WITH_OR_WO_CJ = rf'(?:[^\W_{CJ_RANGES}]|-)+|[{CJ_RANGES}]+'
RE_HOST = rf'(?!-)(?:{RE_WITH_OR_WO_CJ})(?<!-)'
# 3. Domain name + optional port (no underscores, although allowed in subdomains):
# e.g. apple.com, www.google.com, go.to, WITH PORT: én.wik-pédi4.com:8080
RE_DOMAIN = rf'(?:{RE_HOST}\.)+{RE_TLD}'
RE_DOMAIN_PORT = rf'{RE_DOMAIN}(?::[0-9]+)?'
# 4. Simplified (int'l) paths:
# - Do not allow "'()[]", although legal in URL.
# - Do not allow trailing punctuation .!?,;:, although legal in URL.
RE_PATH = (
r'/(?:' # leading slash followed by any number of
r'[\w/!*;:@&=+$,/?#.~-]|' # - allowed characters allowing int'l (\w)
r'(?:%[0-9a-fA-F]{2})' # - hex escapes
r')*'
r'(?<![.!?,;:])'
)
# 5. URL: HTTP(S) URL with optional port, or URL without protocol and port,
# e.g.
# https://x.com/username,
# x.com/username
# https://www.google.com:443/search?q=term
RE_URL = (
rf'(?:https?://{RE_DOMAIN_PORT}|{RE_DOMAIN})(?:{RE_PATH})?'
)
RE_EMAIL = rf'[a-zA-Z0-9_.+-]+@{RE_DOMAIN}'
# Now handled by RE_URL:
# RE_WWW = r'www.[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+'
# Social network handles generally don't start with . and don't contain consecutive .
RE_HANDLE = r'@(?:[a-zA-Z0-9_.]+)*[a-zA-Z0-9_]+'
# PAT_ADDRESS = re.compile(
# r'|'.join((RE_URL, RE_EMAIL, RE_HANDLE))
# )
NAME2RE = {
'censored': RE_CENSORED,
'cc_desc': RE_CC_DESC,
'web': RE_URL,
'email': RE_EMAIL,
'handle': RE_HANDLE
}
NAME2POS = {
'censored': 'CENSORED',
'cc_desc': 'CCDESC',
'web': 'WEB',
'email': 'EMAIL',
'handle': 'HANDLE'
}
NAMES_CENSORED_CC = ('censored', 'cc_desc')
RE_IN = r'|'.join(rf'(?P<{name}>{regex})' for name, regex in NAME2RE.items())
sub_in = re.compile(RE_IN).sub
# If the placeholder is longer than this, MeCab (always?) breaks it into more tokens,
# which makes replacing it difficult (see retry_if_broken).
# Sometimes tokenizers keep placeholders as one token with the neighbor word, e.g.
# '{word} tubelexplchldr', we resolve that as a third try via retry_if_broken too.
# Also: Stanza (Indonesian lemmatization?) lowercases, so the placeholder needs
# to be lowercase.
PLACEHOLDER = 'tubelexplchldr'
PLACEHOLDER_SPC = ' tubelexplchldr '
PLACEHOLDER_LSPC = ' tubelexplchldr'
PLACEHOLDER_RSPC = 'tubelexplchldr '
RE_BROKEN_PLACEHOLDER = r' ?'.join(PLACEHOLDER)
sub_placeholder = re.compile(rf'\b{PLACEHOLDER}\b').sub
sub_broken_placeholder = re.compile(rf'\b{RE_BROKEN_PLACEHOLDER}\b').sub
def _find_substr_in_list(
xs: list[T], y: str, start: int = 0, f: Callable[[T], str] = lambda x: x
) -> Optional[slice]:
for j in range(max(0, start), len(xs)):
wjk = ''
for k in range(j, len(xs)):
wjk += f(xs[k])
if wjk == y:
return slice(j, k + 1)
return None
def _it_split_placeholders_in_tokens(tokens: list[str]) -> Iterator[str]:
i = 0
for j, t in enumerate(tokens):
if PLACEHOLDER not in t:
continue
if t.startswith(PLACEHOLDER_RSPC):
yield from tokens[i:j]
i = j + 1
yield PLACEHOLDER
yield t.removeprefix(PLACEHOLDER_RSPC)
elif t.endswith(PLACEHOLDER_LSPC):
yield from tokens[i:j]
i = j + 1
yield t.removesuffix(PLACEHOLDER_LSPC)
yield PLACEHOLDER
yield from tokens[i:]
def _it_split_placeholders_in_tagged(
tagged: list[tuple[str, str]]
) -> Iterator[list[tuple[str, str]]]:
i = 0
for j, t_pos in enumerate(tagged):
t, pos = t_pos
if PLACEHOLDER not in t:
continue
if t.startswith(PLACEHOLDER_RSPC):
yield from tagged[i:j]
i = j + 1
yield (PLACEHOLDER, pos)
yield (t.removeprefix(PLACEHOLDER_RSPC), pos)
elif t.endswith(PLACEHOLDER_LSPC):
yield from tagged[i:j]
i = j + 1
yield (t.removesuffix(PLACEHOLDER_LSPC), pos)
yield (PLACEHOLDER, pos)
yield from tagged[i:]
class Replacer:
'''
Replace special tokens by placeholders before tokenization, and then by
final tokens (`out_tokens`) after tokenization.
Usage on continuous text:
>>> counter = Counter()
>>> addresses = defaultdict(list)
>>> r = Replacer(counter, addresses)
>>> t = '[ __ ] Email foo@bar.com and @foo_bar. www.foo.com[engine starts]'
>>> p = r.replace_with_placeholders(t)
>>> p.replace(PLACEHOLDER, '*')
' * Email * and * . * * '
>>> r.replace_placeholders_with_tokens(p)
' [____] Email <email> and <handle> . <web> [engine_starts] '
>>> r.all_placeholders_replaced()
True
>>> counter.total()
5
>>> addresses['handle']
['@foo_bar']
>>> addresses['web']
['www.foo.com']
Usage on individual tokens:
>>> r = Replacer()
>>> p = r.replace_with_placeholders(t)
>>> ' '.join(map(r.replace_placeholder_with_token, p.split()))
'[____] Email <email> and <handle> . <web> [engine_starts]'
Alphabetical addresses and IDNs in Japanese text:
>>> t = 'ウェブサイトはfoobar.co.jpまたは フーバー.コム です。このフーバー.コムは無視する。'
>>> r.replace_placeholders_with_tokens(r.replace_with_placeholders(t))
'ウェブサイトは <web> または <web> です。このフーバー.コムは無視する。'
Combined alpha/IDN (without surrounding text):
>>> r.replace_placeholders_with_tokens(r.replace_with_placeholders('foo.日本'))
' <web> '
Several methods have a retry_if_broken parameter: If we cannot replace all
placeholders, it tries again, searching for placeholders broken by the tokenization
(MeCab sometimes does that).
'''
counter: Counter[str]
addresses: dict[str, list]
out_tokens: list[str]
out_pos: list[str]
out_idx: int
def __init__(self,
counter: Optional[Counter] = None,
addresses: Optional[dict[str, list]] = None
):
self.counter = counter if (counter is not None) else Counter()
self.addresses = (addresses if (addresses is not None) else
defaultdict(list))
self.out_tokens = []
self.out_pos = []
self.out_idx = 0
def _repl_in_placeholder(self, m: re.Match) -> str:
for name, group in m.groupdict().items():
if group is None:
continue
if (name in NAMES_CENSORED_CC):
out = group.translate(NORMALIZE_CC)
else:
self.addresses[name].append(group)
out = f'<{name}>'
self.out_tokens.append(out)
self.out_pos.append(NAME2POS[name])
self.counter[name] += 1
break # Only one non-None group
else:
raise Exception(
f'repl_in_placeholder called without any matched group: {m.groupdict()}'
)
return PLACEHOLDER_SPC
def _repl_placeholder_out(self, _m: re.Match) -> str:
out = self.out_tokens[self.out_idx]
self.out_idx += 1
return out
def _it_replace_broken_in_tokens(self, tokens: Sequence[str]) -> Iterator[str]:
i = 0
while (s := _find_substr_in_list(tokens, PLACEHOLDER, i)):
yield from tokens[i:s.start]
yield self.out_tokens[self.out_idx]
self.out_idx += 1
i = s.stop
yield from tokens[i:]
def _it_replace_broken_in_tagged(
self, tagged: Sequence[tuple[str, str]]
) -> list[tuple[str, str]]:
i = 0
while (s := _find_substr_in_list(tagged, PLACEHOLDER, i, f=lambda tp: tp[0])):
yield from tagged[i:s.start]
idx = self.out_idx
yield (self.out_tokens[idx], self.out_pos[idx])
self.out_idx += 1
i = s.stop
yield from tagged[i:]
def replace_with_placeholders(self, s: str) -> str:
return sub_in(self._repl_in_placeholder, s)
def replace_placeholders_with_tokens(
self, s: str, retry_if_broken: bool = False
) -> str:
'''
In-text replacement.
retry_if_broken: If we cannot replace all placeholders, try again, searching
for placeholders broken by the tokenization (MeCab sometimes does that).
'''
if not self.out_tokens:
return s
assert not (retry_if_broken and self.out_idx)
repl_s = sub_placeholder(self._repl_placeholder_out, s)
if retry_if_broken and not self.all_placeholders_replaced():
self.out_idx = 0
repl_s = sub_broken_placeholder(self._repl_placeholder_out, s)
return repl_s
def replace_placeholder_with_token(self, t: str) -> str:
'''
Single token replacement.
'''
if t != PLACEHOLDER:
return t
out = self.out_tokens[self.out_idx]
self.out_idx += 1
return out
def replace_tagged_placeholder_with_token(
self, t_pos: tuple[str, str]
) -> tuple[str, str]:
'''
Single tagged token replacement.
'''
t, _ = t_pos
if t != PLACEHOLDER:
return t_pos
idx = self.out_idx
out = self.out_tokens[idx]
pos = self.out_pos[idx]
self.out_idx += 1
return (out, pos)
def replace_in_tokens(
self, tokens: Sequence[str], retry_if_broken: bool = False
) -> list[str]:
if not self.out_tokens:
return tokens
assert not (retry_if_broken and self.out_idx)
repl_tokens = [self.replace_placeholder_with_token(t) for t in tokens]
if retry_if_broken and not self.all_placeholders_replaced():
assert isinstance(tokens, Sequence)
self.out_idx = 0
repl_tokens = list(self._it_replace_broken_in_tokens(tokens))
if not self.all_placeholders_replaced():
self.out_idx = 0
tokens = list(_it_split_placeholders_in_tokens(tokens))
repl_tokens = list(self._it_replace_broken_in_tokens(tokens))
return repl_tokens
def replace_in_tagged(self,
tagged: Sequence[tuple[str, str]],
retry_if_broken: bool = False) -> list[tuple[str, str]]:
if not self.out_tokens:
return tagged
assert not (retry_if_broken and self.out_idx)
repl_tagged = [self.replace_tagged_placeholder_with_token(tp) for tp in tagged]
if retry_if_broken and not self.all_placeholders_replaced():
assert isinstance(tagged, Sequence)
self.out_idx = 0
repl_tagged = list(self._it_replace_broken_in_tagged(tagged))
if not self.all_placeholders_replaced():
self.out_idx = 0
tagged = list(_it_split_placeholders_in_tagged(tagged))
repl_tagged = list(self._it_replace_broken_in_tagged(tagged))
return repl_tagged
def all_placeholders_replaced(self) -> bool:
return self.out_idx == len(self.out_tokens)
if __name__ == '__main__':
import doctest
doctest.testmod()
# TODO iterations work the best
# from timeit import timeit
# s0 = ' '.join((['adasd']*10 + ['www.apple.com'])*1000)
# r = Replacer()
# sp = r.replace_with_placeholders(s0)
# spb = sp.replace('tubelexplchldr', 't u b e lexplchldr')
# .split(' ')
# print(timeit('r.out_idx=0; r.replace_in_tokens(spb, retry_if_broken=True)',
# globals=globals(), number=1))
# assert r.all_placeholders_replaced(), r.out_idx
# print(timeit('r.out_idx=0; r.test_replace_in_tokens(spb, retry_if_broken=True)',
# globals=globals(), number=1))
# assert r.all_placeholders_replaced(), r.out_idx