-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathinput_data.py
More file actions
82 lines (66 loc) · 2.7 KB
/
input_data.py
File metadata and controls
82 lines (66 loc) · 2.7 KB
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
#encoding:utf-8
#import sys
#reload(sys)
#sys.setdefaultencoding('utf8')
import os
import codecs
import collections
from six.moves import cPickle
import numpy as np
import re
import itertools
class TextLoader():
def __init__(self, data_dir, batch_size, seq_length, mini_frq=3):
self.data_dir = data_dir
self.batch_size = batch_size
self.seq_length = seq_length
self.mini_frq = mini_frq
input_file = os.path.join(data_dir, "input.zh.txt")
vocab_file = os.path.join(data_dir, "vocab.zh.pkl")
self.preprocess(input_file, vocab_file)
self.create_batches()
self.reset_batch_pointer()
def build_vocab(self, sentences):
word_counts = collections.Counter()
if not isinstance(sentences, list):
sentences = [sentences]
for sent in sentences:
word_counts.update(sent)
vocabulary_inv = ['<START>', '<UNK>', '<END>'] + \
[x[0] for x in word_counts.most_common() if x[1] >= self.mini_frq]
vocabulary = {x: i for i, x in enumerate(vocabulary_inv)}
return [vocabulary, vocabulary_inv]
def preprocess(self, input_file, vocab_file):
with codecs.open(input_file, 'r', 'utf-8') as f:
lines = f.readlines()
if lines[0][:1] == codecs.BOM_UTF8:
lines[0] = lines[0][1:]
lines = [line.strip().split() for line in lines]
self.vocab, self.words = self.build_vocab(lines)
self.vocab_size = len(self.words)
#print 'word num: ', self.vocab_size
with open(vocab_file, 'wb') as f:
cPickle.dump(self.words, f)
raw_data = [[0] * self.seq_length +
[self.vocab.get(w, 1) for w in line] +
[2] * self.seq_length for line in lines]
self.raw_data = raw_data
def create_batches(self):
xdata, ydata = list(), list()
for row in self.raw_data:
for ind in range(self.seq_length, len(row)):
xdata.append(row[ind-self.seq_length:ind])
ydata.append([row[ind]])
self.num_batches = int(len(xdata) / self.batch_size)
if self.num_batches == 0:
assert False, "Not enough data. Make seq_length and batch_size small."
xdata = np.array(xdata[:self.num_batches * self.batch_size])
ydata = np.array(ydata[:self.num_batches * self.batch_size])
self.x_batches = np.split(xdata, self.num_batches, 0)
self.y_batches = np.split(ydata, self.num_batches, 0)
def next_batch(self):
x, y = self.x_batches[self.pointer], self.y_batches[self.pointer]
self.pointer += 1
return x, y
def reset_batch_pointer(self):
self.pointer = 0