Skip to main content

A high-performance Python tokenizer using Byte-Pair Encoding (BPE) with 100k vocabulary, supporting text encoding, decoding, and normalization for NLP applications.

Project description

BPE Tokenizer

bpe_tokenizer is a high-performance Python tokenizer based on Byte-Pair Encoding (BPE). It supports a 100k vocabulary, efficient text encoding/decoding, batch processing, and basic text normalization. It is designed for NLP applications, preprocessing pipelines, or any project that requires custom tokenization.

Features

  • Efficient Encoding: Encode text into token IDs using BPE.

  • Decoding: Decode token IDs back to human-readable text.

  • Batch Processing: Support for encoding multiple texts at once.

  • Special Tokens: Built-in support for <UNK>, <BOS>, and <EOS>.

  • Large Vocabulary: Supports a vocabulary size of up to 100,000 tokens.

  • Normalization: Basic text normalization (lowercasing, punctuation removal).

  • Sentence Splitting: Simple utilities to split text into sentences.

Installation

You can install directly from PyPI :

pip install bpe_tokenizer

Quick Start

Import and Initialize

from bpe_tokenizer import Tokenizer

# Initialize tokenizer
tokenizer = Tokenizer()

Encode a Single Text

text = "Hello world!"
encoded = tokenizer.encode(text)
print("Encoded:", encoded)

Output:

Encoded: [72, 9257, 1295, 33]

Decode Token IDs

decoded = tokenizer.decode(encoded)
print("Decoded:", decoded)

Output:

Decoded: Hello world!

Encode a Batch of Texts

texts = ["Hello world!", "Byte-Pair Encoding example."]
encoded_batch = tokenizer.encode_batch(texts)
print("Batch Encoded:", encoded_batch)

Output:

Batch Encoded: [[72, 9257, 1295, 33], [66, 121, 507, 45, 80, 937, 18258, 16022, 2461, 46]]

Additional Utilities

# Get number of tokens in a text
print(tokenizer.token_count("Hello world!"))

# Normalize text
print(tokenizer.normalize_text("Hello, World!!!", lower=True, remove_punct=True))

# Split text into words/punctuation
print(tokenizer.tokenize("Hello, world! How are you?"))

Special Tokens

These tokens are used internally to handle unknown tokens or sequence markers.

Token ID Description
<BOS> 256 Beginning of Sequence
<EOS> 257 End of Sequence
<UNK> 258 Unknown Token

Contributing

If you want to improve bpe_tokenizer:

  1. Fork the repository.

  2. Make your changes.

  3. Submit a pull request with a clear description.

License

This project is licensed under the MIT License – see the LICENSE file for details.

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

bp_tokenizer-1.0.0.tar.gz (716.1 kB view details)

Uploaded Source

Built Distribution

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

bp_tokenizer-1.0.0-py3-none-any.whl (718.2 kB view details)

Uploaded Python 3

File details

Details for the file bp_tokenizer-1.0.0.tar.gz.

File metadata

  • Download URL: bp_tokenizer-1.0.0.tar.gz
  • Upload date:
  • Size: 716.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for bp_tokenizer-1.0.0.tar.gz
Algorithm Hash digest
SHA256 6fabd4645d18ee0a1cfb5682bb32b868d58d0cc5f7a70b7f87fa9be4f015f6e9
MD5 81513a929141ee7f0bb2edc7880eb5b3
BLAKE2b-256 5168b871a4679c33d6a1d4c3b5b712f57c8918a733cbe9ddf32513f28c80d10e

See more details on using hashes here.

File details

Details for the file bp_tokenizer-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: bp_tokenizer-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 718.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for bp_tokenizer-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e6a87934e1ac705ec51e0f6792f3a57cd65017443d06f232c44041e09e3d818c
MD5 834237b13ecaa3caa0e64b0f2d3554eb
BLAKE2b-256 413f546b68fd2a0c432daba810fbf9e9f8be83892c38dd4585081efd0c3a25dc

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