-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdata_loader.py
239 lines (192 loc) · 7.9 KB
/
data_loader.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
import os
import re
import copy
import json
import logging
import torch
from torch.utils.data import TensorDataset
from soynlp.normalizer import emoticon_normalize, repeat_normalize
logger = logging.getLogger(__name__)
def preprocess(title: str,
comment: str):
# Erase redundant \" in the start & end of the title
if title.startswith("\""):
title = title[1:]
if title.endswith("\""):
title = title[:-1]
# Change quotes
title = title.replace("“", "\"").replace("”", "\"").replace("‘", "\'").replace("’", "\'")
# Erase braces in title
braces = r"\[(.*?)\]"
braces2 = r"\{(.*?)\}"
braces3 = r"\【(.*?)\】"
braces4 = r"\<(.*?)\>"
title = re.sub(braces, '', title)
title = re.sub(braces2, '', title)
title = re.sub(braces3, '', title)
title = re.sub(braces4, '', title)
# Normalize the comment
comment = emoticon_normalize(comment, num_repeats=3)
comment = repeat_normalize(comment, num_repeats=3)
return title, comment
class InputExample(object):
""" A single training/test example for simple sequence classification. """
def __init__(self,
guid,
text_a,
text_b,
bias_label,
hate_label):
self.guid = guid
self.text_a = text_a
self.text_b = text_b
self.bias_label = bias_label
self.hate_label = hate_label
def __repr__(self):
return str(self.to_json_string())
def to_dict(self):
"""Serializes this instance to a Python dictionary."""
output = copy.deepcopy(self.__dict__)
return output
def to_json_string(self):
"""Serializes this instance to a JSON string."""
return json.dumps(self.to_dict(), indent=2, sort_keys=True) + "\n"
class InputFeatures(object):
"""A single set of features of data."""
def __init__(self,
input_ids,
attention_mask,
token_type_ids,
bias_label=None,
hate_label=None):
self.input_ids = input_ids
self.attention_mask = attention_mask
self.token_type_ids = token_type_ids
self.bias_label = bias_label
self.hate_label = hate_label
def __repr__(self):
return str(self.to_json_string())
def to_dict(self):
"""Serializes this instance to a Python dictionary."""
output = copy.deepcopy(self.__dict__)
return output
def to_json_string(self):
"""Serializes this instance to a JSON string."""
return json.dumps(self.to_dict(), indent=2, sort_keys=True) + "\n"
class KoreanHateSpeechProcessor(object):
"""Processor for the Korean Hate Speech data set """
def __init__(self, args):
self.args = args
@classmethod
def get_labels(cls):
bias_label_lst = ['none', 'gender', 'others']
hate_label_lst = ['none', 'hate', 'offensive']
return bias_label_lst, hate_label_lst
@classmethod
def _read_file(cls, input_file):
"""Reads a tab separated value file."""
with open(input_file, "r", encoding="utf-8") as f:
lines = []
for line in f:
lines.append(line.strip())
return lines
def _create_examples(self, lines, set_type):
"""Creates examples for the train, dev and test sets."""
examples = []
for (i, line) in enumerate(lines[1:]): # Except the header
line = line.split('\t')
guid = "%s-%s" % (set_type, i)
title = line[0]
comment = line[1]
title, comment = preprocess(title, comment)
bias_label = None
hate_label = None
if set_type != "test":
bias_label = line[2]
hate_label = line[3]
if i % 1000 == 0:
logger.info([title, comment])
examples.append(InputExample(guid=guid,
text_a=comment,
text_b=title,
bias_label=bias_label,
hate_label=hate_label))
return examples
def get_examples(self, mode):
"""
Args:
mode: train, dev, test
"""
file_to_read = None
if mode == 'train':
file_to_read = self.args.train_file
elif mode == 'dev':
file_to_read = self.args.dev_file
elif mode == 'test':
file_to_read = self.args.test_file
logger.info("LOOKING AT {}".format(os.path.join(self.args.data_dir, file_to_read)))
return self._create_examples(self._read_file(os.path.join(self.args.data_dir, file_to_read)), mode)
def convert_examples_to_features(
examples,
tokenizer,
max_length,
):
bias_label_list, hate_label_list = KoreanHateSpeechProcessor.get_labels()
bias_label_map = {label: i for i, label in enumerate(bias_label_list)}
hate_label_map = {label: i for i, label in enumerate(hate_label_list)}
def label_from_example(example):
bias_label_id = -1
hate_label_id = -1
if example.bias_label is not None:
bias_label_id = bias_label_map[example.bias_label]
if example.hate_label is not None:
hate_label_id = hate_label_map[example.hate_label]
return bias_label_id, hate_label_id
labels = [label_from_example(example) for example in examples]
batch_encoding = tokenizer.batch_encode_plus(
[(example.text_a, example.text_b) for example in examples], max_length=max_length, pad_to_max_length=True
)
features = []
for i in range(len(examples)):
inputs = {k: batch_encoding[k][i] for k in batch_encoding}
if "token_type_ids" not in inputs:
inputs["token_type_ids"] = [0] * len(inputs["input_ids"]) # For xlm-roberta, distilkobert
feature = InputFeatures(**inputs, bias_label=labels[i][0], hate_label=labels[i][1])
features.append(feature)
for i, example in enumerate(examples[:5]):
logger.info("*** Example ***")
logger.info("guid: {}".format(example.guid))
logger.info("input_ids: {}".format(" ".join([str(x) for x in features[i].input_ids])))
logger.info("attention_mask: {}".format(" ".join([str(x) for x in features[i].attention_mask])))
logger.info("token_type_ids: {}".format(" ".join([str(x) for x in features[i].token_type_ids])))
logger.info("bias_label: {}".format(features[i].bias_label))
logger.info("hate_label: {}".format(features[i].hate_label))
return features
def load_examples(args, tokenizer, mode):
processor = KoreanHateSpeechProcessor(args)
logger.info("Creating features from dataset file at %s", args.data_dir)
if mode == "train":
examples = processor.get_examples("train")
elif mode == "dev":
examples = processor.get_examples("dev")
elif mode == "test":
examples = processor.get_examples("test")
else:
raise Exception("For mode, Only train, dev, test is available")
features = convert_examples_to_features(
examples,
tokenizer,
args.max_seq_len
)
# Convert to Tensors and build dataset
all_input_ids = torch.tensor([f.input_ids for f in features], dtype=torch.long)
all_attention_mask = torch.tensor([f.attention_mask for f in features], dtype=torch.long)
all_token_type_ids = torch.tensor([f.token_type_ids for f in features], dtype=torch.long)
all_bias_labels = torch.tensor([f.bias_label for f in features], dtype=torch.long)
all_hate_labels = torch.tensor([f.hate_label for f in features], dtype=torch.long)
dataset = TensorDataset(all_input_ids,
all_attention_mask,
all_token_type_ids,
all_bias_labels,
all_hate_labels)
return dataset