urkish-optimized BPE tokenizer with GPT-4 style pre-tokenization. Train, encode, decode with custom special tokens.
Project description
TurkToken
A Turkish-optimized Byte Pair Encoding (BPE) tokenizer with GPT-4 style pre-tokenization patterns.
Installation
pip install turktoken
Quick Start
from turktoken import TurkishBPETokenizer
tokenizer = TurkishBPETokenizer()
Training
Train a tokenizer on your own corpus:
from turktoken import TurkishBPETokenizer
tokenizer = TurkishBPETokenizer()
# Load your training data
with open("corpus.txt", "r", encoding="utf-8") as f:
text = f.read()
# Train with desired vocabulary size
tokenizer.train(text, vocab_size=8000)
# Save the trained tokenizer
tokenizer.save("./my_tokenizer")
Encoding and Decoding
from turktoken import TurkishBPETokenizer
# Load a trained tokenizer
tokenizer = TurkishBPETokenizer()
tokenizer.load("./my_tokenizer")
# Encode text to token IDs
text = "Merhaba dünya! Bu bir test cümlesidir."
ids = tokenizer.encode(text)
print(ids) # [256, 312, 445, 78, ...]
# Decode token IDs back to text
decoded_text = tokenizer.decode(ids)
print(decoded_text) # "Merhaba dünya! Bu bir test cümlesidir."
Special Tokens
Add custom special tokens for your use case:
from turktoken import TurkishBPETokenizer
tokenizer = TurkishBPETokenizer()
tokenizer.train(training_text, vocab_size=4096)
# Add special tokens
tokenizer.add_special_tokens([
"<|bos|>", # beginning of sequence
"<|eos|>", # end of sequence
"<|pad|>", # padding
"<|unk|>", # unknown token
"<|sep|>" # separator
])
tokenizer.save("./my_tokenizer")
Using special tokens during encoding:
tokenizer = TurkishBPETokenizer()
tokenizer.load("./my_tokenizer")
# Special tokens are recognized during encoding
ids = tokenizer.encode("<|bos|>Merhaba dünya!<|eos|>")
text = tokenizer.decode(ids)
API Reference
TurkishBPETokenizer
| Method | Description |
|---|---|
train(text, vocab_size=512) |
Train the tokenizer on the given text corpus |
encode(text) |
Convert text to a list of token IDs |
decode(ids) |
Convert a list of token IDs back to text |
add_special_tokens(tokens) |
Add a list of special tokens to the vocabulary |
save(directory) |
Save the tokenizer to a directory |
load(directory) |
Load a tokenizer from a directory |
Features
- Unicode-aware pre-tokenization optimized for Turkish text
- Byte Pair Encoding for efficient subword tokenization
- Support for custom special tokens
- Save and load trained tokenizers
- Minimal dependencies (only
regex)
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file turktoken-0.1.0.tar.gz.
File metadata
- Download URL: turktoken-0.1.0.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc2b2daf43394b22f66cc567cf1a21ef2040e388eb4e9cdaba28f2f040a2e6c3
|
|
| MD5 |
0f7ad2924051b1ca4cff24ca13e9331b
|
|
| BLAKE2b-256 |
f41cf552ef0e139907138822ad2a2dc9cda3cc395035a616d0fd824174d6d1bc
|
File details
Details for the file turktoken-0.1.0-py3-none-any.whl.
File metadata
- Download URL: turktoken-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f764e37a6dd661817a3e5a755ea0a257eec5e6ffba7c0dd11330bc15a42e29b2
|
|
| MD5 |
ef3d564fb1757d67f2eeace36b3fc4df
|
|
| BLAKE2b-256 |
ca3004693a295a6e1df81afebff81709ac292ce521ef1f2901998d0d01e09bfc
|