NLTK WordNet provides a structured lexical database that helps discover relationships between words such as synonyms and antonyms. Using WordNet in Python makes it easy to explore word meanings and find related terms programmatically which is especially useful in NLP and text analysis tasks.
- Synonyms are words with the same or nearly the same meaning. In WordNet these words are organized into groups called synsets, where each term represents a similar idea or concept.
- Antonyms are words with opposite meanings. WordNet links antonyms at the word-sense level allowing identification of terms that express contrasting ideas, such as hot and cold or increase and decrease.
Step by Step Implementation
Step 1: Installing and Importing NLTK and WordNet
Here in this code we installs NLTK and imports WordNet with optional multilingual support.
!pip install nltk
import nltk
nltk.download('wordnet')
nltk.download('omw-1.4')
from nltk.corpus import wordnet
Output:

Step 2. Understanding Synsets
A synset is a group of words that share the same meaning. Words often have multiple meanings depending on context and each meaning is represented by a different synset:
syns = wordnet.synsets("program")
print(syns[0].name())
print(syns[0].lemmas()[0].name())
print(syns[0].definition())
print(syns[0].examples())
Output:

Step 3: Extracting Synonyms
In WordNet, synonyms are organized as lemmas within synsets that represent a common concept. Here in below code:
- Iterates through all synsets related to the word “good”.
- Collects the names of all lemmas from each synset as synonyms.
- Converts the collected list into a set to remove duplicates and display unique synonyms.
synonyms = []
for syn in wordnet.synsets("good"):
for lemma in syn.lemmas():
synonyms.append(lemma.name())
print(set(synonyms))
Output:
{'right', 'secure', 'adept', 'sound', 'in_force', 'respectable', 'thoroughly', 'upright',
Step 4: Extracting Antonyms
In WordNet, antonym relationships are defined at the lemma level and can be accessed through linked lemmas.
antonyms = []
for syn in wordnet.synsets("good"):
for lemma in syn.lemmas():
if lemma.antonyms():
antonyms.append(lemma.antonyms()[0].name())
print(set(antonyms))
Output:
{'bad', 'badness', 'evil', 'ill', 'evilness'}
Step 5: Combining Synonyms and Antonyms
Here we shows how to retrieve both synonyms and antonyms for a word using a single function. By iterating through WordNet synsets and their associated lemmas, related words with similar and opposite meanings can be collected efficiently. This approach helps in building richer vocabulary-based features for text analysis tasks.
def get_synonyms_antonyms(word):
synonyms = set()
antonyms = set()
for syn in wordnet.synsets(word):
for lemma in syn.lemmas():
synonyms.add(lemma.name())
if lemma.antonyms():
antonyms.add(lemma.antonyms()[0].name())
return synonyms, antonyms
syns, ants = get_synonyms_antonyms("happy")
print("Synonyms:", syns)
print("Antonyms:", ants)
Output:
Synonyms: {'felicitous', 'well-chosen', 'happy', 'glad'}
Antonyms: {'unhappy'}
Step 6: Comparing Semantic Similarity Between Words
WordNet allows measuring similarity between synsets using different methods such as Wu-Palmer similarity. This is useful to quantify how semantically close two words are.
w1 = wordnet.synset('run.v.01')
w2 = wordnet.synset('sprint.v.01')
print("Verb similarity:", w1.wup_similarity(w2))
w1 = wordnet.synset('ship.n.01')
w2 = wordnet.synset('boat.n.01')
print("Noun similarity:", w1.wup_similarity(w2))
Output:
Verb similarity: 0.8571428571428571
Noun similarity: 0.9090909090909091
Step 7: Exploring Additional Semantic Relations
WordNet provides several semantic relations that describe how word meanings are connected, helping in deeper linguistic and semantic analysis.
1. Hypernyms: Hypernyms represent a more general category to which a word belongs.
from nltk.corpus import wordnet
syn = wordnet.synset('dog.n.01')
print(syn.hypernyms())
Output:
[Synset('domestic_animal.n.01'), Synset('canine.n.02')]
This output shows that dog belongs to two broader categories in WordNet canine and domestic animal. In other words, a dog is classified as a type of canine and also as a domesticated animal.
2. Hyponyms: It represent more specific types or subcategories of a word.
syn = wordnet.synset('dog.n.01')
print(syn.hyponyms())
Output:
[Synset('pug.n.01'), Synset('leonberg.n.01'), Synset('griffon.n.02'), Synset('spitz.n.01'), Synset('toy_dog.n.01'), Synset('basenji.n.01'),
The output lists specific breeds or subcategories of the word “dog”, showing how each hyponym represents a more specific concept under the general term. In WordNet these synsets form a hierarchy where dog is the parent concept and individual breeds are its children.
3. Meronyms: It describe the parts or components of a whole.
syn = wordnet.synset('tree.n.01')
print(syn.part_meronyms())
Output:
[Synset('stump.n.01'), Synset('crown.n.07'), Synset('burl.n.02'), Synset('trunk.n.01'), Synset('limb.n.02')]
The output shows different parts of a tree, such as the trunk, limbs, crown and stump. Each synset represents a component that collectively forms the whole object, illustrating the part–whole (meronym) relationship in WordNet.
You can download full code from here