OpenNMT tokenization library
Project description
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.tokenize("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 ..
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
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
Hashes for pyonmttok-1.21.0-cp39-cp39-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0995c6ffe499886b4d0ac7c96ee1f44ee34603e0c63c523fcc79a1982d3aa38e |
|
MD5 | 58452715212a6bb570fd7ccba9a9ad11 |
|
BLAKE2b-256 | 7aaf971bca978b74e55c16828c4f7f50a5db4cdb8e5caa61837e971dffae4217 |
Hashes for pyonmttok-1.21.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d24dba4eb4f2e8c46964cad777ca1c44f012d761d48dd301feb61fae86cabb5 |
|
MD5 | 5663edcb096b03396d8fb0a631fe5b78 |
|
BLAKE2b-256 | e374dd792d8623e9560e0d3f835d91b9b66db7780dfca5a5220ecb8817fe4f3c |
Hashes for pyonmttok-1.21.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee61dc03da2cfcda28463529e8a99a851c2dabf75e93598061c714bbafb57b8e |
|
MD5 | a770daa2f77eb024a37b1d19f82c20ec |
|
BLAKE2b-256 | 533e51a6cdb26f977796ef8cdde62c7ab69169cfbc1a97c21cf4ca5f808c3e69 |
Hashes for pyonmttok-1.21.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e4399a50426b0c8a152b7d9e1d2ef6ff0731becc04786ea75b174098315317a9 |
|
MD5 | 11a4b3079498b5a5303c6383569bc63c |
|
BLAKE2b-256 | 17e5d4544b969d863aed692f6d551010bd5988dfce4f53e1d7dd332702586c41 |
Hashes for pyonmttok-1.21.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 14e0f3f1c43b34f715d1f659c68d94b69ac7f72b207962501b8e35fd6ab6fabf |
|
MD5 | f07a809647dde004a362b4c4b5b0558b |
|
BLAKE2b-256 | dbc2f855d73475df9aa7813e2202019ec9984b622fa4514f0e75fcc0a7b58e0e |
Hashes for pyonmttok-1.21.0-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae3aabfcf2c6a0c69bd87b8348331815f21ad40288f710a0db6a12076cf01087 |
|
MD5 | 5695c1f8929bbfafa93c66392302fcca |
|
BLAKE2b-256 | d5475a5d4114a96a8534dc7c902a044235f241b82dd4c154f7bb1c79c71eabe7 |
Hashes for pyonmttok-1.21.0-cp27-cp27m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | afeead41fabb730f6930949819593bfb49a9107931748ea0118d29baaa577b44 |
|
MD5 | 92f84ee8f870ab5f16c9fed8c5612624 |
|
BLAKE2b-256 | db519783614e6e29ae53add9b2b6f92734e0748a6116837977c97ddfc64a0ae2 |