nltk.ccg package¶
Submodules¶
nltk.ccg.api module¶
-
class
nltk.ccg.api.
AbstractCCGCategory
[source]¶ Bases:
object
Interface for categories in combinatory grammars.
-
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).
-
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.
-
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)
-
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.
-
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.
-
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.
-
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 theNUM_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 theNUM_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.
-
class
nltk.ccg.chart.
CCGEdge
(span, categ, rule)[source]¶ Bases:
nltk.parse.chart.EdgeI
-
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 withtokens[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
-
lhs
()[source]¶ Return this edge’s left-hand side, which specifies what kind of structure is hypothesized by this edge.
See: TreeEdge
andLeafEdge
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
andLeafEdge
for a description of the right-hand side values for each edge type.
-
-
class
nltk.ccg.chart.
CCGLeafEdge
(pos, token, leaf)[source]¶ Bases:
nltk.parse.chart.EdgeI
Class representing leaf edges in a CCG derivation.
-
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 withtokens[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
-
lhs
()[source]¶ Return this edge’s left-hand side, which specifies what kind of structure is hypothesized by this edge.
See: TreeEdge
andLeafEdge
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
andLeafEdge
for a description of the right-hand side values for each edge type.
-
-
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 theNUM_EDGES
class variable.Return type: iter(EdgeI)
-
unicode_repr
¶ Return repr(self).
-
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.
-
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.
-
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.
-
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.
-
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.
-
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
-
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.
-
unicode_repr
¶ Return repr(self).
-
-
class
nltk.ccg.combinator.
UndirectedTypeRaise
[source]¶ Bases:
nltk.ccg.combinator.UndirectedBinaryCombinator
Undirected combinator for type raising.
-
unicode_repr
¶ Return repr(self).
-
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
-
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)
-
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.logic module¶
Helper functions for CCG semantics computation
Module contents¶
Combinatory Categorial Grammar.
For more information see nltk/doc/contrib/ccg/ccg.pdf