Skip to main content

Advanced tokenizer with support for BPE, WordPiece, and Unigram algorithms

Project description

Ultra-Tokenizer

PyPI version Python Version License: Apache 2.0 Documentation Status

Features

  • Multiple Tokenization Algorithms: Supports BPE, WordPiece, and Unigram algorithms
  • High Performance: Optimized for speed and memory efficiency
  • Easy Integration: Simple API for training and using tokenizers
  • Production Ready: Battle-tested with comprehensive test coverage
  • Type Hints: Full Python type support for better development experience

Installation

Install the latest stable version from PyPI:

pip install ultra-tokenizer

Quick Start

Basic Usage

from ultra_tokenizer import Tokenizer

# Initialize tokenizer with default settings
tokenizer = Tokenizer()

# Tokenize text
text = "Hello, world! This is Ultra-Tokenizer in action."
tokens = tokenizer.tokenize(text)
print(tokens)
# Output: ['Hello', ',', 'world', '!', 'This', 'is', 'Ultra', '-', 'Token', '##izer', 'in', 'action', '.']

Training a New Tokenizer

from ultra_tokenizer import TokenizerTrainer

# Initialize trainer
trainer = TokenizerTrainer(
    vocab_size=30000,
    min_frequency=2,
    show_progress=True
)

# Train on text files
tokenizer = trainer.train_from_files(["file1.txt", "file2.txt"])

# Save tokenizer
tokenizer.save("my_tokenizer.json")

# Load tokenizer
from ultra_tokenizer import Tokenizer
tokenizer = Tokenizer.from_file("my_tokenizer.json")

Advanced Usage

Custom Tokenization Rules

from ultra_tokenizer import Tokenizer
import re

# Custom tokenization with regex pattern
custom_tokenizer = Tokenizer(
    tokenization_pattern=r"\b\w+\b|\S"  # Words or non-whitespace characters
)

Batch Processing

# Process multiple texts efficiently
texts = ["First sentence.", "Second sentence.", "Third sentence."]
all_tokens = [tokenizer.tokenize(text) for text in texts]

Customization

Special Tokens

from ultra_tokenizer import Tokenizer

# Initialize with custom special tokens
tokenizer = Tokenizer(
    special_tokens={
        "unk_token": "[UNK]",
        "pad_token": "[PAD]",
        "cls_token": "[CLS]",
        "sep_token": "[SEP]"
    }
)

Custom Preprocessing

def custom_preprocessor(text):
    # Your custom preprocessing logic here
    text = text.lower()
    text = re.sub(r'\s+', ' ', text).strip()
    return text

tokenizer = Tokenizer(preprocessing_fn=custom_preprocessor)

Fine-tuning

Updating an Existing Tokenizer

# Continue training on new data
with open("new_data.txt", "r", encoding="utf-8") as f:
    trainer = TokenizerTrainer(vocab_size=35000)  # Slightly larger vocab
    tokenizer = trainer.train_from_files(
        ["new_data.txt"],
        initial_tokenizer=tokenizer  # Start from existing tokenizer
    )

Domain-Specific Fine-tuning

# Fine-tune on domain-specific data
domain_trainer = TokenizerTrainer(
    vocab_size=32000,
    min_frequency=1,  # Include rare terms
    special_tokens={"additional_special_tokens": ["[MED]", "[DISEASE]", "[TREATMENT]"]}
)

domain_tokenizer = domain_trainer.train_from_files(
    ["medical_corpus.txt"],
    initial_tokenizer=tokenizer  # Start from base tokenizer
)

Performance Optimization

# Optimize for inference
tokenizer.enable_caching()  # Cache tokenization results
fast_tokens = tokenizer.tokenize("Optimized for speed!")

Documentation

For detailed documentation, examples, and API reference, please visit:

Ultra-Tokenizer Documentation

Contributing

Contributions are welcome! Please read our Contributing Guide to get started.

License

This project is licensed under the Apache 2.0 License - see the LICENSE file for details.

Contact

For questions or feedback, please open an issue or contact pranav.singh01010101@gmail.com.

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

ultra_tokenizer-0.1.3.tar.gz (39.1 kB view details)

Uploaded Source

Built Distribution

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

ultra_tokenizer-0.1.3-py3-none-any.whl (31.8 kB view details)

Uploaded Python 3

File details

Details for the file ultra_tokenizer-0.1.3.tar.gz.

File metadata

  • Download URL: ultra_tokenizer-0.1.3.tar.gz
  • Upload date:
  • Size: 39.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for ultra_tokenizer-0.1.3.tar.gz
Algorithm Hash digest
SHA256 961255ad0e1dadadafd1d9dde21516ff42d2aeb8f9b02b10fb33e007d475e8d5
MD5 99dea39a1c1a10838e44ee3c5a54f10f
BLAKE2b-256 f66bd3391cd6509696482a9fbb2023cad8717c9949918340f1576ded92fec734

See more details on using hashes here.

File details

Details for the file ultra_tokenizer-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for ultra_tokenizer-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5bde95e48f2a4632b0d0651e9b020fb845afe55a657e7bc4a7ddcae5dfdc8326
MD5 3a5966b28802e3534810bf1dfadd2e03
BLAKE2b-256 feaf183266aaba6e894f73d007c6fe9b0a4148b3879e796446a3a933a7aa2561

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