dataset_utils.py
15.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import typing
from collections import Counter, defaultdict
from itertools import chain
import logging
logger = logging.getLogger(__name__)
from datasets import ClassLabel, DatasetDict, Features, Sequence
from transformers import BatchEncoding, PreTrainedTokenizerBase
from morfeusz2 import Morfeusz
from .hybrid_tree_utils import tree_from_dataset_instance, TreeNode
from .constants import (
FIRST,
LAST,
MASK_VALUE,
EMPTY,
ALL_UPPER,
ALL_LOWER,
FIRST_UPPER,
KEEP,
SEG_BEGIN,
SEG_INSIDE,
TOKENS,
SEGS,
LEMMAS,
LEMMA_CASES,
LEMMA_RULES,
TAGS,
SPINES,
ANCHORS,
ANCHOR_HS,
HEADS,
ADJACENCY_MATRIX,
GRAPH_HEADS,
GRAPH_DEPRELS,
GRAPH_ADJACENCY_MATRIX,
)
from .type_utils import TokenList, StrLabelList, SimpleLabelList, AnyLabelList, DatasetInstance
from IPython.display import display
# assuming all parts have identical features!
def get_dataset_features(dataset: DatasetDict) -> Features:
any_key = list(dataset.keys())[0]
return dataset[any_key].features
def _none_to_mask(instance: DatasetInstance, columns: list[str]) -> DatasetInstance:
ret: DatasetInstance = {}
for column in columns:
ret[column] = [v if v is not None else MASK_VALUE for v in instance[column]] # type: ignore[literal-required]
return ret
def cast_labels(dataset: DatasetDict, columns: list[str]) -> DatasetDict:
vals: dict[str, set[str]] = defaultdict(set)
for d in dataset.values():
for column in columns:
vals[column].update(chain.from_iterable(s[column] for s in d))
new_features = get_dataset_features(dataset).copy()
for column in columns:
vals[column].discard(None)
new_features[column] = Sequence(ClassLabel(names=sorted(vals[column])))
# 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 get_case_pattern(word: str) -> str:
c = [char.isupper() for char in word]
while len(c) > 1 and c[-1] == c[-2]:
c = c[:-1]
return ''.join('X' if x else 'x' for x in c)
def get_lemma_case_pattern(form: str, lemma: str) -> str:
cf = get_case_pattern(form)
cl = get_case_pattern(lemma)
if cl in (ALL_UPPER, ALL_LOWER, FIRST_UPPER):
return cl
elif cl == cf or form.startswith(lemma):
return KEEP
# fallbacks
elif cl[0] == 'x':
return ALL_LOWER
else:
return FIRST_UPPER
def make_lemma_rule(token: str, lemma: str, tag: str) -> tuple[str, str]:
case = get_lemma_case_pattern(token, lemma)
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: DatasetInstance, tag_dict: dict[int, str]) -> DatasetInstance:
tokens = instance[TOKENS]
lemmas = instance[LEMMAS]
tags = [tag_dict[v] for v in instance[TAGS]] # type: ignore [index] # all v’s are not None
cases, rules = map(list, zip(*(make_lemma_rule(*x) for x in zip(tokens, lemmas, tags)))) # type: ignore [arg-type]
return {
LEMMA_CASES : cases,
LEMMA_RULES : rules,
}
def add_lemma_rules(dataset: DatasetDict) -> DatasetDict:
tag_dict = get_dataset_features(dataset)[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: TreeNode) -> tuple[list[TreeNode], list[tuple[str, int, list[TreeNode]]]]:
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
assert(tree.category is not None)
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: TreeNode) -> dict[TreeNode, tuple[str, int, list[TreeNode]]]:
try:
path, paths = _do_collect_spines(tree)
except:
logger.critical('Couldn’t colllect spines:')
logger.critical(tree.to_brackets())
raise
return {p[-1] : (anchor, h, p[:-1]) for anchor, h, p in [('<ROOT>', -1, path)] + paths}
def _compress_spine(spine: list[str]) -> list[str]:
compressed: list[str] = []
for category in spine:
if category in compressed:
assert(category == compressed[-1])
else:
compressed.append(category)
return compressed
def _add_spines_and_attachments(instance: DatasetInstance, dataset_features: Features, compress: bool) -> DatasetInstance:
tree = tree_from_dataset_instance(instance, dataset_features)
if tree is None:
return {
SPINES : [None for _ in instance[TOKENS]], # type: ignore[typeddict-item]
ANCHORS : [None for _ in instance[TOKENS]], # type: ignore[typeddict-item]
ANCHOR_HS : [None for _ in instance[TOKENS]], # type: ignore[typeddict-item]
}
spines = _collect_spines(tree)
leafs_linear = sorted(tree.get_yield(), key=lambda leaf: leaf.from_index) # type: ignore[arg-type, return-value] # all from_index’s are not None
rows: list[tuple[str, str, str]] = []
for leaf in leafs_linear:
anchor, anchor_h, spine = spines[leaf]
spine_cats: list[str] = [node.category for node in spine] # type: ignore[misc] # all category’s are not None
if compress:
spine_cats = _compress_spine(spine_cats)
spine_label = '_'.join(spine_cats) if spine_cats else EMPTY
rows.append((spine_label, anchor, str(anchor_h) if anchor_h != -1 else '<ROOT>'))
spine_labels, anchors, anchor_hs = map(list, zip(*rows))
return {
SPINES : spine_labels,
ANCHORS : anchors,
ANCHOR_HS : anchor_hs,
}
def add_spines_and_attachments(dataset: DatasetDict, compress: bool = False) -> DatasetDict:
dataset_features = get_dataset_features(dataset)
new_dataset = dataset.map(
lambda instance: _add_spines_and_attachments(instance, dataset_features, compress=compress))
return cast_labels(new_dataset, [SPINES, ANCHORS, ANCHOR_HS])
EDGE, NO_EDGE = 1, 0
def _add_adjacency_matrix(instance: DatasetInstance) -> DatasetInstance:
heads = instance[HEADS]
if set(heads) == {MASK_VALUE}:
return {ADJACENCY_MATRIX : [[MASK_VALUE for j in range(len(heads))] for i in range(len(heads))]}
# ROOT is ‘it’s own’ head
int_heads: list[int] = [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, head in enumerate(int_heads):
am[i][head] = EDGE
return {ADJACENCY_MATRIX : am} # type: ignore[typeddict-item]
def add_adjacency_matrix(dataset: DatasetDict) -> DatasetDict:
return dataset.map(lambda instance: _add_adjacency_matrix(instance))
def _add_graph_matrices(instance: DatasetInstance) -> DatasetInstance:
heads = instance[GRAPH_HEADS]
deprels = instance[GRAPH_DEPRELS]
N = len(heads)
if set(chain.from_iterable(heads)) == {MASK_VALUE}:
return {
GRAPH_ADJACENCY_MATRIX : [[MASK_VALUE for j in range(N)] for i in range(N)],
GRAPH_DEPRELS : [[MASK_VALUE for j in range(N)] for i in range(N)],
}
# ROOT is ‘it’s own’ head
am = [[NO_EDGE for j in range(N)] for i in range(N)]
# deprels: mask unless there is an edge
dm = [[MASK_VALUE for j in range(N)] for i in range(N)]
for i, (graph_heads, graph_deprels) in enumerate(zip(heads, deprels)):
for head, deprel in zip(graph_heads, graph_deprels):
if head is None:
head = i
am[i][head] = EDGE
dm[i][head] = deprel # type: ignore[assignment]
return {GRAPH_ADJACENCY_MATRIX : am, GRAPH_DEPRELS : dm} # type: ignore[typeddict-item]
def add_graph_matrices(dataset: DatasetDict) -> DatasetDict:
return dataset.map(lambda instance: _add_graph_matrices(instance))
# https://huggingface.co/docs/transformers/v4.23.1/en/tasks/token_classification
def masked_word_ids(word_ids: typing.Sequence[int | None], masking_strategy: str = FIRST) -> list[int | None]:
masked: list[int | None] = []
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: SimpleLabelList, masked_word_ids: list[int | None]) -> SimpleLabelList: # in fact, it’s an IntLabelList
try:
return [MASK_VALUE if idx is None else values[idx] for idx in masked_word_ids] # type: ignore[return-value]
except:
logger.critical('Couldn’t align row:')
logger.critical(f'values: {values}')
logger.critical(f'masked_word_ids: {masked_word_ids}')
raise
def _align_example(example: DatasetInstance, masked_ids: list[int | None]) -> DatasetInstance:
labels: DatasetInstance = {}
masked_row = [MASK_VALUE for x in masked_ids]
try:
for column_name, values in example.items():
if column_name in (TOKENS, LEMMAS):
continue
if type(values) == str:
continue
matrix = hasattr(values[0], '__iter__') # type: ignore[index] # [*] because of the items() iteration, mypy complains about values’ type
aligned_labels: AnyLabelList
if matrix:
aligned_labels = [_align_row(values[idx], masked_ids) if idx is not None else masked_row for idx in masked_ids] # type: ignore[index, assignment] # [*]
else:
aligned_labels = _align_row(values, masked_ids) # type: ignore[arg-type] # [*]
labels[column_name] = aligned_labels # type: ignore[literal-required]
except:
logger.critical('Couldn’t align example:')
logger.critical(f'example: {example}')
logger.critical(f'column_name: {column_name}')
logger.critical(f'example[column_name]: {values}')
logger.critical(f'masked_ids: masked_ids')
raise
return labels
def morf_tokenize(text: str, m: Morfeusz) -> TokenList:
segs: dict[tuple[int, int], str] = {}
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)
#12 13 ('<', '<', 'sym', [], [])
#12 14 ('<3', '<3', 'sym', [], [])
#13 15 ('33', '33', 'dig', [], [])
#14 15 ('3', '3', 'dig', [], [])
'''
for i in range(max_j):
if (i, i + 1) not in segs:
s1 = segs.get((i - 1, i + 1), None)
s2 = segs.get((i - 1, i), None)
if s1 and s2:
logger.debug(text)
logger.debug(f' adding token: ({i}, {i + 1}) ‹{s1[len(s2):]}›')
segs[(i, i + 1)] = s1[len(s2):]
'''
# Morfeusz DAG vertices <-> positions in the text
dag_to_text_idx = {0 : 0}
for (i, j), seg in sorted(segs.items()):
assert(i in dag_to_text_idx)
jdx = dag_to_text_idx[i] + len(seg)
if j in dag_to_text_idx:
assert(dag_to_text_idx[j] == jdx)
else:
dag_to_text_idx[j] = jdx
for (i, j), seg in sorted(segs.items()):
try:
assert(seg == text[dag_to_text_idx[i]:dag_to_text_idx[j]])
except:
logger.error(f'Couldn’t get Morfeusz tokenisation: for ‘{text}’')
logger.error(m.analyse(text))
logger.error(segs)
logger.error(dag_to_text_idx)
logger.error('Leaving this segment untokenised')
return [text]
try:
# return [segs[(i, i + 1)] for i in range(max_j)]
return [text[dag_to_text_idx[i]:dag_to_text_idx[i + 1]] for i in range(max_j)]
except:
logger.error(f'Couldn’t get Morfeusz tokenisation: for ‘{text}’')
logger.error(m.analyse(text))
logger.error(segs)
logger.error(dag_to_text_idx)
logger.error('Leaving this segment untokenised')
return [text]
def _morf_tokenize_and_align(example: DatasetInstance, morfeusz: Morfeusz, masking_strategy: str = FIRST) -> DatasetInstance:
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: DatasetInstance = {}
tokens: TokenList = []
segs: StrLabelList = []
mask = []
for i, token in enumerate(example[TOKENS]):
for j, morf_token in enumerate(morf_tokenize(token, morfeusz)):
tokens.append(morf_token)
segs.append(SEG_BEGIN if j == 0 else SEG_INSIDE)
mask.append(i if j == 0 else None)
labels[TOKENS] = tokens
labels[SEGS] = segs
labels.update(_align_example(example, mask))
return labels
def morfeusz_retokenize(
dataset: DatasetDict,
masking_strategy: str = FIRST,
morfeusz_dict_path: str | None = None,
morfeusz_dict_name: str | None = None
) -> DatasetDict:
morfeusz = Morfeusz(generate=False, dict_path=morfeusz_dict_path, dict_name=morfeusz_dict_name)
logger.info(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 lm_tokenize_and_align(
example: DatasetInstance,
tokenizer: PreTrainedTokenizerBase,
masking_strategy: str = FIRST
) -> BatchEncoding:
if masking_strategy not in (FIRST, LAST):
raise RuntimeError(f'Uknown masking strategy: {masking_strategy}')
tokenized_inputs: BatchEncoding = 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