forked from qkhy/poetry-seq2seq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_utils.py
266 lines (218 loc) · 9.13 KB
/
data_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
#! /usr/bin/env python
#-*- coding:utf-8 -*-
import codecs
import os
import random
import numpy as np
from cnt_words import get_pop_quatrains
from rank_words import get_word_ranks
from segment import Segmenter
from utils import DATA_PROCESSED_DIR, embed_w2v, apply_one_hot, apply_sparse, pad_to, SEP_TOKEN, PAD_TOKEN
from vocab import ch2int, VOCAB_SIZE, sentence_to_ints
from word2vec import get_word_embedding
train_path = os.path.join(DATA_PROCESSED_DIR, 'train.txt')
cangtou_train_path = os.path.join(DATA_PROCESSED_DIR, 'cangtou_train.txt')
kw_train_path = os.path.join(DATA_PROCESSED_DIR, 'kw_train.txt')
def fill_np_matrix(vects, batch_size, value):
max_len = max(len(vect) for vect in vects)
res = np.full([batch_size, max_len], value, dtype=np.int32)
for row, vect in enumerate(vects):
res[row, :len(vect)] = vect
return res
def fill_np_array(vect, batch_size, value):
result = np.full([batch_size], value, dtype=np.int32)
result[:len(vect)] = vect
return result
def _gen_train_data():
segmenter = Segmenter()
poems = get_pop_quatrains()
random.shuffle(poems)
ranks = get_word_ranks()
print "Generating training data ..."
data = []
kw_data = []
for idx, poem in enumerate(poems):
sentences = poem['sentences']
if len(sentences) == 4:
flag = True
rows = []
kw_row = []
for sentence in sentences:
rows.append([sentence])
segs = filter(lambda seg: seg in ranks, segmenter.segment(sentence))
if 0 == len(segs):
flag = False
break
keyword = reduce(lambda x,y: x if ranks[x] < ranks[y] else y, segs)
kw_row.append(keyword)
rows[-1].append(keyword)
if flag:
data.extend(rows)
kw_data.append(kw_row)
if 0 == (idx+1)%2000:
print "[Training Data] %d/%d poems are processed." %(idx+1, len(poems))
with codecs.open(train_path, 'w', 'utf-8') as fout:
for row in data:
fout.write('\t'.join(row)+'\n')
with codecs.open(kw_train_path, 'w', 'utf-8') as fout:
for kw_row in kw_data:
fout.write('\t'.join(kw_row)+'\n')
print "Training data is generated."
# TODO(vera): find a better name than cangtou...
def _gen_cangtou_train_data():
poems = get_pop_quatrains()
random.shuffle(poems)
with codecs.open(cangtou_train_path, 'w', 'utf-8') as fout:
for idx, poem in enumerate(poems):
for sentence in poem['sentences']:
fout.write(sentence + "\t" + sentence[0] + "\n")
if 0 == (idx + 1) % 2000:
print "[Training Data] %d/%d poems are processed." %(idx+1, len(poems))
print "Cangtou training data is generated."
def get_train_data(cangtou=False):
train_data_path = cangtou_train_path if cangtou else train_path
if not os.path.exists(train_data_path):
if cangtou:
_gen_cangtou_train_data()
else:
_gen_train_data()
data = []
with codecs.open(train_data_path, 'r', 'utf-8') as fin:
line = fin.readline()
while line:
toks = line.strip().split('\t')
data.append({'sentence':toks[0], 'keyword':toks[1]})
line = fin.readline()
return data
def get_kw_train_data():
if not os.path.exists(kw_train_path):
_gen_train_data()
data = []
with codecs.open(kw_train_path, 'r', 'utf-8') as fin:
line = fin.readline()
while line:
data.append(line.strip().split('\t'))
line = fin.readline()
return data
def batch_train_data(batch_size):
"""Get training data in poem, batch major format
Args:
batch_size:
Returns:
kw_mats: [4, batch_size, time_steps]
kw_lens: [4, batch_size]
s_mats: [4, batch_size, time_steps]
s_lens: [4, batch_size]
"""
if not os.path.exists(train_path):
_gen_train_data()
with codecs.open(train_path, 'r', 'utf-8') as fin:
stop = False
while not stop:
batch_s = [[] for _ in range(4)]
batch_kw = [[] for _ in range(4)]
# NOTE(sdsuo): Modified batch size to remove empty lines in batches
for i in range(batch_size * 4):
line = fin.readline()
if not line:
stop = True
break
else:
toks = line.strip().split('\t')
# NOTE(sdsuo): Removed start token
batch_s[i%4].append([ch2int[ch] for ch in toks[0]])
batch_kw[i%4].append([ch2int[ch] for ch in toks[1]])
if batch_size != len(batch_s[0]):
print 'Batch incomplete with size {}, expecting size {}, dropping batch.'.format(len(batch_s[0]), batch_size)
break
else:
kw_mats = [fill_np_matrix(batch_kw[i], batch_size, VOCAB_SIZE-1) \
for i in range(4)]
kw_lens = [fill_np_array(map(len, batch_kw[i]), batch_size, 0) \
for i in range(4)]
s_mats = [fill_np_matrix(batch_s[i], batch_size, VOCAB_SIZE-1) \
for i in range(4)]
s_lens = [fill_np_array([len(x) for x in batch_s[i]], batch_size, 0) \
for i in range(4)]
yield kw_mats, kw_lens, s_mats, s_lens
def process_sentence(sentence, rev=False, pad_len=None, pad_token=PAD_TOKEN):
if rev:
sentence = sentence[::-1]
sentence_ints = sentence_to_ints(sentence)
if pad_len is not None:
result_len = len(sentence_ints)
for i in range(pad_len - result_len):
sentence_ints.append(pad_token)
return sentence_ints
def prepare_batch_predict_data(keyword, previous=[], prev=True, rev=False, align=False):
# previous sentences
previous_sentences_ints = []
for sentence in previous:
sentence_ints = process_sentence(sentence, rev=rev, pad_len=7 if align else None)
previous_sentences_ints += [SEP_TOKEN] + sentence_ints
# keywords
keywords_ints = process_sentence(keyword, rev=rev, pad_len=4 if align else None)
source_ints = keywords_ints + (previous_sentences_ints if prev else [])
source_len = len(source_ints)
source = fill_np_matrix([source_ints], 1, PAD_TOKEN)
source_len = np.array([source_len])
return source, source_len
def gen_batch_train_data(batch_size, prev=True, rev=False, align=False, cangtou=False):
"""
Get training data in batch major format, with keyword and previous sentences as source,
aligned and reversed
Args:
batch_size:
Returns:
source: [batch_size, time_steps]: keywords + SEP + previous sentences
source_lens: [batch_size]: length of source
target: [batch_size, time_steps]: current sentence
target_lens: [batch_size]: length of target
"""
train_data_path = cangtou_train_path if cangtou else train_path
if not os.path.exists(train_data_path):
if cangtou:
_gen_cangtou_train_data()
else:
_gen_train_data()
with codecs.open(train_data_path, 'r', 'utf-8') as fin:
stop = False
while not stop:
source = []
source_lens = []
target = []
target_lens = []
previous_sentences_ints = []
for i in range(batch_size):
line = fin.readline()
if not line:
stop = True
break
else:
line_number = i % 4
if line_number == 0:
previous_sentences_ints = []
current_sentence, keywords = line.strip().split('\t')
current_sentence_ints = process_sentence(current_sentence, rev=rev, pad_len=7 if align else None)
keywords_ints = process_sentence(keywords, rev=rev, pad_len=4 if align else None)
source_ints = keywords_ints + (previous_sentences_ints if prev else [])
target.append(current_sentence_ints)
target_lens.append(len(current_sentence_ints))
source.append(source_ints)
source_lens.append(len(source_ints))
# Always append to previous sentences
previous_sentences_ints += [SEP_TOKEN] + current_sentence_ints
if len(source) == batch_size:
source_padded = fill_np_matrix(source, batch_size, PAD_TOKEN)
target_padded = fill_np_matrix(target, batch_size, PAD_TOKEN)
source_lens = np.array(source_lens)
target_lens = np.array(target_lens)
yield source_padded, source_lens, target_padded, target_lens
def main():
train_data = get_train_data()
print "Size of the training data: %d" %len(train_data)
kw_train_data = get_kw_train_data()
print "Size of the keyword training data: %d" %len(kw_train_data)
assert len(train_data) == 4 * len(kw_train_data)
if __name__ == '__main__':
main()