nltk.ccg package

Submodules

nltk.ccg.api module

class nltk.ccg.api.AbstractCCGCategory[source]

Bases: object

Interface for categories in combinatory grammars.

can_unify(other)[source]
Determines whether two categories can be unified.
  • Returns None if they cannot be unified
  • Returns a list of necessary substitutions if they can.
is_function()[source]

Returns true if the category is a function application.

is_primitive()[source]

Returns true if the category is primitive.

is_var()[source]

Returns true if the category is a variable.

substitute(substitutions)[source]

Takes a set of (var, category) substitutions, and replaces every occurrence of the variable with the corresponding category.

class nltk.ccg.api.CCGVar(prim_only=False)[source]

Bases: nltk.ccg.api.AbstractCCGCategory

Class representing a variable CCG category. Used for conjunctions (and possibly type-raising, if implemented as a unary rule).

can_unify(other)[source]

If the variable can be replaced with other a substitution is returned.

id()[source]
is_function()[source]

Returns true if the category is a function application.

is_primitive()[source]

Returns true if the category is primitive.

is_var()[source]

Returns true if the category is a variable.

classmethod new_id()[source]

A class method allowing generation of unique variable identifiers.

classmethod reset_id()[source]
substitute(substitutions)[source]

If there is a substitution corresponding to this variable, return the substituted category.

unicode_repr

Return repr(self).

class nltk.ccg.api.Direction(dir, restrictions)[source]

Bases: object

Class representing the direction of a function application. Also contains maintains information as to which combinators may be used with the category.

can_compose()[source]
can_cross()[source]
can_unify(other)[source]
dir()[source]
is_backward()[source]
is_forward()[source]
is_variable()[source]
restrs()[source]

A list of restrictions on the combinators. ‘.’ denotes that permuting operations are disallowed ‘,’ denotes that function composition is disallowed ‘_’ denotes that the direction has variable restrictions. (This is redundant in the current implementation of type-raising)

substitute(subs)[source]
unicode_repr

Return repr(self).

class nltk.ccg.api.FunctionalCategory(res, arg, dir)[source]

Bases: nltk.ccg.api.AbstractCCGCategory

Class that represents a function application category. Consists of argument and result categories, together with an application direction.

arg()[source]
can_unify(other)[source]

Determines whether two categories can be unified. - Returns None if they cannot be unified - Returns a list of necessary substitutions if they can.

dir()[source]
is_function()[source]

Returns true if the category is a function application.

is_primitive()[source]

Returns true if the category is primitive.

is_var()[source]

Returns true if the category is a variable.

res()[source]
substitute(subs)[source]

Takes a set of (var, category) substitutions, and replaces every occurrence of the variable with the corresponding category.

unicode_repr

Return repr(self).

class nltk.ccg.api.PrimitiveCategory(categ, restrictions=[])[source]

Bases: nltk.ccg.api.AbstractCCGCategory

Class representing primitive categories. Takes a string representation of the category, and a list of strings specifying the morphological subcategories.

can_unify(other)[source]

Determines whether two categories can be unified. - Returns None if they cannot be unified - Returns a list of necessary substitutions if they can.

categ()[source]
is_function()[source]

Returns true if the category is a function application.

is_primitive()[source]

Returns true if the category is primitive.

is_var()[source]

Returns true if the category is a variable.

restrs()[source]
substitute(subs)[source]

Takes a set of (var, category) substitutions, and replaces every occurrence of the variable with the corresponding category.

unicode_repr

Return repr(self).

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.BackwardTypeRaiseRule[source]

Bases: nltk.parse.chart.AbstractChartRule

Class for applying backward type raising.

NUMEDGES = 2
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)
unicode_repr

Return repr(self).

class nltk.ccg.chart.BinaryCombinatorRule(combinator)[source]

Bases: nltk.parse.chart.AbstractChartRule

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

NUMEDGES = 2
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)
unicode_repr

Return repr(self).

class nltk.ccg.chart.CCGChart(tokens)[source]

Bases: nltk.parse.chart.Chart

class nltk.ccg.chart.CCGChartParser(lexicon, rules, trace=0)[source]

Bases: nltk.parse.api.ParserI

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

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.CCGEdge(span, categ, rule)[source]

Bases: nltk.parse.chart.EdgeI

categ()[source]
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
end()[source]

Return the end index of this edge’s span.

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
length()[source]

Return the length of this edge’s span.

Return type:int
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.
nextsym()[source]

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

Return type:Nonterminal or terminal or None
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.
rule()[source]
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
class nltk.ccg.chart.CCGLeafEdge(pos, token, leaf)[source]

Bases: nltk.parse.chart.EdgeI

Class representing leaf edges in a CCG derivation.

categ()[source]
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
end()[source]

Return the end index of this edge’s span.

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
leaf()[source]
length()[source]

Return the length of this edge’s span.

Return type:int
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.
nextsym()[source]

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

Return type:Nonterminal or terminal or None
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.
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
token()[source]
class nltk.ccg.chart.ForwardTypeRaiseRule[source]

Bases: nltk.parse.chart.AbstractChartRule

Class for applying forward type raising

NUMEDGES = 2
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)
unicode_repr

Return repr(self).

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

nltk.ccg.combinator module

CCG Combinators

class nltk.ccg.combinator.BackwardCombinator(combinator, predicate, suffix='')[source]

Bases: nltk.ccg.combinator.DirectedBinaryCombinator

The backward equivalent of the ForwardCombinator class.

can_combine(left, right)[source]
combine(left, right)[source]
unicode_repr

