Module nltk_lite.contrib.models
Overview
This module provides data structures for representing first-order
models. A model is a pair <D,V>, where D is a domain of discourse and V
is a valuation function for the non-logical constants of a first-order
language. We assume that the language is based on the lambda calculus,
in the style of Montague grammar.
We assume that non-logical constants are either individual constants
or functors. In particular, rather than interpreting a one-place
predicate P as a set S, we
interpret it as the corresponding characteristic function f, where f(a) = True iff a is in S. For example, instead of
interpreting 'dog' as the set of individuals {'d1',
'd2', 'd3'}, we interpret it as the function which maps 'd1', 'd2'
and 'd3' to True and every other entity to False.
Thus, as a first approximation, non-logical constants are
interpreted as follows (note that e is the type of
entities and t is the type of truth
values):
-
if alpha is an individual constant, then V(alpha) is an element of D.
-
If gamma is a functor of type (e x ... x e) -> t, then V(gamma) is a function
f from D x ... x D to {True, False}.
However, since we are basing our language on the lambda calculus
(see logic
), a binary relation such as 'like'
will not in fact be associated with the type (e x
e) -> t, but rather the type
(e -> (e -> t); i.e., a function from entities to a function from
entities to truth values. In other words, functors are assigned
'Curried' functions as their values. We should also point out that
expressions of our languge are not explicitly typed. We leave it to the
grammar writer to assign 'sensible' values to expressions rather than
enforcing any type-to-denotation consistency.
Characteristic Functions
Within models
, Curried characteristic functions are
implemented as a subclass of dictionaries, using the
CharFun()
constructor.
>>> cf = CharFun({'d1' : {'d2': True}, 'd2' : {'d1': True}})
>>> cf['d1']
{'d2': True}
Values of a CharFun
are accessed by indexing in the
usual way:
>>> cf['d1']['d2']
True
>>> cf['not in domain']
False
In practise, it may be more convenient to specify interpretations as
n-ary relations (i.e., sets of n-tuples) rather than as n-ary
functions. CharFun
provides a parse()
method
which will convert such relations into Curried characteristic
functions:
>>> s = set([('d1', 'd2'), ('d2', 'd1')])
>>> cf = CharFun()
>>> cf.parse(s)
>>> cf
{'d2': {'d1': True}, 'd1': {'d2': True}}
parse()
will raise an exception if the set is not in
fact a relation (i.e., contains tuples of different lengths:
>>> wrong = set([('d1', 'd2'), ('d2', 'd1', 'd3')])
>>> cf.parse(wrong)
Traceback (most recent call last):
...
ValueError: Set contains sequences of different lengths
However, unary relations can be parsed to characteristic
functions.
>>> unary = set(['d1', 'd2'])
>>> cf.parse(unary)
>>> cf
{'d2': True, 'd1': True}
The function flatten
returns a set of the entities used
as keys in a CharFun
instance. The same information can be
accessed via the domain
attribute of
CharFun
.
>>> cf = CharFun({'d1' : {'d2': True}, 'd2' : {'d1': True}})
>>> flatten(cf)
set(['d2', 'd1'])
>>> cf.domain
set(['d2', 'd1'])
Valuations and Models
A Valuation is a mapping from non-logical constants to
appropriate semantic values in the model. Valuations are created using
the Valuation
constructor.
>>> val = Valuation({'Fido' : 'd1', 'dog' : {'d1' : True, 'd2' : True}})
>>> val
{'Fido': 'd1', 'dog': {'d2': True, 'd1': True}}
As with CharFun
, Valuation
will also parse
valuations using relations rather than characteristic functions as
interpretations.
>>> setval = [('adam', 'b1'), ('betty', 'g1'), ('girl', set(['g2', 'g1'])), ('boy', set(['b1', 'b2'])), ('love', set([('b1', 'g1'), ('b2', 'g2'), ('g1', 'b1'), ('g2', 'b1')]))]
>>> val = models.Valuation()
>>> val.parse(setval)
>>> print val
{'adam': 'b1',
'betty': 'g1',
'boy': {'b1': True, 'b2': True},
'girl': {'g2': True, 'g1': True},
'love': {'b1': {'g1': True},
'b2': {'g2': True},
'g1': {'b1': True},
'g2': {'b1': True}}}
Valuations have a domain
attribute, like
CharFun
, and also a symbols
attribute.
>>> val.domain
set(['g1', 'g2', 'b2', 'b1'])
>>> v.symbols
['boy', 'girl', 'love', 'adam', 'betty']
The Model
constructor takes two parameters, a
set
and a Valuation
.
>>> m = Model(val.domain, val)
Classes |
Assignment |
A dictionary which represents an assignment of values to
variables.. |
CharFun |
A dictionary which represents a Curried characteristic function. |
Model |
A first order model is a domain D of discourse and
a valuation V. |
TestModels |
|
Valuation |
A dictionary which represents a model-theoretic Valuation of
non-logical constants. |
Function Summary |
|
decompose (expr)
Function to communicate with a first-order functional language. |
|
demo ()
Example of a propositional model. |
set
|
flatten (value)
Return the set of keys of a CharFun instance. |
|
test(verbosity)
|
|
testsuite()
|
decompose(expr)
Function to communicate with a first-order functional language.
This function tries to make weak assumptions about the parse
structure provided by the logic module.
The (binder, body) pair is for decomposing quantifier formulae. The
(op, args) pair is for decomposing formulae with a boolean operator.
The (fun, args) pair should catch other relevant cases.
-
- Parameters:
expr -
A string representation of a first-order formula.
|
demo()
Example of a propositional model.
-
|
flatten(value)
-
- Parameters:
value
(type=dict)
- Returns:
-
The set of keys of a
CharFun instance.
(type=set)
|