Skip to main content

banglatok

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

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

from banglatok import Tokenizer

tok = Tokenizer.from_pretrained()          # bundled model; no download, no network
tok.tokenize("meetingটা ১০টায়, don't be late 🙂")
# ['meeting', 'টা', ' ১০', 'টায়', ',', " don't", ' be', ' late', ' 🙂']
  • Short sequences. On unseen product reviews 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.
  • Tiny footprint. 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 export to Hugging Face tokenizers/transformers with identical IDs.

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

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.

Tokenizer Vocab Tokens / word ↓ Lossless round trip ↑ Texts with UNK ↓ MB/s, Apple M5 ↑ MB/s, Linux x86_64 ↑
bn-bpe-58k (this package) 57,925 1.379 100% 0 58.5 / 71.6 24.4 / 29.7
bn-bpe-28k (this package) 27,760 1.587 100% 0 61.6 / 72.9 26.1 / 33.6
SentencePiece Unigram, same data and size as 58k 57,925 1.410 100% 0 42.4 / 41.7 20.5 / 20.9
SentencePiece BPE, same data and size as 58k 57,925 1.407 100% 0 17.7 / 17.4 9.5 / 9.8
SentencePiece Unigram, same data and size as 28k 27,760 1.572 100% 0 46.4 / 43.2 22.8 / 23.4
SentencePiece BPE, same data and size as 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 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. Hugging Face tokenizers was limited to one thread like everything else; its batch API can use all cores, which this table does not measure.
  • 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.

Full report: per-script breakdown, the in-domain validation split, more tokenizers, 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.

Install

pip install banglatok

Prebuilt wheels target Linux (x86_64, aarch64), macOS (x86_64, arm64) and Windows (x86_64) for CPython 3.10–3.15, and need no compiler. On other platforms pip builds from source, which needs Go 1.24+ and a C compiler:

pip install git+https://github.com/iamaber/banglatok

Usage

from banglatok import Tokenizer

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

    # Encoder-ready inputs: <s> … </s>, truncation and padding with <pad>.
    batch = tok(["আমি ভালো আছি", "hi"], max_length=128, padding=True)
    batch["input_ids"], batch["attention_mask"]

Load a tokenizer once and reuse it. Calls from many threads at once are safe, because the native call releases the GIL. In worker processes, create a fresh tokenizer inside each worker.

Hugging Face

tok.save_huggingface("bn-bpe-58k-hf")      # tokenizer.json + tokenizer_config.json

from transformers import AutoTokenizer
hf = AutoTokenizer.from_pretrained("bn-bpe-58k-hf")   # same IDs as tok.encode / tok(...)

The exported models are also on the Hub as iamaber/banglatok-bn-bpe-58k and iamaber/banglatok-bn-bpe-28k. Only BPE models can be exported. With the tokenizers library alone, set encode_special_tokens = True so that literal <s> in text stays text; transformers does this from the saved configuration.

Command line

$ banglatok "আমি বাংলা বলি"
আমি |  বাংলা |  বলি
$ echo "ami bangla boli" | banglatok --ids
4405 33616 34749
$ banglatok --json "ami valo"
{"ids": [4405, 32047], "tokens": ["ami", " valo"]}
$ banglatok --count --model bn-bpe-28k < lines.txt    # one count per line

API

Member Description
Tokenizer.from_pretrained(name="bn-bpe-58k") Load a bundled model, offline
Tokenizer(path) Load your own JSON model, e.g. one trained with bntok train
encode(text) / encode_batch(texts) Token IDs, without control tokens
decode(ids, skip_special_tokens=False) / decode_batch(batch) Normalized text
tokenize(text) Token strings: fused IDs start with " ", fallback bytes look like "<0xF0>"
count_tokens(text) len(encode(text)), without building the list
save_huggingface(directory) Write a Hugging Face tokenizer.json with identical IDs (BPE models)
tok(texts, add_special_tokens=True, max_length=None, padding=False) {"input_ids", "attention_mask"}
id_to_token, token_to_id, get_vocab(), vocab_size, algorithm Vocabulary access
normalize(text) The shared normalization: NFC plus whitespace collapse and trim
available_models(), PAD_ID=0, UNK_ID=1, BOS_ID=2, EOS_ID=3 Constants

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. docs/MODELS.md covers the build commands, checksums, behavior and limitations. They are not SentencePiece or Hugging Face vocabularies, so pairing one with a pretrained encoder means adapting that encoder's embeddings.

