radlab‑data
A versatile Python library for loading, preprocessing, and handling a variety of textual data sources (PDF, DOCX, TXT, MD, HTML,
and raw strings) and preparing them for downstream machine‑learning tasks such as classification, sequence labeling, and
generative modeling.
Table of Contents
Overview
radlab-data provides a unified interface for:
- Reading documents from common file formats (PDF, DOCX, TXT, MD, HTML) as well as plain‑text strings.
- Cleaning and normalising raw text using a chain of configurable processors.
- Splitting text into sentences, paragraphs, or token‑level chunks.
- Mapping raw annotations (e.g., NER spans) onto tokenised representations.
- Packaging data into HuggingFace
datasets.Dataset objects, ready for training with transformers.
The library is built with extensibility in mind – you can plug in your own tokenizers, preprocessing steps, or custom
dataset classes.
Features
- Multi‑format document loading – PDF, DOCX, TXT, MD, HTML, and custom “input_text”.
- Automatic language detection (via
ftlangdetect).
- Flexible text cleaning – phrase removal, abbreviation normalisation, regex‑based sanitisation, etc.
- Sentence‑level splitting using spaCy’s Polish model (
pl_core_news_lg).
- Special token handling – conditional insertion of tokens based on metadata rules.
- Dataset abstractions for:
- Text classification
- Sequence labeling (NER, IOB tagging)
- Sequence‑to‑sequence tasks (e.g., translation, summarisation)
- Semantic similarity and dialogue alternatives
- Caching of tokenised datasets for fast reloads.
- Multiprocessing support for loading large corpora.
- Command‑line tools for dataset conversion, preprocessing, and training argument handling.
Installation
# Create a virtual environment (optional but recommended)
python -m venv .venv
source .venv/bin/activate # on Windows: .venv\Scripts\activate
# Core only: document loading, cleaning and small utilities
pip install -e .
# Everything, including the model stack
pip install -e ".[all]"
Note: The library requires Python 3.10+.
Dependency extras
The core installs only the light packages needed to read and clean documents
(tqdm, requests, beautifulsoup4, python-docx, pypdf). Importing the
document loaders, Document, DirectoryFileReader or TextUtils therefore
does not pull in torch, transformers or spacy — a query-only host
(for example the Semantic Search Engine API)
can install the core alone.
| Extra |
Installs |
Needed for |
ml |
transformers, torch, datasets, numpy, scikit-learn, scipy, matplotlib |
token-level chunking (max_tokens_in_chunk), HF datasets, metrics and training helpers |
spacy |
spacy |
SentenceSplitter and PairReader (Polish model: python -m spacy download pl_core_news_lg) |
lang |
fasttext-langdetect, deepl |
TextUtils.text_language, TextUtils.translate_text_deepl |
tables |
pandas, openpyxl |
dataset preprocessing and spreadsheet input |
all |
all of the above |
the full feature set |
Heavy imports are lazy: transformers is imported inside
radlab_data.text.loaders.loader_i.get_tokenizer() (so inside the first use of
token chunking), spacy inside
radlab_data.text.processors.splitter.get_nlp(), and ftlangdetect/deepl
inside the TextUtils methods which use them. requirements.txt lists every
extra for a full development environment.
Quick Start
from radlab_data.text.document import Document
from radlab_data.datasets.dataset_loader import DatasetLoader
from transformers import AutoTokenizer
# 1️⃣ Load a document (PDF, DOCX, TXT, MD, HTML, or raw text)
doc = Document(
file_path="data/example.pdf",
prepare_proper_pages=True,
clear_texts=True,
use_text_denoiser=False,
)
pages = doc.load() # pages is a list of `DocumentPage` objects
# 2️⃣ Initialise a tokenizer
tokenizer = AutoTokenizer.from_pretrained("radlab/polish-fast-tokenizer")
# 3️⃣ Load a dataset (JSON or JSONL) and automatically cache tokenisation
loader = DatasetLoader(
dataset_path="data/my_dataset",
tokenizer=tokenizer,
split_train_valid_test="0.8:0.1", # 80 % train, 10 % validation, 10 % test
pre_shuffle=True,
)
dataset = loader.load()
# 4️⃣ Access train / validation / test splits
train = dataset.train_dataset
valid = dataset.valid_dataset
test = dataset.test_dataset
The resulting train, valid, and test objects are HuggingFace datasets.Dataset instances, ready to be passed to a
transformers trainer.
Core Concepts
Dataset Types
| Class |
Typical Use‑Case |
Key Characteristics |
TextClassificationDataset |
Single‑label classification |
Stores text and a scalar label. |
SequenceLabellingDataset |
NER / token‑level tagging |
Generates IOB tags aligned to tokenised inputs. |
SequenceToSequenceDataset |
Translation, summarisation, etc. |
Handles source‑target pairs (text ↔ label). |
SemanticSimilarityDataset |
Pairwise similarity / ranking |
Provides sentence1, sentence2, and a similarity score. |
DPODataset |
Preference‑based learning (chosen vs. rejected) |
Keeps raw prompts and two possible completions. |
DialogueAlternativesDataset |
Multi‑choice dialogue generation |
Stores a shared context and several possible endings with scores. |
All dataset classes inherit from DatasetBaseClass, which implements caching, optional shuffling, and split handling.
Text Loaders
| Loader |
Supported Extension |
Behaviour |
PDFLoader |
.pdf |
One page per PDF page via pypdf. |
TXTLoader |
.txt |
Whole file as one page, with encoding autodetection. |
MarkdownLoader |
.md |
Reads like TXTLoader, optional markup stripping. |
DOCXLoader |
.docx |
Parses paragraphs and tables via python-docx. |
HTMLLoader |
.html |
Strips markup via BeautifulSoup, split_by_paragraph. |
InputTextLoader |
.input_text |
Wraps a raw string supplied via options. |
Loaders share a common base (LoaderI) that handles:
- Tokeniser initialisation,
- Optional language detection,
- Text denoising,
- Chunking into token‑size windows.
Every loader returns DocumentPage objects (page_content plus a metadata
dictionary), so no document-loading framework is needed to use radlab-data.
Markdown files are read as plain text and every page is tagged with
document_format: markdown. Pass the strip_markdown option to remove markdown markup
(headings, emphasis, links, code fences, list markers) while keeping the underlying text:
from radlab_data.text.loaders.markdown import MarkdownLoader
loader = MarkdownLoader("docs/README.md", options={"strip_markdown": True})
pages = loader.load()
Processing Pipeline
radlab-data ships with a lightweight pipeline framework:
from radlab_data.preprocessing.pipeline import Pipeline
from radlab_data.preprocessing.pipeline_modules import SplitLabels, RemoveDuplicates
pipeline = Pipeline(modules=[
SplitLabels(),
RemoveDuplicates(),
# add your custom PipelineElementBase subclasses here
])
processed_dataset = pipeline(original_dataset)
Each pipeline element inherits from PipelineElementBase and implements a _process_data method that receives a
dataset instance and returns the transformed instance. The framework automatically validates that the element supports
the dataset type.
Command‑Line Utilities
The package currently does not ship command‑line entry points; dataset
conversion and preprocessing are driven from Python through the classes
described above.
Configuration
All default settings live under radlab_data/text/loaders/config.py:
TOKENIZER_PATH = "radlab/polish-fast-tokenizer"
AVAILABLE_FILE_EXTENSIONS = ["txt", "md", "pdf", "docx", "html", "input_text"]
You can override these values at runtime by passing the appropriate arguments to Document or the loader classes.
License
radlab-data is released under the Apache 2.0 License. See the LICENSE file for full details.