Before jumping into the code, it is essential to know what WordNet is and its significance.
Wordnet is a lexical database of English which is very large in size. It belongs to the NLTK corpus and it consists of parts of speech of the English language such as nouns, verbs, adverbs, adjectives, that are grouped into different sets of cognitive synonyms.
It is used to group the words of English into sets of similar meaning words, and this is known as ‘synset’.
WordNet can be used in conjunction with NLTK for a variety of purposes.
Before attempting to use wordnet, it has to be downloaded from the nltk corpus. The below lines of code help in doing so.
import nltk nltk.download('wordnet')
from nltk.corpus import wordnet
Finding syset of the word ‘discipline’
This can be done by executing the below lines of code.
from nltk.corpus import wordnet syns = wordnet.synsets("Discipline") print(syns[0].name())
Output:
discipline.n.01
Once these lines are executed, the below lines can be executed. This will give the root word, i.e. the word whose synonym needs to be found.
print(syns[0].lemmas()[0].name())
Output:
Discipline
The definition of the word can be obtained by calling the ‘definition’ function as shown below.
print(syns[0].definition())
Output:
a branch of knowledge
Examples of using the word in different sentences
print(syns[0].examples())
Output:
['in what discipline is his doctorate?', 'teachers should be well trained in their subject', 'anthropology is the study of human beings']
synonyms = [] antonyms = [] for syn_set in wordnet.synsets("bright"): for l in syn_set.lemmas(): synonyms.append(l.name()) if l.antonyms(): antonyms.append(l.antonyms()[0].name()) print(set(synonyms)) print(set(antonyms))
Output:
{'brilliant', 'vivid', 'brilliantly', 'brightly', 'burnished', 'shining', 'bright', 'lustrous', 'hopeful', 'promising', 'smart', 'undimmed', 'shiny'} {'dull', 'dimmed'}
In this post, we understood how synonyms and antonyms can be obtained with the help of WordNet in Python.
After reading your article, I was amazed. I know that you explain it very well. And I hope that other readers will also experience how I feel after reading your article. Thanks for sharing.
Good and informative article.
I enjoyed reading your articles. This is truly a great read for me. Keep up the good work!
Awesome blog. I enjoyed reading this article. This is truly a great read for me. Keep up the good work!
Thanks for sharing this article!! Machine learning is a branch of artificial intelligence (AI) and computer science that focus on the uses of data and algorithms. I came to know a lot of information from this article.
Leave a Reply
Your email address will not be published. Required fields are marked *