Sub topic 1
Tokens, Types, and Vocabulary
Text processing begins by deciding what counts as a unit. In natural language processing (NLP), that unit is often a token. Token counts describe how much text you have, while type counts describe how varied that text is.
Prerequisites
This chapter assumes basic Python, including strings, lists, functions, and regular expressions. You should also know that NLP systems process text for tasks such as search, classification, or language modelling. Basic grammar terms such as noun, verb, and plural are useful, but detailed linguistics is not required.
Tokens and types
A token is one occurrence of a text unit in a particular piece of text. A token might be a word, number, punctuation mark, emoji, or subword. The definition depends on the tokeniser, which is the component that divides text into tokens.
Consider this sentence:
The dog saw the dog.A word-based tokeniser might produce:
["The", "dog", "saw", "the", "dog", "."]There are six tokens because every occurrence is counted, including repeated words and punctuation.
A type is a distinct token value. In the example, dog occurs twice but contributes only one type. Whether The and the are the same type depends on preprocessing. If the text is converted to lowercase, they become the same type.
The vocabulary is the set of types found in a text or dataset. Its size is often called the vocabulary size. A larger vocabulary usually means that the text contains more distinct forms, although the meaning of “large” depends on the amount and domain of text.
| Measure | Meaning | Example |
|---|---|---|
| Token count | Total number of token occurrences | 6 |
| Type count | Number of distinct token values | 5 |
| Vocabulary | The set of distinct token values | `{the, dog, saw, .}` plus `The` if case-sensitive |
A useful diversity measure is the type-token ratio (TTR):
TTR = number of types / number of tokensFor a short document, TTR can indicate how varied the vocabulary is. However, TTR usually decreases as documents become longer because repeated words add tokens without adding many new types. Compare TTR values only when document lengths are similar, or use a length-controlled measure.
A basic tokenisation example
Python’s split() method separates text on whitespace:
import re
text = "Don't split me naively, please."
print(text.split())
pattern = r"\w+(?:'\w+)?|[^\w\s]"
print(re.findall(pattern, text))The first result keeps the comma and full stop attached to nearby words. The regular expression instead matches either a word, an optional apostrophe followed by more word characters, or one non-word, non-space character.
The regular-expression result treats Don't as one token and punctuation as separate tokens. This makes token counts more useful for many search and classification tasks.
Tokenisation is a modelling choice
There is no single universally correct tokenisation. The best choice depends on the task.
For search, separating punctuation can help match a query consistently. For sentiment analysis, punctuation such as ! may carry useful information. For programming-language text, operators such as == and -> may need special handling. For social media, hashtags, mentions, URLs, and emoji may each deserve their own rules.
Case also changes type counts. The following strings have three case-sensitive types but two types after lowercasing:
["NLP", "nlp", "tools"]Normalisation can reduce vocabulary size, but it can also remove distinctions. For example, US and us may refer to different things. Apply normalisation only when the lost distinction does not matter for the task.
A token and type pipeline
The relationship between raw text, tokens, and vocabulary can be viewed as a pipeline.
Worked calculation
Suppose a tokeniser returns:
["cats", "chase", "mice", "and", "cats", "watch", "mice"]The token count is 7. The vocabulary is:
{"cats", "chase", "mice", "and", "watch"}The type count is 5, so the TTR is 5 / 7, approximately 0.71.
This calculation is simple, but its result depends on earlier choices. If punctuation is included, punctuation contributes tokens and possibly types. If case is preserved, differently capitalised forms remain separate. If words are reduced to lemmas later, cats and cat might be treated as related forms. Tokenisation and normalisation therefore affect every statistic built from the text.
Exercise
A tokeniser produces ["A", "cat", "saw", "a", "cat", "."]. What are the token count, the case-sensitive type count, and the type-token ratio?
Show answer
The token count is 6. The case-sensitive types are A, cat, saw, a, and ., so the type count is 5. The type-token ratio is 5/6, approximately 0.83.