nltk.sem.drt module

class nltk.sem.drt.DrtTokens[source]

Bases: nltk.sem.logic.Tokens

DRS = 'DRS'
DRS_CONC = '+'
PRONOUN = 'PRO'
OPEN_BRACKET = '['
CLOSE_BRACKET = ']'
COLON = ':'
PUNCT = ['+', '[', ']', ':']
SYMBOLS = ['&', '^', '|', '->', '=>', '<->', '<=>', '=', '==', '!=', '\\', '.', '(', ')', ',', '-', '!', '+', '[', ']', ':']
TOKENS = ['and', '&', '^', 'or', '|', 'implies', '->', '=>', 'iff', '<->', '<=>', '=', '==', '!=', 'some', 'exists', 'exist', 'all', 'forall', '\\', '.', '(', ')', ',', 'not', '-', '!', 'DRS', '+', '[', ']', ':']
class nltk.sem.drt.DrtParser[source]

Bases: nltk.sem.logic.LogicParser

A lambda calculus expression parser.

__init__()[source]
Parameters

type_check (bool) – should type checking be performed to their types?

get_all_symbols()[source]

This method exists to be overridden

isvariable(tok)[source]
handle(tok, context)[source]

This method is intended to be overridden for logics that use different operators or expressions

make_NegatedExpression(expression)[source]
handle_DRS(tok, context)[source]
handle_refs()[source]
handle_conds(context)[source]
handle_prop(tok, context)[source]
make_EqualityExpression(first, second)[source]

This method serves as a hook for other logic parsers that have different equality expression classes

get_BooleanExpression_factory(tok)[source]

This method serves as a hook for other logic parsers that have different boolean operators

make_BooleanExpression(factory, first, second)[source]
make_ApplicationExpression(function, argument)[source]
make_VariableExpression(name)[source]
make_LambdaExpression(variables, term)[source]
class nltk.sem.drt.DrtExpression[source]

Bases: object

This is the base abstract DRT Expression from which every DRT Expression extends.

classmethod fromstring(s)[source]
applyto(other)[source]
equiv(other, prover=None)[source]

Check for logical equivalence. Pass the expression (self <-> other) to the theorem prover. If the prover says it is valid, then the self and other are equal.

Parameters
  • other – an DrtExpression to check equality against

  • prover – a nltk.inference.api.Prover

property type
typecheck(signature=None)[source]
get_refs(recursive=False)[source]

Return the set of discourse referents in this DRS. :param recursive: bool Also find discourse referents in subterms? :return: list of Variable objects

is_pronoun_function()[source]

Is self of the form “PRO(x)”?

make_EqualityExpression(first, second)[source]
make_VariableExpression(variable)[source]
resolve_anaphora()[source]
eliminate_equality()[source]
pretty_format()[source]

Draw the DRS :return: the pretty print string

pretty_print()[source]
draw()[source]
class nltk.sem.drt.DRS[source]

Bases: nltk.sem.drt.DrtExpression, nltk.sem.logic.Expression

A Discourse Representation Structure.

__init__(refs, conds, consequent=None)[source]
Parameters
  • refs – list of DrtIndividualVariableExpression for the discourse referents

  • conds – list of Expression for the conditions

replace(variable, expression, replace_bound=False, alpha_convert=True)[source]

Replace all instances of variable v with expression E in self, where v is free in self.

free()[source]
See

Expression.free()

get_refs(recursive=False)[source]
See

AbstractExpression.get_refs()

visit(function, combinator)[source]
See

Expression.visit()

visit_structured(function, combinator)[source]
See

Expression.visit_structured()

eliminate_equality()[source]
fol()[source]
nltk.sem.drt.DrtVariableExpression(variable)[source]

This is a factory method that instantiates and returns a subtype of DrtAbstractVariableExpression appropriate for the given variable.

class nltk.sem.drt.DrtAbstractVariableExpression[source]

Bases: nltk.sem.drt.DrtExpression, nltk.sem.logic.AbstractVariableExpression

fol()[source]
get_refs(recursive=False)[source]
See

AbstractExpression.get_refs()

eliminate_equality()[source]
class nltk.sem.drt.DrtIndividualVariableExpression[source]

Bases: nltk.sem.drt.DrtAbstractVariableExpression, nltk.sem.logic.IndividualVariableExpression

class nltk.sem.drt.DrtFunctionVariableExpression[source]

Bases: nltk.sem.drt.DrtAbstractVariableExpression, nltk.sem.logic.FunctionVariableExpression

class nltk.sem.drt.DrtEventVariableExpression[source]

