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.6-cp314-cp314t-musllinux_1_2_x86_64.whl (2.0 MB view details)

Uploaded CPython 3.14tmusllinux: musl 1.2+ x86-64

ft_tokenize-0.1.6-cp314-cp314t-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (954.0 kB view details)

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

ft_tokenize-0.1.6-cp314-cp314-musllinux_1_2_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.14musllinux: musl 1.2+ x86-64

ft_tokenize-0.1.6-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (841.2 kB view details)

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

ft_tokenize-0.1.6-cp313-cp313-musllinux_1_2_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

ft_tokenize-0.1.6-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (730.6 kB view details)

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

ft_tokenize-0.1.6-cp312-cp312-musllinux_1_2_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

ft_tokenize-0.1.6-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (620.2 kB view details)

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

ft_tokenize-0.1.6-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.6-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (509.6 kB view details)

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

ft_tokenize-0.1.6-cp310-cp310-musllinux_1_2_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

ft_tokenize-0.1.6-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (400.1 kB view details)

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

ft_tokenize-0.1.6-cp39-cp39-musllinux_1_2_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.9musllinux: musl 1.2+ x86-64

ft_tokenize-0.1.6-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (292.0 kB view details)

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

ft_tokenize-0.1.6-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.6-cp38-cp38-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (183.7 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.6-cp314-cp314t-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for ft_tokenize-0.1.6-cp314-cp314t-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 d6d2050af20531fd412decbd7e3c5435a2c97826acd51370f72d19612cff4634
MD5 d4524481b931890874632bd733cf74ab
BLAKE2b-256 5d0f3af58a1d140e82be8615d37bf9b61e2954ea9786b8f32ab6a84dc8cf8607

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.6-cp314-cp314t-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 dcf1fa30ad4abb03e0b419b56f83ab79f865fb2c8ac973d4f79efb2d97c3bcd8
MD5 dd1bcf6fc5c080b8e9d69d7d197f5da9
BLAKE2b-256 cfe2f9d6fdb853565a239c82af0fdd257e459660017900d4e4027170e49762a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.6-cp314-cp314-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 fe1862f989fabb0bf603ae9769ad571046bacb80c4eca077d952f161d51a6253
MD5 c023e5f585a4b67dd23afb5a8d417b97
BLAKE2b-256 21d4c7534dc70884df00bbce1838cac69671a108b05256a9cd95bc33bcafa64e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.6-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 865178db19c64532c8c4f8b58dd88761f3734667779ce21f59763f62397c6baa
MD5 de38b9b3db08ea62db9a813c2acc69cf
BLAKE2b-256 4b6add2c3aa2326c253213418fa3b12e0cbdb3b6e05039170af8922dc333dc43

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.6-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 806b49119b8dc003fd3436a0034ae749eb2734d46dc4c9bf76078cdfcf3e9df3
MD5 099b30ddd188a1bcb1bf39489199791e
BLAKE2b-256 2f536a3865dc3df150cc886de9a8d6436faa7746b7a0bf600b390077ed7e7bb1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.6-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0372bc7f6f717b08e534dc64e6725ffdca1b98f99a456723ad6ffdbdda38165d
MD5 76c2f4f7ac1db029c6b27d31bafe5d5c
BLAKE2b-256 66f0ba57b1606f2bb39e392a7efbaf71a5954206f05af6051e4be4bc35817b48

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.6-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 1cc8d93d9897015455836f53bba0c52ebf7d5d57f035ef201ed67642882f9d00
MD5 a18f3051403f401305cc5635e38e13cd
BLAKE2b-256 374cfae6622ede261a4ce908a5aaf3f7c58476a66f1ed69612442f757433c9bc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.6-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c97fa2a8ea0ff73c7872c683403b3fe914abde32be79bf8603f6c788244009f3
MD5 7a1020791283896658b9457072308ec5
BLAKE2b-256 65ed64d0b051f19e420bb7aafa46c30fe155d7ad93759a60e3d70477f358522b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.6-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 1f3888d5f818375a1dfcae80fcca9434537708e3fdc602ae4f419fa96e5e4831
MD5 6f3dc1816a7d2d12378b75ef82dc7cf6
BLAKE2b-256 e78e9803a13b45679c46415b2e1448834ab32efa605297c34612a3a812c8eb1e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.6-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 46a1c64f694bf8f306db2d01a9417f4db0eed645aa0979340f868b80e1d637dc
MD5 d9226838a4e07d2500940244459bf25a
BLAKE2b-256 d5e55952104d6232c1a6ad1a4a448884a0cbd40e34cf6cc4e5a96732815b8cfc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.6-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 e544c2a83b521981a4ca7f9df43de05a7830316fd1245578a89aacc64e28848b
MD5 6f943cfed0ab68769fb5e489483d5419
BLAKE2b-256 d7aac36cd870f320121ecfafcfec2f14800d5f373b546d017edc3d44279bc7b1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.6-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 19393ef7599fc995bff7a686159eec41773e58fcd7c59f2a1e4626b0f7382594
MD5 b3765625ee5f57c9e7be1292508092d4
BLAKE2b-256 93ec327dd41b40bd6c09d7111d1faec0b44e8cead158926b556bca7745913d38

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.6-cp39-cp39-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 0c1b7a2cff31dc183d860b04b12cac766e96f37f522496042ca806db4228e14f
MD5 f39181ab7f5c4a88d5200d4e87daee2a
BLAKE2b-256 99a59a387686e26fb5320549f44ff612115e1f1691dff7758e7fd4e8e63b572f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.6-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0ad7b6903bf11b37fae800af365f8803aab4a05d26968f334490898c1368adf8
MD5 f840107e7d34aebf60c744fe88a664a7
BLAKE2b-256 5416a829c1fcc3258dac6a2e48deaa4f936eb3ac7a8e0be52f46f30a31028497

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.6-cp38-cp38-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 b568f4ed9c8dece830d537c670dc9310f98944531a0a414d9d9607a98fb388c2
MD5 e1d01548d25ceda8dcb2eea7e3f759fe
BLAKE2b-256 50ba0bd223764fdc09f0adf115393cd5c155333bcbefbac2b61ee7fab119f877

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ft_tokenize-0.1.6-cp38-cp38-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 860d988529ed3ea8bcaea59e6bdc91efe969a8eacd86a8443e1e2566d15564ae
MD5 ccc392ce2acb6d9e435869ce95d6a85a
BLAKE2b-256 7a5e3735069f0abc3dc240f05643951fcfb0bebeccb89882ba7dfe8b47055915

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