Updated March 29, 2023
Definition of NLTK WordNet
Nltk wordnet is an English lexical database (dictionary) built primarily for NLP. Synset is a specific type of simple interface used in NLTK that allows users to search WordNet for words. Synset examples are collections of words that are synonymous and express the same idea. Some words have a single Synset, whereas others have multiple Synsets. WordNet is a focused English dictionary with a more complex structure than a typical thesaurus.
What is NLTK WordNet?
- Wordnet is an English database for lexical which was based on the NLTK corpus reader. It can be used to look for word definitions, synonyms, and antonyms.
- It’s best described as an English dictionary with a semantic focus. The import command is used to bring it into the system. Because Wordnet is a corpus, it is pulled from the ntlk.corpus directory.
- Both the synonym and antonym lists are left blank, and they will be appended later. The module synsets search for active words of synonyms and add them to the synonyms list. It is very useful in python programming language.
How to use NLTK WordNet?
WordNet is a Python dictionary that is part of the NLTK. This is a large library aimed at making NLP simple.
- To use words nltk corpus we need to follow the below steps are as follows.
1) Install nltk by using pip command – The first step is to install nltk by using the pip command. Below examples shown to install nltk by using the pip command are as follows.
pip install nltk
2) After installing the pip command we are login into the python shell by using the python command to execute the code are as follows.
python
3) After login into the python shell in this step we are importing the wordnet module by using nltk.corpus library. The below example shows the import of the wordnet module is as follows.
from nltk.corpus import wordnet
4) Synsets are a collection of related words in WordNet. A name is all assigned to each Synset. Lemmas are the words found in a Synset. The function wordnet.synsets (‘word’) provides an array containing all of the Synsets associated with the word put in as an argument. The below example shows how to use synsets by using wordnet are as follows.
print(wordnet.synsets('cat'))
- In above example, we have used “cat” word by using synsets. The synsets method is returning the technique yielded four Synsets, four of which are nouns with the name cat. The results also indicate that the word cat has ten different meanings or situations.
5) In this step we are using a single Synset can be further investigated for Lemmas it includes by using the definition. The typical definition of this method is that it returns a string. There are two options for doing this. We can access one of the entries in the synsets (‘word’).
py_arr = wordnet.synsets("cat")
print(py_arr[0].definition())
6) In this step we are using definition after passing the Synset’s name, part of speech, and number to a synset. In below example we are using the “cat” word with synset are as follows.
wordnet.synset('cat.n.02').definition()
7) In this step we are using all lemmas of a synset. Similar to the lemma_names () is used to return all lemma names of the array. The below example shows lemma_names with synset are as follows.
print (py_arr[0].lemma_names())
8) In this step we are using hyponyms. In inheritance, it can be compared to a child’s class.
print(wordnet.synset('cat.n.02').hyponyms())
9) In this step we are using hypernym. A Synset can be expanded into a Hypernym. Hypernyms returns an array that contains all Hypernyms in a Synset.
print (wordnet.synset ('cat.n.01').hypernyms())
NLTK WordNet Examples
- WordNet can be used in conjunction with a module of nltk to look up word definitions, synonyms, and antonyms, among other things.
1) Below example shows nltk wordnet are as follows.
Code:
from nltk.corpus import wordnet
py_arr = wordnet.synsets("python")
print (py_arr[0].name())
print (py_arr[0].lemmas()[0].name())
print (py_arr[0].definition())
print (py_arr[0].examples())
- In the above example, in the first line, we have imported the wordnet module by using nltk.corpus module. In second line we have using the python word to find the synsets. In the fourth line, we have defined the example of a synset.
2) Below example shows definition, examples, and name function by using synsets. In nltk wordnet, lemmas are the synonyms and we can identify the antonyms.
Code:
from nltk.corpus import wordnet
py_syn = wordnet.synsets('python')[0]
print ("nltk wordnet : ", py_syn.name())
print ("\n Meaning of synnset: ", py_syn.definition())
print ("\n Examples of synset : ", py_syn.examples())
- In above example first line we have imported the wordnet module by using nltk.corpus module. In the second line, we have created the object name as py_syn. Also, we have used synsets methods with word as python. In the third line, we have defining the name function with the object of py_syn. Then we are using the definition method to print the synsets value. In the output, we can see by using the definition method will show the synsets meaning.
3) Synsets are arranged in an inheritance tree-like manner. Up to a root hypernym. Hypernyms are a means to group and categorize their resemblance. Below example shows use of hyponyms and Hypernerms.
Code:
from nltk.corpus import wordnet
py_syn = wordnet.synsets ('python') [0]
print ("Name of synset : ", py_syn.name ())
print ("\n Term of synsets : ", py_syn.hypernyms ())
print ("\n Specific term synset : ", py_syn.hypernyms()[0].hyponyms())
py_syn.root_hypernyms ()
print ("\n Hypernyms of synset: ", py_syn.root_hypernyms())
4) Below example shows part of speech by using synset are as follows.
Code:
py_syn = wordnet.synsets('Python')[0]
print ("Nltk wordnet : ", py_syn.pos())
py_syn = wordnet.synsets('nltk')[0]
print ("Nltk wordnet : ", py_syn.pos())
py_syn = wordnet.synsets('wordnet')[0]
print ("Nltk wordnet : ", py_syn.pos())
py_syn = wordnet.synsets('example')[0]
print ("Nltk wordnet : ", py_syn.pos())
Conclusion
Wordnet is English database for lexical which was based on the NLTK corpus reader. Nltk wordnet is an English lexical database (dictionary) built primarily for NLP. Synset is a specific type of simple interface used in NLTK that allows users to search WordNet for words.
Recommended Articles
This is a guide to NLTK WordNet. Here we discuss the Definition, What is NLTK WordNet, examples with implementation. You may also have a look at the following articles to learn more –