-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvocab_generator.py
427 lines (383 loc) · 14.5 KB
/
vocab_generator.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
import numpy as np
from cPickle import load, dump
from collections import defaultdict, Counter
import sys, re
import pandas as pd
import gzip
import cPickle
import codecs
_PAD = "_PAD"
_GO = "_GO"
_EOS = "_EOS"
_UNK = "_UNK"
_UNK_LOC = ["_UNK_LOC"]
_START_VOCAB = [_PAD, _GO, _EOS, _UNK]
class DataBuilder():
def __init__(self):
self.dc = DataCleaner()
def build_train_data(self,data_folder, cv=10, clean_string=False):
"""
Loads data and split into 10 folds by default.
"""
revs = []
vocab = defaultdict(float)
print data_folder
with codecs.open( data_folder, 'rb') as fi:
for line in fi.readlines():
line = line.decode('utf-8')
parts = line.split("\n")[0].split("\t")
if len(parts) > 1:
sent = parts[1]
rev = []
rev.append(sent.strip())
if clean_string:
orig_rev = self.dc.clean_str(" ".join(rev))
else:
orig_rev = " ".join(rev).lower()
#print orig_rev
words = set(orig_rev.split())
for word in words:
vocab[word.lower()] += 1
if len(orig_rev.split()) < 50 :
datum = {"y":int(parts[0]),
"text": orig_rev,
"num_words": len(orig_rev.split()),
"split": np.random.randint(0,cv)}
revs.append(datum)
# else:
# print orig_rev
return revs, vocab
def build_eval_data(self,data, num_classes, clean_string=False):
"""
Loads data and split into 10 folds by default.
"""
revs = []
num = [-1, 1]
for line in data:
line = line.decode('utf-8')
sent = line
rev = []
rev.append(sent.strip())
if clean_string:
orig_rev = self.dc.clean_str(" ".join(rev))
else:
orig_rev = " ".join(rev).lower()
#print orig_rev
datum = {"y":num[np.random.randint(0, num_classes)],
"text": orig_rev,
"num_words": len(orig_rev.split()),
}
revs.append(datum)
return revs
def build_data_cv(self, data_folder, cv=10, clean_string=True):
"""
Loads data and split into 10 folds.
"""
revs = []
pos_file = data_folder[0]
neg_file = data_folder[1]
vocab = defaultdict(float)
with open(pos_file, "rb") as f:
for line in f:
rev = []
rev.append(line.strip())
if clean_string:
orig_rev = self.dc.clean_str(" ".join(rev))
else:
orig_rev = " ".join(rev).lower()
words = set(orig_rev.split())
for word in words:
vocab[word] += 1
datum = {"y":1,
"text": orig_rev,
"num_words": len(orig_rev.split()),
"split": np.random.randint(0,cv)}
revs.append(datum)
with open(neg_file, "rb") as f:
for line in f:
rev = []
rev.append(line.strip())
if clean_string:
orig_rev = self.dc.clean_str(" ".join(rev))
else:
orig_rev = " ".join(rev).lower()
words = set(orig_rev.split())
for word in words:
vocab[word] += 1
datum = {"y":0,
"text": orig_rev,
"num_words": len(orig_rev.split()),
"split": np.random.randint(0,cv)}
revs.append(datum)
return revs, vocab
def build_data(self, data_folder, cv=10, clean_string=False):
"""
Loads data and split into 10 folds.
"""
revs = []
# pos_file = loadmodel(data_folder[0])
# neg_file = loadmodel(data_folder[1])
pos_texts = loadmodel(data_folder[0]).get("content")
neg_texts = loadmodel(data_folder[1]).get("content")
vocab = defaultdict(float)
happyList = [ ":-)", ":)", ":D", ":o)", ":]", ":3", ":c)", ":>", "=]", "8)", "=)", ":}", ":^)", ":?)", ":-)", ": )", ": D", ": o)", ":]", ": 3", ":c)", ":>", "= ]", "8 )", "= )", ": }", ":^)", ":?)" ]
sadList = [ ">:[", ":-(", ":(", ":-c", ":c", ":-<", ":?C", ":<", ":-[", ":[", ":{",">:[", ":-(", ": (", ":-c", ": c", ": -<", ": ?C", ": <", ": -[", ": [", ": {" ]
for line in pos_texts:
rev = []
rev.append(line.strip())
if clean_string:
orig_rev = self.dc.clean_str(" ".join(rev))
else:
orig_rev = " ".join(rev).lower()
#print orig_rev
words = set(orig_rev.split())
for word in words:
if word in happyList or word in sadList:
pass
else:
vocab[word] += 1
datum = {"y":1,
"text": orig_rev,
"num_words": len(orig_rev.split()),
"split": np.random.randint(0,cv)}
revs.append(datum)
for line in neg_texts:
rev = []
rev.append(line.strip())
if clean_string:
orig_rev = self.dc.clean_str(" ".join(rev))
else:
orig_rev = " ".join(rev).lower()
words = set(orig_rev.split())
for word in words:
if word in happyList or word in sadList:
pass
else:
vocab[word] += 1
datum = {"y":0,
"text": orig_rev,
"num_words": len(orig_rev.split()),
"split": np.random.randint(0,cv)}
revs.append(datum)
return revs, vocab
def get_idx_from_sent(self, sent, word_idx_map, max_l=45, k=300, filter_h=5):
"""
Transforms sentence into a list of indices. Pad with zeroes.
"""
x = []
pad = filter_h - 1
# for i in xrange(pad):
# x.append(0)
words = sent.split()
for word in words:
if word in word_idx_map:
x.append(word_idx_map[word])
if len(x)==max_l+pad:
break
while len(x) < max_l+2*pad:
x.append(0)
return x
def get_idx_from_sent_rnn(self, sent, word_idx_map, max_l=45):
"""
Transforms sentence into a list of indices. Pad with zeroes.
"""
x = []
mask = None
pad =1
for i in xrange(pad):
x.append(0)
words = sent.split()
for word in words:
if word in word_idx_map:
x.append(word_idx_map[word])
if len(x)==max_l+pad:
break
mask = len(x)
while len(x) < max_l+2*pad:
x.append(0)
return x, mask
def get_idx_from_sent_brnn(self, sent, word_idx_map, max_l=45):
"""
Transforms sentence into a list of indices. Pad with zeroes.
"""
x = []
rev_x = []
mask = None
pad =1
for i in xrange(pad):
x.append(0)
rev_x.append(0)
words = sent.split()
for word in words:
if word in word_idx_map:
x.append(word_idx_map[word])
rev_x.append(word_idx_map[word])
if len(x)==max_l+pad:
break
mask = len(x)
rev_x.append(0)
rev_x.reverse()
while len(rev_x) < max_l+2*pad:
rev_x.append(0)
while len(x) < max_l+2*pad:
x.append(0)
return x,rev_x, mask
def get_idx_from_sent_rnn_mask(self, sent, word_idx_map, max_l=45, k=300, filter_h=5):
"""
Transforms sentence into a list of indices. Pad with zeroes.
"""
x = []
mask = None
pad = 1
for i in xrange(pad):
x.append(0)
words = sent.split()
for word in words:
if word in word_idx_map:
x.append(word_idx_map[word])
mask.append(1)
while len(x) < max_l+2*pad:
x.append(0)
mask.append(0)
return x, mask
def make_idx_data_cv(self, revs, word_idx_map, cv, max_l=59, k=300, filter_h=5):
"""
Transforms sentences into a 2-d matrix.
"""
train, test = [], []
for rev in revs:
sent = self.get_idx_from_sent(rev["text"], word_idx_map, max_l, k, filter_h)
sent.append(rev["y"])
if rev["split"]==cv:
test.append(sent)
else:
train.append(sent)
train = np.array(train,dtype="int")
test = np.array(test,dtype="int")
return [train, test]
class Distributional_Representation():
def __init__(self):
None
def get_W(self,word_vecs, k=300):
"""
Get word matrix. W[i] is the vector for word indexed by i
"""
vocab_size = len(word_vecs)
word_idx_map = dict()
W = np.zeros(shape=(vocab_size+1, k), dtype='float32')
W[0] = np.zeros(k, dtype='float32')
i = 1
for word in word_vecs:
W[i] = word_vecs[word]
word_idx_map[word] = i
i += 1
return W, word_idx_map
def load_bin_vec(self, fname, vocab):
"""
Loads 300x1 word vecs from Google (Mikolov) word2vec
"""
word_vecs = {}
with open(fname, "rb") as f:
header = f.readline()
print header
vocab_size, layer1_size = map(int, header.split())
binary_len = np.dtype('float32').itemsize * layer1_size
for line in xrange(vocab_size):
word = []
while True:
ch = f.read(1)
if ch == ' ':
word = ''.join(word)
break
if ch != '\n':
word.append(ch)
if word in vocab:
word_vecs[word] = np.fromstring(f.read(binary_len), dtype='float32')
# logger.info(word_vecs[word])
else:
f.read(binary_len)
# logger.info("num words already in word2vec: " + str(len(word_vecs)))
return word_vecs
def add_unknown_words(self, word_vecs, vocab, min_df=3, k=300):
"""
For words that occur in at least min_df documents, create a separate word vector.
0.25 is chosen so the unknown vectors have (approximately) same variance as pre-trained ones
"""
for word in vocab:
if word not in word_vecs and vocab[word] >= min_df:
word_vecs[word] = np.random.uniform(-0.25,0.25,k)
class DataCleaner():
def __init__(self):
None
def clean_str(string):
"""
Tokenization/string cleaning for all datasets except for SST.
Every dataset is lower cased except for TREC
"""
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(r"\s{2,}", " ", string)
return string.strip().lower()
def clean_str_sst(string):
"""
Tokenization/string cleaning for the SST dataset
"""
string = re.sub(r"[^A-Za-z0-9(),!?\'\`]", " ", string)
string = re.sub(r"\s{2,}", " ", string)
return string.strip().lower()
def loadmodel(fname):
""" Load model from fname
"""
if not fname.endswith('.pickle.gz'):
fname = fname + '.pickle.gz'
with gzip.open(fname, 'r') as fin:
D = load(fin)
print 'Load model from file: {}'.format(fname)
return D
class Build_Model_Data():
def __init__(self):
None
def test_run(self, w2v_file, data_file, save_file,max_vocabulary_size = 80000):
print "loading data...",
db = DataBuilder()
revs, vocab = db.build_train_data(data_file, cv=10, clean_string=False)
# vocab = vocab.most_common(max_vocabulary_size)
# if len(vocab_list) > max_vocabulary_size:
# vocab = vocab_list[:max_vocabulary_size]
max_l = np.max(pd.DataFrame(revs)["num_words"])
print "data loaded!"
print "number of sentences: " + str(len(revs))
print "vocab size: " + str(len(vocab))
print "max sentence length: " + str(max_l)
print "loading word2vec vectors...",
dist_rep = Distributional_Representation()
w2v = dist_rep.load_bin_vec(w2v_file, vocab)
print "word2vec loaded!"
print "num words already in word2vec: " + str(len(w2v))
dist_rep.add_unknown_words(w2v, vocab)
W, word_idx_map = dist_rep.get_W(w2v)
print len(W)
print word_idx_map
# print "Complete Random Representation"
# rand_vecs = {}
# dist_rep.add_unknown_words(rand_vecs, vocab)
# R, _ = dist_rep.get_W(rand_vecs)
cPickle.dump([revs, W, W , word_idx_map, vocab, max_l], open(save_file, "wb"))
print "dataset created!"
if __name__=="__main__":
max_vocabulary_size = 80000
w2v_file = "/Users/Parry/Downloads/GoogleNews-vectors-negative300.bin"
data_file = "data/all_final_kk.data.txt"
save_file = "test.p"
bm = Build_Model_Data()
bm.test_run(w2v_file, data_file, save_file,max_vocabulary_size = max_vocabulary_size)