wordnet_lch.doctest 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. .. Copyright (C) 2001-2020 NLTK Project
  2. .. For license information, see LICENSE.TXT
  3. ===============================
  4. WordNet Lowest Common Hypernyms
  5. ===============================
  6. Wordnet's lowest_common_hypernyms() method is based used to locate the
  7. lowest single hypernym that is shared by two given words:
  8. >>> from nltk.corpus import wordnet as wn
  9. >>> wn.synset('kin.n.01').lowest_common_hypernyms(wn.synset('mother.n.01'))
  10. [Synset('relative.n.01')]
  11. >>> wn.synset('policeman.n.01').lowest_common_hypernyms(wn.synset('chef.n.01'))
  12. [Synset('person.n.01')]
  13. This method generally returns a single result, but in some cases, more than one
  14. valid LCH is possible:
  15. >>> wn.synset('body.n.09').lowest_common_hypernyms(wn.synset('sidereal_day.n.01'))
  16. [Synset('attribute.n.02'), Synset('measure.n.02')]
  17. In some cases, lowest_common_hypernyms() can return one of the synsets which was
  18. passed to it as an argument:
  19. >>> wn.synset('woman.n.01').lowest_common_hypernyms(wn.synset('girlfriend.n.02'))
  20. [Synset('woman.n.01')]
  21. In NLTK 3.0a2 the behavior of lowest_common_hypernyms() was changed to give more
  22. accurate results in a small set of cases, generally when dealing with nouns describing
  23. social roles or jobs. To emulate the pre v3.0a2 behavior, you can set the use_min_depth=True
  24. flag:
  25. >>> wn.synset('policeman.n.01').lowest_common_hypernyms(wn.synset('chef.n.01'))
  26. [Synset('person.n.01')]
  27. >>> wn.synset('policeman.n.01').lowest_common_hypernyms(wn.synset('chef.n.01'), use_min_depth=True)
  28. [Synset('organism.n.01')]
  29. In some cases use_min_depth=True may return more or fewer results than the default
  30. behavior:
  31. >>> wn.synset('woman.n.01').lowest_common_hypernyms(wn.synset('girlfriend.n.02'))
  32. [Synset('woman.n.01')]
  33. >>> wn.synset('woman.n.01').lowest_common_hypernyms(wn.synset('girlfriend.n.02'), use_min_depth=True)
  34. [Synset('organism.n.01'), Synset('woman.n.01')]
  35. In the general case, however, they tend to return the same results:
  36. >>> wn.synset('body.n.09').lowest_common_hypernyms(wn.synset('sidereal_day.n.01'))
  37. [Synset('attribute.n.02'), Synset('measure.n.02')]
  38. >>> wn.synset('body.n.09').lowest_common_hypernyms(wn.synset('sidereal_day.n.01'), use_min_depth=True)
  39. [Synset('attribute.n.02'), Synset('measure.n.02')]