Making Text Machine-Readable: Cleaning, Tokenisation, and the Corpus
TL;DR: You will be able to clean messy real-world text, tokenise it three ways, and decide when to remove stop words, stem, lemmatise, or keep the original form.
This article is one Wisdom Nugget from What Is Natural Language Processing? by Trevor Stephenson.
If you have ever copied text from a website into a document, you know it rarely comes in clean. There are extra spaces, strange line breaks, maybe some HTML leftovers like ‘&’ instead of ‘&’. There are capital letters used inconsistently, emojis, URLs, hashtags, and the occasional typo. To a computer, that mess is not mildly annoying. It is fundamentally confusing, because a computer starts by treating every distinct character sequence as different unless we tell it otherwise.
By the end of this Nugget you will be able to audit messy text for case, punctuation, typos, emojis, and noise, tokenise a sentence as words, subwords, and multi-word units, explain when removing stop words hurts, compare stemming and lemmatisation with the book’s examples, and judge whether a corpus is representative.
The core idea
Raw text in the wild is messy because human communication is messy. We are creative, informal, multilingual, and we make mistakes. NLP has to embrace that messiness and make it workable. Every impressive demo you see, translation, summarisation, chatbots, rests on careful decisions made at this early stage. Getting the foundation right makes everything downstream easier and more accurate. Tokenisation is not just a mechanical step. It is a design decision that shapes everything downstream. Good tokenisation preserves meaning where it matters and splits where splitting helps. Bad tokenisation destroys meaning or creates unnecessary complexity. When you evaluate an NLP project, one of the best questions you can ask is: how did you handle messy text? What did you do with capitalisation, punctuation, typos, and emojis? The answer tells you a lot about whether the system was built for clean textbook examples or for the real world where you live and work.
How it works
Section 1 (Figure 5.1) covers why raw text is messy: case, punctuation, typos, HTML noise, emojis, spacing. Section 2 (Figure 5.2) covers tokenisation: word tokens, subword tokens, multi-word tokens, BPE and SentencePiece, the LEGO analogy, the recipe analogy (breaking ‘2 cups flour, 1 tsp salt, 3 eggs’ into ingredients and amounts). Section 3 (Figure 5.3) covers stop words, stemming versus lemmatisation, and corpora. Stemming chops to ‘runn’ and ‘happi’; lemmatisation returns the proper base ‘run’ and ‘good’ for ‘better’. A corpus must be representative, diverse, and large enough, and corpus choices, what sources to include, how to clean them, how to tokenise them, whether to remove stop words or lemmatise, and how to handle privacy and bias, should reflect the nature of the corpus.
The tools
Tool 1: Clean messy text thoughtfully. Capitalisation: to you, ‘Apple’ at the start of a sentence and ‘apple’ in the middle are the same word; to a naive system that has not been taught to normalise case, they are different. Punctuation: ‘Hello, world!’ includes a comma and an exclamation mark; in sentiment analysis, ‘Hello!!!’ conveys stronger positivity than ‘Hello’; in a search query you probably want to ignore punctuation; in ‘L’Artusi’ or ‘Wendy’s’ the apostrophe is part of the name and should be preserved; in ‘don’t’ the apostrophe matters for negation. Typos and variation: ‘color’ and ‘colour’, ‘organize’ and ‘organise’, ‘teh’ for ‘the’, ‘ur’ for ‘your’, ‘cooool’ with extra letters, ‘2moro’ for ‘tomorrow’. Noise that is not language: HTML tags like ‘<div>’ and ‘<p>’, URLs, email addresses, ‘@username’, hashtags like ‘#NLP’; hashtags carry topic information and some noise is valuable. Emojis: ‘I loved the movie’ with a heart-eyes emoji contains sentiment in the emoji; for sentiment analysis, absolutely do not remove it. Spaces: ‘New York’ is one location or two words? ‘Ice cream’? Languages like Chinese or Japanese do not use spaces between words at all. The definition of a word becomes a design decision.
Step by step
The end-of-chapter exercise as a numbered protocol, in order.
- Take messy text: OMG LOVE this!!! with heart-eyes emoji, #NLP, 2moro, cooool, teh, link https://t.co/xyz, and decide case handling, punctuation keep/remove, typo normalisation, emoji handling, and justify for a sentiment task.
- Tokenise the sentence I can’t believe it’s 7pm at L’Artusi! three different ways and discuss pros and cons.
- Explain when removing stop words hurts: analyse This is not good and to be or not to be.
- Compare stemming vs lemmatisation for running, runs, ran, better, happiness, saying which returns real words and when you would use each. Bonus: what bias risk if a corpus overrepresents American English vs Nigerian English? No time is stated in the book for this exercise.
Worked example
The book’s worked example is the messy social post: ‘OMG LOVE this!!! 😍 #NLP 2moro cooool teh link https://t.co/xyz’. The chapter’s cleaning decisions: lowercasing except brand name, preserving emojis for sentiment, keeping hashtags as topic tokens, handling ‘2moro’ to ‘tomorrow’ via informal mapping, normalising ‘color’ to ‘colour’ for search but keeping the original for display. Tokenisation: subword tokenisation handles ‘cooool’ as ‘cool’ with emphasis, keeps ‘@spectrumthreadz’ as a single mention token, keeps ‘#SpectrumThreadz’ as a topic token. Stop words: they keep ‘not’ for sentiment because ‘not good’ flips meaning. Stemming vs lemmatisation: they choose lemmatisation to map ‘loving’, ‘loved’, ‘love’ to ‘love’ without collapsing to the non-word ‘happi’.
A real example from the book
Case Study – Chapter 5: CleanTweets: Making Brand Monitoring Machine-Readable. No person age is given. Spectrum Threadz, a London streetwear brand, monitors Instagram mentions. Raw data is messy: OMG LOVE this drop!!! with two heart-eyes emojis, #SpectrumThreadz, @spectrumthreadz, cooool, 2moro, color vs colour, teh drop, link https://t.co/xyz, <div> spam. Early system treated LOVE, love, and looooove as different words, and the heart-eyes emoji was stripped, losing sentiment. Using Chapter 5 principles, the team redesigns cleaning. Normalisation: lowercasing except brand name, preserving emojis for sentiment, keeping hashtags as topic tokens, handling 2moro to tomorrow via informal mapping, normalising color to colour for search but keeping the original for display. Tokenisation: subword tokenisation handles cooool as cool with emphasis, keeps @spectrumthreadz as a single mention token, keeps #SpectrumThreadz as a topic token, keeps 7pm and L’Artusi style names.
Mistakes to avoid
- Treating every distinct character sequence as different. Correction: normalise case and variation thoughtfully.
- Stripping emojis in sentiment work. Correction: the emoji is gold for sentiment, keep it.
- Removing ‘not’, ‘no’, ‘never’ as stop words in sentiment analysis. Correction: ‘This is not good’ becomes ‘This is good’, the opposite meaning.
- Using stemming when you need real words. Correction: stemming gives ‘happi’, lemmatisation gives the proper base form.
- Tokenising ‘L’Artusi’ into pieces or splitting ‘7pm’. Correction: keep meaningful units together.
- Building a corpus from one dialect and assuming it represents everyone. Correction: ask whose language is included and whose is missing.
- Treating tokenisation as purely mechanical. Correction: it is a design decision that shapes everything downstream.
What not to do
- Do NOT strip emojis when doing sentiment analysis.
- Do NOT remove ‘not’, ‘no’, or ‘never’ without checking what it does to meaning.
- Do NOT use stemming when you need a real word as the base form.
- Do NOT split proper names or times into pieces.
- Do NOT build a corpus from one dialect and call it representative.
- Do NOT treat tokenisation as a mechanical step with one right answer.
Low-energy mode
Take one messy sentence you typed today and decide just two things: what to keep and what to normalise. That counts.
If you freelance
This is one way to apply the idea, not a passage from the book. A freelancer can run the messy text audit on their own client messages before choosing a tool: if the messages are full of slang, emojis, and typos, a system that strips them will misread sentiment, so the audit becomes part of the client brief.
How to know it worked
You decided case handling, punctuation, typo normalisation, and emoji handling for the messy text, with a justification for a sentiment task. You tokenised I can’t believe it’s 7pm at L’Artusi! three ways with pros and cons. You explained why removing stop words hurts for This is not good and to be or not to be. You compared stemming and lemmatisation for running, runs, ran, better, happiness.
Key takeaways
- To a computer, that mess is not mildly annoying. It is fundamentally confusing.
- Cleaning is the first act of respect we show to the data.
- Tokenisation, then, is not just a mechanical step. It is a design decision that shapes everything downstream.
- Subword tokenisation is like having a box of LEGO bricks – small, reusable pieces you can combine to build any model, even one you have never built before.
Try it today
Do Exercise – Chapter 5: Making Text Machine-Readable today (no time stated in the book, see Exercise).
End of Chapter Exercise – Chapter 5: Making Text Machine-Readable. No time stated in the book.
- Take messy text: OMG LOVE this!!! with heart-eyes emoji, #NLP, 2moro, cooool, teh, link https://t.co/xyz, and decide case handling, punctuation keep/remove, typo normalisation, emoji handling, and justify for a sentiment task.
- Tokenise the sentence I can’t believe it’s 7pm at L’Artusi! three different ways and discuss pros and cons.
- Explain when removing stop words hurts: analyse This is not good and to be or not to be.
- Compare stemming vs lemmatisation for running, runs, ran, better, happiness, which returns real words and when would you use each? Bonus: what bias risk if a corpus overrepresents American English vs Nigerian English?
Frequently asked questions
Why does trying harder not work here?
Trying harder to clean text by deleting everything unusual fails here, because the chapter’s own logic is that cleaning is a set of thoughtful choices about what to keep, what to normalise, and what to remove. Stripping emojis destroys sentiment; stripping ‘not’ reverses meaning; stripping the apostrophe from L’Artusi destroys a name.
How do I know it worked?
You decided case handling, punctuation, typo normalisation, and emoji handling for the messy text, with a justification for a sentiment task. You tokenised I can’t believe it’s 7pm at L’Artusi! three ways with pros and cons. You explained why removing stop words hurts for This is not good and to be or not to be.
What if it does not work?
If sentiment scores look backwards, then check whether ‘not’ was removed as a stop word. If brand mentions are missed, then check whether case handling split LOVE, love, and looooove into different words. If new words fail, then check whether the tokeniser is word-level only and needs subword pieces.
Get the book
Google Play eBookGoogle Play AudiobookBarnes & Noble
More from What Is Natural Language Processing?
- What Is Natural Language Processing?: A Beginner’s Guide to How Machines Understand Language
- Where to Go Next: Your NLP Future (and From Words to World)
- Ethics, Bias, and Limitations: Using NLP Responsibly
- Opinions, Summaries, and Voice: NLP in Action
- Search, Translation, and Chatbots: How NLP Powers the Web
- The Invisible NLP All Around You: Keyboards, Inboxes, and Voice
- Intent, Entities, and Context: How Machines Follow a Conversation
- Giving Words Meaning: From Counting to Meaning Maps to Language Models
- The End-to-End Pipeline: Your Map for the Rest of the Book
- From Rules to Statistics to Neural Networks: How NLP Got Here
- Why Language Is Hard: Ambiguity, Three Layers, and the Outside-In Machine
- What NLP Really Is: The One-Sentence Definition and the Family Picture
- Why This Book, Why Now: A Framework You Can Use for Years
- Natural Language Processing Glossary: Key Terms and Concepts in Plain Language
- How to Use What Is Natural Language Processing? for Study Groups, Classrooms, and Teams
- After You Finish the Book: How to Build Projects, Keep a Journal, and Stay Current
- How the NLP Pipeline and Meaning Maps Work, in Plain Language?
- How Natural Language Processing Works in Everyday Life and Work: 5 Scenarios
- Common Misunderstandings About Natural Language Processing and How to Fix Them
- 7 Free Tools If You Loved This NLP Book
- How This NLP Book Was Built: Concept First Method
- NLP Beginner Book FAQ: Formats, Scope, Safety, Work, Answered
- Who Should Read This NLP Beginner Book? Reader Guide
- NLP Book vs Textbook vs Article vs Coding Course vs Doing Nothing
- Read Chapter 1 Free: What Is Natural Language Processing Really?
- How to Run a One Day Autocorrect Detective Check
- What Is the NLP Pipeline? Input Processing Understanding Generation Explained
- Best Natural Language Processing Books in 2026: A Beginner First Pick
