In this Python NLP article we are going to learn about Wordnet And Synsets in NLTK,
Wordnet is a dictionary or word database for English language and it is mostly used for
Natural Language Processing(NLP). and Synset is used for searching of the words in the Wordnet.
Some of the words have only one Synset and some have several.
Learn How to remove Stopwords in Python NLTK.
Let’s check the Synset of hello word.
1 2 3 4 5 6 |
from nltk.corpus import wordnet as wn wn_hello = wn.synsets('hello') print(wn_hello) |
If you run the code you will see that we have just one Synset for hello word.
1 |
[Synset('hello.n.01')] |
Now we can find the definition and examples for this hello word.
1 2 3 4 5 6 7 8 9 10 |
from nltk.corpus import wordnet as wn wn_hello = wn.synsets('hello') hello = wn.synset('hello.n.01') print("Definition : ", hello.definition()) print("Name : ", hello.name()) print("Example : " , hello.examples()) |
If you run the code this will be the result.
1 2 3 |
Definition : an expression of greeting Name : hello.n.01 Example : ['every morning they exchanged polite hellos'] |
As i have said some of the words have only one Synset and some have several. now we
are going to check the clear word and you will see that there are different Synsets for the clear
word.
1 2 3 4 5 |
from nltk.corpus import wordnet as wn wn_clear = wn.synsets('clear') print(wn_clear) |
So if you run the code, you will see that there are different Synsets for the clear word.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[Synset('clear.n.01'), Synset('open.n.01'), Synset('unclutter.v.01'), Synset('clear.v.02'), Synset('clear_up.v.04'), Synset('authorize.v.01'), Synset('clear.v.05'), Synset('pass.v.09'), Synset('clear.v.07'), Synset('clear.v.08'), Synset('clear.v.09'), Synset('clear.v.10'), Synset('clear.v.11'), Synset('clear.v.12'), Synset('net.v.02'), Synset('net.v.01'), Synset('gain.v.08'), Synset('clear.v.16'), Synset('clear.v.17'), Synset('acquit.v.01'), Synset('clear.v.19'), Synset('clear.v.20'), Synset('clear.v.21'), Synset('clear.v.22'), Synset('clear.v.23'), Synset('clear.v.24'), Synset('clear.a.01'), Synset('clear.s.02'), Synset('clear.s.03'), Synset('clear.a.04'), Synset('clear.s.05'), Synset('clear.s.06'), Synset('clean.s.03'), Synset('clear.s.08'), Synset('clear.s.09'), Synset('well-defined.a.02'), Synset('clear.a.11'), Synset('clean.s.02'), Synset('clear.s.13'), Synset('clear.s.14'), Synset('clear.s.15'), Synset('absolved.s.01'), Synset('clear.s.17'), Synset('clear.r.01'), Synset('clearly.r.04')] |
You can find the definition for the specific Synset.
1 2 3 4 5 |
from nltk.corpus import wordnet as wn clear_07 = wn.synset('clear.v.07') print(clear_07.definition()) |
Let’s just find the definitions in a range.
1 2 3 4 5 6 7 8 9 10 11 |
from nltk.corpus import wordnet as wn wn_clear = wn.synsets('clear') for i in range(0,7): clear = wn_clear[i] print("Name : " , clear.name()) print("Definition : ", clear.definition()) print("Example : ", clear.examples()) |
So run the code this will be the result.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Name : clear.n.01 Definition : the state of being free of suspicion Example : ['investigation showed that he was in the clear'] Name : open.n.01 Definition : a clear or unobstructed space or expanse of land or water Example : ['finally broke out of the forest into the open'] Name : unclutter.v.01 Definition : rid of obstructions Example : ['Clear your desk'] Name : clear.v.02 Definition : make a way or path by removing objects Example : ['Clear a path through the dense forest'] Name : clear_up.v.04 Definition : become clear Example : ['The sky cleared after the storm'] Name : authorize.v.01 Definition : grant authorization or clearance for Example : ['Clear the manuscript for publication', 'The rock star never authorized this slanderous biography'] Name : clear.v.05 Definition : remove Example : ['clear the leaves from the lawn', 'Clear snow from the road'] |
Finding Antonyms | Python NLP – Wordnet And Synsets in NLTK
In this example we are going to learn how to find antonyms in Wordnet. so Antonyms are
words that have contrasting, or opposite, meanings, for example good and bad.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
from nltk.corpus import wordnet antonyms = [] for syn in wordnet.synsets('bad'): for l in syn.lemmas(): if l.antonyms(): antonyms.append(l.antonyms()[0].name()) print(antonyms) |
Now if you run the code, this is the result and the antonyms of bad is good, goodness.
1 |
['good', 'goodness', 'good', 'unregretful'] |
Subscribe and Get Free Video Courses & Articles in your Email
Comments are closed.