-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
365 lines (296 loc) · 10.9 KB
/
dataset.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import re
from typing import Any, Dict, List, Tuple
import pandas as pd
import torch
from torch.utils.data import Dataset
from transformers import AutoTokenizer
url_pattern = re.compile(r"https?://\S+")
space_pattern = re.compile(r"\s+")
id_pattern = r"\b[0-9a-z]{20,30}\b" # 5a608819f3a50d049abf68ea
def process_text(text: str) -> str:
text = re.sub(space_pattern, " ", text)
text = re.sub(url_pattern, "(url)", text)
return text.replace("()", "").replace("( ", "(").replace(" )", ")").strip()
def process_desc(text: str) -> str:
text = re.sub(space_pattern, " ", text)
text = re.sub(id_pattern, "(id)", text)
text = text.replace("source_url=", "").replace("source_id=", "")
return text
def traverse_tree(
topic_id: str, id2topic: Dict[str, Any], traverse_cache: Dict[str, Any]
):
"""build context(tree) from a node to its root recursively"""
if topic_id in traverse_cache:
return traverse_cache[topic_id]
target_topic = id2topic[topic_id]
parent_id = str(target_topic["parent"])
if parent_id == "nan":
return [target_topic]
traverse_cache[topic_id] = traverse_tree(parent_id, id2topic, traverse_cache) + [
target_topic
]
return traverse_cache[topic_id]
def build_topic_input(
topic_id: str,
id2topic: Dict[str, Any],
tokenizer: AutoTokenizer,
traverse_cache: dict,
max_seq_len: int = 256,
only_input_text: bool = False,
only_use_leaf: bool = False,
use_topic_parent_desc: bool = False,
) -> Dict[str, Any]:
"""특정 topic으로 부터 tree를 순회하고, text로만 이루어진 context를 만듦"""
if only_use_leaf:
target_topic = id2topic[topic_id]
topic_family = [target_topic]
else:
topic_family = traverse_tree(topic_id, id2topic, traverse_cache)
context_input_ids = []
for idx, topic in enumerate(topic_family):
title = str(topic["title"]).strip()
description = str(topic["description"]).strip()
description = process_desc(description)
is_leaf = idx == len(topic_family) - 1
if title == "nan":
title == "null"
if description == "nan":
description = "null"
if title == "null" and description == "null":
continue
if not use_topic_parent_desc and not is_leaf:
context_str = f"title: {title}."
else:
context_str = f"title: {title}. description: {description}."
topic_inputs = tokenizer.encode_plus(
context_str,
add_special_tokens=False,
return_token_type_ids=False,
truncation=False,
)
context_input_ids.append(topic_inputs["input_ids"])
num_topics = len(context_input_ids)
leaf_len = len(context_input_ids[-1])
remain_len = max_seq_len - leaf_len
if len(context_input_ids[:-1]) > 0:
prev_avg = remain_len // len(context_input_ids[:-1])
#: 평균 길이보다 작은 것과 평균의 차이를 기록할 곳
diffs = []
for input_ids in context_input_ids[:-1]:
if len(input_ids) < prev_avg:
diffs.append(prev_avg - len(input_ids))
new_avg = prev_avg
num_bigger = len(context_input_ids[:-1]) - len(diffs)
if num_bigger > 0:
# 짧은 애들에서 확보한 공간 반영
new_avg += sum(diffs) // num_bigger
# CLS, SEP 토큰 넣어줄 자리는 긴 놈들에서 까기
new_avg -= (num_topics + 1) // num_bigger
for idx, input_ids in enumerate(context_input_ids[:-1]):
context_input_ids[idx] = context_input_ids[idx][:new_avg]
if only_input_text:
merged_input_ids = []
for idx, input_ids in enumerate(context_input_ids):
if idx == len(context_input_ids) - 1:
merged_input_ids += [tokenizer.sep_token_id] + input_ids
else:
merged_input_ids += input_ids
return tokenizer.decode(merged_input_ids)
merged_input_ids = [tokenizer.cls_token_id]
for idx, input_ids in enumerate(context_input_ids):
if idx == len(context_input_ids) - 1:
merged_input_ids += [tokenizer.sep_token_id] + input_ids
else:
merged_input_ids += input_ids
merged_input_ids += [tokenizer.sep_token_id]
pad_len = max_seq_len - len(merged_input_ids)
input_ids = merged_input_ids + [tokenizer.pad_token_id] * pad_len
attention_mask = [1] * len(merged_input_ids) + [0] * pad_len
return {
"id": topic_family[-1]["id"],
"input_ids": input_ids[:max_seq_len],
"attention_mask": attention_mask[:max_seq_len],
"language": topic_family[-1]["language"],
"category": topic_family[-1]["category"],
"has_content": topic_family[-1]["has_content"],
}
def build_content_input(
content_id: str,
id2content: Dict[str, Any],
tokenizer: AutoTokenizer,
max_seq_len: int = 256,
text_max_char_len: int = 1024,
only_input_text: bool = False,
) -> Dict[str, Any]:
"""content의 제목/설명/텍스트로 이루어진 context를 만듦"""
target_content = id2content[content_id]
title = str(target_content["title"]).strip()
description = str(target_content["description"]).strip()
description = process_desc(description)
text = str(target_content["text"]).strip()
kind = target_content["kind"]
if title == description:
description = "nan"
if len(description) <= 1:
description = "nan"
if title == "nan":
title == "null"
if description == "nan":
description = "null"
if text == "nan":
text = "null"
content_str = f"type: {kind}. title: {title}. description: {process_text(description)}. text: {process_text(text[:text_max_char_len])}."
topic_inputs = tokenizer.encode_plus(
content_str,
add_special_tokens=False,
return_token_type_ids=False,
truncation=False,
)
if only_input_text:
return tokenizer.decode(topic_inputs["input_ids"])
_input_ids = (
[tokenizer.cls_token_id]
+ topic_inputs["input_ids"][: max_seq_len - 2]
+ [tokenizer.sep_token_id]
)
pad_len = max_seq_len - len(_input_ids)
input_ids = _input_ids + [tokenizer.pad_token_id] * pad_len
attention_mask = [1] * len(_input_ids) + [0] * pad_len
return {
"id": target_content["id"],
"input_ids": input_ids[:max_seq_len],
"attention_mask": attention_mask[:max_seq_len],
"language": target_content["language"],
}
class LEDataset(Dataset):
def __init__(
self,
topic_ids: List[int],
content_ids: List[int],
id2topic: Dict[str, Any],
id2content: Dict[str, Any],
tokenizer: AutoTokenizer,
topic_max_seq_len=256,
content_max_seq_len=256,
):
super().__init__()
self.topic_ids = topic_ids
self.content_ids = content_ids
self.traverse_cache = dict()
self.id2topic = id2topic
self.id2content = id2content
self.topic_max_seq_len = topic_max_seq_len
self.content_max_seq_len = content_max_seq_len
self.tokenizer = tokenizer
def __getitem__(self, index):
topic_id = self.topic_ids[index]
topic_input = build_topic_input(
topic_id,
self.id2topic,
self.tokenizer,
self.traverse_cache,
self.topic_max_seq_len,
)
content_id = self.content_ids[index]
content_input = build_content_input(
content_id, self.id2content, self.tokenizer, self.content_max_seq_len
)
for k, v in topic_input.items():
if k not in ["input_ids", "attention_mask"]:
continue
topic_input[k] = torch.tensor(v, dtype=torch.long)
for k, v in content_input.items():
if k not in ["input_ids", "attention_mask"]:
continue
content_input[k] = torch.tensor(v, dtype=torch.long)
return topic_input, content_input
def __len__(self):
return len(self.topic_ids)
class LEPairwiseDataset(Dataset):
def __init__(
self,
pairs: List[Tuple[str, str, int]],
df_topic: pd.DataFrame,
df_content: pd.DataFrame,
tokenizer: AutoTokenizer,
topic_max_seq_len=256,
content_max_seq_len=128,
use_topic_parent_desc=False,
):
super().__init__()
self.pairs = pairs
self.df_topic = df_topic
self.df_content = df_content
self.traverse_cache = dict()
self.topic_max_seq_len = topic_max_seq_len
self.content_max_seq_len = content_max_seq_len
self.tokenizer = tokenizer
self.use_topic_parent_desc = use_topic_parent_desc
def __getitem__(self, index):
topic_id, content_id, label = self.pairs[index]
topic_str = build_topic_input(
topic_id,
self.df_topic,
self.tokenizer,
self.traverse_cache,
self.topic_max_seq_len,
only_input_text=True,
only_use_leaf=False,
use_topic_parent_desc=self.use_topic_parent_desc,
)
content_str = build_content_input(
content_id,
self.df_content,
self.tokenizer,
self.content_max_seq_len,
only_input_text=True,
)
return topic_str, content_str, label
def __len__(self):
return len(self.pairs)
class LETripletDataset(Dataset):
def __init__(
self,
triplets: List[Tuple[str, str, int]],
df_topic: pd.DataFrame,
df_content: pd.DataFrame,
tokenizer: AutoTokenizer,
topic_max_seq_len=256,
content_max_seq_len=128,
):
super().__init__()
self.triplets = triplets
self.df_topic = df_topic
self.df_content = df_content
self.traverse_cache = dict()
self.topic_max_seq_len = topic_max_seq_len
self.content_max_seq_len = content_max_seq_len
self.tokenizer = tokenizer
def __getitem__(self, index):
topic_id, pos_content_id, neg_content_id = self.triplets[index]
topic_str = build_topic_input(
topic_id,
self.df_topic,
self.tokenizer,
self.traverse_cache,
self.topic_max_seq_len,
only_input_text=True,
only_use_leaf=False,
)
pos_content_str = build_content_input(
pos_content_id,
self.df_content,
self.tokenizer,
self.content_max_seq_len,
only_input_text=True,
)
neg_content_str = build_content_input(
neg_content_id,
self.df_content,
self.tokenizer,
self.content_max_seq_len,
only_input_text=True,
)
return topic_str, pos_content_str, neg_content_str
def __len__(self):
return len(self.triplets)