-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean_tweet.py
250 lines (233 loc) · 6.7 KB
/
clean_tweet.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
#neccessary imports
from bs4 import BeautifulSoup
import itertools
import emoji
import re
import csv
#unlabeled data to python array(only tweets)
def getDataFromCSV(filename):
tweetsList = []
with open(filename, "r", newline="") as file:
reader = csv.reader(file, delimiter=",")
for row in reader:
tweetsList.append(row[0])
return tweetsList
def loadSmileys():
return {
":‑)":"smiley",
":-]":"smiley",
":-3":"smiley",
":->":"smiley",
"8-)":"smiley",
":-}":"smiley",
":)":"smiley",
":]":"smiley",
":3":"smiley",
":>":"smiley",
"8)":"smiley",
":}":"smiley",
":o)":"smiley",
":c)":"smiley",
":^)":"smiley",
"=]":"smiley",
"=)":"smiley",
":-))":"smiley",
":‑D":"smiley",
"8‑D":"smiley",
"x‑D":"smiley",
"X‑D":"smiley",
":D":"smiley",
"8D":"smiley",
"xD":"smiley",
"XD":"smiley",
":‑(":"sad",
":‑c":"sad",
":‑<":"sad",
":‑[":"sad",
":(":"sad",
":c":"sad",
":<":"sad",
":[":"sad",
":-||":"sad",
">:[":"sad",
":{":"sad",
":@":"sad",
">:(":"sad",
":'‑(":"sad",
":'(":"sad",
":‑P":"playful",
"X‑P":"playful",
"x‑p":"playful",
":‑p":"playful",
":‑Þ":"playful",
":‑þ":"playful",
":‑b":"playful",
":P":"playful",
"XP":"playful",
"xp":"playful",
":p":"playful",
":Þ":"playful",
":þ":"playful",
":b":"playful",
"<3":"love"
}
# self defined contractions
def loadContractions():
return {
"ain't":"is not",
"amn't":"am not",
"aren't":"are not",
"can't":"cannot",
"'cause":"because",
"couldn't":"could not",
"couldn't've":"could not have",
"could've":"could have",
"daren't":"dare not",
"daresn't":"dare not",
"dasn't":"dare not",
"didn't":"did not",
"doesn't":"does not",
"don't":"do not",
"e'er":"ever",
"em":"them",
"everyone's":"everyone is",
"finna":"fixing to",
"gimme":"give me",
"gonna":"going to",
"gon't":"go not",
"gotta":"got to",
"hadn't":"had not",
"hasn't":"has not",
"haven't":"have not",
"he'd":"he would",
"he'll":"he will",
"he's":"he is",
"he've":"he have",
"how'd":"how would",
"how'll":"how will",
"how're":"how are",
"how's":"how is",
"I'd":"I would",
"I'll":"I will",
"I'm":"I am",
"I'm'a":"I am about to",
"I'm'o":"I am going to",
"isn't":"is not",
"it'd":"it would",
"it'll":"it will",
"it's":"it is",
"I've":"I have",
"kinda":"kind of",
"let's":"let us",
"mayn't":"may not",
"may've":"may have",
"mightn't":"might not",
"might've":"might have",
"mustn't":"must not",
"mustn't've":"must not have",
"must've":"must have",
"needn't":"need not",
"ne'er":"never",
"o'":"of",
"o'er":"over",
"ol'":"old",
"oughtn't":"ought not",
"shalln't":"shall not",
"shan't":"shall not",
"she'd":"she would",
"she'll":"she will",
"she's":"she is",
"shouldn't":"should not",
"shouldn't've":"should not have",
"should've":"should have",
"somebody's":"somebody is",
"someone's":"someone is",
"something's":"something is",
"that'd":"that would",
"that'll":"that will",
"that're":"that are",
"that's":"that is",
"there'd":"there would",
"there'll":"there will",
"there're":"there are",
"there's":"there is",
"these're":"these are",
"they'd":"they would",
"they'll":"they will",
"they're":"they are",
"they've":"they have",
"this's":"this is",
"those're":"those are",
"'tis":"it is",
"'twas":"it was",
"wanna":"want to",
"wasn't":"was not",
"we'd":"we would",
"we'd've":"we would have",
"we'll":"we will",
"we're":"we are",
"weren't":"were not",
"we've":"we have",
"what'd":"what did",
"what'll":"what will",
"what're":"what are",
"what's":"what is",
"what've":"what have",
"when's":"when is",
"where'd":"where did",
"where're":"where are",
"where's":"where is",
"where've":"where have",
"which's":"which is",
"who'd":"who would",
"who'd've":"who would have",
"who'll":"who will",
"who're":"who are",
"who's":"who is",
"who've":"who have",
"why'd":"why did",
"why're":"why are",
"why's":"why is",
"won't":"will not",
"wouldn't":"would not",
"would've":"would have",
"y'all":"you all",
"you'd":"you would",
"you'll":"you will",
"you're":"you are",
"you've":"you have",
"Whatcha":"What are you",
"luv":"love",
"sux":"sucks"
}
def tweet_cleaning_for_sentiment_analysis(tweet):
#-----------------cleaning-data------------------------
#Escaping HTML characters
tweet = BeautifulSoup(tweet, features = "html.parser").get_text()
tweet = tweet.replace('\x92',"'")
#Removal of hastags/account
tweet = ' '.join(re.sub("(@[A-Za-z0-9]+)|(#[A-Za-z0-9]+)", " ", tweet).split())
#Removal of address
tweet = ' '.join(re.sub("(\w+:\/\/\S+)", " ", tweet).split())
#Removal of Punctuation
tweet = ' '.join(re.sub("[\.\,\!\?\:\;\-\=\\\]", " ", tweet).split())
#Lower case
tweet = tweet.lower()
#Removing contraction_ source wikipedia contraction list
contractions = loadContractions()
tweet = tweet.replace("’","'")
words = tweet.split()
reformed = [contractions[word] if word in contractions else word for word in words]
tweet = " ".join(reformed)
# Standardizing words
tweet = ''.join(''.join(s)[:2] for _, s in itertools.groupby(tweet))
#Deal with emoticons source wikipedia emoticons list
smilies = loadSmileys()
words = tweet.split()
reformed = [smilies[word] if word in smilies else word for word in words]
tweet = " ".join(reformed)
#Deal with emojis
tweet = emoji.demojize(tweet)
tweet = tweet.replace(":"," ")
tweet = ' '.join(tweet.split())
return tweet