Home / Natural Language Processing

Text Tokenization

Break down raw text into meaningful units (tokens) using various segmentation strategies.

Input Corpus

Tokenization Method

Whitespace

Split by spaces only

Word (Regex)

Split words & punctuation

Sentence

Split by delimiters (. ! ?)

Character

Split every character

Token Stream

0 Tokens 0 Unique
Click 'Tokenize' to break the text apart.

Overview

Break down raw text into meaningful units (tokens) using various segmentation strategies.

In this guide
  1. What is Tokenization?
  2. 1. Why is Tokenization So Key?
  3. 2. Exploring Else Tokenization Ways
  4. Cutting text into pieces a net can count
  5. How byte-pair encoding builds a vocabulary
  6. What tokenisation decides for you
  7. 3. Test with the Visualization
  8. Advanced Tokenization: Sub-word Ways
  9. In code
  10. Padding, truncation and attention masks
  11. Four tokenisers on the same sentence, with BPE from scratch
  12. Questions people ask
  13. Recap in one screen

A Beginner's Guide to Text Tokenization

Learn how to break down text into meaningful units for NLP models.

What is Tokenization?

Tokenization is the basic first step in any Natural Language Handling (NLP) pipeline. It's the process of breaking down a stream of raw text into less, meaningful units called tokens. These tokens can be words, characters, or sub-word units. It depends on the chosen plan. For a computer to understand human language, it must first dissect it into these discrete pieces, much like how we learn to read by first finding single words. Three plans dominate. Word-level tokenization splits on whitespace and punctuation, giving familiar units but a vocabulary that can run into the millions. And any word missing from it turns an unknown token. Character-level tokenization uses a tiny vocabulary (the alphabet) but gives quite long sequences that the net must reassemble into meaning. Subword tokenization (BPE, WordPiece, SentencePiece) splits rare words into frequent pieces while keeping common words whole, balancing vocabulary size against sequence length. GPT uses BPE; BERT uses WordPiece. The choice of tokenizer decides the net's vocabulary, its max sequence length, and how it handles unseen words — making it one of the most consequential decisions in an NLP pipeline, still though it receives far less attention than the net design. This page tokenizes a sample sentence with all three plans, checks the coming token counts and vocabulary sizes, and shows why subword ways have turn the default.

1. Why is Tokenization So Key?

Imagine trying to understand a sentence by looking at it as one smooth string of letters. It would be nearly not doable. Tokenization gives the structure that machines need. By turning a sentence like "NLP is fascinating!" into tokens such as ["NLP", "is", "fascinating", "!"], we create a list of things that a net can count, study, and give meaning to. This process is the gateway to almost each NLP task, with sentiment review, machine translation, and text summarization.

2. Exploring Else Tokenization Ways

There's no single "best" way to tokenize text. The right way depends on the task and the language. The interactive visualizer up lets you test with the most common plans. This page covers them:

  • Whitespace Tokenization: This is the simplest way. It splits the text based on empty spaces. While fast and straightforward, it's much too basic. For case, it would treat "New York" as two separate tokens, ["New", "York"], and might fail to separate a word from punctuation, like in "fun!".
  • Word (Regex) Tokenization: A more complex approach that uses regular terms (regex) to define what constitutes a word. This way is much better at handling punctuation and special cases. For instance, it can correctly separate "fun!" into ["fun", "!"] and can be configured to handle hyphenated words or contractions like "don't".
  • Sentence Tokenization: Instead of words, this way splits the text into single sentences. It finds sentence edges with delimiters like periods (.), question marks (?), and exclamation marks (!). This is a key preprocessing step for tasks that need to understand the context of a full sentence, such as document summarization.
  • Character Tokenization: This way breaks the text down to its most granular level: single characters. The sentence "Hello" turns ["H", "e", "l", "l", "o"]. This approach is useful for languages without clear word edges or for tasks like spell-checking and text generation, as it creates a quite small, manageable vocabulary.

Cutting text into pieces a net can count

