Skip to main content

CI PyPI license FOSSA Status

pywsd

Python Implementations of Word Sense Disambiguation (WSD) technologies:

  • Lesk algorithms

    • Original Lesk (Lesk, 1986)
    • Adapted/Extended Lesk (Banerjee and Pederson, 2002/2003)
    • Simple Lesk (with definition, example(s) and hyper+hyponyms)
    • Cosine Lesk (use cosines to calculate overlaps instead of using raw counts)
  • Maximizing Similarity (see also, Pedersen et al. (2003))

    • Path-based: path, Wu-Palmer (1994), Leacock-Chodorow (1998)
    • Information-content: Resnik (1995), Jiang-Conrath (1997), Lin (1998)
  • Baselines

    • Random sense
    • First WordNet sense
    • Highest lemma counts

Requires Python 3.10+. Python 2 users: last supported release is pywsd==1.1.7.

Install

pip install -U pywsd

On first use pywsd will auto-download the Open English WordNet (oewn:2024) via wn to ~/.wn_data/. If you prefer to pre-download:

python -c "import wn; wn.download('oewn:2024')"

Information-content files for Resnik / Jiang-Conrath / Lin similarity are bundled with the package (Brown + Wikipedia, computed against oewn:2024). No additional downloads are required for those metrics to work.

Usage

>>> from pywsd.lesk import simple_lesk
>>> sent = 'I went to the bank to deposit my money'
>>> answer = simple_lesk(sent, 'bank', pos='n')
>>> answer
Synset('oewn-08437235-n')
>>> answer.lemmas()
['depository financial institution', 'bank', 'banking concern', 'banking company']
>>> answer.definition()
'a financial institution that accepts deposits and channels the money into lending activities'

All-words disambiguation:

>>> from pywsd import disambiguate
>>> disambiguate('I went to the bank to deposit my money')
[('I', None), ('went', Synset('oewn-02659957-v')), ('to', None), ('the', None),
 ('bank', Synset('oewn-08437235-n')), ('to', None), ('deposit', Synset('oewn-...')),
 ('my', None), ('money', Synset('oewn-...'))]

With a different similarity metric:

>>> from pywsd.similarity import max_similarity as maxsim
>>> disambiguate('I went to the bank to deposit my money',
...              algorithm=maxsim, similarity_option='wup', keepLemmas=True)

Synset signatures (for simple Lesk). Signatures are computed on demand and memoized with functools.lru_cache, so repeated calls in the same process are essentially free:

>>> from pywsd.lesk import synset_signatures
>>> from pywsd.baseline import first_sense
>>> dog = first_sense('dog', pos='n')
>>> sorted(synset_signatures(dog))[:8]
['bark', 'barker', 'basenji', 'belgian griffon', 'bow-wow', 'breed', ...]

Information-content-based similarity (Resnik / Jiang-Conrath / Lin) uses the bundled Wikipedia IC by default. Swap corpus or install your own frequency dict:

>>> import pywsd._wordnet
>>> pywsd._wordnet.IC_CORPUS = 'brown'          # switch to bundled Brown IC
>>> # or supply your own (e.g. computed via wn.ic.compute)
>>> # pywsd._wordnet.set_ic(my_freq_dict)

Note on synset identifiers: this is OEWN 2024, so synsets are identified oewn-<offset>-<pos> rather than the PWN 3.0 form bank.n.01. Use synset.lemmas(), .definition(), and .pos for human-readable attributes.


Cite

To cite pywsd:

Liling Tan. 2014. Pywsd: Python Implementations of Word Sense Disambiguation (WSD) Technologies [software]. Retrieved from https://github.com/alvations/pywsd

In bibtex:

