Concepts / Tokenization Scheme

Tokenization Scheme

A tokenization scheme is a method for splitting text into individual words or tokens.

  • Programming

Why a Space-Only Splitter Breaks

A tokenization scheme is a method for splitting text into individual words or tokens. The simplest version splits on spaces. That works until the first comma appears. Then the word before the comma carries the punctuation with it, and the model sees a different unit than the one you intended. Consider the sentence Is it well-known, really? A space-only splitter returns the word well-known, with its comma still attached, as if the comma were part of the word.

The tokenizer in this article takes a different approach. It does not look for words directly. It looks for the characters that separate words, and it asks the splitting function to keep those characters in the output. That single decision is what turns punctuation into tokens of its own. Commas, periods, question marks, quotation marks, apostrophes, double dashes, and spaces are all treated as separators.

Tracing a Split With a Capturing Group

The pattern is a single string of symbols. A bracketed group lists the characters that count as separators on their own. Because the list includes the period, the comma, the question mark, and the apostrophe, each of these is matched as its own separator rather than fused onto the neighboring word. The pattern also contains two more alternatives: a double dash, written as a two-character sequence so it is matched as a unit, and a single space. Both alternatives are separators.

The key detail is the parentheses around the whole pattern. They form a capturing group. When re.split is given a pattern with a capturing group, it keeps the text that matched the separators in the output list, interleaved with the words. Without the group, the separators are thrown away. Here is the same pattern run both ways, with the parentheses present and then removed.

python
Output (expected)
['Is', ' ', 'it', ' ', 'well-known', ',', '', ' ', 'really', '?', '']
['Is', 'it', 'well-known', '', 'really', '']
Notes on the code
  • The parentheses make the pattern a capturing group, so each separator appears in the output list.
  • The hyphen in well-known is not in the bracketed list, so it does not split the word.
  • An empty string appears between the comma and the space because two separators are adjacent.
  • Without the parentheses, the comma and question mark are gone from the output.

Read the first output from left to right. The words and separators alternate, and the order matches the sentence. The space after Is is the separator between Is and it. The comma sits directly after well-known, and an empty string sits between the comma and the space that follows it. That empty string is a side effect of two adjacent separators, and it has to be removed later. The period and question mark follow the same rule: each one becomes its own item in the list, in its original position.

Input sentenceIs it well-known, really?re.split withcapturing groupRaw list11 items, includingseparators and emptystringsStrip and dropemptiesFinal tokensIs, it, well-known, comma,really, question marktextwords and separatorscleanupkeep non-empty items
Which pieces does re.split produce, and where does each separator land in the list?

Cleaning Up the Raw Split Output

Because the capturing group keeps the separators, the raw list also contains the spaces and the empty strings. Removing whitespace requires a cleanup step that strips each item and drops the ones that are empty. Without this step, spaces and blanks remain in the output and the token list is noisy. Stripping turns a space item into an empty string, and the filter then removes it along with the existing empties.

python
Output (expected)
['Is', 'it', 'well-known', ',', 'really', '?']
Notes on the code
  • p.strip() removes surrounding whitespace, so a lone space becomes an empty string.
  • The if clause drops every item that is empty after stripping.
  • The comma and question mark survive as tokens, because they are real characters, not whitespace.

Whitespace: What Removing It Saves and Costs

The whitespace decision is not a technical requirement. It depends on the application. The simple tokenizer in this section removes whitespace, and the source says it does so for simplicity and brevity of the tokenized output. Removing whitespace reduces memory and computing requirements. Keeping whitespace can matter when a model is sensitive to the exact layout of the text, and the source names Python code as an example. A later scheme in the course will include whitespace, so the current code should be treated as a deliberate simplification rather than a universal rule.

ChoiceWhat it savesWhat it costs
Remove whitespaceLess memory and computing; shorter token listInformation about layout and indentation is no longer in the output
Keep whitespace as tokensLayout and structure remain visible to the modelMore items to store and process

Trade-offs of removing whitespace in a simple tokenizer

Extending the Punctuation Set

The source notes that the scheme works on a simple sample and is then modified to handle other punctuation and special characters. Extending the scheme means adding symbols to the bracketed list. Before you do that, predict what will happen to a character that is not yet in the list. A hyphen is a good test case. It is not in the list, even though the double dash is. A single hyphen therefore stays inside its word, and anything you want treated as a separator must be added explicitly.

What do you think happens?

If you add a single hyphen to the bracketed list, how is well-known tokenized?

Reveal answer
Answer:

It is split into three tokens: well, the hyphen, and known.

The hyphen becomes a member of the bracketed set, so it is matched as a separator and kept by the capturing group. Cleanup then leaves it as its own token between the two halves of the word.

