-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathpreprocessing.py
336 lines (263 loc) · 13.6 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
from collections import Counter
import nltk
import pandas as pd
from emoticons import EmoticonDetector
import re as regex
import numpy as np
class TwitterData:
data = []
processed_data = []
wordlist = []
ngrams = []
whitelist = ["n't", "not"]
data_model = None
data_labels = None
is_testing = False
def initialize(self, csv_file, is_testing_set=False, from_cached=None):
if from_cached is not None:
self.data_model = pd.read_csv(from_cached)
return
self.is_testing = is_testing_set
if not is_testing_set:
self.data = pd.read_csv(csv_file, header=0, names=["id", "emotion", "text"])
self.data = self.data[self.data["emotion"].isin(["positive", "negative", "neutral"])]
else:
self.data = pd.read_csv(csv_file, header=0, names=["id", "text"])
not_null_text = 1 ^ pd.isnull(self.data["text"])
not_null_id = 1 ^ pd.isnull(self.data["id"])
self.data = self.data.loc[not_null_id & not_null_text, :]
self.processed_data = self.data
self.wordlist = []
self.data_model = None
self.data_labels = None
def cleanup(self, cleanuper):
t = self.processed_data
for cleanup_method in cleanuper.iterate():
if not self.is_testing:
t = cleanup_method(t)
else:
if cleanup_method.__name__ != "remove_na":
t = cleanup_method(t)
self.processed_data = t
def build_features(self):
def count_by_lambda(expression, word_array):
return len(list(filter(expression, word_array)))
def count_occurences(character, word_array):
counter = 0
for j, word in enumerate(word_array):
for char in word:
if char == character:
counter += 1
return counter
def count_by_regex(regex, plain_text):
return len(regex.findall(plain_text))
self.add_column("splitted_text", map(lambda txt: txt.split(" "), self.processed_data["text"]))
# number of uppercase words
uppercase = list(map(lambda txt: count_by_lambda(lambda word: word == word.upper(), txt),
self.processed_data["splitted_text"]))
self.add_column("number_of_uppercase", uppercase)
# number of !
exclamations = list(map(lambda txt: count_occurences("!", txt),
self.processed_data["splitted_text"]))
self.add_column("number_of_exclamation", exclamations)
# number of ?
questions = list(map(lambda txt: count_occurences("?", txt),
self.processed_data["splitted_text"]))
self.add_column("number_of_question", questions)
# number of ...
ellipsis = list(map(lambda txt: count_by_regex(regex.compile(r"\.\s?\.\s?\."), txt),
self.processed_data["text"]))
self.add_column("number_of_ellipsis", ellipsis)
# number of hashtags
hashtags = list(map(lambda txt: count_occurences("#", txt),
self.processed_data["splitted_text"]))
self.add_column("number_of_hashtags", hashtags)
# number of mentions
mentions = list(map(lambda txt: count_occurences("@", txt),
self.processed_data["splitted_text"]))
self.add_column("number_of_mentions", mentions)
# number of quotes
quotes = list(map(lambda plain_text: int(count_occurences("'", [plain_text.strip("'").strip('"')]) / 2 +
count_occurences('"', [plain_text.strip("'").strip('"')]) / 2),
self.processed_data["text"]))
self.add_column("number_of_quotes", quotes)
# number of urls
urls = list(map(lambda txt: count_by_regex(regex.compile(r"http.?://[^\s]+[\s]?"), txt),
self.processed_data["text"]))
self.add_column("number_of_urls", urls)
# number of positive emoticons
ed = EmoticonDetector()
positive_emo = list(
map(lambda txt: count_by_lambda(lambda word: ed.is_emoticon(word) and ed.is_positive(word), txt),
self.processed_data["splitted_text"]))
self.add_column("number_of_positive_emo", positive_emo)
# number of negative emoticons
negative_emo = list(map(
lambda txt: count_by_lambda(lambda word: ed.is_emoticon(word) and not ed.is_positive(word), txt),
self.processed_data["splitted_text"]))
self.add_column("number_of_negative_emo", negative_emo)
pass
def add_column(self, column_name, column_content):
self.processed_data.loc[:, column_name] = pd.Series(column_content, index=self.processed_data.index)
def stem(self, stemmer=nltk.PorterStemmer()):
def stem_and_join(row):
row["text"] = list(map(lambda str: stemmer.stem(str.lower()), row["text"]))
return row
self.processed_data = self.processed_data.apply(stem_and_join, axis=1)
def tokenize(self, tokenizer=nltk.word_tokenize):
def tokenize_row(row):
row["text"] = tokenizer(row["text"])
row["tokenized_text"] = [] + row["text"]
return row
self.processed_data = self.processed_data.apply(tokenize_row, axis=1)
def build_wordlist(self, min_occurrences=5, max_occurences=500, stopwords=nltk.corpus.stopwords.words("english"),
whitelist=None):
self.wordlist = []
whitelist = self.whitelist if whitelist is None else whitelist
import os
if os.path.isfile("data\\wordlist.csv"):
word_df = pd.read_csv("data\\wordlist.csv")
word_df = word_df[word_df["occurrences"] > min_occurrences]
self.wordlist = list(word_df.loc[:, "word"])
return
words = Counter()
for idx in self.processed_data.index:
words.update(self.processed_data.loc[idx, "text"])
for idx, stop_word in enumerate(stopwords):
if stop_word not in whitelist:
del words[stop_word]
word_df = pd.DataFrame(data={"word": [k for k, v in words.most_common() if min_occurrences < v < max_occurences],
"occurrences": [v for k, v in words.most_common() if min_occurrences < v < max_occurences]},
columns=["word", "occurrences"])
word_df.to_csv("data\\wordlist.csv", index_label="idx")
self.wordlist = [k for k, v in words.most_common() if min_occurrences < v < max_occurences]
def build_ngrams(self, ngram=2, stopwords=nltk.corpus.stopwords.words("english"),
whitelist=None):
whitelist = self.whitelist if whitelist is None else whitelist
stopwords = list(filter(lambda sw: sw not in whitelist, stopwords))
ngrams = Counter()
for idx in self.processed_data.index:
tokens = self.processed_data.loc[idx, "text"]
ngrams.update(self.generate_ngrams(tokens, ngram, stopwords))
self.ngrams = [ng for ng, cnt in ngrams.most_common() if cnt >= 2]
def generate_ngrams(self, tokens, ngram, stopwords):
return list(map(lambda ng: str.join("_", ng),
nltk.ngrams(
filter(lambda word: word not in stopwords, tokens),
ngram)))
def build_ngram_model(self, stopwords=nltk.corpus.stopwords.words("english"),
whitelist=None, ngram=2):
whitelist = self.whitelist if whitelist is None else whitelist
stopwords = list(filter(lambda sw: sw not in whitelist, stopwords))
extra_columns = [col for col in self.processed_data.columns if col.startswith("number_of")]
label_column = []
if not self.is_testing:
label_column = ["label"]
columns = label_column + extra_columns + list(self.ngrams)
labels = []
rows = []
for idx in self.processed_data.index:
current_row = []
if not self.is_testing:
# add label
current_label = self.processed_data.loc[idx, "emotion"]
labels.append(current_label)
current_row.append(current_label)
for _, col in enumerate(extra_columns):
current_row.append(self.processed_data.loc[idx, col])
# add ngrams
tokens = self.processed_data.loc[idx, "text"]
current_ngrams = self.generate_ngrams(tokens, ngram, stopwords)
for _, ng in enumerate(self.ngrams):
current_row.append(1 if ng in current_ngrams else 0)
rows.append(current_row)
self.data_model = pd.DataFrame(rows, columns=columns)
self.data_labels = pd.Series(labels)
return self.data_model, self.data_labels
def build_word2vec_model(self, word2vec_provider, stopwords=nltk.corpus.stopwords.words("english"), whitelist=None):
whitelist = self.whitelist if whitelist is None else whitelist
stopwords = list(filter(lambda sw: sw not in whitelist, stopwords))
extra_columns = [col for col in self.processed_data.columns if col.startswith("number_of")]
similarity_columns = ["bad_similarity", "good_similarity", "information_similarity"]
label_column = []
if not self.is_testing:
label_column = ["label"]
columns = label_column + ["original_id"] + extra_columns + similarity_columns + list(
map(lambda i: "word2vec_{0}".format(i), range(0, word2vec_provider.dimensions))) + list(
map(lambda w: w + "_bow",self.wordlist))
labels = []
rows = []
for idx in self.processed_data.index:
current_row = []
if not self.is_testing:
# add label
current_label = self.processed_data.loc[idx, "emotion"]
labels.append(current_label)
current_row.append(current_label)
current_row.append(self.processed_data.loc[idx, "id"])
for _, col in enumerate(extra_columns):
current_row.append(self.processed_data.loc[idx, col])
# average similarities with words
tokens = self.processed_data.loc[idx, "tokenized_text"]
for main_word in map(lambda w: w.split("_")[0], similarity_columns):
current_similarities = [abs(sim) for sim in
map(lambda word: word2vec_provider.get_similarity(main_word, word.lower()), tokens) if
sim is not None]
if len(current_similarities) <= 1:
current_row.append(0 if len(current_similarities) == 0 else current_similarities[0])
continue
max_sim = max(current_similarities)
min_sim = min(current_similarities)
current_similarities = [((sim - min_sim) / (max_sim - min_sim)) for sim in
current_similarities] # normalize to <0;1>
current_row.append(np.array(current_similarities).mean())
# add word2vec vector
tokens = self.processed_data.loc[idx, "tokenized_text"]
current_word2vec = []
for _, word in enumerate(tokens):
vec = word2vec_provider.get_vector(word.lower())
if vec is not None:
current_word2vec.append(vec)
averaged_word2vec = list(np.array(current_word2vec).mean(axis=0))
# averaged_word2vec = map(lambda avg: (avg if abs(avg) > 0.0001 else 0), averaged_word2vec)
current_row += averaged_word2vec
# add bag-of-words
tokens = set(self.processed_data.loc[idx, "text"])
for _, word in enumerate(self.wordlist):
current_row.append(1 if word in tokens else 0)
rows.append(current_row)
self.data_model = pd.DataFrame(rows, columns=columns)
self.data_labels = pd.Series(labels)
return self.data_model, self.data_labels
def build_data_model(self, with_ngram=None):
extra_columns = [col for col in self.processed_data.columns if col.startswith("number_of")]
label_column = []
if not self.is_testing:
label_column = ["label"]
columns = label_column + extra_columns + list(
map(lambda w: w + "_bow",self.wordlist))
if with_ngram is not None:
columns += list(self.ngrams)
labels = []
rows = []
for idx in self.processed_data.index:
current_row = []
if not self.is_testing:
# add label
current_label = self.processed_data.loc[idx, "emotion"]
labels.append(current_label)
current_row.append(current_label)
for _, col in enumerate(extra_columns):
current_row.append(self.processed_data.loc[idx, col])
# add tokens
tokens = set(self.processed_data.loc[idx, "text"])
for _, word in enumerate(self.wordlist):
current_row.append(1 if word in tokens else 0)
if with_ngram is not None:
current_ngrams = self.generate_ngrams(self.processed_data.loc[idx, "text"], with_ngram, [])
for _, ng in enumerate(self.ngrams):
current_row.append(1 if ng in current_ngrams else 0)
rows.append(current_row)
self.data_model = pd.DataFrame(rows, columns=columns)
self.data_labels = pd.Series(labels)
return self.data_model, self.data_labels