nltk.parse.SteppingChartParser¶
- class nltk.parse.SteppingChartParser[source]¶
Bases:
ChartParser
A
ChartParser
that allows you to step through the parsing process, adding a single edge at a time. It also allows you to change the parser’s strategy or grammar midway through parsing a text.The
initialize
method is used to start parsing a text.step
adds a single edge to the chart.set_strategy
changes the strategy used by the chart parser.parses
returns the set of parses that has been found by the chart parser.- Variables
_restart – Records whether the parser’s strategy, grammar, or chart has been changed. If so, then
step
must restart the parsing algorithm.
- __init__(grammar, strategy=[], trace=0)[source]¶
Create a new chart parser, that uses
grammar
to parse texts.- Parameters
grammar (CFG) – The grammar used to parse texts.
strategy (list(ChartRuleI)) – A list of rules that should be used to decide what edges to add to the chart (top-down strategy by default).
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.trace_chart_width (int) – The default total width reserved for the chart in trace output. The remainder of each line will be used to display edges.
use_agenda (bool) – Use an optimized agenda-based algorithm, if possible.
chart_class – The class that should be used to create the parse charts.
- step()[source]¶
Return a generator that adds edges to the chart, one at a time. Each time the generator is resumed, it adds a single edge and yields that edge. If no more edges can be added, then it yields None.
If the parser’s strategy, grammar, or chart is changed, then the generator will continue adding edges using the new strategy, grammar, or chart.
Note that this generator never terminates, since the grammar or strategy might be changed to values that would add new edges. Instead, it yields None when no more edges can be added with the current strategy and grammar.
- parses(tree_class=<class 'nltk.tree.tree.Tree'>)[source]¶
Return the parse trees currently contained in the chart.
- set_strategy(strategy)[source]¶
Change the strategy that the parser uses to decide which edges to add to the chart.
- Parameters
strategy (list(ChartRuleI)) – A list of rules that should be used to decide what edges to add to the chart.
- parse(tokens, tree_class=<class 'nltk.tree.tree.Tree'>)[source]¶
- Returns
An iterator that generates parse trees for the sentence. When possible this list is sorted from most likely to least likely.
- Parameters
sent (list(str)) – The sentence to be parsed
- Return type
iter(Tree)