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 approaches given below.
What is Lemmatization?
In contrast to stemming, lemmatization is a lot more powerful. It looks beyond word reduction and considers a language’s full vocabulary to apply a morphological analysis to words, aiming to remove inflectional endings only and to return the base or dictionary form of a word, which is known as the lemma.
For clarity, look at the following examples given below:
Original Word ---> Root Word (lemma) Feature meeting ---> meet (core-word extraction) was ---> be (tense conversion to present tense) mice ---> mouse (plural to singular)
TIP: Always convert your text to lowercase before performing any NLP task including lemmatizing.
Various Approaches to Lemmatization:
We will be going over 9 different approaches to perform Lemmatization along with multiple examples and code implementations.
- WordNet (with POS tag)
- TextBlob (with POS tag)
- Stanford CoreNLP
1. Wordnet Lemmatizer
Wordnet is a publicly available lexical database of over 200 languages that provides semantic relationships between its words. It is one of the earliest and most commonly used lemmatizer technique.
- It is present in the nltk library in python.
- Wordnet links words into semantic relations. ( eg. synonyms )
- It groups synonyms in the form of synsets.
- synsets : a group of data elements that are semantically equivalent.
How to use:
- Download nltk package : In your anaconda prompt or terminal, type:
pip install nltk
- Download Wordnet from nltk : In your pyhton console, do the following :
2. Wordnet Lemmatizer (with POS tag)
In the above approach, we observed that Wordnet results were not up to the mark. Words like ‘sitting’, ‘flying’ etc remained the same after lemmatization. This is because these words are treated as a noun in the given sentence rather than a verb. To overcome come this, we use POS (Part of Speech) tags.
We add a tag with a particular word defining its type (verb, noun, adjective etc).
Word + Type (POS tag) —> Lemmatized Word
driving + verb ‘v’ —> drive
dogs + noun ‘n’ —> dog
TextBlob is a python library used for processing textual data. It provides a simple API to access its methods and perform basic NLP tasks.
Download TextBlob package : In your anaconda prompt or terminal, type:
pip install textblob
4. TextBlob (with POS tag)
Same as in Wordnet approach without using appropriate POS tags, we observe the same limitations in this approach as well. So, we use one of the more powerful aspects of the TextBlob module the ‘Part of Speech’ tagging to overcome this problem.
Here is a link for all the types of tag abbreviations with their meanings. (scroll down for the tags table)
spaCy is an open-source python library that parses and “understands” large volumes of text. Separate models are available that cater to specific languages (English, French, German, etc.).
Download spaCy package :(a) Open anaconda promt or terminal as administrator and run the command:
pip install -U spacy(b) Now, open anaconda promt or terminal normally and run the command:
python -m spacy download enIf successful, you should see a message like: Linking successful C:\Anaconda3\envs\spacyenv\lib\site-packages\en_core_web_sm --> C:\Anaconda3\envs\spacyenv\lib\site-packages\spacy\data\en You can now load the model via
nlp = spacy.load('en_core_web_sm')
In the above code, we observed that this approach was more powerful than our previous approaches as :
- Even Pro-nouns were detected. ( identified by -PRON-)
- Even best was changed to good.
The TreeTagger is a tool for annotating text with part-of-speech and lemma information. The TreeTagger has been successfully used to tag over 25 languages and is adaptable to other languages if a manually tagged training corpus is available.
How to use: 1. Download TreeTagger package : In your anaconda prompt or terminal, type:
pip install treetaggerwrapper2. Download TreeTagger Software: Click on TreeTagger and download the software as per your OS. (Steps of installation given on website)
Pattern is a Python package commonly used for web mining, natural language processing, machine learning and network analysis. It has many useful NLP capabilities. It also contains a special feature which we will be discussing below.
How to use: Download Pattern package: In your anaconda prompt or terminal, type:
pip install pattern
NOTE : if the above code raises an error saying ‘generator raised StopIteration’. Just run it again. It will work after 3-4 tries.
Gensim is designed to handle large text collections using data streaming. Its lemmatization facilities are based on the pattern package we installed above.
- gensim.utils.lemmatize() function can be used for performing Lemmatization. This method comes under the utils module in python.
- We can use this lemmatizer from pattern to extract UTF8-encoded tokens in their base form=lemma.
- Only considers nouns, verbs, adjectives and adverbs by default (all other lemmas are discarded).
- For example
Word ---> Lemmatized Word are/is/being ---> be saw ---> see
How to use: 1. Download Pattern package: In your anaconda prompt or terminal, type:
pip install pattern2. Download Gensim package: Open your anaconda prompt or terminal as administrator and type:
pip install -U gensimOR
pip3 install -U gensim (if using python3)
NOTE : if the above code raises an error saying ‘generator raised StopIteration‘. Just run it again. It will work after 3-4 tries.
In the above code as you may have already noticed, the gensim lemmatizer ignore the words like ‘the’, ‘with’, ‘by’ as they did not fall into the 4 lemma categories mentioned above. (noun/verb/adjective/adverb)
9. Stanford CoreNLP
CoreNLP enables users to derive linguistic annotations for text, including token and sentence boundaries, parts of speech, named entities, numeric and time values, dependency and constituency parses, sentiment, quote attributions, and relations.
- CoreNLP is your one stop shop for natural language processing in Java!
- CoreNLP currently supports 6 languages, including Arabic, Chinese, English, French, German, and Spanish.
How to use: 1. Get JAVA 8 : Download Java 8 (as per your OS) and install it. 2. Get Stanford_coreNLP package : 2.1) Download Stanford_CoreNLP and unzip it. 2.2) Open terminal (a) go to the directory where you extracted the above file by doing cd C:\Users\...\stanford-corenlp-4.1.0 on terminal (b) then, start your Stanford CoreNLP server by executing the following command on terminal: java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -annotators "tokenize, ssplit, pos, lemma, parse, sentiment" -port 9000 -timeout 30000 **(leave your terminal open as long as you use this lemmatizer)** 3. Download Standford CoreNLP package: Open your anaconda prompt or terminal, type:
pip install stanfordcorenlp
So these are the various Lemmatization approaches that you can refer while working on an NLP project. The selection of the Lemmatization approach is solely dependent upon project requirements. Each approach has its set of pros and cons. Lemmatization is mandatory for critical projects where sentence structure matter like language applications etc.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Lemmatization with NLTK
- Python | Lemmatization with TextBlob
- Python | PoS Tagging and Lemmatization using spaCy
- Important differences between Python 2.x and Python 3.x with examples
- OOP in Python | Set 3 (Inheritance, examples of object, issubclass and super)
- OS Module in Python with Examples
- User-defined Exceptions in Python with Examples
- Lambda and filter in Python Examples
- colorsys module in Python with examples
- Python datetime module with examples
- Python Logical Operators with Examples
- Copy a directory recursively using Python (with examples)
- Regular Expression in Python with Examples | Set 1
- Timeit in Python with Examples
- reflect.IsNil() Function in Golang with Examples
- Non linear Regression examples - ML
- PyQtGraph - Extensive Examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Merge Python key values to list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.