Skip to main content

Custom Byte-Level BPE Tokenizer built from scratch in Python

Project description

Sujit-Tokenizer

A custom Byte-Level BPE (Byte Pair Encoding) Tokenizer implemented from scratch in Python.

Features

  • Byte-level tokenization
  • Custom BPE training
  • Text encoding and decoding
  • Save and load tokenizer models
  • UTF-8 support
  • Lightweight and dependency-free

Installation

Clone Repository

git clone https://github.com/iamsspm07/Sujit-Tokenizer.git
cd Sujit-Tokenizer

Install Package

pip install -e .

Project Structure

Sujit-Tokenizer/
│
├── Sujit_Tokenizer/
│   ├── __init__.py
│   └── tokenizer.py
│
├── corpus.txt
├── train_and_test.py
│
├── README.md
├── setup.py
├── pyproject.toml
└── LICENSE

Quick Start

Import Tokenizer

from Sujit_Tokenizer import CustomByteLevelBPETokenizer

Train Tokenizer

corpus = [
    "Transformers are amazing.",
    "Machine Learning is powerful.",
    "Python is widely used in AI."
]

tokenizer = CustomByteLevelBPETokenizer(
    vocab_size=1000
)

tokenizer.train(corpus)

Save Model

tokenizer.save_model(
    "tokenizer.model"
)

Load Model

tokenizer = CustomByteLevelBPETokenizer()

tokenizer.load_model(
    "tokenizer.model"
)

Encode Text

encoded = tokenizer.encode(
    "Transformers use attention."
)

print(encoded)

Example Output:

[2, 451, 723, 812, 3]

Decode Text

decoded = tokenizer.decode(
    encoded
)

print(decoded)

Output:

Transformers use attention.

Training Workflow

Corpus
   ↓
Byte Conversion
   ↓
Pair Frequency Counting
   ↓
BPE Merging
   ↓
Vocabulary Construction
   ↓
Tokenizer Model

Special Tokens

Token ID
0
1
2
3

Example

from Sujit_Tokenizer import CustomByteLevelBPETokenizer

tokenizer = CustomByteLevelBPETokenizer(
    vocab_size=1000
)

corpus = [
    "Artificial Intelligence",
    "Machine Learning",
    "Deep Learning"
]

tokenizer.train(corpus)

tokenizer.save_model(
    "tokenizer.model"
)

tokenizer.load_model(
    "tokenizer.model"
)

text = "Machine Learning"

encoded = tokenizer.encode(text)
print(encoded)

decoded = tokenizer.decode(encoded)
print(decoded)

Use Cases

  • NLP experiments
  • Tokenization research
  • Educational projects
  • Understanding BPE internals
  • Building custom language models
  • Learning tokenizer architecture

Future Enhancements

  • Faster BPE training
  • WordPiece tokenizer
  • SentencePiece tokenizer
  • Parallel processing
  • Vocabulary statistics
  • Token frequency analysis
  • Hugging Face compatibility

Author

Sujit Maity, Praveen Kumar Kannan

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sujit_tokenizer-1.0.1.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sujit_tokenizer-1.0.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file sujit_tokenizer-1.0.1.tar.gz.

File metadata

  • Download URL: sujit_tokenizer-1.0.1.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for sujit_tokenizer-1.0.1.tar.gz
Algorithm Hash digest
SHA256 b9ca342f3eae744181585ab6a7073115d49f6abed8edbde9ddf101872bb482b5
MD5 b663bdcd9998dccb5de2384474eccc67
BLAKE2b-256 f9630f699c789b036ffbd88769ae39eb8eaa9ed97873579019422792afb0b012

See more details on using hashes here.

File details

Details for the file sujit_tokenizer-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for sujit_tokenizer-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f1a952f62001046ca7627aa6b8f5191a2ffe1dbe5e0392ad0dd49c0e4d7b815e
MD5 bf20c358c4ff6b3676c7579997f1d132
BLAKE2b-256 d669aa468f3aaa496f619cbae7fe3d3706cc00dcfaac3e4b2d3baedfa0c45dab

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page