Word Sense Disambiguation with Lesk¶
This disambiguates the classic ambiguous word "bank" between its financial
and riverbank senses. A single mention carries the ambiguous label and a
sentence of context; two candidate senses carry the same label and a gloss.
LeskLinker scores each candidate by how many
words its gloss shares with the mention's context, so the "deposited money"
context should pull the link toward the financial-institution sense.
"""Word Sense Disambiguation with the Lesk algorithm.
Disambiguates the classic ambiguous word "bank" between its financial and
riverbank senses, by comparing the mention's context against each
candidate sense's gloss.
Run with: `uv run python examples/lesk_wsd.py`
"""
from linkingtk.algorithms.wsd import LeskLinker
from linkingtk.core import Entity
from linkingtk.eval import Evaluator
def main() -> None:
mentions = [
Entity(
id="m1",
labels=["bank"],
context="I deposited money at the bank yesterday",
),
]
senses = [
Entity(
id="bank.n.01",
labels=["bank"],
description="a financial institution that accepts deposits of money",
),
Entity(
id="bank.n.02",
labels=["bank"],
description="the land alongside a river",
),
]
results = LeskLinker().link(mentions, senses)
for result in results:
print(f"{result.source_id} -> {result.target_id} (score={result.score})")
print(f" alternatives: {result.alternatives}")
predictions = [(r.source_id, r.target_id) for r in results]
ground_truth = [("m1", "bank.n.01")]
report = Evaluator.evaluate(predictions=predictions, ground_truth=ground_truth)
print("Metrics:", report.metrics)
if __name__ == "__main__":
main()
Run with:
uv run python examples/lesk_wsd.py
m1 -> bank.n.01 (score=1.0)
alternatives: ['bank.n.02']
Metrics: {'precision@1': 1.0, 'recall': 1.0, 'f1': 1.0}