diff --git a/root/organs10/physics/specialists/solved-cost-3/program.py b/root/organs10/physics/specialists/solved-cost-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..ad893ff96ad343a9996f5e0be8d1d17eaec3db78 --- /dev/null +++ b/root/organs10/physics/specialists/solved-cost-3/program.py @@ -0,0 +1,7 @@ +"""cost from fuel, fuel price (Fuel cost) +The maths organ (systems.py): solved from the system C = F*p; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(fuel, fuel_price): + return 1.0*fuel*fuel_price diff --git a/root/organs10/physics/specialists/solved-cost-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-cost-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-cost-3/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-cowling-number/organ_formula_v2.py b/root/organs10/physics/specialists/solved-cowling-number/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-cowling-number/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-cowling-number/panel.json b/root/organs10/physics/specialists/solved-cowling-number/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..78abbdcf1cd1c770f2f73271004608f143885f3a --- /dev/null +++ b/root/organs10/physics/specialists/solved-cowling-number/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the Cowling number for first Cowling number 107.961 and magnetic Reynolds number 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "Cowling number for [107.960666, 59.4105] (the system first Cowling number (Wikidata Q105084863))" + }, + { + "q": "Tell me the Cowling number with first Cowling number of 107.961 and magnetic Reynolds number of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "Cowling number for [107.960666, 59.4105] (the system first Cowling number (Wikidata Q105084863))" + }, + { + "q": "What is the Cowling number for first Cowling number 169.527 and magnetic Reynolds number 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "Cowling number for [169.526958, 280.773702] (the system first Cowling number (Wikidata Q105084863))" + }, + { + "q": "Tell me the Cowling number with first Cowling number of 169.527 and magnetic Reynolds number of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "Cowling number for [169.526958, 280.773702] (the system first Cowling number (Wikidata Q105084863))" + }, + { + "q": "What is the Cowling number for first Cowling number 1.91195 and magnetic Reynolds number 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "Cowling number for [1.911952, 82.240072] (the system first Cowling number (Wikidata Q105084863))" + }, + { + "q": "Tell me the Cowling number with first Cowling number of 1.91195 and magnetic Reynolds number of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "Cowling number for [1.911952, 82.240072] (the system first Cowling number (Wikidata Q105084863))" + }, + { + "q": "What is the Cowling number for first Cowling number 1.64379 and magnetic Reynolds number 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "Cowling number for [1.643795, 252.709024] (the system first Cowling number (Wikidata Q105084863))" + }, + { + "q": "Tell me the Cowling number with first Cowling number of 1.64379 and magnetic Reynolds number of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "Cowling number for [1.643795, 252.709024] (the system first Cowling number (Wikidata Q105084863))" + }, + { + "q": "What is the Cowling number for first Cowling number 18.3169 and magnetic Reynolds number 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "Cowling number for [18.316867, 6.838598] (the system first Cowling number (Wikidata Q105084863))" + }, + { + "q": "Tell me the Cowling number with first Cowling number of 18.3169 and magnetic Reynolds number of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "Cowling number for [18.316867, 6.838598] (the system first Cowling number (Wikidata Q105084863))" + }, + { + "q": "What is the Cowling number for first Cowling number 156.502 and magnetic Reynolds number 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "Cowling number for [156.50181, 185.886273] (the system first Cowling number (Wikidata Q105084863))" + }, + { + "q": "Tell me the Cowling number with first Cowling number of 156.502 and magnetic Reynolds number of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "Cowling number for [156.50181, 185.886273] (the system first Cowling number (Wikidata Q105084863))" + }, + { + "q": "What is the Cowling number for first Cowling number 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic Reynolds number is not given" + }, + { + "q": "Tell me the Cowling number with first Cowling number of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic Reynolds number is not given" + }, + { + "q": "What is the Cowling number of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the Cowling number of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the Cowling number with first Cowling number of -1.91195 and magnetic Reynolds number of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative first Cowling number: no example had one" + }, + { + "q": "Which Cowling number is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the first Cowling number is 8.50728 and the magnetic Reynolds number is 17.7888, what is the Cowling number?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "Cowling number for [8.507283, 17.788778] (the system first Cowling number (Wikidata Q105084863))" + }, + { + "q": "Could you work out the Cowling number given first Cowling number = 8.50728, magnetic Reynolds number = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "Cowling number for [8.507283, 17.788778] (the system first Cowling number (Wikidata Q105084863))" + }, + { + "q": "I need the Cowling number for a first Cowling number of 8.24951 and a magnetic Reynolds number of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "Cowling number for [8.24951, 0.634021] (the system first Cowling number (Wikidata Q105084863))" + }, + { + "q": "If the first Cowling number is 8.24951 and the magnetic Reynolds number is 0.634021, what is the Cowling number?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "Cowling number for [8.24951, 0.634021] (the system first Cowling number (Wikidata Q105084863))" + }, + { + "q": "Could you work out the Cowling number given first Cowling number = 0.318734, magnetic Reynolds number = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "Cowling number for [0.318734, 9.566071] (the system first Cowling number (Wikidata Q105084863))" + }, + { + "q": "I need the Cowling number for a first Cowling number of 0.318734 and a magnetic Reynolds number of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "Cowling number for [0.318734, 9.566071] (the system first Cowling number (Wikidata Q105084863))" + }, + { + "q": "If the first Cowling number is 0.671697 and the magnetic Reynolds number is 14.2496, what is the Cowling number?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "Cowling number for [0.671697, 14.249569] (the system first Cowling number (Wikidata Q105084863))" + }, + { + "q": "Could you work out the Cowling number given first Cowling number = 0.671697, magnetic Reynolds number = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "Cowling number for [0.671697, 14.249569] (the system first Cowling number (Wikidata Q105084863))" + }, + { + "q": "I need the Cowling number for a first Cowling number of 21.7931 and a magnetic Reynolds number of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "Cowling number for [21.793126, 29.510609] (the system first Cowling number (Wikidata Q105084863))" + }, + { + "q": "If the first Cowling number is 21.7931 and the magnetic Reynolds number is 29.5106, what is the Cowling number?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "Cowling number for [21.793126, 29.510609] (the system first Cowling number (Wikidata Q105084863))" + }, + { + "q": "Could you work out the Cowling number given first Cowling number = 153.15, magnetic Reynolds number = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "Cowling number for [153.149965, 18.595315] (the system first Cowling number (Wikidata Q105084863))" + }, + { + "q": "I need the Cowling number for a first Cowling number of 153.15 and a magnetic Reynolds number of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "Cowling number for [153.149965, 18.595315] (the system first Cowling number (Wikidata Q105084863))" + }, + { + "q": "If the first Cowling number is 8.50728, what is the Cowling number?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic Reynolds number is not given" + }, + { + "q": "Could you work out the Cowling number given first Cowling number = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic Reynolds number is not given" + }, + { + "q": "Do you know the Cowling number of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the Cowling number of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the Cowling number given first Cowling number = -0.318734, magnetic Reynolds number = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative first Cowling number: no example had one" + }, + { + "q": "What is a typical Cowling number?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-cowling-number/program.py b/root/organs10/physics/specialists/solved-cowling-number/program.py new file mode 100644 index 0000000000000000000000000000000000000000..1aa8cdd76a81c80cb6c38cf26020b1acf999914e --- /dev/null +++ b/root/organs10/physics/specialists/solved-cowling-number/program.py @@ -0,0 +1,7 @@ +"""Cowling number from first Cowling number, magnetic Reynolds number (first Cowling number (Wikidata Q105084863)) +The maths organ (systems.py): solved from the system q0 = q1*q2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(first_cowling_number, magnetic_reynolds_number): + return 1.0*first_cowling_number/magnetic_reynolds_number diff --git a/root/organs10/physics/specialists/solved-cowling-number/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-cowling-number/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-cowling-number/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-cross-sectional-area-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-cross-sectional-area-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-cross-sectional-area-2/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-cross-sectional-area-2/panel.json b/root/organs10/physics/specialists/solved-cross-sectional-area-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..d445cf36a7e43522d444a9edbaf3e188b38637b7 --- /dev/null +++ b/root/organs10/physics/specialists/solved-cross-sectional-area-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the cross-sectional area for specific rotation 107.961, angle of optical rotation 59.4105 and mass 5.77717?", + "a": "10.498254293702432", + "valid": true, + "kind": "compute", + "expect": 10.498254293702432, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [107.960666, 59.4105, 5.777165] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Tell me the cross-sectional area with specific rotation of 107.961, angle of optical rotation of 59.4105 and mass of 5.77717.", + "a": "10.498254293702432", + "valid": true, + "kind": "compute", + "expect": 10.498254293702432, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [107.960666, 59.4105, 5.777165] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "What is the cross-sectional area for specific rotation 85.2416, angle of optical rotation 160.878 and mass 2.69428?", + "a": "1.4275672119863798", + "valid": true, + "kind": "compute", + "expect": 1.4275672119863798, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [85.241623, 160.878442, 2.69428] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Tell me the cross-sectional area with specific rotation of 85.2416, angle of optical rotation of 160.878 and mass of 2.69428.", + "a": "1.4275672119863798", + "valid": true, + "kind": "compute", + "expect": 1.4275672119863798, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [85.241623, 160.878442, 2.69428] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "What is the cross-sectional area for specific rotation 45.6157, angle of optical rotation 4.97131 and mass 94.305?", + "a": "865.3232325447225", + "valid": true, + "kind": "compute", + "expect": 865.3232325447225, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [45.61567, 4.971307, 94.305046] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Tell me the cross-sectional area with specific rotation of 45.6157, angle of optical rotation of 4.97131 and mass of 94.305.", + "a": "865.3232325447225", + "valid": true, + "kind": "compute", + "expect": 865.3232325447225, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [45.61567, 4.971307, 94.305046] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "What is the cross-sectional area for specific rotation 10.5638, angle of optical rotation 198.839 and mass 0.671697?", + "a": "0.03568568130708054", + "valid": true, + "kind": "compute", + "expect": 0.03568568130708054, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [10.563843, 198.838955, 0.671697] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Tell me the cross-sectional area with specific rotation of 10.5638, angle of optical rotation of 198.839 and mass of 0.671697.", + "a": "0.03568568130708054", + "valid": true, + "kind": "compute", + "expect": 0.03568568130708054, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [10.563843, 198.838955, 0.671697] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "What is the cross-sectional area for specific rotation 21.7931, angle of optical rotation 29.5106 and mass 8.50341?", + "a": "6.279634237980215", + "valid": true, + "kind": "compute", + "expect": 6.279634237980215, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [21.793126, 29.510609, 8.503407] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Tell me the cross-sectional area with specific rotation of 21.7931, angle of optical rotation of 29.5106 and mass of 8.50341.", + "a": "6.279634237980215", + "valid": true, + "kind": "compute", + "expect": 6.279634237980215, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [21.793126, 29.510609, 8.503407] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "What is the cross-sectional area for specific rotation 223.509, angle of optical rotation 17.3414 and mass 7.10705?", + "a": "91.60072422473009", + "valid": true, + "kind": "compute", + "expect": 91.60072422473009, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [223.508796, 17.341435, 7.107049] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Tell me the cross-sectional area with specific rotation of 223.509, angle of optical rotation of 17.3414 and mass of 7.10705.", + "a": "91.60072422473009", + "valid": true, + "kind": "compute", + "expect": 91.60072422473009, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [223.508796, 17.341435, 7.107049] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "What is the cross-sectional area for specific rotation 107.961 and angle of optical rotation 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Tell me the cross-sectional area with specific rotation of 85.2416 and angle of optical rotation of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "What is the cross-sectional area of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the cross-sectional area of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the cross-sectional area with specific rotation of -45.6157, angle of optical rotation of 4.97131 and mass of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative specific rotation: no example had one" + }, + { + "q": "Which cross-sectional area is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the specific rotation is 17.7888, angle of optical rotation is 167.625 and the mass is 10.3291, what is the cross-sectional area?", + "a": "1.0961450581531387", + "valid": true, + "kind": "compute", + "expect": 1.0961450581531387, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [17.788778, 167.624948, 10.329054] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Could you work out the cross-sectional area given specific rotation = 17.7888, angle of optical rotation = 167.625, mass = 10.3291?", + "a": "1.0961450581531387", + "valid": true, + "kind": "compute", + "expect": 1.0961450581531387, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [17.788778, 167.624948, 10.329054] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "I need the cross-sectional area for a specific rotation of 21.511, an angle of optical rotation of 173.394 and a mass of 251.084, please.", + "a": "31.149197612202414", + "valid": true, + "kind": "compute", + "expect": 31.149197612202414, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [21.511016, 173.393638, 251.084037] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "If the specific rotation is 21.511, angle of optical rotation is 173.394 and the mass is 251.084, what is the cross-sectional area?", + "a": "31.149197612202414", + "valid": true, + "kind": "compute", + "expect": 31.149197612202414, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [21.511016, 173.393638, 251.084037] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Could you work out the cross-sectional area given specific rotation = 129.245, angle of optical rotation = 1.18359, mass = 15.9416?", + "a": "1740.7830274187056", + "valid": true, + "kind": "compute", + "expect": 1740.7830274187056, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [129.244984, 1.18359, 15.941612] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "I need the cross-sectional area for a specific rotation of 129.245, an angle of optical rotation of 1.18359 and a mass of 15.9416, please.", + "a": "1740.7830274187056", + "valid": true, + "kind": "compute", + "expect": 1740.7830274187056, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [129.244984, 1.18359, 15.941612] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "If the specific rotation is 20.3966, angle of optical rotation is 18.3169 and the mass is 6.8386, what is the cross-sectional area?", + "a": "7.615050191987504", + "valid": true, + "kind": "compute", + "expect": 7.615050191987504, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [20.396558, 18.316867, 6.838598] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Could you work out the cross-sectional area given specific rotation = 20.3966, angle of optical rotation = 18.3169, mass = 6.8386?", + "a": "7.615050191987504", + "valid": true, + "kind": "compute", + "expect": 7.615050191987504, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [20.396558, 18.316867, 6.838598] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "I need the cross-sectional area for a specific rotation of 185.886, an angle of optical rotation of 13.2373 and a mass of 4.71941, please.", + "a": "66.27285731134774", + "valid": true, + "kind": "compute", + "expect": 66.27285731134774, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [185.886273, 13.237311, 4.719415] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "If the specific rotation is 185.886, angle of optical rotation is 13.2373 and the mass is 4.71941, what is the cross-sectional area?", + "a": "66.27285731134774", + "valid": true, + "kind": "compute", + "expect": 66.27285731134774, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [185.886273, 13.237311, 4.719415] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Could you work out the cross-sectional area given specific rotation = 9.1061, angle of optical rotation = 24.5721, mass = 108.451?", + "a": "40.19049097830896", + "valid": true, + "kind": "compute", + "expect": 40.19049097830896, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [9.106104, 24.572086, 108.450792] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "I need the cross-sectional area for a specific rotation of 9.1061, an angle of optical rotation of 24.5721 and a mass of 108.451, please.", + "a": "40.19049097830896", + "valid": true, + "kind": "compute", + "expect": 40.19049097830896, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [9.106104, 24.572086, 108.450792] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "If the specific rotation is 17.7888 and the angle of optical rotation is 167.625, what is the cross-sectional area?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Could you work out the cross-sectional area given specific rotation = 21.511, angle of optical rotation = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Do you know the cross-sectional area of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the cross-sectional area of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the cross-sectional area given specific rotation = -129.245, angle of optical rotation = 1.18359, mass = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative specific rotation: no example had one" + }, + { + "q": "What is a typical cross-sectional area?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-cross-sectional-area-2/program.py b/root/organs10/physics/specialists/solved-cross-sectional-area-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..9653ee7507fd852321ba725dab7027138b7a8ff3 --- /dev/null +++ b/root/organs10/physics/specialists/solved-cross-sectional-area-2/program.py @@ -0,0 +1,7 @@ +"""cross-sectional area from specific rotation, angle of optical rotation, mass (specific rotation (Wikidata Q2191631)) +The maths organ (systems.py): solved from the system q0 = q1*((q2)/(q3)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(specific_rotation, angle_of_optical_rotation, mass): + return 1.0*mass*specific_rotation/angle_of_optical_rotation diff --git a/root/organs10/physics/specialists/solved-cross-sectional-area-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-cross-sectional-area-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-cross-sectional-area-2/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-cross-sectional-area/organ_formula_v2.py b/root/organs10/physics/specialists/solved-cross-sectional-area/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-cross-sectional-area/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-cross-sectional-area/panel.json b/root/organs10/physics/specialists/solved-cross-sectional-area/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..292323cc11f5e5994ec1e219a3504b63c9e11288 --- /dev/null +++ b/root/organs10/physics/specialists/solved-cross-sectional-area/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the cross-sectional area for molar optical rotatory power 107.961, angle of optical rotation 59.4105 and amount of substance 5.77717?", + "a": "10.498254293702432", + "valid": true, + "kind": "compute", + "expect": 10.498254293702432, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [107.960666, 59.4105, 5.777165] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Tell me the cross-sectional area with molar optical rotatory power of 107.961, angle of optical rotation of 59.4105 and amount of substance of 5.77717.", + "a": "10.498254293702432", + "valid": true, + "kind": "compute", + "expect": 10.498254293702432, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [107.960666, 59.4105, 5.777165] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "What is the cross-sectional area for molar optical rotatory power 85.2416, angle of optical rotation 160.878 and amount of substance 2.69428?", + "a": "1.4275672119863798", + "valid": true, + "kind": "compute", + "expect": 1.4275672119863798, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [85.241623, 160.878442, 2.69428] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Tell me the cross-sectional area with molar optical rotatory power of 85.2416, angle of optical rotation of 160.878 and amount of substance of 2.69428.", + "a": "1.4275672119863798", + "valid": true, + "kind": "compute", + "expect": 1.4275672119863798, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [85.241623, 160.878442, 2.69428] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "What is the cross-sectional area for molar optical rotatory power 45.6157, angle of optical rotation 4.97131 and amount of substance 94.305?", + "a": "865.3232325447225", + "valid": true, + "kind": "compute", + "expect": 865.3232325447225, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [45.61567, 4.971307, 94.305046] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Tell me the cross-sectional area with molar optical rotatory power of 45.6157, angle of optical rotation of 4.97131 and amount of substance of 94.305.", + "a": "865.3232325447225", + "valid": true, + "kind": "compute", + "expect": 865.3232325447225, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [45.61567, 4.971307, 94.305046] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "What is the cross-sectional area for molar optical rotatory power 10.5638, angle of optical rotation 198.839 and amount of substance 0.671697?", + "a": "0.03568568130708054", + "valid": true, + "kind": "compute", + "expect": 0.03568568130708054, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [10.563843, 198.838955, 0.671697] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Tell me the cross-sectional area with molar optical rotatory power of 10.5638, angle of optical rotation of 198.839 and amount of substance of 0.671697.", + "a": "0.03568568130708054", + "valid": true, + "kind": "compute", + "expect": 0.03568568130708054, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [10.563843, 198.838955, 0.671697] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "What is the cross-sectional area for molar optical rotatory power 21.7931, angle of optical rotation 29.5106 and amount of substance 8.50341?", + "a": "6.279634237980215", + "valid": true, + "kind": "compute", + "expect": 6.279634237980215, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [21.793126, 29.510609, 8.503407] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Tell me the cross-sectional area with molar optical rotatory power of 21.7931, angle of optical rotation of 29.5106 and amount of substance of 8.50341.", + "a": "6.279634237980215", + "valid": true, + "kind": "compute", + "expect": 6.279634237980215, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [21.793126, 29.510609, 8.503407] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "What is the cross-sectional area for molar optical rotatory power 223.509, angle of optical rotation 17.3414 and amount of substance 7.10705?", + "a": "91.60072422473009", + "valid": true, + "kind": "compute", + "expect": 91.60072422473009, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [223.508796, 17.341435, 7.107049] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Tell me the cross-sectional area with molar optical rotatory power of 223.509, angle of optical rotation of 17.3414 and amount of substance of 7.10705.", + "a": "91.60072422473009", + "valid": true, + "kind": "compute", + "expect": 91.60072422473009, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [223.508796, 17.341435, 7.107049] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "What is the cross-sectional area for molar optical rotatory power 107.961 and angle of optical rotation 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the amount of substance is not given" + }, + { + "q": "Tell me the cross-sectional area with molar optical rotatory power of 85.2416 and angle of optical rotation of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the amount of substance is not given" + }, + { + "q": "What is the cross-sectional area of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the cross-sectional area of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the cross-sectional area with molar optical rotatory power of -45.6157, angle of optical rotation of 4.97131 and amount of substance of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative molar optical rotatory power: no example had one" + }, + { + "q": "Which cross-sectional area is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the molar optical rotatory power is 17.7888, angle of optical rotation is 167.625 and the amount of substance is 10.3291, what is the cross-sectional area?", + "a": "1.0961450581531387", + "valid": true, + "kind": "compute", + "expect": 1.0961450581531387, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [17.788778, 167.624948, 10.329054] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Could you work out the cross-sectional area given molar optical rotatory power = 17.7888, angle of optical rotation = 167.625, amount of substance = 10.3291?", + "a": "1.0961450581531387", + "valid": true, + "kind": "compute", + "expect": 1.0961450581531387, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [17.788778, 167.624948, 10.329054] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "I need the cross-sectional area for a molar optical rotatory power of 21.511, an angle of optical rotation of 173.394 and an amount of substance of 251.084, please.", + "a": "31.149197612202414", + "valid": true, + "kind": "compute", + "expect": 31.149197612202414, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [21.511016, 173.393638, 251.084037] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "If the molar optical rotatory power is 21.511, angle of optical rotation is 173.394 and the amount of substance is 251.084, what is the cross-sectional area?", + "a": "31.149197612202414", + "valid": true, + "kind": "compute", + "expect": 31.149197612202414, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [21.511016, 173.393638, 251.084037] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Could you work out the cross-sectional area given molar optical rotatory power = 129.245, angle of optical rotation = 1.18359, amount of substance = 15.9416?", + "a": "1740.7830274187056", + "valid": true, + "kind": "compute", + "expect": 1740.7830274187056, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [129.244984, 1.18359, 15.941612] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "I need the cross-sectional area for a molar optical rotatory power of 129.245, an angle of optical rotation of 1.18359 and an amount of substance of 15.9416, please.", + "a": "1740.7830274187056", + "valid": true, + "kind": "compute", + "expect": 1740.7830274187056, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [129.244984, 1.18359, 15.941612] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "If the molar optical rotatory power is 20.3966, angle of optical rotation is 18.3169 and the amount of substance is 6.8386, what is the cross-sectional area?", + "a": "7.615050191987504", + "valid": true, + "kind": "compute", + "expect": 7.615050191987504, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [20.396558, 18.316867, 6.838598] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Could you work out the cross-sectional area given molar optical rotatory power = 20.3966, angle of optical rotation = 18.3169, amount of substance = 6.8386?", + "a": "7.615050191987504", + "valid": true, + "kind": "compute", + "expect": 7.615050191987504, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [20.396558, 18.316867, 6.838598] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "I need the cross-sectional area for a molar optical rotatory power of 185.886, an angle of optical rotation of 13.2373 and an amount of substance of 4.71941, please.", + "a": "66.27285731134774", + "valid": true, + "kind": "compute", + "expect": 66.27285731134774, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [185.886273, 13.237311, 4.719415] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "If the molar optical rotatory power is 185.886, angle of optical rotation is 13.2373 and the amount of substance is 4.71941, what is the cross-sectional area?", + "a": "66.27285731134774", + "valid": true, + "kind": "compute", + "expect": 66.27285731134774, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [185.886273, 13.237311, 4.719415] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Could you work out the cross-sectional area given molar optical rotatory power = 9.1061, angle of optical rotation = 24.5721, amount of substance = 108.451?", + "a": "40.19049097830896", + "valid": true, + "kind": "compute", + "expect": 40.19049097830896, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [9.106104, 24.572086, 108.450792] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "I need the cross-sectional area for a molar optical rotatory power of 9.1061, an angle of optical rotation of 24.5721 and an amount of substance of 108.451, please.", + "a": "40.19049097830896", + "valid": true, + "kind": "compute", + "expect": 40.19049097830896, + "dtype": "quantity", + "unit": "m^2", + "why": "cross-sectional area for [9.106104, 24.572086, 108.450792] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "If the molar optical rotatory power is 17.7888 and the angle of optical rotation is 167.625, what is the cross-sectional area?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the amount of substance is not given" + }, + { + "q": "Could you work out the cross-sectional area given molar optical rotatory power = 21.511, angle of optical rotation = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the amount of substance is not given" + }, + { + "q": "Do you know the cross-sectional area of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the cross-sectional area of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the cross-sectional area given molar optical rotatory power = -129.245, angle of optical rotation = 1.18359, amount of substance = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative molar optical rotatory power: no example had one" + }, + { + "q": "What is a typical cross-sectional area?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-cross-sectional-area/program.py b/root/organs10/physics/specialists/solved-cross-sectional-area/program.py new file mode 100644 index 0000000000000000000000000000000000000000..7db8140009afea0ea7feb57138780e34aaee2f3e --- /dev/null +++ b/root/organs10/physics/specialists/solved-cross-sectional-area/program.py @@ -0,0 +1,7 @@ +"""cross-sectional area from molar optical rotatory power, angle of optical rotation, amount of substance (molar optical rotatory power (Wikidata Q96346994)) +The maths organ (systems.py): solved from the system q0 = q1*((q2)/(q3)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(molar_optical_rotatory_power, angle_of_optical_rotation, amount_of_substance): + return 1.0*amount_of_substance*molar_optical_rotatory_power/angle_of_optical_rotation diff --git a/root/organs10/physics/specialists/solved-cross-sectional-area/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-cross-sectional-area/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-cross-sectional-area/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-current-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-current-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-current-2/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-current-2/panel.json b/root/organs10/physics/specialists/solved-current-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..131ce4481f4fe1c801bef825b3e6d151af05d9da --- /dev/null +++ b/root/organs10/physics/specialists/solved-current-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the current for power 107.961 and voltage 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "A", + "why": "current for [107.960666, 59.4105] (the system Electric power)" + }, + { + "q": "Tell me the current with power of 107.961 and voltage of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "A", + "why": "current for [107.960666, 59.4105] (the system Electric power)" + }, + { + "q": "What is the current for power 169.527 and voltage 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "A", + "why": "current for [169.526958, 280.773702] (the system Electric power)" + }, + { + "q": "Tell me the current with power of 169.527 and voltage of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "A", + "why": "current for [169.526958, 280.773702] (the system Electric power)" + }, + { + "q": "What is the current for power 1.91195 and voltage 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "A", + "why": "current for [1.911952, 82.240072] (the system Electric power)" + }, + { + "q": "Tell me the current with power of 1.91195 and voltage of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "A", + "why": "current for [1.911952, 82.240072] (the system Electric power)" + }, + { + "q": "What is the current for power 1.64379 and voltage 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "A", + "why": "current for [1.643795, 252.709024] (the system Electric power)" + }, + { + "q": "Tell me the current with power of 1.64379 and voltage of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "A", + "why": "current for [1.643795, 252.709024] (the system Electric power)" + }, + { + "q": "What is the current for power 18.3169 and voltage 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "A", + "why": "current for [18.316867, 6.838598] (the system Electric power)" + }, + { + "q": "Tell me the current with power of 18.3169 and voltage of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "A", + "why": "current for [18.316867, 6.838598] (the system Electric power)" + }, + { + "q": "What is the current for power 156.502 and voltage 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "A", + "why": "current for [156.50181, 185.886273] (the system Electric power)" + }, + { + "q": "Tell me the current with power of 156.502 and voltage of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "A", + "why": "current for [156.50181, 185.886273] (the system Electric power)" + }, + { + "q": "What is the current for power 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the voltage is not given" + }, + { + "q": "Tell me the current with power of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the voltage is not given" + }, + { + "q": "What is the current of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the current of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the current with power of -1.91195 and voltage of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative power: no example had one" + }, + { + "q": "Which current is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the power is 8.50728 and the voltage is 17.7888, what is the current?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "A", + "why": "current for [8.507283, 17.788778] (the system Electric power)" + }, + { + "q": "Could you work out the current given power = 8.50728, voltage = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "A", + "why": "current for [8.507283, 17.788778] (the system Electric power)" + }, + { + "q": "I need the current for a power of 8.24951 and a voltage of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "A", + "why": "current for [8.24951, 0.634021] (the system Electric power)" + }, + { + "q": "If the power is 8.24951 and the voltage is 0.634021, what is the current?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "A", + "why": "current for [8.24951, 0.634021] (the system Electric power)" + }, + { + "q": "Could you work out the current given power = 0.318734, voltage = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "A", + "why": "current for [0.318734, 9.566071] (the system Electric power)" + }, + { + "q": "I need the current for a power of 0.318734 and a voltage of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "A", + "why": "current for [0.318734, 9.566071] (the system Electric power)" + }, + { + "q": "If the power is 0.671697 and the voltage is 14.2496, what is the current?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "A", + "why": "current for [0.671697, 14.249569] (the system Electric power)" + }, + { + "q": "Could you work out the current given power = 0.671697, voltage = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "A", + "why": "current for [0.671697, 14.249569] (the system Electric power)" + }, + { + "q": "I need the current for a power of 21.7931 and a voltage of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "A", + "why": "current for [21.793126, 29.510609] (the system Electric power)" + }, + { + "q": "If the power is 21.7931 and the voltage is 29.5106, what is the current?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "A", + "why": "current for [21.793126, 29.510609] (the system Electric power)" + }, + { + "q": "Could you work out the current given power = 153.15, voltage = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "A", + "why": "current for [153.149965, 18.595315] (the system Electric power)" + }, + { + "q": "I need the current for a power of 153.15 and a voltage of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "A", + "why": "current for [153.149965, 18.595315] (the system Electric power)" + }, + { + "q": "If the power is 8.50728, what is the current?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the voltage is not given" + }, + { + "q": "Could you work out the current given power = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the voltage is not given" + }, + { + "q": "Do you know the current of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the current of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the current given power = -0.318734, voltage = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative power: no example had one" + }, + { + "q": "What is a typical current?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-current-2/program.py b/root/organs10/physics/specialists/solved-current-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..16bdef2c23637dfea2048de5d9f3f3e6b3489360 --- /dev/null +++ b/root/organs10/physics/specialists/solved-current-2/program.py @@ -0,0 +1,7 @@ +"""current from power, voltage (Electric power) +The maths organ (systems.py): solved from the system P = U*I; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(power, voltage): + return 1.0*power/voltage diff --git a/root/organs10/physics/specialists/solved-current-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-current-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-current-2/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-current-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-current-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-current-3/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-current-3/panel.json b/root/organs10/physics/specialists/solved-current-3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..dac9a6b469781382ad71c9f2a9dce66e9e03e3d6 --- /dev/null +++ b/root/organs10/physics/specialists/solved-current-3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the current for charge 107.961 and time 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "A", + "why": "current for [107.960666, 59.4105] (the system Charge)" + }, + { + "q": "Tell me the current with charge of 107.961 and time of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "A", + "why": "current for [107.960666, 59.4105] (the system Charge)" + }, + { + "q": "What is the current for charge 169.527 and time 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "A", + "why": "current for [169.526958, 280.773702] (the system Charge)" + }, + { + "q": "Tell me the current with charge of 169.527 and time of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "A", + "why": "current for [169.526958, 280.773702] (the system Charge)" + }, + { + "q": "What is the current for charge 1.91195 and time 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "A", + "why": "current for [1.911952, 82.240072] (the system Charge)" + }, + { + "q": "Tell me the current with charge of 1.91195 and time of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "A", + "why": "current for [1.911952, 82.240072] (the system Charge)" + }, + { + "q": "What is the current for charge 1.64379 and time 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "A", + "why": "current for [1.643795, 252.709024] (the system Charge)" + }, + { + "q": "Tell me the current with charge of 1.64379 and time of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "A", + "why": "current for [1.643795, 252.709024] (the system Charge)" + }, + { + "q": "What is the current for charge 18.3169 and time 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "A", + "why": "current for [18.316867, 6.838598] (the system Charge)" + }, + { + "q": "Tell me the current with charge of 18.3169 and time of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "A", + "why": "current for [18.316867, 6.838598] (the system Charge)" + }, + { + "q": "What is the current for charge 156.502 and time 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "A", + "why": "current for [156.50181, 185.886273] (the system Charge)" + }, + { + "q": "Tell me the current with charge of 156.502 and time of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "A", + "why": "current for [156.50181, 185.886273] (the system Charge)" + }, + { + "q": "What is the current for charge 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "Tell me the current with charge of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "What is the current of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the current of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the current with charge of -1.91195 and time of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative charge: no example had one" + }, + { + "q": "Which current is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the charge is 8.50728 and the time is 17.7888, what is the current?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "A", + "why": "current for [8.507283, 17.788778] (the system Charge)" + }, + { + "q": "Could you work out the current given charge = 8.50728, time = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "A", + "why": "current for [8.507283, 17.788778] (the system Charge)" + }, + { + "q": "I need the current for a charge of 8.24951 and a time of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "A", + "why": "current for [8.24951, 0.634021] (the system Charge)" + }, + { + "q": "If the charge is 8.24951 and the time is 0.634021, what is the current?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "A", + "why": "current for [8.24951, 0.634021] (the system Charge)" + }, + { + "q": "Could you work out the current given charge = 0.318734, time = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "A", + "why": "current for [0.318734, 9.566071] (the system Charge)" + }, + { + "q": "I need the current for a charge of 0.318734 and a time of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "A", + "why": "current for [0.318734, 9.566071] (the system Charge)" + }, + { + "q": "If the charge is 0.671697 and the time is 14.2496, what is the current?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "A", + "why": "current for [0.671697, 14.249569] (the system Charge)" + }, + { + "q": "Could you work out the current given charge = 0.671697, time = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "A", + "why": "current for [0.671697, 14.249569] (the system Charge)" + }, + { + "q": "I need the current for a charge of 21.7931 and a time of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "A", + "why": "current for [21.793126, 29.510609] (the system Charge)" + }, + { + "q": "If the charge is 21.7931 and the time is 29.5106, what is the current?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "A", + "why": "current for [21.793126, 29.510609] (the system Charge)" + }, + { + "q": "Could you work out the current given charge = 153.15, time = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "A", + "why": "current for [153.149965, 18.595315] (the system Charge)" + }, + { + "q": "I need the current for a charge of 153.15 and a time of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "A", + "why": "current for [153.149965, 18.595315] (the system Charge)" + }, + { + "q": "If the charge is 8.50728, what is the current?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "Could you work out the current given charge = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "Do you know the current of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the current of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the current given charge = -0.318734, time = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative charge: no example had one" + }, + { + "q": "What is a typical current?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-current-3/program.py b/root/organs10/physics/specialists/solved-current-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..9771366451dd3c0508e1ed7b18f9b95a31f8e205 --- /dev/null +++ b/root/organs10/physics/specialists/solved-current-3/program.py @@ -0,0 +1,7 @@ +"""current from charge, time (Charge) +The maths organ (systems.py): solved from the system Q = I*t; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(charge, time): + return 1.0*charge/time diff --git a/root/organs10/physics/specialists/solved-current-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-current-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-current-3/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-current/organ_formula_v2.py b/root/organs10/physics/specialists/solved-current/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-current/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-current/panel.json b/root/organs10/physics/specialists/solved-current/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..09cacc6e13694f97ab81d1abfb8a0f8232d71175 --- /dev/null +++ b/root/organs10/physics/specialists/solved-current/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the current for voltage 107.961 and resistance 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "A", + "why": "current for [107.960666, 59.4105] (the system Ohm's law)" + }, + { + "q": "Tell me the current with voltage of 107.961 and resistance of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "A", + "why": "current for [107.960666, 59.4105] (the system Ohm's law)" + }, + { + "q": "What is the current for voltage 169.527 and resistance 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "A", + "why": "current for [169.526958, 280.773702] (the system Ohm's law)" + }, + { + "q": "Tell me the current with voltage of 169.527 and resistance of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "A", + "why": "current for [169.526958, 280.773702] (the system Ohm's law)" + }, + { + "q": "What is the current for voltage 1.91195 and resistance 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "A", + "why": "current for [1.911952, 82.240072] (the system Ohm's law)" + }, + { + "q": "Tell me the current with voltage of 1.91195 and resistance of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "A", + "why": "current for [1.911952, 82.240072] (the system Ohm's law)" + }, + { + "q": "What is the current for voltage 1.64379 and resistance 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "A", + "why": "current for [1.643795, 252.709024] (the system Ohm's law)" + }, + { + "q": "Tell me the current with voltage of 1.64379 and resistance of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "A", + "why": "current for [1.643795, 252.709024] (the system Ohm's law)" + }, + { + "q": "What is the current for voltage 18.3169 and resistance 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "A", + "why": "current for [18.316867, 6.838598] (the system Ohm's law)" + }, + { + "q": "Tell me the current with voltage of 18.3169 and resistance of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "A", + "why": "current for [18.316867, 6.838598] (the system Ohm's law)" + }, + { + "q": "What is the current for voltage 156.502 and resistance 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "A", + "why": "current for [156.50181, 185.886273] (the system Ohm's law)" + }, + { + "q": "Tell me the current with voltage of 156.502 and resistance of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "A", + "why": "current for [156.50181, 185.886273] (the system Ohm's law)" + }, + { + "q": "What is the current for voltage 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the resistance is not given" + }, + { + "q": "Tell me the current with voltage of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the resistance is not given" + }, + { + "q": "What is the current of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the current of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the current with voltage of -1.91195 and resistance of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative voltage: no example had one" + }, + { + "q": "Which current is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the voltage is 8.50728 and the resistance is 17.7888, what is the current?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "A", + "why": "current for [8.507283, 17.788778] (the system Ohm's law)" + }, + { + "q": "Could you work out the current given voltage = 8.50728, resistance = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "A", + "why": "current for [8.507283, 17.788778] (the system Ohm's law)" + }, + { + "q": "I need the current for a voltage of 8.24951 and a resistance of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "A", + "why": "current for [8.24951, 0.634021] (the system Ohm's law)" + }, + { + "q": "If the voltage is 8.24951 and the resistance is 0.634021, what is the current?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "A", + "why": "current for [8.24951, 0.634021] (the system Ohm's law)" + }, + { + "q": "Could you work out the current given voltage = 0.318734, resistance = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "A", + "why": "current for [0.318734, 9.566071] (the system Ohm's law)" + }, + { + "q": "I need the current for a voltage of 0.318734 and a resistance of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "A", + "why": "current for [0.318734, 9.566071] (the system Ohm's law)" + }, + { + "q": "If the voltage is 0.671697 and the resistance is 14.2496, what is the current?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "A", + "why": "current for [0.671697, 14.249569] (the system Ohm's law)" + }, + { + "q": "Could you work out the current given voltage = 0.671697, resistance = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "A", + "why": "current for [0.671697, 14.249569] (the system Ohm's law)" + }, + { + "q": "I need the current for a voltage of 21.7931 and a resistance of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "A", + "why": "current for [21.793126, 29.510609] (the system Ohm's law)" + }, + { + "q": "If the voltage is 21.7931 and the resistance is 29.5106, what is the current?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "A", + "why": "current for [21.793126, 29.510609] (the system Ohm's law)" + }, + { + "q": "Could you work out the current given voltage = 153.15, resistance = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "A", + "why": "current for [153.149965, 18.595315] (the system Ohm's law)" + }, + { + "q": "I need the current for a voltage of 153.15 and a resistance of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "A", + "why": "current for [153.149965, 18.595315] (the system Ohm's law)" + }, + { + "q": "If the voltage is 8.50728, what is the current?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the resistance is not given" + }, + { + "q": "Could you work out the current given voltage = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the resistance is not given" + }, + { + "q": "Do you know the current of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the current of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the current given voltage = -0.318734, resistance = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative voltage: no example had one" + }, + { + "q": "What is a typical current?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-current/program.py b/root/organs10/physics/specialists/solved-current/program.py new file mode 100644 index 0000000000000000000000000000000000000000..39e7a04113aa68e34c942c4ae47f405f9ac04183 --- /dev/null +++ b/root/organs10/physics/specialists/solved-current/program.py @@ -0,0 +1,7 @@ +"""current from voltage, resistance (Ohm's law) +The maths organ (systems.py): solved from the system U = I*R; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(voltage, resistance): + return 1.0*voltage/resistance diff --git a/root/organs10/physics/specialists/solved-current/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-current/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-current/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-darcy-number/organ_formula_v2.py b/root/organs10/physics/specialists/solved-darcy-number/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-darcy-number/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-darcy-number/panel.json b/root/organs10/physics/specialists/solved-darcy-number/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..f96ad2533fff6cbbb4e7ec64d80842346dd94f60 --- /dev/null +++ b/root/organs10/physics/specialists/solved-darcy-number/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the Darcy number for permeability 107.961 and characteristic length 59.4105?", + "a": "0.030587158736956204", + "valid": true, + "kind": "compute", + "expect": 0.030587158736956204, + "dtype": "quantity", + "unit": "1", + "why": "Darcy number for [107.960666, 59.4105] (the system Darcy number (Wikidata Q16954269))" + }, + { + "q": "Tell me the Darcy number with permeability of 107.961 and characteristic length of 59.4105.", + "a": "0.030587158736956204", + "valid": true, + "kind": "compute", + "expect": 0.030587158736956204, + "dtype": "quantity", + "unit": "1", + "why": "Darcy number for [107.960666, 59.4105] (the system Darcy number (Wikidata Q16954269))" + }, + { + "q": "What is the Darcy number for permeability 169.527 and characteristic length 280.774?", + "a": "0.0021504329786134614", + "valid": true, + "kind": "compute", + "expect": 0.0021504329786134614, + "dtype": "quantity", + "unit": "1", + "why": "Darcy number for [169.526958, 280.773702] (the system Darcy number (Wikidata Q16954269))" + }, + { + "q": "Tell me the Darcy number with permeability of 169.527 and characteristic length of 280.774.", + "a": "0.0021504329786134614", + "valid": true, + "kind": "compute", + "expect": 0.0021504329786134614, + "dtype": "quantity", + "unit": "1", + "why": "Darcy number for [169.526958, 280.773702] (the system Darcy number (Wikidata Q16954269))" + }, + { + "q": "What is the Darcy number for permeability 1.91195 and characteristic length 82.2401?", + "a": "0.0002826896896815724", + "valid": true, + "kind": "compute", + "expect": 0.0002826896896815724, + "dtype": "quantity", + "unit": "1", + "why": "Darcy number for [1.911952, 82.240072] (the system Darcy number (Wikidata Q16954269))" + }, + { + "q": "Tell me the Darcy number with permeability of 1.91195 and characteristic length of 82.2401.", + "a": "0.0002826896896815724", + "valid": true, + "kind": "compute", + "expect": 0.0002826896896815724, + "dtype": "quantity", + "unit": "1", + "why": "Darcy number for [1.911952, 82.240072] (the system Darcy number (Wikidata Q16954269))" + }, + { + "q": "What is the Darcy number for permeability 1.64379 and characteristic length 252.709?", + "a": "2.5739866144509097e-05", + "valid": true, + "kind": "compute", + "expect": 2.5739866144509097e-05, + "dtype": "quantity", + "unit": "1", + "why": "Darcy number for [1.643795, 252.709024] (the system Darcy number (Wikidata Q16954269))" + }, + { + "q": "Tell me the Darcy number with permeability of 1.64379 and characteristic length of 252.709.", + "a": "2.5739866144509097e-05", + "valid": true, + "kind": "compute", + "expect": 2.5739866144509097e-05, + "dtype": "quantity", + "unit": "1", + "why": "Darcy number for [1.643795, 252.709024] (the system Darcy number (Wikidata Q16954269))" + }, + { + "q": "What is the Darcy number for permeability 18.3169 and characteristic length 6.8386?", + "a": "0.3916670481342175", + "valid": true, + "kind": "compute", + "expect": 0.3916670481342175, + "dtype": "quantity", + "unit": "1", + "why": "Darcy number for [18.316867, 6.838598] (the system Darcy number (Wikidata Q16954269))" + }, + { + "q": "Tell me the Darcy number with permeability of 18.3169 and characteristic length of 6.8386.", + "a": "0.3916670481342175", + "valid": true, + "kind": "compute", + "expect": 0.3916670481342175, + "dtype": "quantity", + "unit": "1", + "why": "Darcy number for [18.316867, 6.838598] (the system Darcy number (Wikidata Q16954269))" + }, + { + "q": "What is the Darcy number for permeability 156.502 and characteristic length 185.886?", + "a": "0.004529233648974367", + "valid": true, + "kind": "compute", + "expect": 0.004529233648974367, + "dtype": "quantity", + "unit": "1", + "why": "Darcy number for [156.50181, 185.886273] (the system Darcy number (Wikidata Q16954269))" + }, + { + "q": "Tell me the Darcy number with permeability of 156.502 and characteristic length of 185.886.", + "a": "0.004529233648974367", + "valid": true, + "kind": "compute", + "expect": 0.004529233648974367, + "dtype": "quantity", + "unit": "1", + "why": "Darcy number for [156.50181, 185.886273] (the system Darcy number (Wikidata Q16954269))" + }, + { + "q": "What is the Darcy number for permeability 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the characteristic length is not given" + }, + { + "q": "Tell me the Darcy number with permeability of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the characteristic length is not given" + }, + { + "q": "What is the Darcy number of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the Darcy number of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the Darcy number with permeability of -1.91195 and characteristic length of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative permeability: no example had one" + }, + { + "q": "Which Darcy number is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the permeability is 8.50728 and the characteristic length is 17.7888, what is the Darcy number?", + "a": "0.026884293358369424", + "valid": true, + "kind": "compute", + "expect": 0.026884293358369424, + "dtype": "quantity", + "unit": "1", + "why": "Darcy number for [8.507283, 17.788778] (the system Darcy number (Wikidata Q16954269))" + }, + { + "q": "Could you work out the Darcy number given permeability = 8.50728, characteristic length = 17.7888?", + "a": "0.026884293358369424", + "valid": true, + "kind": "compute", + "expect": 0.026884293358369424, + "dtype": "quantity", + "unit": "1", + "why": "Darcy number for [8.507283, 17.788778] (the system Darcy number (Wikidata Q16954269))" + }, + { + "q": "I need the Darcy number for a permeability of 8.24951 and a characteristic length of 0.634021, please.", + "a": "20.52205739786059", + "valid": true, + "kind": "compute", + "expect": 20.52205739786059, + "dtype": "quantity", + "unit": "1", + "why": "Darcy number for [8.24951, 0.634021] (the system Darcy number (Wikidata Q16954269))" + }, + { + "q": "If the permeability is 8.24951 and the characteristic length is 0.634021, what is the Darcy number?", + "a": "20.52205739786059", + "valid": true, + "kind": "compute", + "expect": 20.52205739786059, + "dtype": "quantity", + "unit": "1", + "why": "Darcy number for [8.24951, 0.634021] (the system Darcy number (Wikidata Q16954269))" + }, + { + "q": "Could you work out the Darcy number given permeability = 0.318734, characteristic length = 9.56607?", + "a": "0.003483062433049944", + "valid": true, + "kind": "compute", + "expect": 0.003483062433049944, + "dtype": "quantity", + "unit": "1", + "why": "Darcy number for [0.318734, 9.566071] (the system Darcy number (Wikidata Q16954269))" + }, + { + "q": "I need the Darcy number for a permeability of 0.318734 and a characteristic length of 9.56607, please.", + "a": "0.003483062433049944", + "valid": true, + "kind": "compute", + "expect": 0.003483062433049944, + "dtype": "quantity", + "unit": "1", + "why": "Darcy number for [0.318734, 9.566071] (the system Darcy number (Wikidata Q16954269))" + }, + { + "q": "If the permeability is 0.671697 and the characteristic length is 14.2496, what is the Darcy number?", + "a": "0.0033080347362955827", + "valid": true, + "kind": "compute", + "expect": 0.0033080347362955827, + "dtype": "quantity", + "unit": "1", + "why": "Darcy number for [0.671697, 14.249569] (the system Darcy number (Wikidata Q16954269))" + }, + { + "q": "Could you work out the Darcy number given permeability = 0.671697, characteristic length = 14.2496?", + "a": "0.0033080347362955827", + "valid": true, + "kind": "compute", + "expect": 0.0033080347362955827, + "dtype": "quantity", + "unit": "1", + "why": "Darcy number for [0.671697, 14.249569] (the system Darcy number (Wikidata Q16954269))" + }, + { + "q": "I need the Darcy number for a permeability of 21.7931 and a characteristic length of 29.5106, please.", + "a": "0.025024373050555816", + "valid": true, + "kind": "compute", + "expect": 0.025024373050555816, + "dtype": "quantity", + "unit": "1", + "why": "Darcy number for [21.793126, 29.510609] (the system Darcy number (Wikidata Q16954269))" + }, + { + "q": "If the permeability is 21.7931 and the characteristic length is 29.5106, what is the Darcy number?", + "a": "0.025024373050555816", + "valid": true, + "kind": "compute", + "expect": 0.025024373050555816, + "dtype": "quantity", + "unit": "1", + "why": "Darcy number for [21.793126, 29.510609] (the system Darcy number (Wikidata Q16954269))" + }, + { + "q": "Could you work out the Darcy number given permeability = 153.15, characteristic length = 18.5953?", + "a": "0.44290420248180523", + "valid": true, + "kind": "compute", + "expect": 0.44290420248180523, + "dtype": "quantity", + "unit": "1", + "why": "Darcy number for [153.149965, 18.595315] (the system Darcy number (Wikidata Q16954269))" + }, + { + "q": "I need the Darcy number for a permeability of 153.15 and a characteristic length of 18.5953, please.", + "a": "0.44290420248180523", + "valid": true, + "kind": "compute", + "expect": 0.44290420248180523, + "dtype": "quantity", + "unit": "1", + "why": "Darcy number for [153.149965, 18.595315] (the system Darcy number (Wikidata Q16954269))" + }, + { + "q": "If the permeability is 8.50728, what is the Darcy number?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the characteristic length is not given" + }, + { + "q": "Could you work out the Darcy number given permeability = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the characteristic length is not given" + }, + { + "q": "Do you know the Darcy number of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the Darcy number of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the Darcy number given permeability = -0.318734, characteristic length = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative permeability: no example had one" + }, + { + "q": "What is a typical Darcy number?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-darcy-number/program.py b/root/organs10/physics/specialists/solved-darcy-number/program.py new file mode 100644 index 0000000000000000000000000000000000000000..22410b30c3c490f58536a0d3a2372549df03b5ad --- /dev/null +++ b/root/organs10/physics/specialists/solved-darcy-number/program.py @@ -0,0 +1,7 @@ +"""Darcy number from permeability, characteristic length (Darcy number (Wikidata Q16954269)) +The maths organ (systems.py): solved from the system q0 = ((q1)/(q2 **(2))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(permeability, characteristic_length): + return 1.0*permeability/characteristic_length**2 diff --git a/root/organs10/physics/specialists/solved-darcy-number/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-darcy-number/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-darcy-number/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-data-transfer-rate/organ_formula_v2.py b/root/organs10/physics/specialists/solved-data-transfer-rate/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-data-transfer-rate/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-data-transfer-rate/panel.json b/root/organs10/physics/specialists/solved-data-transfer-rate/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..90c4d37216df48d427c9f1e30d871fac79c9ea21 --- /dev/null +++ b/root/organs10/physics/specialists/solved-data-transfer-rate/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the data transfer rate for period of data elements 107.961?", + "a": "0.00926263275385168", + "valid": true, + "kind": "compute", + "expect": 0.00926263275385168, + "dtype": "quantity", + "unit": "Hz", + "why": "data transfer rate for [107.960666] (the system period of data elements (Wikidata Q106268500))" + }, + { + "q": "Tell me the data transfer rate with period of data elements of 107.961.", + "a": "0.00926263275385168", + "valid": true, + "kind": "compute", + "expect": 0.00926263275385168, + "dtype": "quantity", + "unit": "Hz", + "why": "data transfer rate for [107.960666] (the system period of data elements (Wikidata Q106268500))" + }, + { + "q": "What is the data transfer rate for period of data elements 22.652?", + "a": "0.044146167826513254", + "valid": true, + "kind": "compute", + "expect": 0.044146167826513254, + "dtype": "quantity", + "unit": "Hz", + "why": "data transfer rate for [22.652023] (the system period of data elements (Wikidata Q106268500))" + }, + { + "q": "Tell me the data transfer rate with period of data elements of 22.652.", + "a": "0.044146167826513254", + "valid": true, + "kind": "compute", + "expect": 0.044146167826513254, + "dtype": "quantity", + "unit": "Hz", + "why": "data transfer rate for [22.652023] (the system period of data elements (Wikidata Q106268500))" + }, + { + "q": "What is the data transfer rate for period of data elements 173.394?", + "a": "0.0057672242718762815", + "valid": true, + "kind": "compute", + "expect": 0.0057672242718762815, + "dtype": "quantity", + "unit": "Hz", + "why": "data transfer rate for [173.393638] (the system period of data elements (Wikidata Q106268500))" + }, + { + "q": "Tell me the data transfer rate with period of data elements of 173.394.", + "a": "0.0057672242718762815", + "valid": true, + "kind": "compute", + "expect": 0.0057672242718762815, + "dtype": "quantity", + "unit": "Hz", + "why": "data transfer rate for [173.393638] (the system period of data elements (Wikidata Q106268500))" + }, + { + "q": "What is the data transfer rate for period of data elements 126.709?", + "a": "0.007892083536070259", + "valid": true, + "kind": "compute", + "expect": 0.007892083536070259, + "dtype": "quantity", + "unit": "Hz", + "why": "data transfer rate for [126.709252] (the system period of data elements (Wikidata Q106268500))" + }, + { + "q": "Tell me the data transfer rate with period of data elements of 126.709.", + "a": "0.007892083536070259", + "valid": true, + "kind": "compute", + "expect": 0.007892083536070259, + "dtype": "quantity", + "unit": "Hz", + "why": "data transfer rate for [126.709252] (the system period of data elements (Wikidata Q106268500))" + }, + { + "q": "What is the data transfer rate for period of data elements 0.671697?", + "a": "1.4887660694527203", + "valid": true, + "kind": "compute", + "expect": 1.4887660694527203, + "dtype": "quantity", + "unit": "Hz", + "why": "data transfer rate for [0.671697] (the system period of data elements (Wikidata Q106268500))" + }, + { + "q": "Tell me the data transfer rate with period of data elements of 0.671697.", + "a": "1.4887660694527203", + "valid": true, + "kind": "compute", + "expect": 1.4887660694527203, + "dtype": "quantity", + "unit": "Hz", + "why": "data transfer rate for [0.671697] (the system period of data elements (Wikidata Q106268500))" + }, + { + "q": "What is the data transfer rate for period of data elements 1.16997?", + "a": "0.8547242274066585", + "valid": true, + "kind": "compute", + "expect": 0.8547242274066585, + "dtype": "quantity", + "unit": "Hz", + "why": "data transfer rate for [1.169968] (the system period of data elements (Wikidata Q106268500))" + }, + { + "q": "Tell me the data transfer rate with period of data elements of 1.16997.", + "a": "0.8547242274066585", + "valid": true, + "kind": "compute", + "expect": 0.8547242274066585, + "dtype": "quantity", + "unit": "Hz", + "why": "data transfer rate for [1.169968] (the system period of data elements (Wikidata Q106268500))" + }, + { + "q": "What is the data transfer rate?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the period of data elements is not given" + }, + { + "q": "Could you work out the data transfer rate, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the period of data elements is not given" + }, + { + "q": "What is the data transfer rate of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the data transfer rate of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the data transfer rate with period of data elements of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative period of data elements: no example had one" + }, + { + "q": "Which data transfer rate is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the period of data elements is 2.57003, what is the data transfer rate?", + "a": "0.38910052946214563", + "valid": true, + "kind": "compute", + "expect": 0.38910052946214563, + "dtype": "quantity", + "unit": "Hz", + "why": "data transfer rate for [2.57003] (the system period of data elements (Wikidata Q106268500))" + }, + { + "q": "Could you work out the data transfer rate given period of data elements = 2.57003?", + "a": "0.38910052946214563", + "valid": true, + "kind": "compute", + "expect": 0.38910052946214563, + "dtype": "quantity", + "unit": "Hz", + "why": "data transfer rate for [2.57003] (the system period of data elements (Wikidata Q106268500))" + }, + { + "q": "I need the data transfer rate for a period of data elements of 48.921, please.", + "a": "0.02044112317474344", + "valid": true, + "kind": "compute", + "expect": 0.02044112317474344, + "dtype": "quantity", + "unit": "Hz", + "why": "data transfer rate for [48.920991] (the system period of data elements (Wikidata Q106268500))" + }, + { + "q": "If the period of data elements is 48.921, what is the data transfer rate?", + "a": "0.02044112317474344", + "valid": true, + "kind": "compute", + "expect": 0.02044112317474344, + "dtype": "quantity", + "unit": "Hz", + "why": "data transfer rate for [48.920991] (the system period of data elements (Wikidata Q106268500))" + }, + { + "q": "Could you work out the data transfer rate given period of data elements = 0.348438?", + "a": "2.869953759769417", + "valid": true, + "kind": "compute", + "expect": 2.869953759769417, + "dtype": "quantity", + "unit": "Hz", + "why": "data transfer rate for [0.348438] (the system period of data elements (Wikidata Q106268500))" + }, + { + "q": "I need the data transfer rate for a period of data elements of 0.348438, please.", + "a": "2.869953759769417", + "valid": true, + "kind": "compute", + "expect": 2.869953759769417, + "dtype": "quantity", + "unit": "Hz", + "why": "data transfer rate for [0.348438] (the system period of data elements (Wikidata Q106268500))" + }, + { + "q": "If the period of data elements is 252.709, what is the data transfer rate?", + "a": "0.0039571202610184176", + "valid": true, + "kind": "compute", + "expect": 0.0039571202610184176, + "dtype": "quantity", + "unit": "Hz", + "why": "data transfer rate for [252.709024] (the system period of data elements (Wikidata Q106268500))" + }, + { + "q": "Could you work out the data transfer rate given period of data elements = 252.709?", + "a": "0.0039571202610184176", + "valid": true, + "kind": "compute", + "expect": 0.0039571202610184176, + "dtype": "quantity", + "unit": "Hz", + "why": "data transfer rate for [252.709024] (the system period of data elements (Wikidata Q106268500))" + }, + { + "q": "I need the data transfer rate for a period of data elements of 20.3966, please.", + "a": "0.04902788018766951", + "valid": true, + "kind": "compute", + "expect": 0.04902788018766951, + "dtype": "quantity", + "unit": "Hz", + "why": "data transfer rate for [20.396558] (the system period of data elements (Wikidata Q106268500))" + }, + { + "q": "If the period of data elements is 20.3966, what is the data transfer rate?", + "a": "0.04902788018766951", + "valid": true, + "kind": "compute", + "expect": 0.04902788018766951, + "dtype": "quantity", + "unit": "Hz", + "why": "data transfer rate for [20.396558] (the system period of data elements (Wikidata Q106268500))" + }, + { + "q": "Could you work out the data transfer rate given period of data elements = 134.993?", + "a": "0.007407793842889246", + "valid": true, + "kind": "compute", + "expect": 0.007407793842889246, + "dtype": "quantity", + "unit": "Hz", + "why": "data transfer rate for [134.992958] (the system period of data elements (Wikidata Q106268500))" + }, + { + "q": "I need the data transfer rate for a period of data elements of 134.993, please.", + "a": "0.007407793842889246", + "valid": true, + "kind": "compute", + "expect": 0.007407793842889246, + "dtype": "quantity", + "unit": "Hz", + "why": "data transfer rate for [134.992958] (the system period of data elements (Wikidata Q106268500))" + }, + { + "q": "I need the data transfer rate, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the period of data elements is not given" + }, + { + "q": "Can you tell me the data transfer rate?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the period of data elements is not given" + }, + { + "q": "Do you know the data transfer rate of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the data transfer rate of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the data transfer rate given period of data elements = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative period of data elements: no example had one" + }, + { + "q": "What is a typical data transfer rate?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-data-transfer-rate/program.py b/root/organs10/physics/specialists/solved-data-transfer-rate/program.py new file mode 100644 index 0000000000000000000000000000000000000000..8a225a6381f06ef027295b8e1ee97d098ed71095 --- /dev/null +++ b/root/organs10/physics/specialists/solved-data-transfer-rate/program.py @@ -0,0 +1,7 @@ +"""data transfer rate from period of data elements (period of data elements (Wikidata Q106268500)) +The maths organ (systems.py): solved from the system q0 = ((1)/(q1)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(period_of_data_elements): + return 1.0/period_of_data_elements diff --git a/root/organs10/physics/specialists/solved-data-transfer-rate/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-data-transfer-rate/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-data-transfer-rate/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-deborah-number/organ_formula_v2.py b/root/organs10/physics/specialists/solved-deborah-number/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-deborah-number/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-deborah-number/panel.json b/root/organs10/physics/specialists/solved-deborah-number/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..eaab35e09613fda22b60674a6fda03add592fbbf --- /dev/null +++ b/root/organs10/physics/specialists/solved-deborah-number/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the Deborah number for stress relaxation time 107.961 and observation duration 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "Deborah number for [107.960666, 59.4105] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "Tell me the Deborah number with stress relaxation time of 107.961 and observation duration of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "Deborah number for [107.960666, 59.4105] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "What is the Deborah number for stress relaxation time 169.527 and observation duration 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "Deborah number for [169.526958, 280.773702] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "Tell me the Deborah number with stress relaxation time of 169.527 and observation duration of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "Deborah number for [169.526958, 280.773702] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "What is the Deborah number for stress relaxation time 1.91195 and observation duration 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "Deborah number for [1.911952, 82.240072] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "Tell me the Deborah number with stress relaxation time of 1.91195 and observation duration of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "Deborah number for [1.911952, 82.240072] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "What is the Deborah number for stress relaxation time 1.64379 and observation duration 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "Deborah number for [1.643795, 252.709024] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "Tell me the Deborah number with stress relaxation time of 1.64379 and observation duration of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "Deborah number for [1.643795, 252.709024] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "What is the Deborah number for stress relaxation time 18.3169 and observation duration 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "Deborah number for [18.316867, 6.838598] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "Tell me the Deborah number with stress relaxation time of 18.3169 and observation duration of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "Deborah number for [18.316867, 6.838598] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "What is the Deborah number for stress relaxation time 156.502 and observation duration 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "Deborah number for [156.50181, 185.886273] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "Tell me the Deborah number with stress relaxation time of 156.502 and observation duration of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "Deborah number for [156.50181, 185.886273] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "What is the Deborah number for stress relaxation time 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the observation duration is not given" + }, + { + "q": "Tell me the Deborah number with stress relaxation time of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the observation duration is not given" + }, + { + "q": "What is the Deborah number of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the Deborah number of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the Deborah number with stress relaxation time of -1.91195 and observation duration of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative stress relaxation time: no example had one" + }, + { + "q": "Which Deborah number is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the stress relaxation time is 8.50728 and the observation duration is 17.7888, what is the Deborah number?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "Deborah number for [8.507283, 17.788778] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "Could you work out the Deborah number given stress relaxation time = 8.50728, observation duration = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "Deborah number for [8.507283, 17.788778] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "I need the Deborah number for a stress relaxation time of 8.24951 and an observation duration of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "Deborah number for [8.24951, 0.634021] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "If the stress relaxation time is 8.24951 and the observation duration is 0.634021, what is the Deborah number?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "Deborah number for [8.24951, 0.634021] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "Could you work out the Deborah number given stress relaxation time = 0.318734, observation duration = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "Deborah number for [0.318734, 9.566071] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "I need the Deborah number for a stress relaxation time of 0.318734 and an observation duration of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "Deborah number for [0.318734, 9.566071] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "If the stress relaxation time is 0.671697 and the observation duration is 14.2496, what is the Deborah number?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "Deborah number for [0.671697, 14.249569] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "Could you work out the Deborah number given stress relaxation time = 0.671697, observation duration = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "Deborah number for [0.671697, 14.249569] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "I need the Deborah number for a stress relaxation time of 21.7931 and an observation duration of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "Deborah number for [21.793126, 29.510609] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "If the stress relaxation time is 21.7931 and the observation duration is 29.5106, what is the Deborah number?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "Deborah number for [21.793126, 29.510609] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "Could you work out the Deborah number given stress relaxation time = 153.15, observation duration = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "Deborah number for [153.149965, 18.595315] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "I need the Deborah number for a stress relaxation time of 153.15 and an observation duration of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "Deborah number for [153.149965, 18.595315] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "If the stress relaxation time is 8.50728, what is the Deborah number?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the observation duration is not given" + }, + { + "q": "Could you work out the Deborah number given stress relaxation time = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the observation duration is not given" + }, + { + "q": "Do you know the Deborah number of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the Deborah number of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the Deborah number given stress relaxation time = -0.318734, observation duration = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative stress relaxation time: no example had one" + }, + { + "q": "What is a typical Deborah number?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-deborah-number/program.py b/root/organs10/physics/specialists/solved-deborah-number/program.py new file mode 100644 index 0000000000000000000000000000000000000000..ce25d9ffeb57b7129dcce698d7144b4f59849a8c --- /dev/null +++ b/root/organs10/physics/specialists/solved-deborah-number/program.py @@ -0,0 +1,7 @@ +"""Deborah number from stress relaxation time, observation duration (Deborah number (Wikidata Q1138045)) +The maths organ (systems.py): solved from the system q2 = ((q0)/(q1)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(stress_relaxation_time, observation_duration): + return 1.0*stress_relaxation_time/observation_duration diff --git a/root/organs10/physics/specialists/solved-deborah-number/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-deborah-number/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-deborah-number/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-debye-angular-frequency/organ_formula_v2.py b/root/organs10/physics/specialists/solved-debye-angular-frequency/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-debye-angular-frequency/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-debye-angular-frequency/panel.json b/root/organs10/physics/specialists/solved-debye-angular-frequency/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..4829a19ca950ca8d278f4797e433d78af5ddc63c --- /dev/null +++ b/root/organs10/physics/specialists/solved-debye-angular-frequency/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the Debye angular frequency for Debye temperature 107.961?", + "a": "14135202048605.86", + "valid": true, + "kind": "compute", + "expect": 14135202048605.86, + "dtype": "quantity", + "unit": "Hz", + "why": "Debye angular frequency for [107.960666] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "Tell me the Debye angular frequency with Debye temperature of 107.961.", + "a": "14135202048605.86", + "valid": true, + "kind": "compute", + "expect": 14135202048605.86, + "dtype": "quantity", + "unit": "Hz", + "why": "Debye angular frequency for [107.960666] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "What is the Debye angular frequency for Debye temperature 22.652?", + "a": "2965810894215.255", + "valid": true, + "kind": "compute", + "expect": 2965810894215.255, + "dtype": "quantity", + "unit": "Hz", + "why": "Debye angular frequency for [22.652023] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "Tell me the Debye angular frequency with Debye temperature of 22.652.", + "a": "2965810894215.255", + "valid": true, + "kind": "compute", + "expect": 2965810894215.255, + "dtype": "quantity", + "unit": "Hz", + "why": "Debye angular frequency for [22.652023] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "What is the Debye angular frequency for Debye temperature 173.394?", + "a": "22702287843425.953", + "valid": true, + "kind": "compute", + "expect": 22702287843425.953, + "dtype": "quantity", + "unit": "Hz", + "why": "Debye angular frequency for [173.393638] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "Tell me the Debye angular frequency with Debye temperature of 173.394.", + "a": "22702287843425.953", + "valid": true, + "kind": "compute", + "expect": 22702287843425.953, + "dtype": "quantity", + "unit": "Hz", + "why": "Debye angular frequency for [173.393638] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "What is the Debye angular frequency for Debye temperature 126.709?", + "a": "16589939130690.219", + "valid": true, + "kind": "compute", + "expect": 16589939130690.219, + "dtype": "quantity", + "unit": "Hz", + "why": "Debye angular frequency for [126.709252] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "Tell me the Debye angular frequency with Debye temperature of 126.709.", + "a": "16589939130690.219", + "valid": true, + "kind": "compute", + "expect": 16589939130690.219, + "dtype": "quantity", + "unit": "Hz", + "why": "Debye angular frequency for [126.709252] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "What is the Debye angular frequency for Debye temperature 0.671697?", + "a": "87944767256.72449", + "valid": true, + "kind": "compute", + "expect": 87944767256.72449, + "dtype": "quantity", + "unit": "Hz", + "why": "Debye angular frequency for [0.671697] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "Tell me the Debye angular frequency with Debye temperature of 0.671697.", + "a": "87944767256.72449", + "valid": true, + "kind": "compute", + "expect": 87944767256.72449, + "dtype": "quantity", + "unit": "Hz", + "why": "Debye angular frequency for [0.671697] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "What is the Debye angular frequency for Debye temperature 1.16997?", + "a": "153182958057.69275", + "valid": true, + "kind": "compute", + "expect": 153182958057.69275, + "dtype": "quantity", + "unit": "Hz", + "why": "Debye angular frequency for [1.169968] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "Tell me the Debye angular frequency with Debye temperature of 1.16997.", + "a": "153182958057.69275", + "valid": true, + "kind": "compute", + "expect": 153182958057.69275, + "dtype": "quantity", + "unit": "Hz", + "why": "Debye angular frequency for [1.169968] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "What is the Debye angular frequency?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Debye temperature is not given" + }, + { + "q": "Could you work out the Debye angular frequency, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Debye temperature is not given" + }, + { + "q": "What is the Debye angular frequency of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the Debye angular frequency of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the Debye angular frequency with Debye temperature of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Debye temperature: no example had one" + }, + { + "q": "Which Debye angular frequency is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the Debye temperature is 2.57003, what is the Debye angular frequency?", + "a": "336491923202.24194", + "valid": true, + "kind": "compute", + "expect": 336491923202.24194, + "dtype": "quantity", + "unit": "Hz", + "why": "Debye angular frequency for [2.57003] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "Could you work out the Debye angular frequency given Debye temperature = 2.57003?", + "a": "336491923202.24194", + "valid": true, + "kind": "compute", + "expect": 336491923202.24194, + "dtype": "quantity", + "unit": "Hz", + "why": "Debye angular frequency for [2.57003] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "I need the Debye angular frequency for a Debye temperature of 48.921, please.", + "a": "6405185486064.726", + "valid": true, + "kind": "compute", + "expect": 6405185486064.726, + "dtype": "quantity", + "unit": "Hz", + "why": "Debye angular frequency for [48.920991] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "If the Debye temperature is 48.921, what is the Debye angular frequency?", + "a": "6405185486064.726", + "valid": true, + "kind": "compute", + "expect": 6405185486064.726, + "dtype": "quantity", + "unit": "Hz", + "why": "Debye angular frequency for [48.920991] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "Could you work out the Debye angular frequency given Debye temperature = 0.348438?", + "a": "45620660274.41757", + "valid": true, + "kind": "compute", + "expect": 45620660274.41757, + "dtype": "quantity", + "unit": "Hz", + "why": "Debye angular frequency for [0.348438] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "I need the Debye angular frequency for a Debye temperature of 0.348438, please.", + "a": "45620660274.41757", + "valid": true, + "kind": "compute", + "expect": 45620660274.41757, + "dtype": "quantity", + "unit": "Hz", + "why": "Debye angular frequency for [0.348438] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "If the Debye temperature is 252.709, what is the Debye angular frequency?", + "a": "33086986707862.05", + "valid": true, + "kind": "compute", + "expect": 33086986707862.05, + "dtype": "quantity", + "unit": "Hz", + "why": "Debye angular frequency for [252.709024] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "Could you work out the Debye angular frequency given Debye temperature = 252.709?", + "a": "33086986707862.05", + "valid": true, + "kind": "compute", + "expect": 33086986707862.05, + "dtype": "quantity", + "unit": "Hz", + "why": "Debye angular frequency for [252.709024] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "I need the Debye angular frequency for a Debye temperature of 20.3966, please.", + "a": "2670504720509.1406", + "valid": true, + "kind": "compute", + "expect": 2670504720509.1406, + "dtype": "quantity", + "unit": "Hz", + "why": "Debye angular frequency for [20.396558] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "If the Debye temperature is 20.3966, what is the Debye angular frequency?", + "a": "2670504720509.1406", + "valid": true, + "kind": "compute", + "expect": 2670504720509.1406, + "dtype": "quantity", + "unit": "Hz", + "why": "Debye angular frequency for [20.396558] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "Could you work out the Debye angular frequency given Debye temperature = 134.993?", + "a": "17674517981275.51", + "valid": true, + "kind": "compute", + "expect": 17674517981275.51, + "dtype": "quantity", + "unit": "Hz", + "why": "Debye angular frequency for [134.992958] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "I need the Debye angular frequency for a Debye temperature of 134.993, please.", + "a": "17674517981275.51", + "valid": true, + "kind": "compute", + "expect": 17674517981275.51, + "dtype": "quantity", + "unit": "Hz", + "why": "Debye angular frequency for [134.992958] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "I need the Debye angular frequency, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Debye temperature is not given" + }, + { + "q": "Can you tell me the Debye angular frequency?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Debye temperature is not given" + }, + { + "q": "Do you know the Debye angular frequency of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the Debye angular frequency of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the Debye angular frequency given Debye temperature = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Debye temperature: no example had one" + }, + { + "q": "What is a typical Debye angular frequency?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-debye-angular-frequency/program.py b/root/organs10/physics/specialists/solved-debye-angular-frequency/program.py new file mode 100644 index 0000000000000000000000000000000000000000..057a3780b6293af750888d982f68d5ff306afcbf --- /dev/null +++ b/root/organs10/physics/specialists/solved-debye-angular-frequency/program.py @@ -0,0 +1,7 @@ +"""Debye angular frequency from Debye temperature (Debye temperature (Wikidata Q3517821)) +The maths organ (systems.py): solved from the system q0 = ((q2*q1)/(q3)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(debye_temperature): + return 130929185477.728*debye_temperature diff --git a/root/organs10/physics/specialists/solved-debye-angular-frequency/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-debye-angular-frequency/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-debye-angular-frequency/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-debye-temperature/organ_formula_v2.py b/root/organs10/physics/specialists/solved-debye-temperature/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-debye-temperature/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-debye-temperature/panel.json b/root/organs10/physics/specialists/solved-debye-temperature/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..59543f47cf24378bfc1bf31415d10c104bbd5e7d --- /dev/null +++ b/root/organs10/physics/specialists/solved-debye-temperature/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the Debye temperature for Debye angular frequency 107.961?", + "a": "8.245729590086445e-10", + "valid": true, + "kind": "compute", + "expect": 8.245729590086445e-10, + "dtype": "quantity", + "unit": "K", + "why": "Debye temperature for [107.960666] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "Tell me the Debye temperature with Debye angular frequency of 107.961.", + "a": "8.245729590086445e-10", + "valid": true, + "kind": "compute", + "expect": 8.245729590086445e-10, + "dtype": "quantity", + "unit": "K", + "why": "Debye temperature for [107.960666] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "What is the Debye temperature for Debye angular frequency 22.652?", + "a": "1.7300972822983785e-10", + "valid": true, + "kind": "compute", + "expect": 1.7300972822983785e-10, + "dtype": "quantity", + "unit": "K", + "why": "Debye temperature for [22.652023] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "Tell me the Debye temperature with Debye angular frequency of 22.652.", + "a": "1.7300972822983785e-10", + "valid": true, + "kind": "compute", + "expect": 1.7300972822983785e-10, + "dtype": "quantity", + "unit": "K", + "why": "Debye temperature for [22.652023] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "What is the Debye temperature for Debye angular frequency 173.394?", + "a": "1.324331452705766e-09", + "valid": true, + "kind": "compute", + "expect": 1.324331452705766e-09, + "dtype": "quantity", + "unit": "K", + "why": "Debye temperature for [173.393638] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "Tell me the Debye temperature with Debye angular frequency of 173.394.", + "a": "1.324331452705766e-09", + "valid": true, + "kind": "compute", + "expect": 1.324331452705766e-09, + "dtype": "quantity", + "unit": "K", + "why": "Debye temperature for [173.393638] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "What is the Debye temperature for Debye angular frequency 126.709?", + "a": "9.677693429303148e-10", + "valid": true, + "kind": "compute", + "expect": 9.677693429303148e-10, + "dtype": "quantity", + "unit": "K", + "why": "Debye temperature for [126.709252] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "Tell me the Debye temperature with Debye angular frequency of 126.709.", + "a": "9.677693429303148e-10", + "valid": true, + "kind": "compute", + "expect": 9.677693429303148e-10, + "dtype": "quantity", + "unit": "K", + "why": "Debye temperature for [126.709252] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "What is the Debye temperature for Debye angular frequency 0.671697?", + "a": "5.130232784564698e-12", + "valid": true, + "kind": "compute", + "expect": 5.130232784564698e-12, + "dtype": "quantity", + "unit": "K", + "why": "Debye temperature for [0.671697] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "Tell me the Debye temperature with Debye angular frequency of 0.671697.", + "a": "5.130232784564698e-12", + "valid": true, + "kind": "compute", + "expect": 5.130232784564698e-12, + "dtype": "quantity", + "unit": "K", + "why": "Debye temperature for [0.671697] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "What is the Debye temperature for Debye angular frequency 1.16997?", + "a": "8.935883941452868e-12", + "valid": true, + "kind": "compute", + "expect": 8.935883941452868e-12, + "dtype": "quantity", + "unit": "K", + "why": "Debye temperature for [1.169968] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "Tell me the Debye temperature with Debye angular frequency of 1.16997.", + "a": "8.935883941452868e-12", + "valid": true, + "kind": "compute", + "expect": 8.935883941452868e-12, + "dtype": "quantity", + "unit": "K", + "why": "Debye temperature for [1.169968] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "What is the Debye temperature?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Debye angular frequency is not given" + }, + { + "q": "Could you work out the Debye temperature, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Debye angular frequency is not given" + }, + { + "q": "What is the Debye temperature of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the Debye temperature of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the Debye temperature with Debye angular frequency of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Debye angular frequency: no example had one" + }, + { + "q": "Which Debye temperature is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the Debye angular frequency is 2.57003, what is the Debye temperature?", + "a": "1.9629159869331194e-11", + "valid": true, + "kind": "compute", + "expect": 1.9629159869331194e-11, + "dtype": "quantity", + "unit": "K", + "why": "Debye temperature for [2.57003] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "Could you work out the Debye temperature given Debye angular frequency = 2.57003?", + "a": "1.9629159869331194e-11", + "valid": true, + "kind": "compute", + "expect": 1.9629159869331194e-11, + "dtype": "quantity", + "unit": "K", + "why": "Debye temperature for [2.57003] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "I need the Debye temperature for a Debye angular frequency of 48.921, please.", + "a": "3.7364465899265494e-10", + "valid": true, + "kind": "compute", + "expect": 3.7364465899265494e-10, + "dtype": "quantity", + "unit": "K", + "why": "Debye temperature for [48.920991] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "If the Debye angular frequency is 48.921, what is the Debye temperature?", + "a": "3.7364465899265494e-10", + "valid": true, + "kind": "compute", + "expect": 3.7364465899265494e-10, + "dtype": "quantity", + "unit": "K", + "why": "Debye temperature for [48.920991] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "Could you work out the Debye temperature given Debye angular frequency = 0.348438?", + "a": "2.6612681378767276e-12", + "valid": true, + "kind": "compute", + "expect": 2.6612681378767276e-12, + "dtype": "quantity", + "unit": "K", + "why": "Debye temperature for [0.348438] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "I need the Debye temperature for a Debye angular frequency of 0.348438, please.", + "a": "2.6612681378767276e-12", + "valid": true, + "kind": "compute", + "expect": 2.6612681378767276e-12, + "dtype": "quantity", + "unit": "K", + "why": "Debye temperature for [0.348438] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "If the Debye angular frequency is 252.709, what is the Debye temperature?", + "a": "1.930119883717712e-09", + "valid": true, + "kind": "compute", + "expect": 1.930119883717712e-09, + "dtype": "quantity", + "unit": "K", + "why": "Debye temperature for [252.709024] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "Could you work out the Debye temperature given Debye angular frequency = 252.709?", + "a": "1.930119883717712e-09", + "valid": true, + "kind": "compute", + "expect": 1.930119883717712e-09, + "dtype": "quantity", + "unit": "K", + "why": "Debye temperature for [252.709024] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "I need the Debye temperature for a Debye angular frequency of 20.3966, please.", + "a": "1.5578312724960015e-10", + "valid": true, + "kind": "compute", + "expect": 1.5578312724960015e-10, + "dtype": "quantity", + "unit": "K", + "why": "Debye temperature for [20.396558] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "If the Debye angular frequency is 20.3966, what is the Debye temperature?", + "a": "1.5578312724960015e-10", + "valid": true, + "kind": "compute", + "expect": 1.5578312724960015e-10, + "dtype": "quantity", + "unit": "K", + "why": "Debye temperature for [20.396558] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "Could you work out the Debye temperature given Debye angular frequency = 134.993?", + "a": "1.0310379392354888e-09", + "valid": true, + "kind": "compute", + "expect": 1.0310379392354888e-09, + "dtype": "quantity", + "unit": "K", + "why": "Debye temperature for [134.992958] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "I need the Debye temperature for a Debye angular frequency of 134.993, please.", + "a": "1.0310379392354888e-09", + "valid": true, + "kind": "compute", + "expect": 1.0310379392354888e-09, + "dtype": "quantity", + "unit": "K", + "why": "Debye temperature for [134.992958] (the system Debye temperature (Wikidata Q3517821))" + }, + { + "q": "I need the Debye temperature, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Debye angular frequency is not given" + }, + { + "q": "Can you tell me the Debye temperature?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Debye angular frequency is not given" + }, + { + "q": "Do you know the Debye temperature of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the Debye temperature of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the Debye temperature given Debye angular frequency = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Debye angular frequency: no example had one" + }, + { + "q": "What is a typical Debye temperature?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-debye-temperature/program.py b/root/organs10/physics/specialists/solved-debye-temperature/program.py new file mode 100644 index 0000000000000000000000000000000000000000..5c2f44b93473908ea93dfbb8a18a3aa85ca2d478 --- /dev/null +++ b/root/organs10/physics/specialists/solved-debye-temperature/program.py @@ -0,0 +1,7 @@ +"""Debye temperature from Debye angular frequency (Debye temperature (Wikidata Q3517821)) +The maths organ (systems.py): solved from the system q0 = ((q2*q1)/(q3)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(debye_angular_frequency): + return 7.63771649805387e-12*debye_angular_frequency diff --git a/root/organs10/physics/specialists/solved-debye-temperature/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-debye-temperature/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-debye-temperature/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-decadic-absorbance/organ_formula_v2.py b/root/organs10/physics/specialists/solved-decadic-absorbance/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-decadic-absorbance/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-decadic-absorbance/panel.json b/root/organs10/physics/specialists/solved-decadic-absorbance/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..11bb513245cf3abfef48fa1e61d81edbf7086e97 --- /dev/null +++ b/root/organs10/physics/specialists/solved-decadic-absorbance/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the decadic absorbance for linear absorption coefficient 107.961 and length 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "1", + "why": "decadic absorbance for [107.960666, 59.4105] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Tell me the decadic absorbance with linear absorption coefficient of 107.961 and length of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "1", + "why": "decadic absorbance for [107.960666, 59.4105] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "What is the decadic absorbance for linear absorption coefficient 169.527 and length 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "1", + "why": "decadic absorbance for [169.526958, 280.773702] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Tell me the decadic absorbance with linear absorption coefficient of 169.527 and length of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "1", + "why": "decadic absorbance for [169.526958, 280.773702] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "What is the decadic absorbance for linear absorption coefficient 1.91195 and length 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "1", + "why": "decadic absorbance for [1.911952, 82.240072] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Tell me the decadic absorbance with linear absorption coefficient of 1.91195 and length of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "1", + "why": "decadic absorbance for [1.911952, 82.240072] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "What is the decadic absorbance for linear absorption coefficient 1.64379 and length 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "1", + "why": "decadic absorbance for [1.643795, 252.709024] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Tell me the decadic absorbance with linear absorption coefficient of 1.64379 and length of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "1", + "why": "decadic absorbance for [1.643795, 252.709024] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "What is the decadic absorbance for linear absorption coefficient 18.3169 and length 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "1", + "why": "decadic absorbance for [18.316867, 6.838598] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Tell me the decadic absorbance with linear absorption coefficient of 18.3169 and length of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "1", + "why": "decadic absorbance for [18.316867, 6.838598] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "What is the decadic absorbance for linear absorption coefficient 156.502 and length 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "1", + "why": "decadic absorbance for [156.50181, 185.886273] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Tell me the decadic absorbance with linear absorption coefficient of 156.502 and length of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "1", + "why": "decadic absorbance for [156.50181, 185.886273] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "What is the decadic absorbance for linear absorption coefficient 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the length is not given" + }, + { + "q": "Tell me the decadic absorbance with linear absorption coefficient of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the length is not given" + }, + { + "q": "What is the decadic absorbance of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the decadic absorbance of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the decadic absorbance with linear absorption coefficient of -1.91195 and length of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative linear absorption coefficient: no example had one" + }, + { + "q": "Which decadic absorbance is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the linear absorption coefficient is 8.50728 and the length is 17.7888, what is the decadic absorbance?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "1", + "why": "decadic absorbance for [8.507283, 17.788778] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Could you work out the decadic absorbance given linear absorption coefficient = 8.50728, length = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "1", + "why": "decadic absorbance for [8.507283, 17.788778] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "I need the decadic absorbance for a linear absorption coefficient of 8.24951 and a length of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "1", + "why": "decadic absorbance for [8.24951, 0.634021] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "If the linear absorption coefficient is 8.24951 and the length is 0.634021, what is the decadic absorbance?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "1", + "why": "decadic absorbance for [8.24951, 0.634021] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Could you work out the decadic absorbance given linear absorption coefficient = 0.318734, length = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "1", + "why": "decadic absorbance for [0.318734, 9.566071] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "I need the decadic absorbance for a linear absorption coefficient of 0.318734 and a length of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "1", + "why": "decadic absorbance for [0.318734, 9.566071] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "If the linear absorption coefficient is 0.671697 and the length is 14.2496, what is the decadic absorbance?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "1", + "why": "decadic absorbance for [0.671697, 14.249569] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Could you work out the decadic absorbance given linear absorption coefficient = 0.671697, length = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "1", + "why": "decadic absorbance for [0.671697, 14.249569] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "I need the decadic absorbance for a linear absorption coefficient of 21.7931 and a length of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "1", + "why": "decadic absorbance for [21.793126, 29.510609] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "If the linear absorption coefficient is 21.7931 and the length is 29.5106, what is the decadic absorbance?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "1", + "why": "decadic absorbance for [21.793126, 29.510609] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Could you work out the decadic absorbance given linear absorption coefficient = 153.15, length = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "1", + "why": "decadic absorbance for [153.149965, 18.595315] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "I need the decadic absorbance for a linear absorption coefficient of 153.15 and a length of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "1", + "why": "decadic absorbance for [153.149965, 18.595315] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "If the linear absorption coefficient is 8.50728, what is the decadic absorbance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the length is not given" + }, + { + "q": "Could you work out the decadic absorbance given linear absorption coefficient = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the length is not given" + }, + { + "q": "Do you know the decadic absorbance of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the decadic absorbance of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the decadic absorbance given linear absorption coefficient = -0.318734, length = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative linear absorption coefficient: no example had one" + }, + { + "q": "What is a typical decadic absorbance?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-decadic-absorbance/program.py b/root/organs10/physics/specialists/solved-decadic-absorbance/program.py new file mode 100644 index 0000000000000000000000000000000000000000..469c87ed6c627f1c4981db0294a48d5d702c60af --- /dev/null +++ b/root/organs10/physics/specialists/solved-decadic-absorbance/program.py @@ -0,0 +1,7 @@ +"""decadic absorbance from linear absorption coefficient, length (linear absorption coefficient (Wikidata Q86204782)) +The maths organ (systems.py): solved from the system q0 = ((q1)/(q2)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(linear_absorption_coefficient, length): + return 1.0*length*linear_absorption_coefficient diff --git a/root/organs10/physics/specialists/solved-decadic-absorbance/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-decadic-absorbance/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-decadic-absorbance/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-decay-constant-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-decay-constant-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-decay-constant-2/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-decay-constant-2/panel.json b/root/organs10/physics/specialists/solved-decay-constant-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..960300e387b25e6cbd7507253aa3ff381c14677f --- /dev/null +++ b/root/organs10/physics/specialists/solved-decay-constant-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the decay constant for mean lifetime 107.961?", + "a": "0.00926263275385168", + "valid": true, + "kind": "compute", + "expect": 0.00926263275385168, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [107.960666] (the system mean lifetime (Wikidata Q1758559))" + }, + { + "q": "Tell me the decay constant with mean lifetime of 107.961.", + "a": "0.00926263275385168", + "valid": true, + "kind": "compute", + "expect": 0.00926263275385168, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [107.960666] (the system mean lifetime (Wikidata Q1758559))" + }, + { + "q": "What is the decay constant for mean lifetime 22.652?", + "a": "0.044146167826513254", + "valid": true, + "kind": "compute", + "expect": 0.044146167826513254, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [22.652023] (the system mean lifetime (Wikidata Q1758559))" + }, + { + "q": "Tell me the decay constant with mean lifetime of 22.652.", + "a": "0.044146167826513254", + "valid": true, + "kind": "compute", + "expect": 0.044146167826513254, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [22.652023] (the system mean lifetime (Wikidata Q1758559))" + }, + { + "q": "What is the decay constant for mean lifetime 173.394?", + "a": "0.0057672242718762815", + "valid": true, + "kind": "compute", + "expect": 0.0057672242718762815, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [173.393638] (the system mean lifetime (Wikidata Q1758559))" + }, + { + "q": "Tell me the decay constant with mean lifetime of 173.394.", + "a": "0.0057672242718762815", + "valid": true, + "kind": "compute", + "expect": 0.0057672242718762815, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [173.393638] (the system mean lifetime (Wikidata Q1758559))" + }, + { + "q": "What is the decay constant for mean lifetime 126.709?", + "a": "0.007892083536070259", + "valid": true, + "kind": "compute", + "expect": 0.007892083536070259, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [126.709252] (the system mean lifetime (Wikidata Q1758559))" + }, + { + "q": "Tell me the decay constant with mean lifetime of 126.709.", + "a": "0.007892083536070259", + "valid": true, + "kind": "compute", + "expect": 0.007892083536070259, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [126.709252] (the system mean lifetime (Wikidata Q1758559))" + }, + { + "q": "What is the decay constant for mean lifetime 0.671697?", + "a": "1.4887660694527203", + "valid": true, + "kind": "compute", + "expect": 1.4887660694527203, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [0.671697] (the system mean lifetime (Wikidata Q1758559))" + }, + { + "q": "Tell me the decay constant with mean lifetime of 0.671697.", + "a": "1.4887660694527203", + "valid": true, + "kind": "compute", + "expect": 1.4887660694527203, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [0.671697] (the system mean lifetime (Wikidata Q1758559))" + }, + { + "q": "What is the decay constant for mean lifetime 1.16997?", + "a": "0.8547242274066585", + "valid": true, + "kind": "compute", + "expect": 0.8547242274066585, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [1.169968] (the system mean lifetime (Wikidata Q1758559))" + }, + { + "q": "Tell me the decay constant with mean lifetime of 1.16997.", + "a": "0.8547242274066585", + "valid": true, + "kind": "compute", + "expect": 0.8547242274066585, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [1.169968] (the system mean lifetime (Wikidata Q1758559))" + }, + { + "q": "What is the decay constant?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mean lifetime is not given" + }, + { + "q": "Could you work out the decay constant, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mean lifetime is not given" + }, + { + "q": "What is the decay constant of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the decay constant of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the decay constant with mean lifetime of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mean lifetime: no example had one" + }, + { + "q": "Which decay constant is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the mean lifetime is 2.57003, what is the decay constant?", + "a": "0.38910052946214563", + "valid": true, + "kind": "compute", + "expect": 0.38910052946214563, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [2.57003] (the system mean lifetime (Wikidata Q1758559))" + }, + { + "q": "Could you work out the decay constant given mean lifetime = 2.57003?", + "a": "0.38910052946214563", + "valid": true, + "kind": "compute", + "expect": 0.38910052946214563, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [2.57003] (the system mean lifetime (Wikidata Q1758559))" + }, + { + "q": "I need the decay constant for a mean lifetime of 48.921, please.", + "a": "0.02044112317474344", + "valid": true, + "kind": "compute", + "expect": 0.02044112317474344, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [48.920991] (the system mean lifetime (Wikidata Q1758559))" + }, + { + "q": "If the mean lifetime is 48.921, what is the decay constant?", + "a": "0.02044112317474344", + "valid": true, + "kind": "compute", + "expect": 0.02044112317474344, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [48.920991] (the system mean lifetime (Wikidata Q1758559))" + }, + { + "q": "Could you work out the decay constant given mean lifetime = 0.348438?", + "a": "2.869953759769417", + "valid": true, + "kind": "compute", + "expect": 2.869953759769417, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [0.348438] (the system mean lifetime (Wikidata Q1758559))" + }, + { + "q": "I need the decay constant for a mean lifetime of 0.348438, please.", + "a": "2.869953759769417", + "valid": true, + "kind": "compute", + "expect": 2.869953759769417, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [0.348438] (the system mean lifetime (Wikidata Q1758559))" + }, + { + "q": "If the mean lifetime is 252.709, what is the decay constant?", + "a": "0.0039571202610184176", + "valid": true, + "kind": "compute", + "expect": 0.0039571202610184176, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [252.709024] (the system mean lifetime (Wikidata Q1758559))" + }, + { + "q": "Could you work out the decay constant given mean lifetime = 252.709?", + "a": "0.0039571202610184176", + "valid": true, + "kind": "compute", + "expect": 0.0039571202610184176, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [252.709024] (the system mean lifetime (Wikidata Q1758559))" + }, + { + "q": "I need the decay constant for a mean lifetime of 20.3966, please.", + "a": "0.04902788018766951", + "valid": true, + "kind": "compute", + "expect": 0.04902788018766951, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [20.396558] (the system mean lifetime (Wikidata Q1758559))" + }, + { + "q": "If the mean lifetime is 20.3966, what is the decay constant?", + "a": "0.04902788018766951", + "valid": true, + "kind": "compute", + "expect": 0.04902788018766951, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [20.396558] (the system mean lifetime (Wikidata Q1758559))" + }, + { + "q": "Could you work out the decay constant given mean lifetime = 134.993?", + "a": "0.007407793842889246", + "valid": true, + "kind": "compute", + "expect": 0.007407793842889246, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [134.992958] (the system mean lifetime (Wikidata Q1758559))" + }, + { + "q": "I need the decay constant for a mean lifetime of 134.993, please.", + "a": "0.007407793842889246", + "valid": true, + "kind": "compute", + "expect": 0.007407793842889246, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [134.992958] (the system mean lifetime (Wikidata Q1758559))" + }, + { + "q": "I need the decay constant, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mean lifetime is not given" + }, + { + "q": "Can you tell me the decay constant?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mean lifetime is not given" + }, + { + "q": "Do you know the decay constant of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the decay constant of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the decay constant given mean lifetime = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mean lifetime: no example had one" + }, + { + "q": "What is a typical decay constant?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-decay-constant-2/program.py b/root/organs10/physics/specialists/solved-decay-constant-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..a3c08507a50d5b9b9abf1479fa0a532a6af78b77 --- /dev/null +++ b/root/organs10/physics/specialists/solved-decay-constant-2/program.py @@ -0,0 +1,7 @@ +"""decay constant from mean lifetime (mean lifetime (Wikidata Q1758559)) +The maths organ (systems.py): solved from the system q0 = ((1)/(q1)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(mean_lifetime): + return 1.0/mean_lifetime diff --git a/root/organs10/physics/specialists/solved-decay-constant-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-decay-constant-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-decay-constant-2/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-decay-constant/organ_formula_v2.py b/root/organs10/physics/specialists/solved-decay-constant/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-decay-constant/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-decay-constant/panel.json b/root/organs10/physics/specialists/solved-decay-constant/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..a0ee3887eef1a0bbaddf124d76b15f35e761f7a5 --- /dev/null +++ b/root/organs10/physics/specialists/solved-decay-constant/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the decay constant for half life 107.961?", + "a": "0.006420367777894493", + "valid": true, + "kind": "compute", + "expect": 0.006420367777894493, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [107.960666] (the system half life (Wikidata Q98118544))" + }, + { + "q": "Tell me the decay constant with half life of 107.961.", + "a": "0.006420367777894493", + "valid": true, + "kind": "compute", + "expect": 0.006420367777894493, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [107.960666] (the system half life (Wikidata Q98118544))" + }, + { + "q": "What is the decay constant for half life 22.652?", + "a": "0.03059979176147383", + "valid": true, + "kind": "compute", + "expect": 0.03059979176147383, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [22.652023] (the system half life (Wikidata Q98118544))" + }, + { + "q": "Tell me the decay constant with half life of 22.652.", + "a": "0.03059979176147383", + "valid": true, + "kind": "compute", + "expect": 0.03059979176147383, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [22.652023] (the system half life (Wikidata Q98118544))" + }, + { + "q": "What is the decay constant for half life 173.394?", + "a": "0.003997535243707928", + "valid": true, + "kind": "compute", + "expect": 0.003997535243707928, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [173.393638] (the system half life (Wikidata Q98118544))" + }, + { + "q": "Tell me the decay constant with half life of 173.394.", + "a": "0.003997535243707928", + "valid": true, + "kind": "compute", + "expect": 0.003997535243707928, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [173.393638] (the system half life (Wikidata Q98118544))" + }, + { + "q": "What is the decay constant for half life 126.709?", + "a": "0.005470375451770663", + "valid": true, + "kind": "compute", + "expect": 0.005470375451770663, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [126.709252] (the system half life (Wikidata Q98118544))" + }, + { + "q": "Tell me the decay constant with half life of 126.709.", + "a": "0.005470375451770663", + "valid": true, + "kind": "compute", + "expect": 0.005470375451770663, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [126.709252] (the system half life (Wikidata Q98118544))" + }, + { + "q": "What is the decay constant for half life 0.671697?", + "a": "1.0319340035544646", + "valid": true, + "kind": "compute", + "expect": 1.0319340035544646, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [0.671697] (the system half life (Wikidata Q98118544))" + }, + { + "q": "Tell me the decay constant with half life of 0.671697.", + "a": "1.0319340035544646", + "valid": true, + "kind": "compute", + "expect": 1.0319340035544646, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [0.671697] (the system half life (Wikidata Q98118544))" + }, + { + "q": "What is the decay constant for half life 1.16997?", + "a": "0.5924496883832029", + "valid": true, + "kind": "compute", + "expect": 0.5924496883832029, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [1.169968] (the system half life (Wikidata Q98118544))" + }, + { + "q": "Tell me the decay constant with half life of 1.16997.", + "a": "0.5924496883832029", + "valid": true, + "kind": "compute", + "expect": 0.5924496883832029, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [1.169968] (the system half life (Wikidata Q98118544))" + }, + { + "q": "What is the decay constant?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the half life is not given" + }, + { + "q": "Could you work out the decay constant, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the half life is not given" + }, + { + "q": "What is the decay constant of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the decay constant of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the decay constant with half life of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative half life: no example had one" + }, + { + "q": "Which decay constant is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the half life is 2.57003, what is the decay constant?", + "a": "0.26970393495106815", + "valid": true, + "kind": "compute", + "expect": 0.26970393495106815, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [2.57003] (the system half life (Wikidata Q98118544))" + }, + { + "q": "Could you work out the decay constant given half life = 2.57003?", + "a": "0.26970393495106815", + "valid": true, + "kind": "compute", + "expect": 0.26970393495106815, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [2.57003] (the system half life (Wikidata Q98118544))" + }, + { + "q": "I need the decay constant for a half life of 48.921, please.", + "a": "0.014168706896051975", + "valid": true, + "kind": "compute", + "expect": 0.014168706896051975, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [48.920991] (the system half life (Wikidata Q98118544))" + }, + { + "q": "If the half life is 48.921, what is the decay constant?", + "a": "0.014168706896051975", + "valid": true, + "kind": "compute", + "expect": 0.014168706896051975, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [48.920991] (the system half life (Wikidata Q98118544))" + }, + { + "q": "Could you work out the decay constant given half life = 0.348438?", + "a": "1.989300356921586", + "valid": true, + "kind": "compute", + "expect": 1.989300356921586, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [0.348438] (the system half life (Wikidata Q98118544))" + }, + { + "q": "I need the decay constant for a half life of 0.348438, please.", + "a": "1.989300356921586", + "valid": true, + "kind": "compute", + "expect": 1.989300356921586, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [0.348438] (the system half life (Wikidata Q98118544))" + }, + { + "q": "If the half life is 252.709, what is the decay constant?", + "a": "0.002742866752061551", + "valid": true, + "kind": "compute", + "expect": 0.002742866752061551, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [252.709024] (the system half life (Wikidata Q98118544))" + }, + { + "q": "Could you work out the decay constant given half life = 252.709?", + "a": "0.002742866752061551", + "valid": true, + "kind": "compute", + "expect": 0.002742866752061551, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [252.709024] (the system half life (Wikidata Q98118544))" + }, + { + "q": "I need the decay constant for a half life of 20.3966, please.", + "a": "0.03398353692091392", + "valid": true, + "kind": "compute", + "expect": 0.03398353692091392, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [20.396558] (the system half life (Wikidata Q98118544))" + }, + { + "q": "If the half life is 20.3966, what is the decay constant?", + "a": "0.03398353692091392", + "valid": true, + "kind": "compute", + "expect": 0.03398353692091392, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [20.396558] (the system half life (Wikidata Q98118544))" + }, + { + "q": "Could you work out the decay constant given half life = 134.993?", + "a": "0.005134691416368003", + "valid": true, + "kind": "compute", + "expect": 0.005134691416368003, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [134.992958] (the system half life (Wikidata Q98118544))" + }, + { + "q": "I need the decay constant for a half life of 134.993, please.", + "a": "0.005134691416368003", + "valid": true, + "kind": "compute", + "expect": 0.005134691416368003, + "dtype": "quantity", + "unit": "Hz", + "why": "decay constant for [134.992958] (the system half life (Wikidata Q98118544))" + }, + { + "q": "I need the decay constant, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the half life is not given" + }, + { + "q": "Can you tell me the decay constant?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the half life is not given" + }, + { + "q": "Do you know the decay constant of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the decay constant of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the decay constant given half life = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative half life: no example had one" + }, + { + "q": "What is a typical decay constant?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-decay-constant/program.py b/root/organs10/physics/specialists/solved-decay-constant/program.py new file mode 100644 index 0000000000000000000000000000000000000000..27aaae8dacec1c042b8cb5c90065a0ab8fa91312 --- /dev/null +++ b/root/organs10/physics/specialists/solved-decay-constant/program.py @@ -0,0 +1,7 @@ +"""decay constant from half life (half life (Wikidata Q98118544)) +The maths organ (systems.py): solved from the system q0 = ((log (2))/(q1)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(half_life): + return 0.693147180559945/half_life diff --git a/root/organs10/physics/specialists/solved-decay-constant/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-decay-constant/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-decay-constant/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-degree-of-dissociation/organ_formula_v2.py b/root/organs10/physics/specialists/solved-degree-of-dissociation/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-degree-of-dissociation/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-degree-of-dissociation/panel.json b/root/organs10/physics/specialists/solved-degree-of-dissociation/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..f2979fe17f9b67a701207dca58161fd9fc49a92e --- /dev/null +++ b/root/organs10/physics/specialists/solved-degree-of-dissociation/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the degree of dissociation for number of dissociated molecules 107.961 and number of molecules 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "degree of dissociation for [107.960666, 59.4105] (the system degree of dissociation (Wikidata Q907334))" + }, + { + "q": "Tell me the degree of dissociation with number of dissociated molecules of 107.961 and number of molecules of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "degree of dissociation for [107.960666, 59.4105] (the system degree of dissociation (Wikidata Q907334))" + }, + { + "q": "What is the degree of dissociation for number of dissociated molecules 169.527 and number of molecules 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "degree of dissociation for [169.526958, 280.773702] (the system degree of dissociation (Wikidata Q907334))" + }, + { + "q": "Tell me the degree of dissociation with number of dissociated molecules of 169.527 and number of molecules of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "degree of dissociation for [169.526958, 280.773702] (the system degree of dissociation (Wikidata Q907334))" + }, + { + "q": "What is the degree of dissociation for number of dissociated molecules 1.91195 and number of molecules 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "degree of dissociation for [1.911952, 82.240072] (the system degree of dissociation (Wikidata Q907334))" + }, + { + "q": "Tell me the degree of dissociation with number of dissociated molecules of 1.91195 and number of molecules of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "degree of dissociation for [1.911952, 82.240072] (the system degree of dissociation (Wikidata Q907334))" + }, + { + "q": "What is the degree of dissociation for number of dissociated molecules 1.64379 and number of molecules 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "degree of dissociation for [1.643795, 252.709024] (the system degree of dissociation (Wikidata Q907334))" + }, + { + "q": "Tell me the degree of dissociation with number of dissociated molecules of 1.64379 and number of molecules of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "degree of dissociation for [1.643795, 252.709024] (the system degree of dissociation (Wikidata Q907334))" + }, + { + "q": "What is the degree of dissociation for number of dissociated molecules 18.3169 and number of molecules 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "degree of dissociation for [18.316867, 6.838598] (the system degree of dissociation (Wikidata Q907334))" + }, + { + "q": "Tell me the degree of dissociation with number of dissociated molecules of 18.3169 and number of molecules of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "degree of dissociation for [18.316867, 6.838598] (the system degree of dissociation (Wikidata Q907334))" + }, + { + "q": "What is the degree of dissociation for number of dissociated molecules 156.502 and number of molecules 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "degree of dissociation for [156.50181, 185.886273] (the system degree of dissociation (Wikidata Q907334))" + }, + { + "q": "Tell me the degree of dissociation with number of dissociated molecules of 156.502 and number of molecules of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "degree of dissociation for [156.50181, 185.886273] (the system degree of dissociation (Wikidata Q907334))" + }, + { + "q": "What is the degree of dissociation for number of dissociated molecules 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the number of molecules is not given" + }, + { + "q": "Tell me the degree of dissociation with number of dissociated molecules of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the number of molecules is not given" + }, + { + "q": "What is the degree of dissociation of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the degree of dissociation of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the degree of dissociation with number of dissociated molecules of -1.91195 and number of molecules of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative number of dissociated molecules: no example had one" + }, + { + "q": "Which degree of dissociation is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the number of dissociated molecules is 8.50728 and the number of molecules is 17.7888, what is the degree of dissociation?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "degree of dissociation for [8.507283, 17.788778] (the system degree of dissociation (Wikidata Q907334))" + }, + { + "q": "Could you work out the degree of dissociation given number of dissociated molecules = 8.50728, number of molecules = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "degree of dissociation for [8.507283, 17.788778] (the system degree of dissociation (Wikidata Q907334))" + }, + { + "q": "I need the degree of dissociation for a number of dissociated molecules of 8.24951 and a number of molecules of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "degree of dissociation for [8.24951, 0.634021] (the system degree of dissociation (Wikidata Q907334))" + }, + { + "q": "If the number of dissociated molecules is 8.24951 and the number of molecules is 0.634021, what is the degree of dissociation?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "degree of dissociation for [8.24951, 0.634021] (the system degree of dissociation (Wikidata Q907334))" + }, + { + "q": "Could you work out the degree of dissociation given number of dissociated molecules = 0.318734, number of molecules = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "degree of dissociation for [0.318734, 9.566071] (the system degree of dissociation (Wikidata Q907334))" + }, + { + "q": "I need the degree of dissociation for a number of dissociated molecules of 0.318734 and a number of molecules of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "degree of dissociation for [0.318734, 9.566071] (the system degree of dissociation (Wikidata Q907334))" + }, + { + "q": "If the number of dissociated molecules is 0.671697 and the number of molecules is 14.2496, what is the degree of dissociation?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "degree of dissociation for [0.671697, 14.249569] (the system degree of dissociation (Wikidata Q907334))" + }, + { + "q": "Could you work out the degree of dissociation given number of dissociated molecules = 0.671697, number of molecules = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "degree of dissociation for [0.671697, 14.249569] (the system degree of dissociation (Wikidata Q907334))" + }, + { + "q": "I need the degree of dissociation for a number of dissociated molecules of 21.7931 and a number of molecules of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "degree of dissociation for [21.793126, 29.510609] (the system degree of dissociation (Wikidata Q907334))" + }, + { + "q": "If the number of dissociated molecules is 21.7931 and the number of molecules is 29.5106, what is the degree of dissociation?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "degree of dissociation for [21.793126, 29.510609] (the system degree of dissociation (Wikidata Q907334))" + }, + { + "q": "Could you work out the degree of dissociation given number of dissociated molecules = 153.15, number of molecules = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "degree of dissociation for [153.149965, 18.595315] (the system degree of dissociation (Wikidata Q907334))" + }, + { + "q": "I need the degree of dissociation for a number of dissociated molecules of 153.15 and a number of molecules of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "degree of dissociation for [153.149965, 18.595315] (the system degree of dissociation (Wikidata Q907334))" + }, + { + "q": "If the number of dissociated molecules is 8.50728, what is the degree of dissociation?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the number of molecules is not given" + }, + { + "q": "Could you work out the degree of dissociation given number of dissociated molecules = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the number of molecules is not given" + }, + { + "q": "Do you know the degree of dissociation of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the degree of dissociation of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the degree of dissociation given number of dissociated molecules = -0.318734, number of molecules = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative number of dissociated molecules: no example had one" + }, + { + "q": "What is a typical degree of dissociation?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-degree-of-dissociation/program.py b/root/organs10/physics/specialists/solved-degree-of-dissociation/program.py new file mode 100644 index 0000000000000000000000000000000000000000..32464884bbf2a0ab977edd1494c6069ca8dcb5ea --- /dev/null +++ b/root/organs10/physics/specialists/solved-degree-of-dissociation/program.py @@ -0,0 +1,7 @@ +"""degree of dissociation from number of dissociated molecules, number of molecules (degree of dissociation (Wikidata Q907334)) +The maths organ (systems.py): solved from the system q1 = ((q0)/(q2)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(number_of_dissociated_molecules, number_of_molecules): + return 1.0*number_of_dissociated_molecules/number_of_molecules diff --git a/root/organs10/physics/specialists/solved-degree-of-dissociation/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-degree-of-dissociation/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-degree-of-dissociation/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-degrees-of-freedom/organ_formula_v2.py b/root/organs10/physics/specialists/solved-degrees-of-freedom/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-degrees-of-freedom/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-degrees-of-freedom/panel.json b/root/organs10/physics/specialists/solved-degrees-of-freedom/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..fbcaf993dc11b3aa95e6ac8ddf7e8c8a85465153 --- /dev/null +++ b/root/organs10/physics/specialists/solved-degrees-of-freedom/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the degrees of freedom for number of components of a thermodynamic system 107.961 and number of phases 59.4105?", + "a": "50.55016569879068", + "valid": true, + "kind": "compute", + "expect": 50.55016569879068, + "dtype": "quantity", + "unit": "1", + "why": "degrees of freedom for [107.960666, 59.4105] (the system Gibbs' phase rule (Wikidata Q752412))" + }, + { + "q": "Tell me the degrees of freedom with number of components of a thermodynamic system of 107.961 and number of phases of 59.4105.", + "a": "50.55016569879068", + "valid": true, + "kind": "compute", + "expect": 50.55016569879068, + "dtype": "quantity", + "unit": "1", + "why": "degrees of freedom for [107.960666, 59.4105] (the system Gibbs' phase rule (Wikidata Q752412))" + }, + { + "q": "What is the degrees of freedom for number of components of a thermodynamic system 157.223 and number of phases 35.6412?", + "a": "123.5814030054458", + "valid": true, + "kind": "compute", + "expect": 123.5814030054458, + "dtype": "quantity", + "unit": "1", + "why": "degrees of freedom for [157.22256, 35.641157] (the system Gibbs' phase rule (Wikidata Q752412))" + }, + { + "q": "Tell me the degrees of freedom with number of components of a thermodynamic system of 157.223 and number of phases of 35.6412.", + "a": "123.5814030054458", + "valid": true, + "kind": "compute", + "expect": 123.5814030054458, + "dtype": "quantity", + "unit": "1", + "why": "degrees of freedom for [157.22256, 35.641157] (the system Gibbs' phase rule (Wikidata Q752412))" + }, + { + "q": "What is the degrees of freedom for number of components of a thermodynamic system 126.709 and number of phases 1.70503?", + "a": "127.00421942160824", + "valid": true, + "kind": "compute", + "expect": 127.00421942160824, + "dtype": "quantity", + "unit": "1", + "why": "degrees of freedom for [126.709252, 1.705032] (the system Gibbs' phase rule (Wikidata Q752412))" + }, + { + "q": "Tell me the degrees of freedom with number of components of a thermodynamic system of 126.709 and number of phases of 1.70503.", + "a": "127.00421942160824", + "valid": true, + "kind": "compute", + "expect": 127.00421942160824, + "dtype": "quantity", + "unit": "1", + "why": "degrees of freedom for [126.709252, 1.705032] (the system Gibbs' phase rule (Wikidata Q752412))" + }, + { + "q": "What is the degrees of freedom for number of components of a thermodynamic system 41.6566 and number of phases 13.8779?", + "a": "29.77877186905885", + "valid": true, + "kind": "compute", + "expect": 29.77877186905885, + "dtype": "quantity", + "unit": "1", + "why": "degrees of freedom for [41.656639, 13.877867] (the system Gibbs' phase rule (Wikidata Q752412))" + }, + { + "q": "Tell me the degrees of freedom with number of components of a thermodynamic system of 41.6566 and number of phases of 13.8779.", + "a": "29.77877186905885", + "valid": true, + "kind": "compute", + "expect": 29.77877186905885, + "dtype": "quantity", + "unit": "1", + "why": "degrees of freedom for [41.656639, 13.877867] (the system Gibbs' phase rule (Wikidata Q752412))" + }, + { + "q": "What is the degrees of freedom for number of components of a thermodynamic system 8.50341 and number of phases 0.58813?", + "a": "9.91527752848457", + "valid": true, + "kind": "compute", + "expect": 9.91527752848457, + "dtype": "quantity", + "unit": "1", + "why": "degrees of freedom for [8.503407, 0.58813] (the system Gibbs' phase rule (Wikidata Q752412))" + }, + { + "q": "Tell me the degrees of freedom with number of components of a thermodynamic system of 8.50341 and number of phases of 0.58813.", + "a": "9.91527752848457", + "valid": true, + "kind": "compute", + "expect": 9.91527752848457, + "dtype": "quantity", + "unit": "1", + "why": "degrees of freedom for [8.503407, 0.58813] (the system Gibbs' phase rule (Wikidata Q752412))" + }, + { + "q": "What is the degrees of freedom for number of components of a thermodynamic system 223.509 and number of phases 17.3414?", + "a": "208.1673613150397", + "valid": true, + "kind": "compute", + "expect": 208.1673613150397, + "dtype": "quantity", + "unit": "1", + "why": "degrees of freedom for [223.508796, 17.341435] (the system Gibbs' phase rule (Wikidata Q752412))" + }, + { + "q": "Tell me the degrees of freedom with number of components of a thermodynamic system of 223.509 and number of phases of 17.3414.", + "a": "208.1673613150397", + "valid": true, + "kind": "compute", + "expect": 208.1673613150397, + "dtype": "quantity", + "unit": "1", + "why": "degrees of freedom for [223.508796, 17.341435] (the system Gibbs' phase rule (Wikidata Q752412))" + }, + { + "q": "What is the degrees of freedom for number of components of a thermodynamic system 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the number of phases is not given" + }, + { + "q": "Tell me the degrees of freedom with number of components of a thermodynamic system of 157.223.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the number of phases is not given" + }, + { + "q": "What is the degrees of freedom of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the degrees of freedom of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the degrees of freedom with number of components of a thermodynamic system of -126.709 and number of phases of 1.70503.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative number of components of a thermodynamic system: no example had one" + }, + { + "q": "Which degrees of freedom is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the number of components of a thermodynamic system is 167.625 and the number of phases is 10.3291, what is the degrees of freedom?", + "a": "159.29589332353083", + "valid": true, + "kind": "compute", + "expect": 159.29589332353083, + "dtype": "quantity", + "unit": "1", + "why": "degrees of freedom for [167.624948, 10.329054] (the system Gibbs' phase rule (Wikidata Q752412))" + }, + { + "q": "Could you work out the degrees of freedom given number of components of a thermodynamic system = 167.625, number of phases = 10.3291?", + "a": "159.29589332353083", + "valid": true, + "kind": "compute", + "expect": 159.29589332353083, + "dtype": "quantity", + "unit": "1", + "why": "degrees of freedom for [167.624948, 10.329054] (the system Gibbs' phase rule (Wikidata Q752412))" + }, + { + "q": "I need the degrees of freedom for a number of components of a thermodynamic system of 13.999 and a number of phases of 0.348438, please.", + "a": "15.650591275493905", + "valid": true, + "kind": "compute", + "expect": 15.650591275493905, + "dtype": "quantity", + "unit": "1", + "why": "degrees of freedom for [13.999029, 0.348438] (the system Gibbs' phase rule (Wikidata Q752412))" + }, + { + "q": "If the number of components of a thermodynamic system is 13.999 and the number of phases is 0.348438, what is the degrees of freedom?", + "a": "15.650591275493905", + "valid": true, + "kind": "compute", + "expect": 15.650591275493905, + "dtype": "quantity", + "unit": "1", + "why": "degrees of freedom for [13.999029, 0.348438] (the system Gibbs' phase rule (Wikidata Q752412))" + }, + { + "q": "Could you work out the degrees of freedom given number of components of a thermodynamic system = 81.1967, number of phases = 6.98086?", + "a": "76.21584883614992", + "valid": true, + "kind": "compute", + "expect": 76.21584883614992, + "dtype": "quantity", + "unit": "1", + "why": "degrees of freedom for [81.196705, 6.980857] (the system Gibbs' phase rule (Wikidata Q752412))" + }, + { + "q": "I need the degrees of freedom for a number of components of a thermodynamic system of 81.1967 and a number of phases of 6.98086, please.", + "a": "76.21584883614992", + "valid": true, + "kind": "compute", + "expect": 76.21584883614992, + "dtype": "quantity", + "unit": "1", + "why": "degrees of freedom for [81.196705, 6.980857] (the system Gibbs' phase rule (Wikidata Q752412))" + }, + { + "q": "If the number of components of a thermodynamic system is 19.4474 and the number of phases is 4.51548, what is the degrees of freedom?", + "a": "16.931881955429077", + "valid": true, + "kind": "compute", + "expect": 16.931881955429077, + "dtype": "quantity", + "unit": "1", + "why": "degrees of freedom for [19.447357, 4.515475] (the system Gibbs' phase rule (Wikidata Q752412))" + }, + { + "q": "Could you work out the degrees of freedom given number of components of a thermodynamic system = 19.4474, number of phases = 4.51548?", + "a": "16.931881955429077", + "valid": true, + "kind": "compute", + "expect": 16.931881955429077, + "dtype": "quantity", + "unit": "1", + "why": "degrees of freedom for [19.447357, 4.515475] (the system Gibbs' phase rule (Wikidata Q752412))" + }, + { + "q": "I need the degrees of freedom for a number of components of a thermodynamic system of 13.2373 and a number of phases of 4.71941, please.", + "a": "10.517896269006052", + "valid": true, + "kind": "compute", + "expect": 10.517896269006052, + "dtype": "quantity", + "unit": "1", + "why": "degrees of freedom for [13.237311, 4.719415] (the system Gibbs' phase rule (Wikidata Q752412))" + }, + { + "q": "If the number of components of a thermodynamic system is 13.2373 and the number of phases is 4.71941, what is the degrees of freedom?", + "a": "10.517896269006052", + "valid": true, + "kind": "compute", + "expect": 10.517896269006052, + "dtype": "quantity", + "unit": "1", + "why": "degrees of freedom for [13.237311, 4.719415] (the system Gibbs' phase rule (Wikidata Q752412))" + }, + { + "q": "Could you work out the degrees of freedom given number of components of a thermodynamic system = 21.7946, number of phases = 9.1061?", + "a": "14.68851669739933", + "valid": true, + "kind": "compute", + "expect": 14.68851669739933, + "dtype": "quantity", + "unit": "1", + "why": "degrees of freedom for [21.794621, 9.106104] (the system Gibbs' phase rule (Wikidata Q752412))" + }, + { + "q": "I need the degrees of freedom for a number of components of a thermodynamic system of 21.7946 and a number of phases of 9.1061, please.", + "a": "14.68851669739933", + "valid": true, + "kind": "compute", + "expect": 14.68851669739933, + "dtype": "quantity", + "unit": "1", + "why": "degrees of freedom for [21.794621, 9.106104] (the system Gibbs' phase rule (Wikidata Q752412))" + }, + { + "q": "If the number of components of a thermodynamic system is 167.625, what is the degrees of freedom?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the number of phases is not given" + }, + { + "q": "Could you work out the degrees of freedom given number of components of a thermodynamic system = 13.999?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the number of phases is not given" + }, + { + "q": "Do you know the degrees of freedom of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the degrees of freedom of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the degrees of freedom given number of components of a thermodynamic system = -81.1967, number of phases = 6.98086?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative number of components of a thermodynamic system: no example had one" + }, + { + "q": "What is a typical degrees of freedom?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-degrees-of-freedom/program.py b/root/organs10/physics/specialists/solved-degrees-of-freedom/program.py new file mode 100644 index 0000000000000000000000000000000000000000..177aa6e395be5fdffdcec36eb5fef6c5b7dd3c4b --- /dev/null +++ b/root/organs10/physics/specialists/solved-degrees-of-freedom/program.py @@ -0,0 +1,7 @@ +"""degrees of freedom from number of components of a thermodynamic system, number of phases (Gibbs' phase rule (Wikidata Q752412)) +The maths organ (systems.py): solved from the system q0 = q1 - q2 + 2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(number_of_components_of_a_thermodynamic_system, number_of_phases): + return 1.0*number_of_components_of_a_thermodynamic_system - 1.0*number_of_phases + 2.0 diff --git a/root/organs10/physics/specialists/solved-degrees-of-freedom/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-degrees-of-freedom/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-degrees-of-freedom/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-density-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-density-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-density-2/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-density-2/panel.json b/root/organs10/physics/specialists/solved-density-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..c62a98630e12c39cc92ab76a0aafd3fda34fe346 --- /dev/null +++ b/root/organs10/physics/specialists/solved-density-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the density for water pressure 107.961 and depth 59.4105?", + "a": "0.18530266706666676", + "valid": true, + "kind": "compute", + "expect": 0.18530266706666676, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [107.960666, 59.4105] (the system Hydrostatic pressure)" + }, + { + "q": "Tell me the density with water pressure of 107.961 and depth of 59.4105.", + "a": "0.18530266706666676", + "valid": true, + "kind": "compute", + "expect": 0.18530266706666676, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [107.960666, 59.4105] (the system Hydrostatic pressure)" + }, + { + "q": "What is the density for water pressure 169.527 and depth 280.774?", + "a": "0.06156893823894976", + "valid": true, + "kind": "compute", + "expect": 0.06156893823894976, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [169.526958, 280.773702] (the system Hydrostatic pressure)" + }, + { + "q": "Tell me the density with water pressure of 169.527 and depth of 280.774.", + "a": "0.06156893823894976", + "valid": true, + "kind": "compute", + "expect": 0.06156893823894976, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [169.526958, 280.773702] (the system Hydrostatic pressure)" + }, + { + "q": "What is the density for water pressure 1.91195 and depth 82.2401?", + "a": "0.0023706791196374337", + "valid": true, + "kind": "compute", + "expect": 0.0023706791196374337, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [1.911952, 82.240072] (the system Hydrostatic pressure)" + }, + { + "q": "Tell me the density with water pressure of 1.91195 and depth of 82.2401.", + "a": "0.0023706791196374337", + "valid": true, + "kind": "compute", + "expect": 0.0023706791196374337, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [1.911952, 82.240072] (the system Hydrostatic pressure)" + }, + { + "q": "What is the density for water pressure 1.64379 and depth 252.709?", + "a": "0.000663294443840899", + "valid": true, + "kind": "compute", + "expect": 0.000663294443840899, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [1.643795, 252.709024] (the system Hydrostatic pressure)" + }, + { + "q": "Tell me the density with water pressure of 1.64379 and depth of 252.709.", + "a": "0.000663294443840899", + "valid": true, + "kind": "compute", + "expect": 0.000663294443840899, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [1.643795, 252.709024] (the system Hydrostatic pressure)" + }, + { + "q": "What is the density for water pressure 18.3169 and depth 6.8386?", + "a": "0.2731262476069092", + "valid": true, + "kind": "compute", + "expect": 0.2731262476069092, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [18.316867, 6.838598] (the system Hydrostatic pressure)" + }, + { + "q": "Tell me the density with water pressure of 18.3169 and depth of 6.8386.", + "a": "0.2731262476069092", + "valid": true, + "kind": "compute", + "expect": 0.2731262476069092, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [18.316867, 6.838598] (the system Hydrostatic pressure)" + }, + { + "q": "What is the density for water pressure 156.502 and depth 185.886?", + "a": "0.08585218822035659", + "valid": true, + "kind": "compute", + "expect": 0.08585218822035659, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [156.50181, 185.886273] (the system Hydrostatic pressure)" + }, + { + "q": "Tell me the density with water pressure of 156.502 and depth of 185.886.", + "a": "0.08585218822035659", + "valid": true, + "kind": "compute", + "expect": 0.08585218822035659, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [156.50181, 185.886273] (the system Hydrostatic pressure)" + }, + { + "q": "What is the density for water pressure 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the depth is not given" + }, + { + "q": "Tell me the density with water pressure of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the depth is not given" + }, + { + "q": "What is the density of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the density of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the density with water pressure of -1.91195 and depth of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative water pressure: no example had one" + }, + { + "q": "Which density is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the water pressure is 8.50728 and the depth is 17.7888, what is the density?", + "a": "0.04876677863157268", + "valid": true, + "kind": "compute", + "expect": 0.04876677863157268, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [8.507283, 17.788778] (the system Hydrostatic pressure)" + }, + { + "q": "Could you work out the density given water pressure = 8.50728, depth = 17.7888?", + "a": "0.04876677863157268", + "valid": true, + "kind": "compute", + "expect": 0.04876677863157268, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [8.507283, 17.788778] (the system Hydrostatic pressure)" + }, + { + "q": "I need the density for a water pressure of 8.24951 and a depth of 0.634021, please.", + "a": "1.3267950621862565", + "valid": true, + "kind": "compute", + "expect": 1.3267950621862565, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [8.24951, 0.634021] (the system Hydrostatic pressure)" + }, + { + "q": "If the water pressure is 8.24951 and the depth is 0.634021, what is the density?", + "a": "1.3267950621862565", + "valid": true, + "kind": "compute", + "expect": 1.3267950621862565, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [8.24951, 0.634021] (the system Hydrostatic pressure)" + }, + { + "q": "Could you work out the density given water pressure = 0.318734, depth = 9.56607?", + "a": "0.003397615137949426", + "valid": true, + "kind": "compute", + "expect": 0.003397615137949426, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [0.318734, 9.566071] (the system Hydrostatic pressure)" + }, + { + "q": "I need the density for a water pressure of 0.318734 and a depth of 9.56607, please.", + "a": "0.003397615137949426", + "valid": true, + "kind": "compute", + "expect": 0.003397615137949426, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [0.318734, 9.566071] (the system Hydrostatic pressure)" + }, + { + "q": "If the water pressure is 0.671697 and the depth is 14.2496, what is the density?", + "a": "0.004806745450727051", + "valid": true, + "kind": "compute", + "expect": 0.004806745450727051, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [0.671697, 14.249569] (the system Hydrostatic pressure)" + }, + { + "q": "Could you work out the density given water pressure = 0.671697, depth = 14.2496?", + "a": "0.004806745450727051", + "valid": true, + "kind": "compute", + "expect": 0.004806745450727051, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [0.671697, 14.249569] (the system Hydrostatic pressure)" + }, + { + "q": "I need the density for a water pressure of 21.7931 and a depth of 29.5106, please.", + "a": "0.0753044597343837", + "valid": true, + "kind": "compute", + "expect": 0.0753044597343837, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [21.793126, 29.510609] (the system Hydrostatic pressure)" + }, + { + "q": "If the water pressure is 21.7931 and the depth is 29.5106, what is the density?", + "a": "0.0753044597343837", + "valid": true, + "kind": "compute", + "expect": 0.0753044597343837, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [21.793126, 29.510609] (the system Hydrostatic pressure)" + }, + { + "q": "Could you work out the density given water pressure = 153.15, depth = 18.5953?", + "a": "0.8398324982026809", + "valid": true, + "kind": "compute", + "expect": 0.8398324982026809, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [153.149965, 18.595315] (the system Hydrostatic pressure)" + }, + { + "q": "I need the density for a water pressure of 153.15 and a depth of 18.5953, please.", + "a": "0.8398324982026809", + "valid": true, + "kind": "compute", + "expect": 0.8398324982026809, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [153.149965, 18.595315] (the system Hydrostatic pressure)" + }, + { + "q": "If the water pressure is 8.50728, what is the density?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the depth is not given" + }, + { + "q": "Could you work out the density given water pressure = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the depth is not given" + }, + { + "q": "Do you know the density of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the density of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the density given water pressure = -0.318734, depth = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative water pressure: no example had one" + }, + { + "q": "What is a typical density?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-density-2/program.py b/root/organs10/physics/specialists/solved-density-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..cb50b258c6f68ca61ef9ae3bc0c70887902e55a0 --- /dev/null +++ b/root/organs10/physics/specialists/solved-density-2/program.py @@ -0,0 +1,7 @@ +"""density from water pressure, depth (Hydrostatic pressure) +The maths organ (systems.py): solved from the system p = r*g*h; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(water_pressure, depth): + return 0.101971621297793*water_pressure/depth diff --git a/root/organs10/physics/specialists/solved-density-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-density-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-density-2/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-density-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-density-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-density-3/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-density-3/panel.json b/root/organs10/physics/specialists/solved-density-3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..8024fe641061fd69af62b98c3801f395fa816929 --- /dev/null +++ b/root/organs10/physics/specialists/solved-density-3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the density for mass flow 107.961 and flow 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [107.960666, 59.4105] (the system Mass flow)" + }, + { + "q": "Tell me the density with mass flow of 107.961 and flow of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [107.960666, 59.4105] (the system Mass flow)" + }, + { + "q": "What is the density for mass flow 169.527 and flow 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [169.526958, 280.773702] (the system Mass flow)" + }, + { + "q": "Tell me the density with mass flow of 169.527 and flow of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [169.526958, 280.773702] (the system Mass flow)" + }, + { + "q": "What is the density for mass flow 1.91195 and flow 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [1.911952, 82.240072] (the system Mass flow)" + }, + { + "q": "Tell me the density with mass flow of 1.91195 and flow of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [1.911952, 82.240072] (the system Mass flow)" + }, + { + "q": "What is the density for mass flow 1.64379 and flow 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [1.643795, 252.709024] (the system Mass flow)" + }, + { + "q": "Tell me the density with mass flow of 1.64379 and flow of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [1.643795, 252.709024] (the system Mass flow)" + }, + { + "q": "What is the density for mass flow 18.3169 and flow 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [18.316867, 6.838598] (the system Mass flow)" + }, + { + "q": "Tell me the density with mass flow of 18.3169 and flow of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [18.316867, 6.838598] (the system Mass flow)" + }, + { + "q": "What is the density for mass flow 156.502 and flow 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [156.50181, 185.886273] (the system Mass flow)" + }, + { + "q": "Tell me the density with mass flow of 156.502 and flow of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [156.50181, 185.886273] (the system Mass flow)" + }, + { + "q": "What is the density for mass flow 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the flow is not given" + }, + { + "q": "Tell me the density with mass flow of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the flow is not given" + }, + { + "q": "What is the density of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the density of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the density with mass flow of -1.91195 and flow of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mass flow: no example had one" + }, + { + "q": "Which density is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the mass flow is 8.50728 and the flow is 17.7888, what is the density?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [8.507283, 17.788778] (the system Mass flow)" + }, + { + "q": "Could you work out the density given mass flow = 8.50728, flow = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [8.507283, 17.788778] (the system Mass flow)" + }, + { + "q": "I need the density for a mass flow of 8.24951 and a flow of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [8.24951, 0.634021] (the system Mass flow)" + }, + { + "q": "If the mass flow is 8.24951 and the flow is 0.634021, what is the density?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [8.24951, 0.634021] (the system Mass flow)" + }, + { + "q": "Could you work out the density given mass flow = 0.318734, flow = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [0.318734, 9.566071] (the system Mass flow)" + }, + { + "q": "I need the density for a mass flow of 0.318734 and a flow of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [0.318734, 9.566071] (the system Mass flow)" + }, + { + "q": "If the mass flow is 0.671697 and the flow is 14.2496, what is the density?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [0.671697, 14.249569] (the system Mass flow)" + }, + { + "q": "Could you work out the density given mass flow = 0.671697, flow = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [0.671697, 14.249569] (the system Mass flow)" + }, + { + "q": "I need the density for a mass flow of 21.7931 and a flow of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [21.793126, 29.510609] (the system Mass flow)" + }, + { + "q": "If the mass flow is 21.7931 and the flow is 29.5106, what is the density?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [21.793126, 29.510609] (the system Mass flow)" + }, + { + "q": "Could you work out the density given mass flow = 153.15, flow = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [153.149965, 18.595315] (the system Mass flow)" + }, + { + "q": "I need the density for a mass flow of 153.15 and a flow of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [153.149965, 18.595315] (the system Mass flow)" + }, + { + "q": "If the mass flow is 8.50728, what is the density?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the flow is not given" + }, + { + "q": "Could you work out the density given mass flow = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the flow is not given" + }, + { + "q": "Do you know the density of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the density of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the density given mass flow = -0.318734, flow = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mass flow: no example had one" + }, + { + "q": "What is a typical density?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-density-3/program.py b/root/organs10/physics/specialists/solved-density-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..4a57f8a1ef9d2ebe88bc6b432b4f06bd08baa5f4 --- /dev/null +++ b/root/organs10/physics/specialists/solved-density-3/program.py @@ -0,0 +1,7 @@ +"""density from mass flow, flow (Mass flow) +The maths organ (systems.py): solved from the system m = r*Q; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(mass_flow, flow): + return 1.0*mass_flow/flow diff --git a/root/organs10/physics/specialists/solved-density-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-density-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-density-3/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-density-of-heavier-fluid/organ_formula_v2.py b/root/organs10/physics/specialists/solved-density-of-heavier-fluid/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-density-of-heavier-fluid/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-density-of-heavier-fluid/panel.json b/root/organs10/physics/specialists/solved-density-of-heavier-fluid/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..50f832fb764d66176d871313fd9e41fd0f3a5924 --- /dev/null +++ b/root/organs10/physics/specialists/solved-density-of-heavier-fluid/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the density of heavier fluid for density of lighter fluid 8.24951 and Atwood number 0.634021?", + "a": "36.83236234927191", + "valid": true, + "kind": "compute", + "expect": 36.83236234927191, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of heavier fluid for [8.24951, 0.634021] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "Tell me the density of heavier fluid with density of lighter fluid of 8.24951 and Atwood number of 0.634021.", + "a": "36.83236234927191", + "valid": true, + "kind": "compute", + "expect": 36.83236234927191, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of heavier fluid for [8.24951, 0.634021] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "What is the density of heavier fluid for density of lighter fluid 8.50341 and Atwood number 0.58813?", + "a": "32.788263573384626", + "valid": true, + "kind": "compute", + "expect": 32.788263573384626, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of heavier fluid for [8.503407, 0.58813] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "Tell me the density of heavier fluid with density of lighter fluid of 8.50341 and Atwood number of 0.58813.", + "a": "32.788263573384626", + "valid": true, + "kind": "compute", + "expect": 32.788263573384626, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of heavier fluid for [8.503407, 0.58813] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "What is the density of heavier fluid for density of lighter fluid 88.6624 and Atwood number 0.633611?", + "a": "395.3167347412008", + "valid": true, + "kind": "compute", + "expect": 395.3167347412008, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of heavier fluid for [88.662361, 0.633611] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "Tell me the density of heavier fluid with density of lighter fluid of 88.6624 and Atwood number of 0.633611.", + "a": "395.3167347412008", + "valid": true, + "kind": "compute", + "expect": 395.3167347412008, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of heavier fluid for [88.662361, 0.633611] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "What is the density of heavier fluid for density of lighter fluid 0.406608 and Atwood number 0.367206?", + "a": "0.878511625075887", + "valid": true, + "kind": "compute", + "expect": 0.878511625075887, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of heavier fluid for [0.406608, 0.367206] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "Tell me the density of heavier fluid with density of lighter fluid of 0.406608 and Atwood number of 0.367206.", + "a": "0.878511625075887", + "valid": true, + "kind": "compute", + "expect": 0.878511625075887, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of heavier fluid for [0.406608, 0.367206] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "What is the density of heavier fluid for density of lighter fluid 15.1985 and Atwood number 0.344595?", + "a": "31.180450556203894", + "valid": true, + "kind": "compute", + "expect": 31.180450556203894, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of heavier fluid for [15.198498, 0.344595] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "Tell me the density of heavier fluid with density of lighter fluid of 15.1985 and Atwood number of 0.344595.", + "a": "31.180450556203894", + "valid": true, + "kind": "compute", + "expect": 31.180450556203894, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of heavier fluid for [15.198498, 0.344595] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "What is the density of heavier fluid for density of lighter fluid 25.0765 and Atwood number 0.562861?", + "a": "89.65346247123823", + "valid": true, + "kind": "compute", + "expect": 89.65346247123823, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of heavier fluid for [25.076463, 0.562861] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "Tell me the density of heavier fluid with density of lighter fluid of 25.0765 and Atwood number of 0.562861.", + "a": "89.65346247123823", + "valid": true, + "kind": "compute", + "expect": 89.65346247123823, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of heavier fluid for [25.076463, 0.562861] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "What is the density of heavier fluid for density of lighter fluid 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Atwood number is not given" + }, + { + "q": "Tell me the density of heavier fluid with density of lighter fluid of 8.50341.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Atwood number is not given" + }, + { + "q": "What is the density of heavier fluid of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the density of heavier fluid of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the density of heavier fluid with density of lighter fluid of -88.6624 and Atwood number of 0.633611.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative density of lighter fluid: no example had one" + }, + { + "q": "Which density of heavier fluid is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the density of lighter fluid is 13.999 and the Atwood number is 0.348438, what is the density of heavier fluid?", + "a": "28.97162264909889", + "valid": true, + "kind": "compute", + "expect": 28.97162264909889, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of heavier fluid for [13.999029, 0.348438] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "Could you work out the density of heavier fluid given density of lighter fluid = 13.999, Atwood number = 0.348438?", + "a": "28.97162264909889", + "valid": true, + "kind": "compute", + "expect": 28.97162264909889, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of heavier fluid for [13.999029, 0.348438] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "I need the density of heavier fluid for a density of lighter fluid of 75.8534 and an Atwood number of 0.558623, please.", + "a": "267.8593934659792", + "valid": true, + "kind": "compute", + "expect": 267.8593934659792, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of heavier fluid for [75.853435, 0.558623] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "If the density of lighter fluid is 75.8534 and the Atwood number is 0.558623, what is the density of heavier fluid?", + "a": "267.8593934659792", + "valid": true, + "kind": "compute", + "expect": 267.8593934659792, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of heavier fluid for [75.853435, 0.558623] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "Could you work out the density of heavier fluid given density of lighter fluid = 34.8151, Atwood number = 0.380269?", + "a": "77.54029198284725", + "valid": true, + "kind": "compute", + "expect": 77.54029198284725, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of heavier fluid for [34.81508, 0.380269] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "I need the density of heavier fluid for a density of lighter fluid of 34.8151 and an Atwood number of 0.380269, please.", + "a": "77.54029198284725", + "valid": true, + "kind": "compute", + "expect": 77.54029198284725, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of heavier fluid for [34.81508, 0.380269] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "If the density of lighter fluid is 10.2544 and the Atwood number is 0.415084, what is the density of heavier fluid?", + "a": "24.808377582376487", + "valid": true, + "kind": "compute", + "expect": 24.808377582376487, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of heavier fluid for [10.254383, 0.415084] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "Could you work out the density of heavier fluid given density of lighter fluid = 10.2544, Atwood number = 0.415084?", + "a": "24.808377582376487", + "valid": true, + "kind": "compute", + "expect": 24.808377582376487, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of heavier fluid for [10.254383, 0.415084] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "I need the density of heavier fluid for a density of lighter fluid of 30.6611 and an Atwood number of 0.436857, please.", + "a": "78.23160935448136", + "valid": true, + "kind": "compute", + "expect": 78.23160935448136, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of heavier fluid for [30.661095, 0.436857] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "If the density of lighter fluid is 30.6611 and the Atwood number is 0.436857, what is the density of heavier fluid?", + "a": "78.23160935448136", + "valid": true, + "kind": "compute", + "expect": 78.23160935448136, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of heavier fluid for [30.661095, 0.436857] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "Could you work out the density of heavier fluid given density of lighter fluid = 44.8717, Atwood number = 0.32142?", + "a": "87.38001299456373", + "valid": true, + "kind": "compute", + "expect": 87.38001299456373, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of heavier fluid for [44.87169, 0.32142] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "I need the density of heavier fluid for a density of lighter fluid of 44.8717 and an Atwood number of 0.32142, please.", + "a": "87.38001299456373", + "valid": true, + "kind": "compute", + "expect": 87.38001299456373, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of heavier fluid for [44.87169, 0.32142] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "If the density of lighter fluid is 13.999, what is the density of heavier fluid?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Atwood number is not given" + }, + { + "q": "Could you work out the density of heavier fluid given density of lighter fluid = 75.8534?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Atwood number is not given" + }, + { + "q": "Do you know the density of heavier fluid of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the density of heavier fluid of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the density of heavier fluid given density of lighter fluid = -34.8151, Atwood number = 0.380269?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative density of lighter fluid: no example had one" + }, + { + "q": "What is a typical density of heavier fluid?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-density-of-heavier-fluid/program.py b/root/organs10/physics/specialists/solved-density-of-heavier-fluid/program.py new file mode 100644 index 0000000000000000000000000000000000000000..a7e2fdb423c60067bf49a27b5f9950598ca7d54d --- /dev/null +++ b/root/organs10/physics/specialists/solved-density-of-heavier-fluid/program.py @@ -0,0 +1,7 @@ +"""density of heavier fluid from density of lighter fluid, Atwood number (Atwood number (Wikidata Q2373823)) +The maths organ (systems.py): solved from the system q2 = ((q0 - q1)/(q0 + q1)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(density_of_lighter_fluid, atwood_number): + return 1.0*density_of_lighter_fluid*(-atwood_number - 1.0)/(atwood_number - 1.0) diff --git a/root/organs10/physics/specialists/solved-density-of-heavier-fluid/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-density-of-heavier-fluid/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-density-of-heavier-fluid/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-density-of-lighter-fluid/organ_formula_v2.py b/root/organs10/physics/specialists/solved-density-of-lighter-fluid/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-density-of-lighter-fluid/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-density-of-lighter-fluid/panel.json b/root/organs10/physics/specialists/solved-density-of-lighter-fluid/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..6b7368d702d0067270da7f75da095fa7187900f4 --- /dev/null +++ b/root/organs10/physics/specialists/solved-density-of-lighter-fluid/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the density of lighter fluid for density of heavier fluid 8.24951 and Atwood number 0.634021?", + "a": "1.8476798314407101", + "valid": true, + "kind": "compute", + "expect": 1.8476798314407101, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of lighter fluid for [8.24951, 0.634021] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "Tell me the density of lighter fluid with density of heavier fluid of 8.24951 and Atwood number of 0.634021.", + "a": "1.8476798314407101", + "valid": true, + "kind": "compute", + "expect": 1.8476798314407101, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of lighter fluid for [8.24951, 0.634021] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "What is the density of lighter fluid for density of heavier fluid 8.50341 and Atwood number 0.58813?", + "a": "2.2052992739477446", + "valid": true, + "kind": "compute", + "expect": 2.2052992739477446, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of lighter fluid for [8.503407, 0.58813] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "Tell me the density of lighter fluid with density of heavier fluid of 8.50341 and Atwood number of 0.58813.", + "a": "2.2052992739477446", + "valid": true, + "kind": "compute", + "expect": 2.2052992739477446, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of lighter fluid for [8.503407, 0.58813] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "What is the density of lighter fluid for density of heavier fluid 88.6624 and Atwood number 0.633611?", + "a": "19.885356425931736", + "valid": true, + "kind": "compute", + "expect": 19.885356425931736, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of lighter fluid for [88.662361, 0.633611] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "Tell me the density of lighter fluid with density of heavier fluid of 88.6624 and Atwood number of 0.633611.", + "a": "19.885356425931736", + "valid": true, + "kind": "compute", + "expect": 19.885356425931736, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of lighter fluid for [88.662361, 0.633611] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "What is the density of lighter fluid for density of heavier fluid 0.406608 and Atwood number 0.367206?", + "a": "0.18819354057064525", + "valid": true, + "kind": "compute", + "expect": 0.18819354057064525, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of lighter fluid for [0.406608, 0.367206] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "Tell me the density of lighter fluid with density of heavier fluid of 0.406608 and Atwood number of 0.367206.", + "a": "0.18819354057064525", + "valid": true, + "kind": "compute", + "expect": 0.18819354057064525, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of lighter fluid for [0.406608, 0.367206] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "What is the density of lighter fluid for density of heavier fluid 15.1985 and Atwood number 0.344595?", + "a": "7.408306240736808", + "valid": true, + "kind": "compute", + "expect": 7.408306240736808, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of lighter fluid for [15.198498, 0.344595] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "Tell me the density of lighter fluid with density of heavier fluid of 15.1985 and Atwood number of 0.344595.", + "a": "7.408306240736808", + "valid": true, + "kind": "compute", + "expect": 7.408306240736808, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of lighter fluid for [15.198498, 0.344595] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "What is the density of lighter fluid for density of heavier fluid 25.0765 and Atwood number 0.562861?", + "a": "7.0139956559697145", + "valid": true, + "kind": "compute", + "expect": 7.0139956559697145, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of lighter fluid for [25.076463, 0.562861] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "Tell me the density of lighter fluid with density of heavier fluid of 25.0765 and Atwood number of 0.562861.", + "a": "7.0139956559697145", + "valid": true, + "kind": "compute", + "expect": 7.0139956559697145, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of lighter fluid for [25.076463, 0.562861] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "What is the density of lighter fluid for density of heavier fluid 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Atwood number is not given" + }, + { + "q": "Tell me the density of lighter fluid with density of heavier fluid of 8.50341.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Atwood number is not given" + }, + { + "q": "What is the density of lighter fluid of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the density of lighter fluid of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the density of lighter fluid with density of heavier fluid of -88.6624 and Atwood number of 0.633611.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative density of heavier fluid: no example had one" + }, + { + "q": "Which density of lighter fluid is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the density of heavier fluid is 13.999 and the Atwood number is 0.348438, what is the density of lighter fluid?", + "a": "6.764302220142066", + "valid": true, + "kind": "compute", + "expect": 6.764302220142066, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of lighter fluid for [13.999029, 0.348438] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "Could you work out the density of lighter fluid given density of heavier fluid = 13.999, Atwood number = 0.348438?", + "a": "6.764302220142066", + "valid": true, + "kind": "compute", + "expect": 6.764302220142066, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of lighter fluid for [13.999029, 0.348438] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "I need the density of lighter fluid for a density of heavier fluid of 75.8534 and an Atwood number of 0.558623, please.", + "a": "21.480462286309322", + "valid": true, + "kind": "compute", + "expect": 21.480462286309322, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of lighter fluid for [75.853435, 0.558623] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "If the density of heavier fluid is 75.8534 and the Atwood number is 0.558623, what is the density of lighter fluid?", + "a": "21.480462286309322", + "valid": true, + "kind": "compute", + "expect": 21.480462286309322, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of lighter fluid for [75.853435, 0.558623] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "Could you work out the density of lighter fluid given density of heavier fluid = 34.8151, Atwood number = 0.380269?", + "a": "15.631741455772913", + "valid": true, + "kind": "compute", + "expect": 15.631741455772913, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of lighter fluid for [34.81508, 0.380269] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "I need the density of lighter fluid for a density of heavier fluid of 34.8151 and an Atwood number of 0.380269, please.", + "a": "15.631741455772913", + "valid": true, + "kind": "compute", + "expect": 15.631741455772913, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of lighter fluid for [34.81508, 0.380269] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "If the density of heavier fluid is 10.2544 and the Atwood number is 0.415084, what is the density of lighter fluid?", + "a": "4.238582766737838", + "valid": true, + "kind": "compute", + "expect": 4.238582766737838, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of lighter fluid for [10.254383, 0.415084] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "Could you work out the density of lighter fluid given density of heavier fluid = 10.2544, Atwood number = 0.415084?", + "a": "4.238582766737838", + "valid": true, + "kind": "compute", + "expect": 4.238582766737838, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of lighter fluid for [10.254383, 0.415084] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "I need the density of lighter fluid for a density of heavier fluid of 30.6611 and an Atwood number of 0.436857, please.", + "a": "12.016917215908114", + "valid": true, + "kind": "compute", + "expect": 12.016917215908114, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of lighter fluid for [30.661095, 0.436857] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "If the density of heavier fluid is 30.6611 and the Atwood number is 0.436857, what is the density of lighter fluid?", + "a": "12.016917215908114", + "valid": true, + "kind": "compute", + "expect": 12.016917215908114, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of lighter fluid for [30.661095, 0.436857] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "Could you work out the density of lighter fluid given density of heavier fluid = 44.8717, Atwood number = 0.32142?", + "a": "23.042667595165177", + "valid": true, + "kind": "compute", + "expect": 23.042667595165177, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of lighter fluid for [44.87169, 0.32142] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "I need the density of lighter fluid for a density of heavier fluid of 44.8717 and an Atwood number of 0.32142, please.", + "a": "23.042667595165177", + "valid": true, + "kind": "compute", + "expect": 23.042667595165177, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density of lighter fluid for [44.87169, 0.32142] (the system Atwood number (Wikidata Q2373823))" + }, + { + "q": "If the density of heavier fluid is 13.999, what is the density of lighter fluid?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Atwood number is not given" + }, + { + "q": "Could you work out the density of lighter fluid given density of heavier fluid = 75.8534?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Atwood number is not given" + }, + { + "q": "Do you know the density of lighter fluid of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the density of lighter fluid of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the density of lighter fluid given density of heavier fluid = -34.8151, Atwood number = 0.380269?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative density of heavier fluid: no example had one" + }, + { + "q": "What is a typical density of lighter fluid?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-density-of-lighter-fluid/program.py b/root/organs10/physics/specialists/solved-density-of-lighter-fluid/program.py new file mode 100644 index 0000000000000000000000000000000000000000..2aeeb1bc73b537b1335b733074b9098361eddffa --- /dev/null +++ b/root/organs10/physics/specialists/solved-density-of-lighter-fluid/program.py @@ -0,0 +1,7 @@ +"""density of lighter fluid from density of heavier fluid, Atwood number (Atwood number (Wikidata Q2373823)) +The maths organ (systems.py): solved from the system q2 = ((q0 - q1)/(q0 + q1)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(density_of_heavier_fluid, atwood_number): + return 1.0*density_of_heavier_fluid*(1.0 - atwood_number)/(atwood_number + 1.0) diff --git a/root/organs10/physics/specialists/solved-density-of-lighter-fluid/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-density-of-lighter-fluid/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-density-of-lighter-fluid/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-density/organ_formula_v2.py b/root/organs10/physics/specialists/solved-density/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-density/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-density/panel.json b/root/organs10/physics/specialists/solved-density/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..e7a611cf2af5ebe2c619e18ea87f489f63723ff0 --- /dev/null +++ b/root/organs10/physics/specialists/solved-density/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the density for mass 107.961 and volume 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [107.960666, 59.4105] (the system Density)" + }, + { + "q": "Tell me the density with mass of 107.961 and volume of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [107.960666, 59.4105] (the system Density)" + }, + { + "q": "What is the density for mass 169.527 and volume 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [169.526958, 280.773702] (the system Density)" + }, + { + "q": "Tell me the density with mass of 169.527 and volume of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [169.526958, 280.773702] (the system Density)" + }, + { + "q": "What is the density for mass 1.91195 and volume 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [1.911952, 82.240072] (the system Density)" + }, + { + "q": "Tell me the density with mass of 1.91195 and volume of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [1.911952, 82.240072] (the system Density)" + }, + { + "q": "What is the density for mass 1.64379 and volume 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [1.643795, 252.709024] (the system Density)" + }, + { + "q": "Tell me the density with mass of 1.64379 and volume of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [1.643795, 252.709024] (the system Density)" + }, + { + "q": "What is the density for mass 18.3169 and volume 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [18.316867, 6.838598] (the system Density)" + }, + { + "q": "Tell me the density with mass of 18.3169 and volume of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [18.316867, 6.838598] (the system Density)" + }, + { + "q": "What is the density for mass 156.502 and volume 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [156.50181, 185.886273] (the system Density)" + }, + { + "q": "Tell me the density with mass of 156.502 and volume of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [156.50181, 185.886273] (the system Density)" + }, + { + "q": "What is the density for mass 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the volume is not given" + }, + { + "q": "Tell me the density with mass of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the volume is not given" + }, + { + "q": "What is the density of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the density of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the density with mass of -1.91195 and volume of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mass: no example had one" + }, + { + "q": "Which density is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the mass is 8.50728 and the volume is 17.7888, what is the density?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [8.507283, 17.788778] (the system Density)" + }, + { + "q": "Could you work out the density given mass = 8.50728, volume = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [8.507283, 17.788778] (the system Density)" + }, + { + "q": "I need the density for a mass of 8.24951 and a volume of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [8.24951, 0.634021] (the system Density)" + }, + { + "q": "If the mass is 8.24951 and the volume is 0.634021, what is the density?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [8.24951, 0.634021] (the system Density)" + }, + { + "q": "Could you work out the density given mass = 0.318734, volume = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [0.318734, 9.566071] (the system Density)" + }, + { + "q": "I need the density for a mass of 0.318734 and a volume of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [0.318734, 9.566071] (the system Density)" + }, + { + "q": "If the mass is 0.671697 and the volume is 14.2496, what is the density?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [0.671697, 14.249569] (the system Density)" + }, + { + "q": "Could you work out the density given mass = 0.671697, volume = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [0.671697, 14.249569] (the system Density)" + }, + { + "q": "I need the density for a mass of 21.7931 and a volume of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [21.793126, 29.510609] (the system Density)" + }, + { + "q": "If the mass is 21.7931 and the volume is 29.5106, what is the density?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [21.793126, 29.510609] (the system Density)" + }, + { + "q": "Could you work out the density given mass = 153.15, volume = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [153.149965, 18.595315] (the system Density)" + }, + { + "q": "I need the density for a mass of 153.15 and a volume of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "density for [153.149965, 18.595315] (the system Density)" + }, + { + "q": "If the mass is 8.50728, what is the density?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the volume is not given" + }, + { + "q": "Could you work out the density given mass = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the volume is not given" + }, + { + "q": "Do you know the density of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the density of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the density given mass = -0.318734, volume = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mass: no example had one" + }, + { + "q": "What is a typical density?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-density/program.py b/root/organs10/physics/specialists/solved-density/program.py new file mode 100644 index 0000000000000000000000000000000000000000..ed174e6cc6c592c2e64b24eb1ac77c1d1cbbe9dd --- /dev/null +++ b/root/organs10/physics/specialists/solved-density/program.py @@ -0,0 +1,7 @@ +"""density from mass, volume (Density) +The maths organ (systems.py): solved from the system r = m/V; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(mass, volume): + return 1.0*mass/volume diff --git a/root/organs10/physics/specialists/solved-density/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-density/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-density/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-depth/organ_formula_v2.py b/root/organs10/physics/specialists/solved-depth/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-depth/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-depth/panel.json b/root/organs10/physics/specialists/solved-depth/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..c97c7fb18c5311997a5e3330743f06ae88075973 --- /dev/null +++ b/root/organs10/physics/specialists/solved-depth/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the depth for water pressure 107.961 and density 59.4105?", + "a": "0.18530266706666676", + "valid": true, + "kind": "compute", + "expect": 0.18530266706666676, + "dtype": "quantity", + "unit": "m", + "why": "depth for [107.960666, 59.4105] (the system Hydrostatic pressure)" + }, + { + "q": "Tell me the depth with water pressure of 107.961 and density of 59.4105.", + "a": "0.18530266706666676", + "valid": true, + "kind": "compute", + "expect": 0.18530266706666676, + "dtype": "quantity", + "unit": "m", + "why": "depth for [107.960666, 59.4105] (the system Hydrostatic pressure)" + }, + { + "q": "What is the depth for water pressure 169.527 and density 280.774?", + "a": "0.06156893823894976", + "valid": true, + "kind": "compute", + "expect": 0.06156893823894976, + "dtype": "quantity", + "unit": "m", + "why": "depth for [169.526958, 280.773702] (the system Hydrostatic pressure)" + }, + { + "q": "Tell me the depth with water pressure of 169.527 and density of 280.774.", + "a": "0.06156893823894976", + "valid": true, + "kind": "compute", + "expect": 0.06156893823894976, + "dtype": "quantity", + "unit": "m", + "why": "depth for [169.526958, 280.773702] (the system Hydrostatic pressure)" + }, + { + "q": "What is the depth for water pressure 1.91195 and density 82.2401?", + "a": "0.0023706791196374337", + "valid": true, + "kind": "compute", + "expect": 0.0023706791196374337, + "dtype": "quantity", + "unit": "m", + "why": "depth for [1.911952, 82.240072] (the system Hydrostatic pressure)" + }, + { + "q": "Tell me the depth with water pressure of 1.91195 and density of 82.2401.", + "a": "0.0023706791196374337", + "valid": true, + "kind": "compute", + "expect": 0.0023706791196374337, + "dtype": "quantity", + "unit": "m", + "why": "depth for [1.911952, 82.240072] (the system Hydrostatic pressure)" + }, + { + "q": "What is the depth for water pressure 1.64379 and density 252.709?", + "a": "0.000663294443840899", + "valid": true, + "kind": "compute", + "expect": 0.000663294443840899, + "dtype": "quantity", + "unit": "m", + "why": "depth for [1.643795, 252.709024] (the system Hydrostatic pressure)" + }, + { + "q": "Tell me the depth with water pressure of 1.64379 and density of 252.709.", + "a": "0.000663294443840899", + "valid": true, + "kind": "compute", + "expect": 0.000663294443840899, + "dtype": "quantity", + "unit": "m", + "why": "depth for [1.643795, 252.709024] (the system Hydrostatic pressure)" + }, + { + "q": "What is the depth for water pressure 18.3169 and density 6.8386?", + "a": "0.2731262476069092", + "valid": true, + "kind": "compute", + "expect": 0.2731262476069092, + "dtype": "quantity", + "unit": "m", + "why": "depth for [18.316867, 6.838598] (the system Hydrostatic pressure)" + }, + { + "q": "Tell me the depth with water pressure of 18.3169 and density of 6.8386.", + "a": "0.2731262476069092", + "valid": true, + "kind": "compute", + "expect": 0.2731262476069092, + "dtype": "quantity", + "unit": "m", + "why": "depth for [18.316867, 6.838598] (the system Hydrostatic pressure)" + }, + { + "q": "What is the depth for water pressure 156.502 and density 185.886?", + "a": "0.08585218822035659", + "valid": true, + "kind": "compute", + "expect": 0.08585218822035659, + "dtype": "quantity", + "unit": "m", + "why": "depth for [156.50181, 185.886273] (the system Hydrostatic pressure)" + }, + { + "q": "Tell me the depth with water pressure of 156.502 and density of 185.886.", + "a": "0.08585218822035659", + "valid": true, + "kind": "compute", + "expect": 0.08585218822035659, + "dtype": "quantity", + "unit": "m", + "why": "depth for [156.50181, 185.886273] (the system Hydrostatic pressure)" + }, + { + "q": "What is the depth for water pressure 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the density is not given" + }, + { + "q": "Tell me the depth with water pressure of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the density is not given" + }, + { + "q": "What is the depth of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the depth of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the depth with water pressure of -1.91195 and density of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative water pressure: no example had one" + }, + { + "q": "Which depth is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the water pressure is 8.50728 and the density is 17.7888, what is the depth?", + "a": "0.04876677863157268", + "valid": true, + "kind": "compute", + "expect": 0.04876677863157268, + "dtype": "quantity", + "unit": "m", + "why": "depth for [8.507283, 17.788778] (the system Hydrostatic pressure)" + }, + { + "q": "Could you work out the depth given water pressure = 8.50728, density = 17.7888?", + "a": "0.04876677863157268", + "valid": true, + "kind": "compute", + "expect": 0.04876677863157268, + "dtype": "quantity", + "unit": "m", + "why": "depth for [8.507283, 17.788778] (the system Hydrostatic pressure)" + }, + { + "q": "I need the depth for a water pressure of 8.24951 and a density of 0.634021, please.", + "a": "1.3267950621862565", + "valid": true, + "kind": "compute", + "expect": 1.3267950621862565, + "dtype": "quantity", + "unit": "m", + "why": "depth for [8.24951, 0.634021] (the system Hydrostatic pressure)" + }, + { + "q": "If the water pressure is 8.24951 and the density is 0.634021, what is the depth?", + "a": "1.3267950621862565", + "valid": true, + "kind": "compute", + "expect": 1.3267950621862565, + "dtype": "quantity", + "unit": "m", + "why": "depth for [8.24951, 0.634021] (the system Hydrostatic pressure)" + }, + { + "q": "Could you work out the depth given water pressure = 0.318734, density = 9.56607?", + "a": "0.003397615137949426", + "valid": true, + "kind": "compute", + "expect": 0.003397615137949426, + "dtype": "quantity", + "unit": "m", + "why": "depth for [0.318734, 9.566071] (the system Hydrostatic pressure)" + }, + { + "q": "I need the depth for a water pressure of 0.318734 and a density of 9.56607, please.", + "a": "0.003397615137949426", + "valid": true, + "kind": "compute", + "expect": 0.003397615137949426, + "dtype": "quantity", + "unit": "m", + "why": "depth for [0.318734, 9.566071] (the system Hydrostatic pressure)" + }, + { + "q": "If the water pressure is 0.671697 and the density is 14.2496, what is the depth?", + "a": "0.004806745450727051", + "valid": true, + "kind": "compute", + "expect": 0.004806745450727051, + "dtype": "quantity", + "unit": "m", + "why": "depth for [0.671697, 14.249569] (the system Hydrostatic pressure)" + }, + { + "q": "Could you work out the depth given water pressure = 0.671697, density = 14.2496?", + "a": "0.004806745450727051", + "valid": true, + "kind": "compute", + "expect": 0.004806745450727051, + "dtype": "quantity", + "unit": "m", + "why": "depth for [0.671697, 14.249569] (the system Hydrostatic pressure)" + }, + { + "q": "I need the depth for a water pressure of 21.7931 and a density of 29.5106, please.", + "a": "0.0753044597343837", + "valid": true, + "kind": "compute", + "expect": 0.0753044597343837, + "dtype": "quantity", + "unit": "m", + "why": "depth for [21.793126, 29.510609] (the system Hydrostatic pressure)" + }, + { + "q": "If the water pressure is 21.7931 and the density is 29.5106, what is the depth?", + "a": "0.0753044597343837", + "valid": true, + "kind": "compute", + "expect": 0.0753044597343837, + "dtype": "quantity", + "unit": "m", + "why": "depth for [21.793126, 29.510609] (the system Hydrostatic pressure)" + }, + { + "q": "Could you work out the depth given water pressure = 153.15, density = 18.5953?", + "a": "0.8398324982026809", + "valid": true, + "kind": "compute", + "expect": 0.8398324982026809, + "dtype": "quantity", + "unit": "m", + "why": "depth for [153.149965, 18.595315] (the system Hydrostatic pressure)" + }, + { + "q": "I need the depth for a water pressure of 153.15 and a density of 18.5953, please.", + "a": "0.8398324982026809", + "valid": true, + "kind": "compute", + "expect": 0.8398324982026809, + "dtype": "quantity", + "unit": "m", + "why": "depth for [153.149965, 18.595315] (the system Hydrostatic pressure)" + }, + { + "q": "If the water pressure is 8.50728, what is the depth?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the density is not given" + }, + { + "q": "Could you work out the depth given water pressure = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the density is not given" + }, + { + "q": "Do you know the depth of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the depth of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the depth given water pressure = -0.318734, density = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative water pressure: no example had one" + }, + { + "q": "What is a typical depth?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-depth/program.py b/root/organs10/physics/specialists/solved-depth/program.py new file mode 100644 index 0000000000000000000000000000000000000000..b82264310b00b16231862226c911f1c92efe1720 --- /dev/null +++ b/root/organs10/physics/specialists/solved-depth/program.py @@ -0,0 +1,7 @@ +"""depth from water pressure, density (Hydrostatic pressure) +The maths organ (systems.py): solved from the system p = r*g*h; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(water_pressure, density): + return 0.101971621297793*water_pressure/density diff --git a/root/organs10/physics/specialists/solved-depth/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-depth/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-depth/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-diameter/organ_formula_v2.py b/root/organs10/physics/specialists/solved-diameter/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-diameter/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-diameter/panel.json b/root/organs10/physics/specialists/solved-diameter/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..e3b4e39fbc9b2cf8e73fd6094ae157be26baa6f2 --- /dev/null +++ b/root/organs10/physics/specialists/solved-diameter/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the diameter for radius 107.961?", + "a": "215.92133177992403", + "valid": true, + "kind": "compute", + "expect": 215.92133177992403, + "dtype": "quantity", + "unit": "m", + "why": "diameter for [107.960666] (the system Diameter)" + }, + { + "q": "Tell me the diameter with radius of 107.961.", + "a": "215.92133177992403", + "valid": true, + "kind": "compute", + "expect": 215.92133177992403, + "dtype": "quantity", + "unit": "m", + "why": "diameter for [107.960666] (the system Diameter)" + }, + { + "q": "What is the diameter for radius 22.652?", + "a": "45.304045593711585", + "valid": true, + "kind": "compute", + "expect": 45.304045593711585, + "dtype": "quantity", + "unit": "m", + "why": "diameter for [22.652023] (the system Diameter)" + }, + { + "q": "Tell me the diameter with radius of 22.652.", + "a": "45.304045593711585", + "valid": true, + "kind": "compute", + "expect": 45.304045593711585, + "dtype": "quantity", + "unit": "m", + "why": "diameter for [22.652023] (the system Diameter)" + }, + { + "q": "What is the diameter for radius 173.394?", + "a": "346.787276810605", + "valid": true, + "kind": "compute", + "expect": 346.787276810605, + "dtype": "quantity", + "unit": "m", + "why": "diameter for [173.393638] (the system Diameter)" + }, + { + "q": "Tell me the diameter with radius of 173.394.", + "a": "346.787276810605", + "valid": true, + "kind": "compute", + "expect": 346.787276810605, + "dtype": "quantity", + "unit": "m", + "why": "diameter for [173.393638] (the system Diameter)" + }, + { + "q": "What is the diameter for radius 126.709?", + "a": "253.41850360036474", + "valid": true, + "kind": "compute", + "expect": 253.41850360036474, + "dtype": "quantity", + "unit": "m", + "why": "diameter for [126.709252] (the system Diameter)" + }, + { + "q": "Tell me the diameter with radius of 126.709.", + "a": "253.41850360036474", + "valid": true, + "kind": "compute", + "expect": 253.41850360036474, + "dtype": "quantity", + "unit": "m", + "why": "diameter for [126.709252] (the system Diameter)" + }, + { + "q": "What is the diameter for radius 0.671697?", + "a": "1.343394399588387", + "valid": true, + "kind": "compute", + "expect": 1.343394399588387, + "dtype": "quantity", + "unit": "m", + "why": "diameter for [0.671697] (the system Diameter)" + }, + { + "q": "Tell me the diameter with radius of 0.671697.", + "a": "1.343394399588387", + "valid": true, + "kind": "compute", + "expect": 1.343394399588387, + "dtype": "quantity", + "unit": "m", + "why": "diameter for [0.671697] (the system Diameter)" + }, + { + "q": "What is the diameter for radius 1.16997?", + "a": "2.3399360119558716", + "valid": true, + "kind": "compute", + "expect": 2.3399360119558716, + "dtype": "quantity", + "unit": "m", + "why": "diameter for [1.169968] (the system Diameter)" + }, + { + "q": "Tell me the diameter with radius of 1.16997.", + "a": "2.3399360119558716", + "valid": true, + "kind": "compute", + "expect": 2.3399360119558716, + "dtype": "quantity", + "unit": "m", + "why": "diameter for [1.169968] (the system Diameter)" + }, + { + "q": "What is the diameter?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radius is not given" + }, + { + "q": "Could you work out the diameter, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radius is not given" + }, + { + "q": "What is the diameter of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the diameter of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the diameter with radius of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative radius: no example had one" + }, + { + "q": "Which diameter is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the radius is 2.57003, what is the diameter?", + "a": "5.140059826607287", + "valid": true, + "kind": "compute", + "expect": 5.140059826607287, + "dtype": "quantity", + "unit": "m", + "why": "diameter for [2.57003] (the system Diameter)" + }, + { + "q": "Could you work out the diameter given radius = 2.57003?", + "a": "5.140059826607287", + "valid": true, + "kind": "compute", + "expect": 5.140059826607287, + "dtype": "quantity", + "unit": "m", + "why": "diameter for [2.57003] (the system Diameter)" + }, + { + "q": "I need the diameter for a radius of 48.921, please.", + "a": "97.84198172002367", + "valid": true, + "kind": "compute", + "expect": 97.84198172002367, + "dtype": "quantity", + "unit": "m", + "why": "diameter for [48.920991] (the system Diameter)" + }, + { + "q": "If the radius is 48.921, what is the diameter?", + "a": "97.84198172002367", + "valid": true, + "kind": "compute", + "expect": 97.84198172002367, + "dtype": "quantity", + "unit": "m", + "why": "diameter for [48.920991] (the system Diameter)" + }, + { + "q": "Could you work out the diameter given radius = 0.348438?", + "a": "0.696875339260061", + "valid": true, + "kind": "compute", + "expect": 0.696875339260061, + "dtype": "quantity", + "unit": "m", + "why": "diameter for [0.348438] (the system Diameter)" + }, + { + "q": "I need the diameter for a radius of 0.348438, please.", + "a": "0.696875339260061", + "valid": true, + "kind": "compute", + "expect": 0.696875339260061, + "dtype": "quantity", + "unit": "m", + "why": "diameter for [0.348438] (the system Diameter)" + }, + { + "q": "If the radius is 252.709, what is the diameter?", + "a": "505.41804849905503", + "valid": true, + "kind": "compute", + "expect": 505.41804849905503, + "dtype": "quantity", + "unit": "m", + "why": "diameter for [252.709024] (the system Diameter)" + }, + { + "q": "Could you work out the diameter given radius = 252.709?", + "a": "505.41804849905503", + "valid": true, + "kind": "compute", + "expect": 505.41804849905503, + "dtype": "quantity", + "unit": "m", + "why": "diameter for [252.709024] (the system Diameter)" + }, + { + "q": "I need the diameter for a radius of 20.3966, please.", + "a": "40.79311592392688", + "valid": true, + "kind": "compute", + "expect": 40.79311592392688, + "dtype": "quantity", + "unit": "m", + "why": "diameter for [20.396558] (the system Diameter)" + }, + { + "q": "If the radius is 20.3966, what is the diameter?", + "a": "40.79311592392688", + "valid": true, + "kind": "compute", + "expect": 40.79311592392688, + "dtype": "quantity", + "unit": "m", + "why": "diameter for [20.396558] (the system Diameter)" + }, + { + "q": "Could you work out the diameter given radius = 134.993?", + "a": "269.985915161476", + "valid": true, + "kind": "compute", + "expect": 269.985915161476, + "dtype": "quantity", + "unit": "m", + "why": "diameter for [134.992958] (the system Diameter)" + }, + { + "q": "I need the diameter for a radius of 134.993, please.", + "a": "269.985915161476", + "valid": true, + "kind": "compute", + "expect": 269.985915161476, + "dtype": "quantity", + "unit": "m", + "why": "diameter for [134.992958] (the system Diameter)" + }, + { + "q": "I need the diameter, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radius is not given" + }, + { + "q": "Can you tell me the diameter?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radius is not given" + }, + { + "q": "Do you know the diameter of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the diameter of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the diameter given radius = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative radius: no example had one" + }, + { + "q": "What is a typical diameter?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-diameter/program.py b/root/organs10/physics/specialists/solved-diameter/program.py new file mode 100644 index 0000000000000000000000000000000000000000..99e75b65957cffd70cee23561db63534fe353d33 --- /dev/null +++ b/root/organs10/physics/specialists/solved-diameter/program.py @@ -0,0 +1,7 @@ +"""diameter from radius (Diameter) +The maths organ (systems.py): solved from the system D = 2*r; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(radius): + return 2.0*radius diff --git a/root/organs10/physics/specialists/solved-diameter/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-diameter/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-diameter/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-diffusion-area/organ_formula_v2.py b/root/organs10/physics/specialists/solved-diffusion-area/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-diffusion-area/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-diffusion-area/panel.json b/root/organs10/physics/specialists/solved-diffusion-area/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..5b30a4a8556f4c0539e6cd3daff11e5dcc099622 --- /dev/null +++ b/root/organs10/physics/specialists/solved-diffusion-area/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the diffusion area for slowing-down area 8.50728 and migration area 17.7888?", + "a": "9.281495473483474", + "valid": true, + "kind": "compute", + "expect": 9.281495473483474, + "dtype": "quantity", + "unit": "m^2", + "why": "diffusion area for [8.507283, 17.788778] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "Tell me the diffusion area with slowing-down area of 8.50728 and migration area of 17.7888.", + "a": "9.281495473483474", + "valid": true, + "kind": "compute", + "expect": 9.281495473483474, + "dtype": "quantity", + "unit": "m^2", + "why": "diffusion area for [8.507283, 17.788778] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "What is the diffusion area for slowing-down area 6.34623 and migration area 21.511?", + "a": "15.164790256952909", + "valid": true, + "kind": "compute", + "expect": 15.164790256952909, + "dtype": "quantity", + "unit": "m^2", + "why": "diffusion area for [6.346226, 21.511016] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "Tell me the diffusion area with slowing-down area of 6.34623 and migration area of 21.511.", + "a": "15.164790256952909", + "valid": true, + "kind": "compute", + "expect": 15.164790256952909, + "dtype": "quantity", + "unit": "m^2", + "why": "diffusion area for [6.346226, 21.511016] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "What is the diffusion area for slowing-down area 1.64379 and migration area 252.709?", + "a": "251.06522875466473", + "valid": true, + "kind": "compute", + "expect": 251.06522875466473, + "dtype": "quantity", + "unit": "m^2", + "why": "diffusion area for [1.643795, 252.709024] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "Tell me the diffusion area with slowing-down area of 1.64379 and migration area of 252.709.", + "a": "251.06522875466473", + "valid": true, + "kind": "compute", + "expect": 251.06522875466473, + "dtype": "quantity", + "unit": "m^2", + "why": "diffusion area for [1.643795, 252.709024] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "What is the diffusion area for slowing-down area 21.7931 and migration area 29.5106?", + "a": "7.717482167609866", + "valid": true, + "kind": "compute", + "expect": 7.717482167609866, + "dtype": "quantity", + "unit": "m^2", + "why": "diffusion area for [21.793126, 29.510609] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "Tell me the diffusion area with slowing-down area of 21.7931 and migration area of 29.5106.", + "a": "7.717482167609866", + "valid": true, + "kind": "compute", + "expect": 7.717482167609866, + "dtype": "quantity", + "unit": "m^2", + "why": "diffusion area for [21.793126, 29.510609] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "What is the diffusion area for slowing-down area 7.10705 and migration area 30.2507?", + "a": "23.143700093865256", + "valid": true, + "kind": "compute", + "expect": 23.143700093865256, + "dtype": "quantity", + "unit": "m^2", + "why": "diffusion area for [7.107049, 30.250749] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "Tell me the diffusion area with slowing-down area of 7.10705 and migration area of 30.2507.", + "a": "23.143700093865256", + "valid": true, + "kind": "compute", + "expect": 23.143700093865256, + "dtype": "quantity", + "unit": "m^2", + "why": "diffusion area for [7.107049, 30.250749] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "What is the diffusion area for slowing-down area 0.869111 and migration area 39.1753?", + "a": "38.3061515435122", + "valid": true, + "kind": "compute", + "expect": 38.3061515435122, + "dtype": "quantity", + "unit": "m^2", + "why": "diffusion area for [0.869111, 39.175263] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "Tell me the diffusion area with slowing-down area of 0.869111 and migration area of 39.1753.", + "a": "38.3061515435122", + "valid": true, + "kind": "compute", + "expect": 38.3061515435122, + "dtype": "quantity", + "unit": "m^2", + "why": "diffusion area for [0.869111, 39.175263] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "What is the diffusion area for slowing-down area 8.50728?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the migration area is not given" + }, + { + "q": "Tell me the diffusion area with slowing-down area of 6.34623.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the migration area is not given" + }, + { + "q": "What is the diffusion area of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the diffusion area of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the diffusion area with slowing-down area of -1.64379 and migration area of 252.709.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative slowing-down area: no example had one" + }, + { + "q": "Which diffusion area is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the slowing-down area is 169.527 and the migration area is 280.774, what is the diffusion area?", + "a": "111.24674440201241", + "valid": true, + "kind": "compute", + "expect": 111.24674440201241, + "dtype": "quantity", + "unit": "m^2", + "why": "diffusion area for [169.526958, 280.773702] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "Could you work out the diffusion area given slowing-down area = 169.527, migration area = 280.774?", + "a": "111.24674440201241", + "valid": true, + "kind": "compute", + "expect": 111.24674440201241, + "dtype": "quantity", + "unit": "m^2", + "why": "diffusion area for [169.526958, 280.773702] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "I need the diffusion area for a slowing-down area of 0.318734 and a migration area of 9.56607, please.", + "a": "9.24733694103161", + "valid": true, + "kind": "compute", + "expect": 9.24733694103161, + "dtype": "quantity", + "unit": "m^2", + "why": "diffusion area for [0.318734, 9.566071] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "If the slowing-down area is 0.318734 and the migration area is 9.56607, what is the diffusion area?", + "a": "9.24733694103161", + "valid": true, + "kind": "compute", + "expect": 9.24733694103161, + "dtype": "quantity", + "unit": "m^2", + "why": "diffusion area for [0.318734, 9.566071] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "Could you work out the diffusion area given slowing-down area = 0.671697, migration area = 14.2496?", + "a": "13.57787211614313", + "valid": true, + "kind": "compute", + "expect": 13.57787211614313, + "dtype": "quantity", + "unit": "m^2", + "why": "diffusion area for [0.671697, 14.249569] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "I need the diffusion area for a slowing-down area of 0.671697 and a migration area of 14.2496, please.", + "a": "13.57787211614313", + "valid": true, + "kind": "compute", + "expect": 13.57787211614313, + "dtype": "quantity", + "unit": "m^2", + "why": "diffusion area for [0.671697, 14.249569] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "If the slowing-down area is 156.502 and the migration area is 185.886, what is the diffusion area?", + "a": "29.38446301183552", + "valid": true, + "kind": "compute", + "expect": 29.38446301183552, + "dtype": "quantity", + "unit": "m^2", + "why": "diffusion area for [156.50181, 185.886273] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "Could you work out the diffusion area given slowing-down area = 156.502, migration area = 185.886?", + "a": "29.38446301183552", + "valid": true, + "kind": "compute", + "expect": 29.38446301183552, + "dtype": "quantity", + "unit": "m^2", + "why": "diffusion area for [156.50181, 185.886273] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "I need the diffusion area for a slowing-down area of 24.5721 and a migration area of 108.451, please.", + "a": "83.87870607231024", + "valid": true, + "kind": "compute", + "expect": 83.87870607231024, + "dtype": "quantity", + "unit": "m^2", + "why": "diffusion area for [24.572086, 108.450792] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "If the slowing-down area is 24.5721 and the migration area is 108.451, what is the diffusion area?", + "a": "83.87870607231024", + "valid": true, + "kind": "compute", + "expect": 83.87870607231024, + "dtype": "quantity", + "unit": "m^2", + "why": "diffusion area for [24.572086, 108.450792] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "Could you work out the diffusion area given slowing-down area = 4.07774, migration area = 276.408?", + "a": "272.32983938133856", + "valid": true, + "kind": "compute", + "expect": 272.32983938133856, + "dtype": "quantity", + "unit": "m^2", + "why": "diffusion area for [4.077743, 276.407582] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "I need the diffusion area for a slowing-down area of 4.07774 and a migration area of 276.408, please.", + "a": "272.32983938133856", + "valid": true, + "kind": "compute", + "expect": 272.32983938133856, + "dtype": "quantity", + "unit": "m^2", + "why": "diffusion area for [4.077743, 276.407582] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "If the slowing-down area is 169.527, what is the diffusion area?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the migration area is not given" + }, + { + "q": "Could you work out the diffusion area given slowing-down area = 0.318734?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the migration area is not given" + }, + { + "q": "Do you know the diffusion area of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the diffusion area of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the diffusion area given slowing-down area = -0.671697, migration area = 14.2496?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative slowing-down area: no example had one" + }, + { + "q": "What is a typical diffusion area?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-diffusion-area/program.py b/root/organs10/physics/specialists/solved-diffusion-area/program.py new file mode 100644 index 0000000000000000000000000000000000000000..b5725722706313a85b907fb95b75a4c11022f502 --- /dev/null +++ b/root/organs10/physics/specialists/solved-diffusion-area/program.py @@ -0,0 +1,7 @@ +"""diffusion area from slowing-down area, migration area (migration area (Wikidata Q98966325)) +The maths organ (systems.py): solved from the system q1 = q0 + q2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(slowing_down_area, migration_area): + return 1.0*migration_area - 1.0*slowing_down_area diff --git a/root/organs10/physics/specialists/solved-diffusion-area/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-diffusion-area/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-diffusion-area/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-diffusion-length/organ_formula_v2.py b/root/organs10/physics/specialists/solved-diffusion-length/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-diffusion-length/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-diffusion-length/panel.json b/root/organs10/physics/specialists/solved-diffusion-length/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..9ad8119dd34b02b99425e9a28d04215f62b7d38f --- /dev/null +++ b/root/organs10/physics/specialists/solved-diffusion-length/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the diffusion length for mass diffusivity 107.961 and mean lifetime 59.4105?", + "a": "80.0874344794149", + "valid": true, + "kind": "compute", + "expect": 80.0874344794149, + "dtype": "quantity", + "unit": "m", + "why": "diffusion length for [107.960666, 59.4105] (the system diffusion length (Wikidata Q106097176))" + }, + { + "q": "Tell me the diffusion length with mass diffusivity of 107.961 and mean lifetime of 59.4105.", + "a": "80.0874344794149", + "valid": true, + "kind": "compute", + "expect": 80.0874344794149, + "dtype": "quantity", + "unit": "m", + "why": "diffusion length for [107.960666, 59.4105] (the system diffusion length (Wikidata Q106097176))" + }, + { + "q": "What is the diffusion length for mass diffusivity 169.527 and mean lifetime 280.774?", + "a": "218.17128923611799", + "valid": true, + "kind": "compute", + "expect": 218.17128923611799, + "dtype": "quantity", + "unit": "m", + "why": "diffusion length for [169.526958, 280.773702] (the system diffusion length (Wikidata Q106097176))" + }, + { + "q": "Tell me the diffusion length with mass diffusivity of 169.527 and mean lifetime of 280.774.", + "a": "218.17128923611799", + "valid": true, + "kind": "compute", + "expect": 218.17128923611799, + "dtype": "quantity", + "unit": "m", + "why": "diffusion length for [169.526958, 280.773702] (the system diffusion length (Wikidata Q106097176))" + }, + { + "q": "What is the diffusion length for mass diffusivity 1.91195 and mean lifetime 82.2401?", + "a": "12.539499605155465", + "valid": true, + "kind": "compute", + "expect": 12.539499605155465, + "dtype": "quantity", + "unit": "m", + "why": "diffusion length for [1.911952, 82.240072] (the system diffusion length (Wikidata Q106097176))" + }, + { + "q": "Tell me the diffusion length with mass diffusivity of 1.91195 and mean lifetime of 82.2401.", + "a": "12.539499605155465", + "valid": true, + "kind": "compute", + "expect": 12.539499605155465, + "dtype": "quantity", + "unit": "m", + "why": "diffusion length for [1.911952, 82.240072] (the system diffusion length (Wikidata Q106097176))" + }, + { + "q": "What is the diffusion length for mass diffusivity 1.64379 and mean lifetime 252.709?", + "a": "20.381412011255374", + "valid": true, + "kind": "compute", + "expect": 20.381412011255374, + "dtype": "quantity", + "unit": "m", + "why": "diffusion length for [1.643795, 252.709024] (the system diffusion length (Wikidata Q106097176))" + }, + { + "q": "Tell me the diffusion length with mass diffusivity of 1.64379 and mean lifetime of 252.709.", + "a": "20.381412011255374", + "valid": true, + "kind": "compute", + "expect": 20.381412011255374, + "dtype": "quantity", + "unit": "m", + "why": "diffusion length for [1.643795, 252.709024] (the system diffusion length (Wikidata Q106097176))" + }, + { + "q": "What is the diffusion length for mass diffusivity 18.3169 and mean lifetime 6.8386?", + "a": "11.192036959969323", + "valid": true, + "kind": "compute", + "expect": 11.192036959969323, + "dtype": "quantity", + "unit": "m", + "why": "diffusion length for [18.316867, 6.838598] (the system diffusion length (Wikidata Q106097176))" + }, + { + "q": "Tell me the diffusion length with mass diffusivity of 18.3169 and mean lifetime of 6.8386.", + "a": "11.192036959969323", + "valid": true, + "kind": "compute", + "expect": 11.192036959969323, + "dtype": "quantity", + "unit": "m", + "why": "diffusion length for [18.316867, 6.838598] (the system diffusion length (Wikidata Q106097176))" + }, + { + "q": "What is the diffusion length for mass diffusivity 156.502 and mean lifetime 185.886?", + "a": "170.56241703604329", + "valid": true, + "kind": "compute", + "expect": 170.56241703604329, + "dtype": "quantity", + "unit": "m", + "why": "diffusion length for [156.50181, 185.886273] (the system diffusion length (Wikidata Q106097176))" + }, + { + "q": "Tell me the diffusion length with mass diffusivity of 156.502 and mean lifetime of 185.886.", + "a": "170.56241703604329", + "valid": true, + "kind": "compute", + "expect": 170.56241703604329, + "dtype": "quantity", + "unit": "m", + "why": "diffusion length for [156.50181, 185.886273] (the system diffusion length (Wikidata Q106097176))" + }, + { + "q": "What is the diffusion length for mass diffusivity 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mean lifetime is not given" + }, + { + "q": "Tell me the diffusion length with mass diffusivity of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mean lifetime is not given" + }, + { + "q": "What is the diffusion length of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the diffusion length of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the diffusion length with mass diffusivity of -1.91195 and mean lifetime of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mass diffusivity: no example had one" + }, + { + "q": "Which diffusion length is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the mass diffusivity is 8.50728 and the mean lifetime is 17.7888, what is the diffusion length?", + "a": "12.301795137331414", + "valid": true, + "kind": "compute", + "expect": 12.301795137331414, + "dtype": "quantity", + "unit": "m", + "why": "diffusion length for [8.507283, 17.788778] (the system diffusion length (Wikidata Q106097176))" + }, + { + "q": "Could you work out the diffusion length given mass diffusivity = 8.50728, mean lifetime = 17.7888?", + "a": "12.301795137331414", + "valid": true, + "kind": "compute", + "expect": 12.301795137331414, + "dtype": "quantity", + "unit": "m", + "why": "diffusion length for [8.507283, 17.788778] (the system diffusion length (Wikidata Q106097176))" + }, + { + "q": "I need the diffusion length for a mass diffusivity of 8.24951 and a mean lifetime of 0.634021, please.", + "a": "2.2869985290709574", + "valid": true, + "kind": "compute", + "expect": 2.2869985290709574, + "dtype": "quantity", + "unit": "m", + "why": "diffusion length for [8.24951, 0.634021] (the system diffusion length (Wikidata Q106097176))" + }, + { + "q": "If the mass diffusivity is 8.24951 and the mean lifetime is 0.634021, what is the diffusion length?", + "a": "2.2869985290709574", + "valid": true, + "kind": "compute", + "expect": 2.2869985290709574, + "dtype": "quantity", + "unit": "m", + "why": "diffusion length for [8.24951, 0.634021] (the system diffusion length (Wikidata Q106097176))" + }, + { + "q": "Could you work out the diffusion length given mass diffusivity = 0.318734, mean lifetime = 9.56607?", + "a": "1.746147910787133", + "valid": true, + "kind": "compute", + "expect": 1.746147910787133, + "dtype": "quantity", + "unit": "m", + "why": "diffusion length for [0.318734, 9.566071] (the system diffusion length (Wikidata Q106097176))" + }, + { + "q": "I need the diffusion length for a mass diffusivity of 0.318734 and a mean lifetime of 9.56607, please.", + "a": "1.746147910787133", + "valid": true, + "kind": "compute", + "expect": 1.746147910787133, + "dtype": "quantity", + "unit": "m", + "why": "diffusion length for [0.318734, 9.566071] (the system diffusion length (Wikidata Q106097176))" + }, + { + "q": "If the mass diffusivity is 0.671697 and the mean lifetime is 14.2496, what is the diffusion length?", + "a": "3.093767251715675", + "valid": true, + "kind": "compute", + "expect": 3.093767251715675, + "dtype": "quantity", + "unit": "m", + "why": "diffusion length for [0.671697, 14.249569] (the system diffusion length (Wikidata Q106097176))" + }, + { + "q": "Could you work out the diffusion length given mass diffusivity = 0.671697, mean lifetime = 14.2496?", + "a": "3.093767251715675", + "valid": true, + "kind": "compute", + "expect": 3.093767251715675, + "dtype": "quantity", + "unit": "m", + "why": "diffusion length for [0.671697, 14.249569] (the system diffusion length (Wikidata Q106097176))" + }, + { + "q": "I need the diffusion length for a mass diffusivity of 21.7931 and a mean lifetime of 29.5106, please.", + "a": "25.359976880696326", + "valid": true, + "kind": "compute", + "expect": 25.359976880696326, + "dtype": "quantity", + "unit": "m", + "why": "diffusion length for [21.793126, 29.510609] (the system diffusion length (Wikidata Q106097176))" + }, + { + "q": "If the mass diffusivity is 21.7931 and the mean lifetime is 29.5106, what is the diffusion length?", + "a": "25.359976880696326", + "valid": true, + "kind": "compute", + "expect": 25.359976880696326, + "dtype": "quantity", + "unit": "m", + "why": "diffusion length for [21.793126, 29.510609] (the system diffusion length (Wikidata Q106097176))" + }, + { + "q": "Could you work out the diffusion length given mass diffusivity = 153.15, mean lifetime = 18.5953?", + "a": "53.3654562059328", + "valid": true, + "kind": "compute", + "expect": 53.3654562059328, + "dtype": "quantity", + "unit": "m", + "why": "diffusion length for [153.149965, 18.595315] (the system diffusion length (Wikidata Q106097176))" + }, + { + "q": "I need the diffusion length for a mass diffusivity of 153.15 and a mean lifetime of 18.5953, please.", + "a": "53.3654562059328", + "valid": true, + "kind": "compute", + "expect": 53.3654562059328, + "dtype": "quantity", + "unit": "m", + "why": "diffusion length for [153.149965, 18.595315] (the system diffusion length (Wikidata Q106097176))" + }, + { + "q": "If the mass diffusivity is 8.50728, what is the diffusion length?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mean lifetime is not given" + }, + { + "q": "Could you work out the diffusion length given mass diffusivity = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mean lifetime is not given" + }, + { + "q": "Do you know the diffusion length of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the diffusion length of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the diffusion length given mass diffusivity = -0.318734, mean lifetime = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mass diffusivity: no example had one" + }, + { + "q": "What is a typical diffusion length?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-diffusion-length/program.py b/root/organs10/physics/specialists/solved-diffusion-length/program.py new file mode 100644 index 0000000000000000000000000000000000000000..60995e27c1e787ab6c61721af53722e8984f4f67 --- /dev/null +++ b/root/organs10/physics/specialists/solved-diffusion-length/program.py @@ -0,0 +1,7 @@ +"""diffusion length from mass diffusivity, mean lifetime (diffusion length (Wikidata Q106097176)) +The maths organ (systems.py): solved from the system q0 = sqrt(q1*q2); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(mass_diffusivity, mean_lifetime): + return 1.0*mass_diffusivity**0.5*mean_lifetime**0.5 diff --git a/root/organs10/physics/specialists/solved-diffusion-length/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-diffusion-length/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-diffusion-length/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-dioptric-power/organ_formula_v2.py b/root/organs10/physics/specialists/solved-dioptric-power/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-dioptric-power/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-dioptric-power/panel.json b/root/organs10/physics/specialists/solved-dioptric-power/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..0115a2fa1d024de96a113c75f0559b17ea86c66c --- /dev/null +++ b/root/organs10/physics/specialists/solved-dioptric-power/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the dioptric power for focal length 107.961?", + "a": "0.00926263275385168", + "valid": true, + "kind": "compute", + "expect": 0.00926263275385168, + "dtype": "quantity", + "unit": "1/m", + "why": "dioptric power for [107.960666] (the system dioptric power (Wikidata Q559265))" + }, + { + "q": "Tell me the dioptric power with focal length of 107.961.", + "a": "0.00926263275385168", + "valid": true, + "kind": "compute", + "expect": 0.00926263275385168, + "dtype": "quantity", + "unit": "1/m", + "why": "dioptric power for [107.960666] (the system dioptric power (Wikidata Q559265))" + }, + { + "q": "What is the dioptric power for focal length 22.652?", + "a": "0.044146167826513254", + "valid": true, + "kind": "compute", + "expect": 0.044146167826513254, + "dtype": "quantity", + "unit": "1/m", + "why": "dioptric power for [22.652023] (the system dioptric power (Wikidata Q559265))" + }, + { + "q": "Tell me the dioptric power with focal length of 22.652.", + "a": "0.044146167826513254", + "valid": true, + "kind": "compute", + "expect": 0.044146167826513254, + "dtype": "quantity", + "unit": "1/m", + "why": "dioptric power for [22.652023] (the system dioptric power (Wikidata Q559265))" + }, + { + "q": "What is the dioptric power for focal length 173.394?", + "a": "0.0057672242718762815", + "valid": true, + "kind": "compute", + "expect": 0.0057672242718762815, + "dtype": "quantity", + "unit": "1/m", + "why": "dioptric power for [173.393638] (the system dioptric power (Wikidata Q559265))" + }, + { + "q": "Tell me the dioptric power with focal length of 173.394.", + "a": "0.0057672242718762815", + "valid": true, + "kind": "compute", + "expect": 0.0057672242718762815, + "dtype": "quantity", + "unit": "1/m", + "why": "dioptric power for [173.393638] (the system dioptric power (Wikidata Q559265))" + }, + { + "q": "What is the dioptric power for focal length 126.709?", + "a": "0.007892083536070259", + "valid": true, + "kind": "compute", + "expect": 0.007892083536070259, + "dtype": "quantity", + "unit": "1/m", + "why": "dioptric power for [126.709252] (the system dioptric power (Wikidata Q559265))" + }, + { + "q": "Tell me the dioptric power with focal length of 126.709.", + "a": "0.007892083536070259", + "valid": true, + "kind": "compute", + "expect": 0.007892083536070259, + "dtype": "quantity", + "unit": "1/m", + "why": "dioptric power for [126.709252] (the system dioptric power (Wikidata Q559265))" + }, + { + "q": "What is the dioptric power for focal length 0.671697?", + "a": "1.4887660694527203", + "valid": true, + "kind": "compute", + "expect": 1.4887660694527203, + "dtype": "quantity", + "unit": "1/m", + "why": "dioptric power for [0.671697] (the system dioptric power (Wikidata Q559265))" + }, + { + "q": "Tell me the dioptric power with focal length of 0.671697.", + "a": "1.4887660694527203", + "valid": true, + "kind": "compute", + "expect": 1.4887660694527203, + "dtype": "quantity", + "unit": "1/m", + "why": "dioptric power for [0.671697] (the system dioptric power (Wikidata Q559265))" + }, + { + "q": "What is the dioptric power for focal length 1.16997?", + "a": "0.8547242274066585", + "valid": true, + "kind": "compute", + "expect": 0.8547242274066585, + "dtype": "quantity", + "unit": "1/m", + "why": "dioptric power for [1.169968] (the system dioptric power (Wikidata Q559265))" + }, + { + "q": "Tell me the dioptric power with focal length of 1.16997.", + "a": "0.8547242274066585", + "valid": true, + "kind": "compute", + "expect": 0.8547242274066585, + "dtype": "quantity", + "unit": "1/m", + "why": "dioptric power for [1.169968] (the system dioptric power (Wikidata Q559265))" + }, + { + "q": "What is the dioptric power?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the focal length is not given" + }, + { + "q": "Could you work out the dioptric power, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the focal length is not given" + }, + { + "q": "What is the dioptric power of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the dioptric power of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the dioptric power with focal length of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative focal length: no example had one" + }, + { + "q": "Which dioptric power is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the focal length is 2.57003, what is the dioptric power?", + "a": "0.38910052946214563", + "valid": true, + "kind": "compute", + "expect": 0.38910052946214563, + "dtype": "quantity", + "unit": "1/m", + "why": "dioptric power for [2.57003] (the system dioptric power (Wikidata Q559265))" + }, + { + "q": "Could you work out the dioptric power given focal length = 2.57003?", + "a": "0.38910052946214563", + "valid": true, + "kind": "compute", + "expect": 0.38910052946214563, + "dtype": "quantity", + "unit": "1/m", + "why": "dioptric power for [2.57003] (the system dioptric power (Wikidata Q559265))" + }, + { + "q": "I need the dioptric power for a focal length of 48.921, please.", + "a": "0.02044112317474344", + "valid": true, + "kind": "compute", + "expect": 0.02044112317474344, + "dtype": "quantity", + "unit": "1/m", + "why": "dioptric power for [48.920991] (the system dioptric power (Wikidata Q559265))" + }, + { + "q": "If the focal length is 48.921, what is the dioptric power?", + "a": "0.02044112317474344", + "valid": true, + "kind": "compute", + "expect": 0.02044112317474344, + "dtype": "quantity", + "unit": "1/m", + "why": "dioptric power for [48.920991] (the system dioptric power (Wikidata Q559265))" + }, + { + "q": "Could you work out the dioptric power given focal length = 0.348438?", + "a": "2.869953759769417", + "valid": true, + "kind": "compute", + "expect": 2.869953759769417, + "dtype": "quantity", + "unit": "1/m", + "why": "dioptric power for [0.348438] (the system dioptric power (Wikidata Q559265))" + }, + { + "q": "I need the dioptric power for a focal length of 0.348438, please.", + "a": "2.869953759769417", + "valid": true, + "kind": "compute", + "expect": 2.869953759769417, + "dtype": "quantity", + "unit": "1/m", + "why": "dioptric power for [0.348438] (the system dioptric power (Wikidata Q559265))" + }, + { + "q": "If the focal length is 252.709, what is the dioptric power?", + "a": "0.0039571202610184176", + "valid": true, + "kind": "compute", + "expect": 0.0039571202610184176, + "dtype": "quantity", + "unit": "1/m", + "why": "dioptric power for [252.709024] (the system dioptric power (Wikidata Q559265))" + }, + { + "q": "Could you work out the dioptric power given focal length = 252.709?", + "a": "0.0039571202610184176", + "valid": true, + "kind": "compute", + "expect": 0.0039571202610184176, + "dtype": "quantity", + "unit": "1/m", + "why": "dioptric power for [252.709024] (the system dioptric power (Wikidata Q559265))" + }, + { + "q": "I need the dioptric power for a focal length of 20.3966, please.", + "a": "0.04902788018766951", + "valid": true, + "kind": "compute", + "expect": 0.04902788018766951, + "dtype": "quantity", + "unit": "1/m", + "why": "dioptric power for [20.396558] (the system dioptric power (Wikidata Q559265))" + }, + { + "q": "If the focal length is 20.3966, what is the dioptric power?", + "a": "0.04902788018766951", + "valid": true, + "kind": "compute", + "expect": 0.04902788018766951, + "dtype": "quantity", + "unit": "1/m", + "why": "dioptric power for [20.396558] (the system dioptric power (Wikidata Q559265))" + }, + { + "q": "Could you work out the dioptric power given focal length = 134.993?", + "a": "0.007407793842889246", + "valid": true, + "kind": "compute", + "expect": 0.007407793842889246, + "dtype": "quantity", + "unit": "1/m", + "why": "dioptric power for [134.992958] (the system dioptric power (Wikidata Q559265))" + }, + { + "q": "I need the dioptric power for a focal length of 134.993, please.", + "a": "0.007407793842889246", + "valid": true, + "kind": "compute", + "expect": 0.007407793842889246, + "dtype": "quantity", + "unit": "1/m", + "why": "dioptric power for [134.992958] (the system dioptric power (Wikidata Q559265))" + }, + { + "q": "I need the dioptric power, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the focal length is not given" + }, + { + "q": "Can you tell me the dioptric power?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the focal length is not given" + }, + { + "q": "Do you know the dioptric power of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the dioptric power of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the dioptric power given focal length = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative focal length: no example had one" + }, + { + "q": "What is a typical dioptric power?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-dioptric-power/program.py b/root/organs10/physics/specialists/solved-dioptric-power/program.py new file mode 100644 index 0000000000000000000000000000000000000000..481cd02749a4e1caf0a52b65f91c494fe7b956b8 --- /dev/null +++ b/root/organs10/physics/specialists/solved-dioptric-power/program.py @@ -0,0 +1,7 @@ +"""dioptric power from focal length (dioptric power (Wikidata Q559265)) +The maths organ (systems.py): solved from the system q0 = ((1)/(q1)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(focal_length): + return 1.0/focal_length diff --git a/root/organs10/physics/specialists/solved-dioptric-power/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-dioptric-power/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-dioptric-power/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-displacement-current-density/organ_formula_v2.py b/root/organs10/physics/specialists/solved-displacement-current-density/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-displacement-current-density/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-displacement-current-density/panel.json b/root/organs10/physics/specialists/solved-displacement-current-density/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..0613cbf51492aec8bba4126965b3d03d3ade175d --- /dev/null +++ b/root/organs10/physics/specialists/solved-displacement-current-density/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the displacement current density for total current density 107.961 and electric current density 59.4105?", + "a": "48.55016569879068", + "valid": true, + "kind": "compute", + "expect": 48.55016569879068, + "dtype": "quantity", + "unit": "A/m^2", + "why": "displacement current density for [107.960666, 59.4105] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "Tell me the displacement current density with total current density of 107.961 and electric current density of 59.4105.", + "a": "48.55016569879068", + "valid": true, + "kind": "compute", + "expect": 48.55016569879068, + "dtype": "quantity", + "unit": "A/m^2", + "why": "displacement current density for [107.960666, 59.4105] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "What is the displacement current density for total current density 157.223 and electric current density 35.6412?", + "a": "121.5814030054458", + "valid": true, + "kind": "compute", + "expect": 121.5814030054458, + "dtype": "quantity", + "unit": "A/m^2", + "why": "displacement current density for [157.22256, 35.641157] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "Tell me the displacement current density with total current density of 157.223 and electric current density of 35.6412.", + "a": "121.5814030054458", + "valid": true, + "kind": "compute", + "expect": 121.5814030054458, + "dtype": "quantity", + "unit": "A/m^2", + "why": "displacement current density for [157.22256, 35.641157] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "What is the displacement current density for total current density 126.709 and electric current density 1.70503?", + "a": "125.00421942160824", + "valid": true, + "kind": "compute", + "expect": 125.00421942160824, + "dtype": "quantity", + "unit": "A/m^2", + "why": "displacement current density for [126.709252, 1.705032] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "Tell me the displacement current density with total current density of 126.709 and electric current density of 1.70503.", + "a": "125.00421942160824", + "valid": true, + "kind": "compute", + "expect": 125.00421942160824, + "dtype": "quantity", + "unit": "A/m^2", + "why": "displacement current density for [126.709252, 1.705032] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "What is the displacement current density for total current density 41.6566 and electric current density 13.8779?", + "a": "27.77877186905885", + "valid": true, + "kind": "compute", + "expect": 27.77877186905885, + "dtype": "quantity", + "unit": "A/m^2", + "why": "displacement current density for [41.656639, 13.877867] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "Tell me the displacement current density with total current density of 41.6566 and electric current density of 13.8779.", + "a": "27.77877186905885", + "valid": true, + "kind": "compute", + "expect": 27.77877186905885, + "dtype": "quantity", + "unit": "A/m^2", + "why": "displacement current density for [41.656639, 13.877867] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "What is the displacement current density for total current density 8.50341 and electric current density 0.58813?", + "a": "7.91527752848457", + "valid": true, + "kind": "compute", + "expect": 7.91527752848457, + "dtype": "quantity", + "unit": "A/m^2", + "why": "displacement current density for [8.503407, 0.58813] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "Tell me the displacement current density with total current density of 8.50341 and electric current density of 0.58813.", + "a": "7.91527752848457", + "valid": true, + "kind": "compute", + "expect": 7.91527752848457, + "dtype": "quantity", + "unit": "A/m^2", + "why": "displacement current density for [8.503407, 0.58813] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "What is the displacement current density for total current density 223.509 and electric current density 17.3414?", + "a": "206.1673613150397", + "valid": true, + "kind": "compute", + "expect": 206.1673613150397, + "dtype": "quantity", + "unit": "A/m^2", + "why": "displacement current density for [223.508796, 17.341435] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "Tell me the displacement current density with total current density of 223.509 and electric current density of 17.3414.", + "a": "206.1673613150397", + "valid": true, + "kind": "compute", + "expect": 206.1673613150397, + "dtype": "quantity", + "unit": "A/m^2", + "why": "displacement current density for [223.508796, 17.341435] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "What is the displacement current density for total current density 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current density is not given" + }, + { + "q": "Tell me the displacement current density with total current density of 157.223.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current density is not given" + }, + { + "q": "What is the displacement current density of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the displacement current density of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the displacement current density with total current density of -126.709 and electric current density of 1.70503.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative total current density: no example had one" + }, + { + "q": "Which displacement current density is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the total current density is 167.625 and the electric current density is 10.3291, what is the displacement current density?", + "a": "157.29589332353083", + "valid": true, + "kind": "compute", + "expect": 157.29589332353083, + "dtype": "quantity", + "unit": "A/m^2", + "why": "displacement current density for [167.624948, 10.329054] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "Could you work out the displacement current density given total current density = 167.625, electric current density = 10.3291?", + "a": "157.29589332353083", + "valid": true, + "kind": "compute", + "expect": 157.29589332353083, + "dtype": "quantity", + "unit": "A/m^2", + "why": "displacement current density for [167.624948, 10.329054] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "I need the displacement current density for a total current density of 13.999 and an electric current density of 0.348438, please.", + "a": "13.650591275493905", + "valid": true, + "kind": "compute", + "expect": 13.650591275493905, + "dtype": "quantity", + "unit": "A/m^2", + "why": "displacement current density for [13.999029, 0.348438] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "If the total current density is 13.999 and the electric current density is 0.348438, what is the displacement current density?", + "a": "13.650591275493905", + "valid": true, + "kind": "compute", + "expect": 13.650591275493905, + "dtype": "quantity", + "unit": "A/m^2", + "why": "displacement current density for [13.999029, 0.348438] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "Could you work out the displacement current density given total current density = 81.1967, electric current density = 6.98086?", + "a": "74.21584883614992", + "valid": true, + "kind": "compute", + "expect": 74.21584883614992, + "dtype": "quantity", + "unit": "A/m^2", + "why": "displacement current density for [81.196705, 6.980857] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "I need the displacement current density for a total current density of 81.1967 and an electric current density of 6.98086, please.", + "a": "74.21584883614992", + "valid": true, + "kind": "compute", + "expect": 74.21584883614992, + "dtype": "quantity", + "unit": "A/m^2", + "why": "displacement current density for [81.196705, 6.980857] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "If the total current density is 19.4474 and the electric current density is 4.51548, what is the displacement current density?", + "a": "14.931881955429075", + "valid": true, + "kind": "compute", + "expect": 14.931881955429075, + "dtype": "quantity", + "unit": "A/m^2", + "why": "displacement current density for [19.447357, 4.515475] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "Could you work out the displacement current density given total current density = 19.4474, electric current density = 4.51548?", + "a": "14.931881955429075", + "valid": true, + "kind": "compute", + "expect": 14.931881955429075, + "dtype": "quantity", + "unit": "A/m^2", + "why": "displacement current density for [19.447357, 4.515475] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "I need the displacement current density for a total current density of 13.2373 and an electric current density of 4.71941, please.", + "a": "8.517896269006052", + "valid": true, + "kind": "compute", + "expect": 8.517896269006052, + "dtype": "quantity", + "unit": "A/m^2", + "why": "displacement current density for [13.237311, 4.719415] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "If the total current density is 13.2373 and the electric current density is 4.71941, what is the displacement current density?", + "a": "8.517896269006052", + "valid": true, + "kind": "compute", + "expect": 8.517896269006052, + "dtype": "quantity", + "unit": "A/m^2", + "why": "displacement current density for [13.237311, 4.719415] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "Could you work out the displacement current density given total current density = 21.7946, electric current density = 9.1061?", + "a": "12.68851669739933", + "valid": true, + "kind": "compute", + "expect": 12.68851669739933, + "dtype": "quantity", + "unit": "A/m^2", + "why": "displacement current density for [21.794621, 9.106104] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "I need the displacement current density for a total current density of 21.7946 and an electric current density of 9.1061, please.", + "a": "12.68851669739933", + "valid": true, + "kind": "compute", + "expect": 12.68851669739933, + "dtype": "quantity", + "unit": "A/m^2", + "why": "displacement current density for [21.794621, 9.106104] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "If the total current density is 167.625, what is the displacement current density?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current density is not given" + }, + { + "q": "Could you work out the displacement current density given total current density = 13.999?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current density is not given" + }, + { + "q": "Do you know the displacement current density of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the displacement current density of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the displacement current density given total current density = -81.1967, electric current density = 6.98086?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative total current density: no example had one" + }, + { + "q": "What is a typical displacement current density?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-displacement-current-density/program.py b/root/organs10/physics/specialists/solved-displacement-current-density/program.py new file mode 100644 index 0000000000000000000000000000000000000000..eb337d8b7143e6b70c0512697e6032f6b5ed470e --- /dev/null +++ b/root/organs10/physics/specialists/solved-displacement-current-density/program.py @@ -0,0 +1,7 @@ +"""displacement current density from total current density, electric current density (total current density (Wikidata Q77680811)) +The maths organ (systems.py): solved from the system q0 = q2 + q1; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(total_current_density, electric_current_density): + return -1.0*electric_current_density + 1.0*total_current_density diff --git a/root/organs10/physics/specialists/solved-displacement-current-density/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-displacement-current-density/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-displacement-current-density/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-distance-10/organ_formula_v2.py b/root/organs10/physics/specialists/solved-distance-10/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-10/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-distance-10/panel.json b/root/organs10/physics/specialists/solved-distance-10/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..b08846e46e1bfb4e2b9cec250ede4b8aed38806d --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-10/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the distance for speed 107.961 and duration 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "m", + "why": "distance for [107.960666, 59.4105] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Tell me the distance with speed of 107.961 and duration of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "m", + "why": "distance for [107.960666, 59.4105] (the system speed (Wikidata Q3711325))" + }, + { + "q": "What is the distance for speed 169.527 and duration 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "m", + "why": "distance for [169.526958, 280.773702] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Tell me the distance with speed of 169.527 and duration of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "m", + "why": "distance for [169.526958, 280.773702] (the system speed (Wikidata Q3711325))" + }, + { + "q": "What is the distance for speed 1.91195 and duration 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "m", + "why": "distance for [1.911952, 82.240072] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Tell me the distance with speed of 1.91195 and duration of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "m", + "why": "distance for [1.911952, 82.240072] (the system speed (Wikidata Q3711325))" + }, + { + "q": "What is the distance for speed 1.64379 and duration 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "m", + "why": "distance for [1.643795, 252.709024] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Tell me the distance with speed of 1.64379 and duration of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "m", + "why": "distance for [1.643795, 252.709024] (the system speed (Wikidata Q3711325))" + }, + { + "q": "What is the distance for speed 18.3169 and duration 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "m", + "why": "distance for [18.316867, 6.838598] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Tell me the distance with speed of 18.3169 and duration of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "m", + "why": "distance for [18.316867, 6.838598] (the system speed (Wikidata Q3711325))" + }, + { + "q": "What is the distance for speed 156.502 and duration 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "m", + "why": "distance for [156.50181, 185.886273] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Tell me the distance with speed of 156.502 and duration of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "m", + "why": "distance for [156.50181, 185.886273] (the system speed (Wikidata Q3711325))" + }, + { + "q": "What is the distance for speed 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the duration is not given" + }, + { + "q": "Tell me the distance with speed of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the duration is not given" + }, + { + "q": "What is the distance of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the distance of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the distance with speed of -1.91195 and duration of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative speed: no example had one" + }, + { + "q": "Which distance is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the speed is 8.50728 and the duration is 17.7888, what is the distance?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "m", + "why": "distance for [8.507283, 17.788778] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Could you work out the distance given speed = 8.50728, duration = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "m", + "why": "distance for [8.507283, 17.788778] (the system speed (Wikidata Q3711325))" + }, + { + "q": "I need the distance for a speed of 8.24951 and a duration of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "m", + "why": "distance for [8.24951, 0.634021] (the system speed (Wikidata Q3711325))" + }, + { + "q": "If the speed is 8.24951 and the duration is 0.634021, what is the distance?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "m", + "why": "distance for [8.24951, 0.634021] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Could you work out the distance given speed = 0.318734, duration = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.318734, 9.566071] (the system speed (Wikidata Q3711325))" + }, + { + "q": "I need the distance for a speed of 0.318734 and a duration of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.318734, 9.566071] (the system speed (Wikidata Q3711325))" + }, + { + "q": "If the speed is 0.671697 and the duration is 14.2496, what is the distance?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.671697, 14.249569] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Could you work out the distance given speed = 0.671697, duration = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.671697, 14.249569] (the system speed (Wikidata Q3711325))" + }, + { + "q": "I need the distance for a speed of 21.7931 and a duration of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "m", + "why": "distance for [21.793126, 29.510609] (the system speed (Wikidata Q3711325))" + }, + { + "q": "If the speed is 21.7931 and the duration is 29.5106, what is the distance?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "m", + "why": "distance for [21.793126, 29.510609] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Could you work out the distance given speed = 153.15, duration = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "m", + "why": "distance for [153.149965, 18.595315] (the system speed (Wikidata Q3711325))" + }, + { + "q": "I need the distance for a speed of 153.15 and a duration of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "m", + "why": "distance for [153.149965, 18.595315] (the system speed (Wikidata Q3711325))" + }, + { + "q": "If the speed is 8.50728, what is the distance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the duration is not given" + }, + { + "q": "Could you work out the distance given speed = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the duration is not given" + }, + { + "q": "Do you know the distance of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the distance of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the distance given speed = -0.318734, duration = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative speed: no example had one" + }, + { + "q": "What is a typical distance?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-distance-10/program.py b/root/organs10/physics/specialists/solved-distance-10/program.py new file mode 100644 index 0000000000000000000000000000000000000000..2a4da695439859d86917abe361417f48043da605 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-10/program.py @@ -0,0 +1,7 @@ +"""distance from speed, duration (speed (Wikidata Q3711325)) +The maths organ (systems.py): solved from the system q0 = q1 / q2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(speed, duration): + return 1.0*duration*speed diff --git a/root/organs10/physics/specialists/solved-distance-10/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-distance-10/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-10/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-distance-11/organ_formula_v2.py b/root/organs10/physics/specialists/solved-distance-11/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-11/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-distance-11/panel.json b/root/organs10/physics/specialists/solved-distance-11/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..cd77c95b55b2e949bc022363e42cf8b9a0bd1d58 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-11/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the distance for shear rate 107.961 and speed 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "m", + "why": "distance for [107.960666, 59.4105] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Tell me the distance with shear rate of 107.961 and speed of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "m", + "why": "distance for [107.960666, 59.4105] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "What is the distance for shear rate 169.527 and speed 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "m", + "why": "distance for [169.526958, 280.773702] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Tell me the distance with shear rate of 169.527 and speed of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "m", + "why": "distance for [169.526958, 280.773702] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "What is the distance for shear rate 1.91195 and speed 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "m", + "why": "distance for [1.911952, 82.240072] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Tell me the distance with shear rate of 1.91195 and speed of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "m", + "why": "distance for [1.911952, 82.240072] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "What is the distance for shear rate 1.64379 and speed 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "m", + "why": "distance for [1.643795, 252.709024] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Tell me the distance with shear rate of 1.64379 and speed of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "m", + "why": "distance for [1.643795, 252.709024] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "What is the distance for shear rate 18.3169 and speed 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "m", + "why": "distance for [18.316867, 6.838598] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Tell me the distance with shear rate of 18.3169 and speed of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "m", + "why": "distance for [18.316867, 6.838598] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "What is the distance for shear rate 156.502 and speed 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "m", + "why": "distance for [156.50181, 185.886273] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Tell me the distance with shear rate of 156.502 and speed of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "m", + "why": "distance for [156.50181, 185.886273] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "What is the distance for shear rate 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Tell me the distance with shear rate of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "What is the distance of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the distance of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the distance with shear rate of -1.91195 and speed of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative shear rate: no example had one" + }, + { + "q": "Which distance is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the shear rate is 8.50728 and the speed is 17.7888, what is the distance?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "m", + "why": "distance for [8.507283, 17.788778] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Could you work out the distance given shear rate = 8.50728, speed = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "m", + "why": "distance for [8.507283, 17.788778] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "I need the distance for a shear rate of 8.24951 and a speed of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "m", + "why": "distance for [8.24951, 0.634021] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "If the shear rate is 8.24951 and the speed is 0.634021, what is the distance?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "m", + "why": "distance for [8.24951, 0.634021] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Could you work out the distance given shear rate = 0.318734, speed = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.318734, 9.566071] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "I need the distance for a shear rate of 0.318734 and a speed of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.318734, 9.566071] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "If the shear rate is 0.671697 and the speed is 14.2496, what is the distance?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.671697, 14.249569] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Could you work out the distance given shear rate = 0.671697, speed = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.671697, 14.249569] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "I need the distance for a shear rate of 21.7931 and a speed of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "m", + "why": "distance for [21.793126, 29.510609] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "If the shear rate is 21.7931 and the speed is 29.5106, what is the distance?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "m", + "why": "distance for [21.793126, 29.510609] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Could you work out the distance given shear rate = 153.15, speed = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "m", + "why": "distance for [153.149965, 18.595315] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "I need the distance for a shear rate of 153.15 and a speed of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "m", + "why": "distance for [153.149965, 18.595315] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "If the shear rate is 8.50728, what is the distance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Could you work out the distance given shear rate = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Do you know the distance of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the distance of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the distance given shear rate = -0.318734, speed = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative shear rate: no example had one" + }, + { + "q": "What is a typical distance?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-distance-11/program.py b/root/organs10/physics/specialists/solved-distance-11/program.py new file mode 100644 index 0000000000000000000000000000000000000000..bf0ac267f42d89aa76323c9688f78cccd7cd2aee --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-11/program.py @@ -0,0 +1,7 @@ +"""distance from shear rate, speed (shear rate (Wikidata Q461293)) +The maths organ (systems.py): solved from the system q0 = ((q1)/(q2)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(shear_rate, speed): + return 1.0*speed/shear_rate diff --git a/root/organs10/physics/specialists/solved-distance-11/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-distance-11/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-11/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-distance-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-distance-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-2/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-distance-2/panel.json b/root/organs10/physics/specialists/solved-distance-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..bb9398d2f082716dfb39f05bd135b701eefadd56 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the distance for final speed 107.961 and acceleration 59.4105?", + "a": "98.09297465851051", + "valid": true, + "kind": "compute", + "expect": 98.09297465851051, + "dtype": "quantity", + "unit": "m", + "why": "distance for [107.960666, 59.4105] (the system Uniform acceleration)" + }, + { + "q": "Tell me the distance with final speed of 107.961 and acceleration of 59.4105.", + "a": "98.09297465851051", + "valid": true, + "kind": "compute", + "expect": 98.09297465851051, + "dtype": "quantity", + "unit": "m", + "why": "distance for [107.960666, 59.4105] (the system Uniform acceleration)" + }, + { + "q": "What is the distance for final speed 173.394 and acceleration 251.084?", + "a": "59.871097659806686", + "valid": true, + "kind": "compute", + "expect": 59.871097659806686, + "dtype": "quantity", + "unit": "m", + "why": "distance for [173.393638, 251.084037] (the system Uniform acceleration)" + }, + { + "q": "Tell me the distance with final speed of 173.394 and acceleration of 251.084.", + "a": "59.871097659806686", + "valid": true, + "kind": "compute", + "expect": 59.871097659806686, + "dtype": "quantity", + "unit": "m", + "why": "distance for [173.393638, 251.084037] (the system Uniform acceleration)" + }, + { + "q": "What is the distance for final speed 0.671697 and acceleration 14.2496?", + "a": "0.015831254903498907", + "valid": true, + "kind": "compute", + "expect": 0.015831254903498907, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.671697, 14.249569] (the system Uniform acceleration)" + }, + { + "q": "Tell me the distance with final speed of 0.671697 and acceleration of 14.2496.", + "a": "0.015831254903498907", + "valid": true, + "kind": "compute", + "expect": 0.015831254903498907, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.671697, 14.249569] (the system Uniform acceleration)" + }, + { + "q": "What is the distance for final speed 41.2905 and acceleration 2.12277?", + "a": "401.57626816912267", + "valid": true, + "kind": "compute", + "expect": 401.57626816912267, + "dtype": "quantity", + "unit": "m", + "why": "distance for [41.290505, 2.122767] (the system Uniform acceleration)" + }, + { + "q": "Tell me the distance with final speed of 41.2905 and acceleration of 2.12277.", + "a": "401.57626816912267", + "valid": true, + "kind": "compute", + "expect": 401.57626816912267, + "dtype": "quantity", + "unit": "m", + "why": "distance for [41.290505, 2.122767] (the system Uniform acceleration)" + }, + { + "q": "What is the distance for final speed 0.432217 and acceleration 16.6571?", + "a": "0.005607590627078495", + "valid": true, + "kind": "compute", + "expect": 0.005607590627078495, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.432217, 16.657051] (the system Uniform acceleration)" + }, + { + "q": "Tell me the distance with final speed of 0.432217 and acceleration of 16.6571.", + "a": "0.005607590627078495", + "valid": true, + "kind": "compute", + "expect": 0.005607590627078495, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.432217, 16.657051] (the system Uniform acceleration)" + }, + { + "q": "What is the distance for final speed 10.2544 and acceleration 0.415084?", + "a": "126.6639170482232", + "valid": true, + "kind": "compute", + "expect": 126.6639170482232, + "dtype": "quantity", + "unit": "m", + "why": "distance for [10.254383, 0.415084] (the system Uniform acceleration)" + }, + { + "q": "Tell me the distance with final speed of 10.2544 and acceleration of 0.415084.", + "a": "126.6639170482232", + "valid": true, + "kind": "compute", + "expect": 126.6639170482232, + "dtype": "quantity", + "unit": "m", + "why": "distance for [10.254383, 0.415084] (the system Uniform acceleration)" + }, + { + "q": "What is the distance for final speed 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the acceleration is not given" + }, + { + "q": "Tell me the distance with final speed of 173.394.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the acceleration is not given" + }, + { + "q": "What is the distance of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the distance of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the distance with final speed of -0.671697 and acceleration of 14.2496.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative final speed: no example had one" + }, + { + "q": "Which distance is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the final speed is 22.652 and the acceleration is 1.78451, what is the distance?", + "a": "143.7689034458718", + "valid": true, + "kind": "compute", + "expect": 143.7689034458718, + "dtype": "quantity", + "unit": "m", + "why": "distance for [22.652023, 1.78451] (the system Uniform acceleration)" + }, + { + "q": "Could you work out the distance given final speed = 22.652, acceleration = 1.78451?", + "a": "143.7689034458718", + "valid": true, + "kind": "compute", + "expect": 143.7689034458718, + "dtype": "quantity", + "unit": "m", + "why": "distance for [22.652023, 1.78451] (the system Uniform acceleration)" + }, + { + "q": "I need the distance for a final speed of 126.709 and an acceleration of 1.70503, please.", + "a": "4708.190499346569", + "valid": true, + "kind": "compute", + "expect": 4708.190499346569, + "dtype": "quantity", + "unit": "m", + "why": "distance for [126.709252, 1.705032] (the system Uniform acceleration)" + }, + { + "q": "If the final speed is 126.709 and the acceleration is 1.70503, what is the distance?", + "a": "4708.190499346569", + "valid": true, + "kind": "compute", + "expect": 4708.190499346569, + "dtype": "quantity", + "unit": "m", + "why": "distance for [126.709252, 1.705032] (the system Uniform acceleration)" + }, + { + "q": "Could you work out the distance given final speed = 1.16997, acceleration = 1.14865?", + "a": "0.5958396377037419", + "valid": true, + "kind": "compute", + "expect": 0.5958396377037419, + "dtype": "quantity", + "unit": "m", + "why": "distance for [1.169968, 1.148652] (the system Uniform acceleration)" + }, + { + "q": "I need the distance for a final speed of 1.16997 and an acceleration of 1.14865, please.", + "a": "0.5958396377037419", + "valid": true, + "kind": "compute", + "expect": 0.5958396377037419, + "dtype": "quantity", + "unit": "m", + "why": "distance for [1.169968, 1.148652] (the system Uniform acceleration)" + }, + { + "q": "If the final speed is 21.7946 and the acceleration is 9.1061, what is the distance?", + "a": "26.08170801514793", + "valid": true, + "kind": "compute", + "expect": 26.08170801514793, + "dtype": "quantity", + "unit": "m", + "why": "distance for [21.794621, 9.106104] (the system Uniform acceleration)" + }, + { + "q": "Could you work out the distance given final speed = 21.7946, acceleration = 9.1061?", + "a": "26.08170801514793", + "valid": true, + "kind": "compute", + "expect": 26.08170801514793, + "dtype": "quantity", + "unit": "m", + "why": "distance for [21.794621, 9.106104] (the system Uniform acceleration)" + }, + { + "q": "I need the distance for a final speed of 241.598 and an acceleration of 1.13479, please.", + "a": "25718.276525156583", + "valid": true, + "kind": "compute", + "expect": 25718.276525156583, + "dtype": "quantity", + "unit": "m", + "why": "distance for [241.598281, 1.134791] (the system Uniform acceleration)" + }, + { + "q": "If the final speed is 241.598 and the acceleration is 1.13479, what is the distance?", + "a": "25718.276525156583", + "valid": true, + "kind": "compute", + "expect": 25718.276525156583, + "dtype": "quantity", + "unit": "m", + "why": "distance for [241.598281, 1.134791] (the system Uniform acceleration)" + }, + { + "q": "Could you work out the distance given final speed = 108.711, acceleration = 3.36463?", + "a": "1756.2241436432928", + "valid": true, + "kind": "compute", + "expect": 1756.2241436432928, + "dtype": "quantity", + "unit": "m", + "why": "distance for [108.711073, 3.364632] (the system Uniform acceleration)" + }, + { + "q": "I need the distance for a final speed of 108.711 and an acceleration of 3.36463, please.", + "a": "1756.2241436432928", + "valid": true, + "kind": "compute", + "expect": 1756.2241436432928, + "dtype": "quantity", + "unit": "m", + "why": "distance for [108.711073, 3.364632] (the system Uniform acceleration)" + }, + { + "q": "If the final speed is 22.652, what is the distance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the acceleration is not given" + }, + { + "q": "Could you work out the distance given final speed = 126.709?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the acceleration is not given" + }, + { + "q": "Do you know the distance of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the distance of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the distance given final speed = -1.16997, acceleration = 1.14865?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative final speed: no example had one" + }, + { + "q": "What is a typical distance?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-distance-2/program.py b/root/organs10/physics/specialists/solved-distance-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..d14a2beac7b0c87b37807d9530b2dd748a74628b --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-2/program.py @@ -0,0 +1,7 @@ +"""distance from final speed, acceleration (Uniform acceleration) +The maths organ (systems.py): solved from the system u = a*t; s = a*t**2/2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(final_speed, acceleration): + return 0.5*final_speed**2/acceleration diff --git a/root/organs10/physics/specialists/solved-distance-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-distance-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-2/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-distance-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-distance-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-3/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-distance-3/panel.json b/root/organs10/physics/specialists/solved-distance-3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..bbdfc8a3d4f82c66ac84a717673c055f5f4ea21a --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the distance for final speed 107.961 and time 59.4105?", + "a": "3206.9985807472867", + "valid": true, + "kind": "compute", + "expect": 3206.9985807472867, + "dtype": "quantity", + "unit": "m", + "why": "distance for [107.960666, 59.4105] (the system Uniform acceleration)" + }, + { + "q": "Tell me the distance with final speed of 107.961 and time of 59.4105.", + "a": "3206.9985807472867", + "valid": true, + "kind": "compute", + "expect": 3206.9985807472867, + "dtype": "quantity", + "unit": "m", + "why": "distance for [107.960666, 59.4105] (the system Uniform acceleration)" + }, + { + "q": "What is the distance for final speed 173.394 and time 251.084?", + "a": "21768.187388942482", + "valid": true, + "kind": "compute", + "expect": 21768.187388942482, + "dtype": "quantity", + "unit": "m", + "why": "distance for [173.393638, 251.084037] (the system Uniform acceleration)" + }, + { + "q": "Tell me the distance with final speed of 173.394 and time of 251.084.", + "a": "21768.187388942482", + "valid": true, + "kind": "compute", + "expect": 21768.187388942482, + "dtype": "quantity", + "unit": "m", + "why": "distance for [173.393638, 251.084037] (the system Uniform acceleration)" + }, + { + "q": "What is the distance for final speed 0.671697 and time 14.2496?", + "a": "4.785697903894181", + "valid": true, + "kind": "compute", + "expect": 4.785697903894181, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.671697, 14.249569] (the system Uniform acceleration)" + }, + { + "q": "Tell me the distance with final speed of 0.671697 and time of 14.2496.", + "a": "4.785697903894181", + "valid": true, + "kind": "compute", + "expect": 4.785697903894181, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.671697, 14.249569] (the system Uniform acceleration)" + }, + { + "q": "What is the distance for final speed 41.2905 and time 2.12277?", + "a": "43.82506489272933", + "valid": true, + "kind": "compute", + "expect": 43.82506489272933, + "dtype": "quantity", + "unit": "m", + "why": "distance for [41.290505, 2.122767] (the system Uniform acceleration)" + }, + { + "q": "Tell me the distance with final speed of 41.2905 and time of 2.12277.", + "a": "43.82506489272933", + "valid": true, + "kind": "compute", + "expect": 43.82506489272933, + "dtype": "quantity", + "unit": "m", + "why": "distance for [41.290505, 2.122767] (the system Uniform acceleration)" + }, + { + "q": "What is the distance for final speed 0.432217 and time 16.6571?", + "a": "3.599733239556981", + "valid": true, + "kind": "compute", + "expect": 3.599733239556981, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.432217, 16.657051] (the system Uniform acceleration)" + }, + { + "q": "Tell me the distance with final speed of 0.432217 and time of 16.6571.", + "a": "3.599733239556981", + "valid": true, + "kind": "compute", + "expect": 3.599733239556981, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.432217, 16.657051] (the system Uniform acceleration)" + }, + { + "q": "What is the distance for final speed 10.2544 and time 0.415084?", + "a": "2.1282156928002545", + "valid": true, + "kind": "compute", + "expect": 2.1282156928002545, + "dtype": "quantity", + "unit": "m", + "why": "distance for [10.254383, 0.415084] (the system Uniform acceleration)" + }, + { + "q": "Tell me the distance with final speed of 10.2544 and time of 0.415084.", + "a": "2.1282156928002545", + "valid": true, + "kind": "compute", + "expect": 2.1282156928002545, + "dtype": "quantity", + "unit": "m", + "why": "distance for [10.254383, 0.415084] (the system Uniform acceleration)" + }, + { + "q": "What is the distance for final speed 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "Tell me the distance with final speed of 173.394.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "What is the distance of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the distance of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the distance with final speed of -0.671697 and time of 14.2496.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative final speed: no example had one" + }, + { + "q": "Which distance is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the final speed is 22.652 and the time is 1.78451, what is the distance?", + "a": "20.21138237365017", + "valid": true, + "kind": "compute", + "expect": 20.21138237365017, + "dtype": "quantity", + "unit": "m", + "why": "distance for [22.652023, 1.78451] (the system Uniform acceleration)" + }, + { + "q": "Could you work out the distance given final speed = 22.652, time = 1.78451?", + "a": "20.21138237365017", + "valid": true, + "kind": "compute", + "expect": 20.21138237365017, + "dtype": "quantity", + "unit": "m", + "why": "distance for [22.652023, 1.78451] (the system Uniform acceleration)" + }, + { + "q": "I need the distance for a final speed of 126.709 and a time of 1.70503, please.", + "a": "108.02168849210678", + "valid": true, + "kind": "compute", + "expect": 108.02168849210678, + "dtype": "quantity", + "unit": "m", + "why": "distance for [126.709252, 1.705032] (the system Uniform acceleration)" + }, + { + "q": "If the final speed is 126.709 and the time is 1.70503, what is the distance?", + "a": "108.02168849210678", + "valid": true, + "kind": "compute", + "expect": 108.02168849210678, + "dtype": "quantity", + "unit": "m", + "why": "distance for [126.709252, 1.705032] (the system Uniform acceleration)" + }, + { + "q": "Could you work out the distance given final speed = 1.16997, time = 1.14865?", + "a": "0.6719432177734406", + "valid": true, + "kind": "compute", + "expect": 0.6719432177734406, + "dtype": "quantity", + "unit": "m", + "why": "distance for [1.169968, 1.148652] (the system Uniform acceleration)" + }, + { + "q": "I need the distance for a final speed of 1.16997 and a time of 1.14865, please.", + "a": "0.6719432177734406", + "valid": true, + "kind": "compute", + "expect": 0.6719432177734406, + "dtype": "quantity", + "unit": "m", + "why": "distance for [1.169968, 1.148652] (the system Uniform acceleration)" + }, + { + "q": "If the final speed is 21.7946 and the time is 9.1061, what is the distance?", + "a": "99.23204131294771", + "valid": true, + "kind": "compute", + "expect": 99.23204131294771, + "dtype": "quantity", + "unit": "m", + "why": "distance for [21.794621, 9.106104] (the system Uniform acceleration)" + }, + { + "q": "Could you work out the distance given final speed = 21.7946, time = 9.1061?", + "a": "99.23204131294771", + "valid": true, + "kind": "compute", + "expect": 99.23204131294771, + "dtype": "quantity", + "unit": "m", + "why": "distance for [21.794621, 9.106104] (the system Uniform acceleration)" + }, + { + "q": "I need the distance for a final speed of 241.598 and a time of 1.13479, please.", + "a": "137.0817592183478", + "valid": true, + "kind": "compute", + "expect": 137.0817592183478, + "dtype": "quantity", + "unit": "m", + "why": "distance for [241.598281, 1.134791] (the system Uniform acceleration)" + }, + { + "q": "If the final speed is 241.598 and the time is 1.13479, what is the distance?", + "a": "137.0817592183478", + "valid": true, + "kind": "compute", + "expect": 137.0817592183478, + "dtype": "quantity", + "unit": "m", + "why": "distance for [241.598281, 1.134791] (the system Uniform acceleration)" + }, + { + "q": "Could you work out the distance given final speed = 108.711, time = 3.36463?", + "a": "182.88639894060165", + "valid": true, + "kind": "compute", + "expect": 182.88639894060165, + "dtype": "quantity", + "unit": "m", + "why": "distance for [108.711073, 3.364632] (the system Uniform acceleration)" + }, + { + "q": "I need the distance for a final speed of 108.711 and a time of 3.36463, please.", + "a": "182.88639894060165", + "valid": true, + "kind": "compute", + "expect": 182.88639894060165, + "dtype": "quantity", + "unit": "m", + "why": "distance for [108.711073, 3.364632] (the system Uniform acceleration)" + }, + { + "q": "If the final speed is 22.652, what is the distance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "Could you work out the distance given final speed = 126.709?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "Do you know the distance of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the distance of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the distance given final speed = -1.16997, time = 1.14865?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative final speed: no example had one" + }, + { + "q": "What is a typical distance?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-distance-3/program.py b/root/organs10/physics/specialists/solved-distance-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..35acb5b7592c8febbb6ebe4af829e4a669331dd2 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-3/program.py @@ -0,0 +1,7 @@ +"""distance from final speed, time (Uniform acceleration) +The maths organ (systems.py): solved from the system u = a*t; s = a*t**2/2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(final_speed, time): + return 0.5*final_speed*time diff --git a/root/organs10/physics/specialists/solved-distance-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-distance-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-3/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-distance-4/organ_formula_v2.py b/root/organs10/physics/specialists/solved-distance-4/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-4/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-distance-4/panel.json b/root/organs10/physics/specialists/solved-distance-4/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..27dcc07742d31a5e8f95d07092950aafddce87d1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-4/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the distance for acceleration 107.961 and time 59.4105?", + "a": "190529.38979457287", + "valid": true, + "kind": "compute", + "expect": 190529.38979457287, + "dtype": "quantity", + "unit": "m", + "why": "distance for [107.960666, 59.4105] (the system Uniform acceleration)" + }, + { + "q": "Tell me the distance with acceleration of 107.961 and time of 59.4105.", + "a": "190529.38979457287", + "valid": true, + "kind": "compute", + "expect": 190529.38979457287, + "dtype": "quantity", + "unit": "m", + "why": "distance for [107.960666, 59.4105] (the system Uniform acceleration)" + }, + { + "q": "What is the distance for acceleration 173.394 and time 251.084?", + "a": "5465644.374939463", + "valid": true, + "kind": "compute", + "expect": 5465644.374939463, + "dtype": "quantity", + "unit": "m", + "why": "distance for [173.393638, 251.084037] (the system Uniform acceleration)" + }, + { + "q": "Tell me the distance with acceleration of 173.394 and time of 251.084.", + "a": "5465644.374939463", + "valid": true, + "kind": "compute", + "expect": 5465644.374939463, + "dtype": "quantity", + "unit": "m", + "why": "distance for [173.393638, 251.084037] (the system Uniform acceleration)" + }, + { + "q": "What is the distance for acceleration 0.671697 and time 14.2496?", + "a": "68.19413400667608", + "valid": true, + "kind": "compute", + "expect": 68.19413400667608, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.671697, 14.249569] (the system Uniform acceleration)" + }, + { + "q": "Tell me the distance with acceleration of 0.671697 and time of 14.2496.", + "a": "68.19413400667608", + "valid": true, + "kind": "compute", + "expect": 68.19413400667608, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.671697, 14.249569] (the system Uniform acceleration)" + }, + { + "q": "What is the distance for acceleration 41.2905 and time 2.12277?", + "a": "93.0304093156119", + "valid": true, + "kind": "compute", + "expect": 93.0304093156119, + "dtype": "quantity", + "unit": "m", + "why": "distance for [41.290505, 2.122767] (the system Uniform acceleration)" + }, + { + "q": "Tell me the distance with acceleration of 41.2905 and time of 2.12277.", + "a": "93.0304093156119", + "valid": true, + "kind": "compute", + "expect": 93.0304093156119, + "dtype": "quantity", + "unit": "m", + "why": "distance for [41.290505, 2.122767] (the system Uniform acceleration)" + }, + { + "q": "What is the distance for acceleration 0.432217 and time 16.6571?", + "a": "59.96093945590033", + "valid": true, + "kind": "compute", + "expect": 59.96093945590033, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.432217, 16.657051] (the system Uniform acceleration)" + }, + { + "q": "Tell me the distance with acceleration of 0.432217 and time of 16.6571.", + "a": "59.96093945590033", + "valid": true, + "kind": "compute", + "expect": 59.96093945590033, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.432217, 16.657051] (the system Uniform acceleration)" + }, + { + "q": "What is the distance for acceleration 10.2544 and time 0.415084?", + "a": "0.8833885430900456", + "valid": true, + "kind": "compute", + "expect": 0.8833885430900456, + "dtype": "quantity", + "unit": "m", + "why": "distance for [10.254383, 0.415084] (the system Uniform acceleration)" + }, + { + "q": "Tell me the distance with acceleration of 10.2544 and time of 0.415084.", + "a": "0.8833885430900456", + "valid": true, + "kind": "compute", + "expect": 0.8833885430900456, + "dtype": "quantity", + "unit": "m", + "why": "distance for [10.254383, 0.415084] (the system Uniform acceleration)" + }, + { + "q": "What is the distance for acceleration 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "Tell me the distance with acceleration of 173.394.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "What is the distance of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the distance of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the distance with acceleration of -0.671697 and time of 14.2496.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative acceleration: no example had one" + }, + { + "q": "Which distance is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the acceleration is 22.652 and the time is 1.78451, what is the distance?", + "a": "36.06741712361322", + "valid": true, + "kind": "compute", + "expect": 36.06741712361322, + "dtype": "quantity", + "unit": "m", + "why": "distance for [22.652023, 1.78451] (the system Uniform acceleration)" + }, + { + "q": "Could you work out the distance given acceleration = 22.652, time = 1.78451?", + "a": "36.06741712361322", + "valid": true, + "kind": "compute", + "expect": 36.06741712361322, + "dtype": "quantity", + "unit": "m", + "why": "distance for [22.652023, 1.78451] (the system Uniform acceleration)" + }, + { + "q": "I need the distance for an acceleration of 126.709 and a time of 1.70503, please.", + "a": "184.18047646729076", + "valid": true, + "kind": "compute", + "expect": 184.18047646729076, + "dtype": "quantity", + "unit": "m", + "why": "distance for [126.709252, 1.705032] (the system Uniform acceleration)" + }, + { + "q": "If the acceleration is 126.709 and the time is 1.70503, what is the distance?", + "a": "184.18047646729076", + "valid": true, + "kind": "compute", + "expect": 184.18047646729076, + "dtype": "quantity", + "unit": "m", + "why": "distance for [126.709252, 1.705032] (the system Uniform acceleration)" + }, + { + "q": "Could you work out the distance given acceleration = 1.16997, time = 1.14865?", + "a": "0.7718291194370324", + "valid": true, + "kind": "compute", + "expect": 0.7718291194370324, + "dtype": "quantity", + "unit": "m", + "why": "distance for [1.169968, 1.148652] (the system Uniform acceleration)" + }, + { + "q": "I need the distance for an acceleration of 1.16997 and a time of 1.14865, please.", + "a": "0.7718291194370324", + "valid": true, + "kind": "compute", + "expect": 0.7718291194370324, + "dtype": "quantity", + "unit": "m", + "why": "distance for [1.169968, 1.148652] (the system Uniform acceleration)" + }, + { + "q": "If the acceleration is 21.7946 and the time is 9.1061, what is the distance?", + "a": "903.6172880544732", + "valid": true, + "kind": "compute", + "expect": 903.6172880544732, + "dtype": "quantity", + "unit": "m", + "why": "distance for [21.794621, 9.106104] (the system Uniform acceleration)" + }, + { + "q": "Could you work out the distance given acceleration = 21.7946, time = 9.1061?", + "a": "903.6172880544732", + "valid": true, + "kind": "compute", + "expect": 903.6172880544732, + "dtype": "quantity", + "unit": "m", + "why": "distance for [21.794621, 9.106104] (the system Uniform acceleration)" + }, + { + "q": "I need the distance for an acceleration of 241.598 and a time of 1.13479, please.", + "a": "155.5591256474593", + "valid": true, + "kind": "compute", + "expect": 155.5591256474593, + "dtype": "quantity", + "unit": "m", + "why": "distance for [241.598281, 1.134791] (the system Uniform acceleration)" + }, + { + "q": "If the acceleration is 241.598 and the time is 1.13479, what is the distance?", + "a": "155.5591256474593", + "valid": true, + "kind": "compute", + "expect": 155.5591256474593, + "dtype": "quantity", + "unit": "m", + "why": "distance for [241.598281, 1.134791] (the system Uniform acceleration)" + }, + { + "q": "Could you work out the distance given acceleration = 108.711, time = 3.36463?", + "a": "615.3455041764468", + "valid": true, + "kind": "compute", + "expect": 615.3455041764468, + "dtype": "quantity", + "unit": "m", + "why": "distance for [108.711073, 3.364632] (the system Uniform acceleration)" + }, + { + "q": "I need the distance for an acceleration of 108.711 and a time of 3.36463, please.", + "a": "615.3455041764468", + "valid": true, + "kind": "compute", + "expect": 615.3455041764468, + "dtype": "quantity", + "unit": "m", + "why": "distance for [108.711073, 3.364632] (the system Uniform acceleration)" + }, + { + "q": "If the acceleration is 22.652, what is the distance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "Could you work out the distance given acceleration = 126.709?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "Do you know the distance of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the distance of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the distance given acceleration = -1.16997, time = 1.14865?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative acceleration: no example had one" + }, + { + "q": "What is a typical distance?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-distance-4/program.py b/root/organs10/physics/specialists/solved-distance-4/program.py new file mode 100644 index 0000000000000000000000000000000000000000..4a9f9f6126084745f74e589f0149b1c7fe05b999 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-4/program.py @@ -0,0 +1,7 @@ +"""distance from acceleration, time (Uniform acceleration) +The maths organ (systems.py): solved from the system u = a*t; s = a*t**2/2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(acceleration, time): + return 0.5*acceleration*time**2 diff --git a/root/organs10/physics/specialists/solved-distance-4/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-distance-4/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-4/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-distance-5/organ_formula_v2.py b/root/organs10/physics/specialists/solved-distance-5/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-5/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-distance-5/panel.json b/root/organs10/physics/specialists/solved-distance-5/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..561d172441463e4a0a9bd70307244daaf03b4f99 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-5/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the distance for work 107.961 and force 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m", + "why": "distance for [107.960666, 59.4105] (the system Work)" + }, + { + "q": "Tell me the distance with work of 107.961 and force of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m", + "why": "distance for [107.960666, 59.4105] (the system Work)" + }, + { + "q": "What is the distance for work 169.527 and force 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m", + "why": "distance for [169.526958, 280.773702] (the system Work)" + }, + { + "q": "Tell me the distance with work of 169.527 and force of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m", + "why": "distance for [169.526958, 280.773702] (the system Work)" + }, + { + "q": "What is the distance for work 1.91195 and force 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m", + "why": "distance for [1.911952, 82.240072] (the system Work)" + }, + { + "q": "Tell me the distance with work of 1.91195 and force of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m", + "why": "distance for [1.911952, 82.240072] (the system Work)" + }, + { + "q": "What is the distance for work 1.64379 and force 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m", + "why": "distance for [1.643795, 252.709024] (the system Work)" + }, + { + "q": "Tell me the distance with work of 1.64379 and force of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m", + "why": "distance for [1.643795, 252.709024] (the system Work)" + }, + { + "q": "What is the distance for work 18.3169 and force 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m", + "why": "distance for [18.316867, 6.838598] (the system Work)" + }, + { + "q": "Tell me the distance with work of 18.3169 and force of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m", + "why": "distance for [18.316867, 6.838598] (the system Work)" + }, + { + "q": "What is the distance for work 156.502 and force 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m", + "why": "distance for [156.50181, 185.886273] (the system Work)" + }, + { + "q": "Tell me the distance with work of 156.502 and force of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m", + "why": "distance for [156.50181, 185.886273] (the system Work)" + }, + { + "q": "What is the distance for work 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the force is not given" + }, + { + "q": "Tell me the distance with work of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the force is not given" + }, + { + "q": "What is the distance of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the distance of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the distance with work of -1.91195 and force of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative work: no example had one" + }, + { + "q": "Which distance is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the work is 8.50728 and the force is 17.7888, what is the distance?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m", + "why": "distance for [8.507283, 17.788778] (the system Work)" + }, + { + "q": "Could you work out the distance given work = 8.50728, force = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m", + "why": "distance for [8.507283, 17.788778] (the system Work)" + }, + { + "q": "I need the distance for a work of 8.24951 and a force of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m", + "why": "distance for [8.24951, 0.634021] (the system Work)" + }, + { + "q": "If the work is 8.24951 and the force is 0.634021, what is the distance?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m", + "why": "distance for [8.24951, 0.634021] (the system Work)" + }, + { + "q": "Could you work out the distance given work = 0.318734, force = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.318734, 9.566071] (the system Work)" + }, + { + "q": "I need the distance for a work of 0.318734 and a force of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.318734, 9.566071] (the system Work)" + }, + { + "q": "If the work is 0.671697 and the force is 14.2496, what is the distance?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.671697, 14.249569] (the system Work)" + }, + { + "q": "Could you work out the distance given work = 0.671697, force = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.671697, 14.249569] (the system Work)" + }, + { + "q": "I need the distance for a work of 21.7931 and a force of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m", + "why": "distance for [21.793126, 29.510609] (the system Work)" + }, + { + "q": "If the work is 21.7931 and the force is 29.5106, what is the distance?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m", + "why": "distance for [21.793126, 29.510609] (the system Work)" + }, + { + "q": "Could you work out the distance given work = 153.15, force = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m", + "why": "distance for [153.149965, 18.595315] (the system Work)" + }, + { + "q": "I need the distance for a work of 153.15 and a force of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m", + "why": "distance for [153.149965, 18.595315] (the system Work)" + }, + { + "q": "If the work is 8.50728, what is the distance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the force is not given" + }, + { + "q": "Could you work out the distance given work = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the force is not given" + }, + { + "q": "Do you know the distance of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the distance of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the distance given work = -0.318734, force = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative work: no example had one" + }, + { + "q": "What is a typical distance?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-distance-5/program.py b/root/organs10/physics/specialists/solved-distance-5/program.py new file mode 100644 index 0000000000000000000000000000000000000000..a032104936cf0d1c18ce1bd55d7c6f884fca7ac2 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-5/program.py @@ -0,0 +1,7 @@ +"""distance from work, force (Work) +The maths organ (systems.py): solved from the system E = F*d; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(work, force): + return 1.0*work/force diff --git a/root/organs10/physics/specialists/solved-distance-5/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-distance-5/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-5/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-distance-6/organ_formula_v2.py b/root/organs10/physics/specialists/solved-distance-6/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-6/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-distance-6/panel.json b/root/organs10/physics/specialists/solved-distance-6/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..ffe87fb74d8cfd2bc802c9f5e984d0bdf82bb6af --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-6/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the distance in km for distance 107.961?", + "a": "0.10796066588996202", + "valid": true, + "kind": "compute", + "expect": 0.10796066588996202, + "dtype": "quantity", + "unit": "km", + "why": "distance in km for [107.960666] (the system Kilometres and metres)" + }, + { + "q": "Tell me the distance in km with distance of 107.961.", + "a": "0.10796066588996202", + "valid": true, + "kind": "compute", + "expect": 0.10796066588996202, + "dtype": "quantity", + "unit": "km", + "why": "distance in km for [107.960666] (the system Kilometres and metres)" + }, + { + "q": "What is the distance in km for distance 22.652?", + "a": "0.022652022796855794", + "valid": true, + "kind": "compute", + "expect": 0.022652022796855794, + "dtype": "quantity", + "unit": "km", + "why": "distance in km for [22.652023] (the system Kilometres and metres)" + }, + { + "q": "Tell me the distance in km with distance of 22.652.", + "a": "0.022652022796855794", + "valid": true, + "kind": "compute", + "expect": 0.022652022796855794, + "dtype": "quantity", + "unit": "km", + "why": "distance in km for [22.652023] (the system Kilometres and metres)" + }, + { + "q": "What is the distance in km for distance 173.394?", + "a": "0.1733936384053025", + "valid": true, + "kind": "compute", + "expect": 0.1733936384053025, + "dtype": "quantity", + "unit": "km", + "why": "distance in km for [173.393638] (the system Kilometres and metres)" + }, + { + "q": "Tell me the distance in km with distance of 173.394.", + "a": "0.1733936384053025", + "valid": true, + "kind": "compute", + "expect": 0.1733936384053025, + "dtype": "quantity", + "unit": "km", + "why": "distance in km for [173.393638] (the system Kilometres and metres)" + }, + { + "q": "What is the distance in km for distance 126.709?", + "a": "0.12670925180018236", + "valid": true, + "kind": "compute", + "expect": 0.12670925180018236, + "dtype": "quantity", + "unit": "km", + "why": "distance in km for [126.709252] (the system Kilometres and metres)" + }, + { + "q": "Tell me the distance in km with distance of 126.709.", + "a": "0.12670925180018236", + "valid": true, + "kind": "compute", + "expect": 0.12670925180018236, + "dtype": "quantity", + "unit": "km", + "why": "distance in km for [126.709252] (the system Kilometres and metres)" + }, + { + "q": "What is the distance in km for distance 0.671697?", + "a": "0.0006716971997941935", + "valid": true, + "kind": "compute", + "expect": 0.0006716971997941935, + "dtype": "quantity", + "unit": "km", + "why": "distance in km for [0.671697] (the system Kilometres and metres)" + }, + { + "q": "Tell me the distance in km with distance of 0.671697.", + "a": "0.0006716971997941935", + "valid": true, + "kind": "compute", + "expect": 0.0006716971997941935, + "dtype": "quantity", + "unit": "km", + "why": "distance in km for [0.671697] (the system Kilometres and metres)" + }, + { + "q": "What is the distance in km for distance 1.16997?", + "a": "0.0011699680059779358", + "valid": true, + "kind": "compute", + "expect": 0.0011699680059779358, + "dtype": "quantity", + "unit": "km", + "why": "distance in km for [1.169968] (the system Kilometres and metres)" + }, + { + "q": "Tell me the distance in km with distance of 1.16997.", + "a": "0.0011699680059779358", + "valid": true, + "kind": "compute", + "expect": 0.0011699680059779358, + "dtype": "quantity", + "unit": "km", + "why": "distance in km for [1.169968] (the system Kilometres and metres)" + }, + { + "q": "What is the distance in km?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the distance is not given" + }, + { + "q": "Could you work out the distance in km, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the distance is not given" + }, + { + "q": "What is the distance in km of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the distance in km of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the distance in km with distance of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative distance: no example had one" + }, + { + "q": "Which distance in km is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the distance is 2.57003, what is the distance in km?", + "a": "0.002570029913303644", + "valid": true, + "kind": "compute", + "expect": 0.002570029913303644, + "dtype": "quantity", + "unit": "km", + "why": "distance in km for [2.57003] (the system Kilometres and metres)" + }, + { + "q": "Could you work out the distance in km given distance = 2.57003?", + "a": "0.002570029913303644", + "valid": true, + "kind": "compute", + "expect": 0.002570029913303644, + "dtype": "quantity", + "unit": "km", + "why": "distance in km for [2.57003] (the system Kilometres and metres)" + }, + { + "q": "I need the distance in km for a distance of 48.921, please.", + "a": "0.04892099086001184", + "valid": true, + "kind": "compute", + "expect": 0.04892099086001184, + "dtype": "quantity", + "unit": "km", + "why": "distance in km for [48.920991] (the system Kilometres and metres)" + }, + { + "q": "If the distance is 48.921, what is the distance in km?", + "a": "0.04892099086001184", + "valid": true, + "kind": "compute", + "expect": 0.04892099086001184, + "dtype": "quantity", + "unit": "km", + "why": "distance in km for [48.920991] (the system Kilometres and metres)" + }, + { + "q": "Could you work out the distance in km given distance = 0.348438?", + "a": "0.00034843766963003046", + "valid": true, + "kind": "compute", + "expect": 0.00034843766963003046, + "dtype": "quantity", + "unit": "km", + "why": "distance in km for [0.348438] (the system Kilometres and metres)" + }, + { + "q": "I need the distance in km for a distance of 0.348438, please.", + "a": "0.00034843766963003046", + "valid": true, + "kind": "compute", + "expect": 0.00034843766963003046, + "dtype": "quantity", + "unit": "km", + "why": "distance in km for [0.348438] (the system Kilometres and metres)" + }, + { + "q": "If the distance is 252.709, what is the distance in km?", + "a": "0.2527090242495275", + "valid": true, + "kind": "compute", + "expect": 0.2527090242495275, + "dtype": "quantity", + "unit": "km", + "why": "distance in km for [252.709024] (the system Kilometres and metres)" + }, + { + "q": "Could you work out the distance in km given distance = 252.709?", + "a": "0.2527090242495275", + "valid": true, + "kind": "compute", + "expect": 0.2527090242495275, + "dtype": "quantity", + "unit": "km", + "why": "distance in km for [252.709024] (the system Kilometres and metres)" + }, + { + "q": "I need the distance in km for a distance of 20.3966, please.", + "a": "0.02039655796196344", + "valid": true, + "kind": "compute", + "expect": 0.02039655796196344, + "dtype": "quantity", + "unit": "km", + "why": "distance in km for [20.396558] (the system Kilometres and metres)" + }, + { + "q": "If the distance is 20.3966, what is the distance in km?", + "a": "0.02039655796196344", + "valid": true, + "kind": "compute", + "expect": 0.02039655796196344, + "dtype": "quantity", + "unit": "km", + "why": "distance in km for [20.396558] (the system Kilometres and metres)" + }, + { + "q": "Could you work out the distance in km given distance = 134.993?", + "a": "0.13499295758073798", + "valid": true, + "kind": "compute", + "expect": 0.13499295758073798, + "dtype": "quantity", + "unit": "km", + "why": "distance in km for [134.992958] (the system Kilometres and metres)" + }, + { + "q": "I need the distance in km for a distance of 134.993, please.", + "a": "0.13499295758073798", + "valid": true, + "kind": "compute", + "expect": 0.13499295758073798, + "dtype": "quantity", + "unit": "km", + "why": "distance in km for [134.992958] (the system Kilometres and metres)" + }, + { + "q": "I need the distance in km, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the distance is not given" + }, + { + "q": "Can you tell me the distance in km?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the distance is not given" + }, + { + "q": "Do you know the distance in km of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the distance in km of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the distance in km given distance = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative distance: no example had one" + }, + { + "q": "What is a typical distance in km?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-distance-6/program.py b/root/organs10/physics/specialists/solved-distance-6/program.py new file mode 100644 index 0000000000000000000000000000000000000000..ca7cfb22831a381b9049ad4e32c56491d711f315 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-6/program.py @@ -0,0 +1,7 @@ +"""distance from distance (Kilometres and metres) +The maths organ (systems.py): solved from the system K = M; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(distance): + return 0.001*distance diff --git a/root/organs10/physics/specialists/solved-distance-6/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-distance-6/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-6/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-distance-7/organ_formula_v2.py b/root/organs10/physics/specialists/solved-distance-7/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-7/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-distance-7/panel.json b/root/organs10/physics/specialists/solved-distance-7/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..abc896836189ff81fe719673b0b33f9852b31fa7 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-7/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the distance in m for distance 107.961?", + "a": "107960.66588996201", + "valid": true, + "kind": "compute", + "expect": 107960.66588996201, + "dtype": "quantity", + "unit": "m", + "why": "distance in m for [107.960666] (the system Kilometres and metres)" + }, + { + "q": "Tell me the distance in m with distance of 107.961.", + "a": "107960.66588996201", + "valid": true, + "kind": "compute", + "expect": 107960.66588996201, + "dtype": "quantity", + "unit": "m", + "why": "distance in m for [107.960666] (the system Kilometres and metres)" + }, + { + "q": "What is the distance in m for distance 22.652?", + "a": "22652.022796855792", + "valid": true, + "kind": "compute", + "expect": 22652.022796855792, + "dtype": "quantity", + "unit": "m", + "why": "distance in m for [22.652023] (the system Kilometres and metres)" + }, + { + "q": "Tell me the distance in m with distance of 22.652.", + "a": "22652.022796855792", + "valid": true, + "kind": "compute", + "expect": 22652.022796855792, + "dtype": "quantity", + "unit": "m", + "why": "distance in m for [22.652023] (the system Kilometres and metres)" + }, + { + "q": "What is the distance in m for distance 173.394?", + "a": "173393.6384053025", + "valid": true, + "kind": "compute", + "expect": 173393.6384053025, + "dtype": "quantity", + "unit": "m", + "why": "distance in m for [173.393638] (the system Kilometres and metres)" + }, + { + "q": "Tell me the distance in m with distance of 173.394.", + "a": "173393.6384053025", + "valid": true, + "kind": "compute", + "expect": 173393.6384053025, + "dtype": "quantity", + "unit": "m", + "why": "distance in m for [173.393638] (the system Kilometres and metres)" + }, + { + "q": "What is the distance in m for distance 126.709?", + "a": "126709.25180018238", + "valid": true, + "kind": "compute", + "expect": 126709.25180018238, + "dtype": "quantity", + "unit": "m", + "why": "distance in m for [126.709252] (the system Kilometres and metres)" + }, + { + "q": "Tell me the distance in m with distance of 126.709.", + "a": "126709.25180018238", + "valid": true, + "kind": "compute", + "expect": 126709.25180018238, + "dtype": "quantity", + "unit": "m", + "why": "distance in m for [126.709252] (the system Kilometres and metres)" + }, + { + "q": "What is the distance in m for distance 0.671697?", + "a": "671.6971997941935", + "valid": true, + "kind": "compute", + "expect": 671.6971997941935, + "dtype": "quantity", + "unit": "m", + "why": "distance in m for [0.671697] (the system Kilometres and metres)" + }, + { + "q": "Tell me the distance in m with distance of 0.671697.", + "a": "671.6971997941935", + "valid": true, + "kind": "compute", + "expect": 671.6971997941935, + "dtype": "quantity", + "unit": "m", + "why": "distance in m for [0.671697] (the system Kilometres and metres)" + }, + { + "q": "What is the distance in m for distance 1.16997?", + "a": "1169.9680059779357", + "valid": true, + "kind": "compute", + "expect": 1169.9680059779357, + "dtype": "quantity", + "unit": "m", + "why": "distance in m for [1.169968] (the system Kilometres and metres)" + }, + { + "q": "Tell me the distance in m with distance of 1.16997.", + "a": "1169.9680059779357", + "valid": true, + "kind": "compute", + "expect": 1169.9680059779357, + "dtype": "quantity", + "unit": "m", + "why": "distance in m for [1.169968] (the system Kilometres and metres)" + }, + { + "q": "What is the distance in m?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the distance is not given" + }, + { + "q": "Could you work out the distance in m, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the distance is not given" + }, + { + "q": "What is the distance in m of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the distance in m of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the distance in m with distance of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative distance: no example had one" + }, + { + "q": "Which distance in m is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the distance is 2.57003, what is the distance in m?", + "a": "2570.0299133036438", + "valid": true, + "kind": "compute", + "expect": 2570.0299133036438, + "dtype": "quantity", + "unit": "m", + "why": "distance in m for [2.57003] (the system Kilometres and metres)" + }, + { + "q": "Could you work out the distance in m given distance = 2.57003?", + "a": "2570.0299133036438", + "valid": true, + "kind": "compute", + "expect": 2570.0299133036438, + "dtype": "quantity", + "unit": "m", + "why": "distance in m for [2.57003] (the system Kilometres and metres)" + }, + { + "q": "I need the distance in m for a distance of 48.921, please.", + "a": "48920.990860011836", + "valid": true, + "kind": "compute", + "expect": 48920.990860011836, + "dtype": "quantity", + "unit": "m", + "why": "distance in m for [48.920991] (the system Kilometres and metres)" + }, + { + "q": "If the distance is 48.921, what is the distance in m?", + "a": "48920.990860011836", + "valid": true, + "kind": "compute", + "expect": 48920.990860011836, + "dtype": "quantity", + "unit": "m", + "why": "distance in m for [48.920991] (the system Kilometres and metres)" + }, + { + "q": "Could you work out the distance in m given distance = 0.348438?", + "a": "348.4376696300305", + "valid": true, + "kind": "compute", + "expect": 348.4376696300305, + "dtype": "quantity", + "unit": "m", + "why": "distance in m for [0.348438] (the system Kilometres and metres)" + }, + { + "q": "I need the distance in m for a distance of 0.348438, please.", + "a": "348.4376696300305", + "valid": true, + "kind": "compute", + "expect": 348.4376696300305, + "dtype": "quantity", + "unit": "m", + "why": "distance in m for [0.348438] (the system Kilometres and metres)" + }, + { + "q": "If the distance is 252.709, what is the distance in m?", + "a": "252709.02424952752", + "valid": true, + "kind": "compute", + "expect": 252709.02424952752, + "dtype": "quantity", + "unit": "m", + "why": "distance in m for [252.709024] (the system Kilometres and metres)" + }, + { + "q": "Could you work out the distance in m given distance = 252.709?", + "a": "252709.02424952752", + "valid": true, + "kind": "compute", + "expect": 252709.02424952752, + "dtype": "quantity", + "unit": "m", + "why": "distance in m for [252.709024] (the system Kilometres and metres)" + }, + { + "q": "I need the distance in m for a distance of 20.3966, please.", + "a": "20396.557961963437", + "valid": true, + "kind": "compute", + "expect": 20396.557961963437, + "dtype": "quantity", + "unit": "m", + "why": "distance in m for [20.396558] (the system Kilometres and metres)" + }, + { + "q": "If the distance is 20.3966, what is the distance in m?", + "a": "20396.557961963437", + "valid": true, + "kind": "compute", + "expect": 20396.557961963437, + "dtype": "quantity", + "unit": "m", + "why": "distance in m for [20.396558] (the system Kilometres and metres)" + }, + { + "q": "Could you work out the distance in m given distance = 134.993?", + "a": "134992.957580738", + "valid": true, + "kind": "compute", + "expect": 134992.957580738, + "dtype": "quantity", + "unit": "m", + "why": "distance in m for [134.992958] (the system Kilometres and metres)" + }, + { + "q": "I need the distance in m for a distance of 134.993, please.", + "a": "134992.957580738", + "valid": true, + "kind": "compute", + "expect": 134992.957580738, + "dtype": "quantity", + "unit": "m", + "why": "distance in m for [134.992958] (the system Kilometres and metres)" + }, + { + "q": "I need the distance in m, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the distance is not given" + }, + { + "q": "Can you tell me the distance in m?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the distance is not given" + }, + { + "q": "Do you know the distance in m of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the distance in m of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the distance in m given distance = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative distance: no example had one" + }, + { + "q": "What is a typical distance in m?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-distance-7/program.py b/root/organs10/physics/specialists/solved-distance-7/program.py new file mode 100644 index 0000000000000000000000000000000000000000..877fecaa92019b04a3d55521e977809b55137a0f --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-7/program.py @@ -0,0 +1,7 @@ +"""distance from distance (Kilometres and metres) +The maths organ (systems.py): solved from the system K = M; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(distance): + return 1000.0*distance diff --git a/root/organs10/physics/specialists/solved-distance-7/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-distance-7/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-7/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-distance-8/organ_formula_v2.py b/root/organs10/physics/specialists/solved-distance-8/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-8/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-distance-8/panel.json b/root/organs10/physics/specialists/solved-distance-8/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..9ae932abe5ada1f4b97c21ca503af2cabaa5b707 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-8/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the distance for fuel 107.961 and consumption 59.4105?", + "a": "1.8171983999893249", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893249, + "dtype": "quantity", + "unit": "km", + "why": "distance for [107.960666, 59.4105] (the system Fuel use)" + }, + { + "q": "Tell me the distance with fuel of 107.961 and consumption of 59.4105.", + "a": "1.8171983999893249", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893249, + "dtype": "quantity", + "unit": "km", + "why": "distance for [107.960666, 59.4105] (the system Fuel use)" + }, + { + "q": "What is the distance for fuel 169.527 and consumption 280.774?", + "a": "0.6037850281809956", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809956, + "dtype": "quantity", + "unit": "km", + "why": "distance for [169.526958, 280.773702] (the system Fuel use)" + }, + { + "q": "Tell me the distance with fuel of 169.527 and consumption of 280.774.", + "a": "0.6037850281809956", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809956, + "dtype": "quantity", + "unit": "km", + "why": "distance for [169.526958, 280.773702] (the system Fuel use)" + }, + { + "q": "What is the distance for fuel 1.91195 and consumption 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "km", + "why": "distance for [1.911952, 82.240072] (the system Fuel use)" + }, + { + "q": "Tell me the distance with fuel of 1.91195 and consumption of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "km", + "why": "distance for [1.911952, 82.240072] (the system Fuel use)" + }, + { + "q": "What is the distance for fuel 1.64379 and consumption 252.709?", + "a": "0.006504696457692341", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692341, + "dtype": "quantity", + "unit": "km", + "why": "distance for [1.643795, 252.709024] (the system Fuel use)" + }, + { + "q": "Tell me the distance with fuel of 1.64379 and consumption of 252.709.", + "a": "0.006504696457692341", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692341, + "dtype": "quantity", + "unit": "km", + "why": "distance for [1.643795, 252.709024] (the system Fuel use)" + }, + { + "q": "What is the distance for fuel 18.3169 and consumption 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "km", + "why": "distance for [18.316867, 6.838598] (the system Fuel use)" + }, + { + "q": "Tell me the distance with fuel of 18.3169 and consumption of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "km", + "why": "distance for [18.316867, 6.838598] (the system Fuel use)" + }, + { + "q": "What is the distance for fuel 156.502 and consumption 185.886?", + "a": "0.8419223616111587", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111587, + "dtype": "quantity", + "unit": "km", + "why": "distance for [156.50181, 185.886273] (the system Fuel use)" + }, + { + "q": "Tell me the distance with fuel of 156.502 and consumption of 185.886.", + "a": "0.8419223616111587", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111587, + "dtype": "quantity", + "unit": "km", + "why": "distance for [156.50181, 185.886273] (the system Fuel use)" + }, + { + "q": "What is the distance for fuel 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the consumption is not given" + }, + { + "q": "Tell me the distance with fuel of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the consumption is not given" + }, + { + "q": "What is the distance of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the distance of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the distance with fuel of -1.91195 and consumption of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative fuel: no example had one" + }, + { + "q": "Which distance is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the fuel is 8.50728 and the consumption is 17.7888, what is the distance?", + "a": "0.4782387296673115", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673115, + "dtype": "quantity", + "unit": "km", + "why": "distance for [8.507283, 17.788778] (the system Fuel use)" + }, + { + "q": "Could you work out the distance given fuel = 8.50728, consumption = 17.7888?", + "a": "0.4782387296673115", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673115, + "dtype": "quantity", + "unit": "km", + "why": "distance for [8.507283, 17.788778] (the system Fuel use)" + }, + { + "q": "I need the distance for a fuel of 8.24951 and a consumption of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "km", + "why": "distance for [8.24951, 0.634021] (the system Fuel use)" + }, + { + "q": "If the fuel is 8.24951 and the consumption is 0.634021, what is the distance?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "km", + "why": "distance for [8.24951, 0.634021] (the system Fuel use)" + }, + { + "q": "Could you work out the distance given fuel = 0.318734, consumption = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "km", + "why": "distance for [0.318734, 9.566071] (the system Fuel use)" + }, + { + "q": "I need the distance for a fuel of 0.318734 and a consumption of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "km", + "why": "distance for [0.318734, 9.566071] (the system Fuel use)" + }, + { + "q": "If the fuel is 0.671697 and the consumption is 14.2496, what is the distance?", + "a": "0.047138070274372355", + "valid": true, + "kind": "compute", + "expect": 0.047138070274372355, + "dtype": "quantity", + "unit": "km", + "why": "distance for [0.671697, 14.249569] (the system Fuel use)" + }, + { + "q": "Could you work out the distance given fuel = 0.671697, consumption = 14.2496?", + "a": "0.047138070274372355", + "valid": true, + "kind": "compute", + "expect": 0.047138070274372355, + "dtype": "quantity", + "unit": "km", + "why": "distance for [0.671697, 14.249569] (the system Fuel use)" + }, + { + "q": "I need the distance for a fuel of 21.7931 and a consumption of 29.5106, please.", + "a": "0.7384844800541928", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541928, + "dtype": "quantity", + "unit": "km", + "why": "distance for [21.793126, 29.510609] (the system Fuel use)" + }, + { + "q": "If the fuel is 21.7931 and the consumption is 29.5106, what is the distance?", + "a": "0.7384844800541928", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541928, + "dtype": "quantity", + "unit": "km", + "why": "distance for [21.793126, 29.510609] (the system Fuel use)" + }, + { + "q": "Could you work out the distance given fuel = 153.15, consumption = 18.5953?", + "a": "8.23594336849931", + "valid": true, + "kind": "compute", + "expect": 8.23594336849931, + "dtype": "quantity", + "unit": "km", + "why": "distance for [153.149965, 18.595315] (the system Fuel use)" + }, + { + "q": "I need the distance for a fuel of 153.15 and a consumption of 18.5953, please.", + "a": "8.23594336849931", + "valid": true, + "kind": "compute", + "expect": 8.23594336849931, + "dtype": "quantity", + "unit": "km", + "why": "distance for [153.149965, 18.595315] (the system Fuel use)" + }, + { + "q": "If the fuel is 8.50728, what is the distance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the consumption is not given" + }, + { + "q": "Could you work out the distance given fuel = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the consumption is not given" + }, + { + "q": "Do you know the distance of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the distance of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the distance given fuel = -0.318734, consumption = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative fuel: no example had one" + }, + { + "q": "What is a typical distance?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-distance-8/program.py b/root/organs10/physics/specialists/solved-distance-8/program.py new file mode 100644 index 0000000000000000000000000000000000000000..d5fc887cec7c2152c25334fb343e89c2af1faf0a --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-8/program.py @@ -0,0 +1,7 @@ +"""distance from fuel, consumption (Fuel use) +The maths organ (systems.py): solved from the system F = c*d; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(fuel, consumption): + return 1.0*fuel/consumption diff --git a/root/organs10/physics/specialists/solved-distance-8/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-distance-8/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-8/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-distance/organ_formula_v2.py b/root/organs10/physics/specialists/solved-distance/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-distance/panel.json b/root/organs10/physics/specialists/solved-distance/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..39f8adeffaebc6a371b90d8992f95d2d19a09bb7 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the distance for speed 107.961 and time 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "m", + "why": "distance for [107.960666, 59.4105] (the system Constant speed)" + }, + { + "q": "Tell me the distance with speed of 107.961 and time of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "m", + "why": "distance for [107.960666, 59.4105] (the system Constant speed)" + }, + { + "q": "What is the distance for speed 169.527 and time 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "m", + "why": "distance for [169.526958, 280.773702] (the system Constant speed)" + }, + { + "q": "Tell me the distance with speed of 169.527 and time of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "m", + "why": "distance for [169.526958, 280.773702] (the system Constant speed)" + }, + { + "q": "What is the distance for speed 1.91195 and time 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "m", + "why": "distance for [1.911952, 82.240072] (the system Constant speed)" + }, + { + "q": "Tell me the distance with speed of 1.91195 and time of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "m", + "why": "distance for [1.911952, 82.240072] (the system Constant speed)" + }, + { + "q": "What is the distance for speed 1.64379 and time 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "m", + "why": "distance for [1.643795, 252.709024] (the system Constant speed)" + }, + { + "q": "Tell me the distance with speed of 1.64379 and time of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "m", + "why": "distance for [1.643795, 252.709024] (the system Constant speed)" + }, + { + "q": "What is the distance for speed 18.3169 and time 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "m", + "why": "distance for [18.316867, 6.838598] (the system Constant speed)" + }, + { + "q": "Tell me the distance with speed of 18.3169 and time of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "m", + "why": "distance for [18.316867, 6.838598] (the system Constant speed)" + }, + { + "q": "What is the distance for speed 156.502 and time 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "m", + "why": "distance for [156.50181, 185.886273] (the system Constant speed)" + }, + { + "q": "Tell me the distance with speed of 156.502 and time of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "m", + "why": "distance for [156.50181, 185.886273] (the system Constant speed)" + }, + { + "q": "What is the distance for speed 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "Tell me the distance with speed of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "What is the distance of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the distance of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the distance with speed of -1.91195 and time of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative speed: no example had one" + }, + { + "q": "Which distance is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the speed is 8.50728 and the time is 17.7888, what is the distance?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "m", + "why": "distance for [8.507283, 17.788778] (the system Constant speed)" + }, + { + "q": "Could you work out the distance given speed = 8.50728, time = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "m", + "why": "distance for [8.507283, 17.788778] (the system Constant speed)" + }, + { + "q": "I need the distance for a speed of 8.24951 and a time of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "m", + "why": "distance for [8.24951, 0.634021] (the system Constant speed)" + }, + { + "q": "If the speed is 8.24951 and the time is 0.634021, what is the distance?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "m", + "why": "distance for [8.24951, 0.634021] (the system Constant speed)" + }, + { + "q": "Could you work out the distance given speed = 0.318734, time = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.318734, 9.566071] (the system Constant speed)" + }, + { + "q": "I need the distance for a speed of 0.318734 and a time of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.318734, 9.566071] (the system Constant speed)" + }, + { + "q": "If the speed is 0.671697 and the time is 14.2496, what is the distance?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.671697, 14.249569] (the system Constant speed)" + }, + { + "q": "Could you work out the distance given speed = 0.671697, time = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "m", + "why": "distance for [0.671697, 14.249569] (the system Constant speed)" + }, + { + "q": "I need the distance for a speed of 21.7931 and a time of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "m", + "why": "distance for [21.793126, 29.510609] (the system Constant speed)" + }, + { + "q": "If the speed is 21.7931 and the time is 29.5106, what is the distance?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "m", + "why": "distance for [21.793126, 29.510609] (the system Constant speed)" + }, + { + "q": "Could you work out the distance given speed = 153.15, time = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "m", + "why": "distance for [153.149965, 18.595315] (the system Constant speed)" + }, + { + "q": "I need the distance for a speed of 153.15 and a time of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "m", + "why": "distance for [153.149965, 18.595315] (the system Constant speed)" + }, + { + "q": "If the speed is 8.50728, what is the distance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "Could you work out the distance given speed = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "Do you know the distance of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the distance of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the distance given speed = -0.318734, time = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative speed: no example had one" + }, + { + "q": "What is a typical distance?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-distance/program.py b/root/organs10/physics/specialists/solved-distance/program.py new file mode 100644 index 0000000000000000000000000000000000000000..1ecc391a6090d00a674fed9d6375dd3704dba3ac --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance/program.py @@ -0,0 +1,7 @@ +"""distance from speed, time (Constant speed) +The maths organ (systems.py): solved from the system d = v*t; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(speed, time): + return 1.0*speed*time diff --git a/root/organs10/physics/specialists/solved-distance/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-distance/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?