A net cannot consume a string. It consumes integers, each pointing at a row of an embedding table. Tokenisation is the step that decides what those integers stand for. And the choice shapes all of it downstream.

Three levels are doable:

Level"unhappiness" becomesVocabulary size
Characteru, n, h, a, p, ...~100
Wordunhappiness100,000+
Subwordun, happi, ness30,000–100,000

Character-level needs a tiny vocabulary and never meets an unknown symbol, but sequences turn quite long and the net has to learn spelling before it can learn meaning.

Word-level gives short sequences and readable tokens, and it breaks on a thing unseen. A vocabulary built from training text will not hold "unhappiness" if just "happiness" appeared. So the word turns <UNK> and its meaning is lost fully. Morphologically rich languages make this far worse.

Subword is the compromise all of it modern uses. Common words stay whole; rare words split into meaningful fragments. Nothing is ever unknown, since the vocabulary includes the single characters as a fallback.

How byte-pair encoding builds a vocabulary

BPE is the algorithm behind GPT's tokeniser, and it is simpler than its reputation.

  1. Start with each character as a token.
  2. Count all next pairs in the training corpus.
  3. Merge the most frequent pair into a new token.
  4. Repeat until the vocabulary reaches the target size.

Run it on English text and the early merges are t+h → th. Then th+e → the. Frequent sequences turn single tokens; rare ones stay as fragments. The yield is a vocabulary that spends its budget where the text really is.

The yield worth knowing: token count is not word count. English averages roughly 1.3 tokens per word. Code, unusual names, else scripts and emoji use far more. A Japanese sentence may cost three or four times as many tokens as its English translation. This is a real cost and latency gap when calling an API.

WordPiece (BERT) and SentencePiece (T5, Llama) are variants of the same idea. SentencePiece is notable for treating the input as a raw byte stream with no pre-tokenisation step. This means it needs no language-set whitespace rules.

What tokenisation decides for you

A few net behaviours trace right back to this step.

Why nets are bad at spelling and character counting. "strawberry" may be three tokens, none of which is a letter. Asking how many r's it holds is asking the net to reason about a thing it cannot see.

Why counts are handled inconsistently. "1234" might be one token or four. It depends on the tokeniser. So arithmetic is learned over an inconsistent form.

Why context limits are in tokens. A 128,000-token window is roughly 96,000 English words. And much fewer if the text is code or non-Latin script.

Why the tokeniser must ship with the net. Token id 1,547 means one thing to one tokeniser and a thing else to one more. A mismatch gives fluent nonsense, not an error.

3. Test with the Visualization

