A Lightweight Word Piece Tokenizer
This library is an implementation of a modified version of Huggingface's Bert Tokenizer in pure python.
Table of Contents
Usage
Installing
Install and update using pip
pip install word-piece-tokenizer
Example
from word_piece_tokenizer import WordPieceTokenizer
tokenizer = WordPieceTokenizer()
ids = tokenizer.tokenize('reading a storybook!')
# [101, 3752, 1037, 2466, 8654, 999, 102]
tokens = tokenizer.convert_ids_to_tokens(ids)
# ['[CLS]', 'reading', 'a', 'story', '##book', '!', '[SEP]']
tokenizer.convert_tokens_to_string(tokens)
# '[CLS] reading a storybook ! [SEP]'
Running Tests
Test the tokenizer against hugging's face implementation:
pip install transformers
python tests/tokenizer_test.py
Making It Lightweight
To make the tokenizer more lightweight and versatile for usage such as embedded systems and browsers, the tokenizer has been stripped of optional and unused features.
Optional Features
The following features has been enabled by default instead of being configurable:
| Category | Feature |
|---|---|
| Tokenizer | - The tokenizer utilises the pre-trained bert-based-uncased vocab list. - Basic tokenization is performed before word piece tokenization |
| Text Cleaning | - Chinese characters are padded with whitespace - Characters are converted to lowercase - Input string is stripped of accent |
Unused Features
The following features has been removed from the tokenizer:
pad_token,mask_token, and special tokens- Ability to add new tokens to the tokenizer
- Ability to never split certain strings (
never_split) - Unused functions such as
build_inputs_with_special_tokens,get_special_tokens_mask,get_vocab,save_vocabulary, and more...
Matching Algorithm
The tokenizer's longest substring token matching algorithm is implemented using a trie instead of greedy longest-match-first
The Trie
The original Trie class has been modified to adapt to the modified longest substring token matching algorithm.
Instead of a split function that seperates the input string into substrings, the new trie implements a getLongestMatchToken function that returns the token value (int) of the longest substring match, and the remaining unmatched substring (str)
Metadata
Release files for word-piece-tokenizer 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| word-piece-tokenizer-1.0.1.tar.gz | 120.3 kB | Without provenance | Details for word-piece-tokenizer-1.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| word_piece_tokenizer-1.0.1-py3-none-any.whl | Python 3 | none | any | Details for word_piece_tokenizer-1.0.1-py3-none-any.whl |
Total release size: 240.2 kB
Release files / word-piece-tokenizer-1.0.1.tar.gz
| Download URL | word-piece-tokenizer-1.0.1.tar.gz |
|---|---|
| Size | 120.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
46348d0a75fb4df364fa20c1dcf050b813debe7dae9e2971cadd05e3c8c0b4ba
|
|
BLAKE2b-256 checksum How to use checksums |
c1e57a38a1cc6fe9d729c0ccbb92273bc6b0ffb8e1d4d9c76c3ee3b522b8fc8b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.7
|
Release files / word_piece_tokenizer-1.0.1-py3-none-any.whl
| Download URL | word_piece_tokenizer-1.0.1-py3-none-any.whl |
|---|---|
| Size | 119.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
655b8e918cebddfd1a13d11d77ca2c670939443e3dc9c4802d4472cc3eb6322c
|
|
BLAKE2b-256 checksum How to use checksums |
48f74bd06cb1294f0f6b133e415a1226c4e4f2a8ae01f1da869315f82674eed8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.10.7
|
Security for word-piece-tokenizer 1.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.