-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset_loader.py
461 lines (352 loc) · 17.2 KB
/
dataset_loader.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
##########################################################
# Main Utilization Module for Loading/Saving On Demand
# [Train/Test] Datasets with preprocessing & cleaning
# Datasets: Sentiment140, ImDB, Sarcasm, Kaggle
##########################################################
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
warnings.filterwarnings("ignore", category=UserWarning)
# ______________________WARNINGS_______________________________
import os
import numpy as np
import pandas as pd
import random
import re, pickle
import zipfile
from matplotlib import pyplot
from abc import abstractmethod
from tqdm import tqdm
import sys
from helper_functions import *
stopWords = None
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
from PIL import Image
# Dataset Hash Dictionary
def dataset_loader(dataset):
try:
return {
'imdb' : imdb(),
'sarcasm' : sarcasm(),
'semeval2016' : semeval2016()
}[dataset]
except KeyError:
print('Dataset: {}, Not Found!'.format(dataset))
# Main Loader
class loader_:
@classmethod
def assign_dataset(cls, dataset):
cls.dataset = dataset
@classmethod
def assign_devset(cls, devset):
cls.devset = devset
@classmethod
def load_devset(cls):
with open('./pickled/datasets/{}/{}.pkl'.format(cls.__name__,'dev_set'), 'rb') as f:
return pickle.load(f)
@classmethod
def save_devset(cls):
with open('./pickled/datasets/{}/{}.pkl'.format(cls.__name__,'dev_set'), 'wb') as f:
pickle.dump(cls.devset, f, pickle.HIGHEST_PROTOCOL)
@classmethod
def load_dataset(cls, name):
with open('./pickled/datasets/{}/{}.pkl'.format(cls.__name__, name), 'rb') as f:
return pickle.load(f)
@classmethod
def save_dataset(cls, name):
with open('./pickled/datasets/{}/{}.pkl'.format(cls.__name__, name), 'wb') as f:
pickle.dump(cls.dataset, f, pickle.HIGHEST_PROTOCOL)
@abstractmethod
def create_devset(cls, size, *args):
raise NotImplementedError("Please Implement this method")
@abstractmethod
def polarity_distribution(self):
raise NotImplementedError("Please Implement this method")
def plot_wordcloud(self, sarcasm=False):
devset = pd.DataFrame(self.load_devset())
#Define Stop Words
stopwords = set(STOPWORDS)
stopwords.remove('no')
stopwords.remove('not')
stopwords.update(['tomorrow','will', 'th',
'st', 'see','go', 'may','sunday', 'today',
'saturday', 'tonight',
'today', 'think', 'say', 'day', 'now',
'make', 'want', 'time', 'know', 'film', 'movie'])
def polarity_plot(polarity_, dataset, sarcasm=False):
sentences = devset['x_data'][devset.y_labels == polarity_].tolist()
text = ' '.join(map(str, sentences))
mask = np.array(Image.open("images/positive_mask.png")) if polarity_ else np.array(Image.open("images/negative_mask.png"))
if not sarcasm:
polarity = 'positive' if polarity_ else 'negative'
else:
polarity = 'sarcastic' if polarity_ else 'neutral'
mask = np.array(Image.open("images/negative_mask.png")) if polarity_ else np.array(Image.open("images/positive_mask.png"))
wordcloud_model = WordCloud(stopwords=stopwords, background_color= 'white', mode="RGBA", max_words=1000, mask=mask).generate(text)
image_colors = ImageColorGenerator(mask)
# plt.imshow(wordcloud_usa.recolor(color_func=image_colors), interpolation="bilinear")
plt.imsave('images/{0}_{1}.png'.format(dataset, polarity), wordcloud_model.recolor(color_func=image_colors))
if self.__class__.__name__ == 'sentiment140':
polarity_plot(polarity_ = 4, dataset = self.__class__.__name__, sarcasm=sarcasm)
else:
polarity_plot(polarity_ = 1, dataset = self.__class__.__name__, sarcasm=sarcasm)
polarity_plot(polarity_ = 0, dataset = self.__class__.__name__, sarcasm=sarcasm)
def plot_frequency(self, pos_freq, neg_freq, labels, bins, dataset, position):
assert(labels is not None and dataset is not None and bins is not None)
bins = bins
plt.style.use('seaborn-deep')
plt.hist([pos_freq, neg_freq], bins, label=labels, histtype='bar', color=['mediumseagreen','indianred'])
plt.legend(loc='upper right') if position=='right' else plt.legend(loc='upper left')
plt.xlabel('Word Density')
plt.ylabel('Sentence Count')
plt.title('{}'.format(dataset))
plt.savefig('images/FPlot_{}'.format(self.__class__.__name__))
plt.show()
class kaggle(loader_):
def __init__(self):
self.dset_loc = './Datasets/twitter_kaggle'
self.clean_reviews = list()
self.reviews = list()
self.df = pd.read_csv('{}/{}'.format(self.dset_loc, 'train.csv'),
encoding='latin-1',
names=['ItemID', 'Sentiment', 'SentimentText'],
usecols=['Sentiment', 'SentimentText']
)
def create_devset(self, size, *args):
pos_df = self.df[self.df.Sentiment == '1'][:size]
neg_df = self.df[self.df.Sentiment == '0'][:size]
pos_df['clean_tweet'] = pos_df.SentimentText.apply(clean_text)
neg_df['clean_tweet'] = neg_df.SentimentText.apply(clean_text)
pos_df['word_count'] = pos_df.clean_tweet.apply(word_count)
neg_df['word_count'] = neg_df.clean_tweet.apply(word_count)
pos_df = pos_df.drop(pos_df[pos_df.word_count < 3].index)
pos_df = pos_df[pos_df.word_count > 3]
neg_df = neg_df[neg_df.word_count > 3]
if pos_df.shape[0] > size:
print('Pos Is bigger')
pos_df = pos_df[:size]
if neg_df.shape[0] > size:
print('Neg Is bigger')
neg_df = neg_df[:size]
Y_labels = pos_df.Sentiment.tolist() + neg_df.Sentiment.tolist()
X_data = pos_df.clean_tweet.tolist() + neg_df.clean_tweet.tolist()
word_count = pos_df.word_count.tolist() + neg_df.word_count.tolist()
dev_dict = {"x_data": X_data,
"y_labels": Y_labels,
"word_count" : word_count}
super().assign_devset(dev_dict)
self.save_devset()
def polarity_distribution(self):
devset = pd.DataFrame(self.load_devset())
devset['word_count'] = devset.x_data.apply(word_count)
sarc_freq = devset['word_count'][devset.Sentiment == 1]
neut_freq = devset['word_count'][devset.Sentiment == 0]
self.plot_frequency(sarc_freq, neut_freq,
labels=['sarcastic', 'neutral'],
dataset = 'kaggle')
class imdb(loader_):
def __init__(self):
self.dset_loc = './Datasets/imdb_reviews/'
self.REPLACE_NO_SPACE = re.compile("(\.)|(\;)|(\:)|(\!)|(\?)|(\,)|(\")|(\()|(\))|(\[)|(\])")
self.REPLACE_WITH_SPACE = re.compile("(<br\s*/><br\s*/>)|(\-)|(\/)")
self.clean_reviews = list()
self.reviews = list()
def preprocess_reviews(self):
self.reviews = [self.REPLACE_NO_SPACE.sub("", line.lower()) for line in self.reviews]
self.reviews = [self.REPLACE_WITH_SPACE.sub(" ", line) for line in self.reviews]
with open('./pickled/datasets/imdb/{}.pkl'.format('reviews2'), 'wb') as f:
pickle.dump(self.reviews, f, pickle.HIGHEST_PROTOCOL)
def create_devset(self, size=2500, *args):
with open('./pickled/datasets/imdb/{}.pkl'.format('reviews1'), 'rb') as file:
reviews = pickle.load(file)
assert(len(reviews) == 25000)
pos_reviews = reviews[:size]
neg_reviews = reviews[-size:]
pos_labels = [1 for value in pos_reviews]
neg_labels = [0 for value in neg_reviews]
df = pd.DataFrame(columns=['sentences', 'sentiment'])
df.sentences = pos_reviews + neg_reviews
df.sentiment = pos_labels + neg_labels
df['word_count'] = df.sentences.apply(word_count)
df['sentences'] = df.sentences.apply(clean_text)
df = df.drop(df[df.word_count < 3].index)
dev_dict = {"x_data": df.sentences.tolist(),
"y_labels": df.sentiment.tolist(),
'word_count': df.word_count.tolist()}
super().assign_devset(dev_dict)
self.save_devset()
def polarity_distribution(self):
devset = pd.DataFrame(self.load_devset())
if 'word_count' not in devset:
devset['word_count'] = devset.x_data.apply(word_count)
pos_freq = devset['word_count'][devset.y_labels == 1]
neg_freq = devset['word_count'][devset.y_labels == 0]
self.plot_frequency(pos_freq, neg_freq,
['Positive Sentences', 'Negative Sentences'],
range(20, 120, 7), 'IMDB Movie Reviews', 'left')
class semeval2016(loader_):
def __init__(self):
self.dname = 'twitter_dataframe'
self.dloc = './Datasets/Twitter_Dataset[2013_2016].txt'
def extract_dataset(self):
df = pd.read_csv(self.dloc ,index_col=None, sep='\t', header=None, names=['id','sentiment','text'])
df = df.drop_duplicates()
df['text']= df['text'].apply(clean_text)
df['word_count'] = df['text'].apply(word_count)
df = df.drop('id', 1)
df = df.drop(df[df.word_count < 3].index)
super().assign_dataset(df)
self.save_dataset(self.dname)
return self
def create_devset(self, split=7500):
df = self.load_dataset(self.dname)
if split == 0:
return df.text.tolist(), df.sentiment.tolist()
else:
df_pos = df[df.sentiment == 'positive'][:split]
df_neg = df[df.sentiment == 'negative'][:split]
pos_labels = [1 for value in df_pos['sentiment'].values.tolist()]
neg_labels = [0 for value in df_pos['sentiment'].values.tolist()]
pos_text = df_pos['text'].values.tolist()
neg_text = df_neg['text'].values.tolist()
Y_labels = pos_labels + neg_labels
X_data = pos_text + neg_text
word_count = df_pos.word_count.tolist() + df_neg.word_count.tolist()
dev_dict = {"x_data": X_data,
"y_labels": Y_labels,
"word_count": word_count}
super().assign_devset(dev_dict)
self.save_devset()
def polarity_distribution(self):
devset = pd.DataFrame(self.load_devset())
devset['word_count'] = devset.x_data.apply(word_count)
pos_freq = devset['word_count'][devset.y_labels == 1]
neg_freq = devset['word_count'][devset.y_labels == 0]
self.plot_frequency(pos_freq, neg_freq,
['Positive Sentences', 'Negative Sentences'],
range(3, 35, 2), 'Twitter: SemEval[2016]', 'left')
class sarcasm(loader_):
def __init__(self):
self.dloc = './Datasets/{}/train-balanced-sarcasm.csv'.format(self.__class__.__name__)
self.df = None
self.devset = None
def clean_dataset(self):
try:
self.df = self.load_devset('sarcasm_dataframe')
except:
self.df = pd.read_csv(self.dloc, encoding='latin1', usecols=['comment', 'label'])
self.df.dropna(inplace=True)
self.df['clean_comment'] = self.df['comment'].apply(clean_text)
self.df.drop(columns=['comment'], axis=1)
self.df['word_count'] = self.df['clean_comment'].apply(word_count)
self.df = self.df[self.df.word_count > 3]
super().assign_dataset(self.df)
self.save_dataset('dataframe_sarcasm[cleaned]')
def create_devset(self, size, *args):
try:
self.df = self.load_dataset('sarcasm_cleaned')
except:
print("Dataset dataframe_sarcasm[cleaned] do not exist!!!")
sys.exit(-1)
rand = random.randrange(1000, 5000) * random.randrange(1,30)
df_neut = self.df[self.df.label == 0][rand: rand + size]
df_sarc = self.df[self.df.label == 1][rand: rand + size]
try:
if args and args[0]['stop_words']:
global stopWords
with open('./stop_words_custom.txt', encoding="Latin1") as file:
s_words = file.readlines()
stopWords = list(map(lambda word: re.sub('\n', '', word), s_words))
df_neut['clean_comment'] = df_neut['clean_comment'].apply(rstop_words)
df_sarc['clean_comment'] = df_sarc['clean_comment'].apply(rstop_words)
except KeyError:
pass
Y_labels = df_neut.label.values.tolist() + df_sarc.label.values.tolist()
X_data = df_neut.clean_comment.values.tolist() + df_sarc.clean_comment.values.tolist()
word_count = df_neut.word_count.tolist() + df_sarc.word_count.tolist()
dev_dict = {"x_data": X_data,
"y_labels": Y_labels,
"word_count": word_count}
super().assign_devset(dev_dict)
self.save_devset()
def polarity_distribution(self):
devset = pd.DataFrame(self.load_devset())
devset['word_count'] = devset.x_data.apply(word_count)
sarc_freq = devset['word_count'][devset.y_labels == 1]
neut_freq = devset['word_count'][devset.y_labels == 0]
self.plot_frequency(sarc_freq, neut_freq,
['Sarcastic Sentences', 'Neutral Sentences'],
range(3, 34, 2), 'Twitter: Sarcasm', 'right')
class sentiment140(loader_):
def __init__(self, file_path="./Datasets/sentiment140.zip", file='training.1600000.processed.noemoticon.csv'):
self.file_path = file_path
self.file = file
self.dataset = None
self.dev_set = None
def open_zipped(self):
try:
zf = zipfile.ZipFile(self.file_path)
df = pd.read_csv(zf.open(self.file),
encoding='latin-1',
names=['target', 'id', 'date', 'flag', 'user', 'text'],
usecols=['target', 'text']
)
except IOError as e:
logger.exception("File I/O error")
return df
def init_dataset(self, verbose=False):
assert(self.file_path is not None)
positives, negatives = list(),list()
# Open file path
df = self.__open_zipped()
df['clean_text'] = df['text'].apply(simple_clean)
df = df.drop(columns = ['text'])
df = df[df['clean_text'] != '']
df['word_count'] = df.clean_text.apply(word_count)
self.assign_dataset(df)
self.save_dataset('sentiment140_cleaned')
def create_corpus(self, test_chunk, filter=3):
self.dataset = self.load_dataset('sentiment140_cleaned')
assert(self.dataset is not None)
if filter != 0:
self.dataset = self.dataset[self.dataset.word_count > filter]
pos_frame = self.dataset[self.dataset.target == 4]
neg_frame = self.dataset[self.dataset.target == 0]
dev_neg = neg_frame[-test_chunk:]
dev_pos = pos_frame[-test_chunk:]
pos_frame = pos_frame[:-test_chunk]
neg_frame = neg_frame[:-test_chunk]
self.corpus = pos_frame.clean_text.values.tolist() + neg_frame.clean_text.values.tolist()
self.dev_set = [dev_neg, dev_pos]
super().assign_dataset(self.corpus)
self.save_dataset('sent140_corpus')
return self
def create_devset(self, _size, *args):
if self.dev_set == None:
self.dev_set = self.load_dataset('sentiment140_cleaned')
X_data, Y_labels = [],[]
neg_,pos_ = 0,4
try:
if args[0]['seed']:
chunk = random.choice(range(1, 4, 1)) * 10000
except KeyError:
chunk=0
except IndexError:
chunk=0
text_n = self.dev_set[self.dev_set.target==neg_].clean_text.tolist()
text_p = self.dev_set[self.dev_set.target==pos_].clean_text.tolist()
pol_n = self.dev_set[self.dev_set.target==neg_].target.tolist()
pol_p = self.dev_set[self.dev_set.target==pos_].target.tolist()
text = text_n[chunk:chunk + _size] + text_p[chunk: chunk + _size]
polarity = pol_n[chunk:chunk + _size] + pol_p[chunk: chunk + _size]
dev_dict = {"x_data": text,
"y_labels": polarity}
super().assign_devset(dev_dict)
self.save_devset()
def polarity_distribution(self):
self.devset = pd.DataFrame(self.load_devset())
self.devset['word_count'] = self.devset.x_data.apply(word_count)
pos_freq = self.devset['word_count'][self.devset.y_labels == 4]
neg_freq = self.devset['word_count'][self.devset.y_labels == 0]
self.plot_frequency(pos_freq, neg_freq,
['Positive Sentences', 'Negative Sentences'],
range(3, 35, 2), 'Twitter: Sentiment140', 'right')