File size: 24,221 Bytes
e14cd69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
"""PyTorch Dataset classes for each pipeline component."""
from __future__ import annotations

from typing import Dict, List, Optional, Tuple

import torch
from torch.utils.data import Dataset
from torch.nn.utils.rnn import pad_sequence
from tqdm import tqdm
from src.data.conllu import Sentence, Token


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------

def _load_tokenizer(name: str):
    from transformers import AutoTokenizer

    return AutoTokenizer.from_pretrained(name)


def _build_vocab(items: List[str], initial_vocab: Optional[Dict[str, int]] = None) -> Dict[str, int]:
    vocab = initial_vocab.copy() if initial_vocab is not None else {"<PAD>": 0, "<UNK>": 1}
    for item in sorted(set(items)):
        if item not in vocab:
            vocab[item] = len(vocab)
    return vocab


def _feats_to_str(token: Token) -> str:
    return token.feats_str() if hasattr(token, "feats_str") else str(token.feats)


# ---------------------------------------------------------------------------
# Tokenizer dataset
# ---------------------------------------------------------------------------

class TokenizerDataset(Dataset):
    """Character-level dataset for sentence/token boundary detection."""

    CONTINUATION = 0
    TOKEN_START = 1
    SENTENCE_START = 2

    def __init__(
        self,
        sentences: List[Sentence],
        hf_tokenizer_name: str,
        max_length: int = 512,
        show_progress: bool = False,
    ):
        self.max_length = max_length
        self.hf_tok = AutoTokenizer.from_pretrained(hf_tokenizer_name)
        self.examples: List[Tuple[str, List[int]]] = []
        self._build_examples(sentences, show_progress=show_progress)

    def collate_fn(self, batch: List[Dict[str, torch.Tensor]]) -> Dict[str, torch.Tensor]:
        padding_values = {
            "input_ids": self.hf_tok.pad_token_id,
            "attention_mask": 0,
            "labels": -100,
            "mwt_labels": -100,
        }
        return {
            key: pad_sequence(
                [example[key] for example in batch],
                batch_first=True,
                padding_value=padding_value,
            )
            for key, padding_value in padding_values.items()
        }

    def _build_examples(self, sentences: List[Sentence], *, show_progress: bool = False) -> None:
        chars: List[str] = []
        labels: List[int] = []
        mwt_labels: List[int] = []

        def flush():
            if chars:
                self.examples.append(("".join(chars), list(labels), list(mwt_labels)))
                chars.clear()
                labels.clear()
                mwt_labels.clear()

        for sent in tqdm(
            sentences,
            desc="Preparing tokenizer data",
            unit="sentences",
            disable=not show_progress,
        ):
            tokens = sent.surface_tokens()
            if not tokens:
                continue
            sentence_text = ""
            sentence_labels: List[int] = []
            sentence_mwt_labels: List[int] = []
            for tok_idx, tok in enumerate(tokens):
                form = tok.form
                for char_idx, ch in enumerate(form):
                    if tok_idx == 0 and char_idx == 0:
                        lbl = self.SENTENCE_START
                    elif char_idx == 0:
                        lbl = self.TOKEN_START
                    else:
                        lbl = self.CONTINUATION
                    sentence_text += ch
                    sentence_labels.append(lbl)
                    sentence_mwt_labels.append(
                        int(tok.is_multiword()) if char_idx == 0 else -100
                    )

                # Handle SpaceAfter: add actual whitespace from the Token if available
                if tok_idx < len(tokens) - 1:
                    if tok.space_after:
                        sentence_text += " "
                        sentence_labels.append(self.CONTINUATION)
                        sentence_mwt_labels.append(-100)
                    else:
                        # No space between tokens (e.g. "don't" -> "do", "'t")
                        pass
                elif tok.space_after:
                    # Trailing space at the end of the sentence
                    sentence_text += " "
                    sentence_labels.append(self.CONTINUATION)
                    sentence_mwt_labels.append(-100)

            # Check if adding this sentence exceeds limit; flush first if so.
            if len(chars) + len(sentence_text) > self.max_length and chars:
                flush()
            chars.extend(sentence_text)
            labels.extend(sentence_labels)
            mwt_labels.extend(sentence_mwt_labels)

        flush()

    def __len__(self) -> int:
        return len(self.examples)

    def __getitem__(self, idx: int) -> Dict[str, torch.Tensor]:
        text, char_labels, char_mwt_labels = self.examples[idx]
        encoding = self.hf_tok(
            text,
            return_offsets_mapping=True,
            max_length=self.max_length,
            truncation=True,
            padding=False,
            return_tensors="pt",
        )
        offset_mapping = encoding.pop("offset_mapping").squeeze(0)  # (seq_len, 2)
        input_ids = encoding["input_ids"].squeeze(0)
        attention_mask = encoding["attention_mask"].squeeze(0)
        token_type_ids = encoding.get("token_type_ids", torch.zeros_like(input_ids))
        if isinstance(token_type_ids, torch.Tensor) and token_type_ids.dim() > 1:
            token_type_ids = token_type_ids.squeeze(0)

        seq_len = input_ids.size(0)
        aligned_labels = torch.zeros(seq_len, dtype=torch.long)
        aligned_mwt_labels = torch.full((seq_len,), -100, dtype=torch.long)

        for i, (start, end) in enumerate(offset_mapping.tolist()):
            if start == 0 and end == 0:
                aligned_labels[i] = -100  # special token — ignored
            else:
                # Use the label of the first character of this sub-word.
                # Some tokenizers (e.g. SentencePiece-based ones used by
                # DeBERTa-v3) include the leading whitespace in the offset
                # span of a sub-word that starts a new word. Skip past any
                # such leading whitespace so we don't pick up the space
                # character's CONTINUATION label instead of the real
                # TOKEN_START/SENTENCE_START label of the word.
                char_idx = start
                while char_idx < end and char_idx < len(text) and text[char_idx].isspace():
                    char_idx += 1
                if char_idx < len(char_labels):
                    aligned_labels[i] = char_labels[char_idx]
                    aligned_mwt_labels[i] = char_mwt_labels[char_idx]
                else:
                    aligned_labels[i] = self.CONTINUATION

        return {
            "input_ids": input_ids,
            "attention_mask": attention_mask,
            "labels": aligned_labels,
            "mwt_labels": aligned_mwt_labels,
        }


