Dataset¶
PyTorch Dataset implementation for parallel neural machine translation data.
Overview¶
NMTDataset loads parallel text corpora and provides encoded tensor pairs suitable for training. It handles:
- Loading multiple file formats (TSV, CSV, JSON, Parquet)
- Data cleaning (removing blanks, normalizing whitespace)
- Automatic vocabulary building
- Sequence encoding with special tokens
- Length truncation
Quick Start¶
from torchlingo.data_processing import NMTDataset
# Basic usage - vocabularies built automatically
dataset = NMTDataset("data/train.tsv")
# Access a sample
src_tensor, tgt_tensor = dataset[0]
# Use vocabularies
print(f"Source vocab size: {len(dataset.src_vocab)}")
print(f"Target vocab size: {len(dataset.tgt_vocab)}")
API Reference¶
NMTDataset
¶
NMTDataset(data_file: Path, src_col: str = None, tgt_col: str = None, src_tok_col: str = None, tgt_tok_col: str = None, src_vocab: Optional[BaseVocab] = None, tgt_vocab: Optional[BaseVocab] = None, max_length: Optional[int] = None, eos_idx: int = None, config: Config = None)
Bases: Dataset
PyTorch Dataset for parallel neural machine translation data.
Loads a parallel corpus from disk and provides encoded source/target tensor
pairs suitable for training and evaluation. Automatically cleans data by
removing empty rows, normalizing whitespace, and handling missing values.
If vocabulary objects are not provided, the dataset will build simple SimpleVocab
instances from the training data.
The dataset supports optional pre-tokenized columns and enforces a maximum sequence length by truncating longer sequences while preserving the EOS token.
Parameters are resolved with fallback priority: 1. Explicitly passed parameter values 2. Values from passed config object 3. Values from default config (get_default_config())
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_file
|
Path or str
|
Path to a structured file (TSV/CSV/JSON/Parquet)
containing configurable src/tgt columns. Parallel .txt corpora must
be converted to a single file upstream (see
|
required |
src_col
|
str
|
Column name for source text. Falls back to config.src_col if not provided. |
None
|
tgt_col
|
str
|
Column name for target text. Falls back to config.tgt_col if not provided. |
None
|
src_tok_col
|
str
|
Column name for pre-tokenized source text. Falls back to config.src_tok_col if not provided. |
None
|
tgt_tok_col
|
str
|
Column name for pre-tokenized target text. Falls back to config.tgt_tok_col if not provided. |
None
|
src_vocab
|
BaseVocab
|
Pre-built source vocabulary. If None, a SimpleVocab will be created from src_col. Defaults to None. |
None
|
tgt_vocab
|
BaseVocab
|
Pre-built target vocabulary. If None, a SimpleVocab will be created from tgt_col. Defaults to None. |
None
|
max_length
|
int
|
Maximum sequence length in tokens. Sequences longer than this are truncated to (max_length - 1) + eos_idx. Falls back to config.max_seq_length if not provided. |
None
|
eos_idx
|
int
|
Index of the EOS (end-of-sequence) token used for truncation. Falls back to config.eos_idx if not provided. |
None
|
config
|
Config
|
Configuration object to use for default values. If None, uses get_default_config(). |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If data_file does not contain both src_col and tgt_col. |
Attributes:
| Name | Type | Description |
|---|---|---|
df |
DataFrame
|
Loaded and cleaned dataframe. |
src_sentences |
list[str]
|
Raw source sentences. |
tgt_sentences |
list[str]
|
Raw target sentences. |
src_texts |
list[str]
|
Text used for vocab building and encoding — the pre-tokenized columns when present, else the raw sentences. |
tgt_texts |
list[str]
|
Target-side counterpart of src_texts. |
src_vocab |
BaseVocab
|
Source vocabulary. |
tgt_vocab |
BaseVocab
|
Target vocabulary. |
has_tokenized |
bool
|
Whether pre-tokenized columns exist in data. |
eos_idx |
int
|
Index of the EOS token used for truncation. |
Source code in src/torchlingo/data_processing/dataset.py
__len__
¶
Return the number of samples in the dataset.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Total number of (source, target) pairs after cleaning. |
__getitem__
¶
Retrieve and encode a (source, target) pair at the given index.
Encodes raw sentences to token indices using the respective vocabularies, adds special tokens (SOS/EOS), and truncates sequences exceeding max_length while preserving the EOS token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
int
|
Index of the sample to retrieve. Must be in range [0, len(self)). |
required |
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, Tensor]
|
tuple[torch.Tensor, torch.Tensor]: A tuple containing: - src_tensor (torch.Tensor): Encoded source indices with shape (seq_len,) and dtype torch.long. Contains special tokens SOS (at position 0) and EOS (at position -1). - tgt_tensor (torch.Tensor): Encoded target indices with shape (seq_len,) and dtype torch.long. Contains special tokens SOS (at position 0) and EOS (at position -1). |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If vocabularies have not been initialized. |
IndexError
|
If idx is out of range. |
Source code in src/torchlingo/data_processing/dataset.py
Examples¶
Custom Column Names¶
# If your data has different column names
dataset = NMTDataset(
"data/train.csv",
src_col="english",
tgt_col="spanish",
)
Reusing Vocabularies¶
# Build vocab on training data
train_dataset = NMTDataset("data/train.tsv")
# Reuse for validation (important!)
val_dataset = NMTDataset(
"data/val.tsv",
src_vocab=train_dataset.src_vocab,
tgt_vocab=train_dataset.tgt_vocab,
)
With SentencePiece¶
from torchlingo.data_processing import SentencePieceVocab
# Load pre-trained SentencePiece models
src_sp = SentencePieceVocab("models/src.model")
tgt_sp = SentencePieceVocab("models/tgt.model")
dataset = NMTDataset(
"data/train.tsv",
src_vocab=src_sp,
tgt_vocab=tgt_sp,
)
Custom Max Length¶
from torchlingo.config import Config
config = Config(max_seq_length=256)
dataset = NMTDataset("data/train.tsv", config=config)
Attributes¶
| Attribute | Type | Description |
|---|---|---|
df |
pd.DataFrame |
Loaded and cleaned dataframe |
src_sentences |
list[str] |
Raw source sentences |
tgt_sentences |
list[str] |
Raw target sentences |
src_vocab |
SimpleVocab or SentencePieceVocab |
Source vocabulary |
tgt_vocab |
SimpleVocab or SentencePieceVocab |
Target vocabulary |
max_length |
int |
Maximum sequence length |
eos_idx |
int |
End-of-sequence token index |
Data Cleaning¶
NMTDataset automatically cleans your data:
- NaN handling: Replaced with empty strings
- Whitespace: Stripped from beginning and end
- Empty rows: Removed if either source or target is blank
- Type conversion: All text converted to strings
Truncation Behavior¶
Sequences longer than max_length are truncated while preserving the EOS token:
# If max_length=10 and sequence has 15 tokens:
# Original: [SOS, t1, t2, ..., t13, EOS]
# Truncated: [SOS, t1, t2, ..., t8, EOS] # 10 tokens total
Pre-tokenized Data¶
If your data has pre-tokenized columns: