Skip to main content

banglatok

Fast, lossless subword tokenizers for Bangla, English and Banglish (romanized Bengali), including code-mixed text.

PyPI CI Python 3.10–3.15 Runtime dependencies: none Hugging Face License: MIT (code), CC BY-SA 4.0 (models)

from banglatok import Tokenizer

tok = Tokenizer.from_pretrained()
tok.tokenize("meetingটা ১০টায়, don't be late 🙂")
# ['meeting', 'টা', ' ১০', 'টায়', ',', " don't", ' be', ' late', ' 🙂']
  • Short sequences. On unseen product reviews banglatok's default model, bn-bpe-58k, produced 22% fewer tokens than BanglaBERT and GPT-4o's o200k_base, 21% fewer than XLM-R/mE5/BGE-M3, 8% fewer than MuRIL, and 62% fewer than Qwen3. Most of that comes from training on Bangla and Banglish text at this vocabulary size: SentencePiece trained on the same data with the same vocabulary size comes within about 2% (see Benchmarks).
  • Lossless. decode(encode(text)) gives back the normalized text exactly, for every input. There is no [UNK] and no lowercasing, and Bengali marks, ZWJ/ZWNJ, emoji and mixed scripts are kept.
  • Fast. Single-threaded, on unseen text, it was 1.1–1.7× faster than SentencePiece Unigram and 2.6–4.4× faster than SentencePiece BPE at the same vocabulary size, 1.3–5.9× faster than tiktoken, and 5–12× faster than Hugging Face tokenizers, on both an Apple M5 and a Linux x86_64 server CPU.
  • Easy to adopt. The engine is native Go behind ctypes, with zero Python runtime dependencies and typed APIs. It is thread-safe, two pretrained models ship inside the wheel, and they are on the Hugging Face Hub with identical IDs.

The vocabularies are meant for training or adapting encoders and LLMs on Bengali text, and for counting or segmenting such text.

Install

We recommend uv:

uv add banglatok                     # add to a uv project
uv pip install banglatok             # or install into the active virtual environment
uvx banglatok "আমি বাংলা বলি"          # or run the command once without installing

pip works too: pip install banglatok.

Prebuilt wheels cover CPython 3.10–3.15 on Linux (x86_64, aarch64), macOS (x86_64, arm64) and Windows (x86_64), and need no compiler. Other platforms build from source, which needs Go 1.24.9+ and a C compiler. See the install guide for details.

Usage

from banglatok import Tokenizer

tok = Tokenizer.from_pretrained()                    # "bn-bpe-58k"; or from_pretrained("bn-bpe-28k")
ids = tok.encode("আমি বাংলায় গান গাই। ami banglay gaan gai")
tok.decode(ids)                                      # 'আমি বাংলায় গান গাই। ami banglay gaan gai'
tok.encode_batch(["আমি ভালো আছি", "kemon acho?"])     # many texts, one native call
tok.count_tokens("অনেক লম্বা লেখা…")                  # length without building the list

# Inputs for a model: <s> … </s>, truncation, padding with <pad>.
batch = tok(["আমি ভালো আছি", "hi"], max_length=128, padding=True)
batch["input_ids"], batch["attention_mask"]
$ banglatok "আমি বাংলা বলি"
আমি |  বাংলা |  বলি
$ echo "ami bangla boli" | banglatok --ids
4405 33616 34749
$ banglatok --count --model bn-bpe-28k < lines.txt    # one count per line

The user guide covers every method, normalization, token strings, the command line, threads and processes, troubleshooting, and the full API reference.

Guides

Short recipes; each links to a complete, tested version in the user guide.

Use it from 🤗 Transformers. Both models are on the Hub and need only transformers:

from transformers import AutoTokenizer

hf = AutoTokenizer.from_pretrained("iamaber/banglatok-bn-bpe-58k")   # same IDs as banglatok

tok.save_huggingface(directory) exports any banglatok BPE model the same way. More

Tokenize a dataset. The tokenizer returns plain lists, which datasets accepts directly:

tokenized = dataset.map(lambda batch: tok(batch["text"], max_length=512), batched=True)

More

Train a model from scratch. A model needs only the vocabulary size and the control-token IDs:

from transformers import GPT2Config, GPT2LMHeadModel
from banglatok import BOS_ID, EOS_ID, PAD_ID

model = GPT2LMHeadModel(GPT2Config(vocab_size=tok.vocab_size, n_embd=256, n_layer=4,
                                   n_head=4, bos_token_id=BOS_ID, eos_token_id=EOS_ID,
                                   pad_token_id=PAD_ID))

More

Count tokens against a budget: guide. Feed a PyTorch DataLoader: guide.

Use several processes. Threads can share one tokenizer. Processes must use the spawn or forkserver start method, because the Go runtime does not survive fork(). More

Benchmarks

Here are 10,000 naturally written e-commerce reviews from BanglishRev, in Bengali, English, Banglish and mixed script. This source was never seen in training. Every tokenizer runs single-threaded (RAYON_NUM_THREADS=1, num_threads=1), on an Apple M5 (macOS) and on an AMD EPYC 9V74 GitHub-hosted Linux x86_64 VM, Python 3.12. Speed is MB/s for single calls / batches of 64. The banglatok rows were measured with 0.2.0; the table predates the 0.2.3 encoder changes and has not been rerun.

Tokenizer Vocab Tokens / word ↓ Lossless round trip ↑ Texts with UNK ↓ MB/s, Apple M5 ↑ MB/s, Linux x86_64 ↑
banglatok bn-bpe-58k 57,925 1.379 100% 0 58.5 / 71.6 24.4 / 29.7
banglatok bn-bpe-28k 27,760 1.587 100% 0 61.6 / 72.9 26.1 / 33.6
SentencePiece Unigram, same data and size as bn-bpe-58k 57,925 1.410 100% 0 42.4 / 41.7 20.5 / 20.9
SentencePiece BPE, same data and size as bn-bpe-58k 57,925 1.407 100% 0 17.7 / 17.4 9.5 / 9.8
SentencePiece Unigram, same data and size as bn-bpe-28k 27,760 1.572 100% 0 46.4 / 43.2 22.8 / 23.4
SentencePiece BPE, same data and size as bn-bpe-28k 27,760 1.578 100% 0 16.6 / 16.4 9.8 / 9.8
BNLP bn_spm (SentencePiece) 50,000 1.825 80.1% 1,932 40.1 / 39.2 19.3 / 19.8
tiktoken o200k_base (GPT-4o) 200,019 1.780 100% 0 37.3 / 12.1 19.4 / 5.4
BanglaBERT (csebuetnlp) 32,000 1.771 51.1% 1,110 9.1 / 9.2 4.8 / 4.6
MuRIL (Google) 197,258 1.498 47.9% 1,617 8.8 / 8.9 4.6 / 4.4
XLM-R / multilingual-E5 / BGE-M3 250,002 1.740 98.2% 109 6.5 / 6.4 3.7 / 3.5
mBERT cased 119,547 2.122 48.0% 1,706 8.1 / 8.1 4.2 / 4.1
Qwen3 151,669 3.588 100% 0 6.0 / 6.0 3.3 / 3.1

What this shows:

  • Against SentencePiece with the same data and vocabulary size, compression is a tie. bn-bpe-58k is about 2% shorter than both SentencePiece models; bn-bpe-28k is about 1% longer. On the in-domain validation split, SentencePiece at the bn-bpe-28k size is 3–4.5% shorter. The gap to BanglaBERT, XLM-R and tiktoken comes from the training data and the vocabulary size, not from a better algorithm.
  • The speed lead is real but depends on the CPU. Against SentencePiece Unigram it is 1.3–1.7× on the M5 and 1.1–1.4× on the Linux VM. This table measures single-threaded calls: Hugging Face tokenizers was limited to one thread, and banglatok batches used one worker. Both can use multiple cores for batches outside this comparison.
  • BanglaBERT is about 3% shorter on Bengali-only text but lossy. Among lossless tokenizers, bn-bpe-58k is the most compact on Bengali, Latin and mixed script.