Train your own

The Go CLI 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/my-bpe.json --budget 64000 \
  --output models/my-fused.json        # optional: add space fusion

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. research/reports/RESULTS.md has the intrinsic evaluation. The retrieval experiments are in research/experiments/retrieval/, and research/reports/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 this library 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. Research corpora keep their own terms and are not distributed.

Metadata

Release files for banglatok 0.2.0

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.0
File Size Uploaded Provenance Actions
banglatok-0.2.0.tar.gz 1.4 MB With provenance Details for banglatok-0.2.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for banglatok 0.2.0
File Actions
banglatok-0.2.0-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for banglatok-0.2.0-py3-none-win_amd64.whl
banglatok-0.2.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl Python 3 none Linux glibc 2.17+ ARM64, Linux glibc 2.28+ ARM64 Details for banglatok-0.2.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
banglatok-0.2.0-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.0-py3-none-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
banglatok-0.2.0-py3-none-macosx_14_0_x86_64.whl Python 3 none macOS 14.0+ x86-64 Details for banglatok-0.2.0-py3-none-macosx_14_0_x86_64.whl
banglatok-0.2.0-py3-none-macosx_14_0_arm64.whl Python 3 none macOS 14.0+ ARM64 Details for banglatok-0.2.0-py3-none-macosx_14_0_arm64.whl

Total release size: 14.4 MB

Release files / banglatok-0.2.0.tar.gz

Download URL banglatok-0.2.0.tar.gz
Size 1.4 MB
Tags Source
SHA-256 checksum
How to use checksums
e24aa2a51fbdb905c7927f05fe9a3662737275fcfc67d029666b8ae79952be4f
BLAKE2b-256 checksum
How to use checksums
de59b4222c6c77373da5013a279aab7e19ad09f8e275110d17d0d9ee7f530c3d
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 13, 2026.

Transparency log

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

Download URL banglatok-0.2.0-py3-none-win_amd64.whl
Size 2.7 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
b8f8495a21d267ef2f9df7a3de6fd070b71de867cf8513d849c933be336c7609
BLAKE2b-256 checksum
How to use checksums
ddff0ca510e5b7c5d9fbfa02e3bb13111565946a92808bc0df676cb79c8dda39
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 13, 2026.

Transparency log

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

Download URL banglatok-0.2.0-py3-none-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 2.5 MB
Tags Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64 Python 3
SHA-256 checksum
How to use checksums
72eb2044ae17db9a4b4c273a512d11bd4cb622c032c19b096daec814eaac2d43
BLAKE2b-256 checksum
How to use checksums
0ff002d51e1013a6e036102c9a6ea1d2ec86d5d807e33ef32beb9c7667f48009
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 13, 2026.

Transparency log

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

Download URL banglatok-0.2.0-py3-none-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
Size 2.7 MB
Tags Linux glibc 2.28+ x86-64 Linux glibc 2.5+ x86-64 Python 3
SHA-256 checksum
How to use checksums
8ddc76fcc07cb29585665c5261a3b56e4c1282ed6029bfbd9cf5377d1005c2f6
BLAKE2b-256 checksum
How to use checksums
8c12b58babe5ef02bbbd94da7a1fa390a0d5b0b9e13e3b822b12e3ebf8a7c4bf
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 13, 2026.

Transparency log

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

Download URL banglatok-0.2.0-py3-none-macosx_14_0_x86_64.whl
Size 2.6 MB
Tags Python 3 macOS 14.0+ x86-64
SHA-256 checksum
How to use checksums
f818162eb3609b1688459992d920a3ac5f3cbf426e4c42f87a8f66bd3991f1f7
BLAKE2b-256 checksum
How to use checksums
76675137544459fc77c4aa706595690dcaf476744d6351282e39815103faf437
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 13, 2026.

Transparency log

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

Download URL banglatok-0.2.0-py3-none-macosx_14_0_arm64.whl
Size 2.5 MB
Tags Python 3 macOS 14.0+ ARM64
SHA-256 checksum
How to use checksums
48706c28eba200bdbdcb1146ed061c14552353b07ce0bbc93996071c67387e8b
BLAKE2b-256 checksum
How to use checksums
69c349bc88e467efed3c95155c211fa782b2bc14263f5dc5cfa57f363cec785d
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 13, 2026.

Transparency log

Security for banglatok 0.2.0

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.0.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 13, 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