sr-zero 0.9.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sr_zero-0.9.0.dist-info/METADATA +148 -0
- sr_zero-0.9.0.dist-info/RECORD +36 -0
- sr_zero-0.9.0.dist-info/WHEEL +5 -0
- sr_zero-0.9.0.dist-info/entry_points.txt +3 -0
- sr_zero-0.9.0.dist-info/licenses/DATA_SOURCES.md +322 -0
- sr_zero-0.9.0.dist-info/licenses/LICENSE +202 -0
- sr_zero-0.9.0.dist-info/licenses/NOTICE +10 -0
- sr_zero-0.9.0.dist-info/top_level.txt +1 -0
- system_zero/__init__.py +8 -0
- system_zero/__main__.py +5 -0
- system_zero/cli.py +175 -0
- system_zero/console.py +162 -0
- system_zero/cues.py +90 -0
- system_zero/decision_index.py +52 -0
- system_zero/evidence.py +42 -0
- system_zero/marks.py +27 -0
- system_zero/model.py +489 -0
- system_zero/modules/__init__.py +5 -0
- system_zero/modules/match.py +141 -0
- system_zero/modules/music.py +85 -0
- system_zero/modules/puzzles.py +489 -0
- system_zero/modules/run.py +668 -0
- system_zero/modules/scenes.py +345 -0
- system_zero/modules/tools.py +400 -0
- system_zero/modules/verse.py +105 -0
- system_zero/request.py +34 -0
- system_zero/rules/__init__.py +9 -0
- system_zero/rules/cues.py +15014 -0
- system_zero/rules/frames.py +150337 -0
- system_zero/rules/modules.py +54 -0
- system_zero/rules/option_sets.py +865 -0
- system_zero/rules/options.py +123346 -0
- system_zero/scopes.py +38 -0
- system_zero/server.py +169 -0
- system_zero/systemone.py +771 -0
- system_zero/text.py +308 -0
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
"""Scenes a state describes, read into data and then queried: the figure an SVG path draws, and the coloured objects
|
|
2
|
+
lying on a surface. Each reader proposes an answer only when the whole description parses and exactly one option
|
|
3
|
+
fits; anything else is left to the rules.
|
|
4
|
+
|
|
5
|
+
- figure: the path's points and arcs traced into the shape they draw (a line, a triangle, a kite, a rectangle, a
|
|
6
|
+
polygon by its number of corners, a circle, an ellipse or a sector)
|
|
7
|
+
- objects: "On the desk, you see a red pen, two blue cups, ..." read as a list of (colour, item) in order, and the
|
|
8
|
+
question about it answered by counting, looking up or looking left and right
|
|
9
|
+
"""
|
|
10
|
+
import functools
|
|
11
|
+
import math
|
|
12
|
+
import re
|
|
13
|
+
|
|
14
|
+
from .. import text
|
|
15
|
+
from .puzzles import options_of, pick_truth
|
|
16
|
+
|
|
17
|
+
NAME = "scenes"
|
|
18
|
+
CASES = {
|
|
19
|
+
"figure": "the shape the path draws, traced from its points and arcs",
|
|
20
|
+
"objects": "the answer read off the objects described, in the order given",
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
PATH = re.compile(r"\bd\s*=\s*\"([^\"]+)\"")
|
|
24
|
+
TOKEN = re.compile(r"[A-Za-z]|-?\d*\.?\d+(?:[eE][-+]?\d+)?")
|
|
25
|
+
POLYGONS = {3: "triangle", 5: "pentagon", 6: "hexagon", 7: "heptagon", 8: "octagon"}
|
|
26
|
+
NUMBER_WORDS = ("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven",
|
|
27
|
+
"twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty")
|
|
28
|
+
COUNTS = {word: i for i, word in enumerate(NUMBER_WORDS)} | {"a": 1, "an": 1}
|
|
29
|
+
COLOURS = ("red orange yellow green blue brown magenta fuchsia mauve teal turquoise burgundy silver gold black grey "
|
|
30
|
+
"gray purple pink white").split()
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
# ---- figures ------------------------------------------------------------------------------------------------------
|
|
34
|
+
|
|
35
|
+
def _segments(d):
|
|
36
|
+
"""[(start, end, arc radii or None)] of an SVG path with absolute M, L, A and Z commands, or None."""
|
|
37
|
+
tokens = TOKEN.findall(d)
|
|
38
|
+
out, i, command, here, start = [], 0, None, None, None
|
|
39
|
+
while i < len(tokens):
|
|
40
|
+
if tokens[i].isalpha():
|
|
41
|
+
command = tokens[i]
|
|
42
|
+
i += 1
|
|
43
|
+
if command in "Zz":
|
|
44
|
+
if here is None or start is None:
|
|
45
|
+
return None
|
|
46
|
+
out.append((here, start, None))
|
|
47
|
+
here = start
|
|
48
|
+
continue
|
|
49
|
+
if command == "M":
|
|
50
|
+
here = start = (float(tokens[i]), float(tokens[i + 1]))
|
|
51
|
+
i += 2
|
|
52
|
+
command = "L"
|
|
53
|
+
elif command == "L" and here is not None:
|
|
54
|
+
point = (float(tokens[i]), float(tokens[i + 1]))
|
|
55
|
+
out.append((here, point, None))
|
|
56
|
+
here = point
|
|
57
|
+
i += 2
|
|
58
|
+
elif command == "A" and here is not None:
|
|
59
|
+
rx, ry = float(tokens[i]), float(tokens[i + 1])
|
|
60
|
+
point = (float(tokens[i + 5]), float(tokens[i + 6]))
|
|
61
|
+
out.append((here, point, (rx, ry)))
|
|
62
|
+
here = point
|
|
63
|
+
i += 7
|
|
64
|
+
else:
|
|
65
|
+
return None
|
|
66
|
+
return out
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _close(a, b, scale):
|
|
70
|
+
return abs(a - b) <= 0.02 * scale
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _cycle(edges):
|
|
74
|
+
"""The corners of a closed polygon in order, if the edges form exactly one cycle; else None."""
|
|
75
|
+
neighbours = {}
|
|
76
|
+
for a, b in edges:
|
|
77
|
+
neighbours.setdefault(a, set()).add(b)
|
|
78
|
+
neighbours.setdefault(b, set()).add(a)
|
|
79
|
+
if any(len(v) != 2 for v in neighbours.values()):
|
|
80
|
+
return None
|
|
81
|
+
first = next(iter(neighbours))
|
|
82
|
+
order, previous = [first], None
|
|
83
|
+
while True:
|
|
84
|
+
here = order[-1]
|
|
85
|
+
following = [p for p in neighbours[here] if p != previous]
|
|
86
|
+
nxt = following[0]
|
|
87
|
+
if nxt == first:
|
|
88
|
+
break
|
|
89
|
+
order.append(nxt)
|
|
90
|
+
previous = here
|
|
91
|
+
if len(order) > len(neighbours):
|
|
92
|
+
return None
|
|
93
|
+
return order if len(order) == len(neighbours) else None
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _quadrilateral(corners):
|
|
97
|
+
sides = [math.dist(corners[i], corners[(i + 1) % 4]) for i in range(4)]
|
|
98
|
+
scale = max(sides)
|
|
99
|
+
def turn(i):
|
|
100
|
+
a, b, c = corners[i - 1], corners[i], corners[(i + 1) % 4]
|
|
101
|
+
return (b[0] - a[0]) * (c[0] - b[0]) + (b[1] - a[1]) * (c[1] - b[1])
|
|
102
|
+
|
|
103
|
+
right = all(abs(turn(i)) <= 0.02 * scale ** 2 for i in range(4))
|
|
104
|
+
if right:
|
|
105
|
+
return "square" if all(_close(s, sides[0], scale) for s in sides) else "rectangle"
|
|
106
|
+
if (_close(sides[0], sides[1], scale) and _close(sides[2], sides[3], scale)) or \
|
|
107
|
+
(_close(sides[1], sides[2], scale) and _close(sides[3], sides[0], scale)):
|
|
108
|
+
return "kite"
|
|
109
|
+
return "trapezoid"
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def shape_of(d):
|
|
113
|
+
"""The name of the shape an SVG path draws, or None."""
|
|
114
|
+
segments = _segments(d)
|
|
115
|
+
if not segments:
|
|
116
|
+
return None
|
|
117
|
+
arcs = [s for s in segments if s[2] is not None]
|
|
118
|
+
lines = [s for s in segments if s[2] is None and s[0] != s[1]]
|
|
119
|
+
if arcs:
|
|
120
|
+
if lines:
|
|
121
|
+
return "sector"
|
|
122
|
+
rx, ry = arcs[0][2]
|
|
123
|
+
return "circle" if all(_close(a[2][0], a[2][1], max(a[2])) for a in arcs) and _close(rx, ry, max(rx, ry)) \
|
|
124
|
+
else "ellipse"
|
|
125
|
+
points = {(round(a[0], 2), round(a[1], 2)) for s in lines for a in s[:2]}
|
|
126
|
+
edges = {tuple(sorted(((round(s[0][0], 2), round(s[0][1], 2)), (round(s[1][0], 2), round(s[1][1], 2)))))
|
|
127
|
+
for s in lines}
|
|
128
|
+
if len(points) == 2:
|
|
129
|
+
return "line"
|
|
130
|
+
corners = _cycle(edges)
|
|
131
|
+
if corners is None:
|
|
132
|
+
return None
|
|
133
|
+
if len(corners) == 4:
|
|
134
|
+
return _quadrilateral(corners)
|
|
135
|
+
return POLYGONS.get(len(corners))
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def figure(request, question):
|
|
139
|
+
source = _text(request)
|
|
140
|
+
m = PATH.search(source) if source else None
|
|
141
|
+
if not m or "path" not in source.lower():
|
|
142
|
+
return None
|
|
143
|
+
shape = shape_of(m.group(1))
|
|
144
|
+
if shape is None:
|
|
145
|
+
return None
|
|
146
|
+
names = {k: _option_text(k, d) for k, d in options_of(question)}
|
|
147
|
+
found = [k for k, n in names.items() if n == shape]
|
|
148
|
+
if not found and shape == "square":
|
|
149
|
+
found = [k for k, n in names.items() if n == "rectangle"]
|
|
150
|
+
return found[0] if len(found) == 1 else None
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
# ---- coloured objects -------------------------------------------------------------------------------------------
|
|
154
|
+
|
|
155
|
+
SCENE = re.compile(r"\b(?:on|in) the ([a-z ]+?), (?:you see|there is|there are|i see)(?: the following [a-z]+ arranged "
|
|
156
|
+
r"in a row| a bunch of [a-z]+(?: arranged in a row)?)?:? (.+?)\.(?=\s|$)", re.I)
|
|
157
|
+
ROW = re.compile(r"\barranged in a row on the ([a-z ]+?), there (?:is|are) (.+?)\.(?=\s|$)", re.I)
|
|
158
|
+
ITEM = re.compile(r"(\w+) (" + "|".join(COLOURS) + r") (.+)")
|
|
159
|
+
OF = re.compile(r"^(\w+) of (.+)$")
|
|
160
|
+
NOUN = r"[a-z-]+(?: [a-z-]+){0,3}?"
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def singular(noun):
|
|
164
|
+
"""A plural noun of the scenes in the singular: 'cups' -> 'cup', 'pairs of sunglasses' -> 'pair of sunglasses'."""
|
|
165
|
+
m = OF.match(noun)
|
|
166
|
+
if m:
|
|
167
|
+
return singular(m.group(1)) + " of " + m.group(2)
|
|
168
|
+
words = noun.split()
|
|
169
|
+
last = words[-1]
|
|
170
|
+
if re.search(r"(?:ch|sh|ss|x)es$", last):
|
|
171
|
+
last = last[:-2]
|
|
172
|
+
elif last.endswith("ies") and len(last) > 4:
|
|
173
|
+
last = last[:-3] + "y"
|
|
174
|
+
elif last.endswith("s") and not last.endswith("ss"):
|
|
175
|
+
last = last[:-1]
|
|
176
|
+
return " ".join(words[:-1] + [last])
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
@functools.lru_cache(maxsize=256)
|
|
180
|
+
def scene(source):
|
|
181
|
+
"""(surface, [(colour, item)] in order of mention, ordered) of the first scene, or None."""
|
|
182
|
+
m = ROW.search(source) or SCENE.search(source)
|
|
183
|
+
if not m:
|
|
184
|
+
return None
|
|
185
|
+
listed = m.group(2).strip()
|
|
186
|
+
ordered = "in a row" in m.group(0).lower()
|
|
187
|
+
trailing = re.search(r",?\s*arranged in a row$", listed, re.I)
|
|
188
|
+
if trailing:
|
|
189
|
+
listed, ordered = listed[:trailing.start()], True
|
|
190
|
+
found = []
|
|
191
|
+
for part in re.split(r",\s*(?:and\s+)?|\s+and\s+", listed):
|
|
192
|
+
mm = ITEM.fullmatch(part.strip().lower())
|
|
193
|
+
if not mm or mm.group(1) not in COUNTS:
|
|
194
|
+
return None
|
|
195
|
+
count = COUNTS[mm.group(1)]
|
|
196
|
+
item = mm.group(3).strip()
|
|
197
|
+
found.extend([(mm.group(2), singular(item) if count != 1 else item)] * count)
|
|
198
|
+
return (m.group(1).lower(), found, ordered) if found else None
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def _matches(thing, noun):
|
|
202
|
+
"""Whether an item (singular) is what a noun of the question means: 'thing', 'item' and 'object' mean any."""
|
|
203
|
+
noun = singular(noun.strip().lower())
|
|
204
|
+
return noun in ("thing", "item", "object") or thing == noun
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
LABEL = re.compile(r"^\(?[a-z]\)\s*:?\s*|^[a-z][.:]\s+", re.I)
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def _option_text(key, description):
|
|
211
|
+
"""An option's text in lower case, without a leading letter label such as '(A)', '(A):' or 'A.'."""
|
|
212
|
+
return LABEL.sub("", (description or key).strip()).strip().strip(".").lower()
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def _colour_option(question, colour):
|
|
216
|
+
names = {k: _option_text(k, d) for k, d in options_of(question)}
|
|
217
|
+
found = [k for k, n in names.items() if n == colour or (colour in ("grey", "gray") and n in ("grey", "gray"))]
|
|
218
|
+
return found[0] if len(found) == 1 else None
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def _count_option(question, n):
|
|
222
|
+
words = {NUMBER_WORDS[n] if n < len(NUMBER_WORDS) else None, str(n)}
|
|
223
|
+
found = [k for k, d in options_of(question) if _option_text(k, d) in words]
|
|
224
|
+
return found[0] if len(found) == 1 else None
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def _the(things, noun):
|
|
228
|
+
"""The index of the only item the noun names, or None."""
|
|
229
|
+
found = [i for i, (_, thing) in enumerate(things) if thing == singular(noun.strip().lower()) or thing == noun]
|
|
230
|
+
return found[0] if len(found) == 1 else None
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def _named(things, phrase):
|
|
234
|
+
"""The index of the only item a phrase like 'red cup', 'cup' or 'pair of sunglasses' names, or None."""
|
|
235
|
+
phrase = phrase.strip().lower()
|
|
236
|
+
m = re.fullmatch(r"(" + "|".join(COLOURS) + r") (.+)", phrase)
|
|
237
|
+
if m:
|
|
238
|
+
found = [i for i, (c, t) in enumerate(things) if c == m.group(1) and _matches(t, m.group(2))]
|
|
239
|
+
return found[0] if len(found) == 1 else None
|
|
240
|
+
return _the(things, phrase)
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def _text(request):
|
|
244
|
+
"""The text a state holds (its values, never its keys, when it is JSON), without a trailing list of options; None
|
|
245
|
+
when it holds more than one list of options, several puzzles side by side."""
|
|
246
|
+
return request.once("scenes.text", _read_text)
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def _read_text(request):
|
|
250
|
+
if isinstance(request.data, (dict, list)):
|
|
251
|
+
full = text.values(request.data)
|
|
252
|
+
else:
|
|
253
|
+
full = request.state_text if isinstance(request.state, str) else text.flatten(request.state)
|
|
254
|
+
parts = re.split(r"\nOptions:\n", full)
|
|
255
|
+
return parts[0].strip() if len(parts) <= 2 else None
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def objects(request, question):
|
|
259
|
+
source = _text(request)
|
|
260
|
+
read = scene(source) if source else None
|
|
261
|
+
if not read:
|
|
262
|
+
return None
|
|
263
|
+
_, things, ordered = read
|
|
264
|
+
asks = [s for s in re.split(r"(?<=[.?])\s+", source[source.lower().find(read[0]):]) if s.strip().endswith("?")]
|
|
265
|
+
if len(asks) != 1:
|
|
266
|
+
return None
|
|
267
|
+
q = asks[0].strip().lower()
|
|
268
|
+
colours = "|".join(COLOURS)
|
|
269
|
+
m = re.fullmatch(r"what (?:is the )?colou?r is the (.+?)\?", q) or \
|
|
270
|
+
re.fullmatch(r"what is the colou?r of the (.+?)\?", q)
|
|
271
|
+
if m and not re.search(r"\b(?:left|right|furthest|farthest)\b", m.group(1)):
|
|
272
|
+
i = _named(things, m.group(1))
|
|
273
|
+
return None if i is None else _colour_option(question, things[i][0])
|
|
274
|
+
m = re.fullmatch(r"is the (.+?) (" + colours + r")\?", q)
|
|
275
|
+
if m and not re.search(r"\b(?:left|right)\b", m.group(1)):
|
|
276
|
+
i = _named(things, m.group(1))
|
|
277
|
+
return None if i is None else pick_truth(question, things[i][0] == m.group(2))
|
|
278
|
+
m = re.fullmatch(r"how many (" + NOUN + r") are neither (" + colours + r") nor (" + colours + r")\?", q)
|
|
279
|
+
if m:
|
|
280
|
+
n = sum(1 for c, t in things if _matches(t, m.group(1)) and c not in (m.group(2), m.group(3)))
|
|
281
|
+
return _count_option(question, n)
|
|
282
|
+
m = re.fullmatch(r"if i remove all the (?:(" + colours + r") )?(" + NOUN + r") from the [a-z ]+, how many (?:(" +
|
|
283
|
+
colours + r") )?(" + NOUN + r") remain on it\?", q)
|
|
284
|
+
if m:
|
|
285
|
+
gone, kind, keep, target = m.groups()
|
|
286
|
+
kept = [(c, t) for c, t in things if not ((gone is None or c == gone) and _matches(t, kind))]
|
|
287
|
+
return _count_option(question, sum(1 for c, t in kept if (keep is None or c == keep) and _matches(t, target)))
|
|
288
|
+
if not ordered:
|
|
289
|
+
return None
|
|
290
|
+
m = re.fullmatch(r"what is the colou?r of the (?:thing|item|object) (?:directly )?to the (left|right) of the "
|
|
291
|
+
r"(.+?)\?", q)
|
|
292
|
+
if m:
|
|
293
|
+
i = _named(things, m.group(2))
|
|
294
|
+
if i is None:
|
|
295
|
+
return None
|
|
296
|
+
j = i - 1 if m.group(1) == "left" else i + 1
|
|
297
|
+
return _colour_option(question, things[j][0]) if 0 <= j < len(things) else None
|
|
298
|
+
m = re.fullmatch(r"what is the colou?r of the (left|right)-most (?:thing|item|object)\?", q)
|
|
299
|
+
if m:
|
|
300
|
+
return _colour_option(question, things[0 if m.group(1) == "left" else -1][0])
|
|
301
|
+
m = re.fullmatch(r"what is the colou?r of the (?:thing|item|object) furthest from the (.+?)\?", q)
|
|
302
|
+
if m:
|
|
303
|
+
i = _named(things, m.group(1))
|
|
304
|
+
if i is None or (i != 0 and i != len(things) - 1):
|
|
305
|
+
return None
|
|
306
|
+
return _colour_option(question, things[-1 if i == 0 else 0][0])
|
|
307
|
+
m = re.fullmatch(r"how many non-(" + colours + r") (" + NOUN + r") do you see to the (left|right) of the "
|
|
308
|
+
r"(.+?)\?", q)
|
|
309
|
+
if m:
|
|
310
|
+
i = _named(things, m.group(4))
|
|
311
|
+
if i is None:
|
|
312
|
+
return None
|
|
313
|
+
side = things[:i] if m.group(3) == "left" else things[i + 1:]
|
|
314
|
+
return _count_option(question, sum(1 for c, t in side if c != m.group(1) and _matches(t, m.group(2))))
|
|
315
|
+
m = re.fullmatch(r"is the (?:thing|item|object) (?:directly )?to the (left|right) of the (.+?) (" + colours +
|
|
316
|
+
r")\?", q)
|
|
317
|
+
if m:
|
|
318
|
+
i = _named(things, m.group(2))
|
|
319
|
+
if i is None:
|
|
320
|
+
return None
|
|
321
|
+
j = i - 1 if m.group(1) == "left" else i + 1
|
|
322
|
+
return pick_truth(question, things[j][0] == m.group(3)) if 0 <= j < len(things) else None
|
|
323
|
+
return None
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
SOLVERS = (("figure", figure), ("objects", objects))
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def propose(request, key):
|
|
330
|
+
"""(option key, case) or None."""
|
|
331
|
+
if not isinstance(request.state, str) and not isinstance(request.data, dict):
|
|
332
|
+
return None
|
|
333
|
+
question = request.questions[key]
|
|
334
|
+
for case, solve in SOLVERS:
|
|
335
|
+
try:
|
|
336
|
+
found = solve(request, question)
|
|
337
|
+
except (ValueError, KeyError, IndexError, TypeError, AttributeError, re.error):
|
|
338
|
+
found = None
|
|
339
|
+
if found is not None:
|
|
340
|
+
return found, case
|
|
341
|
+
return None
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
def describe(case):
|
|
345
|
+
return CASES.get(case, case)
|