Skip to main content

Universal tokenizer with modular architecture supporting multiple tokenization strategies

Project description

MyTecZ OmniToken ๐Ÿš€

Universal Tokenizer Framework with Multi-Backend Support

Python Version PyPI Downloads License Tests

๐ŸŽฏ 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


Universal Tokenization Made Simple ๐Ÿš€

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

mytecz_omnitoken-0.1.5.tar.gz (48.8 kB view details)

Uploaded Source

Built Distribution

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

mytecz_omnitoken-0.1.5-py3-none-any.whl (42.0 kB view details)

Uploaded Python 3

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

Hashes for mytecz_omnitoken-0.1.5.tar.gz
Algorithm Hash digest
SHA256 35772e588d39034edb32902064b89a78cc9ca569969408d60eeee58d033d4d00
MD5 8144989d85381c54ba0261a36a659e04
BLAKE2b-256 d0698fb78087c71e387ec92bc5717909b3f754031d614d47ef63b3319afa4293

See more details on using hashes here.

File details

Details for the file mytecz_omnitoken-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for mytecz_omnitoken-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 0748b1aa3814c7d7b9e9317accc4509c572b5510940cddb769c6e82bd913a12b
MD5 14f16e64039e14d4c1c116eb7d05dfe7
BLAKE2b-256 9b81d401e3e8a7b1ff9b8223679000033ae906df207c8bd333a63d6aec2fa2b1

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