-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRankDataset.py
270 lines (237 loc) · 10.4 KB
/
RankDataset.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
import torch
import linecache
from torch.utils.data import Dataset
import numpy as np
import random
import re
class RankDataset_point(Dataset):
def __init__(self, filename, max_seq_length, max_sess_length, tokenizer):
super(RankDataset_point, self).__init__()
self._filename = filename
self._max_seq_length = max_seq_length
self._max_sess_length = max_sess_length
self._tokenizer = tokenizer
with open(filename, "r") as f:
self._total_data = len(f.readlines())
def check_length(self, pairlist):
assert len(pairlist) % 2 == 0
max_seq_length = self._max_seq_length - 3
if len(pairlist) == 2:
while len(pairlist[0]) + len(pairlist[1]) + 2 > max_seq_length:
if len(pairlist[0]) > len(pairlist[1]):
pairlist[0].pop(0)
else:
pairlist[1].pop(-1)
else:
q_d_minimum_length = 0
for i in range(len(pairlist)):
q_d_minimum_length += len(pairlist[i]) + 1
if q_d_minimum_length > max_seq_length:
pairlist.pop(0)
pairlist.pop(0)
pairlist = self.check_length(pairlist)
return pairlist
def anno_main(self, qd_pairs):
#['qd', 'qd',...]
all_qd = []
for qd in qd_pairs:
qd = self._tokenizer.tokenize(qd)# get the word list(haven't encoded as the token ids)
all_qd.append(qd)
all_qd = self.check_length(all_qd)
history = all_qd[:-2]
query_tok = all_qd[-2]
doc_tok = all_qd[-1]
history_toks = ["[CLS]"]
segment_ids = [0]
for iidx, sent in enumerate(history):
history_toks.extend(sent + ["[eos]"])#[[q,d],[q,d],...]-->[q,d,q,d,...]
segment_ids.extend([0] * (len(sent) + 1))
query_tok += ["[eos]"]
query_tok += ["[SEP]"]
doc_tok += ["[eos]"]
doc_tok += ["[SEP]"]
all_qd_toks = history_toks + query_tok + doc_tok
segment_ids.extend([0] * len(query_tok))
segment_ids.extend([0] * len(doc_tok))
all_attention_mask = [1] * len(all_qd_toks)
assert len(all_qd_toks) <= self._max_seq_length
while len(all_qd_toks) < self._max_seq_length:
all_qd_toks.append("[PAD]")
segment_ids.append(0)
all_attention_mask.append(0)
assert len(all_qd_toks) == len(segment_ids) == len(all_attention_mask) == self._max_seq_length
anno_seq = self._tokenizer.convert_tokens_to_ids(all_qd_toks)
input_ids = np.asarray(anno_seq)
all_attention_mask = np.asarray(all_attention_mask)
segment_ids = np.asarray(segment_ids)
return input_ids, all_attention_mask, segment_ids
def __getitem__(self, idx):
line = linecache.getline(self._filename, idx + 1)
line = line.strip().split("\t")
# line = line.stripe().split("\t")
label = int(line[0])
length = len(line)-1
assert length & 1 == 0
length = int(length / 2)
# print('length = ', length)
qd_pairs = line[1:1+length] # ['qd', 'qd', ...] the sequence information including the current query and document
qd_id_pairs = [int(item) for item in line[1+length:-2]]
# print('qd paris = {}, qd_id_pairs = {}'.format(qd_pairs, qd_id_pairs))
assert len(qd_pairs) & 1 == 0
assert len(qd_id_pairs) & 1 == 0
input_ids, attention_mask, segment_ids = self.anno_main(qd_pairs)
qd_id_pairs = qd_id_pairs[-(self._max_sess_length * 2):]
qid = int(line[-2])
did = int(line[-1])
# qd_id_pairs = qd_id_pairs[:-2]
len_sess = len(qd_id_pairs) // 2
if len_sess < self._max_sess_length:
qd_id_pairs += [0, 0] * int(self._max_sess_length - len_sess)
array_ = np.array(qd_id_pairs).reshape(self._max_sess_length, 2)
session_qids = array_[:, 0]
session_qids[len_sess] = qid#add qid
session_dids = array_[:, 1]
# print('session qids = {} session dids = {}'.format(session_qids, session_dids))
batch = {
'input_ids': input_ids,
'token_type_ids': segment_ids,
'attention_mask': attention_mask,
'session_qid': session_qids,
'session_did': session_dids,
'session_len': len_sess + 1,
'qid': qid,
'did': did,
'label': label
}
return batch
def __len__(self):
return self._total_data
class RankDataset_test(Dataset):
def __init__(self, candi_cnt, filename, max_seq_length, max_sess_length, tokenizer):
super(RankDataset_test, self).__init__()
self._candi_cnt = candi_cnt
self._filename = filename
self._max_seq_length = max_seq_length
self._max_sess_length = max_sess_length
self._tokenizer = tokenizer
with open(filename, "r") as f:
linecnt = len(f.readlines())
assert linecnt % self._candi_cnt == 0
self._total_data = linecnt // self._candi_cnt
def check_length(self, pairlist):
assert len(pairlist) % 2 == 0
max_seq_length = self._max_seq_length - 3
if len(pairlist) == 2:
while len(pairlist[0]) + len(pairlist[1]) + 2 > max_seq_length:
if len(pairlist[0]) > len(pairlist[1]):
pairlist[0].pop(0)
else:
pairlist[1].pop(-1)
else:
q_d_minimum_length = 0
for i in range(len(pairlist)):
q_d_minimum_length += len(pairlist[i]) + 1
if q_d_minimum_length > max_seq_length:
pairlist.pop(0)
pairlist.pop(0)
pairlist = self.check_length(pairlist)
return pairlist
def anno_main(self, qd_pairs):
#['qd', 'qd',...]
all_qd = []
for qd in qd_pairs:
qd = self._tokenizer.tokenize(qd)# get the word list(haven't encoded as the token ids)
all_qd.append(qd)
all_qd = self.check_length(all_qd)
history = all_qd[:-2]
query_tok = all_qd[-2]
doc_tok = all_qd[-1]
history_toks = ["[CLS]"]
segment_ids = [0]
for iidx, sent in enumerate(history):
history_toks.extend(sent + ["[eos]"])#[[q,d],[q,d],...]-->[q,d,q,d,...]
segment_ids.extend([0] * (len(sent) + 1))
query_tok += ["[eos]"]
query_tok += ["[SEP]"]
doc_tok += ["[eos]"]
doc_tok += ["[SEP]"]
all_qd_toks = history_toks + query_tok + doc_tok
segment_ids.extend([0] * len(query_tok))
segment_ids.extend([0] * len(doc_tok))
all_attention_mask = [1] * len(all_qd_toks)
assert len(all_qd_toks) <= self._max_seq_length
while len(all_qd_toks) < self._max_seq_length:
all_qd_toks.append("[PAD]")
segment_ids.append(0)
all_attention_mask.append(0)
assert len(all_qd_toks) == len(segment_ids) == len(all_attention_mask) == self._max_seq_length
anno_seq = self._tokenizer.convert_tokens_to_ids(all_qd_toks)
input_ids = np.asarray(anno_seq)
all_attention_mask = np.asarray(all_attention_mask)
segment_ids = np.asarray(segment_ids)
return input_ids, all_attention_mask, segment_ids
def __getitem__(self, idx):
# lines = []
input_ids_all = []
segment_ids_all = []
attention_mask_all = []
did_all = []
label_all = []
pre_qid = -1
pre_sess_qid = -1
pre_sess_did = -1
for i in range(self._candi_cnt):
line = linecache.getline(self._filename, idx * self._candi_cnt + 1 + i)
line = line.strip().split("\t")
# line = line.stripe().split("\t")
label = int(line[0])
length = len(line)-1
assert length & 1 == 0
length = int(length / 2)
# print('length = ', length)
qd_pairs = line[1:1+length] # ['qd', 'qd', ...] the sequence information including the current query and document
qd_id_pairs = [int(item) for item in line[1+length:-2]]
# print('qd paris = {}, qd_id_pairs = {}'.format(qd_pairs, qd_id_pairs))
assert len(qd_pairs) & 1 == 0
assert len(qd_id_pairs) & 1 == 0
input_ids, attention_mask, segment_ids = self.anno_main(qd_pairs)
qd_id_pairs = qd_id_pairs[-(self._max_sess_length * 2):]
qid = int(line[-2])
did = int(line[-1])
# qd_id_pairs = qd_id_pairs[:-2]
len_sess = len(qd_id_pairs) // 2
if len_sess < self._max_sess_length:
qd_id_pairs += [0, 0] * int(self._max_sess_length - len_sess)
array_ = np.array(qd_id_pairs).reshape(self._max_sess_length, 2)
session_qids = array_[:, 0]
session_qids[len_sess] = qid#add qid
session_dids = array_[:, 1]
if pre_qid == -1:
pre_qid = qid
pre_sess_qid = session_qids
pre_sess_did = session_dids
else:
assert pre_qid == qid
for i in range(len(pre_sess_qid)):
assert pre_sess_qid[i] == session_qids[i]
assert pre_sess_did[i] == session_dids[i]
input_ids_all.append(input_ids)
segment_ids_all.append(segment_ids)
attention_mask_all.append(attention_mask)
did_all.append(did)
label_all.append(label)
# print('session qids = {} session dids = {}'.format(session_qids, session_dids))
batch = {
'input_ids': np.array(input_ids_all, dtype=np.int),
'token_type_ids': np.array(segment_ids_all, dtype=np.int),
'attention_mask': np.array(attention_mask_all, dtype=np.int),
'session_qid': session_qids,
'session_did': session_dids,
'session_len': len_sess + 1,
'qid': qid,
'did': np.array(did_all, dtype=np.int),
'label': np.array(label_all, dtype=np.int)
}
return batch
def __len__(self):
return self._total_data