Skip to main content

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 training
    • vocab_size: maximum number of tokens in the vocabulary
    • user_defined_symbols: list of extra tokens to include
    • mode: "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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

ft_tokenize-0.1.8-cp314-cp314t-musllinux_1_2_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.14tmusllinux: musl 1.2+ x86-64

ft_tokenize-0.1.8-cp314-cp314t-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.14tmanylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

ft_tokenize-0.1.8-cp314-cp314-musllinux_1_2_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.14musllinux: musl 1.2+ x86-64

ft_tokenize-0.1.8-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (994.9 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

ft_tokenize-0.1.8-cp313-cp313-musllinux_1_2_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

ft_tokenize-0.1.8-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (862.1 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

ft_tokenize-0.1.8-cp312-cp312-musllinux_1_2_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

ft_tokenize-0.1.8-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (729.3 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

ft_tokenize-0.1.8-cp311-cp311-musllinux_1_2_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

ft_tokenize-0.1.8-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (596.4 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

ft_tokenize-0.1.8-cp310-cp310-musllinux_1_2_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

ft_tokenize-0.1.8-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (464.9 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

ft_tokenize-0.1.8-cp39-cp39-musllinux_1_2_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.9musllinux: musl 1.2+ x86-64

ft_tokenize-0.1.8-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (335.3 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

ft_tokenize-0.1.8-cp38-cp38-musllinux_1_2_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.8musllinux: musl 1.2+ x86-64

ft_tokenize-0.1.8-cp38-cp38-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (205.4 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

File details

Details for the file ft_tokenize-0.1.8-cp314-cp314t-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ft_tokenize-0.1.8-cp314-cp314t-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 9b9c9eae5b798701c0c115d32b8ffd403a9e659b3528c5a56f0264a479c45a4e
MD5 36d29b6112ada3efa2b12a6f3b393dc4
BLAKE2b-256 9fa51851a2dcb66b8f560a351eb7563105b77914fc81545802dac5bae50770d3

See more details on using hashes here.

File details

Details for the file ft_tokenize-0.1.8-cp314-cp314t-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ft_tokenize-0.1.8-cp314-cp314t-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b6e2bd5b744c9844e0d8d6f239c2cd9b6f0da1e7334cc19094b00cb13fde1999
MD5 15878105b025b674003aa9afa0a71003
BLAKE2b-256 03bd96b02dc426b9541de655bd0f70e24f207e8a06d86c47192eb51e07f038d2

See more details on using hashes here.

File details

Details for the file ft_tokenize-0.1.8-cp314-cp314-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ft_tokenize-0.1.8-cp314-cp314-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 381663c9fe458b9ee7295825de0ac0988dfe843e334b0774dfaeec5de3bc3755
MD5 296d8332ee2b2d11fa334e4c857d040f
BLAKE2b-256 0ee4680a600ac8a12d9d8da6ca2d8dee05f9a2b365931e023af8f94fbbbf72b2

See more details on using hashes here.

File details

Details for the file ft_tokenize-0.1.8-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ft_tokenize-0.1.8-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 564874906e1d3629ade00181f1a7c9b70939f7c97e4931dff0edd3c32c816bbc
MD5 acf6fa02c64c1691474448f85f6e254d
BLAKE2b-256 9a5eee8f795e8224fba3a866c7b961f93c1f2e000e8681ce185ba0fb0b56405a

See more details on using hashes here.

File details

Details for the file ft_tokenize-0.1.8-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ft_tokenize-0.1.8-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 e12016bf494db005fa5dc95aaf642371027c2dfffdaf1474a7109703dede1c9a
MD5 ca095bcfea3b3370115e519c9dd5269e
BLAKE2b-256 d081dcdd6f0861316e99ae1660f801954c0d73d2a2dc1cf1a559e68d130fff59

See more details on using hashes here.

File details

Details for the file ft_tokenize-0.1.8-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ft_tokenize-0.1.8-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c88695daa0e8e7193eca0b390bb419cd13cc2061bd772b3aa60e8c6d0101486d
MD5 67e8253edea875a3039cd73146172b74
BLAKE2b-256 2b9f80297f07a214f8bce8c1e6a28f768b259dca37657a432b83b4baafa4cab1

See more details on using hashes here.

File details

Details for the file ft_tokenize-0.1.8-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ft_tokenize-0.1.8-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 cff3a3a0837958d9541585e8e9dd8d6242f0118be45603063ff2a68768b5a6b3
MD5 bd4e07f662a287164bdb886814e4801d
BLAKE2b-256 e8ab207a7b9bb241653b27ca499e11f371b4e7f43f84897cfd75c525ce98ed8d

See more details on using hashes here.

File details

Details for the file ft_tokenize-0.1.8-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ft_tokenize-0.1.8-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b4e9d7cda75482cf0c95addc49897a2230613c513403d313b55a4a3843d9a90e
MD5 66b750992bfcdcfda66a521e9a31686a
BLAKE2b-256 655c6de1509d6cc91586d4f44135023d86f10b1f7d1486562c7968f7db423310

See more details on using hashes here.

File details

Details for the file ft_tokenize-0.1.8-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ft_tokenize-0.1.8-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 567f385b3861c401ec3870462da1cbd2a0b87ffea6c96c23a03c6d882b00c82f
MD5 8947faecc9d241f73ac386ba9030d3f1
BLAKE2b-256 a8bc41a174a8cae496951dc947ab77517dc3d112eddfd68953184eed319089ec

See more details on using hashes here.

File details

Details for the file ft_tokenize-0.1.8-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ft_tokenize-0.1.8-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 58664073f291f7050ae9a362be77757e40f19f8ed8a456e684e3ae1e2480b24c
MD5 7eb0a94595454015c013c6c543d18c85
BLAKE2b-256 c5f18f87eaeadd344c21b45b900c04bdc0c80baf70bce2d562c0723da795f6ae

See more details on using hashes here.

File details

Details for the file ft_tokenize-0.1.8-cp310-cp310-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ft_tokenize-0.1.8-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 78c519e687f073e6cde764e82875915a8049f2a8e7235e18fee1e182c109fa17
MD5 6ee54933d843adde48e8ab8bba833196
BLAKE2b-256 bcd11581bc9ac6cacd9578e223a8adcd96249162ba26e8f89c210cc4b75de6de

See more details on using hashes here.

File details

Details for the file ft_tokenize-0.1.8-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ft_tokenize-0.1.8-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8052786f14bfcc911ec3d3cda900d07f1522aaa6653017a0cccd9f1b00470e89
MD5 711b87189fa826ddf4ccc1e043c9fbe4
BLAKE2b-256 b2d6f6549d8ff054fdec15d184b980353f6a35709135a49c7285859fdeb0a97d

See more details on using hashes here.

File details

Details for the file ft_tokenize-0.1.8-cp39-cp39-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ft_tokenize-0.1.8-cp39-cp39-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 da18314adff83854b2cad2a9b08f22514e1b146d26964172b71f2707a6550e68
MD5 558bd29e61d2a62187f4b658de316188
BLAKE2b-256 2a40ca219b1ecfcba01951849599118a9d05ef2b2930f5720e3602eb158fe27b

See more details on using hashes here.

File details

Details for the file ft_tokenize-0.1.8-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ft_tokenize-0.1.8-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b6585e72cbda257466de73bd915b5d737aaf1e97e8d87501a9defb0896142b4b
MD5 4d9e5bba1484a084c9dbd400c18d8bee
BLAKE2b-256 ac0382c564df9e9f38c06bbd72b1ea8a1ae1a6930cff7ac0769662affb6474d6

See more details on using hashes here.

File details

Details for the file ft_tokenize-0.1.8-cp38-cp38-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ft_tokenize-0.1.8-cp38-cp38-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 f45b7b1d6d1991268a8007de5ed08d48a7dae3e13a08c8683adf4442f4a9be83
MD5 0b50f4f0ec4951242b4af5522b4a5c37
BLAKE2b-256 f4c2932a762c540c4a2a902bb8ecf93af4a5cd99a981e867c9d2272090d906b4

See more details on using hashes here.

File details

Details for the file ft_tokenize-0.1.8-cp38-cp38-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for ft_tokenize-0.1.8-cp38-cp38-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7e8fc0590d3d84a3c2535d14d24290ecf26ed75abb5b4d88e3f9e76c70ddb2e1
MD5 b9e3207c3ef5e5b0a75a5b9337b430cb
BLAKE2b-256 520af0699c1cd8e689aab1c186bf18936105349266bbd5ac7b2902d545b5cf92

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