# ---------------------------------------------------------------------------
# Tagger dataset
# ---------------------------------------------------------------------------

class TaggerDataset(Dataset):
    """Token-level dataset for UPOS/XPOS/FEATS tagging."""

    def __init__(
        self,
        sentences: List[Sentence],
        hf_tokenizer_name: str,
        max_length: int = 512,
        upos_vocab: Optional[Dict[str, int]] = None,
        xpos_vocab: Optional[Dict[str, int]] = None,
        feats_vocab: Optional[Dict[str, int]] = None,
    ):
        self.hf_tok = _load_tokenizer(hf_tokenizer_name)
        self.max_length = max_length

        all_upos = [t.upos for s in sentences for t in s.regular_tokens()]
        all_xpos = [t.xpos for s in sentences for t in s.regular_tokens()]
        all_feats = [_feats_to_str(t) for s in sentences for t in s.regular_tokens()]

        self.upos_vocab = upos_vocab or _build_vocab(all_upos)
        self.xpos_vocab = xpos_vocab or _build_vocab(all_xpos)
        self.feats_vocab = feats_vocab or _build_vocab(all_feats)

        self.sentences = sentences

    def __len__(self) -> int:
        return len(self.sentences)
    
    def collate_fn(self, batch: List[Dict[str, torch.Tensor]]) -> Dict[str, torch.Tensor]:
        """Right-pad tagger examples to the longest sequence in this batch."""
        padding_values = {
            "input_ids": self.hf_tok.pad_token_id,
            "attention_mask": 0,
            "upos_labels": -100,
            "xpos_labels": -100,
            "feats_labels": -100,
        }
        return {
            key: pad_sequence(
                [example[key] for example in batch],
                batch_first=True,
                padding_value=padding_value,
            )
            for key, padding_value in padding_values.items()
        }

    def __getitem__(self, idx: int) -> Dict[str, torch.Tensor]:
        sent = self.sentences[idx]
        tokens = sent.regular_tokens()
        forms = [t.form for t in tokens]

        encoding = self.hf_tok(
            forms,
            is_split_into_words=True,
            max_length=self.max_length,
            truncation=True,
            padding=False,
            return_tensors="pt",
        )
        input_ids = encoding["input_ids"].squeeze(0)
        attention_mask = encoding["attention_mask"].squeeze(0)
        word_ids = encoding.word_ids(batch_index=0)

        seq_len = input_ids.size(0)
        upos_labels = torch.full((seq_len,), -100, dtype=torch.long)
        xpos_labels = torch.full((seq_len,), -100, dtype=torch.long)
        feats_labels = torch.full((seq_len,), -100, dtype=torch.long)

        seen_words = set()
        for i, word_id in enumerate(word_ids):
            if word_id is None or word_id in seen_words:
                continue
            seen_words.add(word_id)
            if word_id < len(tokens):
                tok = tokens[word_id]
                upos_labels[i] = self.upos_vocab.get(tok.upos, self.upos_vocab["<UNK>"])
                xpos_labels[i] = self.xpos_vocab.get(tok.xpos, self.xpos_vocab["<UNK>"])
                feats_labels[i] = self.feats_vocab.get(
                    _feats_to_str(tok), self.feats_vocab["<UNK>"]
                )

        return {
            "input_ids": input_ids,
            "attention_mask": attention_mask,
            "upos_labels": upos_labels,
            "xpos_labels": xpos_labels,
            "feats_labels": feats_labels,
        }


