Tokenizer for NLP
Project description
FT Tokenize
I created this small C++17 tokenizer library as a personal project to better understand how tokenization works.
It supports both word-level and BPE (Byte Pair Encoding) tokenization and provides a simple Python API via pybind11.
The library is also published on PyPI, so it can be easily installed with pip to train, save, load, encode, and decode text.
A demo notebook demonstrating how to use the library is available in the root of this repository and is linked with a Kaggle notebook.
Features
- Word-level tokenizer
- BPE tokenizer with merge-rule learning
- Training from text files
- Vocabulary export / import
- Token ↔ ID conversions
- Thread-safe operations using
std::mutex - Python bindings via
pybind11
Requirements
- Python 3.8+
Installation
You can install the package directly with:
pip install ft_tokenize
Python Usage Example
from ft_tokenize import TokenizerModel
# Create a tokenizer
tok = TokenizerModel()
# Train from a text file
tok.train_from_textfile(
"data.txt",
vocab_size=10000,
user_defined_symbols=["<sos>", "<eos>"],
mode="BPE"
)
# Save / load model
tok.save_model("vocab.txt")
tok.load_model("vocab.txt")
# Encode / decode some sample text
ids = tok.encode_as_ids("My name is Tommaso")
tokens = tok.encode_as_tokens("My name is Tommaso")
# Print the token IDs to see how the text is split internally
print(ids) # Example output: [5, 8, 9, 12]
# Print the actual token strings
print(tokens) # Example output: ['My', 'name', 'is', 'Tommaso']
# Decode back from IDs and tokens to check correctness
print(tok.decode_ids(ids)) # Should return "My name is Tommaso"
print(tok.decode_tokens(tokens)) # Should return "My name is Tommaso"
# Check vocabulary details
print("Vocab size:", tok.get_token_size()) # How many tokens are in the vocabulary
print("Token for ID 10:", tok.id_to_token(10)) # Look up a token by its ID
Project Structure
src/
ft_tokenizer.cpp # pybind11 module
tokenizer_model.hpp # Tokenizer class definition
tokenizer_model.cpp # Implementation
How It Works
WORD mode
- Splits text by whitespace
- Builds a frequency-sorted vocabulary
- Maps unknown words to
<unk>
BPE mode
- Splits words into characters
- Iteratively merges the most frequent symbol pairs
- Stores merge rules inside the vocabulary
- Encodes text greedily (longest match)
The TokenizerModel class provides the following methods:
Training
-
train_from_textfile(input_file, vocab_size=10000, user_defined_symbols=[], mode="WORD")Train the tokenizer from a text file.input_file: path to a text file for trainingvocab_size: maximum number of tokens in the vocabularyuser_defined_symbols: list of extra tokens to includemode:"WORD"or"BPE"
-
train_word_level(input_file, vocab_size, user_defined_symbols)Train a word-level tokenizer. Usually called internally. -
train_bpe(input_file, vocab_size, user_defined_symbols)Train a BPE tokenizer. Usually called internally.
Saving and Loading
-
save_model(model_path)Save the current vocabulary to a file. -
load_model(model_path)Load a vocabulary from a file.
Encoding
-
encode_as_ids(text)→ List of integers Convert a string into token IDs. -
encode_as_tokens(text)→ List of strings Convert a string into token strings.
Decoding
-
decode_ids(ids)→ String Convert a list of token IDs back into a string. -
decode_tokens(tokens)→ String Convert a list of token strings back into a string.
Utility
-
token_to_id(token)→ Integer Get the ID of a token. Returns the<unk>ID if the token is not found. -
id_to_token(id)→ String Get the token corresponding to an ID. Returns<unk>if the ID is invalid. -
get_token_size()→ Integer Returns the size of the vocabulary. -
get_vocab()→ List of strings Returns the full vocabulary.
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 Distributions
Built Distributions
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 ft_tokenize-0.1.9-cp313-cp313-macosx_11_0_arm64.whl.
File metadata
- Download URL: ft_tokenize-0.1.9-cp313-cp313-macosx_11_0_arm64.whl
- Upload date:
- Size: 726.5 kB
- Tags: CPython 3.13, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7fb34b18602d03373686307213626c484ba5ea646184a034ee670523a54daa66
|
|
| MD5 |
aa0eae8a5cefa8d298e855c8baaa8897
|
|
| BLAKE2b-256 |
735b29056c4d2c697929d832073274f932d90d3cd7a30e91a5fcca31bd094525
|
File details
Details for the file ft_tokenize-0.1.9-cp312-cp312-macosx_11_0_arm64.whl.
File metadata
- Download URL: ft_tokenize-0.1.9-cp312-cp312-macosx_11_0_arm64.whl
- Upload date:
- Size: 617.5 kB
- Tags: CPython 3.12, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
63e048d29ab30bb78d46980cd3737f71feda9412579460777878b3c7d6eb0313
|
|
| MD5 |
f453f3969cf7188e429fd81be570755a
|
|
| BLAKE2b-256 |
c7f4d6ee0c427e416444fd75042f3cac1c08108067a2e1f759c07ce859e4b299
|
File details
Details for the file ft_tokenize-0.1.9-cp311-cp311-macosx_11_0_arm64.whl.
File metadata
- Download URL: ft_tokenize-0.1.9-cp311-cp311-macosx_11_0_arm64.whl
- Upload date:
- Size: 508.4 kB
- Tags: CPython 3.11, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dae0b8584f1e18da4434858e0f70a01be729517a17ba44fb08979e355d723aaa
|
|
| MD5 |
96167cb078b778ad3ab3b4ca364c79d5
|
|
| BLAKE2b-256 |
eb87f24305be3113fcc9b72b8a1f43ed53cfc4ff75e53929283779223bbed5d9
|
File details
Details for the file ft_tokenize-0.1.9-cp310-cp310-macosx_11_0_arm64.whl.
File metadata
- Download URL: ft_tokenize-0.1.9-cp310-cp310-macosx_11_0_arm64.whl
- Upload date:
- Size: 399.3 kB
- Tags: CPython 3.10, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aeed553b808771a23ee46a009ace6b0c9ff04aa6bed8bba9fd9f6f1b40fd9745
|
|
| MD5 |
d266030f5418a630c5c25c34cd395997
|
|
| BLAKE2b-256 |
d662b4c7b7f657ea8e18981e6b970c120b5247e715572f0fe489f72af97213ea
|
File details
Details for the file ft_tokenize-0.1.9-cp39-cp39-macosx_11_0_arm64.whl.
File metadata
- Download URL: ft_tokenize-0.1.9-cp39-cp39-macosx_11_0_arm64.whl
- Upload date:
- Size: 291.5 kB
- Tags: CPython 3.9, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f5260d9a251e34cf564873e8c4d3dadcc8ce737808afb48a3e60da0a79dadfdd
|
|
| MD5 |
c2cca9febbc5bb3e50dab917026427ab
|
|
| BLAKE2b-256 |
9f02234cf1dbda52eb0b460c6b5b7a4b9af53b26c52edf25c44ef85334da39f5
|
File details
Details for the file ft_tokenize-0.1.9-cp38-cp38-macosx_11_0_arm64.whl.
File metadata
- Download URL: ft_tokenize-0.1.9-cp38-cp38-macosx_11_0_arm64.whl
- Upload date:
- Size: 183.6 kB
- Tags: CPython 3.8, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
14b12aac34c92b4f6508e64ac0e41a51715ac841b5e221287ac2d2966b2e3988
|
|
| MD5 |
d255549841b919d9bc0024d346a9cde0
|
|
| BLAKE2b-256 |
3d10089e403580f80e20c552ea6e7a82c55e74cf1599b562100ad95f99e3bc49
|