gluesemantics_malt.doctest 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. .. Copyright (C) 2001-2020 NLTK Project
  2. .. For license information, see LICENSE.TXT
  3. .. see also: gluesemantics.doctest
  4. ==============================================================================
  5. Glue Semantics
  6. ==============================================================================
  7. >>> from nltk.sem.glue import *
  8. >>> nltk.sem.logic._counter._value = 0
  9. --------------------------------
  10. Initialize the Dependency Parser
  11. --------------------------------
  12. >>> from nltk.parse.malt import MaltParser
  13. >>> tagger = RegexpTagger(
  14. ... [('^(John|Mary)$', 'NNP'),
  15. ... ('^(sees|chases)$', 'VB'),
  16. ... ('^(a)$', 'ex_quant'),
  17. ... ('^(every)$', 'univ_quant'),
  18. ... ('^(girl|dog)$', 'NN')
  19. ... ])
  20. >>> depparser = MaltParser(tagger=tagger)
  21. --------------------
  22. Automated Derivation
  23. --------------------
  24. >>> glue = Glue(depparser=depparser)
  25. >>> readings = glue.parse_to_meaning('every girl chases a dog'.split())
  26. >>> for reading in sorted([r.simplify().normalize() for r in readings], key=str):
  27. ... print(reading.normalize())
  28. all z1.(girl(z1) -> exists z2.(dog(z2) & chases(z1,z2)))
  29. exists z1.(dog(z1) & all z2.(girl(z2) -> chases(z2,z1)))
  30. >>> drtglue = DrtGlue(depparser=depparser)
  31. >>> readings = drtglue.parse_to_meaning('every girl chases a dog'.split())
  32. >>> for reading in sorted([r.simplify().normalize() for r in readings], key=str):
  33. ... print(reading)
  34. ([],[(([z1],[girl(z1)]) -> ([z2],[dog(z2), chases(z1,z2)]))])
  35. ([z1],[dog(z1), (([z2],[girl(z2)]) -> ([],[chases(z2,z1)]))])
  36. --------------
  37. With inference
  38. --------------
  39. Checking for equality of two DRSs is very useful when generating readings of a sentence.
  40. For example, the ``glue`` module generates two readings for the sentence
  41. *John sees Mary*:
  42. >>> from nltk.sem.glue import DrtGlue
  43. >>> readings = drtglue.parse_to_meaning('John sees Mary'.split())
  44. >>> for drs in sorted([r.simplify().normalize() for r in readings], key=str):
  45. ... print(drs)
  46. ([z1,z2],[John(z1), Mary(z2), sees(z1,z2)])
  47. ([z1,z2],[Mary(z1), John(z2), sees(z2,z1)])
  48. However, it is easy to tell that these two readings are logically the
  49. same, and therefore one of them is superfluous. We can use the theorem prover
  50. to determine this equivalence, and then delete one of them. A particular
  51. theorem prover may be specified, or the argument may be left off to use the
  52. default.
  53. >>> readings[0].equiv(readings[1])
  54. True