Return repr(self).

class nltk.ccg.combinator.DirectedBinaryCombinator[source]

Bases: object

Wrapper for the undirected binary combinator. It takes left and right categories, and decides which is to be the function, and which the argument. It then decides whether or not they can be combined.

can_combine(left, right)[source]
combine(left, right)[source]
class nltk.ccg.combinator.ForwardCombinator(combinator, predicate, suffix='')[source]

Bases: nltk.ccg.combinator.DirectedBinaryCombinator

Class representing combinators where the primary functor is on the left.

Takes an undirected combinator, and a predicate which adds constraints restricting the cases in which it may apply.

can_combine(left, right)[source]
combine(left, right)[source]
unicode_repr

Return repr(self).

class nltk.ccg.combinator.UndirectedBinaryCombinator[source]

Bases: object

Abstract class for representing a binary combinator. Merely defines functions for checking if the function and argument are able to be combined, and what the resulting category is.

Note that as no assumptions are made as to direction, the unrestricted combinators can perform all backward, forward and crossed variations of the combinators; these restrictions must be added in the rule class.

can_combine(function, argument)[source]
combine(function, argument)[source]
class nltk.ccg.combinator.UndirectedComposition[source]

Bases: nltk.ccg.combinator.UndirectedBinaryCombinator

Functional composition (harmonic) combinator. Implements rules of the form X/Y Y/Z -> X/Z (B>) And the corresponding backwards and crossed variations.

can_combine(function, argument)[source]
combine(function, argument)[source]
unicode_repr

Return repr(self).

class nltk.ccg.combinator.UndirectedFunctionApplication[source]

Bases: nltk.ccg.combinator.UndirectedBinaryCombinator

Class representing function application. Implements rules of the form: X/Y Y -> X (>) And the corresponding backwards application rule

can_combine(function, argument)[source]
combine(function, argument)[source]
unicode_repr

Return repr(self).

class nltk.ccg.combinator.UndirectedSubstitution[source]

Bases: nltk.ccg.combinator.UndirectedBinaryCombinator

Substitution (permutation) combinator. Implements rules of the form Y/Z (XY)/Z -> X/Z (<Sx) And other variations.

can_combine(function, argument)[source]
combine(function, argument)[source]
unicode_repr

Return repr(self).

class nltk.ccg.combinator.UndirectedTypeRaise[source]

Bases: nltk.ccg.combinator.UndirectedBinaryCombinator

Undirected combinator for type raising.

can_combine(function, arg)[source]
combine(function, arg)[source]
unicode_repr

Return repr(self).

nltk.ccg.combinator.backwardBxConstraint(left, right)[source]
nltk.ccg.combinator.backwardOnly(left, right)[source]
nltk.ccg.combinator.backwardSxConstraint(left, right)[source]
nltk.ccg.combinator.backwardTConstraint(left, right)[source]
nltk.ccg.combinator.bothBackward(left, right)[source]
nltk.ccg.combinator.bothForward(left, right)[source]
nltk.ccg.combinator.crossedDirs(left, right)[source]
nltk.ccg.combinator.forwardOnly(left, right)[source]
nltk.ccg.combinator.forwardSConstraint(left, right)[source]
nltk.ccg.combinator.forwardTConstraint(left, right)[source]
nltk.ccg.combinator.innermostFunction(categ)[source]

nltk.ccg.lexicon module

CCG Lexicons

class nltk.ccg.lexicon.CCGLexicon(start, primitives, families, entries)[source]

Bases: object

Class representing a lexicon for CCG grammars.

  • primitives: The list of primitive categories for the lexicon
  • families: Families of categories
  • entries: A mapping of words to possible categories
categories(word)[source]

Returns all the possible categories for a word

start()[source]

Return the target category for the parser

unicode_repr

Return repr(self).

class nltk.ccg.lexicon.Token(token, categ, semantics=None)[source]

Bases: object

Class representing a token.

token => category {semantics} e.g. eat => Svar[pl]/var {x y.eat(x,y)}

  • token (string)
  • categ (string)
  • semantics (Expression)
categ()[source]
semantics()[source]
nltk.ccg.lexicon.augParseCategory(line, primitives, families, var=None)[source]

Parse a string representing a category, and returns a tuple with (possibly) the CCG variable for the category

nltk.ccg.lexicon.fromstring(lex_str, include_semantics=False)[source]

Convert string representation into a lexicon for CCGs.

nltk.ccg.lexicon.matchBrackets(string)[source]

Separate the contents matching the first set of brackets from the rest of the input.

nltk.ccg.lexicon.nextCategory(string)[source]

Separate the string for the next portion of the category from the rest of the string

nltk.ccg.lexicon.parseApplication(app)[source]

Parse an application operator

nltk.ccg.lexicon.parsePrimitiveCategory(chunks, primitives, families, var)[source]

Parse a primitive category

If the primitive is the special category ‘var’, replace it with the correct CCGVar.

nltk.ccg.lexicon.parseSubscripts(subscr)[source]

Parse the subscripts for a primitive category

nltk.ccg.logic module

Helper functions for CCG semantics computation

nltk.ccg.logic.compute_composition_semantics(function, argument)[source]
nltk.ccg.logic.compute_function_semantics(function, argument)[source]
nltk.ccg.logic.compute_substitution_semantics(function, argument)[source]
nltk.ccg.logic.compute_type_raised_semantics(semantics)[source]

Module contents

Combinatory Categorial Grammar.

For more information see nltk/doc/contrib/ccg/ccg.pdf