-
Notifications
You must be signed in to change notification settings - Fork 1
/
preprocessTwitter.py
59 lines (47 loc) · 2.16 KB
/
preprocessTwitter.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
'''
-> Based on original Ruby Script for preprocessing tweets by Romain Paulus
with small modifications by Jeffrey Pennington
-> Conversion from Ruby to Python script done by Kaggle user amackrane and accessible here:
https://www.kaggle.com/amackcrane/python-version-of-glove-twitter-preprocess-script
-> Further edits made by me based on idiosyncracies of the Golbeck et al. (2017) Twitter dataset
'''
import sys
import regex as re
FLAGS = re.MULTILINE | re.DOTALL
def hashtag(text):
text = text.group()
hashtag_body = text[1:]
if hashtag_body.isupper():
result = "<hashtag> {} <allcaps>".format(hashtag_body.lower())
else:
result = " ".join(["<hashtag>"] + re.split(r"(?=[A-Z])", hashtag_body, flags=FLAGS))
return result
def allcaps(text):
text = text.group()
return text.lower() + " <allcaps> " # amackcrane added trailing space
def tokenize(text):
# Different regex parts for smiley faces
eyes = r"[8:=;]"
nose = r"['`\-]?"
# function so code less repetitive
def re_sub(pattern, repl):
return re.sub(pattern, repl, text, flags=FLAGS)
text = re_sub(r"[:,!\?\.]?@\w+", "<user>") #Catch any character prefixes attached to <user>
text = re_sub(r"{}{}[)dD]+|[)dD]+{}{}".format(eyes, nose, nose, eyes), "<smile>")
text = re_sub(r"{}{}p+".format(eyes, nose), "<lolface>")
text = re_sub(r"{}{}\(+|\)+{}{}".format(eyes, nose, nose, eyes), "<sadface>")
text = re_sub(r"{}{}[\/|l*]".format(eyes, nose), "<neutralface>")
text = re_sub(r"/"," / ")
text = re_sub(r"<3","<heart>")
text = re_sub(r"[$-+:#]?[.\d]*[\d]+[:,.\d]*(st|s|rd|nd|th|a|%|\))*", "<number>") #I updated to catch more cases
text = re_sub(r"#\w+", hashtag) # amackcrane edit
text = re_sub(r"([!?.]){2,}", r"\1 <repeat>")
text = re_sub(r"\b(\S*?)(.)\2{2,}\b", r"\1\2 <elong>")
# amackcrane additions
text = re_sub(r"([a-zA-Z<>()])([?!.:;,])", r"\1 \2")
text = re_sub(r"\(([a-zA-Z<>]+)\)", r"( \1 )")
text = re_sub(r" ", r" ")
text = re_sub(r" ([A-Z]){2,} ", allcaps)
#My own addition
text = re_sub(r">-?<", r"> <") #Split up special characters that have no space between them
return text.lower()