Python | Lemmatization with NLTK
Last Updated :
02 Jan, 2024
Lemmatization is a fundamental text pre-processing technique widely applied in natural language processing (NLP) and machine learning. Serving a purpose akin to stemming, lemmatization seeks to distill words to their foundational forms. In this linguistic refinement, the resultant base word is referred to as a “lemma.” The article aims to explore the use of lemmatization and demonstrates how to perform lemmatization with NLTK.
Lemmatization
Lemmatization is the process of grouping together the different inflected forms of a word so they can be analyzed as a single item. Lemmatization is similar to stemming but it brings context to the words. So, it links words with similar meanings to one word.
Text preprocessing includes both Stemming as well as lemmatization. Many times, people find these two terms confusing. Some treat these two as the same. Lemmatization is preferred over Stemming because lemmatization does morphological analysis of the words.
Examples of lemmatization:
-> rocks : rock
-> corpora : corpus
-> better : good
One major difference with stemming is that lemmatize takes a part of speech parameter, “pos” If not supplied, the default is “noun.”
Lemmatization Techniques
Lemmatization techniques in natural language processing (NLP) involve methods to identify and transform words into their base or root forms, known as lemmas. These approaches contribute to text normalization, facilitating more accurate language analysis and processing in various NLP applications. Three types of lemmatization techniques are:
1. Rule Based Lemmatization
Rule-based lemmatization involves the application of predefined rules to derive the base or root form of a word. Unlike machine learning-based approaches, which learn from data, rule-based lemmatization relies on linguistic rules and patterns.
Here’s a simplified example of rule-based lemmatization for English verbs:
Rule: For regular verbs ending in “-ed,” remove the “-ed” suffix.
Example:
- Word: “walked”
- Rule Application: Remove “-ed”
- Result: “walk
This approach extends to other verb conjugations, providing a systematic way to obtain lemmas for regular verbs. While rule-based lemmatization may not cover all linguistic nuances, it serves as a transparent and interpretable method for deriving base forms in many cases.
2. Dictionary-Based Lemmatization
Dictionary-based lemmatization relies on predefined dictionaries or lookup tables to map words to their corresponding base forms or lemmas. Each word is matched against the dictionary entries to find its lemma. This method is effective for languages with well-defined rules.
Suppose we have a dictionary with lemmatized forms for some words:
- ‘running’ -> ‘run’
- ‘better’ -> ‘good’
- ‘went’ -> ‘go’
When we apply dictionary-based lemmatization to a text like “I was running to become a better athlete, and then I went home,” the resulting lemmatized form would be: “I was run to become a good athlete, and then I go home.”
3. Machine Learning-Based Lemmatization
Machine learning-based lemmatization leverages computational models to automatically learn the relationships between words and their base forms. Unlike rule-based or dictionary-based approaches, machine learning models, such as neural networks or statistical models, are trained on large text datasets to generalize patterns in language.
Example:
Consider a machine learning-based lemmatizer trained on diverse texts. When encountering the word ‘went,’ the model, having learned patterns, predicts the base form as ‘go.’ Similarly, for ‘happier,’ the model deduces ‘happy’ as the lemma. The advantage lies in the model’s ability to adapt to varied linguistic nuances and handle irregularities, making it robust for lemmatizing diverse vocabularies.
Implementation of Lemmatization
NLTK
Below is the implementation of lemmatization words using NLTK
Python3
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
print("rocks :", lemmatizer.lemmatize("rocks"))
print("corpora :", lemmatizer.lemmatize("corpora"))
print("better :", lemmatizer.lemmatize("better", pos="a"))
|
Output:
rocks : rock
corpora : corpus
better : good
NLTK (Natural Language Toolkit) is a Python library used for natural language processing. One of its modules is the WordNet Lemmatizer, which can be used to perform lemmatization on words.
Lemmatization is the process of reducing a word to its base or dictionary form, known as the lemma. For example, the lemma of the word “cats” is “cat”, and the lemma of “running” is “run”.
Spacy
Python3
import spacy
nlp = spacy.load('en_core_web_sm')
text = "The quick brown foxes are jumping over the lazy dogs."
doc = nlp(text)
lemmatized_tokens = [token.lemma_ for token in doc]
lemmatized_text = ' '.join(lemmatized_tokens)
print("Original Text:", text)
print("Lemmatized Text:", lemmatized_text)
|
Output:
Original Text: The quick brown foxes are jumping over the lazy dogs.
Lemmatized Text: the quick brown fox be jump over the lazy dog .
Advantages of Lemmatization with NLTK
- Improves text analysis accuracy: Lemmatization helps in improving the accuracy of text analysis by reducing words to their base or dictionary form. This makes it easier to identify and analyze words that have similar meanings.
- Reduces data size: Since lemmatization reduces words to their base form, it helps in reducing the data size of the text, which makes it easier to handle large datasets.
- Better search results: Lemmatization helps in retrieving better search results since it reduces different forms of a word to a common base form, making it easier to match different forms of a word in the text.
Disadvantages of Lemmatization with NLTK
- Time-consuming: Lemmatization can be time-consuming since it involves parsing the text and performing a lookup in a dictionary or a database of word forms.
- Not suitable for real-time applications: Since lemmatization is time-consuming, it may not be suitable for real-time applications that require quick response times.
- May lead to ambiguity: Lemmatization may lead to ambiguity, as a single word may have multiple meanings depending on the context in which it is used. In such cases, the lemmatizer may not be able to determine the correct meaning of the word.
Also Check:
Similar Reads
Python | Lemmatization with TextBlob
Lemmatization is the process of grouping together the different inflected forms of a word so they can be analyzed as a single item. Lemmatization is similar to stemming but it brings context to the words. So it links words with similar meanings to one word.Text preprocessing includes both Stemming a
2 min read
Python - Lemmatization Approaches with Examples
The following is a step by step guide to exploring various kinds of Lemmatization approaches in python along with a few examples and code implementation. It is highly recommended that you stick to the given flow unless you have an understanding of the topic, in which case you can look up any of the
14 min read
NLP Augmentation with nlpaug Python Library
Data augmentation is a crucial step in building robust AI models, especially during the data preparation phase. This process involves adding synthetic data to the existing datasets to enhance the quality and diversity of the training data. For textual models, such as generative chatbots and translat
6 min read
Python | PoS Tagging and Lemmatization using spaCy
spaCy is one of the best text analysis library. spaCy excels at large-scale information extraction tasks and is one of the fastest in the world. It is also the best way to prepare text for deep learning. spaCy is much faster and accurate than NLTKTagger and TextBlob. How to Install ? pip install spa
2 min read
How to Perform Lemmatization in R?
Lemmatization is a critical technique in the field of Natural Language Processing (NLP). It plays an essential role in text preprocessing by transforming words into their base or root forms, known as lemmas. This process helps standardize words that appear in different grammatical forms, reducing th
6 min read
Python concordance command in NLTK
The Natural Language Toolkit (NLTK) is a powerful library in Python for working with human language data (text). One of its many useful features is the concordance command, which helps in text analysis by locating occurrences of a specified word within a body of text and displaying them along with t
7 min read
Removing stop words with NLTK in Python
In natural language processing (NLP), stopwords are frequently filtered out to enhance text analysis and computational efficiency. Eliminating stopwords can improve the accuracy and relevance of NLP tasks by drawing attention to the more important words, or content words. The article aims to explore
9 min read
How to use CoreNLPParser in NLTK in Python
The Stanford CoreNLP toolkit, integrated with the Natural Language Toolkit (NLTK) in Python, provides robust tools for linguistic analysis. One of the powerful components of this integration is the CoreNLPParser, which allows for advanced parsing and linguistic analysis of text. In this article, we
7 min read
How To Remove Nltk From Python
In Python, NLTK, or Natural Language Toolkit, is a powerful library that is used for human language data. This library provides tools for tasks like tokenization, stemming, tagging, passing, and more. Once the usage of the library is done, we can remove NLTK from our system. So we can remove it usin
1 min read
NLP Libraries in Python
In today's AI-driven world, text analysis is fundamental for extracting valuable insights from massive volumes of textual data. Whether analyzing customer feedback, understanding social media sentiments, or extracting knowledge from articles, text analysis Python libraries are indispensable for data
15+ min read