Skip to main content

BPE tokenizer for the toke programming language (16K vocab)

Project description

toke-tokenizer

A pure Python BPE tokenizer for the toke programming language. Trained on normalised toke source code with a 16,384-token vocabulary.

Achieves approximately 52% token reduction compared to OpenAI's cl100k_base tokenizer on toke source code.

Installation

pip install toke-tokenizer

Usage

from toke_tokenizer import encode, decode, count_tokens

# Tokenize toke source code
text = "let x:int = 42"
tokens = encode(text)
print(tokens)        # list of token IDs

# Decode back to text
original = decode(tokens)
print(original)      # "let x:int = 42"

# Count tokens
n = count_tokens(text)
print(f"{n} tokens")

String normalisation

Toke source often contains string literals that are not useful for structural tokenization. You can normalise strings before counting:

from toke_tokenizer import count_tokens

# Without normalisation
count_tokens('let msg:str = "hello world"')

# With normalisation (replaces string contents with "_")
count_tokens('let msg:str = "hello world"', normalise_strings=True)
# Equivalent to counting: let msg:str = "_"

API

  • encode(text: str) -> list[int] — Tokenize text into a list of token IDs.
  • decode(ids: list[int]) -> str — Convert token IDs back to text.
  • count_tokens(text: str, normalise_strings: bool = False) -> int — Count the number of tokens in text.

Details

  • Vocabulary: 16,384 tokens
  • Algorithm: Byte-Pair Encoding (BPE)
  • Pre-tokenization: splits on newlines (each newline is a separate token)
  • Special tokens: <|endoftext|> (0), <pad> (1), <newline> (2)

License

MIT

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

toke_tokenizer-0.1.0.tar.gz (265.3 kB view details)

Uploaded Source

Built Distribution

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

toke_tokenizer-0.1.0-py3-none-any.whl (273.6 kB view details)

Uploaded Python 3

File details

Details for the file toke_tokenizer-0.1.0.tar.gz.

File metadata

  • Download URL: toke_tokenizer-0.1.0.tar.gz
  • Upload date:
  • Size: 265.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for toke_tokenizer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2f3d5165abaf12f517bd9c4c2b4b917150c85f97c60307fa61d7214c0b94fe39
MD5 b51f585b71238552dbac1592aac4cc51
BLAKE2b-256 057259c9e9a96c8ab3d39026e4cbdc45a8650d81fa34213ba41383a963750a36

See more details on using hashes here.

File details

Details for the file toke_tokenizer-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: toke_tokenizer-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 273.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for toke_tokenizer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c33eee7501da85966f969dc0007afd30c9cc2a7f2a6fb5f8b769aa7762210fcb
MD5 a9170d3f5dfb805f715f04fe493040ad
BLAKE2b-256 7a1b468e5067d680c881468c8f33e05d4c5ae27363e2c3fc222cbf1007b1181b

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