nltk.sem.Assignment¶
- class nltk.sem.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
- purge(var=None)[source]¶
Remove one or all keys (i.e. logic variables) from an assignment, and update
self.variant
.- Parameters
var – a Variable acting as a key for the assignment.
- __new__(**kwargs)¶
- clear() None. Remove all items from D. ¶
- fromkeys(value=None, /)¶
Create a new dictionary with keys from iterable and values set to value.
- get(key, default=None, /)¶
Return the value for key if key is in the dictionary, else default.
- items() a set-like object providing a view on D's items ¶
- keys() a set-like object providing a view on D's keys ¶
- pop(k[, d]) v, remove specified key and return the corresponding value. ¶
If key is not found, default is returned if given, otherwise KeyError is raised
- popitem()¶
Remove and return a (key, value) pair as a 2-tuple.
Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.
- setdefault(key, default=None, /)¶
Insert key with a value of default if key is not in the dictionary.
Return the value for key if key is in the dictionary, else default.
- update([E, ]**F) None. Update D from dict/iterable E and F. ¶
If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]
- values() an object providing a view on D's values ¶