Data Cleaning in NLP with Python Examples
For teams operationalizing NLP, the real challenge is not just cleaning text once, but making preprocessing repeatable across notebooks, batch jobs, and production services. A preprocessing pipeline should be treated as part of the model system itself: versioned, testable, and aligned with the exact input contracts that downstream classifiers, search indices, or LLM workflows expect. Small inconsistencies in tokenization, stopword policy, or normalization can create silent drift between training and inference.
There is also an architectural choice between ad hoc scripts and reusable pipeline components. In enterprise settings, text cleanup often sits at the boundary between ingestion, storage, and feature generation. That makes dependency management important: the same libraries, language resources, and rule sets should be available in development, CI, and deployment environments. Without that consistency, results can vary across platforms and make debugging much harder.
Operationally, preprocessing rules become a governance issue. Decisions such as whether to strip URLs, preserve punctuation for sentiment, or lemmatize before indexing influence both model quality and auditability. Teams should document these choices because they affect explainability, reproducibility, and future retraining. If the rules are embedded directly in notebooks, they are easy to lose and difficult to reuse.
The main takeaway for engineers is to design text cleaning as a controlled transformation layer rather than a one-off utility. That approach reduces technical debt, simplifies integration across NLP services, and makes it easier to evolve the pipeline when data sources, languages, or business requirements change.
Handful explanation of natural language processing techniques
Photo by AltumCode on Unsplash NLP stands for Natural Language Processing. It is a branch of artificial intelligence (AI) that focuses on enabling computers to understand, interpret, and generate human language in a way that is both meaningful and useful. Natural Language Processing involves the interaction between computers and humans through natural language, which includes tasks such as speech recognition, text analysis, sentiment analysis, language translation, question answering, and text generation. NLP technologies are designed to process and analyze text data in various forms, such as written text, spoken words, and other forms of human-generated language. NLP algorithms and models are trained on large datasets to learn patterns in language and can be used in a wide range of applications, including virtual assistants, chatbots, language translationtools, sentiment analysis tools, and many other applications where human-computer interaction involves language processing. Natural Language Processing has become an increasingly important field of study and has found numerous applications in various industries, including healthcare, finance, customer service, marketing, and many others.
Topics to be covered:
- Text cleaning
- Tokenization
- Stemming
- Lemmatization
- N-grams
- Text Cleaning: This involves removing irrelevant information such as special characters, punctuation, numbers, and white spaces from the text data. It may also involve converting text to lowercase or uppercase to ensure consistency in the data.
- Tokenization: This involves breaking the text data into individual words or tokens, which are the basic units of text for analysis. Tokenization helps in preparing the text data for further analysis, such as word frequency analysis, part-of-speech tagging, and sentiment analysis.
- Stopword Removal: Stopwords are common words such as โthe,โ โand,โ โis,โ โin,โ etc.. These words show no meaning in the text and can be filtered from the raw data.
- Lemmatization and Stemming: These techniques involve reducing words to their root or base form to consolidate words with similar meanings. Lemmatization converts words to their base form using language rules, while stemming involves removing prefixes or suffixes from words to get their root form.
- Removing HTML tags, URLs, or other special elements: If the text data is obtained from web pages or other online sources, it may contain HTML tags, URLs, or other special elements that need to be removed before analysis.
- Spell Checking and Correction: This involves identifying and correcting misspelled words in the text data to improve the accuracy of the analysis.
- Removing or replacing specific patterns or patterns: This may include removing or replacing specific patterns, such as email addresses, phone numbers, or any other patterns that are not relevant to the analysis.
- Handling Text Encoding: Text data may have different encodings, such as UTF-8, ASCII, or Unicode, which may need to be converted to a common encoding format for consistency and compatibility in further analysis.
- Handling Imbalanced Data: In some NLP tasks, such as sentiment analysis or text classification, the text data may be imbalanced, with one class dominating the data. Pre-processing techniques in this situation can be addressed by two methods i.e., oversampling or undersampling,
Text cleaningText cleaning is an important step in Natural Language Processing (NLP) that involves cleaning and preparing raw text data for further analysis or modeling. Hereโs an example of how you can perform text cleaning in Python using various libraries commonly used in NLP, such as NLTK (Natural Language Toolkit) and regular expressions (regex): Python Example:
import nltkIn the above example, we first load the NLTK stop words, which are common words like โthe,โ โand,โ โis,โ etc., that are often removed from text data during text cleaning. Then, we provide an example of raw text data that needs to be cleaned. We convert the text to lowercase, remove numbers and special characters using regular expressions, tokenize the text into individual words using NLTKโs word_tokenize() function, and then remove the stop words from the tokens. Finally, we join the tokens back into a clean text string using Pythonโs string manipulation capabilities. Note that the specific text cleaning steps may vary depending on the requirements and characteristics of the text data, and itโs important to carefully consider the specific needs of your NLP task to ensure accurate and meaningful results. Additionally, text cleaning is often an iterative process that may require experimentation and fine-tuning based on the specific text data and NLP task at hand.
import re
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
# Load NLTK stop words
nltk.download('stopwords')
stop_words = set(stopwords.words("english"))
# Example raw text data
text = "specific text cleaning steps may vary depending on the requirements !@#$%^&*()"
# Convert to lowercase
text = text.lower()
# using re to remove special characters
text = re.sub(r"[^a-zA-Z]", " ", text)
# Tokenize the text into individual words
token_words = word_tokenize(text)
# Remove stopwords
tokens = [word for word in token_words if word not in stop_words]
# Join the tokens back into a clean text string
clean_text = " ".join(tokens)
print("Original Text: ", text)
print("Clean Text: ", clean_text)
#output:
Original Text: specific text cleaning steps may vary depending on the requirements
Clean Text: specific text cleaning steps may vary depending requirements
TokenizationTokenization is the process of breaking text data into individual words or tokens, which are the basic units of text for analysis. Hereโs an example of how you can perform tokenization in Python using the Natural Language Toolkit (NLTK) library: Python Example:
import nltkIn the above example, we first import the nltk library and load the word_tokenize() function from the nltk.tokenize module. Then, we provide an example of raw text data that needs to be tokenized. We pass the text data to the word_tokenize() function, which tokenizes the text into individual words and stores them in a list called tokens.
from nltk.tokenize import word_tokenize
# Example raw text data
text = "My name is Amit Chauhan. I live in New Delhi"
# Tokenize the text into individual words
tokens = word_tokenize(text)
print("Original Text: ", text)
print("Tokens: ", tokens)
#output:
Original Text: Tokenization is the process of breaking text data into individual words
Tokens: ['Tokenization', 'is', 'the', 'process', 'of', 'breaking', 'text', 'data', 'into', 'individual', 'words']
StemmingStemming is a common technique used in Natural Language Processing (NLP) to reduce words to their root or base form, known as a โstem.โ This helps in reducing the complexity of text data and grouping similar words. Hereโs an example of how you can perform stemming in Python using the NLTK (Natural Language Toolkit) library: Python Example:
import nltkIn the above example, we first import the nltk library and create an instance of the PorterStemmer class from the nltk.stem module. Then, we provide an example word โrunningโ that needs to be stemmed. We call the stem() method of the PorterStemmer class, passing the word as an argument, which performs stemming and returns the stemmed form of the word.
from nltk.stem import PorterStemmer
# Create an instance of the PorterStemmer
ps = PorterStemmer()
# Example word to be stemmed
word = "running"
# Perform stemming
stemmed_word = ps.stem(word)
print("Original Word: ", word)
print("Stemmed Word: ", stemmed_word)
#output:
Original Word: running
Stemmed Word: run
LemmatizationLemmatization is a technique used in Natural Language Processing (NLP) to reduce words to their base or canonical form, known as a โlemma.โ Unlike stemming, which simply removes suffixes from words, lemmatization takes into consideration the morphological analysis of words and reduces them to their meaningful base form. Hereโs an example of how you can perform lemmatization in Python using the NLTK (Natural Language Toolkit) library: Python Example:
import nltkIn the above example, we first import the nltk library and create an instance of the WordNetLemmatizer class from the nltk.stem module. Then, we provide an example word โrunningโ that needs to be lemmatized. We use the nltk.pos_tag() function to determine the Part-Of-Speech (POS) tag of the word, as lemmatization is often dependent on the POS of the word. We then map the POS tag to the corresponding WordNet POS tag, as the WordNetLemmatizer requires the use of specific POS tags. Finally, we call the lemmatize() method of the WordNetLemmatizer class, passing the word and the mapped POS tag as arguments, which performs lemmatization and returns the lemmatized form of the word.
nltk.download('averaged_perceptron_tagger')
nltk.download('wordnet')
nltk.download('omw-1.4')
from nltk.stem import WordNetLemmatizer
from nltk.corpus import wordnet
# Create an instance of the WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
# Example word to be lemmatized
word = "running"
# check part of speech (POS) Tag
pos_tag = nltk.pos_tag([word])[0][1]
# mapping POS to wor_net POS
wn_pos = wordnet.ADJ if pos_tag.startswith('J') else wordnet.VERB if pos_tag.startswith('V') else wordnet.NOUN if pos_tag.startswith('N') else wordnet.ADV
# Perform lemmatization
lemmatized_word = lemmatizer.lemmatize(word, wn_pos)
print("Original Word: ", word)
print("Lemmatized Word: ", lemmatized_word)
#output:
Original Word: running
Lemmatized Word: run
N-gramsIn natural language processing (NLP), n-grams are contiguous sequences of n words or characters extracted from a given text. N-grams are used to represent language patterns and can be useful in various NLP tasks such as text generation, language modeling, sentiment analysis, and machine translation. Hereโs an example of how you can generate n-grams in Python:
def generate_ngrams(text, n):In the above example, we define a function called generate_ngrams() that takes a text and an integer n as input, where n specifies the size of the n-grams to be generated. The function splits the input text into words using split(), and then iterates through the words to generate n-grams by joining n consecutive words using join(). The resulting n-grams are stored in a list and returned. I hope you like the article. Reach me on my LinkedIn and twitter.
"""
Function to generate n-grams from a given text.
Args:
- text (str): Input text
- n (int): Number of words or characters in each n-gram
Returns:
- List of n-grams
"""
words = text.split()
ngrams = []
for i in range(len(words)-(n-1)):
ngram = ' '.join(words[i:i+n]) # Join n words to form an n-gram
ngrams.append(ngram)
return ngrams
# Example usage
text = "Today is a beautiful day and sky is cloudy"
n = 3 # Generate trigrams (3-grams)
ngrams = generate_ngrams(text, n)
print(f"{n}-grams: {ngrams}")
#output:
3-grams: ['Today is a', 'is a beautiful', 'a beautiful day', 'beautiful day and', 'day and sky', 'and sky is', 'sky is cloudy']
Large-scale custom natural language processing
Enjoyed this article? Sign up for our newsletter to receive regular insights and stay connected.
