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.

initialize(tokens)[source]

Begin parsing the given tokens.

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.

strategy()[source]

Return the strategy used by this parser.

grammar()[source]

Return the grammar used by this parser.

chart()[source]

Return the chart that is used by this parser.

current_chartrule()[source]

Return the chart rule used to generate the most recent edge.

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.

set_grammar(grammar)[source]

Change the grammar used by the parser.

set_chart(chart)[source]

Load a given chart into the chart parser.

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)

chart_parse(tokens, trace=None)[source]

Return the final parse Chart from which all possible parse trees can be extracted.

Parameters

tokens (list(str)) – The sentence to be parsed

Return type

Chart

parse_all(sent, *args, **kwargs)[source]
Return type

list(Tree)

parse_one(sent, *args, **kwargs)[source]
Return type

Tree or None

parse_sents(sents, *args, **kwargs)[source]

Apply self.parse() to each element of sents. :rtype: iter(iter(Tree))