NLTK Semantic Interpretation Package

This package contains classes for representing semantic structure in formulas of first-order logic and for evaluating such formulas in set-theoretic models.

>>> from nltk.sem import logic
>>> logic._counter._value = 0

The package has two main components:

  • logic provides support for analyzing expressions of First Order Logic (FOL).
  • evaluate allows users to recursively determine truth in a model for formulas of FOL.

A model consists of a domain of discourse and a valuation function, which assigns values to non-logical constants. We assume that entities in the domain are represented as strings such as 'b1', 'g1', etc. A Valuation is initialized with a list of (symbol, value) pairs, where values are entities, sets of entities or sets of tuples of entities. The domain of discourse can be inferred from the valuation, and model is then created with domain and valuation as parameters.

>>> from nltk.sem import Valuation, Model
>>> v = [('adam', 'b1'), ('betty', 'g1'), ('fido', 'd1'),
... ('girl', set(['g1', 'g2'])), ('boy', set(['b1', 'b2'])),
... ('dog', set(['d1'])),
... ('love', set([('b1', 'g1'), ('b2', 'g2'), ('g1', 'b1'), ('g2', 'b1')]))]
>>> val = Valuation(v)
>>> dom = val.domain
>>> m = Model(dom, val)


arity(rel) Check the arity of a relation.
binding_ops() Binding operators
boolean_ops() Boolean operators
clause(reldict, relsym) Print the relation in clausal form.
equality_preds() Equality predicates
evaluate_sents(inputs, grammar, model, ...) Add the truth-in-a-model value to each semantic representation for each syntactic parse of each input sentences.
extract_rels(subjclass, objclass, doc[, ...]) Filter the output of semi_rel2reldict according to specified NE classes and a filler pattern.
interpret_sents(inputs, grammar[, semkey, trace]) Add the semantic representation to each syntactic parse tree of each input sentence.
is_rel(s) Check whether a set represents a relation (of any arity).
parse_sents(inputs, grammar[, trace]) Convert input sentences into syntactic trees.
read_logic(s[, logic_parser, encoding]) Convert a file of First Order Formulas into a list of {Expression}s.
read_valuation(s[, encoding]) Convert a valuation string into a valuation.
root_semrep(syntree[, semkey]) Find the semantic representation at the root of a tree.
rtuple(reldict[, lcon, rcon]) Pretty print the reldict as an rtuple.
set2rel(s) Convert a set containing individuals (strings or numbers) into a set of unary tuples.
skolemize(expression[, univ_scope, ...]) Skolemize the expression and convert to conjunctive normal form (CNF)


ApplicationExpression(function, argument) This class is used to represent two related types of logical expressions.
Assignment(domain[, assign]) A dictionary which represents an assignment of values to variables.
Boxer([boxer_drs_interpreter, elimeq, ...]) This class is an interface to Johan Bos’s program Boxer, a wide-coverage semantic parser that produces Discourse Representation Structures (DRSs).
DRS(refs, conds[, consequent]) A Discourse Representation Structure.
DrtExpression This is the base abstract DRT Expression from which every DRT Expression extends.
Expression This is the base abstract object for all logical expressions
Model(domain, valuation) A first order model is a domain D of discourse and a valuation V.
Valuation(xs) A dictionary which represents a model-theoretic Valuation of non-logical constants.


LogicalExpressionException(index, message)