File size: 2,354 Bytes
d916065
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Natural Language Toolkit: Parser API
#
# Copyright (C) 2001-2023 NLTK Project
# Author: Steven Bird <[email protected]>
#         Edward Loper <[email protected]>
# URL: <https://www.nltk.org/>
# For license information, see LICENSE.TXT
#

import itertools

from nltk.internals import overridden


class ParserI:
    """

    A processing class for deriving trees that represent possible

    structures for a sequence of tokens.  These tree structures are

    known as "parses".  Typically, parsers are used to derive syntax

    trees for sentences.  But parsers can also be used to derive other

    kinds of tree structure, such as morphological trees and discourse

    structures.



    Subclasses must define:

      - at least one of: ``parse()``, ``parse_sents()``.



    Subclasses may define:

      - ``grammar()``

    """

    def grammar(self):
        """

        :return: The grammar used by this parser.

        """
        raise NotImplementedError()

    def parse(self, sent, *args, **kwargs):
        """

        :return: An iterator that generates parse trees for the sentence.

            When possible this list is sorted from most likely to least likely.



        :param sent: The sentence to be parsed

        :type sent: list(str)

        :rtype: iter(Tree)

        """
        if overridden(self.parse_sents):
            return next(self.parse_sents([sent], *args, **kwargs))
        elif overridden(self.parse_one):
            return (
                tree
                for tree in [self.parse_one(sent, *args, **kwargs)]
                if tree is not None
            )
        elif overridden(self.parse_all):
            return iter(self.parse_all(sent, *args, **kwargs))
        else:
            raise NotImplementedError()

    def parse_sents(self, sents, *args, **kwargs):
        """

        Apply ``self.parse()`` to each element of ``sents``.

        :rtype: iter(iter(Tree))

        """
        return (self.parse(sent, *args, **kwargs) for sent in sents)

    def parse_all(self, sent, *args, **kwargs):
        """:rtype: list(Tree)"""
        return list(self.parse(sent, *args, **kwargs))

    def parse_one(self, sent, *args, **kwargs):
        """:rtype: Tree or None"""
        return next(self.parse(sent, *args, **kwargs), None)