# ==============================
# WORD SENSE DISAMBIGUATION (WSD)
# Example: "plant"
# ==============================
import nltk
# Download required datasets (run once per session in Colab)
[Link]('punkt')
[Link]('wordnet')
[Link]('omw-1.4')
from [Link] import lesk
from [Link] import word_tokenize
from [Link] import wordnet as wn
# Function for WSD
def disambiguate_word(sentence, ambiguous_word):
print("\nSentence:", sentence)
# Tokenize sentence
context = word_tokenize(sentence)
# Apply Lesk algorithm
sense = lesk(context, ambiguous_word)
if sense:
print("Ambiguous Word:", ambiguous_word)
print("Predicted Sense:", [Link]())
print("Definition:", [Link]())
print("Example Usage:", [Link]())
else:
print("No sense found.")
# Example 1 (Plant = living organism)
sentence1 = "She watered the plant in the garden."
disambiguate_word(sentence1, "plant")
print("\n------------------------------------")
# Example 2 (Plant = factory)
sentence2 = "The company built a new plant to manufacture cars."
disambiguate_word(sentence2, "plant")
Tokenization
import nltk
[Link]('punkt')
from [Link] import word_tokenize, sent_tokenize
text = "Natural Language Processing is interesting. It is a branch of AI."
# Sentence Tokenization
sentences = sent_tokenize(text)
print("Sentences:", sentences)
# Word Tokenization
words = word_tokenize(text)
print("Words:", words)
Stopword Removal
import nltk
[Link]('stopwords')
[Link]('punkt')
from [Link] import stopwords
from [Link] import word_tokenize
text = "This is a simple example to demonstrate stopword removal."
stop_words = set([Link]('english'))
words = word_tokenize(text)
filtered_words = [w for w in words if [Link]() not in stop_words]
print("Original Words:", words)
print("After Stopword Removal:", filtered_words)
Stemming Example
import nltk
[Link]('punkt')
from [Link] import PorterStemmer
from [Link] import word_tokenize
stemmer = PorterStemmer()
text = "playing played plays easily fair fairness"
words = word_tokenize(text)
stemmed_words = [[Link](word) for word in words]
print("Stemmed Words:", stemmed_words)
POS Tagging
import nltk
[Link]('punkt')
[Link]('averaged_perceptron_tagger_eng')
import nltk
# Download required resources
[Link]('punkt')
[Link]('averaged_perceptron_tagger_eng')
from [Link] import word_tokenize
from nltk import pos_tag
sentence = "Natural Language Processing is very interesting"
words = word_tokenize(sentence)
tags = pos_tag(words)
print("POS Tags:", tags)