Home » Home » Text Preprocessing with Python

In the field of Natural Language Processing (NLP), text preprocessing is an essential step before applying any machine learning or statistical models to text data. Text preprocessing involves transforming raw text data into a more structured format that can be easily analyzed by machines. Python, being one of the most popular programming languages, has a wide range of libraries that can be used for text preprocessing. In this article, we will discuss some of the commonly used techniques for text preprocessing with Python.

  1. Tokenization Tokenization is the process of breaking down a text into smaller components called tokens. Tokens can be words, phrases, or even characters. Python has built-in libraries like NLTK and SpaCy that can be used for tokenization.
import nltk
from nltk.tokenize import word_tokenize
text = "This is a sample sentence."
tokens = word_tokenize(text)

Output: [‘This’, ‘is’, ‘a’, ‘sample’, ‘sentence’, ‘.’]

  1. Stop word removal Stop words are words that occur frequently in a language but do not provide any meaning to the text. Examples of stop words include ‘the’, ‘and’, ‘a’, ‘an’, etc. Removing stop words can help in reducing the size of the dataset and improving the accuracy of models. NLTK library provides a list of stop words for different languages.
import nltk
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))
text = "This is a sample sentence."
tokens = word_tokenize(text)
filtered_tokens = [word for word in tokens if not word in stop_words]

Output: [‘This’, ‘sample’, ‘sentence’, ‘.’]

  1. Stemming and Lemmatization Stemming and Lemmatization are techniques used for reducing words to their base forms. Stemming involves removing suffixes from a word to get its root form. For example, the stem of the word ‘running’ is ‘run’. Lemmatization, on the other hand, involves reducing a word to its base form using morphological analysis. NLTK library provides implementations for both stemming and lemmatization.
import nltk
from nltk.stem import PorterStemmer, WordNetLemmatizer
from nltk.tokenize import word_tokenize
ps = PorterStemmer()
wnl = WordNetLemmatizer()
text = "running is a way to keep fit"
tokens = word_tokenize(text)
stemmed_tokens = [ps.stem(word) for word in tokens]
lemmatized_tokens = [wnl.lemmatize(word) for word in tokens]

Output: Stemmed tokens: [‘run’, ‘is’, ‘a’, ‘way’, ‘to’, ‘keep’, ‘fit’] Lemmatized tokens: [‘running’, ‘is’, ‘a’, ‘way’, ‘to’, ‘keep’, ‘fit’]

  1. Regular expressions Regular expressions are patterns used for matching strings. They can be used for tasks like finding and replacing text, extracting information, and so on. The re module in Python provides support for regular expressions.
import re
text = "John's email is john@example.com"
pattern = r'\w+@\w+\.\w+'
match = re.search(pattern, text)
if match:

Output: john@example.com

  1. Lowercasing Lowercasing involves converting all the characters in a text to lowercase. This technique can be helpful in reducing the number of unique words in the dataset and avoiding duplication of words. The lower() function in Python can be used to convert a string to lowercase.
text = "This is a SAMPLE sentence."
lowercase_text = text.lower()

Output: this is a sample sentence.

  1. Removing punctuation Punctuation marks like commas, periods, exclamation marks, and question marks do not add any meaning to the text and can be removed. Python has a built-in string module that provides a list of punctuation marks.
import string
text = "This is a sample sentence."
no_punc_text = text.translate(str.maketrans("", "", string.punctuation))

Output: This is a sample sentence

  1. Spell checking Spell checking is the process of correcting spelling mistakes in a text. The pyspellchecker library in Python provides an implementation of the spell checking algorithm that can be used for this purpose.
from spellchecker import SpellChecker
spell = SpellChecker()
text = "Thsi is a sample sentence."
tokens = word_tokenize(text)
corrected_tokens = []
for token in tokens:

Output: [‘This’, ‘is’, ‘a’, ‘sample’, ‘sentence’]

  1. N-grams N-grams are contiguous sequences of n items from a given sample of text. N-grams can be used for tasks like language modeling, text classification, and information retrieval. The nltk library provides an implementation for generating n-grams.
import nltk
from nltk.util import ngrams
text = "This is a sample sentence."
tokens = word_tokenize(text)
bigrams = list(ngrams(tokens, 2))
trigrams = list(ngrams(tokens, 3))

Output: Bigrams: [(‘This’, ‘is’), (‘is’, ‘a’), (‘a’, ‘sample’), (‘sample’, ‘sentence’), (‘sentence’, ‘.’)] Trigrams: [(‘This’, ‘is’, ‘a’), (‘is’, ‘a’, ‘sample’), (‘a’, ‘sample’, ‘sentence’), (‘sample’, ‘sentence’, ‘.’)]


Text preprocessing is an important step in Natural Language Processing, and Python provides a wide range of libraries and tools for this purpose. The techniques discussed in this article, including tokenization, stop word removal, stemming and lemmatization, regular expressions, lowercasing, removing punctuation, spell checking, and n-grams, can help in cleaning and preparing text data for further analysis. By using these techniques, we can improve the accuracy of our models and obtain better insights from text data.

Related Posts

Leave a Reply

%d bloggers like this: