/
data_helper.py
552 lines (451 loc) · 13.6 KB
/
data_helper.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
from collections import Counter
import jieba.posseg as pseg
import re, itertools, jieba, glob, random
from tqdm import tqdm
import pandas as pd
from collections import OrderedDict
import numpy as np
from multiprocessing.pool import Pool
UNK = '<UNK>'
BOS = '<S>'
EOS = '</S>'
PAD = '<PAD>'
KEEP_VOCAB_SIZE = 100000
class NodeVocab(object):
def __init__(self):
self.node_size = 0
self.node2id = {}
self.id2node = []
self.pad_id = 0
self.bos_id = 1
self.eos_id = 2
self.unk_id = 3
def node_to_id(self, node):
return self.node2id.get(node, self.unk_id)
def id_to_node(self, cur_id):
return self.id2node[cur_id]
def do_encode(self, path, mode=None):
"""
:param path: list of nodes
:param mode: bos, eos, bos_eos
:return:
"""
ids = [self.node_to_id(w) for w in path]
if mode == "bos":
ids = [self.bos_id] + ids
elif mode == "eos":
ids = ids + [self.eos_id]
elif mode == "bos_eos":
ids = [self.bos_id] + ids + [self.eos_id]
sen_len = len(ids)
return ids, sen_len
def load(self, path):
self.id2node.clear()
self.node2id.clear()
i = 0
for f in open(path, "r", encoding="utf-8"):
node = f.strip()
self.id2node.append(node)
self.node2id[node] = i
i += 1
self.node_size = len(self.id2node)
print("current node size %d" % self.node_size)
def __len__(self):
return len(self.id2node)
def __getitem__(self, word):
return self.node2id.get(word, self.unk_id)
def __contains__(self, word):
return word in self.node2id
class Vocabulary(object):
def __init__(self):
self.vocab_size = 0
self.id2word = []
self.word2id = {}
self.tf = {}
self.idf = {}
self.tfidf = {}
self.pad_id = 0
self.bos_id = 1
self.eos_id = 2
self.unk_id = 3
self.pad = PAD
self.bos = BOS
self.eos = EOS
self.unk = UNK
def build(self, corpus, max_vocab_size=50000):
"""
:param corpus: tokenized_sentences
:param max_vocab_size:
:return:
"""
# tf-idf
counter = Counter(itertools.chain(*corpus))
self.tf = {x[0]: x[1] for x in counter.most_common(len(counter))}
document_freq_dict = {}
doc_num = len(corpus)
for doc in corpus:
word_set = set(doc)
for word in word_set:
document_freq_dict[word] = document_freq_dict.get(word, 0) + 1
for word in self.tf:
self.idf[word] = np.log(doc_num / document_freq_dict.get(word, 1))
self.tfidf[word] = self.tf[word] * self.idf[word]
# frequent words
items = counter.most_common(max_vocab_size)
self.id2word = [self.pad] + [self.bos] + [self.eos] + [self.unk] + [x[0] for x in items if x[0].strip()]
self.word2id = dict([(w, i) for i, w in enumerate(self.id2word)])
self.vocab_size = len(self.word2id)
freq_list = list(self.tf.values())
total_counts = sum(freq_list)
cur_counts = sum([x[1] for x in items])
percent = cur_counts / total_counts * 100
percent_95_to_cover = 0.95
percent_98_to_cover = 0.98
ss = pd.Series(freq_list)
min_count_10 = max(ss[ss >= 10].index)
min_count_5 = max(ss[ss >= 5].index)
percent_95_num = min(ss[(ss.cumsum() / total_counts) >= percent_95_to_cover].index)
percent_98_num = min(ss[(ss.cumsum() / total_counts) >= percent_98_to_cover].index)
print(
"""
Vocab Info:
%d word in total
%.2f freq stored in vocab
%d word cover %.2f freq
%d word cover %.2f freq
min word frequency 10 need %d words
min word frequency 5 need %d words
current vocab len %d, min freq %d
""" % (
len(counter), percent, percent_95_num, percent_95_to_cover,
percent_98_num, percent_98_to_cover,
min_count_10, min_count_5, self.vocab_size, counter[self.id2word[-1]]
))
def word_to_id(self, word):
return self.word2id.get(word, self.unk_id)
def id_to_word(self, cur_id):
return self.id2word[cur_id]
def do_encode(self, sentence, mode=None):
"""
:param sentence: list of words
:param mode: bos, eos, bos_eos
:return:
"""
ids = [self.word_to_id(w) for w in sentence]
if mode == "bos":
ids = [self.bos_id] + ids
elif mode == "eos":
ids = ids + [self.eos_id]
elif mode == "bos_eos":
ids = [self.bos_id] + ids + [self.eos_id]
sen_len = len(ids)
return ids, sen_len
def do_decode(self, tokenids):
"""
:param tokenids:
:return:
"""
return [self.id_to_word(x) for x in tokenids]
def do_decode_to_natural_sentence(self, tokenids):
"""
:param tokenids:
:return:
"""
tokenids = [x for x in tokenids if x not in (self.pad_id, self.bos_id)]
min_eos_index = tokenids.index(self.eos_id) if self.eos_id in tokenids else -1
if min_eos_index > 0:
tokenids = tokenids[:min_eos_index]
return self.do_decode(tokenids)
def write_vocab(self, path):
with open(path, "w", encoding="utf-8") as f:
for item in self.id2word:
print(item, file=f)
def dump(self, path):
with open(path, "w", encoding="utf-8") as f:
for word in self.id2word:
s = "%s\t%d\t%.6f\t%.6f\n" % (
word, self.tf.get(word, 0), self.idf.get(word, 0), self.tfidf.get(word, 0)
)
f.write(s)
def load(self, path, keep_words=160000):
self.id2word.clear()
self.word2id.clear()
self.tf.clear()
self.idf.clear()
self.tfidf.clear()
with open(path, "r", encoding="utf-8") as f:
for i, line in enumerate(f):
s = line.strip().split("\t")
if len(s) == 4:
word, tf, idf, tfidf = line.strip().split("\t")
tf, idf, tfidf = int(tf), float(idf), float(tfidf)
self.id2word.append(word)
self.word2id[word] = i
self.tf[word] = tf
self.idf[word] = idf
self.tfidf[word] = tfidf
if i == keep_words - 1:
break
elif len(s) == 1:
word = line.strip()
self.id2word.append(word)
self.word2id[word] = i
if i == keep_words - 1:
break
elif len(s) == 2:
word, tf = line.strip().split("\t")
tf = int(tf)
self.id2word.append(word)
self.word2id[word] = i
self.tf[word] = tf
if i == keep_words - 1:
break
self.vocab_size = len(self.id2word)
# assert self.id2word[self.pad_id] == self.pad
# assert self.id2word[self.bos_id] == self.bos
# assert self.id2word[self.eos_id] == self.eos
# assert self.id2word[self.unk_id] == self.unk
print("current vocab size %d" % self.vocab_size)
def __len__(self):
return len(self.id2word)
def __getitem__(self, word):
return self.word2id.get(word, self.unk_id)
def __contains__(self, word):
return word in self.word2id
class Tokenizer(object):
def __init__(self):
pass
@staticmethod
def cut(text):
return list(jieba.cut(text))
class DataHelper(object):
def __init__(self, vocabulary):
self.ptr = 0
self.vocab = vocabulary
self.tokenizer = Tokenizer()
def text_vectorize(self, texts):
with Pool(4) as pool:
cut_words_list = pool.map(self.tokenizer.cut, tqdm(texts))
pool.join()
encode_ids = []
lens = []
for cut_words in cut_words_list:
e, l = self.vocab.do_encode(cut_words)
encode_ids.append(e)
lens.append(l)
return encode_ids, lens
class NodeTable(object):
def __init__(self, embedding_path, vocab_path, edim):
self.edim = edim
self.embeddings, self.dictionary = self._load_vectors(embedding_path, vocab_path, self.edim)
def __getitem__(self, item):
if item in self.dictionary:
return self.embeddings[self.dictionary[item], :]
else:
return self.embeddings[self.dictionary[UNK], :]
def __contains__(self, item):
return item in self.dictionary
def unk_embedding(self):
return self[UNK]
def embedding_look_up(self, words):
"""
:return unk embedding if a word is not in vocabulary.
:param words:
:return: [timesteps, edim]
"""
embeddings = []
for word in words:
embeddings.append(
self[word]
)
return np.array(embeddings)
def embedding_look_up_iov(self, words):
"""
inside of vocabulary
:param words:
:return:
"""
embeddings = []
for word in words:
if word in self.dictionary:
embeddings.append(self[word])
return np.array(embeddings) if embeddings != [] else np.array([self.unk_embedding()])
def _load_vectors(self, epath, vpath, dim):
embeddings = []
dictionary = OrderedDict()
invalid = 0
word = [0] * 300
for i, line in enumerate(open(vpath, "r", encoding="utf-8")):
word[i] = line.strip()
for j, line in tqdm(enumerate(open(epath, "r", encoding="utf-8"))):
line = line.strip().split()
vector = [float(val) for val in line]
if len(vector) != dim:
invalid += 1
raise TypeError("wrong word embedding dim: %d" % len(vector))
if word[j] not in dictionary:
dictionary.setdefault(word[j], j)
embeddings.append(vector)
embeddings = np.array(embeddings, np.float32)
print("{0} embeddings loaded. embedding shape ({1},{2})".format(len(dictionary), embeddings.shape[0],
embeddings.shape[1]))
return embeddings, dictionary
class WordTable(object):
def __init__(self, embedding_path, edim, keep_words=160000):
self.edim = edim
self.embeddings, self.dictionary = self._load_vectors(embedding_path, self.edim, keep_words=keep_words)
def __getitem__(self, item):
if item in self.dictionary:
return self.embeddings[self.dictionary[item], :]
else:
return self.embeddings[self.dictionary[UNK], :]
def __contains__(self, item):
return item in self.dictionary
def unk_embedding(self):
return self[UNK]
def embedding_look_up(self, words):
"""
:return unk embedding if a word is not in vocabulary.
:param words:
:return: [timesteps, edim]
"""
embeddings = []
for word in words:
embeddings.append(
self[word]
)
return np.array(embeddings)
def embedding_look_up_iov(self, words):
"""
inside of vocabulary
:param words:
:return:
"""
embeddings = []
for word in words:
if word in self.dictionary:
embeddings.append(self[word])
return np.array(embeddings) if embeddings != [] else np.array([self.unk_embedding()])
@staticmethod
def _load_vectors(path, dim, keep_words=160000):
embeddings = []
dictionary = OrderedDict()
invalid = 0
i = 0
for n, line in tqdm(enumerate(open(path, "r", encoding="utf-8"))):
line = line.split("\t")
word = line[0]
vector = [float(val) for val in line[1].split()]
if len(vector) != dim:
invalid += 1
raise TypeError("wrong word embedding dim: %d" % len(vector))
if word not in dictionary:
dictionary.setdefault(word, i)
embeddings.append(vector)
i += 1
if i == keep_words:
break
embeddings = np.array(embeddings, np.float32)
print("{0} embeddings loaded. embedding shape ({1},{2})".format(len(dictionary), embeddings.shape[0],
embeddings.shape[1]))
return embeddings, dictionary
class CustomSegmentor(object):
def __init__(self):
# 匹配英文数字组合,或者纯粹数字组合
self.alnum_pattern = re.compile("[0-9]+|[0-9]+[a-zA-Z]+|[a-zA-Z]+[0-9]+")
def cut(self, text, clean=True):
words = pseg.cut(text)
clean_words = []
for word, pos in words:
if clean:
word = self.clean_rule(word, pos)
clean_words.append(word)
return clean_words
def clean_rule(self, word, pos):
if re.match(self.alnum_pattern, word) is not None:
return "alnum"
elif pos in ("nr", "nrfg"):
return "personname"
else:
return word
class IterDataset(object):
"""
Hold a iter data dataset.
"""
def __init__(self, filepattern, vocab, nepochs=3, test=False, shuffle_on_load=False,
tokenizer=None):
'''
filepattern = a glob string that specifies the list of files.
vocab = an instance of Vocabulary or UnicodeCharsVocabulary
reverse = if True, then iterate over tokens in each sentence in reverse
test = if True, then iterate through all data once then stop.
Otherwise, iterate forever.
shuffle_on_load = if True, then shuffle the sentences after loading.
'''
self._vocab = vocab
self._nepochs = nepochs
self._all_shards = glob.glob(filepattern)
print('Found %d shards at %s' % (len(self._all_shards), filepattern))
self._shards_to_choose = []
self._test = test
self._shuffle_on_load = shuffle_on_load
self._tokenizer = tokenizer
self._ids = self._load_random_shard()
def _choose_random_shard(self):
if len(self._shards_to_choose) == 0:
if self._nepochs > 0:
self._nepochs -= 1
self._shards_to_choose = list(self._all_shards)
random.shuffle(self._shards_to_choose)
else:
raise StopIteration
shard_name = self._shards_to_choose.pop()
return shard_name
def _load_random_shard(self):
"""Randomly select a file and read it."""
if self._test:
if len(self._all_shards) == 0:
# we've loaded all the data
# this will propogate up to the generator in get_batch
# and stop iterating
raise StopIteration
else:
shard_name = self._all_shards.pop()
else:
# just pick a random shard
shard_name = self._choose_random_shard()
ids = self._load_shard(shard_name)
self._i = 0
self._nids = len(ids)
return ids
def _load_shard(self, shard_name):
"""Read one file and convert to ids.
Args:
shard_name: file path.
Returns:
list of (features) tuples.
"""
print('Loading data from: %s' % shard_name)
shard_data = pd.read_csv(shard_name, sep="\t", engine='python')
shard_array_list = self._do_vec(shard_data)
if self._shuffle_on_load:
random.shuffle(shard_array_list)
print('Loaded %d samples.' % len(shard_array_list))
print('Finished loading')
return shard_array_list
def _do_vec(self, shard_data):
"""
normally shard_data is DataFrame or Json
:param shard_data:
:return: list
"""
agg = shard_data.groupby("doc")
return [agg_name for agg_name, _ in agg]
# return []
def get_sample(self):
while True:
if self._i == self._nids:
self._ids = self._load_random_shard()
ret = self._ids[self._i]
self._i += 1
yield ret