WordNet is a large English dictionary built for computers. It groups words into sets of synonyms called synsets, each with a definition and examples, and links them by meaning: more general words (hypernyms), more specific words (hyponyms), antonyms and more.
What You Will Learn
- Look up synsets, definitions and examples
- Find hypernyms and hyponyms
- Get synonyms and antonyms through lemmas
- Read part-of-speech codes
Prerequisites
Lemmatization. Run nltk.download("wordnet") once.
Synsets, Definitions and Examples
from nltk.corpus import wordnet
syns = wordnet.synsets("welcome")
print(syns)
syn = syns[2]
print("Name:", syn.name())
print("Meaning:", syn.definition())
print("Example:", syn.examples())
[Synset('welcome.n.01'), Synset('welcome.n.02'), Synset('welcome.v.01'), Synset('welcome.v.02'), Synset('welcome.v.03'), Synset('welcome.a.01')]
Name: welcome.v.01
Meaning: accept gladly
Example: ['I welcome your proposals']
Explanation: A word can have many meanings. welcome.v.01 means “welcome, verb, first sense”. POS codes: n = noun, v = verb, a = adjective, s = adjective satellite, r = adverb.
Hypernyms and Hyponyms
car = wordnet.synsets("car")[0]
print(car.hypernyms())
print(car.hypernyms()[0].hyponyms()[:6])
[Synset('motor_vehicle.n.01')]
[Synset('amphibian.n.01'), Synset('bloodmobile.n.01'), Synset('car.n.01'), Synset('doodlebug.n.01'), Synset('four-wheel_drive.n.01'), Synset('go-kart.n.01')]
Explanation: A car is a kind of motor vehicle (hypernym, more general). The motor vehicle’s hyponyms (more specific) include cars, go-karts and others.
Synonyms
synonyms = [lem.name() for s in wordnet.synsets("car") for lem in s.lemmas()]
print(synonyms)
print(len(synonyms), "->", len(set(synonyms)), "unique")
['car', 'auto', 'automobile', 'machine', 'motorcar', 'car', 'railcar', 'railway_car', 'railroad_car', 'car', 'gondola', 'car', 'elevator_car', 'cable_car', 'car'] 15 -> 11 unique
Explanation: Each synset contains lemmas – the words that express that meaning. Collecting them across all senses gives synonyms; set() removes repeats.
Antonyms
antonyms = {ant.name() for s in wordnet.synsets("win") for lem in s.lemmas() for ant in lem.antonyms()}
print(antonyms)
{'losings', 'lose'}
Explanation: Antonyms belong to lemmas, not synsets. “win” as a noun (winnings) has the antonym “losings”; as a verb it has “lose”.
Part of Speech of a Word
for w in ["hello", "doing", "beautiful", "quickly"]:
print(w, wordnet.synsets(w)[0].pos())
hello n doing v beautiful a quickly r
Uses in NLP
- Synonym expansion for search (“car” also finds “automobile”)
- Lemmatization (WordNetLemmatizer)
- Word similarity:
syn1.wup_similarity(syn2) - Data augmentation by replacing words with synonyms
Series
NLP series: Introduction · Tokenizer · Stop Words · Stemming · Lemmatization · WordNet · NLTK Practice · Spam Detection Project · Text to Speech · Speech Assistant · NLP Project Ideas
Interview Questions
Q: What is a synset?
A set of synonyms that share one meaning, with a definition and examples.
Q: Hypernym vs hyponym?
A hypernym is more general (vehicle); a hyponym is more specific (car).
Practice Exercises
- List all synonyms and antonyms of “happy”.
- Find the similarity between “dog” and “cat” with
wup_similarity.
Summary
WordNet links English words by meaning. With NLTK you can read definitions and examples, walk hypernyms and hyponyms, and collect synonyms and antonyms.
Next: NLTK Practice
Machine Learning Tutorials, Courses and Certifications