nltk.parse.featurechart module¶
Extension of chart parsing implementation to handle grammars with feature structures as nodes.
- class nltk.parse.featurechart.FeatureBottomUpChartParser[source]¶
Bases:
FeatureChartParser- __init__(grammar, **parser_args)[source]¶
Create a new chart parser, that uses
grammarto 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.
0will 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.
- class nltk.parse.featurechart.FeatureBottomUpLeftCornerChartParser[source]¶
Bases:
FeatureChartParser- __init__(grammar, **parser_args)[source]¶
Create a new chart parser, that uses
grammarto 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.
0will 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.
- class nltk.parse.featurechart.FeatureBottomUpPredictCombineRule[source]¶
Bases:
BottomUpPredictCombineRule
- class nltk.parse.featurechart.FeatureBottomUpPredictRule[source]¶
Bases:
BottomUpPredictRule
- class nltk.parse.featurechart.FeatureChart[source]¶
Bases:
ChartA Chart for feature grammars. :see:
Chartfor more information.
- class nltk.parse.featurechart.FeatureChartParser[source]¶
Bases:
ChartParser- __init__(grammar, strategy=[<nltk.parse.chart.LeafInitRule object>, <nltk.parse.featurechart.FeatureEmptyPredictRule object>, <nltk.parse.featurechart.FeatureBottomUpPredictCombineRule object>, <nltk.parse.featurechart.FeatureSingleEdgeFundamentalRule object>], trace_chart_width=20, chart_class=<class 'nltk.parse.featurechart.FeatureChart'>, **parser_args)[source]¶
Create a new chart parser, that uses
grammarto 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.
0will 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.
- class nltk.parse.featurechart.FeatureEmptyPredictRule[source]¶
Bases:
EmptyPredictRule
- class nltk.parse.featurechart.FeatureFundamentalRule[source]¶
Bases:
FundamentalRuleA specialized version of the fundamental rule that operates on nonterminals whose symbols are
FeatStructNonterminal``s. Rather than simply comparing the nonterminals for equality, they are unified. Variable bindings from these unifications are collected and stored in the chart using a ``FeatureTreeEdge. When a complete edge is generated, these bindings are applied to all nonterminals in the edge.The fundamental rule states that:
[A -> alpha \* B1 beta][i:j][B2 -> gamma \*][j:k]
licenses the edge:
[A -> alpha B3 \* beta][i:j]
assuming that B1 and B2 can be unified to generate B3.
- class nltk.parse.featurechart.FeatureSingleEdgeFundamentalRule[source]¶
Bases:
SingleEdgeFundamentalRuleA specialized version of the completer / single edge fundamental rule that operates on nonterminals whose symbols are
FeatStructNonterminal. Rather than simply comparing the nonterminals for equality, they are unified.
- class nltk.parse.featurechart.FeatureTopDownChartParser[source]¶
Bases:
FeatureChartParser- __init__(grammar, **parser_args)[source]¶
Create a new chart parser, that uses
grammarto 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.
0will 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.
- class nltk.parse.featurechart.FeatureTopDownInitRule[source]¶
Bases:
TopDownInitRule
- class nltk.parse.featurechart.FeatureTopDownPredictRule[source]¶
Bases:
CachedTopDownPredictRuleA specialized version of the (cached) top down predict rule that operates on nonterminals whose symbols are
FeatStructNonterminal. Rather than simply comparing the nonterminals for equality, they are unified.The top down expand rule states that:
[A -> alpha \* B1 beta][i:j]
licenses the edge:
[B2 -> \* gamma][j:j]
for each grammar production
B2 -> gamma, assuming that B1 and B2 can be unified.
- class nltk.parse.featurechart.FeatureTreeEdge[source]¶
Bases:
TreeEdgeA specialized tree edge that allows shared variable bindings between nonterminals on the left-hand side and right-hand side.
Each
FeatureTreeEdgecontains a set ofbindings, i.e., a dictionary mapping from variables to values. If the edge is not complete, then these bindings are simply stored. However, if the edge is complete, then the constructor applies these bindings to every nonterminal in the edge whose symbol implements the interfaceSubstituteBindingsI.- __init__(span, lhs, rhs, dot=0, bindings=None)[source]¶
Construct a new edge. If the edge is incomplete (i.e., if
dot<len(rhs)), then store the bindings as-is. If the edge is complete (i.e., ifdot==len(rhs)), then apply the bindings to all nonterminals inlhsandrhs, and then clear the bindings. SeeTreeEdgefor a description of the other arguments.
- static from_production(production, index)[source]¶
- Returns:
A new
TreeEdgeformed from the given production. The new edge’s left-hand side and right-hand side will be taken fromproduction; its span will be(index,index); and its dot position will be0.- Return type:
- move_dot_forward(new_end, bindings=None)[source]¶
- Returns:
A new
FeatureTreeEdgeformed from this edge. The new edge’s dot position is increased by1, and its end index will be replaced bynew_end.- Return type:
- Parameters:
new_end (int) – The new end index.
bindings (dict) – Bindings for the new edge.
- class nltk.parse.featurechart.InstantiateVarsChart[source]¶
Bases:
FeatureChartA specialized chart that ‘instantiates’ variables whose names start with ‘@’, by replacing them with unique new variables. In particular, whenever a complete edge is added to the chart, any variables in the edge’s
lhswhose names start with ‘@’ will be replaced by unique newVariable.- __init__(tokens)[source]¶
Construct a new chart. The chart is initialized with the leaf edges corresponding to the terminal leaves.
- Parameters:
tokens (list) – The sentence that this chart will be used to parse.
- insert(edge, child_pointer_list)[source]¶
Add a new edge to the chart, and return True if this operation modified the chart. In particular, return true iff the chart did not already contain
edge, or if it did not already associatechild_pointer_listswithedge.
- instantiate_edge(edge)[source]¶
If the edge is a
FeatureTreeEdge, and it is complete, then instantiate all variables whose names start with ‘@’, by replacing them with unique new variables.Note that instantiation is done in-place, since the parsing algorithms might already hold a reference to the edge for future use.