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.


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.7-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.7-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.7-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.7-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (994.7 kB view details)

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

ft_tokenize-0.1.7-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.7-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (861.8 kB view details)

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

ft_tokenize-0.1.7-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.7-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (729.1 kB view details)

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

ft_tokenize-0.1.7-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.7-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (596.2 kB view details)

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

ft_tokenize-0.1.7-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.7-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (464.7 kB view details)

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

ft_tokenize-0.1.7-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.7-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (334.9 kB view details)

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

ft_tokenize-0.1.7-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.7-cp38-cp38-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (205.3 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.7-cp314-cp314t-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ft_tokenize-0.1.7-cp314-cp314t-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 669dabae1a235734c8a90d7b3a30004c0abc9757b2992c12db351ef8404c953c
MD5 1c13fd4874ed37e53001bee582e09f4a
BLAKE2b-256 47faf8d18eecae9fb0e4b37cb37904dcc5d1073f0889a8e4e565b55188ba25d7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.7-cp314-cp314t-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 9fbe05bd39f6819bf6e2cbaa6a11a4d55c6bfd5d5bc29cd3db8fde82877b6ea8
MD5 4e1c79f03ae10525d0113eed8d7c4ff2
BLAKE2b-256 8b89568d207de3df131a872650a0b556fa91b4decb16c6741ea8b8aaeefc0c6d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.7-cp314-cp314-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 7353c4d399e39e936f52ef2ad584fbf1a378e41cf03f604a1f99877b87c43470
MD5 fee52068581d28763b2c4bca9e4bcd27
BLAKE2b-256 65b6343d44cd51c454cd929dfad095e92523a76780af49cce77f2a74b1f38184

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.7-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 229fbe8330ec7e7641e8c0674318af9a64c062518d3a9ab76720b68dbb587f10
MD5 b414f4270b5882ddfec7ceaee9a53e40
BLAKE2b-256 2425fa0d72b2ef523f9d88192b93fd0cc10eef00788cfbb2e00436a6501f90f7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.7-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 2351ad31e490fd2fc78a452145208b7dff6c84b49a63314a9f350769d8b98aab
MD5 e6a986cfd12e60d06132b5cc8d9c0d0d
BLAKE2b-256 dfc3efd0ae0b0990e5b99a9e70cad4f8f9792853a2c0e3ef05038bd2060e53b1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.7-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 973f3a60382f742bb275e91942ed9bfb920098164a4f6b9519fb54a2922c786b
MD5 0823da0d9e862fa5de9aa7d9298c7af7
BLAKE2b-256 54a8d6b34b4f444af0e93daecb99bebafcfcf9f090d7da6d724bb98e54f4ca38

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.7-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 7672dbe3499865b36d0cb871690d55ecea84b10383ca9f4e5c9886b8c106cdd8
MD5 e832b76ee140059aa66086abead50cc8
BLAKE2b-256 b399acf48115cca869ff68e6003bf1abe0f8dee2c456c2ee796a089d26dcaee3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.7-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 f5068ea82502b2b7b318fde35b31ecd75d906337bb592a16dba962da5f8259fe
MD5 e8d393da35c89e49a80bc0b4c1f5f15b
BLAKE2b-256 afcd0e80b8f82a3e1072d36597ab204572a1a668802fab8f20b8a13950a9d827

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.7-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 20beebe1586c5733a3fbb9b8c1eb0d593ac24cf3e30d078a2825ce8a3a8a1297
MD5 80a9a776b3814c618a1713679e65610b
BLAKE2b-256 a6bbba436809e1dba15c3578d89b69e5b75ba922ddad7a7fa5778e3f06d5a5d0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.7-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 bbc0394f6f3954dffdc8ba717aea3782b9f2400fba77dc207d00a30aea88ef23
MD5 d1e3be2069a0c3a1e6fc7e1e72241e13
BLAKE2b-256 88a0b8e3a956b33c3dfc5fe4df2530d3ee8a6115f986d7d8aa8001a6366edb8a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.7-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 d5750ff1f7053dff4f3eb3f7f2cfb753a85fcb39315941e1c4b2c93e55b9c2d6
MD5 0d37e0c1c5843e3608720e034235017e
BLAKE2b-256 3d94999440acca8f1409517673d4a705ca1b338a67fe46a5c223e2e4ed2d68cb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.7-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 8c86b07019b4258a3f4d3697a55b631d6a0ff48ef6b932a3f462a6e611332c40
MD5 930973e11824eb86da5190ce232bd0d7
BLAKE2b-256 a9d71f4a1ec65fb108473f625b4abe47d15cc7def6b8c7ee7d794673dfa04f47

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.7-cp39-cp39-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 4f6faee611ac318117c8a6ad7c6170026902ed78883fb1cadbaac4fb2cb59862
MD5 52179d795237e81c2edaa0dd48ee2635
BLAKE2b-256 71c5087ad29ba3e99db36870cb4f7d5dd3c9a2a0eaf5d25f47d8d91e88084810

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.7-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 285bc5cff32b50ca455ef17f80758be5778f52bab26eba6af91c5a3e167f85f5
MD5 5b800f1913cd3f3bbfce98eac09ed27e
BLAKE2b-256 620248e112190b24ae7d6d34d2c9238cd67dbe13807d6e6cdb22bcc63c793920

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.7-cp38-cp38-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 7dbaa3e69061dc40989aeb708321dc9ac5298bf43719f6947057c46eac844c3d
MD5 45ee62042d28864c6da6fc1fc9964afe
BLAKE2b-256 9f845aa27cbeaf4fb7ae47a8fe9eaac05348cd00cf10022d98c3ae9ed672d25a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.7-cp38-cp38-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 70a753e28e792deb0f92b6785d3a7c96deb62641bf11f78564c969d639693eab
MD5 63caeaf938a9dbdae7acfa978fbaa76b
BLAKE2b-256 77acfc8ce108c9e08c3f9297b3ade1af3bdc18c512983a83543d0d97b7a6e243

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