"""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 {"": 0, "": 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[""]) xpos_labels[i] = self.xpos_vocab.get(tok.xpos, self.xpos_vocab[""]) feats_labels[i] = self.feats_vocab.get( _feats_to_str(tok), self.feats_vocab[""] ) 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[""] ) 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={"": 0, "": 1, "": 2, "": 3}) self.output_vocab = output_vocab or _build_vocab(all_output_chars, initial_vocab={"": 0, "": 1, "": 2, "": 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] 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('')]+[vocab.get(c, vocab[""]) for c in text]+[vocab.get('')] 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('')] * (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[""]) 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={"": 0, "": 1, "": 2, "": 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={"": 0, "": 1, "": 2, "": 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[""], *(vocab.get(char, vocab[""]) for char in text), vocab[""]], 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, ), }