-
Notifications
You must be signed in to change notification settings - Fork 2
/
tweet_cleaner.py
208 lines (188 loc) · 4.67 KB
/
tweet_cleaner.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
#!/usr/bin/python
# -*- coding: <utf-8> -*-
def clean_tweet(tweet_after_split):
return split_slashes(split_commas(split_sgl_qt(split_dbl_qt(split_ques(split_excl(split_periods(tweet_after_split)))))))
def split_periods(tweet_after_split):
final_tweet = []
new_tweet = []
for word in tweet_after_split:
split_words = word.split('..')
for word in split_words:
if not len(word) > 0:
pass
else:
new_tweet.append(word)
for word in new_tweet:
split_words = word.split('.')
for word in split_words:
if not len(word) > 0:
pass
else:
final_tweet.append(word)
return final_tweet
def split_excl(split_tweet):
new_tweet = []
final_tweet = []
for word in split_tweet:
split_words = word.split('!!')
for word in split_words:
if not len(word) > 0:
pass
else:
new_tweet.append(word)
for word in new_tweet:
split_words = word.split('!')
for word in split_words:
if not len(word) > 0:
pass
else:
final_tweet.append(word)
return final_tweet
def split_ques(split_tweet):
new_tweet = []
final_tweet = []
for word in split_tweet:
split_words = word.split('??')
for word in split_words:
if not len(word) > 0:
pass
else:
new_tweet.append(word)
for word in new_tweet:
split_words = word.split('?')
for word in split_words:
if not len(word) > 0:
pass
else:
final_tweet.append(word)
return final_tweet
def split_dbl_qt(split_tweet):
new_tweet = []
for word in split_tweet:
split_words = word.split('\"')
for word in split_words:
if not len(word) > 0:
pass
else:
new_tweet.append(word)
return new_tweet
def split_sgl_qt(split_tweet):
new_tweet = []
for word in split_tweet:
split_words = word.split('\'')
for word in split_words:
if not len(word) > 0:
pass
else:
new_tweet.append(word)
return new_tweet
def split_commas(split_tweet):
new_tweet = []
final_tweet = []
for word in split_tweet:
split_words = word.split(',,')
for word in split_words:
if not len(word) > 0:
pass
else:
new_tweet.append(word)
for word in new_tweet:
split_words = word.split(',')
for word in split_words:
if not len(word) > 0:
pass
else:
final_tweet.append(word)
return final_tweet
def split_slashes(split_tweet):
final_tweet = []
new_tweet = []
for word in split_tweet:
split_words = word.split('\\\\')
for word in split_words:
if not len(word) > 0:
pass
else:
new_tweet.append(word)
for word in new_tweet:
split_words = word.split('\\')
for word in split_words:
if not len(word) > 0:
pass
else:
final_tweet.append(word)
return final_tweet
def strip_word(word):
word = word.rstrip(',\"\'\\!.?-:<;~`-_=+[]{}()')
word = word.lstrip(',\"\'\\!.?-:<;~`-_=+[]{}()')
return word
def check_beg_nums(word):
beg_num = ['@', '$', '#', ':', ';', '>', '&', '^','0','1','2','3','4','5','6','7','8','9']
if word[0] in beg_num:
word = 0
if word != 0:
for element in beg_num:
if element in word:
word = 0
break
return word
def check_meta(word):
if word != 0:
meta = ['RT', 'RTs', 'RT\'s', 'R/T', 'rt', 'rt\'s', 'rts', 'r/t', 'HT', 'H/T','ht', 'h/t', 'MT', 'M/T', 'mt', 'm/t', 'CR', 'C/R', 'cr', 'c/r']
if word in meta:
word = 0
if word != 0:
for element in meta:
if element in word:
word = 0
break
return word
def check_non_words(word):
if word != 0:
non_words = ['jeje', 'haha', 'mwah', 'bwah', 'muah', 'buah', 'http', 'teeh', 'jeej', 'jaja', 'JEJE', 'HAHA', 'MWAH', 'BWAH', 'MUAH', 'BUAH', 'HTTP', 'TEEH', 'JEEJ', 'JAJA']
if word[:4] in non_words:
word = 0
if word != 0:
if len(word) > 4:
for element in non_words:
if element in word:
word = 0
break
return word
def check_emoticons(word):
if word != 0:
emoticons = [':-D', ':-)', ':-(', ':-P', ';-D', ';-P', ':\'-(', ':\'(', '-_-', '>_>', 'o.o', 'O.o', '0.o', 'O.O', '0.0', 'o.O', 'o.0', 'o_o', 'O_o', '0_o', 'O_O', '0_0', 'o_O', 'o_0']
if word in emoticons:
word = 0
if len(word) > 3:
for element in emoticons:
if element in word:
word = 0
break
return word
def check_possessives(word):
if word != 0:
if word[-1] == 's' and word[-2] == '\'':
word = 0
elif word[-1] == 's' and word[-2] == '\"':
word = 0
return word
def check_letters(word):
if word != 0:
for letter in word:
if ord(letter) > 128:
word = 0
break
return word
def check_mult_lett(word):
if word != 0:
length = 0
while length < len(word)-2:
if word[length] == word[length+1] and word[length] == word[length+2]:
word = 0
break
else:
length += 1
return word
def check_words(word):
return check_mult_lett(check_letters(check_possessives(check_emoticons(check_non_words(check_meta(check_beg_nums(word)))))))