nltk.sem.evaluate module

This module provides data structures for representing first-order models.

exception nltk.sem.evaluate.Error[source]

Bases: Exception

exception nltk.sem.evaluate.Undefined[source]

Bases: nltk.sem.evaluate.Error

nltk.sem.evaluate.trace(f, *args, **kw)[source]

Check whether a set represents a relation (of any arity).


s (set) – a set containing tuples of str elements

Return type



Convert a set containing individuals (strings or numbers) into a set of unary tuples. Any tuples of strings already in the set are passed through unchanged.

For example:
  • set([‘a’, ‘b’]) => set([(‘a’,), (‘b’,)])

  • set([3, 27]) => set([(‘3’,), (‘27’,)])

Return type

set of tuple of str


Check the arity of a relation. :type rel: set of tuples :rtype: int of tuple of str

class nltk.sem.evaluate.Valuation[source]

Bases: dict

A dictionary which represents a model-theoretic Valuation of non-logical constants. Keys are strings representing the constants to be interpreted, and values correspond to individuals (represented as strings) and n-ary relations (represented as sets of tuples of strings).

An instance of Valuation will raise a KeyError exception (i.e., just behave like a standard dictionary) if indexed with an expression that is not in its list of symbols.


xs – a list of (symbol, value) pairs.

property domain

Set-theoretic domain of the value-space of a Valuation.

property symbols

The non-logical constants which the Valuation recognizes.

classmethod fromstring(s)[source]
nltk.sem.evaluate.read_valuation(s, encoding=None)[source]

Convert a valuation string into a valuation.

  • s (str) – a valuation string

  • encoding (str) – the encoding of the input string, if it is binary


a nltk.sem valuation

Return type


class nltk.sem.evaluate.Assignment[source]

Bases: dict

A dictionary which represents an assignment of values to variables.

An assignment can only assign values from its domain.

If an unknown expression a is passed to a model M‘s interpretation function i, i will first check whether M‘s valuation assigns an interpretation to a as a constant, and if this fails, i will delegate the interpretation of a to g. g only assigns values to individual variables (i.e., members of the class IndividualVariableExpression in the logic module. If a variable is not assigned a value by g, it will raise an Undefined exception.

A variable Assignment is a mapping from individual variables to entities in the domain. Individual variables are usually indicated with the letters 'x', 'y', 'w' and 'z', optionally followed by an integer (e.g., 'x0', 'y332'). Assignments are created using the Assignment constructor, which also takes the domain as a parameter.

>>> from nltk.sem.evaluate import Assignment
>>> dom = set(['u1', 'u2', 'u3', 'u4'])
>>> g3 = Assignment(dom, [('x', 'u1'), ('y', 'u2')])
>>> g3 == {'x': 'u1', 'y': 'u2'}

There is also a print format for assignments which uses a notation closer to that in logic textbooks:

>>> print(g3)

It is also possible to update an assignment using the add method:

>>> dom = set(['u1', 'u2', 'u3', 'u4'])
>>> g4 = Assignment(dom)
>>> g4.add('x', 'u1')
{'x': 'u1'}

With no arguments, purge() is equivalent to clear() on a dictionary:

>>> g4.purge()
>>> g4
  • domain (set) – the domain of discourse

  • assign (list) – a list of (varname, value) associations

__init__(domain, assign=None)[source]
copy() a shallow copy of D[source]

Remove one or all keys (i.e. logic variables) from an assignment, and update self.variant.


var – a Variable acting as a key for the assignment.

add(var, val)[source]

Add a new variable-value pair to the assignment, and update self.variant.

class nltk.sem.evaluate.Model[source]

Bases: object

A first order model is a domain D of discourse and a valuation V.

A domain D is a set, and a valuation V is a map that associates expressions with values in the model. The domain of V should be a subset of D.

Construct a new Model.

  • domain (set) – A set of entities representing the domain of discourse of the model.

  • valuation (Valuation) – the valuation of the model.

  • prop – If this is set, then we are building a propositional model and don’t require the domain of V to be subset of D.

__init__(domain, valuation)[source]
evaluate(expr, g, trace=None)[source]

Read input expressions, and provide a handler for satisfy that blocks further propagation of the Undefined error. :param expr: An Expression of logic. :type g: Assignment :param g: an assignment to individual variables. :rtype: bool or ‘Undefined’

satisfy(parsed, g, trace=None)[source]

Recursive interpretation function for a formula of first-order logic.

Raises an Undefined error when parsed is an atomic string but is not a symbol or an individual variable.


Returns a truth value or Undefined if parsed is complex, and calls the interpretation function i if parsed is atomic.

  • parsed – An expression of logic.

  • g (Assignment) – an assignment to individual variables.

i(parsed, g, trace=False)[source]

An interpretation function.

Assuming that parsed is atomic:

  • if parsed is a non-logical constant, calls the valuation V

  • else if parsed is an individual variable, calls assignment g

  • else returns Undefined.

  • parsed – an Expression of logic.

  • g (Assignment) – an assignment to individual variables.


a semantic value

satisfiers(parsed, varex, g, trace=None, nesting=0)[source]

Generate the entities from the model’s domain that satisfy an open formula.


a set of the entities that satisfy parsed.


Example of a propositional model.

nltk.sem.evaluate.folmodel(quiet=False, trace=None)[source]

Example of a first-order model.


Interpretation of closed expressions in a first-order model.


Satisfiers of an open formula in a first order model.

nltk.sem.evaluate.demo(num=0, trace=None)[source]

Run exists demos.

  • num = 1: propositional logic demo

  • num = 2: first order model demo (only if trace is set)

  • num = 3: first order sentences demo

  • num = 4: satisfaction of open formulas demo

  • any other value: run all the demos


trace – trace = 1, or trace = 2 for more verbose tracing