Afrisyl-tokenizer
A syllable-aware tokenizer for African languages
Afrisyl-tokenizer is built to fix a core problem: BPE and WordPiece tokenizers break African languages. Instead of splitting "ndinoda" into ['n', 'din', 'oda'], we respect the natural CV syllable structure of Bantu languages.
Built as part of MSc research by Nkosilomusa Ncube, Alumni WeThinkCode.
Key Features
- Syllable-based: Uses CV, CVC, V patterns found in Shona, Ndebele, Zulu, Swahili, etc
- Low fertility: 30% fewer tokens than BPE on Shona text
- Fast: Pure Python + regex. No dependencies for inference
- HF Compatible: Drop-in replacement for HuggingFace tokenizers
- Trainable: Train your own tokenizer on any African language corpus
- Small vocab: 8k-16k vocab covers 95%+ of tokens
- Open Source: MIT Licensed for research and social impact
Installation
pip install afrisyl-tokenizer
Quick Usage
1. Load Tokenizer
from afrisyl_tokenizer import AfriSylTokenizer
# Load default shona vocab from package
tok = AfriSylTokenizer(language="shona")
# Or load custom vocab
# tok = AfriSylTokenizer(vocab_path="path/to/ndebele_vocab.json")
# Batch processing
texts = ["ndinoda rubatsiro", "mhoroi shamwari"]
batch = tok.batch_encode(
texts,
add_bos=True,
add_eos=True,
max_len=32,
padding=True
)
# batch['input_ids'] -> [[1, 12, 45, ...], [1, 8, 22, ...]]
# batch['attention_mask'] -> [[1, 1, 1, ...], [1, 1, 1, ...]]
decoded_batch = tok.batch_decode(batch["input_ids"])
print(decoded_batch)
# ['ndinoda rubatsiro', 'mhoroi shamwari']
Whitespace handling
Spaces are tokenized the same way o200k (GPT-4o's tokenizer) handles them:
a space is fused onto the front of the token that follows it (e.g. " ya")
instead of being dropped or turned into its own token between every word.
decode(encode(text)) is fully reversible, spacing included:
tok = AfriSylTokenizer(language="shona")
ids = tok.encode("nyika yakatanga")
tok.decode(ids) == "nyika yakatanga" # True
Every syllable, fallback letter, digit and punctuation mark also has a
space-prefixed sibling in the vocab ("ba" and " ba" are both real
tokens), and multi-space/newline runs get their own tokens too -- the same
trick o200k uses so whitespace doesn't need special-casing at decode time.
Emoji & other scripts
Anything outside the vocab -- emoji, accented letters, other scripts --
falls back to its raw UTF-8 bytes (256 <0xXX> byte tokens) instead of a
lossy <unk>, the same way byte-level BPE tokenizers like o200k always have
full coverage. This also correctly round-trips multi-codepoint emoji
sequences (ZWJ family emoji, flags, skin-tone modifiers):
tok = AfriSylTokenizer(language="shona")
ids = tok.encode("mhoroi shamwari 😀🇿🇼")
tok.decode(ids) == "mhoroi shamwari 😀🇿🇼" # True
Metadata
Release files for afrisyl-tokenizer 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| afrisyl_tokenizer-0.1.2.tar.gz | 49.0 kB | Without provenance | Details for afrisyl_tokenizer-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| afrisyl_tokenizer-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for afrisyl_tokenizer-0.1.2-py3-none-any.whl |
Total release size: 95.8 kB
Release files / afrisyl_tokenizer-0.1.2.tar.gz
| Download URL | afrisyl_tokenizer-0.1.2.tar.gz |
|---|---|
| Size | 49.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4e86787b52e624084eddf3077c4ea37207c5cf38eb7a374050d9b6f5bafc950a
|
|
BLAKE2b-256 checksum How to use checksums |
2d6d263c249db3195472701e96e1350832c5c792a1d97176cb63f2eeb10b5357
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.0
|
Release files / afrisyl_tokenizer-0.1.2-py3-none-any.whl
| Download URL | afrisyl_tokenizer-0.1.2-py3-none-any.whl |
|---|---|
| Size | 46.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
fb57994d7e9c7fd76c5d6c2fef4a97bd8c874fdae18bf790dd51e7aefde472c2
|
|
BLAKE2b-256 checksum How to use checksums |
6c5d57b518f26c731ff4b119278e1811d9233b2fb51760d9cd166bf20968cc78
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.0
|
Security for afrisyl-tokenizer 0.1.2
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.