Skip to content
wordpiece.org

Separating CJK pre-tokenization from vocabulary coverage

Probe mixed Chinese and Latin strings under both pinned vocabularies, distinguishing isolated character boundaries from [UNK] coverage

Published 8 min read Verified

Verified: 2026-10-05. This verification covers the cited source artifacts and dependency pin, not a claimed execution of the diagnostic below. The relevant behavior and counts apply only to google-bert/bert-base-uncased@86b5e0934494bd15c9632b12f734a8a67f723594 and google-bert/bert-base-multilingual-cased@3f076fdb1ab68d5b2880cb87a0886f315b8146f8, using transformers==4.56.2.

Separate boundary formation from vocabulary lookup

A CJK character in a mixed string answers two independent questions: did BERT isolate it before WordPiece processing, and does the vocabulary contain a usable representation for that isolated unit? Confusing those questions leads to a common misdiagnosis: [UNK] does not show that neighboring characters were not separated, while a character-sized token does not prove that the character was covered rather than replaced by [UNK].

Google’s BERT preprocessing inserts boundaries around codepoints in configured Han-character ranges before greedy WordPiece matching runs. The fast-tokenizer path also applies the pinned normalizer: the uncased configuration lowercases text, while the multilingual cased configuration preserves case. Consequently, a pre-token’s surface form and its final token need not be identical, even when their source offsets agree. The exact behavior comes from each pinned tokenizer.json; Google’s tokenization.py explains the original isolation and WordPiece algorithms.

After a Han character has been isolated into a one-character candidate, the WordPiece model either finds that character or returns [UNK]. This implementation has no byte-level fallback that would split an uncovered character into smaller representable pieces. That is different from fragmentation: fragmentation is one candidate represented by several known pieces, potentially including ## continuation pieces. A multi-character Latin candidate that becomes [UNK] has failed to find any complete decomposition; it does not mean the same thing as a one-character CJK candidate becoming [UNK].

Vocabulary size is not a CJK-coverage metric: each vocabulary includes entries unrelated to Han text, as well as control and unknown-related entries. The diagnostic therefore checks particular characters rather than inferring coverage from vocabulary size.

Run a structural probe

The following Python 3 program loads both exact revisions with AutoTokenizer.from_pretrained, prints the locally obtained results, and makes no assumption about whether the coverage-probe character 𠮷 is covered. It exposes the normalizer and pre-tokenizer separately, then compares their result with source-offset mappings from content-only and special-token-bearing encodings. Padding and truncation are disabled.

import tokenizers
import transformers
from transformers import AutoTokenizer

MODELS = [
    (
        "google-bert/bert-base-uncased",
        "86b5e0934494bd15c9632b12f734a8a67f723594",
    ),
    (
        "google-bert/bert-base-multilingual-cased",
        "3f076fdb1ab68d5b2880cb87a0886f315b8146f8",
    ),
]
SAMPLES = ("中文BERT", "BERT中文", "𠮷中文BERT")
HAN_PROBES = set("中文𠮷")


def load_pinned(model_id, FULL_COMMIT):
    return AutoTokenizer.from_pretrained(
        model_id, revision=FULL_COMMIT, use_fast=True
    )


def is_probe_han(character):
    return character in HAN_PROBES


def probe(tokenizer, text):
    backend = tokenizer.backend_tokenizer
    normalized = backend.normalizer.normalize_str(text)
    pre_tokens = backend.pre_tokenize_str(normalized)

    isolated_han = [
        piece
        for piece, _ in pre_tokens
        if len(piece) == 1 and is_probe_han(piece)
    ]
    assert len(isolated_han) == sum(is_probe_han(ch) for ch in text)

    content = tokenizer(
        text,
        add_special_tokens=False,
        padding=False,
        truncation=False,
        return_offsets_mapping=True,
    )
    content_ids = content["input_ids"]
    offsets = content["offset_mapping"]
    pieces = tokenizer.convert_ids_to_tokens(content_ids)

    coverage = []
    for index, character in enumerate(text):
        if not is_probe_han(character):
            continue
        matches = [
            piece
            for piece, (start, end) in zip(pieces, offsets)
            if (start, end) == (index, index + 1)
        ]
        assert len(matches) == 1
        assert matches[0] in (character, tokenizer.unk_token)
        assert text[index:index + 1] == character
        state = "uncovered" if matches[0] == tokenizer.unk_token else "covered"
        coverage.append((character, matches[0], state))

    wrapped = tokenizer(
        text,
        add_special_tokens=True,
        padding=False,
        truncation=False,
        return_offsets_mapping=True,
        return_special_tokens_mask=True,
    )
    wrapped_ids = wrapped["input_ids"]
    special_mask = wrapped["special_tokens_mask"]
    content_positions = [
        index for index, is_special in enumerate(special_mask) if not is_special
    ]
    assert [wrapped_ids[index] for index in content_positions] == content_ids
    assert [wrapped["offset_mapping"][index] for index in content_positions] == offsets
    assert [
        token_id
        for token_id, is_special in zip(wrapped_ids, special_mask)
        if is_special
    ] == [tokenizer.cls_token_id, tokenizer.sep_token_id]

    print(f"  normalized: {normalized!r}")
    print(f"  pre_tokens: {pre_tokens}")
    print("  content:", [
        (piece, text[start:end], (start, end))
        for piece, (start, end) in zip(pieces, offsets)
    ])
    print("  CJK coverage:", coverage)
    print("  [UNK] positions:", [
        index for index, piece in enumerate(pieces)
        if piece == tokenizer.unk_token
    ])
    print("  continuation pieces:", [
        (index, piece) for index, piece in enumerate(pieces)
        if piece.startswith("##")
    ])
    print(
        f"  content IDs={len(content_ids)}, "
        f"model-input IDs={len(wrapped_ids)}, "
        f"special IDs={sum(special_mask)}"
    )


