## Machine Learning Tutorial

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. WordNet establishes links between words that are related to each other. It doesn’t simply establish a link based on form or letters in the words. Due to this ability of WordNet, words that are similar to each other lie in the same or nearby semantic network, without any ambiguity. It labels semantic relationship among words. Downloading wordnet 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') Importing 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'] Extracting synonyms and antonyms for a specific word 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'} Conclusion In this post, we understood how synonyms and antonyms can be obtained with the help of WordNet in Python.

# How to get synonyms/antonyms from NLTK WordNet in Python?

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.

• WordNet establishes links between words that are related to each other. It doesn’t simply establish a link based on form or letters in the words. Due to this ability of WordNet, words that are similar to each other lie in the same or nearby semantic network, without any ambiguity.
• It labels semantic relationship among words.

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') 

### Importing 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']

### Extracting synonyms and antonyms for a specific word

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'} 

#### Conclusion

In this post, we understood how synonyms and antonyms can be obtained with the help of WordNet in Python.

#### Pavan Kumar Reddy B

Test

Wow what great post about machine learning I never read such a blog before, this is very interesting!