The WordNet is a part of Python's Natural Language Toolkit. It is a large word database of English Nouns, Adjectives, Adverbs and Verbs. These are grouped into some set of cognitive synonyms, which are called synsets.
To use the Wordnet, at first we have to install the NLTK module, then download the WordNet package.
$ sudo pip3 install nltk $ python3 >>> import nltk >>>nltk.download('wordnet')
In the wordnet, there are some groups of words, whose meaning are same.
In the first example, we will see how wordnet returns meaning and other details of a word. Sometimes, if some examples are available, it may also provide that.
Example code
from nltk.corpus import wordnet #Import wordnet from the NLTK synset = wordnet.synsets("Travel") print('Word and Type : ' + synset[0].name()) print('Synonym of Travel is: ' + synset[0].lemmas()[0].name()) print('The meaning of the word : ' + synset[0].definition()) print('Example of Travel : ' + str(synset[0].examples()))
Output
$ python3 322a.word_info.py Word and Type : travel.n.01 Synonym of Travel is: travel The meaning of the word : the act of going from one place to another Example of Travel : ['he enjoyed selling but he hated the travel'] $
In the previous example, we are getting detail information about some words. Here we will see how wordnet can send the synonyms and antonyms of a given word.
Example code
import nltk from nltk.corpus import wordnet #Import wordnet from the NLTK syn = list() ant = list() for synset in wordnet.synsets("Worse"): for lemma in synset.lemmas(): syn.append(lemma.name()) #add the synonyms if lemma.antonyms(): #When antonyms are available, add them into the list ant.append(lemma.antonyms()[0].name()) print('Synonyms: ' + str(syn)) print('Antonyms: ' + str(ant))
Output
$ python3 322b.syn_ant.py Synonyms: ['worse', 'worse', 'worse', 'worsened', 'bad', 'bad', 'big', 'bad', 'tough', 'bad', 'spoiled', 'spoilt', 'regretful', 'sorry', 'bad', 'bad', 'uncollectible', 'bad', 'bad', 'bad', 'risky', 'high-risk', 'speculative', 'bad', 'unfit', 'unsound', 'bad', 'bad', 'bad', 'forged', 'bad', 'defective', 'worse'] Antonyms: ['better', 'better', 'good', 'unregretful'] $
The NLTK wordnet has another great feature, by using it we can check whether two words are nearly equal or not. It will return the similarity ratio from a pair of words.
Example code
import nltk from nltk.corpus import wordnet #Import wordnet from the NLTK first_word = wordnet.synset("Travel.v.01") second_word = wordnet.synset("Walk.v.01") print('Similarity: ' + str(first_word.wup_similarity(second_word))) first_word = wordnet.synset("Good.n.01") second_word = wordnet.synset("zebra.n.01") print('Similarity: ' + str(first_word.wup_similarity(second_word)))
Output
$ python3 322c.compare.py Similarity: 0.6666666666666666 Similarity: 0.09090909090909091 $