Bases: nltk.sem.drt.DrtIndividualVariableExpression, nltk.sem.logic.EventVariableExpression

class nltk.sem.drt.DrtConstantExpression[source]

Bases: nltk.sem.drt.DrtAbstractVariableExpression, nltk.sem.logic.ConstantExpression

class nltk.sem.drt.DrtProposition[source]

Bases: nltk.sem.drt.DrtExpression, nltk.sem.logic.Expression

__init__(variable, drs)[source]
replace(variable, expression, replace_bound=False, alpha_convert=True)[source]

Replace every instance of ‘variable’ with ‘expression’ :param variable: Variable The variable to replace :param expression: Expression The expression with which to replace it :param replace_bound: bool Should bound variables be replaced? :param alpha_convert: bool Alpha convert automatically to avoid name clashes?

eliminate_equality()[source]
get_refs(recursive=False)[source]

Return the set of discourse referents in this DRS. :param recursive: bool Also find discourse referents in subterms? :return: list of Variable objects

fol()[source]
visit(function, combinator)[source]
See

Expression.visit()

visit_structured(function, combinator)[source]
See

Expression.visit_structured()

class nltk.sem.drt.DrtNegatedExpression[source]

Bases: nltk.sem.drt.DrtExpression, nltk.sem.logic.NegatedExpression

fol()[source]
get_refs(recursive=False)[source]
See

AbstractExpression.get_refs()

class nltk.sem.drt.DrtLambdaExpression[source]

Bases: nltk.sem.drt.DrtExpression, nltk.sem.logic.LambdaExpression

alpha_convert(newvar)[source]

Rename all occurrences of the variable introduced by this variable binder in the expression to newvar. :param newvar: Variable, for the new variable

fol()[source]
get_refs(recursive=False)[source]
See

AbstractExpression.get_refs()

class nltk.sem.drt.DrtBinaryExpression[source]

Bases: nltk.sem.drt.DrtExpression, nltk.sem.logic.BinaryExpression

get_refs(recursive=False)[source]
See

AbstractExpression.get_refs()

class nltk.sem.drt.DrtBooleanExpression[source]

Bases: nltk.sem.drt.DrtBinaryExpression, nltk.sem.logic.BooleanExpression

class nltk.sem.drt.DrtOrExpression[source]

Bases: nltk.sem.drt.DrtBooleanExpression, nltk.sem.logic.OrExpression

fol()[source]
class nltk.sem.drt.DrtEqualityExpression[source]

Bases: nltk.sem.drt.DrtBinaryExpression, nltk.sem.logic.EqualityExpression

fol()[source]
class nltk.sem.drt.DrtConcatenation[source]

Bases: nltk.sem.drt.DrtBooleanExpression

DRS of the form ‘(DRS + DRS)’

__init__(first, second, consequent=None)[source]
replace(variable, expression, replace_bound=False, alpha_convert=True)[source]

Replace all instances of variable v with expression E in self, where v is free in self.

eliminate_equality()[source]
simplify()[source]
Returns

beta-converted version of this expression

get_refs(recursive=False)[source]
See

AbstractExpression.get_refs()

getOp()[source]
fol()[source]
visit(function, combinator)[source]
See

Expression.visit()

class nltk.sem.drt.DrtApplicationExpression[source]

Bases: nltk.sem.drt.DrtExpression, nltk.sem.logic.ApplicationExpression

fol()[source]
get_refs(recursive=False)[source]
See

AbstractExpression.get_refs()

class nltk.sem.drt.PossibleAntecedents[source]

Bases: list, nltk.sem.drt.DrtExpression, nltk.sem.logic.Expression

free()[source]

Set of free variables.

replace(variable, expression, replace_bound=False, alpha_convert=True)[source]

Replace all instances of variable v with expression E in self, where v is free in self.

exception nltk.sem.drt.AnaphoraResolutionException[source]

Bases: Exception

nltk.sem.drt.resolve_anaphora(expression, trail=[])[source]
class nltk.sem.drt.DrsDrawer[source]

Bases: object

BUFFER = 3
TOPSPACE = 10
OUTERSPACE = 6
__init__(drs, size_canvas=True, canvas=None)[source]
Parameters
  • drsDrtExpression, The DRS to be drawn

  • size_canvas – bool, True if the canvas size should be the exact size of the DRS

  • canvasCanvas The canvas on which to draw the DRS. If none is given, create a new canvas.

draw(x=6, y=10)[source]

Draw the DRS

nltk.sem.drt.demo()[source]
nltk.sem.drt.test_draw()[source]