api.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # Natural Language Toolkit: Chunk parsing API
  2. #
  3. # Copyright (C) 2001-2019 NLTK Project
  4. # Author: Edward Loper <edloper@gmail.com>
  5. # Steven Bird <stevenbird1@gmail.com> (minor additions)
  6. # URL: <http://nltk.org/>
  7. # For license information, see LICENSE.TXT
  8. ##//////////////////////////////////////////////////////
  9. ## Chunk Parser Interface
  10. ##//////////////////////////////////////////////////////
  11. from nltk.parse import ParserI
  12. from nltk.chunk.util import ChunkScore
  13. class ChunkParserI(ParserI):
  14. """
  15. A processing interface for identifying non-overlapping groups in
  16. unrestricted text. Typically, chunk parsers are used to find base
  17. syntactic constituents, such as base noun phrases. Unlike
  18. ``ParserI``, ``ChunkParserI`` guarantees that the ``parse()`` method
  19. will always generate a parse.
  20. """
  21. def parse(self, tokens):
  22. """
  23. Return the best chunk structure for the given tokens
  24. and return a tree.
  25. :param tokens: The list of (word, tag) tokens to be chunked.
  26. :type tokens: list(tuple)
  27. :rtype: Tree
  28. """
  29. raise NotImplementedError()
  30. def evaluate(self, gold):
  31. """
  32. Score the accuracy of the chunker against the gold standard.
  33. Remove the chunking the gold standard text, rechunk it using
  34. the chunker, and return a ``ChunkScore`` object
  35. reflecting the performance of this chunk peraser.
  36. :type gold: list(Tree)
  37. :param gold: The list of chunked sentences to score the chunker on.
  38. :rtype: ChunkScore
  39. """
  40. chunkscore = ChunkScore()
  41. for correct in gold:
  42. chunkscore.score(correct, self.parse(correct.leaves()))
  43. return chunkscore