Skip to content

Text Preprocessing in Python: A Practical Walkthrough

Garbage in, garbage out applies to NLP more than anywhere. A short, practical tour of cleaning text in Python before it reaches a model.

P Priya Sharma Updated 3 min read

Real text is messy. It arrives with HTML tags, stray URLs, inconsistent casing and emoji, and if you feed that straight to a model you will get noisy results. Preprocessing is the step that turns raw text into a clean, consistent form. This walkthrough shows a dependable Python recipe using the standard library plus NLTK and spaCy, with the reasoning behind each step so you know what to keep and what to drop.

Start with the standard library

Before pulling in heavy NLP libraries, handle the obvious problems with built-in tools. Normalise whitespace with a simple regex, strip HTML with the html module, and lower the case unless casing carries meaning for your task. Remove URLs and email addresses if they are noise for your use case, but think first: for some tasks, like phishing detection, the URL is the signal. Keep an original copy of each document so you can audit what preprocessing removed.

  • Collapse repeated whitespace and strip surrounding spaces.
  • Decode HTML entities and strip tags when scraping web pages.
  • Normalise casing consistently across the whole corpus.
  • Remove or mask URLs, emails and numbers only when they are noise.

Tokenise with NLTK or spaCy

Tokenisation splits text into words and punctuation. A naive split on whitespace breaks contractions and keeps stray punctuation glued to words. NLTK's word_tokenize handles common cases out of the box, while spaCy produces tokens that already carry rich annotations. Choose spaCy when you also want part-of-speech tags, entities and lemmas, and choose NLTK when you want a lightweight dependency-free step. Test the tokeniser on your own data before trusting it.

Remove stop words, but know the cost

Stop-word lists remove words like the, and and of that appear everywhere and carry little meaning for many tasks. Removing them shrinks your vocabulary and speeds up training. The catch is context: the word not flips meaning entirely, and phrases like to be or not to be lose their point without stop words. For sentiment and question answering, keep a minimal stop list or skip removal altogether. For topic modelling and search indexing, removal usually helps.

  • Default stop-word lists are tuned for English news text.
  • Keep negations such as not, never and no in sentiment work.
  • Domain text may need a custom stop list built from frequency.
  • Re-run your evaluation after any change to the stop list.

Lemmatise and build your final corpus

Lemmatisation reduces running, ran and runs to their dictionary base run, which collapses near-duplicate features into one. spaCy exposes lemmas as token.lemma_ with no extra setup. After lemmatisation, decide the unit your model will consume: single words for classical models, whole sentences for embeddings, or fixed-length chunks for retrieval. Store the pipeline as a function, apply it consistently to training and new data, and keep the raw text alongside the clean version for debugging.

Key takeaways

  • Handle formatting noise with the standard library before adding NLP tools.
  • Tokenise with a tested library rather than a naive whitespace split.
  • Stop-word removal helps some tasks and quietly harms others.
  • A reusable preprocessing function applied consistently beats one-off scripts.
P

Written by

Priya Sharma

Priya previously built ML systems at a cloud provider. She writes hands-on tutorials covering embeddings, RAG and model deployment.

More articles by Priya Sharma →

Comments

Leave a comment

Comments are moderated and will appear once approved.