How to get synonyms/antonyms from NLTK WordNet in Python?
Last Updated :
22 Oct, 2017
WordNet is a large lexical database of English. Nouns, verbs, adjectives and adverbs are grouped into sets of cognitive synonyms (synsets), each expressing a distinct concept. Synsets are interlinked by means of conceptual-semantic and lexical relations.
WordNet's structure makes it a useful tool for computational linguistics and natural language processing.
WordNet superficially resembles a thesaurus, in that it groups words together based on their meanings. However, there are some important distinctions.
- First, WordNet interlinks not just word forms—strings of letters—but specific senses of words. As a result, words that are found in close proximity to one another in the network are semantically disambiguated.
- Second, WordNet labels the semantic relations among words, whereas the groupings of words in a thesaurus does not follow any explicit pattern other than meaning similarity.
Python
# First, you're going to need to import wordnet:
from nltk.corpus import wordnet
# Then, we're going to use the term "program" to find synsets like so:
syns = wordnet.synsets("program")
# An example of a synset:
print(syns[0].name())
# Just the word:
print(syns[0].lemmas()[0].name())
# Definition of that first synset:
print(syns[0].definition())
# Examples of the word in use in sentences:
print(syns[0].examples())
The output will look like:
plan.n.01
plan
a series of steps to be carried out or goals to be accomplished
['they drew up a six-step plan', 'they discussed plans for a new bond issue']
Next, how might we discern synonyms and antonyms to a word? The lemmas will be synonyms, and then you can use .antonyms to find the antonyms to the lemmas. As such, we can populate some lists like:
Python
import nltk
from nltk.corpus import wordnet
synonyms = []
antonyms = []
for syn in wordnet.synsets("good"):
for l in syn.lemmas():
synonyms.append(l.name())
if l.antonyms():
antonyms.append(l.antonyms()[0].name())
print(set(synonyms))
print(set(antonyms))
The output will be two sets of synonyms and antonyms
{'beneficial', 'just', 'upright', 'thoroughly', 'in_force', 'well', 'skilful', 'skillful', 'sound', 'unspoiled', 'expert', 'proficient', 'in_effect', 'honorable', 'adept', 'secure', 'commodity', 'estimable', 'soundly', 'right', 'respectable', 'good', 'serious', 'ripe', 'salutary', 'dear', 'practiced', 'goodness', 'safe', 'effective', 'unspoilt', 'dependable', 'undecomposed', 'honest', 'full', 'near', 'trade_good'} {'evil', 'evilness', 'bad', 'badness', 'ill'}
Now , let's compare the similarity index of any two words
Python
import nltk
from nltk.corpus import wordnet
# Let's compare the noun of "ship" and "boat:"
w1 = wordnet.synset('run.v.01') # v here denotes the tag verb
w2 = wordnet.synset('sprint.v.01')
print(w1.wup_similarity(w2))
Output:
0.857142857143
Python
w1 = wordnet.synset('ship.n.01')
w2 = wordnet.synset('boat.n.01') # n denotes noun
print(w1.wup_similarity(w2))
Output:
0.9090909090909091
Similar Reads
Correcting Words using NLTK in Python
nltk stands for Natural Language Toolkit and is a powerful suite consisting of libraries and programs that can be used for statistical natural language processing. The libraries can implement tokenization, classification, parsing, stemming, tagging, semantic reasoning, etc. This toolkit can make mac
4 min read
Find the k most frequent words from data set in Python
The goal is to find the k most common words in a given dataset of text. We'll look at different ways to identify and return the top k words based on their frequency, using Python.Using collections.Countercollections.Counter that works like a dictionary, but its main job is to count how many times ea
3 min read
Toggle characters in words having same case - Python
We are given a sentence and need to toggle the case of words that have all characters in the same case, either all lowercase or all uppercase. If a word meets this condition, we change each letter to its opposite case using swapcase(). Words with a mix of uppercase and lowercase letters remain uncha
3 min read
Python | NLTK nltk.tokenize.ConditionalFreqDist()
With the help of nltk.tokenize.ConditionalFreqDist() method, we are able to count the frequency of words in a sentence by using tokenize.ConditionalFreqDist() method. Syntax : tokenize.ConditionalFreqDist() Return : Return the frequency distribution of words in a dictionary. Example #1 : In this exa
1 min read
Python program to print even length words in a string
The task of printing even-length words from a string in Python involves identifying and extracting words whose lengths are divisible by 2. Given an input string, the goal is to filter out words with an even number of characters and display them. For example , s = "Python is great", the even-length w
3 min read
Get similar words suggestion using Enchant in Python
For the given user input, get similar words through Enchant module. Enchant is a module in python which is used to check the spelling of a word, gives suggestions to correct words. Also, gives antonym and synonym of words. It checks whether a word exists in dictionary or not. Other dictionaries can
1 min read
How to implement Dictionary with Python3?
This program uses python's container called dictionary (in dictionary a key is associated with some information). This program will take a word as input and returns the meaning of that word. Python3 should be installed in your system. If it not installed, install it from this link. Always try to ins
3 min read
Python NLTK | nltk.tokenizer.word_tokenize()
With the help of nltk.tokenize.word_tokenize() method, we are able to extract the tokens from string of characters by using tokenize.word_tokenize() method. It actually returns the syllables from a single word. A single word can contain one or two syllables. Syntax : tokenize.word_tokenize() Return
1 min read
Reverse each word in a sentence in Python
In this article, we will explore various methods to reverse each word in a sentence. The simplest approach is by using a loop.Using LoopsWe can simply use a loop (for loop) to reverse each word in a sentence.Pythons = "Hello World" # Split 's' into words words = s.split() # Reverse each word using a
2 min read
Reverse Words in a Given String in Python
In this article, we explore various ways to reverse the words in a string using Python. From simple built-in methods to advanced techniques like recursion and stacks. We are going to see various techniques to reverse a string.Using split() and join()Using split() and join() is the most common method
2 min read