natural language toolkit parser api c 20012023 nltk project steven bird stevenbird1gmail com edward loper edlopergmail com url https www nltk org for license information see license txt 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 parsesents subclasses may define grammar return the grammar used by this parser 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 liststr rtype itertree apply self parse to each element of sents rtype iteritertree rtype listtree return listself parsesent args kwargs def parseoneself sent args kwargs natural language toolkit parser api c 2001 2023 nltk project steven bird stevenbird1 gmail com edward loper edloper gmail com url https www nltk org for license information see license txt 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 return the grammar used by this parser 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 apply self parse to each element of sents rtype iter iter tree rtype list tree rtype tree or none
import itertools

from nltk.internals import overridden


class ParserI:
    

    def grammar(self):
        
        raise NotImplementedError()

    def parse(self, sent, *args, **kwargs):
        
        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):
        
        return (self.parse(sent, *args, **kwargs) for sent in sents)

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

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