# ---------------------------------------------------------------------------
# Parser dataset
# ---------------------------------------------------------------------------

class ParserDataset(Dataset):
    """Token-level dataset for dependency parsing (HEAD + DEPREL)."""

    def __init__(
        self,
        sentences: List[Sentence],
        hf_tokenizer_name: str,
        max_length: int = 512,
        deprel_vocab: Optional[Dict[str, int]] = None,
    ):
        self.hf_tok = _load_tokenizer(hf_tokenizer_name)
        self.max_length = max_length

        all_deprels = [t.deprel for s in sentences for t in s.regular_tokens()]
        self.deprel_vocab = deprel_vocab or _build_vocab(all_deprels)
        self.sentences = sentences
        self.hf_tok.padding_side = "right"

    def __len__(self) -> int:
        return len(self.sentences)

    def collate_fn(self, batch: List[Dict[str, torch.Tensor]]) -> Dict[str, torch.Tensor]:
        """Right-pad parser examples to the longest sequence in this batch."""
        padding_values = {
            "input_ids": self.hf_tok.pad_token_id,
            "attention_mask": 0,
            "head_labels": -100,
            "deprel_labels": -100,
        }
        return {
            key: pad_sequence(
                [example[key] for example in batch],
                batch_first=True,
                padding_value=padding_value,
            )
            for key, padding_value in padding_values.items()
        }


    def __getitem__(self, idx: int) -> Dict[str, torch.Tensor]:
        sent = self.sentences[idx]
        tokens = sent.regular_tokens()
        forms = [t.form for t in tokens]

        encoding = self.hf_tok(
            forms,
            is_split_into_words=True,
            max_length=self.max_length,
            truncation=True,
            padding=False,
            return_tensors="pt",
        )
        input_ids = encoding["input_ids"].squeeze(0)
        attention_mask = encoding["attention_mask"].squeeze(0)
        word_ids = encoding.word_ids(batch_index=0)

        seq_len = input_ids.size(0)
        head_labels = torch.full((seq_len,), -100, dtype=torch.long)
        deprel_labels = torch.full((seq_len,), -100, dtype=torch.long)

        # Map word-id -> first sub-word position. The arc classifier scores
        # pairs of *sequence positions* (not word indices), and position 0
        # (the CLS token) stands in for the virtual ROOT node. Gold
        # ``tok.head`` values are 1-based CoNLL-U word indices (0 == ROOT),
        # so they must be translated into sequence positions before being
        # used as classification targets — otherwise the labels are
        # meaningless whenever any word is split into multiple sub-words.
        word_positions: Dict[int, int] = {}
        for i, word_id in enumerate(word_ids):
            if word_id is not None and word_id not in word_positions:
                word_positions[word_id] = i

        seen_words = set()
        for i, word_id in enumerate(word_ids):
            if word_id is None or word_id in seen_words:
                continue
            seen_words.add(word_id)
            if word_id < len(tokens):
                tok = tokens[word_id]
                head_pos: Optional[int] = None
                if tok.head is None:
                    # Unknown head (e.g. malformed CoNLL-U data) -> leave
                    # the label ignored (-100) rather than silently
                    # injecting a ROOT target, same treatment as the
                    # truncated-head case below.
                    pass
                elif tok.head == 0:
                    head_pos = 0  # ROOT -> CLS position
                else:
                    # `tok.head` is the gold token's 1-based CoNLL-U index
                    # into `tokens` (`regular_tokens()`), and `word_id`
                    # (from `encoding.word_ids()`, single-example call) is
                    # the matching 0-based index into that same
                    # `forms`/`tokens` list passed to
                    # `is_split_into_words=True` — so `tok.head - 1` is
                    # exactly the `word_id` of the head token. The head
                    # word may have been truncated out of the sequence
                    # (max_length); in that case there's no valid target
                    # position, so leave the label ignored (-100) instead
                    # of silently mislabeling it as ROOT.
                    head_pos = word_positions.get(tok.head - 1)
                if head_pos is not None:
                    head_labels[i] = head_pos
                    deprel_labels[i] = self.deprel_vocab.get(
                        tok.deprel, self.deprel_vocab["<UNK>"]
                    )

        return {
            "input_ids": input_ids,
            "attention_mask": attention_mask,
            "head_labels": head_labels,
            "deprel_labels": deprel_labels,
        }


