-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtextpreprocessor.py
400 lines (331 loc) · 10.3 KB
/
textpreprocessor.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# License: MIT
# author: Luis Rei < [email protected] >
"""
Text preprocessing functions.
"""
import unicodedata
import re
from functools import reduce, partial
import ftfy
from sacremoses import MosesTokenizer
from unidecode import unidecode
#
# CONSTANTS
#
NUMBER_WORDS_EN = {
"0": "zero",
"1": "one",
"2": "two",
"3": "three",
"4": "four",
"5": "five",
"6": "six",
"7": "seven",
"8": "eight",
"9": "nine",
}
NUMBER_WORDS_CA = {
"1": "un",
"2": "dos",
"3": "tres",
"4": "quatre",
"5": "cinc",
"6": "sis",
"7": "set",
"8": "vuit",
"9": "nou",
}
NUMBER_WORDS_ES = {
"1": "uno",
"2": "dos",
"3": "tres",
"4": "cuatro",
"5": "cinco",
"6": "seis",
"7": "siete",
"8": "ocho",
"9": "nueve",
}
RPLS = {
"ssilk": "silk",
"inches": "inches",
"length": "length",
"jaqard": "jacquard",
"jaquard": "jacquard",
"italiantextile": "italian textile",
"needleweaving": "needle weaving",
"waistseam": "waist seam",
"sleeveband": "sleeve band",
"drawnwork": "drawn work",
"needlework": "needle work",
"needlewoven": "needle woven",
"threadwork": "thread work",
"needlecase": "needle case",
"longsleeve": "long sleeve",
"designerembroidery": "designer embroidery",
"lampàs": "lampas",
"esfumaturas": "esfumado",
"madrids": "madrid",
}
RPLS_EN = {
"botehs": "boteh",
"halbedier": "halberdier",
"manuscruipt": "manuscript",
"latchets": "latchet",
"lustring": "calendering",
"unplied": "not plied",
"cannellé": "canelle",
"canellé": "canelle",
"clothiing": "clothing",
"bizantinos": "byzantine",
"backseam": "back seam",
"unembroidered": "not embroidered",
"emboidered": "embroidered",
"floorspread": "floor spread",
"overknit": "over knit",
"overstitch": "over stitch",
"underbodice": "under bodice",
"undersleeve": "under sleeve",
"handscreens": "hand screens",
"backstitched": "back stitched",
"regiion": "region",
"lisere": "edging",
"laceing": "lacing",
"commmission": "commission",
}
RPLS_ES = {
"espolinadas": "brocado",
"espolinada": "brocado",
"espolinado": "brocado",
"brochadas": "brocado",
"brochada": "brocado",
"esfumaturas": "esfumado",
"esfumatura": "esfumado",
"lampàs": "lampas",
"éventails": "eventail",
"beentjes": "beentje",
"abanos": "abano",
}
RPLS_CA = {
"espolinadas": "brocades",
"espolinades": "brocades",
"espolinat": "brocades",
"espolinada": "brocades",
"brochadas": "brocades",
"lampàs": "lampas",
"intensidad": "intensitat",
}
cleanr = re.compile("<.*?>")
cleanr_e = re.compile("<.*?>|&([a-z0-9]+|#[0-9]{1,6}|#x[0-9a-f]{1,6});")
def compose_f(funcs):
"""Given a list of functions `funcs`=[f1, f2, f3, ..., fn] returns their
composition:
f = fn(...f3(f2(f1()))...)
"""
funcs = reversed(funcs)
# reduce applies a function of 2 arguments (the first argument of reduce)
# cumulatively to the items of the iterable (second argument) from
# left to right. The left argument (here `f`) is the accumulated value
# and the right argument (here `g`) is the update value from the iterable.
# Each `g` value from the list is a function. The applied function
# (the first lambda) returns a function that is the composition of its two
# arguments (`f(g(x))`. The initializer i.e. the first value of `f` is the
# identity function (second lambda).
return reduce(lambda f, g: lambda x: f(g(x)), funcs, lambda x: x)
def char_name(char):
"""Returns the name of a character or empty string"""
try:
return unicodedata.name(char).lower()
except ValueError:
return ""
return ""
def char_is_punct(char):
"""True if char is a punctuation character."""
return unicodedata.category(char).startswith("P")
def fix_text(text):
"""Fixes text issues using ftfy including mojibake and html entities.
Returns NFC normalized unicode text."""
return ftfy.fix_text(text)
def clean_html(s):
"""Uses a regular expression to remove leftover html tags in text.
"""
s = re.sub(cleanr, "", s)
s = re.sub(cleanr_e, "", s)
return s
def normalize_quotes_and_dashes(text):
"""Normalizes many of the quotations and dashes.
Note: Some of these are not handled by unidecode transliteration."""
res = []
# fix dashes
for c in text:
if not unicodedata.category(c) == "Pd": # "Punctuation, Dash"
res.append(c)
else:
res.append("-")
text = res
# fix quotes
# see https://en.wikipedia.org/wiki/Quotation_mark
# this should cover most common cases
res = []
for c in text:
if not unicodedata.category(c) in ["Pe", "Ps", "Pf", "Pi"]:
res.append(c)
else:
name = char_name(c)
if (
"quot" in name
or "corner bracket" in name
or "double" in name
or "fullwidth" in name
):
res.append('"') # replace
else:
res.append(c)
text = "".join(res)
# dumb quotes
text = re.sub(r"''", r'"', text)
text = re.sub(r"``", r'"', text)
text = re.sub(r"<<", r'"', text)
text = re.sub(r">>", r'"', text)
return text
def strip_accents(text):
"""Strips accents from text."""
text = unicodedata.normalize("NFD", text)
text = "".join([c for c in text if not unicodedata.combining(c)])
return text
def ascii_fold(text):
"""Converts all characters in text to ascii (ascii transliteration)."""
return unidecode(text)
def strip_punct(text):
"""Strips punctuation (including quotes and dash) from text.
Beware:
john's -> johns
zero-tolerance -> zerotolerance
example.com -> examplecom
"""
return "".join(c for c in text if not char_is_punct(c))
def replace_punct(text, replacement=" ", protect=""):
"""Replaces punctuation (including quotes and dash) from text.
If using whitespace (default) it is probably a good idea to call
normalize_whitespace after.
e.g.:
doctor, who? -> doctor who
"""
return "".join(
c if (not char_is_punct(c)) or (c in protect) else replacement
for c in text
)
def replace_string(text, replacements=None, whitespace=True):
"""A wrapper around str.replace where replacements is a dictionary:
original_string -> replacement_string
whitespace=True surounds the replacement with whitespaces.
"""
if not replacements:
return text
for ori, rpl in replacements.items():
if whitespace:
rpl = " " + rpl + " "
text = text.replace(ori, rpl)
return text
def normalize_unicode_punctuation(text):
"""Converts (transliterates) unicode punctuation, symbols,
and numbers to their ASCII equivalent.
"""
res = []
for c in text:
cat = unicodedata.category(c)[0]
if cat in ["P", "N"]:
c = unidecode(c)
elif cat == "S":
c = unidecode(c)
if len(c) > 1:
# most commonly these will be things like EUR, (tm) and (r)
# we want to add a space before, if it is extra it will
# be removed by whitespace normalization
c = " " + c
if c:
res.append(c)
return "".join(res)
def strip_non_printing(text):
"""Removes non-printing (including control) characters from text.
(same as moses script).
"""
return "".join([c for c in text if c.isprintable()])
def strip_symbols(text):
"""Removes symbols (unicode category 'S').
"""
return "".join(c for c in text if unicodedata.category(c)[0] != "S")
def strip_digits(text):
"""Removes digits (number) charaters.
"""
return "".join(c for c in text if not c.isdigit())
def case_fold(text):
"""Converts text to lower case."""
return text.lower()
def normalize_whitespace(text):
"""Merges multiple consecutive whitespace characthers converting them to
space (` `). Also strips whitespaces from start and end of the text."""
return " ".join(text.split())
def str_strip(text):
"""Strips whitespaces from the start and the end of the text."""
return text.strip()
def str_lstrip(text):
"""Strips whitespaces from thestart of the text."""
return text.lstrip()
def str_rstrip(text):
"""Strips whitespaces from the end of the text."""
return text.rstrip()
def normalize_nfc(text):
"""Applies NFC normalization."""
return unicodedata.normalize("NFC", text)
def normalize_nfd(text):
"""Applies NFD normalization."""
return unicodedata.normalize("NFD", text)
def normalize_nfkc(text):
"""Applies NFKC normalization e.g. ™ to TM, ..."""
return unicodedata.normalize("NFKC", text)
def normalize_nfkd(text):
"""Applies NFKD normalization."""
return unicodedata.normalize("NFKD", text)
def sn_preprocessing_function():
"""Returns the SilkNOW text preprocessing function.
Return: function to be used to preprocess text.
"""
punct_f1 = partial(replace_punct, replacement="", protect="'-")
punct_f2 = partial(replace_punct, replacement=" ", protect="'")
punct_f3 = partial(replace_punct, replacement=" ' ", protect="")
replace_f = partial(replace_string, replacements=RPLS)
funcs = [
case_fold,
replace_f,
strip_non_printing,
clean_html,
normalize_quotes_and_dashes,
punct_f1,
punct_f2,
punct_f3,
strip_symbols,
normalize_nfkc,
normalize_whitespace,
]
return compose_f(funcs)
def sn_preprocess_text(text, lang="en"):
"""SilkNOW: preprocess and tokenize text.
Returns preprocessed and tokenized text as a space delimited sequence of
tokens (i.e. returns a string).
"""
preprocess = sn_preprocessing_function()
# tokenize
tkz = MosesTokenizer(lang=lang)
text = tkz.tokenize(text, return_str=True, escape=False)
if lang == "ca":
text = replace_string(text, replacements=RPLS_CA)
elif lang == "es":
text = replace_string(text, replacements=RPLS_ES)
elif lang == "en":
text = replace_string(text, replacements=RPLS_EN)
# preprocess
text = preprocess(text)
return text