-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprocess.py
172 lines (151 loc) · 6.64 KB
/
process.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
from bisect import bisect_left
def preprocess(args, examples):
answers = examples["answers"]
# print(examples["question"][0])
# print(examples["context"][0])
examples = args.tokenizer(
examples["question"],
examples["context"],
truncation="only_second",
max_length=args.max_length,
stride=args.stride,
return_overflowing_tokens=True,
return_offsets_mapping=True,
# padding="max_length", #
)
# print(examples["token_type_ids"][0])
examples["start_positions"] = []
examples["end_positions"] = []
for i, (
input_ids,
token_type_ids,
offset_mapping,
overflow_to_sample_mapping,
) in enumerate(
zip(
examples["input_ids"],
examples["token_type_ids"],
examples["offset_mapping"],
examples["overflow_to_sample_mapping"],
)
):
cls_token_idx = input_ids.index(args.tokenizer.cls_token_id)
answer_token_start_idx = answer_token_end_idx = cls_token_idx
# print(examples["token_type_ids"][i])
# print(len(examples.sequence_ids(i)))
# token_type_ids = examples.sequence_ids(i)
token_type_ids = examples.sequence_ids(i)
examples["token_type_ids"][i] = token_type_ids
# print(token_type_ids)
# print(token_type_ids.index(1))
# print(examples.sequence_ids(i).index(1))
answer_info = answers[overflow_to_sample_mapping]
if answer_info:
answer_start_idx = answer_info["answer_start"][0]
answer_end_idx = answer_start_idx + len(answer_info["text"][0])
context_token_start_idx = token_type_ids.index(1)
# Additional step forward(last index - 1) to exclude the last special token
context_token_end_idx = len(token_type_ids) - 2
offset_start_idxs, offset_end_idxs = zip(*offset_mapping)
if (
answer_start_idx >= offset_start_idxs[context_token_start_idx]
and answer_end_idx <= offset_end_idxs[context_token_end_idx]
):
answer_token_start_idx = context_token_start_idx + bisect_left(
offset_start_idxs[context_token_start_idx:context_token_end_idx],
answer_start_idx,
)
answer_token_end_idx = context_token_start_idx + bisect_left(
offset_end_idxs[context_token_start_idx:context_token_end_idx],
answer_end_idx,
)
examples["start_positions"].append(answer_token_start_idx)
examples["end_positions"].append(answer_token_end_idx)
# print(examples["start_positions"])
# print(examples["end_positions"])
args.token_type_ids = examples["token_type_ids"]
# print(examples["token_type_ids"])
if "roberta" in args.config.model_type.lower():
examples.pop("token_type_ids")
return examples
def preprocess_testset(args, examples):
examples = args.tokenizer(
examples["question"],
examples["context"],
truncation="only_second",
max_length=args.max_length,
stride=args.stride,
return_overflowing_tokens=True,
return_offsets_mapping=True,
)
args.token_type_ids = examples["token_type_ids"]
if "roberta" in args.config.model_type.lower():
examples.pop("token_type_ids")
return examples
def preprocess_temp(args, examples):
answers = examples["answers"]
examples = args.tokenizer(
examples["question"],
examples["context"],
truncation="only_second",
max_length=args.max_length,
stride=args.stride,
return_overflowing_tokens=True,
return_offsets_mapping=True,
# padding="max_length", #
)
# print(examples)
sample_mapping = examples.pop("overflow_to_sample_mapping")
offset_mapping = examples.pop("offset_mapping")
examples["start_positions"] = []
examples["end_positions"] = []
for i, offsets in enumerate(offset_mapping):
input_ids = examples["input_ids"][i]
cls_index = input_ids.index(args.tokenizer.cls_token_id) # cls index
# sequence id를 설정합니다 (to know what is the context and what is the question).
sequence_ids = examples.sequence_ids(i)
# 하나의 example이 여러개의 span을 가질 수 있습니다.
sample_index = sample_mapping[i]
# print(examples)
answers_info = answers[sample_index]
# answer가 없을 경우 cls_index를 answer로 설정합니다(== example에서 정답이 없는 경우 존재할 수 있음).
if len(answers_info["answer_start"]) == 0:
examples["start_positions"].append(cls_index)
examples["end_positions"].append(cls_index)
else:
# text에서 정답의 Start/end character index
start_char = answers_info["answer_start"][0]
end_char = start_char + len(answers_info["text"][0])
# text에서 current span의 Start token index
token_start_index = 0
while sequence_ids[token_start_index] != 1:
token_start_index += 1
# text에서 current span의 End token index
token_end_index = len(input_ids) - 1
while sequence_ids[token_end_index] != 1:
token_end_index -= 1
# 정답이 span을 벗어났는지 확인합니다(정답이 없는 경우 CLS index로 label되어있음).
if not (
offsets[token_start_index][0] <= start_char
and offsets[token_end_index][1] >= end_char
):
examples["start_positions"].append(cls_index)
examples["end_positions"].append(cls_index)
else:
# token_start_index 및 token_end_index를 answer의 끝으로 이동합니다.
# Note: answer가 마지막 단어인 경우 last offset을 따라갈 수 있습니다(edge case).
while (
token_start_index < len(offsets)
and offsets[token_start_index][0] <= start_char
):
token_start_index += 1
examples["start_positions"].append(token_start_index - 1)
while offsets[token_end_index][1] >= end_char:
token_end_index -= 1
examples["end_positions"].append(token_end_index + 1)
args.token_type_ids = examples["token_type_ids"]
print(examples["start_positions"])
print(examples["end_positions"])
if "roberta" in args.config.model_type.lower():
examples.pop("token_type_ids")
return examples