Sub topic 3
Where Simple Tokenisers Break
Assume you know basic Python, regular expressions, and common NLP tasks such as search or classification. This chapter focuses on the cases where a whitespace split or a small regular expression gives the wrong tokens.
A token is one unit produced by a tokeniser. A token might be a word, punctuation mark, emoji, URL, or part of a word. Tokenisation is therefore a modelling decision, not just a string operation.
Punctuation Is Not Always Noise
A whitespace split keeps punctuation attached to nearby text:
text = "Wait... really? I paid $12.50."
print(text.split())This produces tokens such as really?, $12.50., and Wait.... Those forms are inconvenient if a classifier or search system needs to compare really with really!.
A regular expression can separate runs of word characters from punctuation:
import re
text = "Wait... really? I paid $12.50."
pattern = r"\w+(?:[.,]\w+)*|[^\w\s]"
tokens = re.findall(pattern, text)
print(tokens)The expression keeps decimal-like text such as $12.50 partly together, but it still reflects a chosen policy. Should $ be attached to the number? Should 12.50 be one token? Should three dots become one ellipsis token or three period tokens?
There is no universally correct answer. A financial system may preserve $12.50 as one meaningful unit. A punctuation-aware language model may prefer $, 12, ., and 50.
Contractions Need a Policy
Contractions contain multiple grammatical parts but often behave like a single everyday word. For example, don't can be represented as:
don'tdoandn'tdoandnot
The best choice depends on the downstream task. Sentiment analysis may benefit from retaining don't, while a grammar system may need to identify the negation separately.
A simple pattern can keep an apostrophe inside a word:
import re
text = "Don't split me naively, please."
pattern = r"\w+(?:'\w+)?|[^\w\s]"
print(text.split())
print(re.findall(pattern, text))The first output leaves please. attached to the word. The second keeps Don't together and emits the comma and period separately. However, this pattern does not understand every apostrophe style. Curly apostrophes, possessives, and quoted text may require additional rules.
Hyphens and Multiword Names
Hyphenated text is ambiguous. In state-of-the-art, the whole expression may be one searchable phrase, or each component may need an individual representation. In well-known, splitting at the hyphen can help match known, but it can also lose the meaning of the compound.
Names and expressions create similar problems:
New Yorkhas two tokens but one place.AT&Tcontains punctuation that belongs to the name.e-mailandemailare spelling variants.COVID-19combines letters, a hyphen, and digits.#nlpand@userinclude markers that may carry meaning.
A production tokeniser should state its treatment of these forms. It should also be tested against representative data rather than judged only on short examples.
Languages Without Spaces
Whitespace tokenisation assumes that spaces separate words. That assumption fails for languages such as Chinese, Japanese, and Thai, where word boundaries are not consistently marked with spaces.
For example, a Chinese sentence can be divided into words in more than one plausible way. A general-purpose regular expression can identify characters, but it cannot reliably decide which characters form a word. Such systems need language-specific dictionaries, statistical segmenters, or neural tokenisers.
Even languages with spaces may need special handling for:
- clitics attached to words
- compound words
- abbreviations
- emojis and repeated symbols
- mixed scripts
- spelling variation
Tokenisation as a Pipeline Decision
The following pipeline shows why tokenisation choices depend on later processing:
A fixed rule may work well for English search but fail for multilingual text. A task-specific tokenizer can preserve URLs or split contractions when those distinctions matter. When uncertainty is high, retain the original text and make tokenisation reversible or auditable.
Test the Boundary Cases
Create a small test set before choosing rules. Include punctuation, contractions, hyphens, numbers, URLs, email addresses, emojis, and text from every supported language. Inspect both individual outputs and task-level results.
Exercise
Why can text.split() be unsuitable for a search system that must distinguish words from punctuation?
Show answer
It leaves punctuation attached to words, so tokens such as word and word, do not match without extra processing.