Universal tokenizer with modular architecture supporting multiple tokenization strategies
Project description
MyTecZ OmniToken ๐
Universal Tokenizer Framework with Multi-Backend Support
๐ฏ Overview
MyTecZ OmniToken is a production-ready universal tokenizer framework that provides a unified interface for multiple tokenization backends. Designed for modern NLP workflows, it offers seamless switching between BPE, WordPiece, SentencePiece, and experimental Hybrid tokenization strategies.
Perfect for:
- ๐ค Machine Learning practitioners building NLP models
- ๐ฌ Researchers comparing tokenization strategies
- ๐๏ธ Production systems requiring robust text processing
- ๐ Educational projects exploring tokenization algorithms
โจ Key Features
- ๐ง Multi-Backend Support: BPE, WordPiece, SentencePiece, and Hybrid tokenizers
- ๐ฏ Unified API: Single interface across all tokenization methods
- ๐ Unicode Ready: Full support for international text, emojis, and complex scripts
- ๐งช 98 Tests Passing: Comprehensive test suite ensuring reliability
- ๐ Frequency Analysis: Built-in token and character frequency tracking
- โ๏ธ Highly Configurable: Extensive customization options
- โก Production Optimized: Efficient training and inference with robust error handling
๐ ๏ธ Supported Tokenization Methods
| Method | Description | Best For |
|---|---|---|
| BPE | Byte Pair Encoding with merge operations | General-purpose, handling OOV words |
| WordPiece | BERT-style with ## continuation prefixes |
Transformer models, subword tokenization |
| SentencePiece | Language-independent raw character processing | Multilingual applications, robust Unicode |
| Hybrid | Adaptive strategy combining multiple approaches | Mixed content types, experimental research |
๐ Quick Start
Installation
pip install mytecz-omnitoken
Basic Usage
from omnitoken import OmniToken
# Create tokenizer with your preferred method
tokenizer = OmniToken(method="wordpiece", vocab_size=1000)
# Train on text data
training_texts = [
"Hello world! This is sample text.",
"More training data here.",
"Tokenization is important for NLP."
]
tokenizer.fit(training_texts)
# Encode text to token IDs
text = "Hello world! ๐ Unicode works perfectly."
token_ids = tokenizer.encode(text)
print(f"Token IDs: {token_ids}")
# Decode back to text
decoded = tokenizer.decode(token_ids)
print(f"Decoded: {decoded}")
# Get vocabulary and frequencies
vocab = tokenizer.get_vocab()
frequencies = tokenizer.get_token_frequencies()
print(f"Vocabulary size: {len(vocab)}")
Advanced Example
from omnitoken import OmniToken
# Configure tokenizer with custom settings
tokenizer = OmniToken(
method="bpe",
vocab_size=2000,
min_frequency=1, # Include rare tokens
special_tokens=["[CUSTOM]", "[SPECIAL]"]
)
# Train with text data
training_texts = [
"The quick brown fox jumps over the lazy dog.",
"Artificial intelligence and machine learning.",
"Natural language processing with tokenization."
]
tokenizer.fit(training_texts)
# Analyze tokenization results
test_text = "Machine learning tokenization example!"
token_ids = tokenizer.encode(test_text)
decoded = tokenizer.decode(token_ids)
print(f"Original: {test_text}")
print(f"Token IDs: {token_ids}")
print(f"Decoded: {decoded}")
print(f"Vocabulary size: {len(tokenizer.get_vocab())}")
Method Comparison
# Compare different tokenization methods on the same text
text = "Hello world! Tokenization comparison."
methods = ["bpe", "wordpiece", "sentencepiece", "hybrid"]
training_data = ["Sample training text", "More training data", "Hello world"]
for method in methods:
tokenizer = OmniToken(method=method, vocab_size=500, min_frequency=1)
tokenizer.fit(training_data)
token_ids = tokenizer.encode(text)
decoded = tokenizer.decode(text)
vocab_size = len(tokenizer.get_vocab())
print(f"{method}: {len(token_ids)} tokens, vocab={vocab_size}")
๐ API Reference
Tokenization Methods
BPE (Byte Pair Encoding)
tokenizer = OmniToken(method="bpe", vocab_size=10000)
- Learns merge operations for frequent character pairs
- Good for handling out-of-vocabulary words
- Deterministic and efficient
WordPiece
tokenizer = OmniToken(method="wordpiece", vocab_size=10000)
- BERT-style tokenization with ## continuation prefixes
- Greedy longest-match-first algorithm
- Excellent for transformer models
SentencePiece
tokenizer = OmniToken(method="sentencepiece", vocab_size=10000)
- Language-independent tokenization
- Treats text as raw character sequence
- Robust Unicode handling
Hybrid (Experimental)
tokenizer = OmniToken(method="hybrid", vocab_size=10000)
- Combines multiple strategies adaptively
- Analyzes text characteristics to choose optimal approach
- Best for diverse content types
Input Format Support
String Input
tokenizer.fit("Simple string input for training")
File Input
tokenizer.fit("path/to/textfile.txt")
tokenizer.fit(["file1.txt", "file2.txt", "file3.txt"])
JSON Input
data = {
"texts": ["Text 1", "Text 2"],
"metadata": "Additional content"
}
tokenizer.fit(data)
Mixed Input
mixed = [
"Direct string",
"data/file.txt",
{"json": "object"},
["list", "of", "items"]
]
tokenizer.fit(mixed)
๐งช Testing
MyTecZ OmniToken includes a comprehensive test suite with 98 passing tests covering all functionality.
Running Tests
# Install the package
pip install mytecz-omnitoken
# Clone repository for tests (if contributing)
git clone https://github.com/kalyanakkondapalli/mytecz_omnitoken.git
cd mytecz_omnitoken
# Run all tests
python -m pytest tests/ -v
# Run specific test categories
python -m pytest tests/test_basic.py -v # Basic functionality
python -m pytest tests/test_roundtrip.py -v # Encode/decode consistency
python -m pytest tests/test_unicode.py -v # Unicode and multilingual
Test Coverage
Our test suite covers:
- โ Round-trip encode/decode accuracy
- โ Unicode and emoji handling
- โ All input format types
- โ Edge cases and error conditions
- โ Performance benchmarks
- โ Deterministic behavior
- โ Cross-method consistency
โ๏ธ Configuration Options
TokenizerConfig Parameters
from omnitoken import OmniToken
from omnitoken.tokenizer_base import TokenizerConfig
config = TokenizerConfig(
vocab_size=10000, # Maximum vocabulary size
min_frequency=2, # Minimum token frequency
special_tokens=["[MASK]"], # Custom special tokens
unk_token="[UNK]", # Unknown token
pad_token="[PAD]", # Padding token
case_sensitive=True, # Case sensitivity
max_token_length=100 # Maximum token length
)
tokenizer = OmniToken(method="bpe", config=config)
Method-Specific Options
BPE Options
tokenizer = OmniToken(
method="bpe",
vocab_size=10000,
dropout=0.1, # BPE dropout for regularization
end_of_word_suffix="</w>" # End-of-word marker
)
WordPiece Options
tokenizer = OmniToken(
method="wordpiece",
vocab_size=10000,
continuation_prefix="##", # Subword continuation prefix
do_lower_case=True, # Lowercase normalization
max_input_chars_per_word=100 # Max characters per word
)
Hybrid Options
tokenizer = OmniToken(
method="hybrid",
vocab_size=10000,
char_ratio=0.3, # Character vocab ratio
word_ratio=0.4, # Word vocab ratio
subword_ratio=0.3, # Subword vocab ratio
adaptive_mode=True # Enable adaptive strategy selection
)
๐ Visualization and Analysis
Token Visualization
from omnitoken.utils import TokenVisualizer
# Visualize tokenization
text = "Example text with emojis ๐ฏ"
tokens = tokenizer._tokenizer.tokenize(text)
token_ids = tokenizer.encode(text)
viz = TokenVisualizer.visualize_tokens(text, tokens, token_ids)
print(viz)
Method Comparison
# Compare different tokenization methods
tokenizations = {
"BPE": bpe_tokenizer.tokenize(text),
"WordPiece": wp_tokenizer.tokenize(text),
"Hybrid": hybrid_tokenizer.tokenize(text)
}
comparison = TokenVisualizer.compare_tokenizations(text, tokenizations)
print(comparison)
Vocabulary Statistics
vocab = tokenizer._tokenizer.get_vocab()
stats = TokenVisualizer.show_vocabulary_stats(vocab)
print(stats)
๐ฏ Use Cases
Natural Language Processing
- Text preprocessing for transformer models
- Multi-language document processing
- Social media content analysis
Code Analysis
- Programming language tokenization
- Code documentation processing
- Technical text analysis
Content Processing
- Web scraping and text extraction
- Document indexing and search
- Content recommendation systems
Research and Development
- Tokenization algorithm research
- Comparative analysis studies
- Custom tokenization strategies
๐๏ธ Package Structure
mytecz_omnitoken/
โโโ omnitoken/
โ โโโ __init__.py # Main OmniToken interface
โ โโโ tokenizer_base.py # Abstract base class
โ โโโ tokenizer_bpe.py # BPE implementation
โ โโโ tokenizer_wordpiece.py # WordPiece implementation
โ โโโ tokenizer_sentencepiece.py # SentencePiece implementation
โ โโโ tokenizer_hybrid.py # Hybrid tokenizer
โ โโโ trainer.py # Training algorithms
โ โโโ utils.py # Utilities and helpers
โ โโโ data/ # Training corpora and test samples
โโโ tests/ # Comprehensive test suite
๐ค Contributing
We welcome contributions! Here's how you can help:
- ๐ Report Issues: Open an issue for bugs or feature requests
- ๐ Improve Documentation: Help enhance examples and documentation
- ๐งช Add Tests: Contribute test cases for edge cases
- ๐ง Submit Code: Submit pull requests with improvements or fixes
Development Setup
# Clone the repository
git clone https://github.com/kalyanakkondapalli/mytecz_omnitoken.git
cd mytecz_omnitoken
# Install in development mode
pip install -e .
# Run tests
python -m pytest tests/ -v
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Links
- PyPI Package: https://pypi.org/project/mytecz-omnitoken/
- Source Code: https://github.com/kalyanakkondapalli/mytecz_omnitoken
- Issues & Support: https://github.com/kalyanakkondapalli/mytecz_omnitoken/issues
Universal Tokenization Made Simple ๐
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 mytecz_omnitoken-0.1.5.tar.gz.
File metadata
- Download URL: mytecz_omnitoken-0.1.5.tar.gz
- Upload date:
- Size: 48.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
35772e588d39034edb32902064b89a78cc9ca569969408d60eeee58d033d4d00
|
|
| MD5 |
8144989d85381c54ba0261a36a659e04
|
|
| BLAKE2b-256 |
d0698fb78087c71e387ec92bc5717909b3f754031d614d47ef63b3319afa4293
|
File details
Details for the file mytecz_omnitoken-0.1.5-py3-none-any.whl.
File metadata
- Download URL: mytecz_omnitoken-0.1.5-py3-none-any.whl
- Upload date:
- Size: 42.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0748b1aa3814c7d7b9e9317accc4509c572b5510940cddb769c6e82bd913a12b
|
|
| MD5 |
14f16e64039e14d4c1c116eb7d05dfe7
|
|
| BLAKE2b-256 |
9b81d401e3e8a7b1ff9b8223679000033ae906df207c8bd333a63d6aec2fa2b1
|