Observed since 20 Oct, 19:33 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
A domain-specific Byte Pair Encoding tokenizer for medical and general NLP corpora
A from-scratch implementation of a scalable domain-specific Byte Pair Encoding (BPE) tokenizer for medical and general NLP corpora.
This project implements a complete byte-level BPE tokenizer pipeline without relying on external tokenizer frameworks. It includes scalable corpus collection, corpus cleaning, weighted BPE training, merge-rule optimization, serialization, evaluation tooling, benchmarking against GPT-2/tiktoken, automated testing, and PyPI-ready packaging.
The tokenizer was designed for large-scale medical-domain language modeling experiments and was later integrated into a custom GPT-style language model training pipeline.
This repository focuses on building and evaluating a domain-specific BPE tokenizer from scratch.
Core goals:
Domain-Specific-BPE-Tokenizer/
├── domain_specific_bpe_tokenizer/
│ ├── __init__.py
│ ├── bpe_tokenizer.py
│ ├── trainer.py
│ ├── encoder.py
│ ├── decoder.py
│ ├── serialization.py
│ └── vocab.py
│
├── tests/
│ ├── test_bpe_tokenizer.py
│ ├── test_vocab.py
│ ├── test_trainer.py
│ ├── test_encoder.py
│ ├── test_decoder.py
│ ├── test_serialization.py
│ ├── test_save_load.py
│ ├── test_encode_decode.py
│ ├── test_special_tokens.py
│ └── test_invalid_inputs.py
│
├── examples/
│ ├── basic_usage.py
│ ├── load_pretrained_tokenizer.py
│ └── medical_tokenization_demo.py
│
├── scripts/
│ ├── prepare_medical_corpus.py
│ ├── collect_general_corpus.py
│ ├── collect_pubmed_corpus.py
│ ├── collect_pmc_open_corpus.py
│ ├── clean_corpus.py
│ ├── build_word_frequencies.py
│ ├── train_tokenizer.py
│ ├── tokenize_corpus.py
│ ├── evaluate_tokenizer.py
│ └── benchmark_against_tiktoken.py
│
├── resources/
│ ├── raw_corpus/
│ ├── cleaned_corpus/
│ ├── tokenized_corpus/
│ ├── trained_tokenizer/
│ └── evaluation/
│
├── docs/
│ ├── architecture.md
│ ├── evaluation.md
│ └── benchmarks.md
│
├── .github/
│ └── workflows/
│ └── tests.yml
│
├── pyproject.toml
├── requirements.txt
├── .gitignore
└── README.md
git clone https://github.com/adithya-prabhu-22/Domain-Specific-BPE-Tokenizer.git
cd Domain-Specific-BPE-Tokenizer
pip install -e .
pip install tiktoken
from domain_specific_bpe_tokenizer import BPETokenizer
from domain_specific_bpe_tokenizer import BPETokenizer
tokenizer = BPETokenizer(
vocab_size=5000,
min_frequency=2,
)
text = "medical corpus text goes here"
tokenizer.train(text)
encoded = tokenizer.encode(
"myocardial infarction"
)
print(encoded)
decoded = tokenizer.decode(encoded)
print(decoded)
tokenizer.save(
"resources/trained_tokenizer/bpe_tokenizer.json"
)
loaded_tokenizer = BPETokenizer.load(
"resources/trained_tokenizer/bpe_tokenizer.json"
)
The tokenizer supports the following built-in special tokens:
| Token | Purpose |
|---|---|
[UNK] | Unknown token |
[PAD] | Padding token |
[BOS] | Beginning of sequence |
[EOS] | End of sequence |
The tokenizer training pipeline supports scalable corpus preprocessing.
Pipeline stages:
The project includes scripts for collecting:
Example:
python -m scripts.collect_pubmed_corpus
The tokenizer supports weighted word-frequency BPE training for scalable preprocessing.
Features:
Example:
python -m scripts.train_tokenizer
The repository includes tokenizer evaluation tooling.
Evaluation metrics include:
Run evaluation:
python -m scripts.evaluate_tokenizer
The repository includes benchmarking against GPT-2/tiktoken.
Comparison metrics:
Run benchmark:
python -m scripts.benchmark_against_tiktoken
| Medical Term | Custom BPE Tokens | GPT-2 Tokens |
|---|---|---|
| electrocardiogram | 1 | 5 |
| pneumothorax | 1 | 5 |
| myocardial infarction | 3 | 6 |
| hepatocellular carcinoma | 3 | 7 |
The custom medical-domain tokenizer significantly reduces fragmentation for biomedical terminology compared with GPT-2/tiktoken.
The tokenizer training pipeline evolved across multiple scalability stages:
| Stage | Vocabulary Size | Corpus Scale |
|---|---|---|
| Initial Prototype | 500 | Small sample corpus |
| Intermediate Training | 24K | Multi-million token corpus |
| Final Biomedical Tokenizer | 52K | ~240M-token medical corpus |
The tokenizer includes several performance optimizations:
These optimizations significantly improved scalability during large-corpus tokenizer training.
The repository includes a comprehensive automated test suite.
Current coverage includes:
Run tests:
pytest
26 tests passed
The repository is structured as a reusable Python package using pyproject.toml.
Editable installation:
pip install -e .
The public API is exposed through:
from domain_specific_bpe_tokenizer import BPETokenizer
Additional documentation is available in:
docs/
Including:
This tokenizer was later integrated into a custom GPT-style medical language model training pipeline.
The tokenizer was used for:
Future improvements include:
This project demonstrates a complete from-scratch implementation of a scalable domain-specific BPE tokenizer.
The repository evolved from a simple educational tokenizer into a scalable biomedical NLP preprocessing pipeline featuring weighted BPE training, optimized merge learning, tokenizer evaluation, benchmarking against GPT-2/tiktoken, automated testing, and PyPI-ready packaging.
The strongest results appeared in medical-domain tokenization, where the tokenizer significantly reduced fragmentation for biomedical terminology compared with GPT-2/tiktoken.
Overall, the project demonstrates the importance of tokenizer design, merge-rule optimization, scalable preprocessing, and domain-specific vocabulary construction for modern NLP and LLM systems.
Download counts and popularity will appear here as the collector receives them.
| Version | Published | Direct dependencies | Open version |
|---|---|---|---|
| 0.1.0 | 30 May 2026 | 0 |
This describes our collected data; it is not a security assessment.
Observed since 20 Oct, 19:33 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
package:bf0c3501924cc13487f5433dc6bfc15eaa952187eec1dafa69e22e71e7e88e85