nltk.ccg.chart module

The lexicon is constructed by calling lexicon.fromstring(<lexicon string>).

In order to construct a parser, you also need a rule set. The standard English rules are provided in chart as chart.DefaultRuleSet.

The parser can then be constructed by calling, for example: parser = chart.CCGChartParser(<lexicon>, <ruleset>)

Parsing is then performed by running parser.parse(<sentence>.split()).

While this returns a list of trees, the default representation of the produced trees is not very enlightening, particularly given that it uses the same tree class as the CFG parsers. It is probably better to call: chart.printCCGDerivation(<parse tree extracted from list>) which should print a nice representation of the derivation.

This entire process is shown far more clearly in the demonstration: python chart.py

class nltk.ccg.chart.CCGEdge[source]

Bases: nltk.parse.chart.EdgeI

__init__(span, categ, rule)[source]
lhs()[source]

Return this edge’s left-hand side, which specifies what kind of structure is hypothesized by this edge.

See

TreeEdge and LeafEdge for a description of the left-hand side values for each edge type.

span()[source]

Return a tuple (s, e), where tokens[s:e] is the portion of the sentence that is consistent with this edge’s structure.

Return type

tuple(int, int)

start()[source]

Return the start index of this edge’s span.

Return type

int

end()[source]

Return the end index of this edge’s span.

Return type

int

length()[source]

Return the length of this edge’s span.

Return type

int

rhs()[source]

Return this edge’s right-hand side, which specifies the content of the structure hypothesized by this edge.

See

TreeEdge and LeafEdge for a description of the right-hand side values for each edge type.

dot()[source]

Return this edge’s dot position, which indicates how much of the hypothesized structure is consistent with the sentence. In particular, self.rhs[:dot] is consistent with tokens[self.start():self.end()].

Return type

int

is_complete()[source]

Return True if this edge’s structure is fully consistent with the text.

Return type

bool

is_incomplete()[source]

Return True if this edge’s structure is partially consistent with the text.

Return type

bool

nextsym()[source]

Return the element of this edge’s right-hand side that immediately follows its dot.

Return type

Nonterminal or terminal or None

categ()[source]
rule()[source]
class nltk.ccg.chart.CCGLeafEdge[source]

Bases: nltk.parse.chart.EdgeI

Class representing leaf edges in a CCG derivation.

__init__(pos, token, leaf)[source]
lhs()[source]

Return this edge’s left-hand side, which specifies what kind of structure is hypothesized by this edge.

See

TreeEdge and LeafEdge for a description of the left-hand side values for each edge type.

span()[source]

Return a tuple (s, e), where tokens[s:e] is the portion of the sentence that is consistent with this edge’s structure.

Return type

tuple(int, int)

start()[source]

Return the start index of this edge’s span.

Return type

int

end()[source]

Return the end index of this edge’s span.

Return type

int

length()[source]

Return the length of this edge’s span.

Return type

int

rhs()[source]

Return this edge’s right-hand side, which specifies the content of the structure hypothesized by this edge.

See

TreeEdge and LeafEdge for a description of the right-hand side values for each edge type.

dot()[source]

Return this edge’s dot position, which indicates how much of the hypothesized structure is consistent with the sentence. In particular, self.rhs[:dot] is consistent with tokens[self.start():self.end()].

Return type

int

is_complete()[source]

Return True if this edge’s structure is fully consistent with the text.

Return type

bool

is_incomplete()[source]

Return True if this edge’s structure is partially consistent with the text.

Return type

bool

nextsym()[source]

Return the element of this edge’s right-hand side that immediately follows its dot.

Return type

Nonterminal or terminal or None

token()[source]
categ()[source]
leaf()[source]
class nltk.ccg.chart.BinaryCombinatorRule[source]

Bases: nltk.parse.chart.AbstractChartRule

Class implementing application of a binary combinator to a chart. Takes the directed combinator to apply.

NUMEDGES = 2
__init__(combinator)[source]
apply(chart, grammar, left_edge, right_edge)[source]

Return a generator that will add edges licensed by this rule and the given edges to the chart, one at a time. Each time the generator is resumed, it will either add a new edge and yield that edge; or return.

Parameters

edges (list(EdgeI)) – A set of existing edges. The number of edges that should be passed to apply() is specified by the NUM_EDGES class variable.

Return type

iter(EdgeI)

class nltk.ccg.chart.ForwardTypeRaiseRule[source]

Bases: nltk.parse.chart.AbstractChartRule

Class for applying forward type raising

NUMEDGES = 2
__init__()[source]
apply(chart, grammar, left_edge, right_edge)[source]

Return a generator that will add edges licensed by this rule and the given edges to the chart, one at a time. Each time the generator is resumed, it will either add a new edge and yield that edge; or return.

Parameters

edges (list(EdgeI)) – A set of existing edges. The number of edges that should be passed to apply() is specified by the NUM_EDGES class variable.

Return type

iter(EdgeI)

class nltk.ccg.chart.BackwardTypeRaiseRule[source]

Bases: nltk.parse.chart.AbstractChartRule

Class for applying backward type raising.

NUMEDGES = 2
__init__()[source]
apply(chart, grammar, left_edge, right_edge)[source]

Return a generator that will add edges licensed by this rule and the given edges to the chart, one at a time. Each time the generator is resumed, it will either add a new edge and yield that edge; or return.

Parameters

edges (list(EdgeI)) – A set of existing edges. The number of edges that should be passed to apply() is specified by the NUM_EDGES class variable.

Return type

iter(EdgeI)

class nltk.ccg.chart.CCGChartParser[source]

Bases: nltk.parse.api.ParserI

Chart parser for CCGs. Based largely on the ChartParser class from NLTK.

__init__(lexicon, rules, trace=0)[source]
lexicon()[source]
parse(tokens)[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)

class nltk.ccg.chart.CCGChart[source]

Bases: nltk.parse.chart.Chart

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

nltk.ccg.chart.compute_semantics(children, edge)[source]
nltk.ccg.chart.printCCGDerivation(tree)[source]
nltk.ccg.chart.printCCGTree(lwidth, tree)[source]
nltk.ccg.chart.demo()[source]