-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtext_processing.py
More file actions
216 lines (172 loc) · 6.35 KB
/
text_processing.py
File metadata and controls
216 lines (172 loc) · 6.35 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
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
import re
import abc
import itertools
from typing import Any, List
import inflect
from spellchecker import SpellChecker
import contractions
from emot.emo_unicode import UNICODE_EMOJI, UNICODE_EMOJI_ALIAS, EMOTICONS_EMO
from flashtext import KeywordProcessor
replacement_CASED = {
"Edit:": " ",
"Translation:": " ",
"[deleted]": " ",
"[NAME]": "person",
"100%": "full",
"\r": " ", "\\s": " ", "\\u": " "
}
delete_symbols = '%/^<>#/}{:"~\[\]'
replacement_UNCASED = {
"lol": "lots of laughs",
"lmao": "laughing",
"lmfao": "laughing hard",
"stfu": "shut the fuck up",
"lmk": "let me know",
"wtf": "what the fuck",
"af": "as fuck",
"fk": "fuck",
"smh": "somehow",
"nvm": "never mind",
"ofc": "of course",
"tf": "what the fuck",
"thx": "thanks",
"idk": "i do not know",
"omg": "oh my god",
"u": "you",
"n": "and",
"b": "be",
}
EMOTICONS_EMO = {
**EMOTICONS_EMO,
**{
'-_-': 'suspicious',
'<3': 'love',
"'\(^^)/'": 'happy',
}
}
class TextProcessingPipe(abc.ABC):
def __init__(self):
pass
def forward(self, text: str, *args, **kwds) -> str:
return text
def __call__(self, text: str, *args: Any, **kwds: Any) -> str:
return self.forward(text, *args, **kwds)
class UncasePipe(TextProcessingPipe):
def __init__(self):
super().__init__()
def forward(self, text: str,) -> str:
return text.lower()
class NumbersPipe(TextProcessingPipe):
def __init__(self, replace_with=None):
super().__init__()
self.regex_compiled = re.compile(r"[-+]?(?:\d*\.\d+|\d+)")
self.engine = inflect.engine()
self.replace_with = replace_with
def _callback(self, match) -> str:
group = match.group()
if '.' in group:
number = float(group)
else:
number = int(group)
return self.engine.number_to_words(number)
def forward(self, text: str) -> str:
if self.replace_with is not None and isinstance(self.replace_with, str):
return self.regex_compiled.sub(self.replace_with, text)
else:
return self.regex_compiled.sub(self._callback, text)
class SpellingPipe(TextProcessingPipe):
def __init__(self):
super().__init__()
self.regex_compiled = re.compile(r"(\w+)")
self.spell = SpellChecker()
def _callback(self, match) -> str:
self.spell.correction(match.group())
def forward(self, text: str) -> str:
return self.regex_compiled.sub(self._callback, text)
class ContractionsPipe(TextProcessingPipe):
def __init__(self):
super().__init__()
def forward(self, text: str) -> str:
return contractions.fix(text)
class RepeatedSymbolsPipe(TextProcessingPipe):
def __init__(self, max_repeat=2, regex_symbols=r"a-zA-Z"):
super().__init__()
self.regex_compiled = re.compile(r"([" + regex_symbols + r"])\1{" + str(max_repeat) + r",}")
def _callback(self, match) -> str:
return match.group()[:1]
def forward(self, text: str) -> str:
return self.regex_compiled.sub(self._callback, text)
class SymbolReplacePipe(TextProcessingPipe):
def __init__(self, regex_symbols, replace_with=""):
super().__init__()
self.regex_compiled = re.compile(r"([" + regex_symbols + "])")
self.replace_with = replace_with
def forward(self, text: str) -> str:
return self.regex_compiled.sub(self.replace_with, text)
class WordReplacePipe(TextProcessingPipe):
def __init__(self, replace_dict: dict):
super().__init__()
self.key_processor = KeywordProcessor(case_sensitive=True)
for key, value in replace_dict.items():
self.key_processor.add_keyword(key, value)
def forward(self, text: str) -> str:
return self.key_processor.replace_keywords(text)
class SubstringReplacePipe(TextProcessingPipe):
def __init__(self, replace_dict: dict, add_spacing=False):
super().__init__()
self.replace_dict = replace_dict
pattern = '|'.join(sorted(re.escape(key) for key in replace_dict))
self.regex_compiled = re.compile(pattern)
self.add_spacing = add_spacing
def _callback(self, match) -> str:
return (' ' if self.add_spacing else '') + \
self.replace_dict[(match.group())] + \
(' ' if self.add_spacing else '')
def forward(self, text):
return self.regex_compiled.sub(self._callback, text)
class EmojiToTextPipe(SubstringReplacePipe):
def __new__(cls):
def short_emoticon(text):
or_index = text.find('or')
cm_index = text.find(',')
if or_index == -1 and cm_index == -1:
return text
elif or_index == -1:
return text[:cm_index].strip()
elif cm_index == -1:
return text[:or_index].strip()
else:
return text[:min(or_index, cm_index)].strip()
emoji = {
**UNICODE_EMOJI,
**UNICODE_EMOJI_ALIAS
}
emoji = { key: value.replace(":","").replace("_"," ").strip()
for key, value in emoji.items() }
emoticons = {
**EMOTICONS_EMO,
}
emoticons = { key: short_emoticon(value) for key, value in emoticons.items() }
return SubstringReplacePipe({ **emoji, **emoticons }, add_spacing=True)
class TextProcessingPipeline(TextProcessingPipe):
def __init__(self, pipes: List[TextProcessingPipe]) -> None:
self.pipes = pipes
def forward(self, text: str) -> str:
text = str(text)
for pipe in self.pipes:
text = pipe(text)
return text
@classmethod
def get_standard_pipeline(cls):
return cls([
EmojiToTextPipe(),
SubstringReplacePipe(replacement_CASED, add_spacing=True),
SymbolReplacePipe(delete_symbols, ' '),
UncasePipe(),
RepeatedSymbolsPipe(max_repeat=2, regex_symbols=r"a-zA-Z"),
WordReplacePipe(replacement_UNCASED),
SymbolReplacePipe("$€", replace_with=" money "),
NumbersPipe(replace_with=" "),
RepeatedSymbolsPipe(max_repeat=1, regex_symbols=r"?!,* "),
ContractionsPipe(),
])