class documentation

A RecursiveDescentParser that allows you to step through the parsing process, performing a single operation at a time.

The initialize method is used to start parsing a text. expand expands the first element on the frontier using a single CFG production, and match matches the first element on the frontier against the next text token. backtrack undoes the most recent expand or match operation. step performs a single expand, match, or backtrack operation. parses returns the set of parses that have been found by the parser.

See Also
nltk.grammar
Method __init__ Create a new RecursiveDescentParser, that uses grammar to parse texts.
Method backtrack Return the parser to its state before the most recent match or expand operation. Calling undo repeatedly return the parser to successively earlier states. If no match or expand operations have been performed, ...
Method currently_complete No summary
Method expand Expand the first element of the frontier. In particular, if the first element of the frontier is a subtree whose node type is equal to production's left hand side, then add a child to that subtree for each element of ...
Method expandable_productions No summary
Method frontier No summary
Method initialize Start parsing a given text. This sets the parser's tree to the start symbol, its frontier to the root node, and its remaining text to token['SUBTOKENS'].
Method match Match the first element of the frontier. In particular, if the first element of the frontier has the same type as the next text token, then substitute the text token into the tree.
Method parse When possible this list is sorted from most likely to least likely.
Method parses No summary
Method remaining_text No summary
Method set_grammar Change the grammar used to parse texts.
Method step Perform a single parsing operation. If an untried match is possible, then perform the match, and return the matched token. If an untried expansion is possible, then perform the expansion, and return the production that it is based on...
Method tree No summary
Method untried_expandable_productions No summary
Method untried_match No summary
Method _freeze Undocumented
Method _parse A stub version of _parse that sets the parsers current state to the given arguments. In RecursiveDescentParser, the _parse method is used to recursively continue parsing a text. SteppingRecursiveDescentParser...
Instance Variable _frontier Undocumented
Instance Variable _grammar Undocumented
Instance Variable _history A list of (rtext, tree, frontier) tripples, containing the previous states of the parser. This history is used to implement the backtrack operation.
Instance Variable _parses Undocumented
Instance Variable _rtext Undocumented
Instance Variable _tree Undocumented
Instance Variable _tried_e A record of all productions that have been tried for a given tree. This record is used by expand to perform the next untried production.
Instance Variable _tried_m A record of what tokens have been matched for a given tree. This record is used by step to decide whether or not to match a token.

Inherited from RecursiveDescentParser:

Method grammar No summary
Method trace Set the level of tracing output that should be generated when parsing a text.
Method _expand No summary
Method _match No summary
Method _production_to_tree No summary
Method _trace_backtrack Undocumented
Method _trace_expand Undocumented
Method _trace_fringe Print trace output displaying the fringe of tree. The fringe of tree consists of all of its leaves and all of its childless subtrees.
Method _trace_match Undocumented
Method _trace_start Undocumented
Method _trace_succeed Undocumented
Method _trace_tree Print trace output displaying the parser's current state.
Instance Variable _trace Undocumented

Inherited from ParserI (via RecursiveDescentParser):

Method parse_all No summary
Method parse_one No summary
Method parse_sents Apply self.parse() to each element of sents. :rtype: iter(iter(Tree))
def __init__(self, grammar, trace=0): (source)

Create a new RecursiveDescentParser, that uses grammar to parse texts.

Parameters
grammar:CFGThe grammar used to parse texts.
trace:intThe level of tracing that should be used when parsing a text. 0 will generate no tracing output; and higher numbers will produce more verbose tracing output.
def backtrack(self): (source)

Return the parser to its state before the most recent match or expand operation. Calling undo repeatedly return the parser to successively earlier states. If no match or expand operations have been performed, undo will make no changes.

Returns
booltrue if an operation was successfully undone.
def currently_complete(self): (source)
Returns
boolWhether the parser's current state represents a complete parse.
def expand(self, production=None): (source)

Expand the first element of the frontier. In particular, if the first element of the frontier is a subtree whose node type is equal to production's left hand side, then add a child to that subtree for each element of production's right hand side. If production is not specified, then use the first untried expandable production. If all expandable productions have been tried, do nothing.

Returns
Production or NoneThe production used to expand the frontier, if an expansion was performed. If no expansion was performed, return None.
def expandable_productions(self): (source)
Returns
list(Production)A list of all the productions for which expansions are available for the current parser state.
def frontier(self): (source)
Returns
list(tuple(int))A list of the tree locations of all subtrees that have not yet been expanded, and all leaves that have not yet been matched.
def initialize(self, tokens): (source)

Start parsing a given text. This sets the parser's tree to the start symbol, its frontier to the root node, and its remaining text to token['SUBTOKENS'].

def match(self): (source)

Match the first element of the frontier. In particular, if the first element of the frontier has the same type as the next text token, then substitute the text token into the tree.

Returns
str or NoneThe token matched, if a match operation was performed. If no match was performed, return None
def parse(self, tokens): (source)

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

Parameters
tokensUndocumented
sent:list(str)The sentence to be parsed
Returns
iter(Tree)An iterator that generates parse trees for the sentence.
def parses(self): (source)
Returns
list of TreeAn iterator of the parses that have been found by this parser so far.
def remaining_text(self): (source)
Returns
list(str)The portion of the text that is not yet covered by the tree.
def set_grammar(self, grammar): (source)

Change the grammar used to parse texts.

Parameters
grammar:CFGThe new grammar.
def step(self): (source)

Perform a single parsing operation. If an untried match is possible, then perform the match, and return the matched token. If an untried expansion is possible, then perform the expansion, and return the production that it is based on. If backtracking is possible, then backtrack, and return True. Otherwise, return None.

Returns
Production or String or boolNone if no operation was performed; a token if a match was performed; a production if an expansion was performed; and True if a backtrack operation was performed.
def tree(self): (source)
Returns
TreeA partial structure for the text that is currently being parsed. The elements specified by the frontier have not yet been expanded or matched.
def untried_expandable_productions(self): (source)
Returns
list(Production)A list of all the untried productions for which expansions are available for the current parser state.
def untried_match(self): (source)
Returns
boolWhether the first element of the frontier is a token that has not yet been matched.
def _freeze(self, tree): (source)

Undocumented

def _parse(self, remaining_text, tree, frontier): (source)

A stub version of _parse that sets the parsers current state to the given arguments. In RecursiveDescentParser, the _parse method is used to recursively continue parsing a text. SteppingRecursiveDescentParser overrides it to capture these recursive calls. It records the parser's old state in the history (to allow for backtracking), and updates the parser's new state using the given arguments. Finally, it returns [1], which is used by match and expand to detect whether their operations were successful.

Returns
list of int[1]
_frontier = (source)

Undocumented

_history: list = (source)

A list of (rtext, tree, frontier) tripples, containing the previous states of the parser. This history is used to implement the backtrack operation.

_parses: list = (source)

Undocumented

Undocumented

Undocumented

_tried_e: dict = (source)

A record of all productions that have been tried for a given tree. This record is used by expand to perform the next untried production.

_tried_m: dict = (source)

A record of what tokens have been matched for a given tree. This record is used by step to decide whether or not to match a token.