forked from claravania/subword-lstm-lm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
768 lines (701 loc) · 28.7 KB
/
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
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
#!/usr/bin/python
# Author: Clara Vania
import numpy as np
import os
import re
import sys
import random
import pickle
import collections
import operator
class TextLoader:
def __init__(self, args, train=True):
self.save_dir = args.save_dir
self.batch_size = args.batch_size
self.num_steps = args.num_steps
self.out_vocab_size = args.out_vocab_size
self.unit = args.unit
self.composition = args.composition
self.lowercase = args.lowercase
self.n = 3
self.use_all_morphemes = True
self.eos = args.eos
self.sos = args.sos
self.words_vocab_file = os.path.join(self.save_dir, "words_vocab.pkl")
if self.unit == "oracle":
self.lowercase = True
if self.unit != "word":
self.sub_vocab_file = os.path.join(self.save_dir, "sub_vocab.pkl")
# Variables
self.max_word_len = 0
self.subword_vocab_size = 0
self.word_vocab_size = 0
# Dictionaries and lists
self.char_to_id = dict()
self.word_to_id = dict()
self.unk_word_list = set()
self.unk_char_list = set()
if self.unit == "morpheme" or self.unit == "oracle":
self.max_morph_per_word = 0
self.morpheme_to_id = dict()
self.unk_morph_list = set()
if self.unit == "char-ngram":
self.max_ngram_per_word = 0
self.ngram_to_id = dict()
self.unk_ngram_list = set()
self.output_vocab = {}
self.unk_list = set()
if args.output_vocab_file:
self.output_vocab, self.unk_list = self.load_vocab_dict(args.output_vocab_file)
if train:
self.train_data = self.read_dataset(args.train_file)
self.dev_data = self.read_dataset(args.dev_file)
self.preprocess()
else:
self.load_preprocessed()
@staticmethod
def is_hyperlink(word):
keywords = ('www', 'http', 'html')
for key in keywords:
if key in word:
return True
return False
@staticmethod
def padding(arr, max_len, vocab):
"""
Padding a vector of characters or words
:param arr: array to be padded
:param start: start symbol, ex: <w> for character sequence
:param end: end symbol
:param max_len: maximum length for padding
:param vocab: vocabulary to get the indexes of start, end, and PAD symbols
:return:
"""
tmp = []
if len(arr) <= max_len:
tmp.extend([element for element in arr])
tmp.extend([vocab['<PAD>'] for _ in range(max_len - len(arr))])
else:
tmp.extend([element for element in arr[:max_len]])
return tmp
def preprocess(self):
"""
Preprocess dataset and build vocabularies
"""
self.word_to_id, self.unk_word_list = self.build_vocab(mode="word")
self.word_vocab_size = len(self.word_to_id)
self.max_word_len = self.get_max_word_length(self.word_to_id)
with open(self.words_vocab_file, 'wb') as f:
pickle.dump((self.word_to_id, self.unk_word_list), f)
if self.unit != "word":
self.preprocess_sub_units()
def preprocess_sub_units(self):
"""
Build dictionaries for sub word units
"""
if self.unit == "char":
self.preprocess_char()
elif self.unit == "char-ngram":
self.preprocess_char_ngram()
elif self.unit == "morpheme":
self.preprocess_morpheme()
elif self.unit == "oracle":
self.preprocess_oracle()
else:
sys.exit("Unknown unit")
def preprocess_char(self):
"""
Build dictionaries for character representation
"""
self.char_to_id, self.unk_char_list = self.build_vocab(mode="char")
self.subword_vocab_size = len(self.char_to_id)
with open(self.sub_vocab_file, 'wb') as f:
pickle.dump((self.char_to_id, self.unk_char_list, self.max_word_len), f)
def preprocess_char_ngram(self):
"""
Build dictionaries for char-ngram representation
"""
self.char_to_id, self.unk_char_list = self.build_vocab(mode="char")
self.ngram_to_id, self.unk_ngram_list, self.max_ngram_per_word = self.build_ngram_vocab(self.n)
for ch in self.char_to_id:
if ch not in self.ngram_to_id:
self.ngram_to_id[ch] = len(self.ngram_to_id)
self.subword_vocab_size = len(self.ngram_to_id)
with open(self.sub_vocab_file, 'wb') as f:
pickle.dump((self.ngram_to_id, self.unk_char_list, self.unk_ngram_list, self.max_ngram_per_word), f)
def preprocess_morpheme(self):
"""
Preprocess for morpheme model
"""
self.char_to_id, self.unk_char_list = self.build_vocab(mode="char")
self.morpheme_to_id, self.unk_morph_list, self.max_morph_per_word = self.build_morpheme_vocab()
for ch in self.char_to_id:
if ch not in self.morpheme_to_id:
self.morpheme_to_id[ch] = len(self.morpheme_to_id)
self.subword_vocab_size = len(self.morpheme_to_id)
with open(self.sub_vocab_file, 'wb') as f:
pickle.dump((self.morpheme_to_id, self.unk_char_list, self.unk_morph_list, self.max_morph_per_word), f)
def preprocess_oracle(self):
"""
Preprocess for morpheme model
"""
self.morpheme_to_id, self.max_morph_per_word = self.build_oracle_vocab()
self.subword_vocab_size = len(self.morpheme_to_id)
with open(self.sub_vocab_file, 'wb') as f:
pickle.dump((self.morpheme_to_id, self.max_morph_per_word), f)
def load_preprocessed(self):
"""
Load preprocessed dictionaries, this is called during testing.
"""
with open(self.words_vocab_file, 'rb') as f:
self.word_to_id, self.unk_word_list = pickle.load(f)
self.word_vocab_size = len(self.word_to_id)
if self.unit != "word":
with open(self.sub_vocab_file, 'rb') as f:
if self.unit == "char":
self.max_word_len = self.get_max_word_length(self.word_to_id) + 2
self.char_to_id, self.unk_char_list, self.max_word_len = pickle.load(f)
self.subword_vocab_size = len(self.char_to_id)
elif self.unit == "char-ngram":
self.ngram_to_id, self.unk_char_list, self.unk_ngram_list, \
self.max_ngram_per_word = pickle.load(f)
self.subword_vocab_size = len(self.ngram_to_id)
elif self.unit == "morpheme":
self.morpheme_to_id, self.unk_char_list, self.unk_morph_list, \
self.max_morph_per_word = pickle.load(f)
self.subword_vocab_size = len(self.morpheme_to_id)
elif self.unit == "oracle":
self.morpheme_to_id, self.max_morph_per_word = pickle.load(f)
self.subword_vocab_size = len(self.morpheme_to_id)
else:
sys.exit("Unknown unit")
def load_vocab_dict(self, vocab_file):
"""
Load preprocessed dictionaries, this is called during testing.
"""
with open(vocab_file, 'rb') as f:
output_vocab, unk_list = pickle.load(f)
return output_vocab, unk_list
def build_vocab(self, mode):
"""
Build vocabularies: word_to_id OR char_to_id
Keys: words or chars
Values: ids
unk_list defines the list of words or chars which only occur once in the
training data.
:param mode: defines the type of vocabulary: word or char
"""
if mode == "word":
data = self.read_words()
else:
data = self.read_chars()
counter = collections.Counter(data)
count_pairs = sorted(counter.items(), key=lambda x: (-x[-1], x[0]))
item_to_id = dict()
unk_list = set()
if mode == "char":
# '^' is a start of word symbol
# '$' is a end of word symbol
item_to_id = self.add_to_dict(item_to_id, '^', '$')
else:
if len(self.output_vocab) > 0:
print('Using output vocab!')
print('Output vocab size:', self.out_vocab_size)
sorted_vocab = sorted(self.output_vocab.items(), key=operator.itemgetter(1))
for k, v in sorted_vocab:
item_to_id[k] = len(item_to_id)
if len(item_to_id) == self.out_vocab_size:
break
else:
item_to_id['<unk>'] = len(item_to_id)
if self.sos != '':
item_to_id[self.sos] = len(item_to_id)
if self.eos != '':
item_to_id[self.eos] = len(item_to_id)
for i, (token, freq) in enumerate(count_pairs):
if token not in item_to_id:
item_to_id[token] = len(item_to_id)
if freq == 1:
unk_list.add(token)
return item_to_id, unk_list
def build_ngram_vocab(self, n):
"""
Build a dictionary of character ngrams found in the training data
Keys: n-grams
Values: n-gram frequencies
:param n: length of the character ngram
:return: a dictionary of character ngrams, (ngram, freq) key-value pairs
and max_ngram_per_word
"""
max_ngram_per_word = 0
ngram_dict = collections.defaultdict(int)
for word in self.train_data:
if word == self.eos or word == self.sos:
continue
_word = '^' + word + '$'
ngram_counts = len(_word) - n + 1
if ngram_counts > max_ngram_per_word:
max_ngram_per_word = ngram_counts
for i in range(ngram_counts):
ngram = _word[i:i + n]
ngram_dict[ngram] += 1
unk_ngram_list = set()
item_to_id = dict()
sorted_dict = sorted(ngram_dict.items(), key=operator.itemgetter(1), reverse=True)
for token, freq in sorted_dict:
if freq == 1:
unk_ngram_list.add(token)
if token not in item_to_id:
item_to_id[token] = len(item_to_id)
return item_to_id, unk_ngram_list, max_ngram_per_word
def build_morpheme_vocab(self):
"""
Build morpheme vocab from a given file
Keys: morphemes
Values: morpheme frequencies
:return: a dictionary: (morpheme, freq) key-value pairs and max_morph_per_word
"""
max_morph_per_word = 0
morpheme_dict = collections.defaultdict(int)
splitter = "@@"
for token in self.train_data:
if token == self.eos or token == self.sos:
continue
token = '^' + token + '$'
morphemes = token.split(splitter)
if len(morphemes) > max_morph_per_word:
max_morph_per_word = len(morphemes)
for morpheme in morphemes:
morpheme_dict[morpheme] += 1
unk_morpheme_list = set()
item_to_id = dict()
sorted_dict = sorted(morpheme_dict.items(), key=operator.itemgetter(1), reverse=True)
for token, freq in sorted_dict:
if freq == 1:
unk_morpheme_list.add(token)
if token not in item_to_id:
item_to_id[token] = len(item_to_id)
return item_to_id, unk_morpheme_list, max_morph_per_word
def build_oracle_vocab(self):
max_morph_per_word = 0
morpheme_dict = dict()
morpheme_dict['<unk>'] = len(morpheme_dict)
morpheme_dict['<PAD>'] = len(morpheme_dict)
if self.eos != '':
morpheme_dict[self.eos] = len(morpheme_dict)
if self.sos != '':
morpheme_dict[self.sos] = len(morpheme_dict)
splitter = "+"
for token in self.train_data:
if token == self.eos or token == self.sos:
continue
morphemes = token.split(splitter)
if splitter in token:
# remove the word form
morphemes = morphemes[1:]
if len(morphemes) > max_morph_per_word:
max_morph_per_word = len(morphemes)
for morpheme in morphemes:
if self.use_all_morphemes:
if morpheme not in morpheme_dict:
morpheme_dict[morpheme] = len(morpheme_dict)
else:
if "lemma:" in morpheme or "pos:" in morpheme or "stem:" in morpheme:
if morpheme not in morpheme_dict:
morpheme_dict[morpheme] = len(morpheme_dict)
return morpheme_dict, max_morph_per_word
def replace_special_chars(self, word):
"""
Replace special characters since we want to use them for
the start and beginning of word symbols
"""
word = re.sub("\^", "¬", word)
word = re.sub("\$", "£", word)
return word
def read_dataset(self, filename):
"""
Read data set from a file and put them into a list of tokens
:param filename: file to read
:return: data
"""
data = []
with open(filename, 'r') as f:
for line in f:
line = line.strip()
if self.lowercase or self.unit == "oracle":
line = line.lower()
if self.sos != '':
data.append(self.sos)
for word in line.split():
word = self.replace_special_chars(word)
_word = word
if self.unit == "oracle":
if "+" in word:
_word = word.split('+')[0].split(":")[1]
if self.unit == "morpheme":
_word = re.sub("@@", "", word)
if not self.is_hyperlink(_word.lower()) and len(_word) <= 100:
data.append(word)
if self.eos != '':
data.append(self.eos)
return data
def read_words(self):
"""
Read sequence of tokens from a given file
This function is used to build dictionary
"""
# If lowercase is True, it is already handled when we read the dataset.
# If it is False, the unit must be other than word, so that we need to lowercase
# the data since the word lookup table is for target words which are
# always in lowercase.
data = self.train_data
if not self.lowercase or self.unit == "oracle":
tmp_data = []
for word in data:
if self.unit == "oracle":
if '+' in word:
tags = word.split('+')
word_tag = tags[0].split(':')
word = word_tag[1]
if self.unit == "morpheme":
word = re.sub("@@", "", word)
word = word.lower()
tmp_data.append(word)
data = tmp_data
return data
def read_chars(self):
"""
Read sequence of chars from a given file
"""
char_data = []
for word in self.train_data:
if word == self.eos or word == self.sos:
continue
if self.unit == "oracle":
if '+' in word:
tags = word.split('+')
word_tag = tags[0].split(':')
word = word_tag[1]
if self.unit == "morpheme":
word = re.sub("@@", "", word)
char_data.extend([ch for ch in word])
return char_data
def get_max_word_length(self, word_dict):
"""
Get maximum word length from the vocabulary
:param word_dict: the vocabulary
:return: maximum word length
"""
max_len = 0
max_word = ""
for word in word_dict:
word = "^" + word + "$"
if len(word) > max_len:
max_len = len(word)
max_word = word
print("Longest word: " + max_word + " " + str(max_len))
return max_len
def add_to_dict(self, _dict, start, end):
"""
Add special symbols
:param _dict: the dictionary
:param start: start symbol
:param end: end symbol
:return: dictionary with counts
"""
symbols = ['<unk>', start, end, '<PAD>']
if self.eos != '':
symbols.append(self.eos)
if self.sos != '':
symbols.append(self.sos)
for s in symbols:
_dict[s] = len(_dict)
return _dict
def data_to_word_ids(self, input_data, filter=False):
"""
Given a list of words, convert each word into it's word id
:param input_data: a list of words
:return: a list of word ids
"""
_buffer = list()
for word in input_data:
word = word.lower()
if self.unit == "oracle":
if "+" in word:
tokens = word.split('+')
word_tag = tokens[0].split(':')
word = word_tag[1]
if self.unit == "morpheme":
word = re.sub("@@", "", word)
# flag to randomize token with frequency one
flag = 1
if word in self.unk_word_list:
flag = random.randint(0, 1)
if word in self.word_to_id and flag == 1:
# if filter is True, reduce output vocabulary for softmax
# (map words not in top self.out_vocab_size to UNK)
if filter:
# index start from 0
if self.word_to_id[word] < self.out_vocab_size:
_buffer.append(self.word_to_id[word])
else:
_buffer.append(self.word_to_id['<unk>'])
else:
_buffer.append(self.word_to_id[word])
else:
_buffer.append(self.word_to_id['<unk>'])
return _buffer
def create_binary_morpheme_vector(self, word):
"""
Encode word into a binary vector of morphemes
:param word: input word
:return: a vector unit of the input word
"""
dimension = len(self.morpheme_to_id)
encoding = np.zeros(dimension)
if word == self.eos or word == self.sos:
encoding[self.morpheme_to_id[word]] = 1
else:
if self.unit == "morpheme":
word = "^" + word + "$"
morphemes = word.split("@@")
else: # oracle model
morphemes = word.split("+")
if "+" in word:
# remove the original word form
morphemes = morphemes[1:]
for morpheme in morphemes:
if self.unit == "morpheme":
if morpheme in self.morpheme_to_id:
encoding[self.morpheme_to_id[morpheme]] = 1
else:
for ch in morpheme:
flag = 1
if ch in self.unk_char_list:
flag = random.randint(0, 1)
if ch in self.morpheme_to_id and flag == 1:
encoding[self.morpheme_to_id[ch]] = 1
else:
encoding[self.morpheme_to_id['<unk>']] = 1
else: # oracle model
if morpheme in self.morpheme_to_id:
if self.use_all_morphemes:
encoding[self.morpheme_to_id[morpheme]] = 1
else:
if "lemma:" in morpheme or "pos:" in morpheme or "stem:" in morpheme:
encoding[self.morpheme_to_id[morpheme]] = 1
else:
encoding[self.morpheme_to_id['<unk>']] = 1
return encoding
def word_to_morphemes(self, word):
"""
Encode word into a vector of its morpheme ids
:param word: input word
:return: a vector morphemic representation of the word
"""
encoding = list()
if word == self.eos or word == self.sos:
encoding.append(self.morpheme_to_id[word])
else:
if self.unit == "morpheme":
word = "^" + word + "$"
morphemes = word.split("@@")
else: # oracle model
morphemes = word.split("+")
if "+" in word:
# remove the original word form
morphemes = morphemes[1:]
for morpheme in morphemes:
if self.unit == "morpheme":
if morpheme in self.morpheme_to_id:
encoding.append(self.morpheme_to_id[morpheme])
else:
for ch in morpheme:
flag = 1
if ch in self.unk_char_list:
flag = random.randint(0, 1)
if ch in self.morpheme_to_id and flag == 1:
encoding.append(self.morpheme_to_id[ch])
else:
encoding.append(self.morpheme_to_id['<unk>'])
else: # oracle model
if morpheme in self.morpheme_to_id:
if self.use_all_morphemes:
encoding.append(self.morpheme_to_id[morpheme])
else:
if "lemma:" in morpheme or "pos:" in morpheme or "stem:" in morpheme:
encoding.append(self.morpheme_to_id[morpheme])
else:
encoding.append(self.morpheme_to_id['<unk>'])
return encoding
def morpheme_encoding(self, data):
"""
Given a list of words, convert each word into its morpheme vector
:param data: a list of words
:return: a list of morpheme vectors
"""
_buffer = list()
for word in data:
if self.composition == "addition":
_buffer.append(self.create_binary_morpheme_vector(word))
elif self.composition == "bi-lstm":
morphemes = self.word_to_morphemes(word)
_buffer.append(self.padding(morphemes, self.max_morph_per_word,
self.morpheme_to_id))
else:
sys.exit("Unknown composition")
return _buffer
def create_binary_ngram_vector(self, word, n):
"""
Encode word into a binary vector of character ngrams
:param word: input word
:param n: the length of character to encode (n-gram)
:return: a vector unit of the input word
"""
dimension = len(self.ngram_to_id)
encoding = np.zeros(dimension)
if word == self.eos or word == self.sos:
encoding[self.ngram_to_id[word]] = 1
else:
_word = '^' + word + '$'
for i in range(len(_word) - n + 1):
ngram = _word[i:i+n]
if ngram in self.ngram_to_id:
encoding[self.ngram_to_id[ngram]] = 1
else:
for ch in ngram:
flag = 1
if ch in self.unk_char_list:
flag = random.randint(0, 1)
if ch in self.ngram_to_id and flag == 1:
encoding[self.ngram_to_id[ch]] = 1
else:
encoding[self.ngram_to_id['<unk>']] = 1
return encoding
def word_to_ngrams(self, word):
"""
Encode word into a vector of its ngram ids
:param word: input word
:return: a vector ngram representation of the word
"""
encoding = list()
n = self.n
if word == self.eos or word == self.sos:
encoding.append(self.ngram_to_id[word])
else:
_word = '^' + word + '$'
for i in range(len(_word) - n + 1):
ngram = _word[i:i + n]
if ngram in self.ngram_to_id:
encoding.append(self.ngram_to_id[ngram])
else:
for ch in ngram:
flag = 1
if ch in self.unk_char_list:
flag = random.randint(0, 1)
if ch in self.ngram_to_id and flag == 1:
encoding.append(self.ngram_to_id[ch])
else:
encoding.append(self.ngram_to_id['<unk>'])
return encoding
def ngram_encoding(self, data):
"""
Given a list of words, convert each word into its character ngram unit
:param input_data: a list of words
:return: a list of character ngram unit of words
"""
_buffer = list()
for word in data:
if self.composition == "addition":
_buffer.append(self.create_binary_ngram_vector(word, self.n))
elif self.composition == "bi-lstm":
ngrams = self.word_to_ngrams(word)
_buffer.append(self.padding(ngrams, self.max_ngram_per_word,
self.ngram_to_id))
else:
sys.exit("Unknown composition")
return _buffer
def word_to_chars(self, word):
"""
Break word into a sequence of characters
:param word: a word
:return: a list of character ids of the word
"""
chars = list()
if word == self.eos or word == self.sos:
chars.append(self.char_to_id[word])
else:
word = "^" + word + "$"
for ch in word:
flag = 1
if ch in self.unk_char_list:
flag = random.randint(0, 1)
if ch in self.char_to_id and flag == 1:
chars.append(self.char_to_id[ch])
else:
chars.append(self.char_to_id['<unk>'])
return chars
def char_encoding(self, data):
"""
Given a list of words, convert each word into a list of characters
:param data: a list of words
:return: [a list of characters]
"""
_buffer = list()
for word in data:
chars = self.word_to_chars(word)
_buffer.append(self.padding(chars, self.max_word_len, self.char_to_id))
return _buffer
def encode_data(self, data):
"""
Encode data according to the specified unit
:param data: input data
:return: encoded input data
"""
if self.unit == "char":
data = self.char_encoding(data)
elif self.unit == "char-ngram":
data = self.ngram_encoding(data)
elif self.unit == "morpheme" or self.unit == "oracle":
data = self.morpheme_encoding(data)
else:
data = self.data_to_word_ids(data, False)
return data
def data_iterator(self, raw_data, batch_size, num_steps):
data_len = len(raw_data)
batch_len = data_len // batch_size
data = []
for i in range(batch_size):
x = raw_data[batch_len * i:batch_len * (i + 1)]
data.append(x)
epoch_size = (batch_len - 1) // num_steps
if epoch_size == 0:
raise ValueError("epoch_size == 0, decrease batch_size or num_steps")
for i in range(epoch_size):
xs = list()
ys = list()
for j in range(batch_size):
x = data[j][i * num_steps:(i + 1) * num_steps]
y = data[j][i * num_steps + 1:(i + 1) * num_steps + 1]
xs.append(self.encode_data(x))
ys.append(self.data_to_word_ids(y, True))
yield (xs, ys)
def data_iterator_test(self, raw_data, batch_size, num_steps):
data_len = len(raw_data)
batch_len = data_len // batch_size
data = []
for i in range(batch_size):
x = raw_data[batch_len * i:batch_len * (i + 1)]
data.append(x)
epoch_size = (batch_len - 1) // num_steps
if epoch_size == 0:
raise ValueError("epoch_size == 0, decrease batch_size or num_steps")
for i in range(epoch_size):
xs = list()
ys = list()
for j in range(batch_size):
x = data[j][i * num_steps:(i + 1) * num_steps]
y = data[j][i * num_steps + 1:(i + 1) * num_steps + 1]
xs.append(self.encode_data(x))
ys.append(self.data_to_word_ids(y, True))
yield (xs, ys)