-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathutils.py
259 lines (198 loc) · 5.64 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
__author__ = 'qiao'
# coding=utf-8
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''
Load PubLogBERT dataset.
'''
import json
import logging
import math
import random
random.seed(2023)
from tqdm import tqdm
import torch
from torch.utils.data import TensorDataset
logger = logging.getLogger(__name__)
class QueryDataset(object):
def __init__(
self,
all_qids,
all_input_ids,
all_input_mask,
all_segment_ids
):
self.tensors = [all_input_ids, all_input_mask, all_segment_ids]
self.qid2idx = {qid:idx for idx, qid in enumerate(all_qids)}
def __getitem__(self, qid):
# input_ids, input_mask, segment_ids
return tuple(torch.tensor(tensor[self.qid2idx[qid]], dtype=torch.long) for tensor in self.tensors)
class QueryFeatures(object):
'''
basically a dict
'''
def __init__(
self,
qid,
input_ids,
input_mask,
segment_ids,
):
self.qid = qid
self.input_ids = input_ids
self.input_mask = input_mask
self.segment_ids = segment_ids
def convert_query_to_features(
qid2info,
tokenizer,
max_query_length
):
'''Loads a data file into a list of QueryFeatures.'''
features = []
#for idx, qid in tqdm(enumerate(qid2info.keys())):
#for query, qid in tqdm(query2qid.items()):
for qid, query in tqdm(qid2info.items()):
tokens, input_ids, input_mask, segment_ids = process_query(
query,
tokenizer,
max_query_length
)
if int(qid) < 20:
logger.info("\n*** Example ***")
logger.info("qid: %d" % int(qid))
logger.info("query tokens: %s" % " ".join(tokens))
logger.info("query input_ids: %s" % " ".join([str(x) for x in input_ids]))
logger.info("query input_mask: %s" % " ".join([str(x) for x in input_mask]))
logger.info("query segment_ids: %s" % " ".join([str(x) for x in segment_ids]))
features.append(
QueryFeatures(
qid=qid,
input_ids=input_ids,
input_mask=input_mask,
segment_ids=segment_ids,
)
)
return features
class PubMedDataset(object):
def __init__(
self,
all_pmids,
all_input_ids,
all_input_mask,
all_segment_ids
):
self.tensors = [all_input_ids, all_input_mask, all_segment_ids]
self.pmid2idx = {int(pmid): idx for idx, pmid in enumerate(all_pmids)}
def __getitem__(self, pmid):
# input_ids, input_mask, segment_ids
return tuple(torch.tensor(tensor[self.pmid2idx[pmid]], dtype=torch.long) for tensor in self.tensors)
class PubMedFeatures(object):
'''
basically a dict
'''
def __init__(
self,
pmid,
input_ids,
input_mask,
segment_ids,
):
self.pmid = pmid
self.input_ids = input_ids
self.input_mask = input_mask
self.segment_ids = segment_ids
def convert_pubmed_to_features(
pmid2info,
tokenizer,
max_doc_length
):
'''Loads a data file into a list of PubMedFeatures.'''
features = []
for idx, pmid in tqdm(enumerate(pmid2info.keys())):
tokens, input_ids, input_mask, segment_ids = process_doc(
pmid,
tokenizer,
max_doc_length,
pmid2info
)
if idx < 20:
logger.info("\n*** Example ***")
logger.info("pmid: %d" % int(pmid))
logger.info("doc tokens: %s" % " ".join(tokens))
logger.info("doc input_ids: %s" % " ".join([str(x) for x in input_ids]))
logger.info("doc input_mask: %s" % " ".join([str(x) for x in input_mask]))
logger.info("doc segment_ids: %s" % " ".join([str(x) for x in segment_ids]))
features.append(
PubMedFeatures(
pmid=pmid,
input_ids=input_ids,
input_mask=input_mask,
segment_ids=segment_ids,
)
)
return features
def read_train_examples(input_file):
'''
read the training dataset and convert them to examples
'''
with open(input_file, "r", encoding="utf-8") as f:
lines = f.readlines()
examples = []
for line in lines:
entry = json.loads(line)
example = TrainExample(
qid=int(entry['qid']),
pmid=int(entry['pmid']),
click=entry['click']
)
examples.append(example)
return examples
def process_query(query, tokenizer, max_len):
ori_tokens = tokenizer.tokenize(query)
tokens = []
tokens += ['[CLS]']
tokens += ori_tokens[:(max_len - 2)]
tokens += ['[SEP]']
input_ids = tokenizer.convert_tokens_to_ids(tokens)
segment_ids = [0] * len(tokens)
input_mask = [1] * len(tokens)
# Zero-pad up to the sequence length
while len(input_ids) < max_len:
input_ids.append(0)
input_mask.append(0)
segment_ids.append(0)
assert len(input_ids) == max_len
assert len(input_mask) == max_len
assert len(segment_ids) == max_len
return tokens, input_ids, input_mask, segment_ids
def process_doc(pmid, tokenizer, max_len, pmid2info):
if len(pmid2info[pmid]) < 2:
pmid2info[pmid] += [''] * (2 - len(pmid2info[pmid]))
ti_tokens = tokenizer.tokenize(pmid2info[pmid][0])
ab_tokens = tokenizer.tokenize(pmid2info[pmid][1])
tiab_tokens = ti_tokens + ab_tokens
tokens = []
tokens += tiab_tokens[:(max_len - 1)]
tokens += ['[SEP]']
segment_ids = [1] * len(tokens)
input_mask = [1] * len(tokens)
input_ids = tokenizer.convert_tokens_to_ids(tokens)
# Zero-pad up to the sequence length
while len(input_ids) < max_len:
input_ids.append(0)
input_mask.append(0)
segment_ids.append(1)
assert len(input_ids) == max_len
assert len(input_mask) == max_len
assert len(segment_ids) == max_len
return tokens, input_ids, input_mask, segment_ids