Base Utilities¶
Core data loading, saving, and splitting utilities for neural machine translation.
Overview¶
This module provides foundational functions for working with parallel corpora:
- Load data from TSV, CSV, JSON, and Parquet formats
- Convert parallel text files to DataFrames
- Save processed data
- Split datasets into train/val/test sets
API Reference¶
load_data
¶
Load a single structured data file containing src/tgt columns.
Supports multiple file formats and automatically detects format from file extension if not explicitly specified. Single-file inputs only; for parallel .txt corpora, use parallel_txt_to_dataframe first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
Path | str
|
Path to the data file. |
required |
format
|
str
|
File format ('tsv', 'csv', 'parquet', 'json'). If None, inferred from file extension. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: Loaded data with src/tgt columns. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If format is 'txt' (must use parallel_txt_to_dataframe), or if format is unsupported. |
FileNotFoundError
|
If filepath does not exist. |
Examples:
>>> df = load_data('data.tsv')
>>> df = load_data('data.csv', format='csv')
>>> df = load_data('data.parquet')
Source code in src/torchlingo/preprocessing/base.py
save_data
¶
Save a DataFrame to disk in the specified format.
Creates parent directories as needed. Automatically detects format from file extension if not explicitly specified. Prints a confirmation message with record count and output path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
DataFrame to save with src/tgt columns. |
required |
filepath
|
Path
|
Destination file path. |
required |
format
|
str
|
Output format ('tsv', 'csv', 'parquet', 'json'). If None, inferred from file extension. Defaults to None. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If format is unsupported. |
Examples:
>>> save_data(df, Path('output.tsv'))
Saved 1000 records to output.tsv
>>> save_data(df, Path('output.json'), format='json')
Source code in src/torchlingo/preprocessing/base.py
parallel_txt_to_dataframe
¶
parallel_txt_to_dataframe(src_path: Path, tgt_path: Path, src_col: str = None, tgt_col: str = None, config: Config = None) -> DataFrame
Convert parallel .txt files into a single DataFrame with src/tgt columns.
Reads two aligned text files (source and target) and combines them into a single DataFrame with 'src' and 'tgt' columns. Files must have an equal number of lines. Each line is stripped of whitespace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src_path
|
Path
|
Path to source language file (one sentence per line). |
required |
tgt_path
|
Path
|
Path to target language file (one sentence per line). |
required |
src_col
|
str
|
Column name for source text. Falls back to config.src_col if not provided. Defaults to None. |
None
|
tgt_col
|
str
|
Column name for target text. Falls back to config.tgt_col if not provided. Defaults to None. |
None
|
config
|
Config
|
Configuration object. If None, uses default config. Explicit parameters take priority over config values. |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: DataFrame with columns defined by src_col and tgt_col, containing one parallel pair per row. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If src_path and tgt_path have different number of lines. |
FileNotFoundError
|
If either file does not exist. |
Examples:
>>> df = parallel_txt_to_dataframe('en.txt', 'es.txt')
>>> print(df.columns)
Index(['src', 'tgt'], dtype='object')
>>> len(df)
1000
Source code in src/torchlingo/preprocessing/base.py
split_data
¶
split_data(df: DataFrame, train_ratio: float = None, val_ratio: float = None, seed: int = None, config: Config = None) -> Tuple[DataFrame, DataFrame, DataFrame]
Split a DataFrame into train, validation, and test sets.
Shuffles the entire DataFrame using the provided seed for reproducibility, then partitions by ratio. Test set is the remainder after train and val.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input DataFrame to split. |
required |
train_ratio
|
float
|
Fraction of data for training. Falls back to config.train_ratio if not provided. Defaults to None. |
None
|
val_ratio
|
float
|
Fraction of data for validation. Falls back to config.val_ratio if not provided. Defaults to None. Remaining (1 - train_ratio - val_ratio) goes to test. |
None
|
seed
|
int
|
Random seed for reproducibility. Falls back to config.seed if not provided. Defaults to None. |
None
|
config
|
Config
|
Configuration object. If None, uses default config. Explicit parameters take priority over config values. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Tuple[DataFrame, DataFrame, DataFrame]
|
(train_df, val_df, test_df) as pandas DataFrames. |
Examples:
>>> train, val, test = split_data(df, train_ratio=0.8, val_ratio=0.1)
>>> len(train), len(val), len(test)
(800, 100, 100)
Source code in src/torchlingo/preprocessing/base.py
Examples¶
Loading Data¶
from torchlingo.preprocessing import load_data
# Auto-detect format from extension
df = load_data("data/train.tsv")
# Explicit format
df = load_data("data/train.csv", format="csv")
# Supported formats
df_tsv = load_data("data.tsv") # Tab-separated
df_csv = load_data("data.csv") # Comma-separated
df_json = load_data("data.json") # JSON Lines
df_parquet = load_data("data.parquet") # Parquet
Converting Parallel Files¶
Many NMT datasets come as separate source and target files:
Convert them to a DataFrame:
from torchlingo.preprocessing import parallel_txt_to_dataframe
df = parallel_txt_to_dataframe(
src_path="english.txt",
tgt_path="spanish.txt",
)
print(df)
# src tgt
# 0 Hello world Hola mundo
# 1 Good morning Buenos días
Custom Column Names¶
df = parallel_txt_to_dataframe(
src_path="english.txt",
tgt_path="spanish.txt",
src_col="english",
tgt_col="spanish",
)
Saving Data¶
from torchlingo.preprocessing import save_data
# Save as TSV (recommended)
save_data(df, "output/train.tsv")
# Save as other formats
save_data(df, "output/train.csv")
save_data(df, "output/train.json")
save_data(df, "output/train.parquet")
Splitting Data¶
from torchlingo.preprocessing import train_test_split
train_df, val_df, test_df = train_test_split(
df,
val_ratio=0.1, # 10% validation
test_ratio=0.1, # 10% test
shuffle=True, # Shuffle before splitting
random_state=42, # Reproducibility
)
print(f"Train: {len(train_df)}") # 80%
print(f"Val: {len(val_df)}") # 10%
print(f"Test: {len(test_df)}") # 10%
Format Details¶
TSV (Tab-Separated Values)¶
Recommended for text data because tabs rarely appear in natural text.
CSV (Comma-Separated Values)¶
Requires quoting if text contains commas:
JSON Lines¶
One JSON object per line, good for complex metadata:
Parquet¶
Binary columnar format:
- Efficient compression
- Fast loading for large files
- Not human-readable
# Parquet is great for large datasets
save_data(df, "data/large_corpus.parquet")
df = load_data("data/large_corpus.parquet") # Fast!
Error Handling¶
Mismatched Line Counts¶
# english.txt has 1000 lines, spanish.txt has 999 lines
df = parallel_txt_to_dataframe("english.txt", "spanish.txt")
# Raises: ValueError: Parallel files have different number of lines
Missing Columns¶
Unsupported Format¶
Best Practices¶
- Use TSV for text: Tabs are rare in natural language
- Always shuffle before splitting: Ensures representative splits
- Check data after loading: Verify row counts and sample content
- Use Parquet for large data: Faster loading, smaller files