Views
No views yet

##-continuation pieces, HF tokenizers
library (not SentencePiece's WordPiece mode).sentencepiece/
unigram_{1000,5000,10000,20000,30000}/
unigram_{N}.model, unigram_{N}.vocab # raw -- needed for Subword Regularization
tokenizer.json # converted -- for AutoTokenizer (deterministic only)
tokenizer_config.json, special_tokens_map.json
wordpiece/
wordpiece_{1000,5000,10000,20000,30000}/
tokenizer.json # self-contained (decoder embedded)
tokenizer_config.json, special_tokens_map.json
bpe/
bpe_{1000,5000,10000,20000,30000}/
tokenizer.json, vocab.json, merges.txt # self-contained (byte-level pre-tokenizer + decoder)
tokenizer_config.json, special_tokens_map.jsonAutoTokenizer.from_pretrained(...) (point
it at any subfolder above), for deterministic encode/decode:1from transformers import AutoTokenizer
2
3tok = AutoTokenizer.from_pretrained("bpe/bpe_20000")
4# or "wordpiece/wordpiece_20000", or "sentencepiece/unigram_20000"
5
6ids = tok("راني عارف bezzaf")["input_ids"]
7tok.decode(ids, skip_special_tokens=True)AutoTokenizer still has the newline caveat below (nothing
about the HF wrapper changes that). SentencePiece Unigram via
AutoTokenizer is the plain deterministic model — for Unigram + Subword
Regularization (sampled, non-deterministic), enable_sampling=True only
exists in the sentencepiece library's own API, not in the Rust
tokenizers/AutoTokenizer port, so that variant still needs the raw
.model file:1import sentencepiece as spm
2sp = spm.SentencePieceProcessor(model_file="sentencepiece/unigram_20000/unigram_20000.model")
3ids = sp.encode("راني عارف bezzaf", enable_sampling=True, alpha=0.1, nbest_size=-1)
4sp.decode(ids).model/.vocab files also work for plain deterministic Unigram
without enable_sampling, if you'd rather use the sentencepiece API
directly than AutoTokenizer.WhitespaceSplit) treats \n as ordinary
whitespace, indistinguishable from a space once encoded — so
tok.decode(tok.encode(text).ids) on text containing a real line break
will come back with the newline collapsed to a single space. This is a
known limitation of BERT-style WordPiece tokenization generally (the
byte-level tokenizers above don't have it). If you need multi-line text to
round-trip exactly, wrap the raw tokenizer with this substitution (the
same one this project's own evaluation pipeline uses internally,
_load_wordpiece() in tokenizer_utils.py):1import re
2from tokenizers import Tokenizer
3
4tok = Tokenizer.from_file("wordpiece/wordpiece_20000/tokenizer.json")
5_NEWLINE_RE = re.compile(r"\s?\[NEWLINE\]\s?")
6
7def encode(text):
8 return tok.encode(text.replace("\n", " [NEWLINE] ")).ids
9
10def decode(ids):
11 decoded = tok.decode(ids, skip_special_tokens=False)
12 return _NEWLINE_RE.sub("\n", decoded)[NEWLINE] is already a registered special token in every WordPiece
vocab here, so this works with any of the 5 sizes as-is. Even with this
wrapper, one narrow edge case remains unfixed: a real space immediately
adjacent to a newline (e.g. a trailing space before a line break) can
still lose that one extra space on round-trip — full losslessness there
would require a byte-level scheme, which would make WordPiece redundant
with the BPE tokenizer above.decode(encode(text)) != text count out of 1,926.| Tokenizer | Vocab | CF | Fertility | Round-trip mismatches |
|---|---|---|---|---|
| Unigram | 1,000 | 0.5546 | 2.7974 | 0 |
| Unigram + SR | 1,000 | 0.7349 | 3.7816 | 0 |
| WordPiece | 1,000 | 0.8105 | 4.2118 | 78 |
| BPE | 1,000 | 0.5258 | 2.6723 | 0 |
| Unigram | 5,000 | 0.3448 | 1.8159 | 0 |
| Unigram + SR | 5,000 | 0.5966 | 3.1201 | 0 |
| WordPiece | 5,000 | 0.8105 | 4.2118 | 78 |
| BPE | 5,000 | 0.4049 | 2.1276 | 0 |
| Unigram | 10,000 | 0.3048 | 1.6086 | 0 |
| Unigram + SR | 10,000 | 0.5664 | 2.9607 | 0 |
| WordPiece | 10,000 | 0.3536 | 1.8078 | 78 |
| BPE | 10,000 | 0.3687 | 1.9562 | 0 |
| Unigram | 20,000 | 0.2744 | 1.4471 | 0 |
| Unigram + SR | 20,000 | 0.5390 | 2.8310 | 0 |
| WordPiece | 20,000 | 0.2833 | 1.4535 | 78 |
| BPE | 20,000 | 0.3385 | 1.8005 | 0 |
| Unigram | 30,000 | 0.2603 | 1.3715 | 0 |
| Unigram + SR | 30,000 | 0.5292 | 2.7575 | 0 |
| WordPiece | 30,000 | 0.2629 | 1.3455 | 78 |
| BPE | 30,000 | 0.3217 | 1.7240 | 0 |
1Kharroubi Nasrellah.
2DarijaDz Tokenizers: Algerian Darija Subword Tokenizers.
32026.