Package nltk :: Package parse :: Module sr :: Class SteppingShiftReduceParser
[hide private]
[frames] | no frames]

Class SteppingShiftReduceParser

source code

   object --+        
            |        
  api.ParserI --+    
                |    
ShiftReduceParser --+
                    |
                   SteppingShiftReduceParser
Known Subclasses:

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.cfg

Instance Methods [hide private]
 
__init__(self, grammar, trace=0)
Create a new ShiftReduceParser, that uses grammar to parse texts.
source code
list of Tree
nbest_parse(self, tokens, n=None)
Returns: A list of parse trees that represent possible structures for the given sentence.
source code
list of String and Tree
stack(self)
Returns: The parser's stack.
source code
list of String
remaining_text(self)
Returns: The portion of the text that is not yet covered by the stack.
source code
 
initialize(self, tokens)
Start parsing a given text.
source code
Production or boolean
step(self)
Perform a single parsing operation.
source code
boolean
shift(self)
Move a token from the beginning of the remaining text to the end of the stack.
source code
Production or None
reduce(self, production=None)
Use production to combine the rightmost stack elements into a single Tree.
source code
boolean
undo(self)
Return the parser to its state before the most recent shift or reduce operation.
source code
list of Production
reducible_productions(self)
Returns: A list of the productions for which reductions are available for the current parser state.
source code
list of Tree
parses(self)
Returns: A list of the parses that have been found by this parser so far.
source code
 
set_grammar(self, grammar)
Change the grammar used to parse texts.
source code

Inherited from ShiftReduceParser: grammar, parse, trace

Inherited from api.ParserI: batch_iter_parse, batch_nbest_parse, batch_parse, batch_prob_parse, iter_parse, prob_parse

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __str__

    Deprecated

Inherited from api.ParserI: batch_test, get_parse, get_parse_dict, get_parse_list, get_parse_prob

Instance Variables [hide private]
  _history
A list of (stack, remaining_text) pairs, containing all of the previous states of the parser.
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, grammar, trace=0)
(Constructor)

source code 

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

Parameters:
  • grammar - The grammar used to parse texts.
  • trace - 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.
Overrides: ShiftReduceParser.__init__
(inherited documentation)

nbest_parse(self, tokens, n=None)

source code 
Parameters:
  • sent - The sentence to be parsed
  • n - The maximum number of trees to return.
Returns: list of Tree
A list of parse trees that represent possible structures for the given sentence. When possible, this list is sorted from most likely to least likely. If n is specified, then the returned list will contain at most n parse trees.
Overrides: api.ParserI.nbest_parse
(inherited documentation)

stack(self)

source code 
Returns: list of String and Tree
The parser's stack.

remaining_text(self)

source code 
Returns: list of String
The portion of the text that is not yet covered by the stack.

initialize(self, tokens)

source code 

Start parsing a given text. This sets the parser's stack to [] and sets its remaining text to tokens.

step(self)

source code 

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 1. Otherwise, return 0.

Returns: Production or boolean
0 if no operation was performed; 1 if a shift was performed; and the CFG production used to reduce if a reduction was performed.

shift(self)

source code 

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: boolean
True if the shift operation was successful.

reduce(self, production=None)

source code 

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.

undo(self)

source code 

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: boolean
true if an operation was successfully undone.

reducible_productions(self)

source code 
Returns: list of Production
A list of the productions for which reductions are available for the current parser state.

parses(self)

source code 
Returns: list of Tree
A list of the parses that have been found by this parser so far.

set_grammar(self, grammar)

source code 

Change the grammar used to parse texts.

Parameters:
  • grammar (CFG) - The new grammar.

Instance Variable Details [hide private]

_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.