Stemming and Lemmatization: NLP Tutorial For Beginners - S1 E10

codebasics · Beginner ·📊 Data Analytics & Business Intelligence ·4y ago

Key Takeaways

The video demonstrates stemming and lemmatization techniques in NLP using NLTK library and Porter Stemmer, with a focus on reducing words to their base form using simple rules and linguistic knowledge. The tutorial covers the installation of NLTK, importing necessary libraries, and applying stemming and lemmatization to words using Porter Stemmer and Snowball Stemmer.

Full Transcript

today we are talking about stemming and limitization and these are the essential steps that you need to perform in pre-processing stage while building nlp application you might have noticed when you search in google with some word it might search for some other words as well which are matching with that particular word which is here i'm talking about talking and then it will match a word talk so what we are doing here is essentially reducing the word to its base word and not only like google search is just one primary example that i gave you for your understanding but even in text classification let's say you are doing sentiment classification and you are using some words such as talking during the classification review product review then if someone has used the base word which is talk you know talking talk talked in past tense they all mean the same thing so you want to map all those words to its its base word there is a value in reducing a word to its base word i am giving some other examples here eating to eat it is a past tense but if you can map that to eat which is the present tense the base word then it can have a value in your nlp application similarly adjustable to adjust you'll notice that to get the base word of a given word sometimes you can use simple rule such as if the word has ing as a suffix just remove ing talking walking running if you remove ing you get the base word eating there are other suffixes as well such as adjustable mi cable remarkable so if you remove able then you get the base word so the point i want to make here is by using simple rules without using any language knowledge just by using simple rules that if the word has ing remove ing or removable you can have a list of these suffixes and you can use the rule that remove these suffixes from a given word to get a base word since we are using fixed rules or heuristic this process is called stemming it's like stemming you're just stamming the given word and getting the base word but on some other occasions applying just the fixed rule is not going to work it to eat you can't apply this rule you have to have that knowledge of that language so whenever you want to use knowledge of the language or a linguistic knowledge jargon alert linguistic knowledge is nothing but a knowledge of a language when you want to use that knowledge of a language and you get the base word that base word by the way is also called lemma so eat is a lemma for eight and this process is called lemmatization so stemming is just to summarize using fixed set of rules dumb rules that remove this x number of prefixes or suffixes to get a base word whereas limitation is little more sophisticated where you need need to know the rules of that particular language you need to have linguistic knowledge you might be guessing already that with stemming you can get into issues sometimes for example if your word ability if you just apply the dumb rules it will remove iity and you will get this base word abil which doesn't mean anything whereas if you apply limitation you will get the same word ability but as you learn more about nlp application you will realize that there is a value of both so stemming and limitation both are used and we are going to write some code by the way for stemming we will use nltk because spacey doesn't have support for stemming there is an article where spacey authors have mentioned why they don't support stemming because limitation as you can already see is more sophisticated more correct so they just have limitation support but nldk has both stemming and limitation both they support both so i'm going to show you anal decay and i will demonstrate stemming i want to take a moment and talk about first language dot in which makes building nlp applications easier for you you can perform various nlp tasks which are shown here via this platform if you're using a spacey type of library you download it you run your code locally if you are training a heavy model you need higher compute resources such as gpus whereas here everything is in cloud you just make an http call things run on the cloud you don't need a local machine with lot of computer resources there is a demo which you can see by clicking on this link and i have bought this bulb recently and you see there is a negative review on amazon and when i copy paste this here and i select text classification and when i say submit it immediately classifies that as negative and if i have a positive review i just copy paste it here [Music] and if i submit it it will classify this as a positive see it's pretty powerful so it makes uh things much easier for you you don't need to have like too much like detailed nano pin knowledge you can just call apis and get things done quickly for yourself the free tire is something you can try today you can just sign in and you can get an api access once you sign in if you go to dashboard you will get your own api key and you can use that api key to make the calls they have sdks available both in python and typescript go check it out the link is in video description below and thanks first language dot in for sponsoring this video so let's go to a notebook so i imported analytics pc and by the way if you don't have analytic install you can in your command prompt where is my command prompt in your command prompt you can just run pip install nltk very basic right to install it i have already imported both of these libraries and now i'm going to [Music] import porter stammer from nltk so from nlt [Music] and ltk dot stem import porter you can just hit tab to auto complete and stammer this is a class so i'm creating an object of this class in python when you do this this is a class and you're creating an object of this class storing it here there is other uh stammer as well called snowball stemmer but i'm just going to show you one and i have a couple of words here and you can say for word in words print so i'll print a word and then sum separation okay and stemmer.stem that particular word and it prints the the it does the stemming and it prints the base word so you you see it applied fixed set of rules so from it removed ing got it then when it has eats it will remove s and it if you have a base word it will not do anything so it is it but not is this it it did not do anything because stammer doesn't have a knowledge of a language it is just using fixed rules adjustable it is saying adjust drafting rat look at this abilityable apple doesn't mean anything but still you will notice that many applications even production applications they use nltk and using stammer could be efficient because it is faster it just applies bunch of rules it doesn't require any language knowledge and it can have a value in your nlp application so although it is dumb it can have a value now let's do limit adjacent in spaces so this was a quick demo of stemming in nltk so as usual so i'm going to now create nlp object and say spacey dot load en core web sm is small you can use medium i'm going to use small version of english language processing pipeline if you don't know what i'm talking about you have to watch previous video and then i will create some document so let's use the same word here and see in space it's pretty straightforward you can just say for word or for token in a doc print token some separator and you can say token dot lemma underscore notice this ate it reduces to eat eating eat see it worked fine adjustable it did not say just because the limitation the rules which are set by default english model says that the base word for adjustable is adjustable ability is the ability but look at this when you have better it reduced that word too well so it has this mapping rules in the trained model that you just loaded so based on the language based on what kind of model you're loading you might be loading a different english model which will have a different rule so there is this is a quick demo of limited edition in spacey if you by the way let me show you one more thing which is this this thing called token dot lemma so see there is lemma and there is lemon underscore so lemma is just printing a hash so it's a unique hash of your lemma which is a base word because this is a train english model with a fixed vocabulary it has few thousand words and for each word it will have a unique identifier short of and that is this hash and you'll see that for eat the hash is same this is just for your knowledge by the way i just wanted to quickly show you different uh sentence so i'll just do copy paste and i will try i don't know if you all watch mandalorian mandarin is my favorite series on disney so mendo doesn't talk that much but mendo talked for three hours hooray mendo is becoming talkative mendo for three hours although talking isn't his thing he became talkative and i will remove so sit talk it reduces to talk talking is also reduced to talk but talkative is still talkative because this is this is how as i said this is how this particular model behaves sometimes you might want to modify a behavior right you want to customize it so how do you do that so if you remember our pipe names so we have for this particular model the pipeline that we have is this there's a talk to actor tagger parser attribute ruler and then there is a lemmatizer now attribute ruler assigns attribute to a particular token and you can customize it so let me explain what what i mean here so let's say you have this sentence now bro all these are the [Music] you know you mean brother basically when you say this but by default the language model doesn't understand this is slang right so it doesn't understand so it will say bro is bro bra is bra right and if you look at the first token itself so like the first token is this correct bro and if you look at the lemma of that it is bro because it doesn't understand the slang but let's say you want to customize this model you know that bro and bra means brother you can customize it by doing this so what you do is you go here you get this particular attribute ruler by doing this so you say dot get pipe attribute ruler so it will give you that particular component from the pipeline and then you can customize it by adding that custom rule okay so see here i am adding a custom rule where i am saying this so what i want to say is i want lemma to be brother for few words so for bro and bra i want their lemar to be brother okay so here you are assigning attribute and attribute is lemma so basically you're saying the lemma for the words that i'm going to specify here and the way you specify those words i mean that is this i'm just going to copy paste using for brother bro and bra make their base word to be brother so you notice previously bro was bro and bra was bra but when i run this again bro is brother and bra is also brother and for the bro see the lemma was broke but now the lemma became brother so i hope this gave you uh some understanding of stemming and limitization i don't have an exercise for this particular video but i might add exercises in the future as well so always check the video description below because sometimes if i don't find time for the exercise while recording this video and if i find time later on i add those exercises and i i talk about those exercises in the video description below so always when you are watching my videos on my channel any video make sure you check video description because i might have included more information after i publish the video if i find some correction if i want to add some exercises some additional information articles i always add them in the video description so make sure you check it out i hope you're liking this series if you do please give it a thumbs up and share it with your friends one of the way you can share this series is by posting on linkedin linkedin there are some serious folks so you can post about this series so that other people learning nlp can also benefit thank you very much for watching if you have any question post in a comment box below

