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,141 @@
|
|
|
1
|
+
"""Lexical matching: how well texts share their rarer words (BM25, with the textbook constants k1 = 1.2, b = 0.75 and
|
|
2
|
+
rarity judged among the texts being compared, so nothing is learned but the record of each case).
|
|
3
|
+
|
|
4
|
+
Two uses:
|
|
5
|
+
- ranking candidates: questions that each ask whether one candidate (a document or a tool, in the instructions) is
|
|
6
|
+
relevant to the state's query. The candidates of a request are ranked against the query; the case is the rank.
|
|
7
|
+
Every candidate of a request is scored by this one ranker (its cases are SOLE: no learned rule answers a single
|
|
8
|
+
candidate in its place), so the probabilities of one query's candidates order them by one measure. A tool is
|
|
9
|
+
ranked by its name and by its whole text, the two ranks fused by reciprocal rank with the customary constant 60.
|
|
10
|
+
- options that describe the state: for an option set never seen in training, the option whose key and description
|
|
11
|
+
match the state best, when it is clearly ahead of the next.
|
|
12
|
+
"""
|
|
13
|
+
import collections
|
|
14
|
+
import zlib
|
|
15
|
+
|
|
16
|
+
from .. import text
|
|
17
|
+
from ..cues import STOP
|
|
18
|
+
from ..request import parse
|
|
19
|
+
from .tools import bm25, terms, user_text
|
|
20
|
+
|
|
21
|
+
NAME = "match"
|
|
22
|
+
CASES = {
|
|
23
|
+
"query_rank_1": "yes: of the request's candidates, this one matches the query best",
|
|
24
|
+
"query_rank_2": "yes: this candidate matches the query second best",
|
|
25
|
+
"query_rank_3_5": "no: this candidate ranks third to fifth against the query",
|
|
26
|
+
"query_rank_6_10": "no: this candidate ranks sixth to tenth against the query",
|
|
27
|
+
"query_rank_11_plus": "no: this candidate ranks below the first ten against the query",
|
|
28
|
+
"query_no_match": "no: this candidate shares no word with the query",
|
|
29
|
+
"describes_state_far": "the option whose words match the state best, at least four times as well as the next",
|
|
30
|
+
"describes_state_clear": "the option whose words match the state best, at least twice as well as the next",
|
|
31
|
+
"describes_state": "the option whose words match the state best",
|
|
32
|
+
}
|
|
33
|
+
BINS = ((1, "query_rank_1"), (2, "query_rank_2"), (5, "query_rank_3_5"), (10, "query_rank_6_10"))
|
|
34
|
+
RELEVANT = ("query_rank_1", "query_rank_2")
|
|
35
|
+
SOLE = frozenset(case for _, case in BINS) | {"query_rank_11_plus", "query_no_match"}
|
|
36
|
+
NUDGE = 1e-6
|
|
37
|
+
RRF = 60
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def candidate_text(question):
|
|
41
|
+
"""(the words of the candidate a relevance question asks about: a tool's name (twice), description and parameters,
|
|
42
|
+
or a document's text; the words of a tool's name, or None for a document), or None when the question names no
|
|
43
|
+
candidate."""
|
|
44
|
+
ins = question.get("instructions")
|
|
45
|
+
if not isinstance(ins, dict) or "candidate" not in ins:
|
|
46
|
+
return None
|
|
47
|
+
found = ins["candidate"]
|
|
48
|
+
tool = parse(found) if isinstance(found, str) else found
|
|
49
|
+
if isinstance(tool, dict) and "name" in tool:
|
|
50
|
+
name = terms(tool["name"])
|
|
51
|
+
parameters = terms(tool.get("parameters", tool.get("arguments", "")))
|
|
52
|
+
return name * 2 + terms(tool.get("description", "")) + parameters, name
|
|
53
|
+
return terms(found), None
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def words_of(value):
|
|
57
|
+
"""The content words of a text as counts, for matching it against options."""
|
|
58
|
+
return collections.Counter(w for w in terms(value) if w not in STOP and len(w) > 1)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def query_of(request):
|
|
62
|
+
data = request.data
|
|
63
|
+
said = user_text(data) if isinstance(data, (dict, list)) else ""
|
|
64
|
+
return set(terms(said or request.state_text))
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _ranks(scores):
|
|
68
|
+
"""{key: rank} by descending score, tied keys sharing their best rank; keys scoring 0 are not ranked."""
|
|
69
|
+
out, place, last = {}, 0, None
|
|
70
|
+
for i, k in enumerate(sorted((k for k in scores if scores[k] > 0), key=lambda k: -scores[k]), 1):
|
|
71
|
+
if scores[k] != last:
|
|
72
|
+
place, last = i, scores[k]
|
|
73
|
+
out[k] = place
|
|
74
|
+
return out
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def rank(request):
|
|
78
|
+
"""Every candidate question's rank against the query, its fused score, and that score as a share of the best."""
|
|
79
|
+
docs, names = {}, {}
|
|
80
|
+
for key, question in request.questions.items():
|
|
81
|
+
found = candidate_text(question)
|
|
82
|
+
if found is not None:
|
|
83
|
+
docs[key] = found[0]
|
|
84
|
+
if found[1] is not None:
|
|
85
|
+
names[key] = found[1]
|
|
86
|
+
query = query_of(request)
|
|
87
|
+
if len(docs) < 2 or not query:
|
|
88
|
+
return {}
|
|
89
|
+
full = _ranks(bm25(query, docs))
|
|
90
|
+
by_name = _ranks(bm25(query, names)) if len(names) >= 2 else {}
|
|
91
|
+
fused = {k: (1 / (RRF + full[k]) if k in full else 0.0) + (1 / (RRF + by_name[k]) if k in by_name else 0.0)
|
|
92
|
+
for k in docs}
|
|
93
|
+
arbitrary = {k: zlib.crc32(text.flatten(request.questions[k]["instructions"]["candidate"]).encode()) for k in docs}
|
|
94
|
+
order = sorted(docs, key=lambda k: (-fused[k], arbitrary[k]))
|
|
95
|
+
top = fused[order[0]] or 1
|
|
96
|
+
return {k: (i + 1, fused[k], fused[k] / top) for i, k in enumerate(order)}
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def yes_no(question):
|
|
100
|
+
keys = {k.lower(): k for k, _ in text.options(question)}
|
|
101
|
+
for yes, no in (("yes", "no"), ("true", "false")):
|
|
102
|
+
if set(keys) == {yes, no}:
|
|
103
|
+
return keys[yes], keys[no]
|
|
104
|
+
return None
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def propose(request, key):
|
|
108
|
+
"""(option key, case, nudge) or None. The nudge orders candidates of one case by their score."""
|
|
109
|
+
question = request.questions[key]
|
|
110
|
+
pair = yes_no(question)
|
|
111
|
+
if pair and isinstance(question.get("instructions"), dict) and "candidate" in question["instructions"]:
|
|
112
|
+
ranked = request.once(NAME + ".rank", rank)
|
|
113
|
+
if key not in ranked:
|
|
114
|
+
return None
|
|
115
|
+
place, score, share = ranked[key]
|
|
116
|
+
if score <= 0:
|
|
117
|
+
arbitrary = zlib.crc32(text.flatten(question["instructions"]["candidate"]).encode()) % 1000 / 1000
|
|
118
|
+
return pair[1], "query_no_match", NUDGE * arbitrary / 10
|
|
119
|
+
for limit, case in BINS:
|
|
120
|
+
if place <= limit:
|
|
121
|
+
return (pair[0], case, NUDGE * share) if case in RELEVANT else (pair[1], case, -NUDGE * share)
|
|
122
|
+
return pair[1], "query_rank_11_plus", -NUDGE * share
|
|
123
|
+
if request.familiar.get(key) or pair:
|
|
124
|
+
return None
|
|
125
|
+
options = text.options(question)
|
|
126
|
+
docs = {k: [w for w in terms(k) + terms(d) if w not in STOP and len(w) > 1] for k, d in options}
|
|
127
|
+
query = request.once(NAME + ".state", lambda r: words_of(text.values(r.data if r.data is not None else r.state)))
|
|
128
|
+
if not query or len(docs) < 2:
|
|
129
|
+
return None
|
|
130
|
+
scores = bm25(query, docs)
|
|
131
|
+
ranked = sorted(docs, key=lambda k: -scores[k])
|
|
132
|
+
best, second = scores[ranked[0]], scores[ranked[1]]
|
|
133
|
+
if best <= 0 or best == second:
|
|
134
|
+
return None
|
|
135
|
+
case = "describes_state_far" if best >= 4 * second else "describes_state_clear" if best >= 2 * second \
|
|
136
|
+
else "describes_state"
|
|
137
|
+
return ranked[0], case, 0.0
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def describe(case):
|
|
141
|
+
return CASES.get(case, case)
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"""Music: which chord sounds at a beat, from the notes of a score.
|
|
2
|
+
|
|
3
|
+
Reads a state with `target_time_beats` and `context_notes` (each a pitch, its pitch class and the beats it starts and
|
|
4
|
+
ends on) and options that are chords given as pitch-class sets. Every pitch class is weighed by how long it sounds in
|
|
5
|
+
the beat at the target, the lowest note sounding there counting twice; a chord scores the weight of its own pitch
|
|
6
|
+
classes, less half the weight of the others and a little for every note it has. The constants are fixed by hand.
|
|
7
|
+
"""
|
|
8
|
+
from ..request import parse
|
|
9
|
+
|
|
10
|
+
NAME = "music"
|
|
11
|
+
CASES = {
|
|
12
|
+
"chord_template": "the chord whose pitch classes hold most of the notes sounding at the target beat",
|
|
13
|
+
"no_chord": "no chord: no note sounds in the beat at the target",
|
|
14
|
+
}
|
|
15
|
+
OUTSIDE = 0.5
|
|
16
|
+
PER_NOTE = 0.05
|
|
17
|
+
BASS = 2.0
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def pitch_classes(description):
|
|
21
|
+
"""The pitch-class set of a chord option, given as an object or as JSON text; None for anything else."""
|
|
22
|
+
value = description if isinstance(description, dict) else parse(description) if isinstance(description, str) \
|
|
23
|
+
else None
|
|
24
|
+
if not isinstance(value, dict) or not isinstance(value.get("pitch_classes"), list):
|
|
25
|
+
return None
|
|
26
|
+
try:
|
|
27
|
+
return frozenset(int(p) % 12 for p in value["pitch_classes"])
|
|
28
|
+
except (TypeError, ValueError):
|
|
29
|
+
return None
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def weights(data):
|
|
33
|
+
"""{pitch class: how long it sounds in the beat at the target}, the bass counted twice; None when unreadable."""
|
|
34
|
+
try:
|
|
35
|
+
target = float(data["target_time_beats"])
|
|
36
|
+
notes = [(int(n["pitch"]), int(n.get("pitch_class", n["pitch"])) % 12, float(n["start_beat"]),
|
|
37
|
+
float(n["end_beat"])) for n in data["context_notes"]]
|
|
38
|
+
except (KeyError, TypeError, ValueError):
|
|
39
|
+
return None
|
|
40
|
+
end = target + 1.0
|
|
41
|
+
out = {}
|
|
42
|
+
for _, pc, start, stop in notes:
|
|
43
|
+
overlap = min(stop, end) - max(start, target)
|
|
44
|
+
if overlap > 0:
|
|
45
|
+
out[pc] = out.get(pc, 0.0) + overlap
|
|
46
|
+
sounding = [(pitch, pc) for pitch, pc, start, stop in notes if start <= target < stop]
|
|
47
|
+
if sounding:
|
|
48
|
+
_, bass = min(sounding)
|
|
49
|
+
out[bass] = out.get(bass, 0.0) + BASS * max(0.25, out.get(bass, 0.0))
|
|
50
|
+
return out
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def propose(request, key):
|
|
54
|
+
"""(option key, case) or None."""
|
|
55
|
+
data = request.data
|
|
56
|
+
if not isinstance(data, dict) or "context_notes" not in data or "target_time_beats" not in data:
|
|
57
|
+
return None
|
|
58
|
+
question = request.questions[key]
|
|
59
|
+
criteria = question.get("criteria")
|
|
60
|
+
if not isinstance(criteria, dict) or len(criteria) < 2:
|
|
61
|
+
return None
|
|
62
|
+
chords = {k: pitch_classes(v) for k, v in criteria.items()}
|
|
63
|
+
silent = [k for k, v in criteria.items() if chords[k] is None and "no chord" in str(v).lower()]
|
|
64
|
+
chords = {k: v for k, v in chords.items() if v is not None}
|
|
65
|
+
if len(chords) < 2:
|
|
66
|
+
return None
|
|
67
|
+
heard = request.once(NAME, lambda r: weights(data))
|
|
68
|
+
if heard is None:
|
|
69
|
+
return None
|
|
70
|
+
if not heard:
|
|
71
|
+
return (silent[0], "no_chord") if len(silent) == 1 else None
|
|
72
|
+
total = sum(heard.values())
|
|
73
|
+
scores = {}
|
|
74
|
+
for option, chord in chords.items():
|
|
75
|
+
inside = sum(w for pc, w in heard.items() if pc in chord)
|
|
76
|
+
scores[option] = inside - OUTSIDE * (total - inside) - PER_NOTE * len(chord)
|
|
77
|
+
ranked = sorted(scores, key=lambda k: -scores[k])
|
|
78
|
+
if scores[ranked[0]] == scores[ranked[1]]:
|
|
79
|
+
return None
|
|
80
|
+
return ranked[0], "chord_template"
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def describe(case):
|
|
84
|
+
return CASES.get(case, case)
|
|
85
|
+
|