@misc{pywsd14,
author =   {Liling Tan},
title =    {Pywsd: Python Implementations of Word Sense Disambiguation (WSD) Technologies [software]},
howpublished = {https://github.com/alvations/pywsd},
year = {2014}
}

References

  • Michael Lesk. 1986. Automatic sense disambiguation using machine readable dictionaries: how to tell a pine cone from an ice cream cone. In Proceedings of the 5th annual international conference on Systems documentation (SIGDOC '86), Virginia DeBuys (Ed.). ACM, New York, NY, USA, 24-26. DOI=10.1145/318723.318728 http://doi.acm.org/10.1145/318723.318728

  • Satanjeev Banerjee and Ted Pedersen. 2002. An Adapted Lesk Algorithm for Word Sense Disambiguation Using WordNet. In Proceedings of the Third International Conference on Computational Linguistics and Intelligent Text Processing (CICLing '02), Alexander F. Gelbukh (Ed.). Springer-Verlag, London, UK, UK, 136-145.

  • Satanjeev Banerjee and Ted Pedersen. 2003. Extended gloss overlaps as a measure of semantic relatedness. In Proceedings of the Eighteenth International Joint Conference on Artificial Intelligence, pages 805–810, Acapulco.

  • Jay J. Jiang and David W. Conrath. 1997. Semantic similarity based on corpus statistics and lexical taxonomy. In Proceedings of International Conference on Research in Computational Linguistics, Taiwan.

  • Claudia Leacock and Martin Chodorow. 1998. Combining local context and WordNet similarity for word sense identification. In Fellbaum 1998, pp. 265–283.

  • Lee, Yoong Keok, Hwee Tou Ng, and Tee Kiah Chia. "Supervised word sense disambiguation with support vector machines and multiple knowledge sources." Senseval-3: Third International Workshop on the Evaluation of Systems for the Semantic Analysis of Text. 2004.

  • Dekang Lin. 1998. An information-theoretic definition of similarity. In Proceedings of the 15th International Conference on Machine Learning, Madison, WI.

  • Linlin Li, Benjamin Roth and Caroline Sporleder. 2010. Topic Models for Word Sense Disambiguation and Token-based Idiom Detection. The 48th Annual Meeting of the Association for Computational Linguistics (ACL). Uppsala, Sweden.

  • Andrea Moro, Roberto Navigli, Francesco Maria Tucci and Rebecca J. Passonneau. 2014. Annotating the MASC Corpus with BabelNet. In Proceedings of the Ninth International Conference on Language Resources and Evaluation (LREC'14). Reykjavik, Iceland.

  • Zhi Zhong and Hwee Tou Ng. 2010. It makes sense: a wide-coverage word sense disambiguation system for free text. In Proceedings of the ACL 2010 System Demonstrations (ACLDemos '10). Association for Computational Linguistics, Stroudsburg, PA, USA, 78-83.

  • Steven Bird, Ewan Klein, and Edward Loper. 2009. Natural Language Processing with Python (1st ed.). O'Reilly Media, Inc..

  • Eneko Agirre and Aitor Soroa. 2009. Personalizing PageRank for Word Sense Disambiguation. Proceedings of the 12th conference of the European chapter of the Association for Computational Linguistics (EACL-2009). Athens, Greece.

  • P. Resnik. 1995. Using Information Content to Evaluate Semantic Similarity in a Taxonomy. In Proceedings of IJCAI-95. arXiv:cmp-lg/9511007.

Release files for pywsd 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pywsd 1.3.0
File Size Uploaded
pywsd-1.3.0.tar.gz 1.4 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for pywsd 1.3.0
File Interpreter ABI Platform
pywsd-1.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 2.9 MB

Release files / pywsd-1.3.0.tar.gz

Download URL pywsd-1.3.0.tar.gz
Size 1.4 MB
Tags Source
SHA-256 checksum
How to use checksums
62ec74a4c2ab141f87959b3c8e2c0bf0e16eafc4c6a30957819bc4f893f05995
BLAKE2b-256 checksum
How to use checksums
7e808682420b883b1ac9d9435d362fc8083ad636443a19be5810d6cf489ecf11
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.4

Release files / pywsd-1.3.0-py3-none-any.whl

Download URL pywsd-1.3.0-py3-none-any.whl
Size 1.5 MB
Tags Python 3
SHA-256 checksum
How to use checksums
85d8d9eb2646e5058b056e2524521031ba3cfd010d7797d999abb7dd340c724b
BLAKE2b-256 checksum
How to use checksums
ac8f04644d44e1aa954536748a24ab32b99b5424c7ce9bff38233cc09b0fc72c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.4

Release history Release notifications | RSS feed

This release

1.3.0 This release

2 release files

1.2.5

2 release files

1.2.4

1 release file

1.2.3

1 release file

1.2.1

1 release file

1.2.0

1 release file

1.1.7

1 release file

1.1.6

1 release file

1.1.5

1 release file

1.1.4

1 release file

1.1.2

1 release file

1.1.1

1 release file

1.1.0

1 release file

1.0.4

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page