wsd.doctest 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. .. Copyright (C) 2001-2019 NLTK Project
  2. .. For license information, see LICENSE.TXT
  3. .. -*- coding: utf-8 -*-
  4. =========================
  5. Word Sense Disambiguation
  6. =========================
  7. Lesk Algorithm
  8. --------------
  9. Performs the classic Lesk algorithm for Word Sense Disambiguation (WSD) using
  10. a the definitions of the ambiguous word.
  11. Given an ambiguous word and the context in which the word occurs, Lesk returns
  12. a Synset with the highest number of overlapping words between the context
  13. sentence and different definitions from each Synset.
  14. >>> from nltk.wsd import lesk
  15. >>> sent = ['I', 'went', 'to', 'the', 'bank', 'to', 'deposit', 'money', '.']
  16. >>> print(lesk(sent, 'bank', 'n'))
  17. Synset('savings_bank.n.02')
  18. >>> print(lesk(sent, 'bank'))
  19. Synset('savings_bank.n.02')
  20. The definitions for "bank" are:
  21. >>> from nltk.corpus import wordnet as wn
  22. >>> for ss in wn.synsets('bank'):
  23. ... print(ss, ss.definition())
  24. ...
  25. Synset('bank.n.01') sloping land (especially the slope beside a body of water)
  26. Synset('depository_financial_institution.n.01') a financial institution that accepts deposits and channels the money into lending activities
  27. Synset('bank.n.03') a long ridge or pile
  28. Synset('bank.n.04') an arrangement of similar objects in a row or in tiers
  29. Synset('bank.n.05') a supply or stock held in reserve for future use (especially in emergencies)
  30. Synset('bank.n.06') the funds held by a gambling house or the dealer in some gambling games
  31. Synset('bank.n.07') a slope in the turn of a road or track; the outside is higher than the inside in order to reduce the effects of centrifugal force
  32. Synset('savings_bank.n.02') a container (usually with a slot in the top) for keeping money at home
  33. Synset('bank.n.09') a building in which the business of banking transacted
  34. Synset('bank.n.10') a flight maneuver; aircraft tips laterally about its longitudinal axis (especially in turning)
  35. Synset('bank.v.01') tip laterally
  36. Synset('bank.v.02') enclose with a bank
  37. Synset('bank.v.03') do business with a bank or keep an account at a bank
  38. Synset('bank.v.04') act as the banker in a game or in gambling
  39. Synset('bank.v.05') be in the banking business
  40. Synset('deposit.v.02') put into a bank account
  41. Synset('bank.v.07') cover with ashes so to control the rate of burning
  42. Synset('trust.v.01') have confidence or faith in
  43. Test disambiguation of POS tagged `able`.
  44. >>> [(s, s.pos()) for s in wn.synsets('able')]
  45. [(Synset('able.a.01'), 'a'), (Synset('able.s.02'), 's'), (Synset('able.s.03'), 's'), (Synset('able.s.04'), 's')]
  46. >>> sent = 'people should be able to marry a person of their choice'.split()
  47. >>> lesk(sent, 'able')
  48. Synset('able.s.04')
  49. >>> lesk(sent, 'able', pos='a')
  50. Synset('able.a.01')
  51. Test behavior if there is are no matching senses.
  52. >>> lesk('John loves Mary'.split(), 'loves', synsets=[])