-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpreprocessing.py
47 lines (35 loc) · 1.23 KB
/
preprocessing.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
import re
def cleanup_text(texts):
cleaned_text = []
for text in texts:
# remove ugly " and &
text = re.sub(r""(.*?)"", "\g<1>", text)
text = re.sub(r"&", "", text)
# replace emoticon
text = re.sub(
r"(^| )(\:\w+\:|\<[\/\\]?3|[\(\)\\\D|\*\$][\-\^]?[\:\;\=]|[\:\;\=B8][\-\^]?[3DOPp\@\$\*\\\)\(\/\|])(?=\s|[\!\.\?]|$)",
"\g<1>TOKEMOTICON",
text,
)
text = text.lower()
text = text.replace("tokemoticon", "TOKEMOTICON")
# replace url
text = re.sub(
r"(http|ftp|https)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?",
"TOKURL",
text,
)
# replace mention
text = re.sub(r"@[\w]+", "TOKMENTION", text)
# replace hashtag
text = re.sub(r"#[\w]+", "TOKHASHTAG", text)
# replace dollar
text = re.sub(r"\$\d+", "TOKDOLLAR", text)
# remove punctuation
text = re.sub("[^a-zA-Z0-9]", " ", text)
# remove multiple spaces
text = re.sub(r" +", " ", text)
# remove newline
text = re.sub(r"\n", " ", text)
cleaned_text.append(text)
return cleaned_text