-
Notifications
You must be signed in to change notification settings - Fork 40
/
data.py
277 lines (235 loc) · 8 KB
/
data.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
import random
import torch
import numpy as np
PAD, UNK, BOS, EOS = '<pad>', '<unk>', '<bos>', '<eos>'
BOC, EOC = '<boc>', '<eoc>'
LS, RS, SP = '<s>', '</s>', ' '
CS = ['<c-1>'] + ['<c' + str(i) + '>' for i in range(32)] # content
SS = ['<s-1>'] + ['<s' + str(i) + '>' for i in range(512)] # segment
PS = ['<p-1>'] + ['<p' + str(i) + '>' for i in range(512)] # position
TS = ['<t-1>'] + ['<t' + str(i) + '>' for i in range(32)] # other types
PUNCS = set([",", ".", "?", "!", ":", ",", "。", "?", "!", ":"])
BUFSIZE = 4096000
def ListsToTensor(xs, vocab=None):
max_len = max(len(x) for x in xs)
ys = []
for x in xs:
if vocab is not None:
y = vocab.token2idx(x) + [vocab.padding_idx]*(max_len - len(x))
else:
y = x + [0]*(max_len - len(x))
ys.append(y)
return ys
def _back_to_text_for_check(x, vocab):
w = x.t().tolist()
for sent in vocab.idx2token(w):
print (' '.join(sent))
def batchify(data, vocab):
xs_tpl, xs_seg, xs_pos, \
ys_truth, ys_inp, \
ys_tpl, ys_seg, ys_pos, msk = [], [], [], [], [], [], [], [], []
for xs_tpl_i, xs_seg_i, xs_pos_i, ys_i, ys_tpl_i, ys_seg_i, ys_pos_i in data:
xs_tpl.append(xs_tpl_i)
xs_seg.append(xs_seg_i)
xs_pos.append(xs_pos_i)
ys_truth.append(ys_i)
ys_inp.append([BOS] + ys_i[:-1])
ys_tpl.append(ys_tpl_i)
ys_seg.append(ys_seg_i)
ys_pos.append(ys_pos_i)
msk.append([1 for i in range(len(ys_i))])
xs_tpl = torch.LongTensor(ListsToTensor(xs_tpl, vocab)).t_().contiguous()
xs_seg = torch.LongTensor(ListsToTensor(xs_seg, vocab)).t_().contiguous()
xs_pos = torch.LongTensor(ListsToTensor(xs_pos, vocab)).t_().contiguous()
ys_truth = torch.LongTensor(ListsToTensor(ys_truth, vocab)).t_().contiguous()
ys_inp = torch.LongTensor(ListsToTensor(ys_inp, vocab)).t_().contiguous()
ys_tpl = torch.LongTensor(ListsToTensor(ys_tpl, vocab)).t_().contiguous()
ys_seg = torch.LongTensor(ListsToTensor(ys_seg, vocab)).t_().contiguous()
ys_pos = torch.LongTensor(ListsToTensor(ys_pos, vocab)).t_().contiguous()
msk = torch.FloatTensor(ListsToTensor(msk)).t_().contiguous()
return xs_tpl, xs_seg, xs_pos, ys_truth, ys_inp, ys_tpl, ys_seg, ys_pos, msk
def s2t(strs, vocab):
inp, msk = [], []
for x in strs:
inp.append(x)
msk.append([1 for i in range(len(x))])
inp = torch.LongTensor(ListsToTensor(inp, vocab)).t_().contiguous()
msk = torch.FloatTensor(ListsToTensor(msk)).t_().contiguous()
return inp, msk
def s2xy(lines, vocab, max_len, min_len):
data = []
for line in lines:
res = parse_line(line, max_len, min_len)
if not res:
continue
data.append(res)
return batchify(data, vocab)
def parse_line(line, max_len, min_len):
line = line.strip()
if not line:
return None
fs = line.split("<s2>")
author, cipai = fs[0].split("<s1>")
sents = fs[1].strip()
if len(sents) > max_len:
sents = sents[:max_len]
if len(sents) < min_len:
return None
sents = sents.split("</s>")
ys = []
xs_tpl = []
xs_seg = []
xs_pos = []
ctx = cipai
ws = [w for w in ctx]
xs_tpl = ws + [EOC]
xs_seg = [SS[0] for w in ws] + [EOC]
xs_pos = [SS[i+300] for i in range(len(ws))] + [EOC]
ys_tpl = []
ys_seg = []
ys_pos = []
for si, sent in enumerate(sents):
ws = []
sent = sent.strip()
if not sent:
continue
for w in sent:
ws.append(w)
if w.strip() and w not in PUNCS:
ys_tpl.append(CS[2])
else:
ys_tpl.append(CS[1])
ys += ws + [RS]
if ws[-1] in PUNCS:
ys_tpl[-2] = CS[3]
else:
ys_tpl[-1] = CS[3]
ys_tpl += [RS]
ys_seg += [SS[si + 1] for w in ws] + [RS]
ys_pos += [PS[len(ws) - i] for i in range(len(ws))] + [RS]
ys += [EOS]
ys_tpl += [EOS]
ys_seg += [EOS]
ys_pos += [EOS]
xs_tpl += ys_tpl
xs_seg += ys_seg
xs_pos += ys_pos
if len(ys) < min_len:
return None
return xs_tpl, xs_seg, xs_pos, ys, ys_tpl, ys_seg, ys_pos
def s2xy_polish(lines, vocab, max_len, min_len):
data = []
for line in lines:
res = parse_line_polish(line, max_len, min_len)
data.append(res)
return batchify(data, vocab)
def parse_line_polish(line, max_len, min_len):
line = line.strip()
if not line:
return None
fs = line.split("<s2>")
author, cipai = fs[0].split("<s1>")
sents = fs[1].strip()
if len(sents) > max_len:
sents = sents[:max_len]
if len(sents) < min_len:
return None
sents = sents.split("</s>")
ys = []
xs_tpl = []
xs_seg = []
xs_pos = []
ctx = cipai
ws = [w for w in ctx]
xs_tpl = ws + [EOC]
xs_seg = [SS[0] for w in ws] + [EOC]
xs_pos = [SS[i+300] for i in range(len(ws))] + [EOC]
ys_tpl = []
ys_seg = []
ys_pos = []
for si, sent in enumerate(sents):
ws = []
sent = sent.strip()
if not sent:
continue
for w in sent:
ws.append(w)
if w == "_":
ys_tpl.append(CS[2])
else:
ys_tpl.append(w)
ys += ws + [RS]
ys_tpl += [RS]
ys_seg += [SS[si + 1] for w in ws] + [RS]
ys_pos += [PS[len(ws) - i] for i in range(len(ws))] + [RS]
ys += [EOS]
ys_tpl += [EOS]
ys_seg += [EOS]
ys_pos += [EOS]
xs_tpl += ys_tpl
xs_seg += ys_seg
xs_pos += ys_pos
if len(ys) < min_len:
return None
return xs_tpl, xs_seg, xs_pos, ys, ys_tpl, ys_seg, ys_pos
class DataLoader(object):
def __init__(self, vocab, filename, batch_size, max_len_y, min_len_y):
self.batch_size = batch_size
self.vocab = vocab
self.max_len_y = max_len_y
self.min_len_y = min_len_y
self.filename = filename
self.stream = open(self.filename, encoding='utf8')
self.epoch_id = 0
def __iter__(self):
lines = self.stream.readlines(BUFSIZE)
if not lines:
self.epoch_id += 1
self.stream.close()
self.stream = open(self.filename, encoding='utf8')
lines = self.stream.readlines(BUFSIZE)
data = []
for line in lines[:-1]: # the last sent may be imcomplete
res = parse_line(line, self.max_len_y, self.min_len_y)
if not res:
continue
data.append(res)
random.shuffle(data)
idx = 0
while idx < len(data):
yield batchify(data[idx:idx+self.batch_size], self.vocab)
idx += self.batch_size
class Vocab(object):
def __init__(self, filename, min_occur_cnt, specials = None):
idx2token = [PAD, UNK, BOS, EOS] + [BOC, EOC, LS, RS, SP] + CS + SS + PS + TS \
+ (specials if specials is not None else [])
for line in open(filename, encoding='utf8').readlines():
try:
token, cnt = line.strip().split()
except:
continue
if int(cnt) >= min_occur_cnt:
idx2token.append(token)
self._token2idx = dict(zip(idx2token, range(len(idx2token))))
self._idx2token = idx2token
self._padding_idx = self._token2idx[PAD]
self._unk_idx = self._token2idx[UNK]
@property
def size(self):
return len(self._idx2token)
@property
def unk_idx(self):
return self._unk_idx
@property
def padding_idx(self):
return self._padding_idx
def random_token(self):
return self.idx2token(1 + np.random.randint(self.size-1))
def idx2token(self, x):
if isinstance(x, list):
return [self.idx2token(i) for i in x]
return self._idx2token[x]
def token2idx(self, x):
if isinstance(x, list):
return [self.token2idx(i) for i in x]
return self._token2idx.get(x, self.unk_idx)