forked from to82350/Viva-Pro.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processors.py
253 lines (205 loc) · 8.81 KB
/
processors.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
import os
import copy
import json
import logging
import torch
from torch.nn import CrossEntropyLoss
from torch.utils.data import TensorDataset
logger = logging.getLogger(__name__)
class InputExample(object):
"""
A single training/test example for simple sequence classification.
"""
def __init__(self, guid, words, labels):
self.guid = guid
self.words = words
self.labels = labels
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, label_ids):
self.input_ids = input_ids
self.attention_mask = attention_mask
self.token_type_ids = token_type_ids
self.label_ids = label_ids
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"
def ner_convert_examples_to_features(
args,
examples,
tokenizer,
max_seq_length,
task,
pad_token_label_id=-100,
):
label_lst = ner_processors[task](args).get_labels()
label_map = {label: i for i, label in enumerate(label_lst)}
features = []
for (ex_index, example) in enumerate(examples):
if ex_index % 10000 == 0:
logger.info("Writing example {} of {}".format(ex_index, len(examples)))
tokens = []
label_ids = []
for word, label in zip(example.words, example.labels):
word_tokens = tokenizer.tokenize(word)
if not word_tokens:
word_tokens = [tokenizer.unk_token] # For handling the bad-encoded word
tokens.extend(word_tokens)
# Use the real label id for the first token of the word, and padding ids for the remaining tokens
label_ids.extend([label_map[label]] + [pad_token_label_id] * (len(word_tokens) - 1))
special_tokens_count = 2
if len(tokens) > max_seq_length - special_tokens_count:
tokens = tokens[:(max_seq_length - special_tokens_count)]
label_ids = label_ids[:(max_seq_length - special_tokens_count)]
# Add [SEP]
tokens += [tokenizer.sep_token]
label_ids += [pad_token_label_id]
# Add [CLS]
tokens = [tokenizer.cls_token] + tokens
label_ids = [pad_token_label_id] + label_ids
token_type_ids = [0] * len(tokens)
input_ids = tokenizer.convert_tokens_to_ids(tokens)
attention_mask = [1] * len(input_ids)
padding_length = max_seq_length - len(input_ids)
input_ids += [tokenizer.pad_token_id] * padding_length
attention_mask += [0] * padding_length
token_type_ids += [0] * padding_length
label_ids += [pad_token_label_id] * padding_length
assert len(input_ids) == max_seq_length
assert len(attention_mask) == max_seq_length
assert len(token_type_ids) == max_seq_length
assert len(label_ids) == max_seq_length
if ex_index < 5:
logger.info("*** Example ***")
logger.info("guid: %s" % example.guid)
logger.info("tokens: %s" % " ".join([str(x) for x in tokens]))
logger.info("input_ids: %s" % " ".join([str(x) for x in input_ids]))
logger.info("attention_mask: %s" % " ".join([str(x) for x in attention_mask]))
logger.info("token_type_ids: %s" % " ".join([str(x) for x in token_type_ids]))
logger.info("label: %s " % " ".join([str(x) for x in label_ids]))
features.append(
InputFeatures(input_ids=input_ids,
attention_mask=attention_mask,
token_type_ids=token_type_ids,
label_ids=label_ids)
)
return features
class NaverNerProcessor(object):
"""Processor for the Naver NER data set """
def __init__(self, args):
self.args = args
def get_labels(self):
return ["O",
"PS-B", "PS-I", "FD-B", "FD-I", "AF-B", "AF-I", "OG-B", "OG-I",
"LC-B", "LC-I", "CV-B", "CV-I", "DT-B", "DT-I", "TI-B", "TI-I",
"QT-B", "QT-I", "EV-B", "EV-I", "AM-B", "AM-I", "PT-B", "PT-I",
"MT-B", "MT-I", "TM-B", "TM-I", "TR-B", "TR-I"]
@classmethod
def _read_file(cls, input_file):
"""Read tsv file, and return words and label as list"""
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, dataset, set_type):
"""Creates examples for the training and dev sets."""
examples = []
for (i, data) in enumerate(dataset):
words, labels = data.split('\t')
words = words.split()
labels = labels.split()
guid = "%s-%s" % (set_type, i)
assert len(words) == len(labels)
if i % 10000 == 0:
logger.info(data)
examples.append(InputExample(guid=guid, words=words, labels=labels))
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,
self.args.task,
file_to_read)))
return self._create_examples(self._read_file(os.path.join(self.args.data_dir,
self.args.task,
file_to_read)), mode)
ner_processors = {
"naver-ner": NaverNerProcessor
}
ner_tasks_num_labels = {
"naver-ner": 31
}
def get_labels(self):
return ["O",
"PS-B", "PS-I", "FD-B", "FD-I", "AF-B", "AF-I", "OG-B", "OG-I",
"LC-B", "LC-I", "CV-B", "CV-I", "DT-B", "DT-I", "TI-B", "TI-I",
"QT-B", "QT-I", "EV-B", "EV-I", "AM-B", "AM-I", "PT-B", "PT-I",
"MT-B", "MT-I", "TM-B", "TM-I", "TR-B", "TR-I"]
def ner_load_and_cache_examples(args, tokenizer, mode):
processor = ner_processors[args.task](args)
# Load data features from cache or dataset file
cached_features_file = os.path.join(
args.data_dir,
"cached_{}_{}_{}_{}".format(
str(args.task),
list(filter(None, args.model_name_or_path.split("/"))).pop(),
str(args.max_seq_len),
mode
)
)
if os.path.exists(cached_features_file):
logger.info("Loading features from cached file %s", cached_features_file)
features = torch.load(cached_features_file)
else:
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 ValueError("For mode, only train, dev, test is avaiable")
pad_token_label_id = CrossEntropyLoss().ignore_index
features = ner_convert_examples_to_features(
args,
examples,
tokenizer,
max_seq_length=args.max_seq_len,
task=args.task,
pad_token_label_id=pad_token_label_id
)
logger.info("Saving features into cached file %s", cached_features_file)
torch.save(features, cached_features_file)
# 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_label_ids = torch.tensor([f.label_ids for f in features], dtype=torch.long)
dataset = TensorDataset(all_input_ids, all_attention_mask, all_token_type_ids, all_label_ids)
return dataset