A simple sum-based tokenizer for NLP/LLM research
Project description
🍇 Grapes Tokenizer
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:
-
Mapping each character:
a–z→1–260–9→0–9- others →
ord(ch)(ASCII code)
-
Multiplying each value by its 1-based position index
-
Converting each result to binary and summing via binary addition
-
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
GrapesTokenizerto override_char_to_value - Batch processing: use list comprehensions or DataFrame apply for large corpora
Development
-
Clone the repo
-
Create a virtual environment
-
Install dev dependencies:
pip install -e .[dev]
-
Run tests:
pytest
-
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f7dbc79e9127b4df6c1fc2e2543e7bbd08d34680f535bbabee09f3a00b4cb206
|
|
| MD5 |
0d75e8bbaa9e954dac2898a1b4948498
|
|
| BLAKE2b-256 |
af6e799d16f506ac9ca5c97f457cffdbd11456e7e81e0e991547298f553087f6
|
File details
Details for the file grapes_tokenizer-0.1.3-py3-none-any.whl.
File metadata
- Download URL: grapes_tokenizer-0.1.3-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
948632630f69bc544d86b0e539b10bdb7d04ea298bc6abc1a4bdf1c4033b3e1b
|
|
| MD5 |
d08727e63d0c7006fe8e0e7aaaebd5da
|
|
| BLAKE2b-256 |
e44250c6e03d180f042663779bd58d01319d0ec51137eb8445c33ff011d7257c
|