Wordnet is a large lexical database of English, which was created by Princeton. It is a part of the NLTK corpus. Nouns, verbs, adjectives and adverbs all are grouped into set of synsets, i.e., cognitive synonyms. Here each set of synsets express a distinct meaning. Following are some use cases of Wordnet −
Wordnet can be imported with the help of following command −
from nltk.corpus import wordnet
For more compact command, use the following −
from nltk.corpus import wordnet as wn
Synset are groupings of synonyms words that express the same concept. When you use Wordnet to look up words, you will get a list of Synset instances.
To get a list of Synsets, we can look up any word in Wordnet by using wordnet.synsets(word). For example, in next Python recipe, we are going to look up the Synset for the ‘dog’ along with some properties and methods of Synset −
First, import the wordnet as follows −
from nltk.corpus import wordnet as wn
Now, provide the word you want to look up the Synset for −
syn = wn.synsets('dog')[0]
Here, we are using name() method to get the unique name for the synset which can be used to get the Synset directly −
syn.name() Output: 'dog.n.01'
Next, we are using definition() method which will give us the definition of the word −
syn.definition() Output: 'a member of the genus Canis (probably descended from the common wolf) that has been domesticated by man since prehistoric times; occurs in many breeds'
Another method is examples() which will give us the examples related to the word −
syn.examples() Output: ['the dog barked all night']
from nltk.corpus import wordnet as wn syn = wn.synsets('dog')[0] syn.name() syn.definition() syn.examples()
Synsets are organized in an inheritance tree like structure in which Hypernyms represents more abstracted terms while Hyponyms represents the more specific terms. One of the important things is that this tree can be traced all the way to a root hypernym. Let us understand the concept with the help of the following example −
from nltk.corpus import wordnet as wn syn = wn.synsets('dog')[0] syn.hypernyms()
[Synset('canine.n.02'), Synset('domestic_animal.n.01')]
Here, we can see that canine and domestic_animal are the hypernyms of ‘dog’.
Now, we can find hyponyms of ‘dog’ as follows −
syn.hypernyms()[0].hyponyms()
[ Synset('bitch.n.04'), Synset('dog.n.01'), Synset('fox.n.01'), Synset('hyena.n.01'), Synset('jackal.n.01'), Synset('wild_dog.n.01'), Synset('wolf.n.01') ]
From the above output, we can see that ‘dog’ is only one of the many hyponyms of ‘domestic_animals’.
To find the root of all these, we can use the following command −
syn.root_hypernyms()
[Synset('entity.n.01')]
From the above output, we can see it has only one root.
from nltk.corpus import wordnet as wn syn = wn.synsets('dog')[0] syn.hypernyms() syn.hypernyms()[0].hyponyms() syn.root_hypernyms()
[Synset('entity.n.01')]
In linguistics, the canonical form or morphological form of a word is called a lemma. To find a synonym as well as antonym of a word, we can also lookup lemmas in WordNet. Let us see how.
By using the lemma() method, we can find the number of synonyms of a Synset. Let us apply this method on ‘dog’ synset −
from nltk.corpus import wordnet as wn syn = wn.synsets('dog')[0] lemmas = syn.lemmas() len(lemmas)
3
The above output shows ‘dog’ has three lemmas.
Getting the name of the first lemma as follows −
lemmas[0].name() Output: 'dog'
Getting the name of the second lemma as follows −
lemmas[1].name() Output: 'domestic_dog'
Getting the name of the third lemma as follows −
lemmas[2].name() Output: 'Canis_familiaris'
Actually, a Synset represents a group of lemmas that all have similar meaning while a lemma represents a distinct word form.
In WordNet, some lemmas also have antonyms. For example, the word ‘good ‘has a total of 27 synets, among them, 5 have lemmas with antonyms. Let us find the antonyms (when the word ‘good’ used as noun and when the word ‘good’ used as adjective).
from nltk.corpus import wordnet as wn syn1 = wn.synset('good.n.02') antonym1 = syn1.lemmas()[0].antonyms()[0] antonym1.name()
'evil'
antonym1.synset().definition()
'the quality of being morally wrong in principle or practice'
The above example shows that the word ‘good’, when used as noun, have the first antonym ‘evil’.
from nltk.corpus import wordnet as wn syn2 = wn.synset('good.a.01') antonym2 = syn2.lemmas()[0].antonyms()[0] antonym2.name()
'bad'
antonym2.synset().definition()
'having undesirable or negative qualities’
The above example shows that the word ‘good’, when used as adjective, have the first antonym ‘bad’.