api.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # Natural Language Toolkit: Parser API
  2. #
  3. # Copyright (C) 2001-2019 NLTK Project
  4. # Author: Steven Bird <stevenbird1@gmail.com>
  5. # Edward Loper <edloper@gmail.com>
  6. # URL: <http://nltk.org/>
  7. # For license information, see LICENSE.TXT
  8. #
  9. import itertools
  10. from nltk.internals import overridden
  11. class ParserI(object):
  12. """
  13. A processing class for deriving trees that represent possible
  14. structures for a sequence of tokens. These tree structures are
  15. known as "parses". Typically, parsers are used to derive syntax
  16. trees for sentences. But parsers can also be used to derive other
  17. kinds of tree structure, such as morphological trees and discourse
  18. structures.
  19. Subclasses must define:
  20. - at least one of: ``parse()``, ``parse_sents()``.
  21. Subclasses may define:
  22. - ``grammar()``
  23. """
  24. def grammar(self):
  25. """
  26. :return: The grammar used by this parser.
  27. """
  28. raise NotImplementedError()
  29. def parse(self, sent, *args, **kwargs):
  30. """
  31. :return: An iterator that generates parse trees for the sentence.
  32. When possible this list is sorted from most likely to least likely.
  33. :param sent: The sentence to be parsed
  34. :type sent: list(str)
  35. :rtype: iter(Tree)
  36. """
  37. if overridden(self.parse_sents):
  38. return next(self.parse_sents([sent], *args, **kwargs))
  39. elif overridden(self.parse_one):
  40. return (
  41. tree
  42. for tree in [self.parse_one(sent, *args, **kwargs)]
  43. if tree is not None
  44. )
  45. elif overridden(self.parse_all):
  46. return iter(self.parse_all(sent, *args, **kwargs))
  47. else:
  48. raise NotImplementedError()
  49. def parse_sents(self, sents, *args, **kwargs):
  50. """
  51. Apply ``self.parse()`` to each element of ``sents``.
  52. :rtype: iter(iter(Tree))
  53. """
  54. return (self.parse(sent, *args, **kwargs) for sent in sents)
  55. def parse_all(self, sent, *args, **kwargs):
  56. """:rtype: list(Tree)"""
  57. return list(self.parse(sent, *args, **kwargs))
  58. def parse_one(self, sent, *args, **kwargs):
  59. """:rtype: Tree or None"""
  60. return next(self.parse(sent, *args, **kwargs), None)