python
Output (expected)
['Is', 'it', 'well', '-', 'known', ',', 'really', '?']
Notes on the code
  • The hyphen has been added to the bracketed list, so it now counts as a separator.
  • The output confirms the prediction: well-known has become three tokens.
Next characterIn bracketed set?Separator tokenkept by capturing groupStays in current wordnot splitcheckyesno
For each character, does the tokenizer find it in the punctuation set? A hyphen is kept as a separator only after it is added to the set.

Mistakes When Building the Splitter

  • Common mistake

    Dropping the parentheses and expecting punctuation to survive

    What it looks like
    re.split(r"[,.?']|--| ", "Is it well-known, really?") returns no comma or question mark.
    Why it fails

    Without a capturing group, re.split discards the text that matched the separators.

    Do this instead

    Wrap the whole alternation in parentheses so the separators are kept in the output.

  • Common mistake

    Forgetting that the hyphen is not in the set

    What it looks like

    Assuming well-known will split because a double dash is handled.

    Why it fails

    The double-dash alternative matches two hyphens together. A single hyphen is not in the bracketed list, so it is not matched.

    Do this instead

    Add the single hyphen explicitly to the bracketed list if you want it treated as a separator.

  • Common mistake

    Skipping the cleanup step

    What it looks like

    The raw list contains spaces and empty strings such as ''.

    Why it fails

    The capturing group keeps separators, including the spaces, and adjacent separators leave empty strings behind.

    Do this instead

    Strip each item and filter out the empty results before using the tokens.

  • Common mistake

    Treating whitespace removal as always correct, even when the model needs layout information

Alternative Schemes and Attention in the References

This part of the module does not teach a new procedure. It points you to original research papers. The section lists three references. Two describe tokenization schemes, and one describes an attention technique. The table below records the facts the source gives for each: the name, the year, the authors, and the kind of contribution. The last column is what separates the tokenizers from the attention method.

ReferenceYearAuthorsType of contribution
Bahdanau attention (Neural Machine Translation by Jointly Learning to Align and Translate)2014Bahdanau, Cho, and BengioAttention technique for RNN-based neural machine translation; not a tokenizer
SentencePiece2018Kudo and RichardsonSimple, language-independent subword tokenizer and detokenizer for neural text processing
Fast WordPiece Tokenization2020Song et al.Fast tokenization method for WordPiece, a tokenization scheme used by some LLMs

The three referenced methods, in publication order

A subword tokenizer does not split only at spaces and punctuation. It breaks words into smaller pieces that can be reused across the vocabulary. SentencePiece is described in the source as a simple, language-independent subword tokenizer and detokenizer for neural text processing, used by some popular LLMs. The phrase language-independent means that its description is not tied to one particular language. The source does not give the internal method, so this article does not describe it further.

WordPiece is also described as a tokenization scheme used by some LLMs. The 2020 paper Fast WordPiece Tokenization by Song et al. focuses on making WordPiece tokenization faster. The source confirms that the work presents a fast tokenization method but does not give its details.

Bahdanau attention is a different kind of thing. It is an attention technique for RNN-based neural machine translation, and it is not a tokenizer. In the tokenization pipeline, the tokenizer decides what the units are, and the attention technique later decides how strongly the model weighs those units against each other. The source lists neural machine translation as a prerequisite, so this paper is best read after you have a working picture of translation models.

Tokenization schemeSplits text intotokensSentencePiece (2018)WordPiece, fastmethod (2020)Attention techniqueWeighs tokens againsteach otherBahdanau attention(2014)producesexampleexampleperformsexample
Which references describe splitting text into tokens, and which describe weighing tokens against each other?

Reading the Original Papers

Before you open any of the three papers, fill in the table of facts for each one: name, year, authors, and type of contribution. Then ask what question you should bring to the paper. For a tokenizer, ask how it decides where to split and how it handles whitespace and punctuation. For the attention paper, ask how it relates the tokens to one another.

MEDIUM

Add an exclamation mark to the bracketed list in the splitter. Predict how the sentence Wow! Really? will be tokenized, then run the code to check your answer. What does the output show about the empty strings in the raw list?

Hints
  • Each character in the bracketed list is a separator on its own.
  • Strip and filter the raw list before you compare the output with your prediction.
  • Empty strings appear wherever two separators sit next to each other.

Key Takeaways

  • A tokenizer built on re.split keeps its separators when the pattern contains a capturing group, which is how punctuation becomes separate tokens.
  • Only characters named in the bracketed list are split out, so a single hyphen stays inside its word unless you add it explicitly.
  • Removing whitespace reduces memory and computing requirements but can discard layout information that matters for some models, such as those working on Python code.
  • The three references are Bahdanau attention (2014, Bahdanau, Cho, and Bengio), SentencePiece (2018, Kudo and Richardson), and Fast WordPiece Tokenization (2020, Song et al.); the first is an attention technique and the other two are tokenizers.

Related concepts

More concepts starting with T