How to use Spacy lemmatizer?

This recipe helps you use Spacy lemmatizer

Recipe Objective

How to use Spacy lemmatizer, As we have discussed earlier only what is Spacy and what is lemmatizer. Spacy Lemmatization which gives the lemma of the word, lemma is nothing the but base word which has been converted through the process of lemmatization for e.g 'hostorical', 'history' will become 'history' so the lemma is 'history' here.

Learn How to Build a Simple Chatbot from Scratch in Python (using NLTK)

Step 1 - Import Spacy

import spacy

Step 2 - Initialize the Spacy en model.

load_model = spacy.load('en', disable = ['parser','ner'])

In the above code we have initialized the Spacy model and kept only the things which is required for lemmatization which is nothing but the tagger and disabled the parser and ner which are not required for now.

Step 3 - Take a simple text for sample

My_text = "This is just a sample text for the purpose of testing"

Step 4 - Parse the text

doc = load_model(My_text)

here in the above we have parsed the text that we have taken for sample by using the model that we have initialized i.e load_model.

Step 5 - Extract the lemma for each token

" ".join([token.lemma_ for token in doc])

So from the above we can see that there is slight change in the sentence which is, original - 'this is' and extracted - 'this be'.

Step 6 - Lets try with another example

text2 = "I am hanging out in a garden" doc2 = load_model(text2) " ".join([token.lemma_ for token in doc2])

So we can see whenever Spacy detects a pronoun it adds a -PRON- in the text.

What Users are saying..

profile image

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Classification Projects on Machine Learning for Beginners - 2
Learn to implement various ensemble techniques to predict license status for a given business.

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.

Build Multi Class Text Classification Models with RNN and LSTM
In this Deep Learning Project, you will use the customer complaints data about consumer financial products to build multi-class text classification models using RNN and LSTM.

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

OpenCV Project for Beginners to Learn Computer Vision Basics
In this OpenCV project, you will learn computer vision basics and the fundamentals of OpenCV library using Python.

Locality Sensitive Hashing Python Code for Look-Alike Modelling
In this deep learning project, you will find similar images (lookalikes) using deep learning and locality sensitive hashing to find customers who are most likely to click on an ad.

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.