Word Sense Disambiguation (WSD) focuses on determining the correct meaning of a word when it has multiple possible interpretations. Many words in natural language are ambiguous, meaning their meaning changes depending on the context in which they are used. WSD enables NLP systems to analyze the surrounding words in a sentence and identify the intended sense, improving language understanding and reducing ambiguity.
For example, consider the word "bank":
- "She deposited money in the bank." → Bank refers to a financial institution.
- "The fisherman sat on the river bank." → Bank refers to the land beside a river.
Approaches to Word Sense Disambiguation
WSD techniques can be categorized into three main approaches, each with distinct methodologies and use cases.
1. Knowledge-Based Methods
Knowledge-based approaches utilize lexical resources such as dictionaries and semantic networks to determine word meanings. The Lesk algorithm works over this approach.
- Compare context words with dictionary definitions of candidate senses
- Calculate overlap between contextual words and definitional content
- Select the sense with maximum overlap score
Advantages:
- Does not require annotated training data
- Leverages existing linguistic knowledge bases
- Provides interpretable disambiguation decisions
The Leak algorithm assumes that words used together in coherent text will have semantic relationships reflected in their dictionary definitions.
2. Supervised Learning Methods
Supervised approaches treat WSD as a classification problem, training machine learning models on datasets where word instances have been manually annotated with correct senses.
Key characteristics:
- Requires substantial amounts of sense-annotated training data
- Employs standard machine learning algorithms such as support vector machines, decision trees or neural networks
- Uses contextual features including surrounding words and syntactic relationships
Training process:
- Extract features from annotated examples
- Train classifier to map feature vectors to sense labels
- Apply trained model to disambiguate new instances
While supervised methods achieve high accuracy, they face the challenge of obtaining sufficient annotated data for all word-sense combinations.
3. Unsupervised Learning Methods
Unsupervised approaches operate without sense-labeled training data, instead relying on distributional patterns in large text corpora.
Fundamental principle:
- Words appearing in similar contexts tend to have similar meanings
- Cluster word occurrences based on contextual similarity
- Assign sense labels to resulting clusters
Modern techniques:
- Utilize word embeddings and contextualized representations
- Employ clustering algorithms to group similar contexts
- Leverage large-scale language models for contextual understanding
These methods are particularly valuable when annotated data is scarce or unavailable for specific domains or languages.
Implementation
1. Creating the Class and Sense Inventory
We begin by creating the BasicWSD class, which contains the sense inventory and a list of stop words used during preprocessing.
self.sense_inventorystores ambiguous words along with their possible senses and the keywords associated with each sense.self.stop_wordscontains commonly used words that are ignored during preprocessing because they contribute little to determining the intended meaning of a word.
class BasicWSD:
def __init__(self):
self.sense_inventory = {
'bank': {
'financial': ['money', 'deposit', 'account', 'loan', 'cash', 'credit', 'savings'],
'geographical': ['river', 'water', 'shore', 'stream', 'fishing', 'boat']
},
'mouse': {
'computer': ['click', 'computer', 'cursor', 'button', 'screen', 'software'],
'animal': ['cheese', 'cat', 'rodent', 'small', 'trap', 'pet']
},
'star': {
'celebrity': ['famous', 'actor', 'movie', 'film', 'hollywood', 'performance'],
'celestial': ['sky', 'night', 'bright', 'constellation', 'galaxy', 'space']
}
}
self.stop_words = {
'the', 'a', 'an', 'and', 'or', 'but',
'in', 'on', 'at', 'to', 'for',
'of', 'with', 'by'
}
2. Preprocessing the Input Sentence
The preprocess() method cleans the input sentence before sense prediction. It converts the text to lowercase, removes punctuation marks, splits the sentence into individual words, and eliminates stop words to retain only meaningful context words.
sentence.lower()converts all characters to lowercase for consistent matching.- The
forloop removes punctuation symbols from the sentence. sentence.split()divides the sentence into individual words.- Stop words and single-character tokens are removed to keep only meaningful context words.
def preprocess(self, sentence):
sentence = sentence.lower()
for ch in ".,!?;:":
sentence = sentence.replace(ch, "")
return [
word for word in sentence.split()
if word not in self.stop_words and len(word) > 1
]
3. Disambiguating the Target Word
The disambiguate() method predicts the correct meaning of the target word by comparing the surrounding context words with the predefined keywords for each possible sense.
context = [w for w in self.preprocess(sentence) if w != target]extracts all meaningful context words except the target word.scores[sense] = len(set(context) & set(keywords))calculates the overlap score by counting the number of matching keywords between the context and each sense.max(scores, key=scores.get)selects the sense with the highest overlap score as the predicted meaning.
def disambiguate(self, target, sentence):
if target not in self.sense_inventory:
return "Target word not found in sense inventory."
context = [
w for w in self.preprocess(sentence)
if w != target
]
scores = {}
for sense, keywords in self.sense_inventory[target].items():
scores[sense] = len(set(context) & set(keywords))
best_sense = max(scores, key=scores.get)
return best_sense, scores
4. Testing the Implementation
An object of the BasicWSD class is created and tested using sample sentences containing ambiguous words.
wsd.disambiguate(word, sentence)predicts the most appropriate sense of the target word and returns the overlap scores for all possible senses.- The output displays the input sentence, target word, predicted sense, and the overlap score for each available meaning.
wsd = BasicWSD()
examples = [
("bank", "I need to deposit money into my savings account at the bank"),
("bank", "The fisherman stood on the river bank casting his line"),
("mouse", "The computer mouse stopped responding to clicks")
]
for word, sentence in examples:
sense, score = wsd.disambiguate(word, sentence)
print(f"\n• Sentence: {sentence}")
print(f"• Target Word: '{word}'")
print(f"• Predicted Sense: {sense}")
print("• Overlap Scores:")
for s, val in score.items():
print(f" - {s}: {val}")
Output:

We can see from the output that:
1. Financial context example:
- Sentence: "I need to deposit money into my savings account at the bank"
- Predicted sense: "financial" (overlaps: money, deposit, account, savings)
- Confidence score: 4 matching words
2. Geographical context example:
- Sentence: "The fisherman stood on the river bank casting his line"
- Predicted sense: "geographical" (overlaps: river)
- Confidence score: 1 matching words
3. Computer Context Example
- Sentence: "The computer mouse stopped responding to clicks"
- Predicted Sense: "
computer" (overlaps: computer) - Confidence Score: 1 matching word
You can download the complete code from here.
Applications
- Machine Translation: Identifies the correct meaning of ambiguous words before translating them into another language.
- Information Retrieval: Improves search results by understanding the intended meaning of user queries.
- Chatbots and Virtual Assistants: Helps conversational systems interpret user input and generate context-aware responses.
- Question Answering Systems: Determines the correct meaning of words to provide accurate answers.
- Sentiment Analysis: Resolves ambiguous words to improve sentiment classification accuracy.
- Text Summarization: Preserves the intended meaning of words while generating concise summaries.
Advantages
- Resolves ambiguity by identifying the correct meaning of words in context.
- Improves the accuracy of various Natural Language Processing applications.
- Enhances semantic understanding instead of relying only on keyword matching.
- Produces more relevant search results and information retrieval.
- Improves the quality of machine translation and question answering systems.
Limitations
- Performance depends heavily on the availability of sufficient contextual information.
- Knowledge-based methods require comprehensive lexical resources such as dictionaries or WordNet.
- Supervised approaches need large annotated datasets for effective training.
- Domain-specific words and rare senses are difficult to disambiguate accurately.
- Transformer-based models require significant computational resources.
- No single WSD technique performs equally well across all languages and domains