The full report has the per-script breakdown, the in-domain validation split, more tokenizers, the method, limitations, and the commands to reproduce it. Shorter sequences cut compute and context use, but they do not by themselves make a downstream model more accurate.

Models

Name Vocab Best for
bn-bpe-58k (default) 57,925 Shortest sequences
bn-bpe-28k 27,760 Smaller embedding tables, same speed

Both are BPE vocabularies with space fusion: a space and the token after it can share a single ID. That removes most standalone space tokens without overloading a literal marker such as ▁. They were learned from 563k records of Bengali Wikipedia, Tatoeba and BanglaTLit. On the Hub they are iamaber/banglatok-bn-bpe-58k and iamaber/banglatok-bn-bpe-28k. docs/MODELS.md covers the build commands, checksums, behavior and limitations. Their vocabulary IDs differ from those of pretrained encoders such as BanglaBERT or XLM-R, so pairing one with such an encoder means adapting its embeddings.

Training data

The corpus used for both tokenizer models is available as iamaber/banglatok-training-corpus. It contains 563,476 training records, 14,779 validation records and 16,712 test records from Bengali Wikipedia, Tatoeba and BanglaTLit. Only the training split was used to learn the vocabularies, merges and word caches.

Each record retains its source and attribution. The dataset card explains the source-specific licenses, filtering and privacy limitations; the software's MIT license does not apply to all corpus text.

Documentation

Document Contents
User guide Install, API, command line, Hugging Face, guides, troubleshooting
Models How the bundled models were built, checksums, limitations
Training dataset Downloadable splits, sources, attribution, licenses and data limitations
Training and research Training your own models, corpus format, model contract
Benchmarks Full comparison with popular tokenizers
Changelog Release notes

Train your own

The Go bntok command trains Unigram or BPE models on your JSONL corpus, and Tokenizer(path) loads the result:

go run ./cmd/bntok train -algorithm bpe -vocab 384 \
  -input examples/training-records.jsonl -out models/toy-bpe.json
go run ./cmd/bntok encode -model models/toy-bpe.json -text 'আমি বাংলা বলি। ami bangla boli'
python benchmarks/build_fused_model.py --base models/toy-bpe.json --budget 512 \
  --training examples/training-records.jsonl --output models/toy-fused.json

See docs/TRAINING_AND_RESEARCH.md for the corpus format, the full research pipeline and the model contract.

Research

This repository also holds the research behind the models, under research/. data/CORPUS.md documents the corpus construction, provenance and leakage controls. RESULTS.md has the intrinsic evaluation. The retrieval experiments are in research/experiments/retrieval/, and ROUND2_ORIGINAL_COMPARISON.md has the encoder-adaptation results. In those experiments, swapping a custom vocabulary into multilingual-E5 did not beat the unchanged model. Treat these vocabularies as a strong starting point for training, not as a drop-in accuracy gain for existing encoders.

Contributing

CONTRIBUTING.md explains how to work on the project, SECURITY.md how to report vulnerabilities, and docs/RELEASING.md how releases are made. Platform reports, regression cases and independent benchmark runs are especially welcome.

Citation

If you use banglatok or its models, please cite it with the metadata in CITATION.cff.

License

  • Code, documentation and the demo vocabulary: MIT.
  • Bundled model files (bn-bpe-58k, bn-bpe-28k): CC BY-SA 4.0, because they are learned from Wikipedia and Tatoeba text.
  • Native wheels include BSD-3-Clause Go components (notices).

The SPDX expression is MIT AND BSD-3-Clause AND CC-BY-SA-4.0. Corpus text keeps its source-specific terms and is published separately on Hugging Face, not included in the Python package.

Metadata

