Text tokens and normalization
What a token represents
Natural language processing (NLP) turns text into representations that software can analyze, retrieve or generate. A token is a unit chosen by a tokenizer: a word, punctuation mark, subword, character or byte-level unit. It is not necessarily a word or a single Unicode code point. For a tiny catalog search, splitting on whitespace is a useful baseline: red bike red becomes three tokens but only two distinct token types. The same phrase could be represented differently by a model tokenizer. The vocabulary is the set of token types with an assigned coordinate or ID, not a dictionary of meanings.
Normalize deliberately
Unicode permits some equivalent strings to use different code-point sequences, such as an accented letter stored as a single code point or as a base letter plus a combining mark. NFC canonical normalization makes such representations consistent. Python casefold() supports caseless matching and can change length; it is not a universal rule for every language. Here we casefold, apply NFC, and split on whitespace. This intentionally retains punctuation attached to words. The original text must be kept for display, auditing and span offsets.
import unicodedata
def tokens(text): text = unicodedata.normalize("NFC", text.casefold()) return text.split()
print(tokens("Red bike RED"))print(tokens("bike, bike!"))print(tokens("Cafe\u0301") == tokens("Caf\u00e9"))print(tokens(" "))Read the output
The first result has three tokens. The second has two distinct tokens, because the comma and exclamation mark stay attached. The equality check is True for the two canonically equivalent spellings of the accented word, and whitespace alone yields no tokens. This pipeline is a small search policy, not a full multilingual tokenizer or a drop-in implementation of a model tokenizer. Splitting on spaces does not separate every language correctly.
Language and task boundaries
For Persian text, Arabic Yeh (U+064A) and Persian Yeh (U+06CC), and Arabic Kaf (U+0643) and Persian Keheh (U+06A9), are distinct code points; NFC does not merge them. A search system may map variants using an explicit, tested policy, but preserve the original and check whether the distinction matters. The zero-width non-joiner (U+200C), sometimes called a half-space, can carry meaningful orthographic information; stripping it indiscriminately changes text. Bidirectional rendering and logical storage order are separate concerns. This English lesson uses code-point names rather than localized prose. For code identifiers, product IDs, negation and decimal values, deleting punctuation or folding case may damage meaning. NFKC performs compatibility normalization and can collapse distinctions beyond NFC; choose only when justified.
Adjacent tokens and n-grams
A unigram is one token; a bigram is two consecutive tokens; an n-gram contains n consecutive tokens. Sliding a window of width n over m tokens yields max(0, m-n+1) windows for n >= 1. Repeated windows count separately. A word bigram can distinguish not working from two independent words, but cannot preserve unlimited context. The following implementation accepts a list of already-tokenized strings and a positive integer n.
def ngrams(words, n): if n < 1: raise ValueError("n must be positive") return [tuple(words[i:i+n]) for i in range(len(words)-n+1)]
words = "red bike red".split()print(ngrams(words, 2))print(ngrams(words, 4))print(ngrams("bike red red".split(), 2))Interpret and practice
Reordering the same words changes the bigrams. A window longer than the sequence returns an empty list; zero is rejected. Word n-grams differ from character n-grams and from arbitrary combinations. Before choosing a tokenizer, inspect sample outputs and record its normalization, punctuation, whitespace and language policies. Practice: make a frequency table for repeated bigrams, and explain whether stripping punctuation is appropriate for a catalog identifier such as A-12. A model-specific tokenizer, including its special tokens and vocabulary revision, must match the actual model; this lesson does not implement BPE or WordPiece. Review vocabulary-based features next.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Text tokens and normalization quiz
Apply the representation and evaluation concepts.
Which statement best describes a token in NLP?
Under the lesson policy, what is the result for "BIKE, bike"?
What does NFC normalization do to canonically equivalent composed and decomposed accented text?
How many adjacent bigram occurrences, counting repeats, are in ["red", "bike", "red", "bike"]?
Why retain the original text when creating normalized search features?