-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvocab.py
225 lines (177 loc) · 5.97 KB
/
vocab.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
from __future__ import print_function
from __future__ import division
import pandas as pd
import numpy as np
import tensorflow as tf
import sys
import os
from nltk.tokenize import RegexpTokenizer
import pickle
import string
import csv
def save(obj, filename):
print('Saving to {}'.format(filename))
with open(filename, 'w') as f:
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
def maybe_load(filename):
if os.path.isfile(filename):
with open(filename, 'rb') as f:
print('Restoring {}'.format(filename))
return pickle.load(f)
return None
def clean_sentence(sentences):
tokenizer = RegexpTokenizer(r'\w+')
printable = set(string.printable)
print('Cleaning and tokenizing')
for i in range(len(sentences)):
if i % 1000 == 0:
print("Step {}/{}".format(i, len(sentences)))
sentences[i] = tokenizer.tokenize(
filter(lambda x: x in printable, sentences[i]))
for x in range(len(sentences[i])):
sentences[i][x] = sentences[i][x].lower().strip('=\\|/.,?!')
return sentences
def get_and_clean_data(data):
save_file = 'data/cleaned.pickle'
restore = maybe_load(save_file)
if (restore is None):
sentences = data['comment_text'].fillna('CVxTz').values
sentences = clean_sentence(sentences)
save(sentences, save_file)
return sentences
else:
return restore
def get_word_count(sentences):
save_file = 'data/word_count.pickle'
restore = maybe_load(save_file)
if (restore is None):
print('Counting words')
word_count = {}
for i in range(len(sentences)):
if i % 1000 == 0:
print("Step {}/{}".format(i, len(sentences)))
for word in sentences[i]:
if not word in word_count:
word_count[word] = 0
word_count[word] += 1
save(word_count, save_file)
return word_count
else:
return restore
def get_word2index(word_count):
save_file = 'data/word2index.pickle'
restore = maybe_load(save_file)
if (restore is None):
print('Sorting Dictionary')
word_count_list = sorted(
word_count.iteritems(), key=lambda x: x[1], reverse=True)
word_count_list = word_count_list[:max_features]
word2index = {}
i = 1
for k in word_count_list:
if i % 1000 == 0:
print("Step {}/{}".format(i, len(word_count_list)))
word2index[k[0]] = i
i += 1
save(word2index, save_file)
return word2index
else:
return restore
def convert_training_data(word2index, features, dataset):
save_file = 'data/features.pickle'
restore = maybe_load(save_file)
if (restore is None):
print('Converting words to indeces')
labels = np.zeros((len(features), len(possible_labels)))
for i in range(len(features)):
if i % 1000 == 0:
print("Step {}/{}".format(i, len(features)))
for x in range(len(features[i])):
if features[i][x] in word2index:
features[i][x] = word2index[features[i][x]]
else:
features[i][x] = UNKNOWN
# Labels
for k in range(len(possible_labels)):
labels[:, k] = dataset[possible_labels[k]].values
save_obj = {'features': features, 'labels': labels}
save(save_obj, save_file)
return features, labels
else:
return restore['features'], restore['labels']
def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=value))
def save_to_binary_file(output_filename, text, labels):
assert (text.shape[0] == labels.shape[0])
count = 0
with tf.python_io.TFRecordWriter(output_filename) as writer:
for i in range(text.shape[0]):
if i % 1000 == 0:
print('Processed data: {}/{}'.format(i, text.shape[0]))
sys.stdout.flush()
feature = {
'text': _int64_feature(text[i]),
'labels': _int64_feature(labels[i])
}
example = tf.train.Example(features=tf.train.Features(feature=feature))
writer.write(example.SerializeToString())
count += 1
sys.stdout.flush()
print('Wrote {}/{} datapoints'.format(count, text.shape[0]))
def resize(text, maxlen, empty):
resized = np.zeros([text.shape[0], maxlen])
resized.fill(empty)
for i in range(len(text)):
if len(text[i]) > maxlen:
resized[i, :] = text[i][:maxlen]
else:
resized[i, :len(text[i])] = text[i][:]
return resized
max_features = 50000
UNKNOWN = 0
EMPTY = max_features + 1
possible_labels = [
'toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate'
]
maxlen = 30
def update():
dataset = pd.read_csv('data/train.csv')
sentences = get_and_clean_data(dataset)
word_count = get_word_count(sentences)
word2index = get_word2index(word_count)
return dataset, sentences, word_count, word2index
if __name__ == '__main__':
dataset, sentences, word_count, word2index = update()
# Write embedding mappings.
with open('output/embedding_mapping.tsv', 'wb') as csv_file:
writer = csv.writer(csv_file, delimiter='\t')
for key, value in word2index.items():
writer.writerow([key, value])
text, labels = convert_training_data(word2index, sentences, dataset)
labels = labels.astype(np.int64)
print('Text, Labels')
print(text, labels)
print(text.shape, labels.shape)
train_ratio = 0.9
valid_ratio = 0.05
test_ratio = 0.05
size = text.shape[0]
train_end = int(train_ratio * size)
valid_end = int(valid_ratio * size) + train_end
test_end = int(test_ratio * size) + valid_end
train_text = text[:train_end]
train_labels = labels[:train_end]
print(train_text, train_text.shape)
valid_text = text[train_end:valid_end]
valid_labels = labels[train_end:valid_end]
test_text = text[valid_end:test_end]
test_labels = labels[valid_end:test_end]
train_file = 'data/train.tfrecords'
valid_file = 'data/valid.tfrecords'
test_file = 'data/test.tfrecords'
if not os.path.isfile(train_file):
save_to_binary_file(train_file, train_text, train_labels)
if not os.path.isfile(valid_file):
save_to_binary_file(valid_file, valid_text, valid_labels)
if not os.path.isfile(test_file):
save_to_binary_file(test_file, test_text, test_labels)