Skip to main content

A simple sum-based tokenizer for NLP/LLM research

Project description

🍇 Grapes Tokenizer

PyPI version License: Apache-2.0 Build Status


Grapes Tokenizer is a lightweight, pure-Python library for converting text into deterministic, sum-based tokens. Originally designed for NLP research and LLM experiments, it offers a toy-friendly encoding scheme that:

  • Maps characters to numeric values (letters, digits, and specials)
  • Applies positional weighting to capture order information
  • Performs binary addition of weighted values for a final single decimal token

While not suited for production subword tokenization, Grapes Tokenizer is perfect for exploring collision patterns, prompt engineering tricks, or integrating custom token schemes into larger pipelines.

Table of Contents

Features

  • 🔢 Deterministic: identical text always yields the same token
  • ⚙️ Positional Weighting: retains order by multiplying each value by its index
  • 🔣 Full ASCII Support: letters, digits, punctuation, whitespace
  • ⚠️ Non-reversible: cannot reconstruct original text from token
  • ⚠️ Collisions Possible: different strings may produce the same token
  • 🚀 Zero Dependencies: pure Python 3.6+ implementation

Installation

Install from PyPI:

pip install grapes-tokenizer

Or clone and install from source:

git clone https://github.com/akashpittalwar/Grapes-tokenizer.git
dcd Grapes-tokenizer
pip install -e .

Quick Start

from grapes_tokenizer import GrapesTokenizer

# Initialize the tokenizer (always positional weighting)
tok = GrapesTokenizer()

# Encode text to a single decimal token
print(tok.encode("cat"))    # → 65
print(tok.encode("tac"))    # → 31
print(tok.encode("!2872B")) # → 111

API Reference

class GrapesTokenizer(case_sensitive: bool = False)

Create a new tokenizer instance.

  • case_sensitive: keep letter case (True) or normalize to lowercase (False, default).

Methods

encode(text: str) -> int

Converts the input string text to a single decimal token by:

  1. Mapping each character:

    • a–z1–26
    • 0–90–9
    • others → ord(ch) (ASCII code)
  2. Multiplying each value by its 1-based position index

  3. Converting each result to binary and summing via binary addition

  4. Returning the final sum as a decimal integer

tok = GrapesTokenizer()
token = tok.encode("Hello, world!")

Examples

Basic Encoding

>>> tok.encode("apple")
# Computation: a*1 + p*2 + p*3 + l*4 + e*5

Segment-and-Pack

Split text into words, spaces, and punctuation, then encode each segment:

import re
from grapes_tokenizer import GrapesTokenizer

def simple_segment(text):
    return re.findall(r"\w+|\s+|[^\w\s]", text)

def segment_and_pack(text):
    tok = GrapesTokenizer()
    return [tok.encode(seg) for seg in simple_segment(text)]

print(segment_and_pack("Hello World!"))
# → [0x48656c6c6f, 0x20, 0x576f726c64, 0x21]

Advanced Usage

  • Case-sensitive mode: preserve uppercase values
  • Custom mapping: subclass GrapesTokenizer to override _char_to_value
  • Batch processing: use list comprehensions or DataFrame apply for large corpora

Development

  1. Clone the repo

  2. Create a virtual environment

  3. Install dev dependencies:

    pip install -e .[dev]
    
  4. Run tests:

    pytest
    
  5. Build docs and distribution:

    python -m build
    

Contributing

Contributions are welcome! Please open issues and pull requests on GitHub:

https://github.com/akashpittalwar/Grapes-tokenizer

Please review CONTRIBUTING.md and adhere to the Apache 2.0 license.

License

This project is licensed under the Apache 2.0 License. See LICENSE 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

grapes_tokenizer-0.1.3.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

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

grapes_tokenizer-0.1.3-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for grapes_tokenizer-0.1.3.tar.gz
Algorithm Hash digest
SHA256 f7dbc79e9127b4df6c1fc2e2543e7bbd08d34680f535bbabee09f3a00b4cb206
MD5 0d75e8bbaa9e954dac2898a1b4948498
BLAKE2b-256 af6e799d16f506ac9ca5c97f457cffdbd11456e7e81e0e991547298f553087f6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for grapes_tokenizer-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 948632630f69bc544d86b0e539b10bdb7d04ea298bc6abc1a4bdf1c4033b3e1b
MD5 d08727e63d0c7006fe8e0e7aaaebd5da
BLAKE2b-256 e44250c6e03d180f042663779bd58d01319d0ec51137eb8445c33ff011d7257c

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