Original Description

Stemming and lemmatization are two popular techniques to reduce a given word to its base word. Stemming uses a fixed set of rules to remove suffixes, and prefixes whereas lemmatization use language knowledge to come up with a correct base word. Stemming will be demonstrated in ntlk (spacy doesn't support stemming) whereas code for lemmatization is written in spacy NLP platform: https://www.firstlanguage.in/ Code: https://github.com/codebasics/nlp-tutorials/blob/main/6_stemming_lematization/6_stemming_lematization.ipynb Exercise: https://github.com/codebasics/nlp-tutorials/blob/main/6_stemming_lematization/stemming_and_lemmatization_exercise.ipynb Complete NLP Playlist: https://www.youtube.com/playlist?list=PLeo1K3hjS3uuvuAXhYjV2lMEShq2UYSwX 🔖Hashtags🔖 #nlp #nlptutorial #nlppython #spacytutorial #spacytutorialnlp #nlptutorialpython #naturallanguageprocessingstemming #nlpstemming #nlpstemmingtutorial #stemming #lemmatization Do you want to learn technology from me? Check https://codebasics.io/?utm_source=description&utm_medium=yt&utm_campaign=description&utm_id=description for my affordable video courses. Need help building software or data analytics and AI solutions? My company https://www.atliq.com/ can help. Click on the Contact button on that website. 🎥 Codebasics Hindi channel: https://www.youtube.com/channel/UCTmFBhuhMibVoSfYom1uXEg #️⃣ Social Media #️⃣ 🔗 Discord: https://discord.gg/r42Kbuk 📸 Instagram: https://www.instagram.com/codebasicshub/ 🔊 Facebook: https://www.facebook.com/codebasicshub 📱 Twitter: https://twitter.com/codebasicshub 📝 Linkedin (Personal): https://www.linkedin.com/in/dhavalsays/ 📝 Linkedin (Codebasics): https://www.linkedin.com/company/codebasics/ 🔗 Patreon: https://www.patreon.com/codebasics?fan_landing=true ❗❗ DISCLAIMER: All opinions expressed in this video are of my own and not that of my employers'.
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from codebasics · codebasics · 0 of 60

← Previous Next →
1 Python Tutorial - 1. Install python on windows
Python Tutorial - 1. Install python on windows
codebasics
2 Python Tutorial - 2. Variables
Python Tutorial - 2. Variables
codebasics
3 Python Tutorial - 3. Numbers
Python Tutorial - 3. Numbers
codebasics
4 Python Tutorial - 4. Strings
Python Tutorial - 4. Strings
codebasics
5 Python Tutorial - 5. Lists
Python Tutorial - 5. Lists
codebasics
6 Python Tutorial - 6. Install PyCharm on Windows
Python Tutorial - 6. Install PyCharm on Windows
codebasics
7 PyCharm Tutorial - 7. Debug python code using PyCharm
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
8 Python Tutorial -  8. If Statement
Python Tutorial - 8. If Statement
codebasics
9 Python Tutorial - 9. For loop
Python Tutorial - 9. For loop
codebasics
10 Python Tutorial -  10. Functions
Python Tutorial - 10. Functions
codebasics
11 Python Tutorial - 11. Dictionaries and Tuples
Python Tutorial - 11. Dictionaries and Tuples
codebasics
12 Python Tutorial - 12. Modules
Python Tutorial - 12. Modules
codebasics
13 Python Tutorial - 13. Reading/Writing Files
Python Tutorial - 13. Reading/Writing Files
codebasics
14 How to install Julia on Windows
How to install Julia on Windows
codebasics
15 Python Tutorial - 14. Working With JSON
Python Tutorial - 14. Working With JSON
codebasics
16 Julia Tutorial - 1. Variables
Julia Tutorial - 1. Variables
codebasics
17 Julia Tutorial - 2. Numbers
Julia Tutorial - 2. Numbers
codebasics
18 Python Tutorial - 15. if __name__ == "__main__"
Python Tutorial - 15. if __name__ == "__main__"
codebasics
19 Julia Tutorial - Why Should I Learn Julia Programming Language
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
20 Python Tutorial  - 16. Exception Handling
Python Tutorial - 16. Exception Handling
codebasics
21 Julia Tutorial - 3. Complex and Rational Numbers
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
22 Julia Tutorial - 4. Strings
Julia Tutorial - 4. Strings
codebasics
23 Python Tutorial -  17. Class and Objects
Python Tutorial - 17. Class and Objects
codebasics
24 Julia Tutorial - 5. Functions
Julia Tutorial - 5. Functions
codebasics
25 Julia Tutorial - 6. If Statement and Ternary Operator
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
26 Julia Tutorial - 7. For While Loop
Julia Tutorial - 7. For While Loop
codebasics
27 Python Tutorial  - 18. Inheritance
Python Tutorial - 18. Inheritance
codebasics
28 Julia Tutorial - 8. begin and (;) Compound Expressions
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
29 Python Tutorial - 12.1 - Install Python Module (using pip)
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
30 Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
31 Julia Tutorial - 10. Exception Handling
Julia Tutorial - 10. Exception Handling
codebasics
32 Python Tutorial  - 19. Multiple Inheritance
Python Tutorial - 19. Multiple Inheritance
codebasics
33 Python Tutorial - 20. Raise Exception And Finally
Python Tutorial - 20. Raise Exception And Finally
codebasics
34 Python Tutorial - 21. Iterators
Python Tutorial - 21. Iterators
codebasics
35 Python Tutorial - 22. Generators
Python Tutorial - 22. Generators
codebasics
36 Python Tutorial - 23. List Set Dict Comprehensions
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
37 Python Tutorial - 24. Sets and Frozen Sets
Python Tutorial - 24. Sets and Frozen Sets
codebasics
38 Python Tutorial - 25. Command line argument processing using argparse
Python Tutorial - 25. Command line argument processing using argparse
codebasics
39 Debugging Tips - What is bug and debugging?
Debugging Tips - What is bug and debugging?
codebasics
40 Debugging Tips - Conditional Breakpoint
Debugging Tips - Conditional Breakpoint
codebasics
41 Debugging Tips - Watches and Call Stack
Debugging Tips - Watches and Call Stack
codebasics
42 Python Tutorial - 26. Multithreading - Introduction
Python Tutorial - 26. Multithreading - Introduction
codebasics
43 Git Tutorial 3:  How To Install Git
Git Tutorial 3: How To Install Git
codebasics
44 Git Tutorial 1: What is git / What is version control system?
Git Tutorial 1: What is git / What is version control system?
codebasics
45 Git Tutorial 2 : What is Github? | github tutorial
Git Tutorial 2 : What is Github? | github tutorial
codebasics
46 Git Tutorial 4: Basic Commands: add, commit, push
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
47 Git Tutorial 5: Undoing/Reverting/Resetting code changes
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
48 Git Tutorial 6: Branches (Create, Merge, Delete a branch)
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
49 Git Github Tutorial 10: What is Pull Request?
Git Github Tutorial 10: What is Pull Request?
codebasics
50 Git Tutorial 7: What is HEAD?
Git Tutorial 7: What is HEAD?
codebasics
51 Git Tutorial 9: Diff and Merge using meld
Git Tutorial 9: Diff and Merge using meld
codebasics
52 Difference between Multiprocessing and Multithreading
Difference between Multiprocessing and Multithreading
codebasics
53 Python Tutorial - 27. Multiprocessing Introduction
Python Tutorial - 27. Multiprocessing Introduction
codebasics
54 Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
55 Git Tutorial 8 - .gitignore file
Git Tutorial 8 - .gitignore file
codebasics
56 Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
57 Python Tutorial - 30. Multiprocessing Lock
Python Tutorial - 30. Multiprocessing Lock
codebasics
58 Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
59 What is code?
What is code?
codebasics
60 Python unit testing - pytest introduction
Python unit testing - pytest introduction
codebasics

This video tutorial covers the basics of stemming and lemmatization in NLP, including the installation of NLTK, applying stemming and lemmatization to words, and customizing language models. The tutorial provides a practical introduction to NLP techniques and tools. By the end of this tutorial, viewers will be able to apply stemming and lemmatization to words and understand the basics of NLP.

Key Takeaways
  1. Install NLTK library using pip install nltk
  2. Import necessary libraries and classes
  3. Create an object of the Porter Stemmer class
  4. Use the stem method to apply stemming to words
  5. Use the lemma method to apply lemmatization to words
  6. Assign attribute to a particular token
  7. Customize the attribute ruler by adding a custom rule
  8. Run the pipeline again to see the changes
💡 Stemming and lemmatization are two popular techniques in NLP that can be used to reduce words to their base form, with stemming using simple rules and lemmatization using linguistic knowledge.

Related Reads

Up next
This could be the most perfect data frontend
Matt Williams
Watch →