dataset_utils.py
12.1 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
from collections import Counter, defaultdict
from itertools import chain
from datasets import ClassLabel, Sequence
from morfeusz2 import Morfeusz
from .hybrid_tree_utils import tree_from_dataset_instance
from .constants import (
FIRST,
LAST,
MASK_VALUE,
EMPTY,
UPPERCASE,
LOWERCASE,
SEG_BEGIN,
SEG_INSIDE,
TOKENS,
SEGS,
LEMMAS,
LEMMA_CASES,
LEMMA_RULES,
TAGS,
SPINES,
ANCHORS,
ANCHOR_HS,
HEADS,
ADJACENCY_MATRIX,
NE_SPINES,
NE_ANCHORS,
NE_ANCHOR_HS,
NE_HEADS,
NE_ADJACENCY_MATRIX,
)
def make_lemma_rule(token, lemma, tag):
case = UPPERCASE if lemma[0].isupper() else LOWERCASE
prefix_cut = 0
token, lemma = token.lower(), lemma.lower()
#if lemma.startswith('naj') and or
if (token.startswith('nie') and 'neg' in tag) or (token.startswith('naj') and 'sup' in tag):
prefix_cut = 3
token = token[3:]
cut = 0
while token[:(cut + 1)] == lemma[:(cut + 1)] and cut < len(token):
cut += 1
suffix = lemma[cut:]
cut = len(token) - cut
return case, f'{prefix_cut}_{cut}_{suffix}'
def _add_lemma_rules(instance, tag_dict):
tokens = instance[TOKENS]
lemmas = instance[LEMMAS]
tags = [tag_dict[v] for v in instance[TAGS]]
cases, rules = zip(*(make_lemma_rule(*x) for x in zip(tokens, lemmas, tags)))
return {
LEMMA_CASES : cases,
LEMMA_RULES : rules,
}
def _none_to_mask(instance, columns):
ret = dict()
for column in columns:
ret[column] = [v if v is not None else MASK_VALUE for v in instance[column]]
return ret
def cast_labels(dataset, columns):
vals = defaultdict(Counter)
for d in dataset.values():
for column in columns:
vals[column].update(chain.from_iterable(s[column] for s in d))
new_features = dataset['train'].features.copy()
for column in columns:
if None in vals[column]:
vals[column].pop(None)
new_features[column] = Sequence(ClassLabel(names=sorted(vals[column].keys())))
# labels to indices
cast_dataset = dataset.cast(new_features)
# replace None with MASK_VALUE
return cast_dataset.map(lambda instance: _none_to_mask(instance, columns))
def add_lemma_rules(dataset):
tag_dict = dataset['train'].features[TAGS].feature.names
new_dataset = dataset.map(lambda instance: _add_lemma_rules(instance, tag_dict))
return cast_labels(new_dataset, [LEMMA_CASES, LEMMA_RULES])
def _do_collect_spines(tree):
if not tree.children:
return [tree], []
heads = [child for child in tree.children if child.is_head]
assert(len(heads) == 1)
head = heads[0]
paths = []
my_path = [tree]
non_heads = []
for child in tree.children:
child_path, grandchildren_paths = _do_collect_spines(child)
paths += grandchildren_paths
if child == head:
my_path += child_path
else:
non_heads.append(child_path)
for child_path in non_heads:
# h == which <tree.category> counting from the bottom is the anchor
h = [n.category for n in my_path].count(tree.category)
paths.append((tree.category, h, child_path))
return my_path, paths
def _collect_spines(tree):
try:
path, paths = _do_collect_spines(tree)
except:
print(tree.to_brackets())
raise
return {p[-1] : (anchor, h, p[:-1]) for anchor, h, p in [('<ROOT>', '<ROOT>', path)] + paths}
def _compress_spine(spine):
compressed = []
for category in spine:
if category in compressed:
assert(category == compressed[-1])
else:
compressed.append(category)
return compressed
def _add_spines_and_attachments(instance, dataset_features, compress, NER=False):
tree = tree_from_dataset_instance(instance, dataset_features, NER=NER)
new_columns = [NE_SPINES, NE_ANCHORS, NE_ANCHOR_HS] if NER else [SPINES, ANCHORS, ANCHOR_HS]
spines_col, anchors_col, anchor_hs_col = new_columns
if tree is None:
return {
spines_col : [None for _ in instance[TOKENS]],
anchors_col : [None for _ in instance[TOKENS]],
anchor_hs_col : [None for _ in instance[TOKENS]],
}
spines = _collect_spines(tree)
leafs_linear = sorted(tree.get_yield(), key=lambda leaf: leaf.from_index)
rows = []
for leaf in leafs_linear:
anchor, anchor_h, spine = spines[leaf]
spine = [node.category for node in spine]
if compress:
spine = _compress_spine(spine)
spine = '_'.join(spine) if spine else EMPTY
rows.append((spine, anchor, str(anchor_h)))
spines, anchors, anchor_hs = zip(*rows)
return {
spines_col : spines,
anchors_col : anchors,
anchor_hs_col : anchor_hs,
}
def add_spines_and_attachments(dataset, compress=False, NER=False):
dataset_features = dataset['train'].features
new_dataset = dataset.map(
lambda instance: _add_spines_and_attachments(instance, dataset_features, compress=compress, NER=NER))
new_columns = [NE_SPINES, NE_ANCHORS, NE_ANCHOR_HS] if NER else [SPINES, ANCHORS, ANCHOR_HS]
return cast_labels(new_dataset, new_columns)
def _preprocess_ne_heads(instance, root_value, empty_value):
heads = instance[NE_HEADS]
spines = instance[NE_SPINES]
anchors = instance[NE_ANCHORS]
new_heads = [None if s == empty_value and a == root_value else h for h, s, a in zip(heads, spines, anchors)]
return {
NE_HEADS : new_heads,
}
def preprocess_ne_heads(dataset):
root_value = dataset['train'].features[NE_ANCHORS].feature.str2int('ROOT')
empty_value = dataset['train'].features[NE_SPINES].feature.str2int(EMPTY)
return dataset.map(lambda instance: _preprocess_ne_heads(instance, root_value, empty_value))
EDGE, NO_EDGE = 1, 0
def _add_adjacency_matrix(instance, NER=False):
matrix_col = NE_ADJACENCY_MATRIX if NER else ADJACENCY_MATRIX
heads = instance[NE_HEADS if NER else HEADS]
if set(heads) == {MASK_VALUE}:
return {matrix_col : [[MASK_VALUE for j in range(len(heads))] for i in range(len(heads))]}
# ROOT is ‘it’s own’ head
heads = [x if x is not None else i for i, x in enumerate(heads)]
am = [[NO_EDGE for j in range(len(heads))] for i in range(len(heads))]
for i, (token, head) in enumerate(zip(instance[TOKENS], heads)):
am[i][head] = EDGE
return {matrix_col : am}
def add_adjacency_matrix(dataset, NER=False):
return dataset.map(lambda instance: _add_adjacency_matrix(instance, NER=NER))
# https://huggingface.co/docs/transformers/v4.23.1/en/tasks/token_classification
def masked_word_ids(word_ids, masking_strategy=FIRST):
masked = []
for i, word_idx in enumerate(word_ids):
# Set the label for the first/last token of each word.
# Mask the label for:
# * special tokens (word id = None)
# * other tokens in a word
if word_idx is None:
masked.append(None)
else:
if masking_strategy == FIRST:
masked.append(word_idx if word_idx != word_ids[i - 1] else None)
elif masking_strategy == LAST:
masked.append(word_idx if word_idx != word_ids[i + 1] else None)
return masked
def _align_row(values, masked_word_ids):
try:
return [MASK_VALUE if idx is None else values[idx] for idx in masked_word_ids]
except:
print(values)
print(masked_word_ids)
raise
def _align_example(example, masked_ids):
column_names = list(example.keys())
labels = defaultdict(list)
masked_row = [MASK_VALUE for x in masked_ids]
try:
for column_name in column_names:
if column_name in (TOKENS, LEMMAS):
continue
values = example[column_name]
if type(values) == str:
continue
matrix = hasattr(values[0], '__iter__')
if matrix:
aligned_labels = [_align_row(values[idx], masked_ids) if idx is not None else masked_row for idx in masked_ids]
else:
aligned_labels = _align_row(example[column_name], masked_ids)
labels[column_name] = aligned_labels
except:
print(example, masked_ids)
raise
return labels
def morf_tokenize(text, m):
segs = dict()
max_j = 0
for i, j, interp in m.analyse(text):
orth = interp[0]
if (i, j) in segs:
assert (orth == segs[(i, j)])
else:
segs[(i, j)] = orth
max_j = max(max_j, j)
return [segs[(i, i + 1)] for i in range(max_j)]
def _morf_tokenize_and_align(example, morfeusz, masking_strategy=FIRST):
if masking_strategy not in (FIRST, LAST):
raise RuntimeError(f'Uknown masking strategy: {masking_strategy}')
if masking_strategy == LAST:
raise RuntimeError(f'Can’t use {masking_strategy} masking strategy with retokenize')
labels = defaultdict(list)
mask = []
for i, token in enumerate(example[TOKENS]):
for j, morf_token in enumerate(morf_tokenize(token, morfeusz)):
labels[TOKENS].append(morf_token)
labels[SEGS].append(SEG_BEGIN if j == 0 else SEG_INSIDE)
mask.append(i if j == 0 else None)
labels.update(_align_example(example, mask))
return labels
def morfeusz_retokenize(dataset, masking_strategy=FIRST):
morfeusz = Morfeusz(generate=False)
print(f'retokenizing using {morfeusz.dict_id()}')
new_dataset = dataset.map(lambda x: _morf_tokenize_and_align(x, morfeusz, masking_strategy=masking_strategy))
return cast_labels(new_dataset, [SEGS])
def bert_tokenize_and_align(example, tokenizer, masking_strategy=FIRST):
if masking_strategy not in (FIRST, LAST):
raise RuntimeError(f'Uknown masking strategy: {masking_strategy}')
tokenized_inputs = tokenizer(example[TOKENS], truncation=True, is_split_into_words=True)
word_ids = tokenized_inputs.word_ids()
mask = masked_word_ids(word_ids, masking_strategy)
labels = _align_example(example, mask)
tokenized_inputs.update(labels)
return tokenized_inputs
'''
def _remove_columns(self, dataset):
to_keep = ['id', TOKENS] + self.categories + self.categories2d
columns_to_remove = [col for col in dataset.column_names if col not in to_keep]
return dataset.remove_columns(columns_to_remove)
# TODO for 2d categories!!!
def _unify_signatures(self, datasets):
if not datasets:
return None
datasets = [self._remove_columns(dataset) for dataset in datasets]
if len(datasets) == 1:
return datasets
print('unifying datasets:')
for dataset in datasets:
print(len(dataset), 'examples')
for category in self.categories: #TODO!!! + self.categories2d:
values = set()
for dataset in datasets:
if category in dataset.features:
feature = dataset.features[category].feature
if type(feature) == ClassLabel:
values.add(tuple(dataset.features[category].feature.names))
else:
print(type(feature))
1/0
values.add('VALUE')
if len(values) > 1:
print(f'{category}: aligning labels')
mapping = {value : i for i, value in enumerate(sorted(set(chain.from_iterable(values))))}
datasets = [dataset.align_labels_with_mapping(mapping, category) for dataset in datasets]
return datasets
def _join_datasets(self, datasets):
if not datasets:
return None
if self.segmentation:
datasets = [self._retokenize_dataset(d) for d in datasets]
datasets = self._unify_signatures(datasets)
if len(datasets) == 1:
return datasets[0]
print('joining datasets:')
for dataset in datasets:
print(len(dataset), 'examples')
joined = concatenate_datasets(datasets)
print('result:', len(joined), 'examples')
if self.segmentation:
joined = self._retokenize_dataset(joined)
return joined
''';