nltk.sem.evaluate module¶
This module provides data structures for representing first-order models.
- 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 thelogic
module. If a variable is not assigned a value by g, it will raise anUndefined
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 theAssignment
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'} True
There is also a
print
format for assignments which uses a notation closer to that in logic textbooks:>>> print(g3) g[u1/x][u2/y]
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 toclear()
on a dictionary:>>> g4.purge() >>> g4 {}
- Parameters
domain (set) – the domain of discourse
assign (list) – a list of (varname, value) associations
- 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
.- Parameters
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.
- evaluate(expr, g, trace=None)[source]¶
Read input expressions, and provide a handler for
satisfy
that blocks further propagation of theUndefined
error. :param expr: AnExpression
oflogic
. :type g: Assignment :param g: an assignment to individual variables. :rtype: bool or ‘Undefined’
- i(parsed, g, trace=False)[source]¶
An interpretation function.
Assuming that
parsed
is atomic:if
parsed
is a non-logical constant, calls the valuation Velse if
parsed
is an individual variable, calls assignment gelse returns
Undefined
.
- Parameters
parsed – an
Expression
oflogic
.g (Assignment) – an assignment to individual variables.
- Returns
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.
- Parameters
parsed (Expression) – an open formula
varex (VariableExpression or str) – the relevant free individual variable in
parsed
.g (Assignment) – a variable assignment
- Returns
a set of the entities that satisfy
parsed
.
- satisfy(parsed, g, trace=None)[source]¶
Recursive interpretation function for a formula of first-order logic.
Raises an
Undefined
error whenparsed
is an atomic string but is not a symbol or an individual variable.- Returns
Returns a truth value or
Undefined
ifparsed
is complex, and calls the interpretation functioni
ifparsed
is atomic.- Parameters
parsed – An expression of
logic
.g (Assignment) – an assignment to individual variables.
- 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.- property domain¶
Set-theoretic domain of the value-space of a Valuation.
- property symbols¶
The non-logical constants which the Valuation recognizes.
- nltk.sem.evaluate.arity(rel)[source]¶
Check the arity of a relation. :type rel: set of tuples :rtype: int of tuple of str
- 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
- Parameters
trace – trace = 1, or trace = 2 for more verbose tracing
- nltk.sem.evaluate.foldemo(trace=None)[source]¶
Interpretation of closed expressions in a first-order model.
- nltk.sem.evaluate.is_rel(s)[source]¶
Check whether a set represents a relation (of any arity).
- Parameters
s (set) – a set containing tuples of str elements
- Return type
bool
- nltk.sem.evaluate.read_valuation(s, encoding=None)[source]¶
Convert a valuation string into a valuation.
- Parameters
s (str) – a valuation string
encoding (str) – the encoding of the input string, if it is binary
- Returns
a
nltk.sem
valuation- Return type
- nltk.sem.evaluate.satdemo(trace=None)[source]¶
Satisfiers of an open formula in a first order model.
- nltk.sem.evaluate.set2rel(s)[source]¶
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