Release files for banglatok 0.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for banglatok 0.2.3
File Size Uploaded Provenance Actions
banglatok-0.2.3.tar.gz 3.7 MB With provenance Details for banglatok-0.2.3.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for banglatok 0.2.3
File Actions
banglatok-0.2.3-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for banglatok-0.2.3-py3-none-win_amd64.whl
banglatok-0.2.3-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl Python 3 none Linux glibc 2.28+ ARM64, Linux glibc 2.17+ ARM64 Details for banglatok-0.2.3-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
banglatok-0.2.3-py3-none-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl Python 3 none Linux glibc 2.5+ x86-64, Linux glibc 2.28+ x86-64 Details for banglatok-0.2.3-py3-none-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
banglatok-0.2.3-py3-none-macosx_14_0_x86_64.whl Python 3 none macOS 14.0+ x86-64 Details for banglatok-0.2.3-py3-none-macosx_14_0_x86_64.whl
banglatok-0.2.3-py3-none-macosx_14_0_arm64.whl Python 3 none macOS 14.0+ ARM64 Details for banglatok-0.2.3-py3-none-macosx_14_0_arm64.whl

Total release size: 28.7 MB

Release files / banglatok-0.2.3.tar.gz

Download URL banglatok-0.2.3.tar.gz
Size 3.7 MB
Tags Source
SHA-256 checksum
How to use checksums
b2a2a7e8f7d7c52aabbfda20ba390c197dcd479b3a8a763eca29ca788f692b67
BLAKE2b-256 checksum
How to use checksums
4252d088165a4b87c30c8b1dcd0b2cef595f589776700db87f032dbf6da94b37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 14, 2026.

Transparency log

Release files / banglatok-0.2.3-py3-none-win_amd64.whl

Download URL banglatok-0.2.3-py3-none-win_amd64.whl
Size 5.1 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
1e7a4b162b63fed88a382b7b66824d98a80f3afabec46097f69feb1df0c0b047
BLAKE2b-256 checksum
How to use checksums
1039bfd8ffbd2f1bce2c966a0979df5634e9df09271c7a53160a68f8f40b4c15
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 14, 2026.

Transparency log

Release files / banglatok-0.2.3-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL banglatok-0.2.3-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 4.9 MB
Tags Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64 Python 3
SHA-256 checksum
How to use checksums
7532786ba5608bfbbf6cf6f782d43a0b666d117c8c6b32a55cc18e1efb39d00f
BLAKE2b-256 checksum
How to use checksums
36d935dde55fed464f18ee6eea250c7ee91bf3f2b18a552bdaf63822ee0e54a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 14, 2026.

Transparency log

Release files / banglatok-0.2.3-py3-none-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl

Download URL banglatok-0.2.3-py3-none-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Size 5.1 MB
Tags Linux glibc 2.28+ x86-64 Linux glibc 2.5+ x86-64 Python 3
SHA-256 checksum
How to use checksums
0438582a867fa0c5352e4b794b62914c57ec121f6c73efbc0a094e7dd35ab28e
BLAKE2b-256 checksum
How to use checksums
2877094216fe2076bc8b28c3c6e5963d5a73ecd550b4444a885dec1fece6ae4d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 14, 2026.

Transparency log

Release files / banglatok-0.2.3-py3-none-macosx_14_0_x86_64.whl

Download URL banglatok-0.2.3-py3-none-macosx_14_0_x86_64.whl
Size 5.0 MB
Tags Python 3 macOS 14.0+ x86-64
SHA-256 checksum
How to use checksums
09feaa4518e92da2f00c8566d1df829b6328b64221dfad93f56338c9ab36e299
BLAKE2b-256 checksum
How to use checksums
3db918369ada429164eb55980770122e68e72621ce5d7d316d7057bc68f43b52
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 14, 2026.

Transparency log

Release files / banglatok-0.2.3-py3-none-macosx_14_0_arm64.whl

Download URL banglatok-0.2.3-py3-none-macosx_14_0_arm64.whl
Size 4.9 MB
Tags Python 3 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
65b234a2fa63128da50ffbcbfe9f4127ef4ac033c2563bb902a50f5d657036d9
BLAKE2b-256 checksum
How to use checksums
9c7d28cd9cdce6036f7c45139c05a7fd7da5fab1380e2c64b40605b22f173842
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 14, 2026.

Transparency log

Security for banglatok 0.2.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 6 files in this release have been uploaded with provenance metadata.

Select from 6 files

Attestation for banglatok-0.2.3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 14, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page