MCPcopy Create free account
hub / github.com/OpenNMT/Tokenizer

github.com/OpenNMT/Tokenizer @v1.38.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.38.1 ↗ · + Follow
293 symbols 727 edges 37 files 3 documented · 1% updated 6mo agov1.38.1 · 2026-01-10★ 3346 open issues

Browse by type

Functions 261 Types & classes 32
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

CI PyPI version Forum

Tokenizer

Tokenizer is a fast, generic, and customizable text tokenization library for C++ and Python with minimal dependencies.

Overview

By default, the Tokenizer applies a simple tokenization based on Unicode types. It can be customized in several ways:

  • Reversible tokenization

Marking joints or spaces by annotating tokens or injecting modifier characters. * Subword tokenization

Support for training and using BPE and SentencePiece models. * Advanced text segmentation

Split digits, segment on case or alphabet change, segment each character of selected alphabets, etc. * Case management

Lowercase text and return case information as a separate feature or inject case modifier tokens. * Protected sequences

Sequences can be protected against tokenization with the special characters ⦅ and ⦆.

See the available options for an overview of supported features.

Using

The Tokenizer can be used in Python, C++, or command line. Each mode exposes the same set of options.

Python API

pip install pyonmttok
>>> import pyonmttok
>>> tokenizer = pyonmttok.Tokenizer("conservative", joiner_annotate=True)
>>> tokens = tokenizer("Hello World!")
>>> tokens
['Hello', 'World', '■!']
>>> tokenizer.detokenize(tokens)
'Hello World!'

See the Python API description for more details.

C++ API

#include <onmt/Tokenizer.h>

using namespace onmt;

int main() {
  Tokenizer tokenizer(Tokenizer::Mode::Conservative, Tokenizer::Flags::JoinerAnnotate);
  std::vector<std::string> tokens;
  tokenizer.tokenize("Hello World!", tokens);
}

See the Tokenizer class for more details.

Command line clients

$ echo "Hello World!" | cli/tokenize --mode conservative --joiner_annotate
Hello World ■!
$ echo "Hello World!" | cli/tokenize --mode conservative --joiner_annotate | cli/detokenize
Hello World!

See the -h flag to list the available options.

Development

Dependencies

Compiling

CMake and a compiler that supports the C++11 standard are required to compile the project.

git submodule update --init
mkdir build
cd build
cmake .. -DICU_ROOT=<path to root of ICU dependencies>
make

It will produce the dynamic library libOpenNMTTokenizer and tokenization clients in cli/.

  • To compile only the library, use the -DLIB_ONLY=ON flag.

Testing

The tests are using Google Test which is included as a Git submodule. Run the tests with:

mkdir build
cd build
cmake -DBUILD_TESTS=ON ..
make
test/onmt_tokenizer_test ../test/data

Core symbols most depended-on inside this repo

Shape

Function 156
Method 105
Class 23
Enum 9

Languages

C++82%
Python18%

Modules by API surface

bindings/python/test/test.py47 symbols
bindings/python/pyonmttok/Python.cc36 symbols
src/Tokenizer.cc35 symbols
src/unicode/Unicode.cc22 symbols
src/BPELearner.cc16 symbols
src/Casing.cc13 symbols
src/BPE.cc13 symbols
include/onmt/unicode/Unicode.h10 symbols
src/ITokenizer.cc9 symbols
include/onmt/Token.h9 symbols
test/test.cc8 symbols
src/Utils.cc8 symbols

For agents

$ claude mcp add Tokenizer \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page