class SteppingRecursiveDescentParser(RecursiveDescentParser): (source)
Constructor: SteppingRecursiveDescentParser(grammar, trace)
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 |
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 |
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 |
No summary |
Method | set |
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 |
No summary |
Method | untried |
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 |
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 |
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 |
No summary |
Method | _trace |
Undocumented |
Method | _trace |
Undocumented |
Method | _trace |
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 |
Undocumented |
Method | _trace |
Undocumented |
Method | _trace |
Undocumented |
Method | _trace |
Print trace output displaying the parser's current state. |
Instance Variable | _trace |
Undocumented |
Inherited from ParserI
(via RecursiveDescentParser
):
Method | parse |
No summary |
Method | parse |
No summary |
Method | parse |
Apply self.parse() to each element of sents. :rtype: iter(iter(Tree)) |
Create a new RecursiveDescentParser, that uses grammar to parse texts.
Parameters | |
grammar:CFG | The grammar used to parse texts. |
trace:int | The 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. |
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 | |
bool | true if an operation was successfully undone. |
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 None | The production used to expand the frontier, if an expansion was performed. If no expansion was performed, return None. |
Returns | |
list(Production) | A list of all the productions for which expansions are available for the current parser state. |
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. |
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'].
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 None | The token matched, if a match operation was performed. If no match was performed, return None |
When possible this list is sorted from most likely to least likely.
Parameters | |
tokens | Undocumented |
sent:list(str) | The sentence to be parsed |
Returns | |
iter(Tree) | An iterator that generates parse trees for the sentence. |
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 bool | None 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. |
Returns | |
Tree | A partial structure for the text that is currently being parsed. The elements specified by the frontier have not yet been expanded or matched. |
Returns | |
list(Production) | A list of all the untried productions for which expansions are available for the current parser state. |
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] |
A list of (rtext, tree, frontier) tripples, containing the previous states of the parser. This history is used to implement the backtrack operation.