Skip to content

WSD against live WordNet with wn

The same "bank" disambiguation as Lesk WSD, but the candidate side is WnEntitySource, a query-driven EntitySource wrapping the wn library. Instead of two hand-picked candidate senses, ExactMatch queries wn for every synset lexicalized as "bank" -- the full ambiguity set a real dictionary has, not a toy subset -- without ever materializing the whole lexicon as Entity objects.

Requires the wn optional dependency and a one-time lexicon download:

uv pip install linkingtk[wn]
python -m wn download oewn:2021
"""Word Sense Disambiguation against live WordNet, via `wn`.

Disambiguates the classic ambiguous word "bank" between its financial and
riverbank senses, exactly like `lesk_wsd.py`, but scoring against every
sense `wn` knows about instead of two hand-picked candidates --
`WnEntitySource` fetches only the ones `ExactMatch` actually asks for
(`bank`), rather than materializing the whole lexicon as `Entity` objects.

Requires the `wn` optional dependency and a downloaded lexicon:

    uv pip install linkingtk[wn]
    python -m wn download oewn:2021

Run with: `uv run python examples/wn_wsd.py`
"""

from linkingtk.algorithms.wsd import LeskLinker
from linkingtk.blocking import ExactMatch
from linkingtk.core import Entity
from linkingtk.sources import WnEntitySource


def main() -> None:
    mentions = [
        Entity(
            id="m1",
            labels=["bank"],
            context="The bank approved my loan application for a new lending institution",
        ),
    ]
    senses = WnEntitySource(lang="en")

    results = LeskLinker().link(mentions, senses, blocking=ExactMatch())
    for result in results:
        print(f"{result.source_id} -> {result.target_id} (score={result.score})")
        print(f"  alternatives: {result.alternatives}")

    target = senses.get(results[0].target_id)
    assert target is not None
    print(f"  gloss: {target.description}")


if __name__ == "__main__":
    main()

Run with:

uv run python examples/wn_wsd.py
m1 -> oewn-08437235-n (score=2.0)
  alternatives: ['oewn-00170126-n', 'oewn-02790795-n', 'oewn-04146942-n', 'oewn-08479077-n', 'oewn-09236341-n', 'oewn-09236472-n', 'oewn-09236735-n', 'oewn-13377435-n', 'oewn-13389491-n']
  gloss: a financial institution that accepts deposits and channels the money into lending activities