class LemmatizerDataset(Dataset):
    """Token-level dataset for lemmatization via edit scripts."""

    def __init__(
        self,
        sentences: List[Sentence],
        hf_tokenizer_name: str,
        max_length: int = 512,
        upos_vocab: Optional[Dict[str, int]] = None,
        input_vocab: Optional[Dict[str, int]] = None,
        output_vocab: Optional[Dict[str, int]] = None
    ):
        self.hf_tok = _load_tokenizer(hf_tokenizer_name)
        self.max_length = max_length

        all_upos = [t.upos for s in sentences for t in s.regular_tokens()]
        self.upos_vocab = upos_vocab or _build_vocab(all_upos)

        all_input_chars = [c for s in sentences for t in s.regular_tokens() for c in t.form]
        all_output_chars = [c for s in sentences for t in s.regular_tokens() for c in t.lemma]

        self.input_vocab = input_vocab or _build_vocab(all_input_chars, initial_vocab={"<PAD>": 0, "<UNK>": 1, "<SOS>": 2, "<EOS>": 3})
        self.output_vocab = output_vocab or _build_vocab(all_output_chars, initial_vocab={"<PAD>": 0, "<UNK>": 1, "<SOS>": 2, "<EOS>": 3}) 
        self.sentences = sentences

    def __len__(self) -> int:
        return len(self.sentences)
    
    def collate_fn(self, batch: List[Dict[str, torch.Tensor]]) -> Dict[str, torch.Tensor]:
        """Right-pad lemmatizer examples to the longest sequence in this batch."""
        padding_values = {
            "input_ids": self.hf_tok.pad_token_id,
            "attention_mask": 0,
            "upos_ids": 0,
            "input_char_ids": torch.tensor([[0]]),
            "output_char_ids": torch.tensor([[-100]]),
        }
        output={}
        for key, padding_value in padding_values.items():
            if key=="input_char_ids" or key=="output_char_ids":
                max_len = max(len(example[key][0]) for example in batch)
                padded_sequences = []
                for example in batch:
                    seq = torch.cat([e.unsqueeze(0) for e in example[key]])
                    pad_length = max_len - seq.shape[1]
                    padded_seq = torch.cat([seq, torch.full((seq.shape[0], pad_length), padding_value[0][0], dtype=torch.long)], dim=1)
                    padded_sequences.append(padded_seq)
                
                max_sentence_len=max(seq.shape[0] for seq in padded_sequences)
                repadded_seqs=[]
                for seq in padded_sequences:
                    if seq.shape[0]<max_sentence_len:
                        pad_length=max_sentence_len-seq.shape[0]
                        padded_seq=torch.cat([seq, torch.full((pad_length, seq.shape[1]), padding_value[0][0], dtype=torch.long)], dim=0)
                        repadded_seqs.append(padded_seq)
                    else:
                        repadded_seqs.append(seq)

                output[key]= torch.stack(repadded_seqs, dim=0)
            else:
                output[key] = pad_sequence(
                    [example[key] for example in batch],
                    batch_first=True,
                    padding_value=padding_value,
                )   
        return output

    def encode_chars(self, word_list: List[str], vocab: Dict[str, int]) -> torch.Tensor:
        """Encode a string of characters into a tensor of input IDs."""
        char_ids_list = []
        for text in word_list:
            char_ids = [vocab.get('<SOS>')]+[vocab.get(c, vocab["<UNK>"]) for c in text]+[vocab.get('<EOS>')]
            char_ids_list.append(char_ids)
        max_len = max(len(ids) for ids in char_ids_list)
        for i in range(len(char_ids_list)):
            char_ids_list[i] += [vocab.get('<PAD>')] * (max_len - len(char_ids_list[i]))        
        return torch.tensor(char_ids_list, dtype=torch.long)

    def __getitem__(self, idx: int) -> Dict[str, torch.Tensor]:
        sent = self.sentences[idx]
        tokens = sent.regular_tokens()
        forms = [t.form for t in tokens]
        encoding = self.hf_tok(
            forms,
            is_split_into_words=True,
            max_length=self.max_length,
            truncation=True,
            padding=False,
            return_tensors="pt",
        )
        input_ids = encoding["input_ids"].squeeze(0)
        attention_mask = encoding["attention_mask"].squeeze(0)
        word_ids = encoding.word_ids(batch_index=0)

        seq_len = input_ids.size(0)
        upos_ids = torch.zeros(seq_len, dtype=torch.long)
        all_words=[t.form for t in tokens]+['']
        all_lemmas=[t.lemma for t in tokens]+['']
        input_encoded = self.encode_chars(all_words, self.input_vocab)
        output_encoded = self.encode_chars(all_lemmas, self.output_vocab)
        input_char_ids = []
        output_char_ids = []
        seen_words = set()
        for i, word_id in enumerate(word_ids):
            if word_id is None:
                continue
            tok = tokens[word_id] if word_id < len(tokens) else None
            if tok is not None:
                upos_ids[i] = self.upos_vocab.get(tok.upos, self.upos_vocab["<UNK>"])
            if word_id not in seen_words and tok is not None:
                seen_words.add(word_id)
                input_char_ids.append(input_encoded[word_id])
                output_char_ids.append(output_encoded[word_id])
            else:
                input_char_ids.append(input_encoded[-1])
                output_char_ids.append(output_encoded[-1])

        return {
            "input_ids": input_ids,
            "attention_mask": attention_mask,
            "upos_ids": upos_ids,
            "input_char_ids": input_char_ids,
            "output_char_ids": output_char_ids
        }


