NLP 51% to 60% • Built-in NLP Modules
Synonyms, Antonyms and Meaning Matching using NLTK WordNet
This lesson is rebuilt completely. We will not depend on hand-written mini dictionaries. Students will now use real NLP resources through Python modules, especially NLTK WordNet, to find word meanings, synonyms, antonyms, examples, word families, and simple sentence meaning matches.
0 to infinity path
51% to 60% Roadmap
Lesson shift
From hand-written dictionaries to NLP modules
Old beginner approach
We manually wrote a dictionary like:
synonyms = {
"happy": ["glad", "joyful"],
"big": ["large", "huge"]
}
This is good for understanding, but it is not scalable. We cannot manually type thousands of words.
New NLP-module approach
We use a real NLP resource:
from nltk.corpus import wordnet as wn
synsets = wn.synsets("happy")
Now Python can search a large lexical database instead of relying only on our small manual list.
52%
Setup: Install and download WordNet
Step 1: Install NLTK
pip install nltk
Step 2: Download WordNet data once
import nltk
nltk.download("wordnet")
nltk.download("omw-1.4")
wordnet gives English word meanings and relationships.
omw-1.4 provides Open Multilingual WordNet data used by NLTK.
53%
WordNet basics: synsets, lemmas, definitions
Synset
A synset is a set of words that share one meaning.
Lemma
A lemma is a word form inside a synset.
Definition
A definition explains the meaning of that synset.
Explore a word with WordNet
from nltk.corpus import wordnet as wn
word = "happy"
synsets = wn.synsets(word)
print("Word:", word)
print("Number of meanings found:", len(synsets))
for s in synsets:
print("\nSynset:", s.name())
print("Definition:", s.definition())
print("Examples:", s.examples())
print("Lemma names:", s.lemma_names())
54%
Find synonyms using NLTK WordNet
In WordNet, synonyms can be collected from lemma names inside synsets. We do not have to manually write every synonym.
Synonym finder using WordNet
from nltk.corpus import wordnet as wn
def get_synonyms(word):
synonyms = set()
for synset in wn.synsets(word):
for lemma in synset.lemmas():
name = lemma.name().replace("_", " ")
if name.lower() != word.lower():
synonyms.add(name)
return sorted(synonyms)
word = input("Enter a word: ")
print("\nSynonyms:")
for item in get_synonyms(word):
print("-", item)
Try these words
happy, big, small, fast, start,
smart, beautiful
55%
Find antonyms using WordNet lemmas
In WordNet, antonyms are attached to lemmas. We search all synsets, then all lemmas, then ask each lemma whether it has antonyms.
Antonym finder using WordNet
from nltk.corpus import wordnet as wn
def get_antonyms(word):
antonyms = set()
for synset in wn.synsets(word):
for lemma in synset.lemmas():
for antonym in lemma.antonyms():
name = antonym.name().replace("_", " ")
antonyms.add(name)
return sorted(antonyms)
word = input("Enter a word: ")
print("\nAntonyms:")
for item in get_antonyms(word):
print("-", item)
56%
Find multiple meanings, definitions and examples
WordNet is useful because it does not only return synonyms. It also returns definitions and example sentences. This helps students see that a word can have more than one meaning.
Meaning explorer
from nltk.corpus import wordnet as wn
def explain_word(word):
synsets = wn.synsets(word)
if not synsets:
print("No meaning found for:", word)
return
print("Word:", word)
print("Meanings found:", len(synsets))
for number, synset in enumerate(synsets, start=1):
print("\nMeaning", number)
print("Synset:", synset.name())
print("Part of speech:", synset.pos())
print("Definition:", synset.definition())
examples = synset.examples()
if examples:
print("Examples:")
for ex in examples:
print("-", ex)
print("Related words:", ", ".join(synset.lemma_names()))
word = input("Enter a word: ")
explain_word(word)
Classroom demonstration
Try light, bank, play, fast, and run.
These words show why context matters in NLP.
57%
Compare word meanings using WordNet similarity
WordNet can compare some word meanings using path similarity. The score is based on how close two synsets are in the WordNet hierarchy.
This is not perfect. It works better for nouns than for every kind of word. It also depends on selecting the correct synset.
Word similarity using first synset
from nltk.corpus import wordnet as wn
def word_similarity(word1, word2):
synsets1 = wn.synsets(word1)
synsets2 = wn.synsets(word2)
if not synsets1 or not synsets2:
return None
# Beginner version:
# take the first meaning of each word
s1 = synsets1[0]
s2 = synsets2[0]
return s1.path_similarity(s2)
pairs = [
("car", "automobile"),
("dog", "cat"),
("happy", "sad"),
("big", "large"),
("book", "table")
]
for w1, w2 in pairs:
score = word_similarity(w1, w2)
print(w1, "-", w2, "=", score)
58%
Sentence meaning using synonym expansion
CountVectorizer and TF-IDF may fail when two sentences use different words with similar meaning. We can improve sentence matching by expanding each word into its WordNet synonyms.
Sentence meaning score using WordNet synonyms
from nltk.corpus import wordnet as wn
import re
def clean_words(sentence):
sentence = sentence.lower()
return re.findall(r"[a-z]+", sentence)
def synonyms_of(word):
words = {word}
for synset in wn.synsets(word):
for lemma in synset.lemmas():
words.add(lemma.name().lower().replace("_", " "))
return words
def expanded_sentence_words(sentence):
result = set()
for word in clean_words(sentence):
result.update(synonyms_of(word))
return result
def sentence_meaning_score(sentence1, sentence2):
words1 = expanded_sentence_words(sentence1)
words2 = expanded_sentence_words(sentence2)
common = words1.intersection(words2)
total = words1.union(words2)
if not total:
return 0, common
score = len(common) / len(total)
return score, common
s1 = "I begin Python today"
s2 = "I start Python today"
score, common_words = sentence_meaning_score(s1, s2)
print("Sentence 1:", s1)
print("Sentence 2:", s2)
print("Meaning score:", round(score, 3))
print("Common meaning words:")
for word in sorted(common_words):
print("-", word)
59% and 60%
Mini Projects using NLP modules
Project 1: Word Meaning Explorer
import nltk
from nltk.corpus import wordnet as wn
# Run these once if WordNet is not downloaded:
# nltk.download("wordnet")
# nltk.download("omw-1.4")
def get_synonyms(word):
synonyms = set()
for synset in wn.synsets(word):
for lemma in synset.lemmas():
name = lemma.name().replace("_", " ")
if name.lower() != word.lower():
synonyms.add(name)
return sorted(synonyms)
def get_antonyms(word):
antonyms = set()
for synset in wn.synsets(word):
for lemma in synset.lemmas():
for antonym in lemma.antonyms():
antonyms.add(antonym.name().replace("_", " "))
return sorted(antonyms)
def show_meanings(word):
synsets = wn.synsets(word)
if not synsets:
print("No WordNet entry found.")
return
for i, synset in enumerate(synsets, start=1):
print("\nMeaning", i)
print("Name:", synset.name())
print("Part of speech:", synset.pos())
print("Definition:", synset.definition())
if synset.examples():
print("Examples:")
for ex in synset.examples():
print("-", ex)
word = input("Enter a word: ").lower()
print("\nWORD:", word)
print("\nSYNONYMS")
for item in get_synonyms(word):
print("-", item)
print("\nANTONYMS")
for item in get_antonyms(word):
print("-", item)
print("\nMEANINGS")
show_meanings(word)
Project 2: Sentence Meaning Matcher with WordNet
import re
from nltk.corpus import wordnet as wn
def clean_words(sentence):
sentence = sentence.lower()
return re.findall(r"[a-z]+", sentence)
def synonyms_of(word):
result = {word}
for synset in wn.synsets(word):
for lemma in synset.lemmas():
name = lemma.name().lower().replace("_", " ")
result.add(name)
return result
def antonyms_of(word):
result = set()
for synset in wn.synsets(word):
for lemma in synset.lemmas():
for antonym in lemma.antonyms():
name = antonym.name().lower().replace("_", " ")
result.add(name)
return result
def expand_sentence(sentence):
expanded = set()
for word in clean_words(sentence):
expanded.update(synonyms_of(word))
return expanded
def find_antonym_conflicts(sentence1, sentence2):
words1 = clean_words(sentence1)
words2 = clean_words(sentence2)
conflicts = []
for w1 in words1:
antonyms = antonyms_of(w1)
for w2 in words2:
if w2 in antonyms:
conflicts.append((w1, w2))
return conflicts
def meaning_match(sentence1, sentence2):
expanded1 = expand_sentence(sentence1)
expanded2 = expand_sentence(sentence2)
common = expanded1.intersection(expanded2)
total = expanded1.union(expanded2)
synonym_score = 0
if total:
synonym_score = len(common) / len(total)
conflicts = find_antonym_conflicts(sentence1, sentence2)
final_score = synonym_score - (0.15 * len(conflicts))
return synonym_score, conflicts, final_score, common
s1 = input("Enter first sentence: ")
s2 = input("Enter second sentence: ")
synonym_score, conflicts, final_score, common = meaning_match(s1, s2)
print("\nSentence 1:", s1)
print("Sentence 2:", s2)
print("\nSynonym expansion score:", round(synonym_score, 3))
print("\nCommon meaning words:")
for word in sorted(common):
print("-", word)
print("\nAntonym conflicts:")
if conflicts:
for pair in conflicts:
print("-", pair[0], "opposes", pair[1])
else:
print("No direct antonym conflict found.")
print("\nFinal meaning score:", round(final_score, 3))
if final_score > 0.20:
print("Result: Sentences may be meaningfully related.")
elif final_score < 0:
print("Result: Sentences may contain opposite meaning.")
else:
print("Result: Weak or unclear relationship.")
Project 3: Lesson Search with WordNet Expansion
import re
from nltk.corpus import wordnet as wn
lessons = [
"Start Python from zero",
"Begin machine learning with Python",
"Introduction to Natural Language Processing",
"HTML and CSS for beautiful web pages",
"Deploy Angular website on GitHub Pages",
"Text similarity using cosine similarity",
"TF IDF search ranking in NLP"
]
def clean_words(text):
return re.findall(r"[a-z]+", text.lower())
def expand_word(word):
result = {word}
for synset in wn.synsets(word):
for lemma in synset.lemmas():
result.add(lemma.name().lower().replace("_", " "))
return result
def expand_text(text):
expanded = set()
for word in clean_words(text):
expanded.update(expand_word(word))
return expanded
def score(query, lesson):
q = expand_text(query)
l = expand_text(lesson)
if not q or not l:
return 0
return len(q.intersection(l)) / len(q.union(l))
query = input("Search a lesson: ")
ranked = []
for lesson in lessons:
ranked.append((lesson, score(query, lesson)))
ranked.sort(key=lambda item: item[1], reverse=True)
print("\nSearch:", query)
print("\nRanked lessons:")
for lesson, sc in ranked:
print(round(sc, 3), "->", lesson)
Critical thinking
Limitations of WordNet-based meaning matching
Strengths
- Good for definitions.
- Good for synonyms and antonyms.
- Good for showing multiple meanings.
- Excellent for teaching word relationships.
Limitations
- It may not understand modern slang.
- It may not choose the correct meaning automatically.
- Sentence meaning still needs context.
- Advanced systems use embeddings and transformers later.
Practice
Embedded Python Editor
Paste the code examples into the editor. If your editor environment does not have NLTK installed,
run locally on your computer or use a Python notebook environment that allows pip install nltk.
60% checkpoint
Revision Test
Why did we replace manual dictionaries?
Manual dictionaries are useful for beginners but too small. NLP modules like WordNet contain many word meanings and relationships.
What is NLTK?
NLTK is a Python library used for natural language processing tasks and learning.
What is WordNet?
WordNet is a lexical database of English words, meanings, relationships, definitions, synonyms, and antonyms.
What is a synset?
A synset is a group of words that share one meaning.
What is a lemma?
A lemma is a word form inside a synset.
How do we find synonyms in WordNet?
We collect lemma names from synsets of a word.
How do we find antonyms in WordNet?
We check antonyms attached to lemmas inside synsets.
Why is context important?
One word can have many meanings, such as light, bank, fast, and run.
Why is WordNet not enough for all modern NLP?
It is lexical and rule/resource-based. Modern systems often need embeddings and transformer models for deeper context.
What should students understand by 60%?
They should understand how to use real NLP modules to explore meanings, synonyms, antonyms, and basic sentence meaning.
One-Line Summary
At 60%, NLP moves beyond counting words and starts using real meaning resources such as NLTK WordNet to understand word relationships.
Back to top