-
Notifications
You must be signed in to change notification settings - Fork 6
/
lexica.py
235 lines (196 loc) · 8.46 KB
/
lexica.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env python
"""
Essentially a convenience function for analyzing complex lexical
spaces, especially those involving disjunctive closures. Use
python lexica.py
to see how a space of lexica is created from a simple baselexicon and
some useful specifications.
"""
__author__ = "Christopher Potts"
__version__ = "2.0"
__license__ = "GNU general public license, version 3"
__maintainer__ = "Christopher Potts"
__email__ = "See the author's website"
from collections import defaultdict
from copy import copy
from functools import reduce
from itertools import product
import numpy as np
from pypragmods.utils import powerset, display_matrix
NULL_MSG = 'NULL'
DISJUNCTION_SIGN = ' v '
CONJUNCTION_SIGN = ' & '
class Lexica:
def __init__(self,
baselexicon,
atomic_states=[],
nullsem=True,
join_closure=False,
block_ineffability=False,
costs=defaultdict(float),
disjunction_cost=0.01,
nullcost=5.0,
unknown_word=None):
"""
baselexicon : dict
Mapping strings to iteraables.
atomic_states : list
Usually the atomic states are the keys of `baselexicon`,
but that set can be augmented if one wants states
where no message is true (except `nullsem` perhaps).
nullsem : bool
If True, add the null message, true in all states in
all lexica.
join_closure : bool
Close the messages and states under disjunction.
block_ineffability : bool
Block states without true messages; relevant only if
`nullsem=False`.
costs : defaultdict(float)
`costs.keys()` must contain `baselexicon.keys()`
disjunction_cost : float
Cost of a disjunction.
nullcost : float
Should probably be higher than regular messages' costs.
unknown_word : str or None
A message constrained to have a singleton meaning in all lexica.
Attributes
----------
All of the parameters to __init__ become attributes.
self.messages : list
The keys of `baselexicon`.
self.atomic_states : list
This is the union of `atomic_states` with all the elements
of the list valued `self.baselexicon`.
self.states : list
Same as self.atomic_states` at first, but possibly
expanded by closures later.
self.lexica : list
Created by `self.get_lexica`.
"""
self.baselexicon = baselexicon
self.messages = sorted(self.baselexicon.keys())
self.atomic_states = \
sorted(set(atomic_states) | \
set([x for X in self.baselexicon.values() for x in X]))
self.states = copy(self.atomic_states)
self.nullsem = nullsem
self.join_closure = join_closure
self.block_ineffability = block_ineffability
self.costs = costs
self.disjunction_cost = disjunction_cost
self.nullcost = nullcost
self.unknown_word = unknown_word
self.lexica = self.get_lexica()
def cost_vector(self):
"""The numerical message cost vector in the same order
as self.messages"""
return np.array([self.costs[msg] for msg in self.messages])
def get_lexica(self):
"""The main funtion for building lexicon sets from the user's specs."""
lexica = []
enrichments = [powerset(self.baselexicon[msg]) for msg in self.messages]
for x in product(*enrichments):
lexica.append(dict(list(zip(self.messages, x))))
# If there's an unknown word, require it to have an
# atomic meaning in each lexicon:
new_lexica = []
if self.unknown_word and self.unknown_word in self.messages:
for lex in lexica:
if len(lex[self.unknown_word]) == 1:
new_lexica.append(lex)
lexica = new_lexica
# Close the lexica:
if self.join_closure:
lexica = self.add_join_closure(lexica)
atomic_messages = sorted([x for x in list(self.baselexicon.keys())
if CONJUNCTION_SIGN not in x])
self.messages += [DISJUNCTION_SIGN.join(sorted(set(cm)))
for cm in powerset(atomic_messages, minsize=2)]
self.states += [DISJUNCTION_SIGN.join(sorted(set(sem)))
for sem in powerset(self.atomic_states, minsize=2)]
# Add nullsem last so that it doesn't participate in any
# closures (and displays last in matrices):
if self.nullsem:
lexica = self.add_nullsem(lexica)
self.messages.append(NULL_MSG)
self.costs[NULL_MSG] = self.nullcost
return lexica
def add_join_closure(self, lexica):
"""Close the atomic messages and atomic states under joins"""
return self.add_closure(
lexica=lexica,
connective=DISJUNCTION_SIGN,
combo_func=(lambda x,y : x | y),
cost_value=self.disjunction_cost)
def add_closure(self,
lexica=None,
connective=None,
combo_func=None,
cost_value=None):
"""Generic function for adding closures."""
atomic_messages = sorted([x for x in list(self.baselexicon.keys())
if CONJUNCTION_SIGN not in x])
complex_msgs = [connective.join(sorted(set(cm)))
for cm in powerset(atomic_messages, minsize=1)]
for i, lex in enumerate(lexica):
for cm in complex_msgs:
# Get all the worlds consistent with the complex message:
vals = reduce(combo_func, [set(lex[word])
for word in cm.split(connective)])
# Closure space:
vals = powerset(vals, minsize=1)
# Create the new value, containing worlds and "conjoined worlds":
if cm not in lex: lex[cm] = set([])
lex[cm] = list(set(lex[cm]) | set([connective.join(sorted(set(sem)))
for sem in vals]))
args = cm.split(connective)
# Costs for the complex messages:
signs = len(args)-1
self.costs[cm] = (cost_value*signs) + sum(self.costs[word]
for word in args)
lexica[i] = lex
return lexica
def add_nullsem(self, lexica):
"""Adds the null message to every lexicon"""
for i, lex in enumerate(lexica):
lex[NULL_MSG] = self.states
lexica[i] = lex
return lexica
def lexica2matrices(self):
"""Map the dict-based lexica to matrices for use with pragmods.py"""
mats = []
for lex in self.lexica:
mat = np.zeros((len(self.messages), len(self.states)))
for i, msg in enumerate(self.messages):
for j, d in enumerate(self.states):
if d in lex[msg]:
mat[i,j] = 1.0
minval = 1 if self.nullsem else 0
# The models aren't defined for lexica where a message
# denotes the emptyset:
if 0.0 not in np.sum(mat, axis=1):
# Option to ensure that every state can be named by some message:
if not (self.block_ineffability and minval in np.sum(mat, axis=0)):
mats.append(mat)
return mats
def display(self, digits=4):
"""Display all the lexica in a readable way"""
for i, mat in enumerate(self.lexica2matrices()):
display_matrix(
mat,
rnames=self.messages,
cnames=self.states,
title="Lex{}".format(i),
digits=digits)
def __len__(self):
"""Number of lexica that are included after any filtering
the user wanted."""
return len(self.lexica2matrices())
if __name__ == '__main__':
lexica = Lexica(
baselexicon={'some': ['w_SOMENOTALL', 'w_ALL'], 'all': ['w_ALL']},
costs={'some':0.0, 'all':0.0},
join_closure=True,
nullsem=True)
lexica.display()