forked from chandangope/w266-finalproject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_utils.py
243 lines (194 loc) · 8.33 KB
/
data_utils.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
import os
import re
import pandas as pd
import numpy as np
import tensorflow as tf
from random import shuffle
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from collections import OrderedDict
class DataLoader(object):
"""Class to load data"""
def __init__(self, path="./data"):
assert(os.path.isdir(path))
self._path = path
def splitDataAndSave(self, data):
toxic_no = data.loc[data['toxic'] == 0]['comment_text'].tolist()
toxic_yes = data.loc[data['toxic'] == 1]['comment_text'].tolist()
vectorizer = TfidfVectorizer(stop_words='english', max_features=200, ngram_range=(1,1), analyzer='word')
vectorizer.fit(toxic_yes)
top_words_dict = vectorizer.vocabulary_
top_words_dict = OrderedDict(sorted(top_words_dict.items(), key=lambda t: t[1], reverse=True))
# toxic_no = [self.clean_str(sent) for sent in toxic_no]
# toxic_yes = [self.clean_str(sent) for sent in toxic_yes]
toxic_no = [self.preprocess_comment(sent, top_words_dict, max_length=80, neighborhood=2) for sent in toxic_no]
toxic_yes = [self.preprocess_comment(sent, top_words_dict, max_length=80, neighborhood=2) for sent in toxic_yes]
shuffle(toxic_no)
shuffle(toxic_yes)
dev_index = -1 * int(0.2 * float(len(toxic_no)))
toxic_no_train, toxic_no_dev = toxic_no[:dev_index], toxic_no[dev_index:]
print("toxic_no_train size:{0}, toxic_no_dev size:{1}".format(len(toxic_no_train), len(toxic_no_dev)))
outF = open("./data/toxic_no_train.txt", "w")
for item in toxic_no_train:
outF.write("%s\n" % item)
outF.close()
outF = open("./data/toxic_no_dev.txt", "w")
for item in toxic_no_dev:
outF.write("%s\n" % item)
outF.close()
dev_index = -1 * int(0.2 * float(len(toxic_yes)))
toxic_yes_train, toxic_yes_dev = toxic_yes[:dev_index], toxic_yes[dev_index:]
print("toxic_yes_train size:{0}, toxic_yes_dev size:{1}".format(len(toxic_yes_train), len(toxic_yes_dev)))
outF = open("./data/toxic_yes_train.txt", "w")
for item in toxic_yes_train:
outF.write("%s\n" % item)
outF.close()
outF = open("./data/toxic_yes_dev.txt", "w")
for item in toxic_yes_dev:
outF.write("%s\n" % item)
outF.close()
def readTrainTest(self, verbose=False):
"""
Read train.csv and test.csv.
Args:
verbose(optional): whether to print basic info while loading
Returns (train, test), where:
train: pandas dataframe holding train data
test: pandas dataframe holding test data
"""
train = pd.read_csv(self._path + '/train.csv', dtype={'id': object})
test = pd.read_csv(self._path + '/test.csv', dtype={'id': object})
if(verbose==True):
print("Loaded train.csv: Num rows = {0}, Num cols = {1}".format(train.shape[0], train.shape[1]))
print("Loaded test.csv: Num rows = {0}, Num cols = {1}".format(test.shape[0], test.shape[1]))
print("Train data column names: {0}".format(train.columns.get_values()))
print("Test data column names: {0}".format(test.columns.get_values()))
return (train, test)
def preprocess_comment(self, in_comment, top_words_dict, max_length, neighborhood=2):
in_comment = re.sub(r"[^A-Za-z0-9]", " ", in_comment)
in_comment = re.sub(" \d+", " NUM", in_comment)
in_comment = re.sub(r"\s{2,}", " ", in_comment)
in_words = in_comment.split()
if len(in_words) <= max_length:
return in_comment.strip().lower()
else:
triggers = []
for i,k in enumerate(top_words_dict.keys()):
if k in in_words:
index = in_words.index(k)
min_index = max(0, index-neighborhood)
max_index = min(len(in_words), index+neighborhood)
phrase = [w for w in in_words[min_index:max_index+1]]
triggers.append((min_index, max_index, phrase))
sorted_triggers = sorted(triggers, key=lambda t: t[0] , reverse=False)
triggers_count = 0
for c in sorted_triggers:
triggers_count += len(c[2])
nonTriggerRoom = max_length - triggers_count
out_comment = []
prev_end = 0
for c in sorted_triggers:
start = c[0]
end = c[1]
if prev_end < start and nonTriggerRoom > 0:
end_tmp = min(start, prev_end+nonTriggerRoom)
out_comment.extend(in_words[prev_end : end_tmp])
nonTriggerRoom -= end_tmp - prev_end
start = max(start, prev_end)
out_comment.extend(in_words[start:end])
prev_end = end
out_comment = out_comment[:max_length]
return ' '.join(out_comment).strip().lower()
def clean_str(self, string):
"""
Tokenization/string cleaning for all datasets except for SST.
Original taken from https://github.com/yoonkim/CNN_sentence/blob/master/process_data.py
"""
string = re.sub(r"[^A-Za-z0-9(),!?\'\`]", " ", string)
string = re.sub(r"\'s", " \'s", string)
string = re.sub(r"\'ve", " \'ve", string)
string = re.sub(r"n\'t", " n\'t", string)
string = re.sub(r"\'re", " \'re", string)
string = re.sub(r"\'d", " \'d", string)
string = re.sub(r"\'ll", " \'ll", string)
string = re.sub(r",", " , ", string)
string = re.sub(r"!", " ! ", string)
string = re.sub(r"\(", " \( ", string)
string = re.sub(r"\)", " \) ", string)
string = re.sub(r"\?", " \? ", string)
string = re.sub(" \d+", " NUM", string)
string = re.sub(r"\s{2,}", " ", string)
return string.strip().lower()
def readTinyData(self, verbose=False):
"""
Read tiny_data.csv
Args:
verbose(optional): whether to print basic info while loading
Returns (tiny_data), where:
tiny_data: pandas dataframe holding data
"""
tiny_data = pd.read_csv(self._path + '/tiny_data.csv', dtype={'id': object})
if(verbose==True):
print("Loaded tiny_data.csv: Num rows = {0}, Num cols = {1}".format(tiny_data.shape[0], tiny_data.shape[1]))
print("Column names: {0}".format(tiny_data.columns.get_values()))
return tiny_data
def batchGenerator(self, pd_dataframe, batch_size, num_epochs, verbose=False):
"""
Generate batches
Args:
'pd_dataframe': pd data frame of format - 'id' 'comment_text' 'toxic' 'severe_toxic' 'obscene' 'threat' 'insult', 'identity_hate'
'batch_size': number of rows in a batch
'num_epochs': number of epochs for which to generate
'verbose'(optional): whether to print some status messages like epoch completed etc.
Yields: a batch of 'comments','labels'
"""
#Convert pd dataframe to numpy array
data_np=pd_dataframe.values
comments = data_np[:, 1] #np array of shape [numrows-in-data,]
labels = data_np[:, 2:].astype(int) #np array of shape [numrows-in-data, numcols-in-labels]
dataset = tf.data.Dataset.from_tensor_slices((comments, labels))
dataset = dataset.shuffle(buffer_size=10000)
dataset = dataset.batch(batch_size)
iter = dataset.make_initializable_iterator()
el = iter.get_next()
with tf.Session() as sess:
for epoch in range(num_epochs):
sess.run(iter.initializer)
while True:
try:
b_comments, b_labels = sess.run(el)
if(b_comments.shape[0] != batch_size):
if(verbose==True):
print("\nInsufficient number of items in batch. Skipping...")
else:
yield b_comments, b_labels
except tf.errors.OutOfRangeError:
if(verbose==True):
print("*************Epoch {0} finished*************\n".format(epoch+1))
break
def testBatching(self, comments, labels, batch_size, num_epochs):
dataset = tf.data.Dataset.from_tensor_slices((comments, labels))
dataset = dataset.shuffle(buffer_size=1000)
dataset = dataset.batch(batch_size)
iter = dataset.make_initializable_iterator()
el = iter.get_next()
with tf.Session() as sess:
for epoch in range(num_epochs):
sess.run(iter.initializer)
while True:
try:
b_comments, b_labels = sess.run(el)
if(b_comments.shape[0] != batch_size):
print("\nInsufficient number of items in batch. Skipping...")
else:
print("\nGot batch...")
for c,l in zip(b_comments, b_labels):
print(c,l)
except tf.errors.OutOfRangeError:
print("*************Epoch {0} finished*************\n".format(epoch+1))
break
if __name__ == "__main__":
data_loader = DataLoader(path="./data")
train, test = data_loader.readTrainTest(verbose=True)
# cleaned = data_loader.clean_str( train['comment_text'][16] )
# print(cleaned)
data_loader.splitDataAndSave(train)