ProCodebaseProCodebase
  • ModusA Growth OS for your business, on WhatsAppKiosqSell more. Chase less.AI InterviewerAutomated screening & AI-led interviewsXperto AIAI prep companion for candidatesAI Tools HubResume builder, learning paths & more
  • Pre-Vetted DevelopersScreened, scored and ready to interviewAI-Native DevelopersSenior engineers on an hourly basis
  • Services
  • Features
  • AI Coaching
  • Jobs
  • FAQs
Sign inBook a demo
  • Services
  • Features
  • AI Coaching
  • Jobs
  • FAQs
Sign inBook a demo
ProCodebaseProCodebase

ProCodebase Technologies builds AI products for hiring and growth, and ships software for clients as a technical consultancy. We source, screen and deliver pre-vetted developers — so you only interview high-signal candidates.

Products

  • Modus
  • Kiosq
  • AI Interviewer
  • Xperto AI
  • AI Tools Hub

Hire & build

  • Pre-Vetted Developers
  • AI-Native Developers
  • Technical Consultancy
  • MVP Development
  • Features

Resources

  • Articles
  • Topics
  • Certifications
  • Collections
  • Jobs

Company

  • About Us
  • Contact Us
  • Book a Demo
  • FAQs

© 2026 ProCodebase Technologies. All rights reserved.

  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Lemmatization in Python Using WordNet Lemmatizer

author
Generated by
ProCodebase AI

22/11/2024

Python

Sign in to read full article

Natural Language Processing (NLP) is a fascinating field within artificial intelligence that deals with the interaction between computers and human languages. One of the key components of NLP is the process of text normalization, which includes techniques like stemming and lemmatization. In this post, we will delve into lemmatization using the WordNet Lemmatizer from the NLTK (Natural Language Toolkit) library in Python.

What is Lemmatization?

Lemmatization is the process of reducing a word to its base or root form, also known as the lemma. Unlike stemming, which simply truncates words to their roots, lemmatization considers the context and converts a word to its meaningful base form. For example, the words "running," "ran," and "runs" would all be converted to "run."

Why Use WordNet Lemmatizer?

The WordNet Lemmatizer uses WordNet, a large lexical database of English, to ensure that the lemmatization process is context-aware. This means that it can correctly identify the part of speech (POS) of words and apply the appropriate transformation. This capability makes it far more effective than simple stemming methods.

Getting Started with NLTK

To use the WordNet Lemmatizer, you'll first need to make sure you have NLTK installed in your Python environment. If you haven't installed it yet, you can do so via pip:

pip install nltk

After installing NLTK, we will also need to download the WordNet data:

import nltk nltk.download('wordnet') nltk.download('omw-1.4') # Optional, for multilingual support

Using the WordNet Lemmatizer

Now we can get started with the WordNet Lemmatizer. Let’s see how to create an instance of the lemmatizer and use it to lemmatize words:

from nltk.stem import WordNetLemmatizer # Create a WordNetLemmatizer object lemmatizer = WordNetLemmatizer() # Example words words = ["running", "ran", "better", "cats", "mouse", "geese"] # Lemmatizing words without specifying parts of speech for word in words: print(f'Original: {word} -> Lemma: {lemmatizer.lemmatize(word)}')

Output:

Original: running -> Lemma: running
Original: ran -> Lemma: ran
Original: better -> Lemma: better
Original: cats -> Lemma: cat
Original: mouse -> Lemma: mouse
Original: geese -> Lemma: geese

As you can see, some of the words didn’t get changed when we did not specify their parts of speech. The default behavior of the lemmatizer treats the input words as nouns. To get more accurate results, we should specify the correct part of speech.

Specifying Parts of Speech

The WordNet Lemmatizer allows you to specify the part of speech while lemmatizing. The following mappings can be used:

  • n for noun
  • v for verb
  • a for adjective
  • r for adverb

Let's see how using part of speech improves the results:

# Example words with parts of speech words_with_pos = [("running", "v"), ("ran", "v"), ("better", "a"), ("cats", "n")] for word, pos in words_with_pos: print(f'Original: {word} (POS: {pos}) -> Lemma: {lemmatizer.lemmatize(word, pos)}')

Output:

Original: running (POS: v) -> Lemma: run
Original: ran (POS: v) -> Lemma: run
Original: better (POS: a) -> Lemma: good
Original: cats (POS: n) -> Lemma: cat

Now we see the lemmatizer effectively transformed "running" to "run," and "better" to "good." By integrating the correct parts of speech, we can achieve meaningful reductions.

Handling User Input

In a real-world application, you might process user-generated text. Here's how to combine everything into a simple function that can lemmatize input text:

def lemmatize_text(text): words = nltk.word_tokenize(text) # Tokenizing the text lemmatized_words = [] for word in words: # Here, we can assume all words are verbs. In practice, you'd need a method to determine the correct POS. lemma = lemmatizer.lemmatize(word.lower(), 'v') # Converting to lowercase for case-insensitivity lemmatized_words.append(lemma) return ' '.join(lemmatized_words) input_text = "He has been running and ran a good race better than the other cats" output_text = lemmatize_text(input_text) print(f'Input: {input_text}\nLemmatized: {output_text}')

Conclusion

Lemmatization is a crucial part of preprocessing in NLP, allowing our machine learning models to better understand and process text. The WordNet Lemmatizer in NLTK provides a powerful and effective way to achieve this in Python. Whether you’re developing chatbots, analyzing customer feedback, or preprocessing datasets for text analysis, understanding lemmatization is essential for improving the performance of your NLP tasks.

Popular tags

PythonNLTKNatural Language Processing

Share now!

Like & bookmark

Related collections

  • PyTorch Mastery: From Basics to Advanced

    14/11/2024 · Python

  • Mastering Scikit-learn from Basics to Advanced

    15/11/2024 · Python

  • TensorFlow Mastery: From Foundations to Frontiers

    06/10/2024 · Python

  • Seaborn: Data Visualization from Basics to Advanced

    06/10/2024 · Python

  • Mastering NLTK for Natural Language Processing

    22/11/2024 · Python

Related articles

  • Deploying Automation Scripts with Python

    08/12/2024 · Python

  • Exploring Parts of Speech Tagging with NLTK in Python

    22/11/2024 · Python

  • Building Domain Specific Languages with Python

    13/01/2025 · Python

  • Sentiment Analysis with NLTK

    22/11/2024 · Python

  • Understanding Python Classes and Object-Oriented Programming

    21/09/2024 · Python

  • Understanding Python Syntax and Structure

    21/09/2024 · Python

  • Image Thresholding in Python

    06/12/2024 · Python

Popular category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design