Coverage for nltk.ccg.combinator : 84%
![](keybd_closed.png)
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# Natural Language Toolkit: Combinatory Categorial Grammar # # Copyright (C) 2001-2012 NLTK Project # Author: Graeme Gange <ggange@csse.unimelb.edu.au> # URL: <http://www.nltk.org/> # For license information, see LICENSE.TXT
""" 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. """ raise NotImplementedError()
raise NotImplementedError()
""" 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. """ raise NotImplementedError()
raise NotImplementedError()
''' 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. '''
self._predicate(left,right))
''' The backward equivalent of the ForwardCombinator class. '''
self._predicate(left,right))
""" Class representing function application. Implements rules of the form: X/Y Y -> X (>) And the corresponding backwards application rule """
return
return
# Predicates for function application.
# Ensures the left functor takes an argument on the right
# Ensures the right functor takes an argument on the left
# Application combinator instances forwardOnly) backwardOnly)
""" Functional composition (harmonic) combinator. Implements rules of the form X/Y Y/Z -> X/Z (B>) And the corresponding backwards and crossed variations. """ # Can only combine two functions, and both functions must # allow composition.
return argument.arg().substitute(subs),argument.dir())
# Predicates for restricting application of straight composition.
return left.dir().is_backward() and right.dir().is_backward()
# Predicates for crossed composition
# The functors must be crossed inwards # Permuting combinators must be allowed return False # The resulting argument category is restricted to be primitive
# Straight composition combinators forwardOnly) backwardOnly)
# Backward crossed composition suffix='x')
""" Substitution (permutation) combinator. Implements rules of the form Y/Z (X\Y)/Z -> X/Z (<Sx) And other variations. """
# These could potentially be moved to the predicates, as the # constraints may not be general to all languages.
# Predicate for forward substitution return left.res().dir().is_forward() and left.arg().is_primitive()
# Predicate for backward crossed substitution return False return False
# Instances of substitution combinators forwardSConstraint) backwardSxConstraint,'x')
# Retrieves the left-most functional category. # ie, (N\N)/(S/NP) => N\N
''' Undirected combinator for type raising. ''' # The argument must be a function. # The restriction that arg.res() must be a function # merely reduces redundant type-raising; if arg.res() is # primitive, we have: # X Y\X =>(<T) Y/(Y\X) Y\X =>(>) Y # which is equivalent to # X Y\X =>(<) Y if not (arg.is_function() and arg.res().is_function()): return False
arg = innermostFunction(arg)
# left, arg_categ are undefined! subs = left.can_unify(arg_categ.arg()) if subs is not None: return True return False
arg.is_function() and arg.res().is_function()):
# Type-raising matches only the innermost application.
FunctionalCategory(xcat,function,arg.dir()), -(arg.dir()))
# Predicates for type-raising # The direction of the innermost category must be towards # the primary functor. # The restriction that the variable must be primitive is not # common to all versions of CCGs; some authors have other restrictions. arg = innermostFunction(right) return arg.dir().is_backward() and arg.res().is_primitive()
arg = innermostFunction(left) return arg.dir().is_forward() and arg.res().is_primitive()
# Instances of type-raising combinators |