-
Notifications
You must be signed in to change notification settings - Fork 2
/
preprocess.py
340 lines (259 loc) · 10.1 KB
/
preprocess.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
#coding: utf-8
from __future__ import print_function
import gzip
import os
import numpy
import theano
import ast
import pickle
from konlpy.tag import Mecab
import numpy as np
def train_prepare_data(seqs, labels, maxlen=None) :
# x: a list of sentences
lengths = [len(s) for s in seqs]
if maxlen is not None:
new_seqs = []
new_labels = []
new_lengths = []
for l, s, y in zip(lengths, seqs, labels):
if l < maxlen:
new_seqs.append(s)
new_labels.append(y)
new_lengths.append(l)
lengths = new_lengths
labels = new_labels
seqs = new_seqs
if len(lengths) < 1:
return None, None, None
n_samples = len(seqs)
maxlen = numpy.max(lengths)
x = numpy.zeros((maxlen, n_samples)).astype('int64')
x_mask = numpy.zeros((maxlen, n_samples)).astype(theano.config.floatX)
for idx, s in enumerate(seqs):
x[:lengths[idx], idx] = s
x_mask[:lengths[idx], idx] = 1.
return x, x_mask, labels
def prepare_data(seqs, labels, sentences, titles, imgs, maxlen=None) :
"""Create the matrices from the datasets.
This pad each sequence to the same lenght: the lenght of the
longuest sequence or maxlen.
if maxlen is set, we will cut all sequence to this maximum
lenght.
This swap the axis!
"""
# x: a list of sentences
lengths = [len(s) for s in seqs]
if maxlen is not None:
new_seqs = []
new_labels = []
new_sentences = []
new_imgs = []
new_lengths = []
for l, s, y, sents, img in zip(lengths, seqs, labels, sentences, imgs):
if l < maxlen:
new_seqs.append(s)
new_labels.append(y)
new_lengths.append(l)
new_sentences.append(sents)
new_imgs.append(img)
lengths = new_lengths
labels = new_labels
seqs = new_seqs
sentences = new_sentences
imgs = new_imgs
if len(lengths) < 1:
return None, None, None, None, None
n_samples = len(seqs)
maxlen = numpy.max(lengths)
x = numpy.zeros((maxlen, n_samples)).astype('int64')
x_mask = numpy.zeros((maxlen, n_samples)).astype(theano.config.floatX)
sentence = []
title = []
img = []
for idx, [s, sent, t, i] in enumerate(zip(seqs, sentences, titles, imgs)):
sentence.append(sent)
title.append(t)
img.append(i)
x[:lengths[idx], idx] = s
x_mask[:lengths[idx], idx] = 1.
return x, x_mask, labels, sentence, title, img
def get_dataset_file(dataset, default_dataset, origin):
'''Look for it as if it was a full path, if not, try local file,
if not try in the data directory.
Download dataset if it is not present
'''
data_dir, data_file = os.path.split(dataset)
if data_dir == "" and not os.path.isfile(dataset) :
# Check if dataset is in the data directory.
new_path = os.path.join(
os.path.split(__file__)[0],
"..",
"data",
dataset
)
if os.path.isfile(new_path) or data_file == default_dataset:
dataset = new_path
if (not os.path.isfile(dataset)) and data_file == default_dataset:
from six.moves import urllib
print('Downloading data from %s' % origin)
urllib.request.urlretrieve(origin, dataset)
return dataset
def load_predict_data(path='predict.txt', n_words=100000, maxlen=None,
sort_by_len=None) :
unknown_token = u"UNKNOWN_TOKEN"
mecab = Mecab()
fv = open('vocabulary.pkl', 'r')
vocabulary = pickle.load(fv)
def read_data(filename) :
with open(filename, 'r') as f :
data = [line.split('\t') for line in f.read().splitlines()]
data = data[0:]
return data
def tokenize(doc) :
doc = doc.decode('utf-8')
return ['/'.join(t) for t in mecab.pos(doc)]
predict_data = read_data(path)
x_list = []
y_list = []
sent_list = []
title_list = []
img_list = []
for i in range(len(predict_data)) :
sentence = []
token = tokenize(predict_data[i][1])
for t in token :
if t in vocabulary :
sentence.append(vocabulary[t])
else :
sentence.append(vocabulary[unknown_token])
if(np.sum(sentence) != 0) :
x_list.append(sentence)
title_list.append(predict_data[i][0])
sent_list.append(predict_data[i][1])
y_list.append(int(predict_data[i][2]))
img_list.append(predict_data[i][3])
fv.close()
#//////////////////////////////////////////////////////////////////////////
predict_set = (x_list, y_list, sent_list, title_list, img_list)
if maxlen:
new_predict_set_x = []
new_predict_set_y = []
new_predict_set_sent = []
new_predict_title = []
new_predict_img = []
for x, y, sent, title, img in zip(predict_set[0], predict_set[1], predict_set[2], predict_set[3], predict_set[4]) :
if len(x) < maxlen:
new_predict_set_x.append(x)
new_predict_set_y.append(y)
new_predict_set_sent.append(sent)
new_predict_title.append(title)
new_predict_img.append(img)
predict_set = (new_predict_set_x, new_predict_set_y, new_predict_set_sent, new_predict_title, new_predict_img)
del new_predict_set_x, new_predict_set_y, new_predict_set_sent, new_predict_title, new_predict_img
predict_set_x, predict_set_y, predict_set_sent, predict_set_title, predict_set_img = predict_set
def remove_unk(x):
return [[1 if w >= n_words else w for w in sen] for sen in x]
predict_set_x = remove_unk(predict_set_x)
def len_argsort(seq):
return sorted(range(len(seq)), key=lambda x: len(seq[x]))
if sort_by_len:
sorted_index = len_argsort(predict_set_x)
predict_set_x = [predict_set_x[i] for i in sorted_index]
predict_set_y = [predict_set_y[i] for i in sorted_index]
predict_set_sent = [predict_set_sent[i] for i in sorted_index]
predict_set_title = [predict_set_title[i] for i in sorted_index]
predict_set_img = [predict_set_img[i] for i in sorted_index]
predict = (predict_set_x, predict_set_y, predict_set_sent, predict_set_title, predict_set_img)
return predict
def load_data(n_words=100000, valid_portion=0.1, maxlen=None,
sort_by_len=True) :
unknown_token = u"UNKNOWN_TOKEN"
mecab = Mecab()
fv = open('vocabulary.pkl', 'r')
vocabulary = pickle.load(fv)
def read_data(filename) :
with open(filename, 'r') as f :
data = [line.split('\t') for line in f.read().splitlines()]
data = data[1:]
return data
def tokenize(doc) :
doc = doc.decode('utf-8')
return ['/'.join(t) for t in mecab.pos(doc)]
# [x][0] : id
# [x][1] : comment
# [x][2] : label
train_data = read_data('ratings_train.txt')
x_list = []
y_list = []
for i in range(len(train_data)) :
sentence = []
token = tokenize(train_data[i][1])
for t in token :
if t in vocabulary :
sentence.append(vocabulary[t])
else :
sentence.append(vocabulary[unknown_token])
if(np.sum(sentence) != 0) :
x_list.append(sentence)
y_list.append(int(train_data[i][2]))
test_data = read_data('ratings_test.txt')
test_x_list = []
test_y_list = []
for i in range(len(test_data)) :
sentence = []
token = tokenize(test_data[i][1])
for t in token :
if t in vocabulary :
sentence.append(vocabulary[t])
else :
sentence.append(vocabulary[unknown_token])
if(np.sum(sentence) != 0) :
test_x_list.append(sentence)
test_y_list.append(int(test_data[i][2]))
fv.close()
train_set = (x_list, y_list)
test_set = (test_x_list, test_y_list)
if maxlen:
new_train_set_x = []
new_train_set_y = []
for x, y in zip(train_set[0], train_set[1]) :
if len(x) < maxlen:
new_train_set_x.append(x)
new_train_set_y.append(y)
train_set = (new_train_set_x, new_train_set_y)
del new_train_set_x, new_train_set_y
# split training set into validation set
train_set_x, train_set_y = train_set
n_samples = len(train_set_x) # 2103
sidx = numpy.random.permutation(n_samples) # [0~n_samples) 숫자 random sequence로 list를 생성
n_train = int(numpy.round(n_samples * (1. - valid_portion)))
valid_set_x = [train_set_x[s] for s in sidx[n_train:]]
valid_set_y = [train_set_y[s] for s in sidx[n_train:]]
train_set_x = [train_set_x[s] for s in sidx[:n_train]]
train_set_y = [train_set_y[s] for s in sidx[:n_train]]
train_set = (train_set_x, train_set_y)
valid_set = (valid_set_x, valid_set_y)
def remove_unk(x):
return [[1 if w >= n_words else w for w in sen] for sen in x]
test_set_x, test_set_y = test_set
valid_set_x, valid_set_y = valid_set
train_set_x, train_set_y = train_set
train_set_x = remove_unk(train_set_x)
valid_set_x = remove_unk(valid_set_x)
test_set_x = remove_unk(test_set_x)
def len_argsort(seq):
return sorted(range(len(seq)), key=lambda x: len(seq[x]))
if sort_by_len:
sorted_index = len_argsort(test_set_x)
test_set_x = [test_set_x[i] for i in sorted_index]
test_set_y = [test_set_y[i] for i in sorted_index]
sorted_index = len_argsort(valid_set_x)
valid_set_x = [valid_set_x[i] for i in sorted_index]
valid_set_y = [valid_set_y[i] for i in sorted_index]
sorted_index = len_argsort(train_set_x)
train_set_x = [train_set_x[i] for i in sorted_index]
train_set_y = [train_set_y[i] for i in sorted_index]
train = (train_set_x, train_set_y)
valid = (valid_set_x, valid_set_y)
test = (test_set_x, test_set_y)
return train, valid, test