-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreprocessor.py
228 lines (189 loc) · 6.54 KB
/
Preprocessor.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
import spacy
import textblob
import wordsegment
import wordsegment
import re
import html.entities
from PosTagger import PosTagger
import emoji
from textblob import TextBlob
import json
global sp
def set_up():
sp = spacy.load('en_core_web_sm')
wordsegment.load()
wordsegment.UNIGRAMS['bitcoin'] = 9e10
wordsegment.UNIGRAMS['btc'] = 9e10
wordsegment.UNIGRAMS['ath'] = 1.3e7
wordsegment.UNIGRAMS['ethereum'] = 2e8
wordsegment.UNIGRAMS['eth'] = 2e8
wordsegment.UNIGRAMS['cryptocurrency'] = 3e8
wordsegment.UNIGRAMS['blockchain'] = 3.2e8
wordsegment.UNIGRAMS['altcoin'] = 5e6
wordsegment.UNIGRAMS['binance'] = 1.9e8
wordsegment.UNIGRAMS['cryptocurrencies'] = 3e8
wordsegment.UNIGRAMS['usdt'] = 1e7
wordsegment.UNIGRAMS['dogecoin'] = 1e8
wordsegment.UNIGRAMS['hodl'] = 9e5
wordsegment.UNIGRAMS['usdt'] = 1.9e8
wordsegment.UNIGRAMS['altcoins'] = 5e6
wordsegment.UNIGRAMS['memecoin'] = 2e5
wordsegment.UNIGRAMS['ai'] = 5e8
wordsegment.UNIGRAMS['nft'] = 2.8e8
wordsegment.UNIGRAMS['inu'] = 1e8
wordsegment.UNIGRAMS['htf'] = 1e6
wordsegment.UNIGRAMS['ltf'] = 1e6
wordsegment.UNIGRAMS['bnb'] = 1e6
wordsegment.UNIGRAMS['ta'] = 2e5
wordsegment.UNIGRAMS['defi'] = 2e8
def normalize_text(tweets):
rep = {"&": "&",
">": ">",
"<": "<",
"\u2026": "...",
"\u00a3": "£",
"\ufe0f": "",
"\u2013": "-",
"\u201c": "'",
"\u201d": "'",
"\u2019": "'",
"\u20bf": "₿",
"\U0001f1fa": "",
"\U0001f3fc": "",
"\ud83c\udffb": "",
"\ud83c\udffe": ""}
rep = dict((re.escape(k), v) for k, v in rep.items())
pattern = re.compile("|".join(rep.keys()))
translated_tweets = [pattern.sub(lambda m: rep[re.escape(m.group(0))], tweet) for tweet in tweets]
return translated_tweets
def pos_tagging(sentence):
for token in sentence:
sen = sp(token)
for w in sen:
print(sen, w, spacy.explain(w.tag_))
def viterbi_pos_tagging():
pt = PosTagger()
PosTagger.tag(pt)
def segment_hashtags(tweet):
transformed_tweet = ""
for word in tweet.split():
if word[0] == '#' or (word[0] == '$' and not has_numbers(word)):
segments = segment_text(word)
for x in segments:
transformed_tweet = transformed_tweet + " " + x
else:
transformed_tweet = transformed_tweet + " " + word
return transformed_tweet
def has_numbers(text):
return any(char.isdigit() for char in text)
def segment_text(tweet):
return wordsegment.segment(tweet)
def clean_mentions_urls(tweet):
clean_tweet = ""
for word in tweet.split():
if word[0] == '@':
clean_tweet = clean_tweet + " " + "@USER"
elif word[0:4] == 'http':
clean_tweet = clean_tweet + " " + "HTTPURL"
else:
clean_tweet = clean_tweet + " " + word
return clean_tweet
def translate_emojis(tweet):
if not emoji.emoji_lis(tweet):
return tweet
else:
clean_tweet = ""
for word in tweet.split():
emoji_locations = [e['location'] for e in emoji.emoji_lis(word)]
if emoji_locations:
for index in range(len(word)):
if index in emoji_locations:
translation = emoji.demojize(word[index])
clean_tweet = clean_tweet + " " + translation + " "
else:
clean_tweet = clean_tweet + word[index]
else:
clean_tweet = clean_tweet + " " + word + " "
return clean_tweet
def translate_abbreviations_slang(tweet):
rep = {" ath ": " all time high ",
" hodl ": " hold on for dear life ",
" htf ": " higher time frame ",
" ltf ": " lower time frame ",
"btc": " bitcoin ",
"BTC": " Bitcoin ",
" ada ": " cardano ",
" ADA ": " Cardano",
" eth ": " ethereum ",
" ETH ": " Ethereum ",
" usdt ": " tether ",
" USDT ": " Tether",
"xrp": " ripple ",
"XRP": " Ripple ",
" doge ": " dogecoin ",
" DOGE ": " Dogecoin",
"bnb": " binance ",
"BNB": " Binance",
" sol ": " solana ",
" SOL ": " Solana ",
" ta ": " technical analysis ",
" ann ": " announcement ",
" avg ": " average ",
" apr ": " april ",
" bpi ": " bitcoin price index ",
" bro ": " brother ",
" cmon ": " come on ",
" Im ": " I'm ",
" macd ": " moving average convergence divergence indicator",
" stfu ": " shut the fuck up",
" wtf ": " what the fuck ",
" mfs ": " motherfuckers ",
" ur ": " your ",
" af ": " as fuck ",
" algo ": " algorithm ",
" uber ": " very ",
" mkt ": " market ",
" chg ": " change "}
rep = dict((re.escape(k), v) for k, v in rep.items())
pattern = re.compile("|".join(rep.keys()))
translated_tweet = pattern.sub(lambda m: rep[re.escape(m.group(0))], tweet)
prices = [m.span() for m in re.finditer(' \d+k', translated_tweet)]
offset = 0
for i in prices:
pos = i[-1]-1 + offset
new_char = '.000 '
temp = list(translated_tweet)
temp[pos] = new_char
translated_tweet = "".join(temp)
offset += 4
prices2 = [m.span() for m in re.finditer('\d+[,|.]?\d*k', translated_tweet)]
offset = 0
for i in prices2:
pos = i[-1] - 1 + offset
new_char = '00 '
temp = list(translated_tweet)
temp[pos] = new_char
translated_tweet = "".join(temp)
offset += 3
return translated_tweet
def spelling_correction(tweet):
return TextBlob(tweet).correct()
def correct_spacing(tweet):
return tweet.replace(" ", " ")
def process_data():
jsonfile = open('LabeledData/LabeledTweets.json', 'r')
values = json.load(jsonfile)
jsonfile.close()
X = values['X']
y = values['y']
x_clean = normalize_text(
[correct_spacing(
translate_abbreviations_slang(
segment_hashtags(
translate_emojis(
clean_mentions_urls(tweet))))) for tweet in X])
data = {'X': x_clean, 'y': y}
f = open("LabeledData/LabeledProcessedTweets.json", 'w')
f.write(json.dumps(data, indent=0, sort_keys=True))
f.close()
return data