Skip to content
wordpiece.org

Measuring WordPiece splits in URLs and query strings

Probe punctuation-heavy URLs with safe example domains and show how to compute counts programmatically

Published 8 min read Verified

To measure WordPiece splits in a URL, count the tokens the pinned tokenizer emits for that exact string, not the number of path segments, query parameters, or Unicode characters. A URL such as https://example.com/search?q=bert%2Btokenizer&lang=en#results first passes through BERT’s basic tokenization rules, which isolate punctuation and apply normalization, and then each remaining piece goes through WordPiece. That second pass can fragment one piece into several tokens, marking continuation pieces with ##. A different outcome is [UNK]: the piece could not be decomposed into vocabulary entries at all. The diagnostic below separates those cases for google-bert/bert-base-uncased at revision 86b5e0934494bd15c9632b12f734a8a67f723594 with Transformers 4.56.2.

What the counts mean

For a single URL, content tokens are the tokens produced with add_special_tokens=False. These are the tokens that represent the URL text after the tokenizer’s normalization and splitting rules. Model-input tokens are the tokens the model would receive, normally [CLS] plus the content tokens plus [SEP] for BERT, before any padding or truncation. If you enable padding or truncation, the model-input count changes, but the content-token count does not. Unknown tokens are token IDs equal to [UNK]; they replace a piece that cannot be matched in the vocabulary. Fragmentation is different: it is a sequence of tokens where later pieces start with ##. Fragmentation increases sequence length but preserves the characters modulo normalization. Unknown tokens lose information.

In an uncased model, lowercasing happens before WordPiece, so case is not preserved in the token stream. In the multilingual cased pin, case is preserved, but the vocabulary and the resulting splits can differ. The pinned uncased file is a 30,522-entry WordPiece vocabulary; the pinned multilingual cased file is 119,547. Those sizes define the lookup space, not the split for a particular URL.

A runnable probe

Use reserved example domains so the test is safe to share. The script prints per-URL content tokens, model-input tokens, unknown tokens, continuation pieces, and offsets when the fast tokenizer provides them.

import transformers
from transformers import AutoTokenizer

assert transformers.__version__ == "4.56.2", transformers.__version__

MODEL = "google-bert/bert-base-uncased"
REVISION = "86b5e0934494bd15c9632b12f734a8a67f723594"

tokenizer = AutoTokenizer.from_pretrained(
    MODEL,
    revision=REVISION,
    use_fast=True,
)

urls = [
    "https://example.com/search?q=bert%2Btokenizer&lang=en#results",
    "https://example.org/a/b?x=1&y=2",
    "https://example.net/path/to/page?query=wordpiece;split,test",
]

print("version:", transformers.__version__)
print("model:", tokenizer.name_or_path)
print("revision:", REVISION)
print("commit_hash:", tokenizer.init_kwargs.get("_commit_hash"))
print("is_fast:", tokenizer.is_fast)
print("vocab_size:", tokenizer.vocab_size)
print("specials:", tokenizer.special_tokens_map)

content = tokenizer(
    urls,
    add_special_tokens=False,
    return_offsets_mapping=tokenizer.is_fast,
)
model_input = tokenizer(
    urls,
    add_special_tokens=True,
    return_offsets_mapping=tokenizer.is_fast,
)

for i, url in enumerate(urls):
    content_ids = content["input_ids"][i]
    model_ids = model_input["input_ids"][i]
    content_tokens = tokenizer.convert_ids_to_tokens(content_ids)
    model_tokens = tokenizer.convert_ids_to_tokens(model_ids)
    offsets = content["offset_mapping"][i] if tokenizer.is_fast else None
    unknown_tokens = [t for t in content_tokens if t == tokenizer.unk_token]
    continuation_pieces = [t for t in content_tokens if t.startswith("##")]

    print("\nURL:", url)
    print("content_token_count:", len(content_ids))
    print("model_input_token_count:", len(model_ids))
    print("content_tokens:", content_tokens)
    print("model_input_tokens:", model_tokens)
    print("unknown_content_tokens:", unknown_tokens)
    print("continuation_piece_count:", len(continuation_pieces))
    if offsets is not None:
        print("content_offsets:", offsets)

The printed values are local observations for the pinned revision and your installed Transformers version. Do not copy counts from this article; none are given, because the exact tokens depend on the artifact you load.

Reading the output

Inspect each URL’s content_tokens, model_input_tokens, unknown_content_tokens, and continuation_piece_count. If unknown_content_tokens is empty, the URL’s content survived the vocabulary lookup. If it contains [UNK], some part of the URL cannot be represented and the model sees a placeholder. If continuation_piece_count is greater than zero, WordPiece is fragmenting content, not failing. Punctuation-heavy query strings often produce many short tokens because ?, =, &, %, #, /, :, ;, and , are split away from surrounding text. Percent-encoded values may be split across %, digits, and letters, and uncased lowercasing may alter the apparent case of hex digits. That is a tokenization observation, not a semantic judgment about the query.

Offsets help trace which original character spans became which content tokens. Use them to see where punctuation or percent-encoded fragments were separated. Offsets are a debugging aid; verify alignment for your exact revision and dependency. If an offset looks inconsistent, decode the token IDs and compare with the original URL.

Limitations

These measurements are scoped to the exact model, revision, and Transformers version. They do not generalize to another BERT checkpoint, a different tokenizer, or any unrelated LLM. A token count from a local run is not a universal property of URLs. Tokenization is also not semantic: two visually similar URLs can have different splits, and the same URL can change if the pinned tokenizer.json or library behavior changes. Normalization, lowercasing, and punctuation rules belong to the tokenizer, not to HTTP. Padding and truncation further change model-input length; if you measure those, report the exact options. Unknown tokens are not fragmentation: [UNK] loses information, while ## pieces retain it as separate model inputs.

Verified and re-check

Verified: 2026-10-05 against google-bert/bert-base-uncased@86b5e0934494bd15c9632b12f734a8a67f723594 and Transformers 4.56.2. The parallel multilingual scope, if used, is google-bert/bert-base-multilingual-cased@3f076fdb1ab68d5b2880cb87a0886f315b8146f8. When the pinned revision changes, or when you upgrade Transformers, re-run the probe with the new full commit and compare the printed tokens, unknown tokens, and counts. Treat previous counts as stale. Check the resolved revision in the loaded tokenizer, for example by printing tokenizer.init_kwargs.get("_commit_hash") if present, and keep the full revision string in your script rather than relying on a floating main. If a dependency update changes fast-tokenizer alignment or special-token handling, re-check offsets and model-input counts as well. That keeps the measurement tied to a reproducible artifact instead of to a moving tokenizer.

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