The best way to grasp these concepts is to see them in action. Use the interactive tokenizer to build your intuition:

  • Check Whitespace vs. Word Tokenization: Use the default sentence. First, run the "Whitespace" tokenizer and observe the output. Notice how "U.S.A." and "$50." are treated as single tokens. Now, switch to "Word (Regex)" and run it again. See how it intelligently separates the punctuation. This shows what rule-based tokenization.
  • Study the Token and Vocab Counts: Type a sentence with repeated words, like "The quick brown fox jumps over the lazy dog." Notice that the "Token Count" is 9. But the "Unique" (vocabulary) count is 8, since "the" appears twice. This distinction is critical for building a net's vocabulary.
  • Test Edge Cases: Try tokenizing else kinds of text. Use a URL (like https://vizlearn.com), an email address (hello@case.com), or a sentence with hashtags (#NLP). How does each tokenizer handle these? Experimenting with these edge cases will show you the challenges and nuances of tokenization in real-world scenarios.

Advanced Tokenization: Sub-word Ways

Modern NLP nets like BERT and GPT use more advanced ways called sub-word tokenization (e.g., Byte-Pair Encoding or WordPiece). These ways break down rare words into less, known sub-word units. For case, "tokenization" might turn ["token", "##ization"]. This lets the net to handle any word it hits, still if it wasn't in the training data. This stops the "unknown token" problem and maintaining a manageable vocabulary size.

In code

from transformers import AutoTokenizer

tok = AutoTokenizer.from_pretrained("bert-base-uncased")

tok.tokenize("Tokenisation isn't trivial.")
# ['token', '##isation', 'isn', "'", 't', 'trivial', '.']

ids = tok("Tokenisation isn't trivial.", return_tensors="pt")
ids["input_ids"]          # includes [CLS] and [SEP] automatically
tok.decode(ids["input_ids"][0])

The ## prefix marks a follow-up of the last word, which is how WordPiece distinguishes "token" at the start of a word from the same letters inside one.

Special tokens matter and are easy to forget: [CLS] and [SEP] for BERT, <s> and </s> for rest, plus [PAD] for batching and [MASK] for masked-language training. With the tokeniser's __call__ adds them correctly. Calling tokenize() alone does not.

For classical NLP the simpler tools still apply:

import re
re.findall(r"\b\w+\b", text.lower())     # crude word tokens

Splitting on whitespace looks adequate and is not: "don't", "state-of-the-art", "U.S.A." and "€19.99" all need decisions. NLTK's and spaCy's tokenisers encode those decisions. A regular term encodes your assumptions.

Padding, truncation and attention masks

Nets take fixed-size batches, so sequences of else lengths must be equalised.

Padding appends a filler token to short sequences. The attention mask is what tells the net to ignore those spots. Without it, the net attends to padding as if it were content. And the yields degrade for no visible reason.

Truncation cuts sequences that exceed the limit. Which end you cut matters: for classification the start of a document is much most informative, for question answering the apt passage may be anywhere, and blind truncation silently discards the answer.

batch = tok(texts, padding=True, truncation=True, max_length=512, return_tensors="pt")
model(**batch)          # attention_mask is included in batch

Padding to the longest sequence in the batch rather than to a fixed max — dynamic padding — is meaningfully faster when lengths vary.

Four tokenisers on the same sentence, with BPE from scratch

Character, word and subword tokenisation each trade vocabulary size against sequence length. Byte-pair encoding is trained here on a small corpus so you can watch the merges being learned.

example_01.pyNumPy
Output

Building on the same setup, the next block shows the left cases.

example_02.pyNumPy
Output

The down block covers the left steps.

example_03.pyNumPy
Output

The next block goes on with the left demonstrations.

example_04.pyNumPy
Output

Building on the same setup, the next block shows the left cases.

example_05.pyNumPy
Output

The down block covers the left steps.

example_06.pyNumPy
Output

Questions people ask

How many tokens is a word? About 1.3 for English prose, more for code, names and non-Latin scripts. Count with the actual tokeniser rather than estimating.

Should I lowercase before tokenising? Just if the tokeniser is uncased. Modern subword tokenisers much handle case them, and lowercasing first destroys data they were trained to use.

Can I use a else tokeniser than the net was trained with? No. The ids would mean the wrong things.

Why does my net see <UNK> all over? A word-level vocabulary built from too little text. Use subword tokenisation.

Do I still need stemming and stop-word cutting? For classical bag-of-words nets, sometimes. For transformers, no — they use the full form and the task words carry syntax.

What is the vocabulary size trade-off? More means shorter sequences and a bigger embedding table. Less means longer sequences and more fragmenting. 30,000–100,000 is the usual range.

Recap in one screen

  • Tokenisation turns text into integer ids. The choice of unit shapes all of it past it.
  • Character level is reliable and long. Word level is short and breaks on unseen words; subword is the standard compromise.
  • BPE repeatedly merges the most frequent next pair, so common words stay whole and rare ones fragment.
  • Token count is not word count, and it varies by language and content type.
  • Pad with an attention mask, truncate on purpose, and always ship the tokeniser with the net.

The key is representation quality: better representation usually improves downstream performance.

Recall check

0 of 3

Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.

  1. What does this module say about “What is Tokenization”?

  2. What does this module say about “Why is Tokenization So Key”?

  3. What does this module say about “Exploring Else Tokenization Ways”?

Cheat sheet

NLP Tokenizer

Split text into words, characters or sentences and see how the tokenizer you choose changes what a model actually reads.

NLP · vizlearn.in/natural_language_processing/tokenization.html

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.