class MWTDataset(Dataset):
    """Character-level source/expansion pairs for gold MWT tokens only."""

    def __init__(
        self,
        sentences: List[Sentence],
        output_vocab: Optional[Dict[str, int]] = None,
        input_vocab: Optional[Dict[str, int]] = None,
    ):
        self.examples = [
            (token.form, targets[token.id])
            for sentence in sentences
            for targets in [self._expansion_targets(sentence)]
            for token in sentence.surface_tokens()
            if token.is_multiword() and token.id in targets
        ]
        self.input_vocab = input_vocab or _build_vocab(
            [char for form, _ in self.examples for char in form],
            initial_vocab={"<PAD>": 0, "<UNK>": 1, "<SOS>": 2, "<EOS>": 3},
        )
        target_chars = [
            char
            for _, expansion in self.examples
            for char in expansion
        ]
        self.output_vocab = output_vocab or _build_vocab(
            target_chars,
            initial_vocab={"<PAD>": 0, "<UNK>": 1, "<SOS>": 2, "<EOS>": 3},
        )

    @staticmethod
    def _expansion_targets(sentence: Sentence) -> Dict[int | str, str]:
        targets = {}
        regular_tokens = sentence.regular_tokens()
        for token in sentence.tokens:
            if not token.is_multiword():
                continue
            try:
                start, end = map(int, str(token.id).split("-", 1))
            except ValueError:
                continue
            components = [
                part.form
                for part in regular_tokens
                if isinstance(part.id, int) and start <= part.id <= end
            ]
            if len(components) >= 2:
                targets[token.id] = " ".join(components)
        return targets

    def __len__(self) -> int:
        return len(self.examples)

    def __getitem__(self, index: int) -> Dict[str, torch.Tensor]:
        form, expansion = self.examples[index]
        return {
            "input_char_ids": self.encode_chars(form, self.input_vocab),
            "output_char_ids": self.encode_chars(expansion, self.output_vocab),
        }

    @staticmethod
    def encode_chars(text: str, vocab: Dict[str, int]) -> torch.Tensor:
        return torch.tensor(
            [vocab["<SOS>"], *(vocab.get(char, vocab["<UNK>"]) for char in text), vocab["<EOS>"]],
            dtype=torch.long,
        )

    def collate_fn(
        self,
        batch: List[Dict[str, torch.Tensor]],
    ) -> Dict[str, torch.Tensor]:
        return {
            "input_char_ids": pad_sequence(
                [example["input_char_ids"] for example in batch],
                batch_first=True,
                padding_value=0,
            ),
            "output_char_ids": pad_sequence(
                [example["output_char_ids"] for example in batch],
                batch_first=True,
                padding_value=-100,
            ),
        }