NLTK WordNet: Find Synonyms from NLTK WordNet in Python

โšก Smart Summary

WordNet is a lexical database and NLTK corpus reader for English that groups nouns, verbs, adjectives, and adverbs into synsets, letting Python programs look up synonyms, antonyms, hypernyms, and word meanings for natural language processing.

  • ๐Ÿงฉ Corpus reader: WordNet is imported from nltk.corpus and acts as a semantically oriented dictionary of English.
  • ๐Ÿ” Synsets: A synset is a collection of synonym words, such as the eight senses returned for “dog”.
  • ๐Ÿ”— Lexical relations: Reciprocal semantic links connect synonyms, antonyms, hypernyms, and hyponyms.
  • ๐Ÿงช Synonyms and antonyms: Looping over synsets and lemmas collects every synonym and antonym of a word.
  • ๐ŸŒณ Hierarchy: Hypernyms, hyponyms, holonyms, and meronyms organize words into noun, verb, adjective, and adverb subnets.
  • ๐Ÿค– AI benefit: WordNet powers text analysis tasks like spelling correction, translation, and spam detection.

NLTK WordNet

What is Wordnet?

WordNet is an NLTK corpus reader, a lexical database for English. It can be used to find the meaning of words, a synonym, or an antonym. One can define it as a semantically oriented dictionary of English. WordNet is imported with the following command:

from nltk.corpus import wordnet as guru

Because WordNet ships as a ready-made corpus, you can load it once and immediately begin querying word relationships without building any dictionary yourself.

Find Synonyms from NLTK WordNet in Python

Stats reveal that there are 155287 words and 117659 synonym sets included with English WordNet. Different methods available with WordNet can be found by typing dir(guru), which lists every loader attribute and helper method exposed by the corpus reader.

Synset: It is also called a synonym set or collection of synonym words. Let us check an example.

from nltk.corpus import wordnet
syns = wordnet.synsets("dog")
print(syns)

Output:

[Synset('dog.n.01'), Synset('frump.n.01'), Synset('dog.n.03'), Synset('cad.n.01'), Synset('frank.n.02'), Synset('pawl.n.01'), Synset('andiron.n.01'), Synset('chase.v.01')]

Lexical Relations: These are semantic relations which are reciprocated. If there is a relationship between {x1,x2,…xn} and {y1,y2,…yn}, then there is also a relation between {y1,y2,…yn} and {x1,x2,…xn}. For example, synonym is the opposite of antonym, while hypernyms and hyponyms are a type of lexical concept.

Let us write a program using Python to find the synonym and antonym of the word “active” using WordNet.

from nltk.corpus import wordnet
synonyms = []
antonyms = []
for syn in wordnet.synsets("active"):
    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 of the code:

{'dynamic', 'fighting', 'combat-ready', 'active_voice', 'active_agent', 'participating', 'alive', 'active'} -- Synonym
{'stative', 'passive', 'quiet', 'passive_voice', 'extinct', 'dormant', 'inactive'} -- Antonym

Explanation of the code:

  • WordNet is a corpus, so it is imported from the nltk.corpus package.
  • Two lists for synonyms and antonyms are taken as empty, which will be used for appending.
  • Synonyms of the word “active” are searched in the synsets method and appended to the list synonyms. The same process collects antonyms.
  • Output is printed as Python sets so duplicate entries are removed automatically.

Beyond synonyms and antonyms, WordNet also exposes hypernyms (broader terms), hyponyms (more specific terms), holonyms, and meronyms, so a single query can place a word inside an entire is-a hierarchy. These relations make WordNet a practical thesaurus for spelling checkers, language translation, spam detection, and other text-analysis tasks in artificial intelligence.

FAQs

WordNet is an NLTK corpus reader and lexical database for English. It works like a semantically oriented dictionary, letting you look up the meaning, synonyms, and antonyms of a word with simple Python commands.

A synset, short for synonym set, is a group of words that share the same concept. Calling wordnet.synsets(“dog”) returns several synsets, each representing one distinct sense of the word “dog”.

Loop through wordnet.synsets(word), then iterate each synset’s lemmas(). Append lemma.name() to collect synonyms, and call lemma.antonyms() to collect antonyms. Wrapping the results in set() removes duplicate entries.

A hypernym is a broader, generic term (meal is a hypernym of breakfast), while a hyponym is a more specific term (rice is a hyponym of meal). WordNet links these to form an is-a hierarchy.

English WordNet includes about 155,287 words organized into roughly 117,659 synonym sets. The database spans four parts of speech: nouns, verbs, adjectives, and adverbs, each stored in its own subnet.

In artificial intelligence, WordNet supports text analysis tasks such as word-sense disambiguation, spelling correction, language translation, and spam detection. Its structured relations give machine learning models richer linguistic features than raw tokens alone.

Yes. AI and machine learning pipelines use WordNet similarity measures like path similarity and Wu-Palmer similarity. They score how close two synsets are by measuring the shortest path between them in the hypernym and hyponym hierarchy.

A normal dictionary lists definitions alphabetically. WordNet instead links words by meaning through synsets and lexical relations, so it behaves like a combined dictionary and thesaurus that machines can traverse programmatically.

Summarize this post with: