-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprocessor.py
192 lines (153 loc) · 9.25 KB
/
processor.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
class DataProcessor:
def __init__(
self,
data_args,
sketch_tokenizer,
intensive_tokenizer,
column_names,
):
self.data_args = data_args
self.sketch_tokenizer = sketch_tokenizer
self.intensive_tokenizer = intensive_tokenizer
self.question_column_name = "question" if "question" in column_names else column_names[0]
self.context_column_name = "context" if "context" in column_names else column_names[1]
self.answer_column_name = "answers" if "answers" in column_names else column_names[2]
# self.max_seq_length = min(self.data_args.max_seq_length, self.tokenizer.model_max_length)
self.max_seq_length = self.data_args.max_seq_length
# self.pad_on_right = self.tokenizer.padding_side == "right"
self.pad_on_right = True
def prepare_train_features_for_sketch_reader(self, examples):
examples[self.question_column_name] = [q.lstrip() for q in examples[self.question_column_name]]
tokenized_examples = self.sketch_tokenizer(
examples[self.question_column_name if self.pad_on_right else self.context_column_name],
examples[self.context_column_name if self.pad_on_right else self.question_column_name],
truncation="only_second" if self.pad_on_right else "only_first",
max_length=self.max_seq_length,
stride=self.data_args.doc_stride,
return_overflowing_tokens=True,
return_offsets_mapping=False,
padding="max_length" if self.data_args.pad_to_max_length else False,
)
tokenized_examples["labels"] = []
sample_mapping = tokenized_examples.pop("overflow_to_sample_mapping")
for i in range(len(tokenized_examples["input_ids"])):
sample_index = sample_mapping[i]
# answerable: 0, unanswerable: 1
is_impossible = examples["is_impossible"][sample_index]
tokenized_examples["labels"].append(0 if not is_impossible else 1)
return tokenized_examples
def prepare_eval_features_for_sketch_reader(self, examples):
examples[self.question_column_name] = [q.lstrip() for q in examples[self.question_column_name]]
tokenized_examples = self.sketch_tokenizer(
examples[self.question_column_name if self.pad_on_right else self.context_column_name],
examples[self.context_column_name if self.pad_on_right else self.question_column_name],
truncation="only_second" if self.pad_on_right else "only_first",
max_length=self.max_seq_length,
stride=self.data_args.doc_stride,
return_overflowing_tokens=True,
return_offsets_mapping=False,
padding="max_length" if self.data_args.pad_to_max_length else False,
)
tokenized_examples["labels"] = []
tokenized_examples["example_id"] = []
sample_mapping = tokenized_examples.pop("overflow_to_sample_mapping")
for i in range(len(tokenized_examples["input_ids"])):
sample_index = sample_mapping[i]
id_col = examples["guid"][sample_index]
tokenized_examples["example_id"].append(id_col)
# answerable: 0, unanswerable: 1
is_impossible = examples["is_impossible"][sample_index]
tokenized_examples["labels"].append(0 if not is_impossible else 1)
return tokenized_examples
def prepare_train_features_for_intensive_reader(self, examples):
examples[self.question_column_name] = [q.lstrip() for q in examples[self.question_column_name]]
tokenized_examples = self.intensive_tokenizer(
examples[self.question_column_name if self.pad_on_right else self.context_column_name],
examples[self.context_column_name if self.pad_on_right else self.question_column_name],
truncation="only_second" if self.pad_on_right else "only_first",
max_length=self.max_seq_length,
stride=self.data_args.doc_stride,
return_overflowing_tokens=True,
return_offsets_mapping=True,
padding="max_length" if self.data_args.pad_to_max_length else False,
)
sample_mapping = tokenized_examples.pop("overflow_to_sample_mapping")
offset_mapping = tokenized_examples.pop("offset_mapping")
tokenized_examples["start_positions"] = []
tokenized_examples["end_positions"] = []
tokenized_examples["is_impossible"] = []
tokenized_examples["cls_idx"] = []
for i, offsets in enumerate(offset_mapping):
input_ids = tokenized_examples["input_ids"][i]
cls_index = input_ids.index(self.intensive_tokenizer.cls_token_id)
tokenized_examples["cls_idx"].append(cls_index)
sequence_ids = tokenized_examples.sequence_ids(i)
sample_index = sample_mapping[i]
answers = examples[self.answer_column_name][sample_index]
is_impossible = examples["is_impossible"][sample_index]
# is_impossible = "is_impossible"
# tokenized_examples[is_impossible].append(examples[is_impossible][sample_index] - 1) # 1부터 시작이므로
# If no answers are given, set the cls_index as answer.
if is_impossible or len(answers["answer_start"]) == 0:
tokenized_examples["start_positions"].append(cls_index)
tokenized_examples["end_positions"].append(cls_index)
tokenized_examples["is_impossible"].append(1.0)
else:
# Start/end character index of the answer in the text.
start_char = answers["answer_start"][0]
end_char = start_char + len(answers["text"][0])
# Start token index of the current span in the text.
token_start_index = 0
while sequence_ids[token_start_index] != (1 if self.pad_on_right else 0):
token_start_index += 1
# End token index of the current span in the text.
token_end_index = len(input_ids) - 1
while sequence_ids[token_end_index] != (1 if self.pad_on_right else 0):
token_end_index -= 1
# Detect if the answer is out of the span (in which case this feature is labeled with the CLS index).
if not (
offsets[token_start_index][0] <= start_char and offsets[token_end_index][1] >= end_char
):
tokenized_examples["start_positions"].append(cls_index)
tokenized_examples["end_positions"].append(cls_index)
tokenized_examples["is_impossible"].append(1.0)
else:
# Otherwise move the token_start_index and token_end_index to the two ends of the answer.
# Note: we could go after the last offset if the answer is the last word (edge case).
while token_start_index < len(offsets) and offsets[token_start_index][0] <= start_char:
token_start_index += 1
tokenized_examples["start_positions"].append(token_start_index - 1)
while offsets[token_end_index][1] >= end_char:
token_end_index -= 1
tokenized_examples["end_positions"].append(token_end_index + 1)
tokenized_examples["is_impossible"].append(0.0)
return tokenized_examples
def prepare_eval_features_for_intensive_reader(self, examples):
examples[self.question_column_name] = [q.lstrip() for q in examples[self.question_column_name]]
tokenized_examples = self.intensive_tokenizer(
examples[self.question_column_name if self.pad_on_right else self.context_column_name],
examples[self.context_column_name if self.pad_on_right else self.question_column_name],
truncation="only_second" if self.pad_on_right else "only_first",
max_length=self.max_seq_length,
stride=self.data_args.doc_stride,
return_overflowing_tokens=True,
return_offsets_mapping=True,
padding="max_length" if self.data_args.pad_to_max_length else False,
)
sample_mapping = tokenized_examples.pop("overflow_to_sample_mapping")
tokenized_examples["example_id"] = []
for i in range(len(tokenized_examples["input_ids"])):
# Grab the sequence corresponding to that example (to know what is the context and what is the question).
sequence_ids = tokenized_examples.sequence_ids(i)
input_ids = tokenized_examples["input_ids"][i]
context_index = 1 if self.pad_on_right else 0
# One example can give several spans, this is the index of the example containing this span of text.
sample_index = sample_mapping[i]
tokenized_examples["example_id"].append(examples["guid"][sample_index])
# Set to None the offset_mapping that are not part of the context so it's easy to determine if a token
# position is part of the context or not.
tokenized_examples["offset_mapping"][i] = [
(o if sequence_ids[k] == context_index else None)
for k, o in enumerate(tokenized_examples["offset_mapping"][i])
]
return tokenized_examples