assert transformers.__version__ == "4.56.2"
print("transformers:", transformers.__version__)
print("tokenizers:", tokenizers.__version__)

for model_id, full_commit in MODELS:
    tokenizer = load_pinned(model_id, full_commit)
    print(f"\n{model_id}@{full_commit}: {type(tokenizer).__name__}")
    for sample in SAMPLES:
        print(f"\n{sample!r}")
        probe(tokenizer, sample)

Run it in an environment installed with python -m pip install transformers==4.56.2; the first load also needs access to the pinned repositories or a populated cache.

Interpret the observations in order

First, inspect pre_tokens. Its offsets refer to the normalized string, so they need not equal source offsets. The assertion establishes that each tested Han character becomes a one-character pre-token, including the supplementary-plane coverage probe. If a character instead remains inside a longer candidate, the script fails rather than mislabeling a coverage problem as isolation.

Second, inspect CJK coverage, which uses the content encoding’s source offsets. A literal character means the isolated candidate was covered. [UNK] with a one-character source span means isolation succeeded but WordPiece coverage failed. That [UNK] is still a content token, not a model special token. By contrast, multiple pieces whose offsets partition one Latin source span are fragmentation. Their ## markers make continuation pieces visible, while [UNK] positions identify failed candidates.

Third, keep the two lengths separate. content IDs excludes the special tokens added by add_special_tokens=True; model-input IDs includes the pinned BERT [CLS] and [SEP]. special_tokens_mask identifies those additions. A batch padded to another sequence length would require a separate model-input length, and enabling truncation could change both kinds of content locally; this diagnostic deliberately avoids both transformations.

Comparing the two pinned tokenizers is valid for preprocessing and vocabulary behavior on the same strings. It is not a quality, efficiency, or downstream-accuracy comparison. Their normalization and vocabularies differ, so a shorter content encoding in one output would not establish better CJK handling overall.

Limitations and re-check procedure

BERT’s “CJK” boundary policy is specifically a configured list of Han ranges. It does not imply identical isolation for every character that readers might informally classify as CJK, including all kana or Hangul cases. Normalization can also alter surrounding text, although the Han characters in these probes are not case-folded. The offset checks are therefore intentionally narrow.

The three strings cannot establish corpus-wide CJK coverage, rare-character behavior beyond the tested point, or model quality. They are structural probes only. The script also prints the installed tokenizers backend version because the exact transformers==4.56.2 pin alone does not describe every resolved transitive dependency.

The evidence boundary is the pinned uncased tokenizer.json and multilingual cased tokenizer.json. To re-check, resolve both full commits, record the downloaded files’ hashes, and confirm the tokenizer class, vocabulary size, and printed dependency versions. If a file obtained for either full commit differs from the reviewed artifact, stop and inspect its vocabulary, normalizer, unknown token, and pre-tokenizer settings before changing the commit pin. If only a dependency changes, reproduce the output in a clean environment and retain the exact backend lock; do not silently carry conclusions from transformers==4.56.2 to another release.

Sources

Token counts in this article were produced by the tokenizers pinned on the sources page and are checked by the site's test suite. If a count ever stops matching the pinned vocabulary, the build fails.

Check it yourself

Open the tokenizer

Every count in this guide comes from a vocabulary pinned by checksum. Paste the same text into the tool and you will get the same output.

Related guides