class SteppingShiftReduceParser(ShiftReduceParser): (source)
Constructor: SteppingShiftReduceParser(grammar, trace)
A ShiftReduceParser that allows you to setp through the parsing process, performing a single operation at a time. It also allows you to change the parser's grammar midway through parsing a text.
The initialize method is used to start parsing a text. shift performs a single shift operation, and reduce performs a single reduce operation. step will perform a single reduce operation if possible; otherwise, it will perform a single shift operation. parses returns the set of parses that have been found by the parser.
See Also | |
nltk.grammar |
Method | __init__ |
Create a new ShiftReduceParser, that uses grammar to parse texts. |
Method | initialize |
Start parsing a given text. This sets the parser's stack to [] and sets its remaining text to tokens. |
Method | parse |
When possible this list is sorted from most likely to least likely. |
Method | parses |
No summary |
Method | reduce |
Use production to combine the rightmost stack elements into a single Tree. If production does not match the rightmost stack elements, then do nothing. |
Method | reducible |
No summary |
Method | remaining |
No summary |
Method | set |
Change the grammar used to parse texts. |
Method | shift |
Move a token from the beginning of the remaining text to the end of the stack. If there are no more tokens in the remaining text, then do nothing. |
Method | stack |
No summary |
Method | step |
Perform a single parsing operation. If a reduction is possible, then perform that reduction, and return the production that it is based on. Otherwise, if a shift is possible, then perform it, and return True... |
Method | undo |
Return the parser to its state before the most recent shift or reduce operation. Calling undo repeatedly return the parser to successively earlier states. If no shift or reduce operations have been performed, ... |
Instance Variable | _grammar |
Undocumented |
Instance Variable | _history |
A list of (stack, remaining_text) pairs, containing all of the previous states of the parser. This history is used to implement the undo operation. |
Instance Variable | _remaining |
Undocumented |
Instance Variable | _stack |
Undocumented |
Inherited from ShiftReduceParser
:
Method | grammar |
No summary |
Method | trace |
Set the level of tracing output that should be generated when parsing a text. |
Method | _check |
Check to make sure that all of the CFG productions are potentially useful. If any productions can never be used, then print a warning. |
Method | _match |
No summary |
Method | _reduce |
Find a CFG production whose right hand side matches the rightmost stack elements; and combine those stack elements into a single Tree, with the node specified by the production's left-hand side. If more than one CFG production matches the stack, then use the production that is listed earliest in the grammar... |
Method | _shift |
Move a token from the beginning of remaining_text to the end of stack. |
Method | _trace |
Print trace output displaying that production was used to reduce stack. |
Method | _trace |
Print trace output displaying that a token has been shifted. |
Method | _trace |
Print trace output displaying the given stack and text. |
Instance Variable | _trace |
Undocumented |
Inherited from ParserI
(via ShiftReduceParser
):
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 ShiftReduceParser, that uses grammar to parse texts.
Parameters | |
grammar:Grammar | 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. |
Start parsing a given text. This sets the parser's stack to [] and sets its remaining text to tokens.
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. |
Use production to combine the rightmost stack elements into a single Tree. If production does not match the rightmost stack elements, then do nothing.
Returns | |
Production or None | The production used to reduce the stack, if a reduction was performed. If no reduction was performed, return None. |
Returns | |
list(Production) | A list of the productions for which reductions are available for the current parser state. |
Move a token from the beginning of the remaining text to the end of the stack. If there are no more tokens in the remaining text, then do nothing.
Returns | |
bool | True if the shift operation was successful. |
Perform a single parsing operation. If a reduction is possible, then perform that reduction, and return the production that it is based on. Otherwise, if a shift is possible, then perform it, and return True. Otherwise, return False.
Returns | |
Production or bool | False if no operation was performed; True if a shift was performed; and the CFG production used to reduce if a reduction was performed. |
Return the parser to its state before the most recent shift or reduce operation. Calling undo repeatedly return the parser to successively earlier states. If no shift or reduce operations have been performed, undo will make no changes.
Returns | |
bool | true if an operation was successfully undone. |