{"id":1642,"date":"2022-03-17T11:05:04","date_gmt":"2022-03-17T11:05:04","guid":{"rendered":"https:\/\/jenniferkwentoh.com\/?p=1642"},"modified":"2023-04-03T11:56:55","modified_gmt":"2023-04-03T11:56:55","slug":"nltk-wordnet-python","status":"publish","type":"post","link":"https:\/\/jenniferkwentoh.com\/nltk-wordnet-python\/","title":{"rendered":"NLTK WordNet: Synonyms, Antonyms, Hypernyms [Python Examples]"},"content":{"rendered":"\n

What is Wordnet?<\/h2>\n\n\n\n

WordNet is a large lexical database for words in the English language. It provides synonyms, definitions, and other useful information and relations between synonyms.<\/p>\n\n\n\n

Wordnet groups English words into sets of synonyms called Synsets.<\/strong> It can be seen as a semantic database used to find synonyms, antonyms, part of speech information and related words in English.<\/p>\n\n\n\n

Wordnet has been used extensively in computational linguistics, cognitive psychology, and machine translation research.<\/p>\n\n\n\n

What are Synonyms in NLP?<\/h2>\n\n\n\n

Synonyms<\/strong> are words that have the same or similar meaning. They can be used in different contexts and with different meanings but they still have the same underlying meaning. Synonyms are a great way to find new words if you don’t know how to express what you want to say.<\/p>\n\n\n\n

In some cases, the meaning of synonymous words is not always interchangeable, but they often share some of their intentions.<\/p>\n\n\n\n

for example:<\/p>\n\n\n\n

The word “regretful” is synonymous with “bad” but can’t be interchanged in the sentence below. <\/p>\n\n\n\n

“A bad<\/em> motor caused the ship to sink”<\/p>\n\n\n\n

WordNet Python Examples using NLTK: Find Synonyms and Antonyms from NLTK<\/h2>\n\n\n\n

The purpose of this tutorial is to show you how you can use NLTK<\/a> WordNet in Python to find synonyms and antonyms.<\/p>\n\n\n\n

NLTK is a natural language processing<\/a> library with a built-in WordNet database. So, in this tutorial, we will write a simple python code that will help us find synonyms for any given word by using NLTK WordNet in Python.<\/p>\n\n\n\n

Import WordNet <\/p>\n\n\n\n

from nltk.corpus import wordnet<\/code><\/pre>\n\n\n\n

A Synset<\/strong> is a WordNet synonym set. It is a collection of words that have the same meaning, and are grouped together in a sense as they are related to each other.<\/p>\n\n\n\n

Synset python example:<\/p>\n\n\n\n

from nltk.corpus import wordnet\nsynset = wordnet.synsets(\"cunning\")\nprint(synset)<\/code><\/pre>\n\n\n\n

Output:<\/p>\n\n\n\n

\"synset<\/figure>\n\n\n\n

From the results, we can see that the word “cunning” is synonymous with “craft”, “crafty” and “clever”. <\/p>\n\n\n\n

To see more words in the synset, we can expand and loop through the synset lists for more words.<\/p>\n\n\n\n

for syns in synset:\n    for word in syns.lemmas():\n        print(word.name())<\/code><\/pre>\n\n\n\n

Output:<\/p>\n\n\n\n

craft\ncraftiness\ncunning\nfoxiness\nguile\nslyness\nwiliness\ncunning\ncunning\ncute\ncrafty\ncunning\ndodgy\nfoxy\nguileful\nknavish\nslick\nsly\ntricksy\ntricky\nwily\nclever\ncunning\ningenious<\/code><\/pre>\n\n\n\n
\"synset<\/figure>\n\n\n\n

<\/p>\n\n\n\n

What are Antonyms in NLP?<\/h2>\n\n\n\n

Antonyms are words that have opposite or nearly opposite meanings. They are usually opposites in the way they are used. For example, “close” is an antonym of “open”.<\/p>\n\n\n\n

They are also known as antonym pairs.<\/p>\n\n\n\n

Some examples of antonyms are:<\/p>\n\n\n\n