-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessing.py
226 lines (194 loc) · 8.45 KB
/
processing.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
import pandas as pd
import numpy as np
from datasets import Dataset
from transformers import AutoTokenizer
import collections
from typing import Tuple, List, Mapping, Union
def prepare_train_features(
examples: Dataset,
tokenizer: AutoTokenizer,
max_length: int = 384,
doc_stride: int = 128,
pad_on_right: bool = True
) -> Mapping[str, List[int]]:
examples["question"] = [q.lstrip() for q in examples["question"]]
tokenized_examples = tokenizer(
examples["question" if pad_on_right else "context"],
examples["context" if pad_on_right else "question"],
truncation="only_second" if pad_on_right else "only_first",
max_length=max_length,
stride=doc_stride,
return_overflowing_tokens=True,
return_offsets_mapping=True,
padding="max_length",
)
sample_mapping = tokenized_examples.pop("overflow_to_sample_mapping")
offset_mapping = tokenized_examples.pop("offset_mapping")
tokenized_examples["start_positions"] = []
tokenized_examples["end_positions"] = []
for i, offsets in enumerate(offset_mapping):
input_ids = tokenized_examples["input_ids"][i]
cls_index = input_ids.index(tokenizer.cls_token_id)
sequence_ids = tokenized_examples.sequence_ids(i)
sample_index = sample_mapping[i]
answers = examples["answers"][sample_index]
if len(answers["answer_start"]) == 0:
tokenized_examples["start_positions"].append(cls_index)
tokenized_examples["end_positions"].append(cls_index)
else:
start_char = answers["answer_start"][0]
end_char = start_char + len(answers["text"][0])
token_start_index = 0
while sequence_ids[token_start_index] != (1 if pad_on_right else 0):
token_start_index += 1
token_end_index = len(input_ids) - 1
while sequence_ids[token_end_index] != (1 if pad_on_right else 0):
token_end_index -= 1
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)
else:
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)
return tokenized_examples
def prepare_validation_features(
examples: Dataset,
tokenizer: AutoTokenizer,
max_length: int = 384,
doc_stride: int = 128,
pad_on_right: bool = True
) -> Mapping[str, List[Union[int, str]]]:
examples["question"] = [q.lstrip() for q in examples["question"]]
tokenized_examples = tokenizer(
examples["question" if pad_on_right else "context"],
examples["context" if pad_on_right else "question"],
truncation="only_second" if pad_on_right else "only_first",
max_length=max_length,
stride=doc_stride,
return_overflowing_tokens=True,
return_offsets_mapping=True,
padding="max_length",
)
sample_mapping = tokenized_examples.pop("overflow_to_sample_mapping")
tokenized_examples["example_id"] = []
for i in range(len(tokenized_examples["input_ids"])):
sequence_ids = tokenized_examples.sequence_ids(i)
context_index = 1 if pad_on_right else 0
sample_index = sample_mapping[i]
tokenized_examples["example_id"].append(examples["id"][sample_index])
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
def postprocess_qa_predictions(
examples: Dataset,
features: Dataset,
raw_predictions: Tuple[np.ndarray, np.ndarray],
tokenizer: AutoTokenizer,
n_best_size: int = 20,
max_answer_length: int = 30
) -> Mapping[str, List[Union[int, str]]]:
all_start_logits, all_end_logits = raw_predictions
example_id_to_index = {k: i for i, k in enumerate(examples["id"])}
features_per_example = collections.defaultdict(list)
for i, feature in enumerate(features):
features_per_example[example_id_to_index[feature["example_id"]]].append(i)
prediction_ids = []
prediction_texts = []
prediction_start_chars = []
prediction_end_chars = []
for example_index, example in enumerate(examples):
feature_indices = features_per_example[example_index]
min_null_score = None # Only used if squad_v2 is True.
valid_answers = []
context = example["context"]
for feature_index in feature_indices:
start_logits = all_start_logits[feature_index]
end_logits = all_end_logits[feature_index]
offset_mapping = features[feature_index]["offset_mapping"]
cls_index = features[feature_index]["input_ids"].index(tokenizer.cls_token_id)
feature_null_score = start_logits[cls_index] + end_logits[cls_index]
if min_null_score is None or min_null_score < feature_null_score:
min_null_score = feature_null_score
start_indexes = np.argsort(start_logits)[-1: -n_best_size - 1: -1].tolist()
end_indexes = np.argsort(end_logits)[-1: -n_best_size - 1: -1].tolist()
for start_index in start_indexes:
for end_index in end_indexes:
if (
start_index >= len(offset_mapping)
or end_index >= len(offset_mapping)
or offset_mapping[start_index] is None
or offset_mapping[end_index] is None
):
continue
if end_index < start_index or end_index - start_index + 1 > max_answer_length:
continue
start_char = offset_mapping[start_index][0]
end_char = offset_mapping[end_index][1]
valid_answers.append(
{
"score": start_logits[start_index] + end_logits[end_index],
"text": context[start_char: end_char],
"start": start_char,
"end": end_char
}
)
if len(valid_answers) > 0:
best_answer = sorted(valid_answers, key=lambda x: x["score"], reverse=True)[0]
else:
best_answer = {"text": "", "score": 0.0, "start": 0, "end": 0}
prediction_ids.append(example["id"])
prediction_texts.append(best_answer["text"])
prediction_start_chars.append(best_answer["start"])
prediction_end_chars.append(best_answer["end"])
return pd.DataFrame({
"id": prediction_ids,
"PredictionString": prediction_texts,
"answer_start": prediction_start_chars,
"answer_end": prediction_end_chars
})
def convert_answers(r):
start = r[0]
text = r[1]
return {
'answer_start': [start],
'text': [text]
}
def filter_pred_strings(predictions: pd.DataFrame) -> List[str]:
bad_starts = [".", ",", "(", ")", "-", "–", ",", ";"]
bad_endings = ["...", "-", "(", ")", "–", ",", ";"]
'''tamil_ad = "கி.பி"
tamil_bc = "கி.மு"
tamil_km = "கி.மீ"
hindi_ad = "ई"
hindi_bc = "ई.पू"'''
cleaned_preds = []
# for pred, context in predictions.to_numpy():
for pred in predictions.to_numpy():
if pred == "":
cleaned_preds.append(pred)
continue
while any([pred.startswith(y) for y in bad_starts]):
pred = pred[1:]
while any([pred.endswith(y) for y in bad_endings]):
if pred.endswith("..."):
pred = pred[:-3]
else:
pred = pred[:-1]
'''if any([
pred.endswith(tamil_ad),
pred.endswith(tamil_bc),
pred.endswith(tamil_km),
pred.endswith(hindi_ad),
pred.endswith(hindi_bc)
]) and pred+"." in context:
pred = pred+"."'''
cleaned_preds.append(pred)
return cleaned_preds