diff --git a/root/organs10/physics/specialists/solved-impact-speed-2/narrow_v1.py b/root/organs10/physics/specialists/solved-impact-speed-2/narrow_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..647d4d5e961acd006c9de30f0ce8b0b8f5139cda --- /dev/null +++ b/root/organs10/physics/specialists/solved-impact-speed-2/narrow_v1.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 +"""narrow_v1 - the shared NARROWING rule for specialist organs (2026-09-29, from the full-core sweep): + + WORD PROBLEMS AND NARRATIVE TURNS BELONG TO THE HEAD. An organ answers a direct domain question. A turn that tells a + story ("Justine wants to put a deck ...", "A new energy drink advertises 106 calories for 8 ounces ...") or relates + quantities to each other ("the length is 9 inches more than the width") is a word problem, which the core's word- + problem organ owns; organs firing on its nouns declined it or answered a different question. + An organ may DECLINE only a short direct question (it is then refusing the domain question that was asked); on + anything longer it says nothing and the head answers. + +Narrowing only ever removes turns an organ takes, so on any input where it does not fire, the organ's reply is +byte-identical to the frozen version it wraps (checked per specialist by an invariance count before re-gating). +""" +import re + +RELATION = re.compile(r"\b(?:\d+(?:\.\d+)?|one|two|three|four|five|six|seven|eight|nine|ten)\s+(?:\w+\s+){0,2}(?:more|less|fewer|longer|shorter)\s+than\b|" + r"\b(?:twice|three times|half)\s+(?:as\s+\w+\s+as\s+)?the\s+\w+\b", re.I) + + +# a RATE ("106 calories for 8 ounces", "3 apples per 2 dollars") makes the turn a proportion problem +RATE = re.compile(r"\b\d+(?:\.\d+)?\s+[a-z]+\s+(?:for|per|every)\s+(?:\d+(?:\.\d+)?|a|an|one|each)\s+[a-z]+", re.I) +NUM = re.compile(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-impact-speed-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-impact-speed-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-impact-speed-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-impact-speed-2/panel.json b/root/organs10/physics/specialists/solved-impact-speed-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..e29dad435e68090c94f176c00a38219b00224873 --- /dev/null +++ b/root/organs10/physics/specialists/solved-impact-speed-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the impact speed for fall time 107.961?", + "a": "1058.732464149796", + "valid": true, + "kind": "compute", + "expect": 1058.732464149796, + "dtype": "quantity", + "unit": "m/s", + "why": "impact speed for [107.960666] (the system Free fall)" + }, + { + "q": "Tell me the impact speed with fall time of 107.961.", + "a": "1058.732464149796", + "valid": true, + "kind": "compute", + "expect": 1058.732464149796, + "dtype": "quantity", + "unit": "m/s", + "why": "impact speed for [107.960666] (the system Free fall)" + }, + { + "q": "What is the impact speed for fall time 6.34623?", + "a": "62.235213240556966", + "valid": true, + "kind": "compute", + "expect": 62.235213240556966, + "dtype": "quantity", + "unit": "m/s", + "why": "impact speed for [6.346226] (the system Free fall)" + }, + { + "q": "Tell me the impact speed with fall time of 6.34623.", + "a": "62.235213240556966", + "valid": true, + "kind": "compute", + "expect": 62.235213240556966, + "dtype": "quantity", + "unit": "m/s", + "why": "impact speed for [6.346226] (the system Free fall)" + }, + { + "q": "What is the impact speed for fall time 0.551236?", + "a": "5.405777063355242", + "valid": true, + "kind": "compute", + "expect": 5.405777063355242, + "dtype": "quantity", + "unit": "m/s", + "why": "impact speed for [0.551236] (the system Free fall)" + }, + { + "q": "Tell me the impact speed with fall time of 0.551236.", + "a": "5.405777063355242", + "valid": true, + "kind": "compute", + "expect": 5.405777063355242, + "dtype": "quantity", + "unit": "m/s", + "why": "impact speed for [0.551236] (the system Free fall)" + }, + { + "q": "What is the impact speed for fall time 186.27?", + "a": "1826.6852560600696", + "valid": true, + "kind": "compute", + "expect": 1826.6852560600696, + "dtype": "quantity", + "unit": "m/s", + "why": "impact speed for [186.270057] (the system Free fall)" + }, + { + "q": "Tell me the impact speed with fall time of 186.27.", + "a": "1826.6852560600696", + "valid": true, + "kind": "compute", + "expect": 1826.6852560600696, + "dtype": "quantity", + "unit": "m/s", + "why": "impact speed for [186.270057] (the system Free fall)" + }, + { + "q": "What is the impact speed for fall time 0.710236?", + "a": "6.965039042499072", + "valid": true, + "kind": "compute", + "expect": 6.965039042499072, + "dtype": "quantity", + "unit": "m/s", + "why": "impact speed for [0.710236] (the system Free fall)" + }, + { + "q": "Tell me the impact speed with fall time of 0.710236.", + "a": "6.965039042499072", + "valid": true, + "kind": "compute", + "expect": 6.965039042499072, + "dtype": "quantity", + "unit": "m/s", + "why": "impact speed for [0.710236] (the system Free fall)" + }, + { + "q": "What is the impact speed for fall time 79.8435?", + "a": "782.9976284045038", + "valid": true, + "kind": "compute", + "expect": 782.9976284045038, + "dtype": "quantity", + "unit": "m/s", + "why": "impact speed for [79.843538] (the system Free fall)" + }, + { + "q": "Tell me the impact speed with fall time of 79.8435.", + "a": "782.9976284045038", + "valid": true, + "kind": "compute", + "expect": 782.9976284045038, + "dtype": "quantity", + "unit": "m/s", + "why": "impact speed for [79.843538] (the system Free fall)" + }, + { + "q": "What is the impact speed?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the fall time is not given" + }, + { + "q": "Could you work out the impact speed, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the fall time is not given" + }, + { + "q": "What is the impact speed 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 impact speed 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 impact speed with fall time of -0.551236.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative fall time: no example had one" + }, + { + "q": "Which impact speed 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 fall time is 58.5346, what is the impact speed?", + "a": "574.0282789827824", + "valid": true, + "kind": "compute", + "expect": 574.0282789827824, + "dtype": "quantity", + "unit": "m/s", + "why": "impact speed for [58.534594] (the system Free fall)" + }, + { + "q": "Could you work out the impact speed given fall time = 58.5346?", + "a": "574.0282789827824", + "valid": true, + "kind": "compute", + "expect": 574.0282789827824, + "dtype": "quantity", + "unit": "m/s", + "why": "impact speed for [58.534594] (the system Free fall)" + }, + { + "q": "I need the impact speed for a fall time of 31.9492, please.", + "a": "313.31434492634656", + "valid": true, + "kind": "compute", + "expect": 313.31434492634656, + "dtype": "quantity", + "unit": "m/s", + "why": "impact speed for [31.949172] (the system Free fall)" + }, + { + "q": "If the fall time is 31.9492, what is the impact speed?", + "a": "313.31434492634656", + "valid": true, + "kind": "compute", + "expect": 313.31434492634656, + "dtype": "quantity", + "unit": "m/s", + "why": "impact speed for [31.949172] (the system Free fall)" + }, + { + "q": "Could you work out the impact speed given fall time = 6.8386?", + "a": "67.06373767906294", + "valid": true, + "kind": "compute", + "expect": 67.06373767906294, + "dtype": "quantity", + "unit": "m/s", + "why": "impact speed for [6.838598] (the system Free fall)" + }, + { + "q": "I need the impact speed for a fall time of 6.8386, please.", + "a": "67.06373767906294", + "valid": true, + "kind": "compute", + "expect": 67.06373767906294, + "dtype": "quantity", + "unit": "m/s", + "why": "impact speed for [6.838598] (the system Free fall)" + }, + { + "q": "If the fall time is 17.3414, what is the impact speed?", + "a": "170.0613834781648", + "valid": true, + "kind": "compute", + "expect": 170.0613834781648, + "dtype": "quantity", + "unit": "m/s", + "why": "impact speed for [17.341435] (the system Free fall)" + }, + { + "q": "Could you work out the impact speed given fall time = 17.3414?", + "a": "170.0613834781648", + "valid": true, + "kind": "compute", + "expect": 170.0613834781648, + "dtype": "quantity", + "unit": "m/s", + "why": "impact speed for [17.341435] (the system Free fall)" + }, + { + "q": "I need the impact speed for a fall time of 0.380269, please.", + "a": "3.7291603636697994", + "valid": true, + "kind": "compute", + "expect": 3.7291603636697994, + "dtype": "quantity", + "unit": "m/s", + "why": "impact speed for [0.380269] (the system Free fall)" + }, + { + "q": "If the fall time is 0.380269, what is the impact speed?", + "a": "3.7291603636697994", + "valid": true, + "kind": "compute", + "expect": 3.7291603636697994, + "dtype": "quantity", + "unit": "m/s", + "why": "impact speed for [0.380269] (the system Free fall)" + }, + { + "q": "Could you work out the impact speed given fall time = 291.545?", + "a": "2859.078776895802", + "valid": true, + "kind": "compute", + "expect": 2859.078776895802, + "dtype": "quantity", + "unit": "m/s", + "why": "impact speed for [291.544898] (the system Free fall)" + }, + { + "q": "I need the impact speed for a fall time of 291.545, please.", + "a": "2859.078776895802", + "valid": true, + "kind": "compute", + "expect": 2859.078776895802, + "dtype": "quantity", + "unit": "m/s", + "why": "impact speed for [291.544898] (the system Free fall)" + }, + { + "q": "I need the impact speed, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the fall time is not given" + }, + { + "q": "Can you tell me the impact speed?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the fall time is not given" + }, + { + "q": "Do you know the impact speed 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 impact speed 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 impact speed given fall time = -6.8386?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative fall time: no example had one" + }, + { + "q": "What is a typical impact speed?", + "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-impact-speed-2/program.py b/root/organs10/physics/specialists/solved-impact-speed-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..ee481a9837e1a4dea02338d0020fe358e0e132fe --- /dev/null +++ b/root/organs10/physics/specialists/solved-impact-speed-2/program.py @@ -0,0 +1,7 @@ +"""impact speed from fall time (Free fall) +The maths organ (systems.py): solved from the system h = g*t**2/2; v = g*t; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(fall_time): + return 9.80665*fall_time diff --git a/root/organs10/physics/specialists/solved-impact-speed-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-impact-speed-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-impact-speed-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-incident-radiant-flux-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-incident-radiant-flux-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-incident-radiant-flux-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-incident-radiant-flux-2/panel.json b/root/organs10/physics/specialists/solved-incident-radiant-flux-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..51de8aeba4c117a5af66cd7a584d420c479540de --- /dev/null +++ b/root/organs10/physics/specialists/solved-incident-radiant-flux-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the incident radiant flux for absorbed radiant flux 107.961 and absorptance 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [107.960666, 59.4105] (the system absorptance (Wikidata Q16635541))" + }, + { + "q": "Tell me the incident radiant flux with absorbed radiant flux of 107.961 and absorptance of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [107.960666, 59.4105] (the system absorptance (Wikidata Q16635541))" + }, + { + "q": "What is the incident radiant flux for absorbed radiant flux 169.527 and absorptance 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [169.526958, 280.773702] (the system absorptance (Wikidata Q16635541))" + }, + { + "q": "Tell me the incident radiant flux with absorbed radiant flux of 169.527 and absorptance of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [169.526958, 280.773702] (the system absorptance (Wikidata Q16635541))" + }, + { + "q": "What is the incident radiant flux for absorbed radiant flux 1.91195 and absorptance 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [1.911952, 82.240072] (the system absorptance (Wikidata Q16635541))" + }, + { + "q": "Tell me the incident radiant flux with absorbed radiant flux of 1.91195 and absorptance of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [1.911952, 82.240072] (the system absorptance (Wikidata Q16635541))" + }, + { + "q": "What is the incident radiant flux for absorbed radiant flux 1.64379 and absorptance 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [1.643795, 252.709024] (the system absorptance (Wikidata Q16635541))" + }, + { + "q": "Tell me the incident radiant flux with absorbed radiant flux of 1.64379 and absorptance of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [1.643795, 252.709024] (the system absorptance (Wikidata Q16635541))" + }, + { + "q": "What is the incident radiant flux for absorbed radiant flux 18.3169 and absorptance 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [18.316867, 6.838598] (the system absorptance (Wikidata Q16635541))" + }, + { + "q": "Tell me the incident radiant flux with absorbed radiant flux of 18.3169 and absorptance of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [18.316867, 6.838598] (the system absorptance (Wikidata Q16635541))" + }, + { + "q": "What is the incident radiant flux for absorbed radiant flux 156.502 and absorptance 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [156.50181, 185.886273] (the system absorptance (Wikidata Q16635541))" + }, + { + "q": "Tell me the incident radiant flux with absorbed radiant flux of 156.502 and absorptance of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [156.50181, 185.886273] (the system absorptance (Wikidata Q16635541))" + }, + { + "q": "What is the incident radiant flux for absorbed radiant flux 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the absorptance is not given" + }, + { + "q": "Tell me the incident radiant flux with absorbed radiant flux of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the absorptance is not given" + }, + { + "q": "What is the incident radiant flux 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 incident radiant flux 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 incident radiant flux with absorbed radiant flux of -1.91195 and absorptance of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative absorbed radiant flux: no example had one" + }, + { + "q": "Which incident radiant flux 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 absorbed radiant flux is 8.50728 and the absorptance is 17.7888, what is the incident radiant flux?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [8.507283, 17.788778] (the system absorptance (Wikidata Q16635541))" + }, + { + "q": "Could you work out the incident radiant flux given absorbed radiant flux = 8.50728, absorptance = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [8.507283, 17.788778] (the system absorptance (Wikidata Q16635541))" + }, + { + "q": "I need the incident radiant flux for an absorbed radiant flux of 8.24951 and an absorptance of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [8.24951, 0.634021] (the system absorptance (Wikidata Q16635541))" + }, + { + "q": "If the absorbed radiant flux is 8.24951 and the absorptance is 0.634021, what is the incident radiant flux?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [8.24951, 0.634021] (the system absorptance (Wikidata Q16635541))" + }, + { + "q": "Could you work out the incident radiant flux given absorbed radiant flux = 0.318734, absorptance = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [0.318734, 9.566071] (the system absorptance (Wikidata Q16635541))" + }, + { + "q": "I need the incident radiant flux for an absorbed radiant flux of 0.318734 and an absorptance of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [0.318734, 9.566071] (the system absorptance (Wikidata Q16635541))" + }, + { + "q": "If the absorbed radiant flux is 0.671697 and the absorptance is 14.2496, what is the incident radiant flux?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [0.671697, 14.249569] (the system absorptance (Wikidata Q16635541))" + }, + { + "q": "Could you work out the incident radiant flux given absorbed radiant flux = 0.671697, absorptance = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [0.671697, 14.249569] (the system absorptance (Wikidata Q16635541))" + }, + { + "q": "I need the incident radiant flux for an absorbed radiant flux of 21.7931 and an absorptance of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [21.793126, 29.510609] (the system absorptance (Wikidata Q16635541))" + }, + { + "q": "If the absorbed radiant flux is 21.7931 and the absorptance is 29.5106, what is the incident radiant flux?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [21.793126, 29.510609] (the system absorptance (Wikidata Q16635541))" + }, + { + "q": "Could you work out the incident radiant flux given absorbed radiant flux = 153.15, absorptance = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [153.149965, 18.595315] (the system absorptance (Wikidata Q16635541))" + }, + { + "q": "I need the incident radiant flux for an absorbed radiant flux of 153.15 and an absorptance of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [153.149965, 18.595315] (the system absorptance (Wikidata Q16635541))" + }, + { + "q": "If the absorbed radiant flux is 8.50728, what is the incident radiant flux?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the absorptance is not given" + }, + { + "q": "Could you work out the incident radiant flux given absorbed radiant flux = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the absorptance is not given" + }, + { + "q": "Do you know the incident radiant flux 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 incident radiant flux 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 incident radiant flux given absorbed radiant flux = -0.318734, absorptance = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative absorbed radiant flux: no example had one" + }, + { + "q": "What is a typical incident radiant flux?", + "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-incident-radiant-flux-2/program.py b/root/organs10/physics/specialists/solved-incident-radiant-flux-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..fc492496b3c9b5715dd5d4169636f6774821f7bb --- /dev/null +++ b/root/organs10/physics/specialists/solved-incident-radiant-flux-2/program.py @@ -0,0 +1,7 @@ +"""incident radiant flux from absorbed radiant flux, absorptance (absorptance (Wikidata Q16635541)) +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(absorbed_radiant_flux, absorptance): + return 1.0*absorbed_radiant_flux/absorptance diff --git a/root/organs10/physics/specialists/solved-incident-radiant-flux-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-incident-radiant-flux-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-incident-radiant-flux-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-incident-radiant-flux-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-incident-radiant-flux-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-incident-radiant-flux-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-incident-radiant-flux-3/panel.json b/root/organs10/physics/specialists/solved-incident-radiant-flux-3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..3a9ad761cb587df567c290b834d973dd93fa1dcf --- /dev/null +++ b/root/organs10/physics/specialists/solved-incident-radiant-flux-3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the incident radiant flux for reflected radiant flux 107.961 and reflectance 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [107.960666, 59.4105] (the system reflectance (Wikidata Q663650))" + }, + { + "q": "Tell me the incident radiant flux with reflected radiant flux of 107.961 and reflectance of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [107.960666, 59.4105] (the system reflectance (Wikidata Q663650))" + }, + { + "q": "What is the incident radiant flux for reflected radiant flux 169.527 and reflectance 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [169.526958, 280.773702] (the system reflectance (Wikidata Q663650))" + }, + { + "q": "Tell me the incident radiant flux with reflected radiant flux of 169.527 and reflectance of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [169.526958, 280.773702] (the system reflectance (Wikidata Q663650))" + }, + { + "q": "What is the incident radiant flux for reflected radiant flux 1.91195 and reflectance 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [1.911952, 82.240072] (the system reflectance (Wikidata Q663650))" + }, + { + "q": "Tell me the incident radiant flux with reflected radiant flux of 1.91195 and reflectance of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [1.911952, 82.240072] (the system reflectance (Wikidata Q663650))" + }, + { + "q": "What is the incident radiant flux for reflected radiant flux 1.64379 and reflectance 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [1.643795, 252.709024] (the system reflectance (Wikidata Q663650))" + }, + { + "q": "Tell me the incident radiant flux with reflected radiant flux of 1.64379 and reflectance of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [1.643795, 252.709024] (the system reflectance (Wikidata Q663650))" + }, + { + "q": "What is the incident radiant flux for reflected radiant flux 18.3169 and reflectance 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [18.316867, 6.838598] (the system reflectance (Wikidata Q663650))" + }, + { + "q": "Tell me the incident radiant flux with reflected radiant flux of 18.3169 and reflectance of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [18.316867, 6.838598] (the system reflectance (Wikidata Q663650))" + }, + { + "q": "What is the incident radiant flux for reflected radiant flux 156.502 and reflectance 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [156.50181, 185.886273] (the system reflectance (Wikidata Q663650))" + }, + { + "q": "Tell me the incident radiant flux with reflected radiant flux of 156.502 and reflectance of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [156.50181, 185.886273] (the system reflectance (Wikidata Q663650))" + }, + { + "q": "What is the incident radiant flux for reflected radiant flux 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the reflectance is not given" + }, + { + "q": "Tell me the incident radiant flux with reflected radiant flux of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the reflectance is not given" + }, + { + "q": "What is the incident radiant flux 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 incident radiant flux 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 incident radiant flux with reflected radiant flux of -1.91195 and reflectance of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative reflected radiant flux: no example had one" + }, + { + "q": "Which incident radiant flux 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 reflected radiant flux is 8.50728 and the reflectance is 17.7888, what is the incident radiant flux?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [8.507283, 17.788778] (the system reflectance (Wikidata Q663650))" + }, + { + "q": "Could you work out the incident radiant flux given reflected radiant flux = 8.50728, reflectance = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [8.507283, 17.788778] (the system reflectance (Wikidata Q663650))" + }, + { + "q": "I need the incident radiant flux for a reflected radiant flux of 8.24951 and a reflectance of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [8.24951, 0.634021] (the system reflectance (Wikidata Q663650))" + }, + { + "q": "If the reflected radiant flux is 8.24951 and the reflectance is 0.634021, what is the incident radiant flux?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [8.24951, 0.634021] (the system reflectance (Wikidata Q663650))" + }, + { + "q": "Could you work out the incident radiant flux given reflected radiant flux = 0.318734, reflectance = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [0.318734, 9.566071] (the system reflectance (Wikidata Q663650))" + }, + { + "q": "I need the incident radiant flux for a reflected radiant flux of 0.318734 and a reflectance of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [0.318734, 9.566071] (the system reflectance (Wikidata Q663650))" + }, + { + "q": "If the reflected radiant flux is 0.671697 and the reflectance is 14.2496, what is the incident radiant flux?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [0.671697, 14.249569] (the system reflectance (Wikidata Q663650))" + }, + { + "q": "Could you work out the incident radiant flux given reflected radiant flux = 0.671697, reflectance = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [0.671697, 14.249569] (the system reflectance (Wikidata Q663650))" + }, + { + "q": "I need the incident radiant flux for a reflected radiant flux of 21.7931 and a reflectance of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [21.793126, 29.510609] (the system reflectance (Wikidata Q663650))" + }, + { + "q": "If the reflected radiant flux is 21.7931 and the reflectance is 29.5106, what is the incident radiant flux?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [21.793126, 29.510609] (the system reflectance (Wikidata Q663650))" + }, + { + "q": "Could you work out the incident radiant flux given reflected radiant flux = 153.15, reflectance = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [153.149965, 18.595315] (the system reflectance (Wikidata Q663650))" + }, + { + "q": "I need the incident radiant flux for a reflected radiant flux of 153.15 and a reflectance of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [153.149965, 18.595315] (the system reflectance (Wikidata Q663650))" + }, + { + "q": "If the reflected radiant flux is 8.50728, what is the incident radiant flux?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the reflectance is not given" + }, + { + "q": "Could you work out the incident radiant flux given reflected radiant flux = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the reflectance is not given" + }, + { + "q": "Do you know the incident radiant flux 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 incident radiant flux 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 incident radiant flux given reflected radiant flux = -0.318734, reflectance = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative reflected radiant flux: no example had one" + }, + { + "q": "What is a typical incident radiant flux?", + "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-incident-radiant-flux-3/program.py b/root/organs10/physics/specialists/solved-incident-radiant-flux-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..ace67829dedfceaa719dbb9d4d5b92195de81e6f --- /dev/null +++ b/root/organs10/physics/specialists/solved-incident-radiant-flux-3/program.py @@ -0,0 +1,7 @@ +"""incident radiant flux from reflected radiant flux, reflectance (reflectance (Wikidata Q663650)) +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(reflected_radiant_flux, reflectance): + return 1.0*reflected_radiant_flux/reflectance diff --git a/root/organs10/physics/specialists/solved-incident-radiant-flux-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-incident-radiant-flux-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-incident-radiant-flux-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-incident-radiant-flux/organ_formula_v2.py b/root/organs10/physics/specialists/solved-incident-radiant-flux/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-incident-radiant-flux/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-incident-radiant-flux/panel.json b/root/organs10/physics/specialists/solved-incident-radiant-flux/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..c5c49e8da50d608dc74ebc63d0a7788be31d864e --- /dev/null +++ b/root/organs10/physics/specialists/solved-incident-radiant-flux/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the incident radiant flux for transmitted radiant flux 107.961 and transmittance 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [107.960666, 59.4105] (the system transmittance (Wikidata Q1427863))" + }, + { + "q": "Tell me the incident radiant flux with transmitted radiant flux of 107.961 and transmittance of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [107.960666, 59.4105] (the system transmittance (Wikidata Q1427863))" + }, + { + "q": "What is the incident radiant flux for transmitted radiant flux 169.527 and transmittance 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [169.526958, 280.773702] (the system transmittance (Wikidata Q1427863))" + }, + { + "q": "Tell me the incident radiant flux with transmitted radiant flux of 169.527 and transmittance of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [169.526958, 280.773702] (the system transmittance (Wikidata Q1427863))" + }, + { + "q": "What is the incident radiant flux for transmitted radiant flux 1.91195 and transmittance 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [1.911952, 82.240072] (the system transmittance (Wikidata Q1427863))" + }, + { + "q": "Tell me the incident radiant flux with transmitted radiant flux of 1.91195 and transmittance of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [1.911952, 82.240072] (the system transmittance (Wikidata Q1427863))" + }, + { + "q": "What is the incident radiant flux for transmitted radiant flux 1.64379 and transmittance 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [1.643795, 252.709024] (the system transmittance (Wikidata Q1427863))" + }, + { + "q": "Tell me the incident radiant flux with transmitted radiant flux of 1.64379 and transmittance of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [1.643795, 252.709024] (the system transmittance (Wikidata Q1427863))" + }, + { + "q": "What is the incident radiant flux for transmitted radiant flux 18.3169 and transmittance 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [18.316867, 6.838598] (the system transmittance (Wikidata Q1427863))" + }, + { + "q": "Tell me the incident radiant flux with transmitted radiant flux of 18.3169 and transmittance of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [18.316867, 6.838598] (the system transmittance (Wikidata Q1427863))" + }, + { + "q": "What is the incident radiant flux for transmitted radiant flux 156.502 and transmittance 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [156.50181, 185.886273] (the system transmittance (Wikidata Q1427863))" + }, + { + "q": "Tell me the incident radiant flux with transmitted radiant flux of 156.502 and transmittance of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [156.50181, 185.886273] (the system transmittance (Wikidata Q1427863))" + }, + { + "q": "What is the incident radiant flux for transmitted radiant flux 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the transmittance is not given" + }, + { + "q": "Tell me the incident radiant flux with transmitted radiant flux of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the transmittance is not given" + }, + { + "q": "What is the incident radiant flux 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 incident radiant flux 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 incident radiant flux with transmitted radiant flux of -1.91195 and transmittance of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative transmitted radiant flux: no example had one" + }, + { + "q": "Which incident radiant flux 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 transmitted radiant flux is 8.50728 and the transmittance is 17.7888, what is the incident radiant flux?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [8.507283, 17.788778] (the system transmittance (Wikidata Q1427863))" + }, + { + "q": "Could you work out the incident radiant flux given transmitted radiant flux = 8.50728, transmittance = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [8.507283, 17.788778] (the system transmittance (Wikidata Q1427863))" + }, + { + "q": "I need the incident radiant flux for a transmitted radiant flux of 8.24951 and a transmittance of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [8.24951, 0.634021] (the system transmittance (Wikidata Q1427863))" + }, + { + "q": "If the transmitted radiant flux is 8.24951 and the transmittance is 0.634021, what is the incident radiant flux?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [8.24951, 0.634021] (the system transmittance (Wikidata Q1427863))" + }, + { + "q": "Could you work out the incident radiant flux given transmitted radiant flux = 0.318734, transmittance = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [0.318734, 9.566071] (the system transmittance (Wikidata Q1427863))" + }, + { + "q": "I need the incident radiant flux for a transmitted radiant flux of 0.318734 and a transmittance of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [0.318734, 9.566071] (the system transmittance (Wikidata Q1427863))" + }, + { + "q": "If the transmitted radiant flux is 0.671697 and the transmittance is 14.2496, what is the incident radiant flux?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [0.671697, 14.249569] (the system transmittance (Wikidata Q1427863))" + }, + { + "q": "Could you work out the incident radiant flux given transmitted radiant flux = 0.671697, transmittance = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [0.671697, 14.249569] (the system transmittance (Wikidata Q1427863))" + }, + { + "q": "I need the incident radiant flux for a transmitted radiant flux of 21.7931 and a transmittance of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [21.793126, 29.510609] (the system transmittance (Wikidata Q1427863))" + }, + { + "q": "If the transmitted radiant flux is 21.7931 and the transmittance is 29.5106, what is the incident radiant flux?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [21.793126, 29.510609] (the system transmittance (Wikidata Q1427863))" + }, + { + "q": "Could you work out the incident radiant flux given transmitted radiant flux = 153.15, transmittance = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [153.149965, 18.595315] (the system transmittance (Wikidata Q1427863))" + }, + { + "q": "I need the incident radiant flux for a transmitted radiant flux of 153.15 and a transmittance of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "W", + "why": "incident radiant flux for [153.149965, 18.595315] (the system transmittance (Wikidata Q1427863))" + }, + { + "q": "If the transmitted radiant flux is 8.50728, what is the incident radiant flux?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the transmittance is not given" + }, + { + "q": "Could you work out the incident radiant flux given transmitted radiant flux = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the transmittance is not given" + }, + { + "q": "Do you know the incident radiant flux 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 incident radiant flux 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 incident radiant flux given transmitted radiant flux = -0.318734, transmittance = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative transmitted radiant flux: no example had one" + }, + { + "q": "What is a typical incident radiant flux?", + "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-incident-radiant-flux/program.py b/root/organs10/physics/specialists/solved-incident-radiant-flux/program.py new file mode 100644 index 0000000000000000000000000000000000000000..82b7a9b1a621e3f6afaa80fed2cd3ed5210d0e89 --- /dev/null +++ b/root/organs10/physics/specialists/solved-incident-radiant-flux/program.py @@ -0,0 +1,7 @@ +"""incident radiant flux from transmitted radiant flux, transmittance (transmittance (Wikidata Q1427863)) +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(transmitted_radiant_flux, transmittance): + return 1.0*transmitted_radiant_flux/transmittance diff --git a/root/organs10/physics/specialists/solved-incident-radiant-flux/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-incident-radiant-flux/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-incident-radiant-flux/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-inductance/organ_formula_v2.py b/root/organs10/physics/specialists/solved-inductance/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-inductance/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-inductance/panel.json b/root/organs10/physics/specialists/solved-inductance/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..35519df37d1e5157a4e1234f22fe16f13277cec6 --- /dev/null +++ b/root/organs10/physics/specialists/solved-inductance/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the inductance for total magnetic flux 107.961 and electric current 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2*kg/s^2/A^2", + "why": "inductance for [107.960666, 59.4105] (the system inductance (Wikidata Q177897))" + }, + { + "q": "Tell me the inductance with total magnetic flux of 107.961 and electric current of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2*kg/s^2/A^2", + "why": "inductance for [107.960666, 59.4105] (the system inductance (Wikidata Q177897))" + }, + { + "q": "What is the inductance for total magnetic flux 169.527 and electric current 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2*kg/s^2/A^2", + "why": "inductance for [169.526958, 280.773702] (the system inductance (Wikidata Q177897))" + }, + { + "q": "Tell me the inductance with total magnetic flux of 169.527 and electric current of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2*kg/s^2/A^2", + "why": "inductance for [169.526958, 280.773702] (the system inductance (Wikidata Q177897))" + }, + { + "q": "What is the inductance for total magnetic flux 1.91195 and electric current 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2*kg/s^2/A^2", + "why": "inductance for [1.911952, 82.240072] (the system inductance (Wikidata Q177897))" + }, + { + "q": "Tell me the inductance with total magnetic flux of 1.91195 and electric current of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2*kg/s^2/A^2", + "why": "inductance for [1.911952, 82.240072] (the system inductance (Wikidata Q177897))" + }, + { + "q": "What is the inductance for total magnetic flux 1.64379 and electric current 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2*kg/s^2/A^2", + "why": "inductance for [1.643795, 252.709024] (the system inductance (Wikidata Q177897))" + }, + { + "q": "Tell me the inductance with total magnetic flux of 1.64379 and electric current of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2*kg/s^2/A^2", + "why": "inductance for [1.643795, 252.709024] (the system inductance (Wikidata Q177897))" + }, + { + "q": "What is the inductance for total magnetic flux 18.3169 and electric current 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2*kg/s^2/A^2", + "why": "inductance for [18.316867, 6.838598] (the system inductance (Wikidata Q177897))" + }, + { + "q": "Tell me the inductance with total magnetic flux of 18.3169 and electric current of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2*kg/s^2/A^2", + "why": "inductance for [18.316867, 6.838598] (the system inductance (Wikidata Q177897))" + }, + { + "q": "What is the inductance for total magnetic flux 156.502 and electric current 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2*kg/s^2/A^2", + "why": "inductance for [156.50181, 185.886273] (the system inductance (Wikidata Q177897))" + }, + { + "q": "Tell me the inductance with total magnetic flux of 156.502 and electric current of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2*kg/s^2/A^2", + "why": "inductance for [156.50181, 185.886273] (the system inductance (Wikidata Q177897))" + }, + { + "q": "What is the inductance for total magnetic flux 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current is not given" + }, + { + "q": "Tell me the inductance with total magnetic flux of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current is not given" + }, + { + "q": "What is the inductance 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 inductance 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 inductance with total magnetic flux of -1.91195 and electric current of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative total magnetic flux: no example had one" + }, + { + "q": "Which inductance 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 magnetic flux is 8.50728 and the electric current is 17.7888, what is the inductance?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2*kg/s^2/A^2", + "why": "inductance for [8.507283, 17.788778] (the system inductance (Wikidata Q177897))" + }, + { + "q": "Could you work out the inductance given total magnetic flux = 8.50728, electric current = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2*kg/s^2/A^2", + "why": "inductance for [8.507283, 17.788778] (the system inductance (Wikidata Q177897))" + }, + { + "q": "I need the inductance for a total magnetic flux of 8.24951 and an electric current of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2*kg/s^2/A^2", + "why": "inductance for [8.24951, 0.634021] (the system inductance (Wikidata Q177897))" + }, + { + "q": "If the total magnetic flux is 8.24951 and the electric current is 0.634021, what is the inductance?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2*kg/s^2/A^2", + "why": "inductance for [8.24951, 0.634021] (the system inductance (Wikidata Q177897))" + }, + { + "q": "Could you work out the inductance given total magnetic flux = 0.318734, electric current = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2*kg/s^2/A^2", + "why": "inductance for [0.318734, 9.566071] (the system inductance (Wikidata Q177897))" + }, + { + "q": "I need the inductance for a total magnetic flux of 0.318734 and an electric current of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2*kg/s^2/A^2", + "why": "inductance for [0.318734, 9.566071] (the system inductance (Wikidata Q177897))" + }, + { + "q": "If the total magnetic flux is 0.671697 and the electric current is 14.2496, what is the inductance?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2*kg/s^2/A^2", + "why": "inductance for [0.671697, 14.249569] (the system inductance (Wikidata Q177897))" + }, + { + "q": "Could you work out the inductance given total magnetic flux = 0.671697, electric current = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2*kg/s^2/A^2", + "why": "inductance for [0.671697, 14.249569] (the system inductance (Wikidata Q177897))" + }, + { + "q": "I need the inductance for a total magnetic flux of 21.7931 and an electric current of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2*kg/s^2/A^2", + "why": "inductance for [21.793126, 29.510609] (the system inductance (Wikidata Q177897))" + }, + { + "q": "If the total magnetic flux is 21.7931 and the electric current is 29.5106, what is the inductance?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2*kg/s^2/A^2", + "why": "inductance for [21.793126, 29.510609] (the system inductance (Wikidata Q177897))" + }, + { + "q": "Could you work out the inductance given total magnetic flux = 153.15, electric current = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2*kg/s^2/A^2", + "why": "inductance for [153.149965, 18.595315] (the system inductance (Wikidata Q177897))" + }, + { + "q": "I need the inductance for a total magnetic flux of 153.15 and an electric current of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2*kg/s^2/A^2", + "why": "inductance for [153.149965, 18.595315] (the system inductance (Wikidata Q177897))" + }, + { + "q": "If the total magnetic flux is 8.50728, what is the inductance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current is not given" + }, + { + "q": "Could you work out the inductance given total magnetic flux = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current is not given" + }, + { + "q": "Do you know the inductance 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 inductance 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 inductance given total magnetic flux = -0.318734, electric current = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative total magnetic flux: no example had one" + }, + { + "q": "What is a typical inductance?", + "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-inductance/program.py b/root/organs10/physics/specialists/solved-inductance/program.py new file mode 100644 index 0000000000000000000000000000000000000000..d25114ef6ab54a9080af6db3472d5800bcf2900e --- /dev/null +++ b/root/organs10/physics/specialists/solved-inductance/program.py @@ -0,0 +1,7 @@ +"""inductance from total magnetic flux, electric current (inductance (Wikidata Q177897)) +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(total_magnetic_flux, electric_current): + return 1.0*total_magnetic_flux/electric_current diff --git a/root/organs10/physics/specialists/solved-inductance/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-inductance/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-inductance/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-inertia-tensor/organ_formula_v2.py b/root/organs10/physics/specialists/solved-inertia-tensor/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-inertia-tensor/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-inertia-tensor/panel.json b/root/organs10/physics/specialists/solved-inertia-tensor/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..a54826e76f589b38781ad063c08dbad7f10e640e --- /dev/null +++ b/root/organs10/physics/specialists/solved-inertia-tensor/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the inertia tensor for angular momentum 107.961 and angular velocity 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2*kg", + "why": "inertia tensor for [107.960666, 59.4105] (the system inertia tensor (Wikidata Q4454677))" + }, + { + "q": "Tell me the inertia tensor with angular momentum of 107.961 and angular velocity of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2*kg", + "why": "inertia tensor for [107.960666, 59.4105] (the system inertia tensor (Wikidata Q4454677))" + }, + { + "q": "What is the inertia tensor for angular momentum 169.527 and angular velocity 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2*kg", + "why": "inertia tensor for [169.526958, 280.773702] (the system inertia tensor (Wikidata Q4454677))" + }, + { + "q": "Tell me the inertia tensor with angular momentum of 169.527 and angular velocity of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2*kg", + "why": "inertia tensor for [169.526958, 280.773702] (the system inertia tensor (Wikidata Q4454677))" + }, + { + "q": "What is the inertia tensor for angular momentum 1.91195 and angular velocity 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2*kg", + "why": "inertia tensor for [1.911952, 82.240072] (the system inertia tensor (Wikidata Q4454677))" + }, + { + "q": "Tell me the inertia tensor with angular momentum of 1.91195 and angular velocity of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2*kg", + "why": "inertia tensor for [1.911952, 82.240072] (the system inertia tensor (Wikidata Q4454677))" + }, + { + "q": "What is the inertia tensor for angular momentum 1.64379 and angular velocity 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2*kg", + "why": "inertia tensor for [1.643795, 252.709024] (the system inertia tensor (Wikidata Q4454677))" + }, + { + "q": "Tell me the inertia tensor with angular momentum of 1.64379 and angular velocity of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2*kg", + "why": "inertia tensor for [1.643795, 252.709024] (the system inertia tensor (Wikidata Q4454677))" + }, + { + "q": "What is the inertia tensor for angular momentum 18.3169 and angular velocity 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2*kg", + "why": "inertia tensor for [18.316867, 6.838598] (the system inertia tensor (Wikidata Q4454677))" + }, + { + "q": "Tell me the inertia tensor with angular momentum of 18.3169 and angular velocity of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2*kg", + "why": "inertia tensor for [18.316867, 6.838598] (the system inertia tensor (Wikidata Q4454677))" + }, + { + "q": "What is the inertia tensor for angular momentum 156.502 and angular velocity 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2*kg", + "why": "inertia tensor for [156.50181, 185.886273] (the system inertia tensor (Wikidata Q4454677))" + }, + { + "q": "Tell me the inertia tensor with angular momentum of 156.502 and angular velocity of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2*kg", + "why": "inertia tensor for [156.50181, 185.886273] (the system inertia tensor (Wikidata Q4454677))" + }, + { + "q": "What is the inertia tensor for angular momentum 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the angular velocity is not given" + }, + { + "q": "Tell me the inertia tensor with angular momentum of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the angular velocity is not given" + }, + { + "q": "What is the inertia tensor 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 inertia tensor 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 inertia tensor with angular momentum of -1.91195 and angular velocity of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative angular momentum: no example had one" + }, + { + "q": "Which inertia tensor 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 angular momentum is 8.50728 and the angular velocity is 17.7888, what is the inertia tensor?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2*kg", + "why": "inertia tensor for [8.507283, 17.788778] (the system inertia tensor (Wikidata Q4454677))" + }, + { + "q": "Could you work out the inertia tensor given angular momentum = 8.50728, angular velocity = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2*kg", + "why": "inertia tensor for [8.507283, 17.788778] (the system inertia tensor (Wikidata Q4454677))" + }, + { + "q": "I need the inertia tensor for an angular momentum of 8.24951 and an angular velocity of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2*kg", + "why": "inertia tensor for [8.24951, 0.634021] (the system inertia tensor (Wikidata Q4454677))" + }, + { + "q": "If the angular momentum is 8.24951 and the angular velocity is 0.634021, what is the inertia tensor?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2*kg", + "why": "inertia tensor for [8.24951, 0.634021] (the system inertia tensor (Wikidata Q4454677))" + }, + { + "q": "Could you work out the inertia tensor given angular momentum = 0.318734, angular velocity = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2*kg", + "why": "inertia tensor for [0.318734, 9.566071] (the system inertia tensor (Wikidata Q4454677))" + }, + { + "q": "I need the inertia tensor for an angular momentum of 0.318734 and an angular velocity of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2*kg", + "why": "inertia tensor for [0.318734, 9.566071] (the system inertia tensor (Wikidata Q4454677))" + }, + { + "q": "If the angular momentum is 0.671697 and the angular velocity is 14.2496, what is the inertia tensor?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2*kg", + "why": "inertia tensor for [0.671697, 14.249569] (the system inertia tensor (Wikidata Q4454677))" + }, + { + "q": "Could you work out the inertia tensor given angular momentum = 0.671697, angular velocity = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2*kg", + "why": "inertia tensor for [0.671697, 14.249569] (the system inertia tensor (Wikidata Q4454677))" + }, + { + "q": "I need the inertia tensor for an angular momentum of 21.7931 and an angular velocity of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2*kg", + "why": "inertia tensor for [21.793126, 29.510609] (the system inertia tensor (Wikidata Q4454677))" + }, + { + "q": "If the angular momentum is 21.7931 and the angular velocity is 29.5106, what is the inertia tensor?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2*kg", + "why": "inertia tensor for [21.793126, 29.510609] (the system inertia tensor (Wikidata Q4454677))" + }, + { + "q": "Could you work out the inertia tensor given angular momentum = 153.15, angular velocity = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2*kg", + "why": "inertia tensor for [153.149965, 18.595315] (the system inertia tensor (Wikidata Q4454677))" + }, + { + "q": "I need the inertia tensor for an angular momentum of 153.15 and an angular velocity of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2*kg", + "why": "inertia tensor for [153.149965, 18.595315] (the system inertia tensor (Wikidata Q4454677))" + }, + { + "q": "If the angular momentum is 8.50728, what is the inertia tensor?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the angular velocity is not given" + }, + { + "q": "Could you work out the inertia tensor given angular momentum = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the angular velocity is not given" + }, + { + "q": "Do you know the inertia tensor 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 inertia tensor 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 inertia tensor given angular momentum = -0.318734, angular velocity = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative angular momentum: no example had one" + }, + { + "q": "What is a typical inertia tensor?", + "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-inertia-tensor/program.py b/root/organs10/physics/specialists/solved-inertia-tensor/program.py new file mode 100644 index 0000000000000000000000000000000000000000..fc82238d985f0e2bd57d93b0a8039089bdc05b3c --- /dev/null +++ b/root/organs10/physics/specialists/solved-inertia-tensor/program.py @@ -0,0 +1,7 @@ +"""inertia tensor from angular momentum, angular velocity (inertia tensor (Wikidata Q4454677)) +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(angular_momentum, angular_velocity): + return 1.0*angular_momentum/angular_velocity diff --git a/root/organs10/physics/specialists/solved-inertia-tensor/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-inertia-tensor/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-inertia-tensor/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-initial-amount-of-substance/organ_formula_v2.py b/root/organs10/physics/specialists/solved-initial-amount-of-substance/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-initial-amount-of-substance/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-initial-amount-of-substance/panel.json b/root/organs10/physics/specialists/solved-initial-amount-of-substance/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..77a3f903c21b7b02cf3dd8a8fb3d16026a9e0235 --- /dev/null +++ b/root/organs10/physics/specialists/solved-initial-amount-of-substance/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the initial amount of substance for equilibrium amount of substance 71.0225, stoichiometric number 2.57003 and extent of reaction 8.50728?", + "a": "49.15849829712309", + "valid": true, + "kind": "compute", + "expect": 49.15849829712309, + "dtype": "quantity", + "unit": "mol", + "why": "initial amount of substance for [71.022469, 2.57003, 8.507283] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "Tell me the initial amount of substance with equilibrium amount of substance of 71.0225, stoichiometric number of 2.57003 and extent of reaction of 8.50728.", + "a": "49.15849829712309", + "valid": true, + "kind": "compute", + "expect": 49.15849829712309, + "dtype": "quantity", + "unit": "mol", + "why": "initial amount of substance for [71.022469, 2.57003, 8.507283] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "What is the initial amount of substance for equilibrium amount of substance 82.2401, stoichiometric number 13.999 and extent of reaction 0.348438?", + "a": "77.36228281993988", + "valid": true, + "kind": "compute", + "expect": 77.36228281993988, + "dtype": "quantity", + "unit": "mol", + "why": "initial amount of substance for [82.240072, 13.999029, 0.348438] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "Tell me the initial amount of substance with equilibrium amount of substance of 82.2401, stoichiometric number of 13.999 and extent of reaction of 0.348438.", + "a": "77.36228281993988", + "valid": true, + "kind": "compute", + "expect": 77.36228281993988, + "dtype": "quantity", + "unit": "mol", + "why": "initial amount of substance for [82.240072, 13.999029, 0.348438] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "What is the initial amount of substance for equilibrium amount of substance 6.98086, stoichiometric number 0.551236 and extent of reaction 2.88512?", + "a": "5.390475225481023", + "valid": true, + "kind": "compute", + "expect": 5.390475225481023, + "dtype": "quantity", + "unit": "mol", + "why": "initial amount of substance for [6.980857, 0.551236, 2.88512] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "Tell me the initial amount of substance with equilibrium amount of substance of 6.98086, stoichiometric number of 0.551236 and extent of reaction of 2.88512.", + "a": "5.390475225481023", + "valid": true, + "kind": "compute", + "expect": 5.390475225481023, + "dtype": "quantity", + "unit": "mol", + "why": "initial amount of substance for [6.980857, 0.551236, 2.88512] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "What is the initial amount of substance for equilibrium amount of substance 185.886, stoichiometric number 13.2373 and extent of reaction 4.71941?", + "a": "123.41391091072475", + "valid": true, + "kind": "compute", + "expect": 123.41391091072475, + "dtype": "quantity", + "unit": "mol", + "why": "initial amount of substance for [185.886273, 13.237311, 4.719415] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "Tell me the initial amount of substance with equilibrium amount of substance of 185.886, stoichiometric number of 13.2373 and extent of reaction of 4.71941.", + "a": "123.41391091072475", + "valid": true, + "kind": "compute", + "expect": 123.41391091072475, + "dtype": "quantity", + "unit": "mol", + "why": "initial amount of substance for [185.886273, 13.237311, 4.719415] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "What is the initial amount of substance for equilibrium amount of substance 88.6624, stoichiometric number 0.633611 and extent of reaction 0.869111?", + "a": "88.11168212986358", + "valid": true, + "kind": "compute", + "expect": 88.11168212986358, + "dtype": "quantity", + "unit": "mol", + "why": "initial amount of substance for [88.662361, 0.633611, 0.869111] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "Tell me the initial amount of substance with equilibrium amount of substance of 88.6624, stoichiometric number of 0.633611 and extent of reaction of 0.869111.", + "a": "88.11168212986358", + "valid": true, + "kind": "compute", + "expect": 88.11168212986358, + "dtype": "quantity", + "unit": "mol", + "why": "initial amount of substance for [88.662361, 0.633611, 0.869111] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "What is the initial amount of substance for equilibrium amount of substance 3.21921, stoichiometric number 0.4336 and extent of reaction 2.20115?", + "a": "2.2647938602064017", + "valid": true, + "kind": "compute", + "expect": 2.2647938602064017, + "dtype": "quantity", + "unit": "mol", + "why": "initial amount of substance for [3.219213, 0.4336, 2.201149] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "Tell me the initial amount of substance with equilibrium amount of substance of 3.21921, stoichiometric number of 0.4336 and extent of reaction of 2.20115.", + "a": "2.2647938602064017", + "valid": true, + "kind": "compute", + "expect": 2.2647938602064017, + "dtype": "quantity", + "unit": "mol", + "why": "initial amount of substance for [3.219213, 0.4336, 2.201149] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "What is the initial amount of substance for equilibrium amount of substance 71.0225 and stoichiometric number 2.57003?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the extent of reaction is not given" + }, + { + "q": "Tell me the initial amount of substance with equilibrium amount of substance of 82.2401 and stoichiometric number of 13.999.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the extent of reaction is not given" + }, + { + "q": "What is the initial amount of substance 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 initial amount of substance 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 initial amount of substance with equilibrium amount of substance of -6.98086, stoichiometric number of 0.551236 and extent of reaction of 2.88512.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative equilibrium amount of substance: no example had one" + }, + { + "q": "Which initial amount of substance 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 equilibrium amount of substance is 8.24951, stoichiometric number is 0.634021 and the extent of reaction is 6.34623, what is the initial amount of substance?", + "a": "4.2258697413180455", + "valid": true, + "kind": "compute", + "expect": 4.2258697413180455, + "dtype": "quantity", + "unit": "mol", + "why": "initial amount of substance for [8.24951, 0.634021, 6.346226] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "Could you work out the initial amount of substance given equilibrium amount of substance = 8.24951, stoichiometric number = 0.634021, extent of reaction = 6.34623?", + "a": "4.2258697413180455", + "valid": true, + "kind": "compute", + "expect": 4.2258697413180455, + "dtype": "quantity", + "unit": "mol", + "why": "initial amount of substance for [8.24951, 0.634021, 6.346226] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "I need the initial amount of substance for an equilibrium amount of substance of 126.709, a stoichiometric number of 1.70503 and an extent of reaction of 2.9896, please.", + "a": "121.61188696128404", + "valid": true, + "kind": "compute", + "expect": 121.61188696128404, + "dtype": "quantity", + "unit": "mol", + "why": "initial amount of substance for [126.709252, 1.705032, 2.9896] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "If the equilibrium amount of substance is 126.709, stoichiometric number is 1.70503 and the extent of reaction is 2.9896, what is the initial amount of substance?", + "a": "121.61188696128404", + "valid": true, + "kind": "compute", + "expect": 121.61188696128404, + "dtype": "quantity", + "unit": "mol", + "why": "initial amount of substance for [126.709252, 1.705032, 2.9896] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "Could you work out the initial amount of substance given equilibrium amount of substance = 2.34957, stoichiometric number = 1.16997, extent of reaction = 1.14865?", + "a": "1.0056842074832226", + "valid": true, + "kind": "compute", + "expect": 1.0056842074832226, + "dtype": "quantity", + "unit": "mol", + "why": "initial amount of substance for [2.349571, 1.169968, 1.148652] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "I need the initial amount of substance for an equilibrium amount of substance of 2.34957, a stoichiometric number of 1.16997 and an extent of reaction of 1.14865, please.", + "a": "1.0056842074832226", + "valid": true, + "kind": "compute", + "expect": 1.0056842074832226, + "dtype": "quantity", + "unit": "mol", + "why": "initial amount of substance for [2.349571, 1.169968, 1.148652] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "If the equilibrium amount of substance is 223.509, stoichiometric number is 17.3414 and the extent of reaction is 7.10705, what is the initial amount of substance?", + "a": "100.26236519388404", + "valid": true, + "kind": "compute", + "expect": 100.26236519388404, + "dtype": "quantity", + "unit": "mol", + "why": "initial amount of substance for [223.508796, 17.341435, 7.107049] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "Could you work out the initial amount of substance given equilibrium amount of substance = 223.509, stoichiometric number = 17.3414, extent of reaction = 7.10705?", + "a": "100.26236519388404", + "valid": true, + "kind": "compute", + "expect": 100.26236519388404, + "dtype": "quantity", + "unit": "mol", + "why": "initial amount of substance for [223.508796, 17.341435, 7.107049] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "I need the initial amount of substance for an equilibrium amount of substance of 276.408, a stoichiometric number of 0.406608 and an extent of reaction of 0.367206, please.", + "a": "276.2582732193404", + "valid": true, + "kind": "compute", + "expect": 276.2582732193404, + "dtype": "quantity", + "unit": "mol", + "why": "initial amount of substance for [276.407582, 0.406608, 0.367206] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "If the equilibrium amount of substance is 276.408, stoichiometric number is 0.406608 and the extent of reaction is 0.367206, what is the initial amount of substance?", + "a": "276.2582732193404", + "valid": true, + "kind": "compute", + "expect": 276.2582732193404, + "dtype": "quantity", + "unit": "mol", + "why": "initial amount of substance for [276.407582, 0.406608, 0.367206] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "Could you work out the initial amount of substance given equilibrium amount of substance = 133.743, stoichiometric number = 16.0123, extent of reaction = 5.53629?", + "a": "45.093806527281856", + "valid": true, + "kind": "compute", + "expect": 45.093806527281856, + "dtype": "quantity", + "unit": "mol", + "why": "initial amount of substance for [133.742727, 16.01232, 5.536295] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "I need the initial amount of substance for an equilibrium amount of substance of 133.743, a stoichiometric number of 16.0123 and an extent of reaction of 5.53629, please.", + "a": "45.093806527281856", + "valid": true, + "kind": "compute", + "expect": 45.093806527281856, + "dtype": "quantity", + "unit": "mol", + "why": "initial amount of substance for [133.742727, 16.01232, 5.536295] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "If the equilibrium amount of substance is 8.24951 and the stoichiometric number is 0.634021, what is the initial amount of substance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the extent of reaction is not given" + }, + { + "q": "Could you work out the initial amount of substance given equilibrium amount of substance = 126.709, stoichiometric number = 1.70503?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the extent of reaction is not given" + }, + { + "q": "Do you know the initial amount of substance 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 initial amount of substance 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 initial amount of substance given equilibrium amount of substance = -2.34957, stoichiometric number = 1.16997, extent of reaction = 1.14865?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative equilibrium amount of substance: no example had one" + }, + { + "q": "What is a typical initial amount of substance?", + "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-initial-amount-of-substance/program.py b/root/organs10/physics/specialists/solved-initial-amount-of-substance/program.py new file mode 100644 index 0000000000000000000000000000000000000000..9e1119d08b692fbfd2e178dc4924e0569873fb0f --- /dev/null +++ b/root/organs10/physics/specialists/solved-initial-amount-of-substance/program.py @@ -0,0 +1,7 @@ +"""initial amount of substance from equilibrium amount of substance, stoichiometric number, extent of reaction (extent of reaction (Wikidata Q899046)) +The maths organ (systems.py): solved from the system q3 = ((q0 - q1)/(q2)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(equilibrium_amount_of_substance, stoichiometric_number, extent_of_reaction): + return 1.0*equilibrium_amount_of_substance - 1.0*extent_of_reaction*stoichiometric_number diff --git a/root/organs10/physics/specialists/solved-initial-amount-of-substance/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-initial-amount-of-substance/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-initial-amount-of-substance/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-initial-kinetic-energy-of-an-/organ_formula_v2.py b/root/organs10/physics/specialists/solved-initial-kinetic-energy-of-an-/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-initial-kinetic-energy-of-an-/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-initial-kinetic-energy-of-an-/panel.json b/root/organs10/physics/specialists/solved-initial-kinetic-energy-of-an-/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..e54653477c20837b0abca85ceac1089502e590b5 --- /dev/null +++ b/root/organs10/physics/specialists/solved-initial-kinetic-energy-of-an-/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the initial kinetic energy of an ionizing charged particle for average energy loss per elementary charge produced 107.961 and total ionization 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "J", + "why": "initial kinetic energy of an ionizing charged particle for [107.960666, 59.4105] (the system average energy loss per elementary charge produced (Wikidata Q98793042))" + }, + { + "q": "Tell me the initial kinetic energy of an ionizing charged particle with average energy loss per elementary charge produced of 107.961 and total ionization of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "J", + "why": "initial kinetic energy of an ionizing charged particle for [107.960666, 59.4105] (the system average energy loss per elementary charge produced (Wikidata Q98793042))" + }, + { + "q": "What is the initial kinetic energy of an ionizing charged particle for average energy loss per elementary charge produced 169.527 and total ionization 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "J", + "why": "initial kinetic energy of an ionizing charged particle for [169.526958, 280.773702] (the system average energy loss per elementary charge produced (Wikidata Q98793042))" + }, + { + "q": "Tell me the initial kinetic energy of an ionizing charged particle with average energy loss per elementary charge produced of 169.527 and total ionization of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "J", + "why": "initial kinetic energy of an ionizing charged particle for [169.526958, 280.773702] (the system average energy loss per elementary charge produced (Wikidata Q98793042))" + }, + { + "q": "What is the initial kinetic energy of an ionizing charged particle for average energy loss per elementary charge produced 1.91195 and total ionization 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "J", + "why": "initial kinetic energy of an ionizing charged particle for [1.911952, 82.240072] (the system average energy loss per elementary charge produced (Wikidata Q98793042))" + }, + { + "q": "Tell me the initial kinetic energy of an ionizing charged particle with average energy loss per elementary charge produced of 1.91195 and total ionization of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "J", + "why": "initial kinetic energy of an ionizing charged particle for [1.911952, 82.240072] (the system average energy loss per elementary charge produced (Wikidata Q98793042))" + }, + { + "q": "What is the initial kinetic energy of an ionizing charged particle for average energy loss per elementary charge produced 1.64379 and total ionization 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "J", + "why": "initial kinetic energy of an ionizing charged particle for [1.643795, 252.709024] (the system average energy loss per elementary charge produced (Wikidata Q98793042))" + }, + { + "q": "Tell me the initial kinetic energy of an ionizing charged particle with average energy loss per elementary charge produced of 1.64379 and total ionization of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "J", + "why": "initial kinetic energy of an ionizing charged particle for [1.643795, 252.709024] (the system average energy loss per elementary charge produced (Wikidata Q98793042))" + }, + { + "q": "What is the initial kinetic energy of an ionizing charged particle for average energy loss per elementary charge produced 18.3169 and total ionization 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "J", + "why": "initial kinetic energy of an ionizing charged particle for [18.316867, 6.838598] (the system average energy loss per elementary charge produced (Wikidata Q98793042))" + }, + { + "q": "Tell me the initial kinetic energy of an ionizing charged particle with average energy loss per elementary charge produced of 18.3169 and total ionization of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "J", + "why": "initial kinetic energy of an ionizing charged particle for [18.316867, 6.838598] (the system average energy loss per elementary charge produced (Wikidata Q98793042))" + }, + { + "q": "What is the initial kinetic energy of an ionizing charged particle for average energy loss per elementary charge produced 156.502 and total ionization 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "J", + "why": "initial kinetic energy of an ionizing charged particle for [156.50181, 185.886273] (the system average energy loss per elementary charge produced (Wikidata Q98793042))" + }, + { + "q": "Tell me the initial kinetic energy of an ionizing charged particle with average energy loss per elementary charge produced of 156.502 and total ionization of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "J", + "why": "initial kinetic energy of an ionizing charged particle for [156.50181, 185.886273] (the system average energy loss per elementary charge produced (Wikidata Q98793042))" + }, + { + "q": "What is the initial kinetic energy of an ionizing charged particle for average energy loss per elementary charge produced 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total ionization is not given" + }, + { + "q": "Tell me the initial kinetic energy of an ionizing charged particle with average energy loss per elementary charge produced of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total ionization is not given" + }, + { + "q": "What is the initial kinetic energy of an ionizing charged particle 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 initial kinetic energy of an ionizing charged particle 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 initial kinetic energy of an ionizing charged particle with average energy loss per elementary charge produced of -1.91195 and total ionization of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative average energy loss per elementary charge produced: no example had one" + }, + { + "q": "Which initial kinetic energy of an ionizing charged particle 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 average energy loss per elementary charge produced is 8.50728 and the total ionization is 17.7888, what is the initial kinetic energy of an ionizing charged particle?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "J", + "why": "initial kinetic energy of an ionizing charged particle for [8.507283, 17.788778] (the system average energy loss per elementary charge produced (Wikidata Q98793042))" + }, + { + "q": "Could you work out the initial kinetic energy of an ionizing charged particle given average energy loss per elementary charge produced = 8.50728, total ionization = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "J", + "why": "initial kinetic energy of an ionizing charged particle for [8.507283, 17.788778] (the system average energy loss per elementary charge produced (Wikidata Q98793042))" + }, + { + "q": "I need the initial kinetic energy of an ionizing charged particle for an average energy loss per elementary charge produced of 8.24951 and a total ionization of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "J", + "why": "initial kinetic energy of an ionizing charged particle for [8.24951, 0.634021] (the system average energy loss per elementary charge produced (Wikidata Q98793042))" + }, + { + "q": "If the average energy loss per elementary charge produced is 8.24951 and the total ionization is 0.634021, what is the initial kinetic energy of an ionizing charged particle?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "J", + "why": "initial kinetic energy of an ionizing charged particle for [8.24951, 0.634021] (the system average energy loss per elementary charge produced (Wikidata Q98793042))" + }, + { + "q": "Could you work out the initial kinetic energy of an ionizing charged particle given average energy loss per elementary charge produced = 0.318734, total ionization = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "J", + "why": "initial kinetic energy of an ionizing charged particle for [0.318734, 9.566071] (the system average energy loss per elementary charge produced (Wikidata Q98793042))" + }, + { + "q": "I need the initial kinetic energy of an ionizing charged particle for an average energy loss per elementary charge produced of 0.318734 and a total ionization of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "J", + "why": "initial kinetic energy of an ionizing charged particle for [0.318734, 9.566071] (the system average energy loss per elementary charge produced (Wikidata Q98793042))" + }, + { + "q": "If the average energy loss per elementary charge produced is 0.671697 and the total ionization is 14.2496, what is the initial kinetic energy of an ionizing charged particle?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "J", + "why": "initial kinetic energy of an ionizing charged particle for [0.671697, 14.249569] (the system average energy loss per elementary charge produced (Wikidata Q98793042))" + }, + { + "q": "Could you work out the initial kinetic energy of an ionizing charged particle given average energy loss per elementary charge produced = 0.671697, total ionization = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "J", + "why": "initial kinetic energy of an ionizing charged particle for [0.671697, 14.249569] (the system average energy loss per elementary charge produced (Wikidata Q98793042))" + }, + { + "q": "I need the initial kinetic energy of an ionizing charged particle for an average energy loss per elementary charge produced of 21.7931 and a total ionization of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "J", + "why": "initial kinetic energy of an ionizing charged particle for [21.793126, 29.510609] (the system average energy loss per elementary charge produced (Wikidata Q98793042))" + }, + { + "q": "If the average energy loss per elementary charge produced is 21.7931 and the total ionization is 29.5106, what is the initial kinetic energy of an ionizing charged particle?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "J", + "why": "initial kinetic energy of an ionizing charged particle for [21.793126, 29.510609] (the system average energy loss per elementary charge produced (Wikidata Q98793042))" + }, + { + "q": "Could you work out the initial kinetic energy of an ionizing charged particle given average energy loss per elementary charge produced = 153.15, total ionization = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "J", + "why": "initial kinetic energy of an ionizing charged particle for [153.149965, 18.595315] (the system average energy loss per elementary charge produced (Wikidata Q98793042))" + }, + { + "q": "I need the initial kinetic energy of an ionizing charged particle for an average energy loss per elementary charge produced of 153.15 and a total ionization of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "J", + "why": "initial kinetic energy of an ionizing charged particle for [153.149965, 18.595315] (the system average energy loss per elementary charge produced (Wikidata Q98793042))" + }, + { + "q": "If the average energy loss per elementary charge produced is 8.50728, what is the initial kinetic energy of an ionizing charged particle?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total ionization is not given" + }, + { + "q": "Could you work out the initial kinetic energy of an ionizing charged particle given average energy loss per elementary charge produced = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total ionization is not given" + }, + { + "q": "Do you know the initial kinetic energy of an ionizing charged particle 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 initial kinetic energy of an ionizing charged particle 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 initial kinetic energy of an ionizing charged particle given average energy loss per elementary charge produced = -0.318734, total ionization = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative average energy loss per elementary charge produced: no example had one" + }, + { + "q": "What is a typical initial kinetic energy of an ionizing charged particle?", + "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-initial-kinetic-energy-of-an-/program.py b/root/organs10/physics/specialists/solved-initial-kinetic-energy-of-an-/program.py new file mode 100644 index 0000000000000000000000000000000000000000..d3acb52d13041b569d0cc49626da23962d813827 --- /dev/null +++ b/root/organs10/physics/specialists/solved-initial-kinetic-energy-of-an-/program.py @@ -0,0 +1,7 @@ +"""initial kinetic energy of an ionizing charged particle from average energy loss per elementary charge produced, total ionization (average energy loss per elementary charge produced (Wikidata Q98793042)) +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(average_energy_loss_per_elementary_charge_produced, total_ionization): + return 1.0*average_energy_loss_per_elementary_charge_produced*total_ionization diff --git a/root/organs10/physics/specialists/solved-initial-kinetic-energy-of-an-/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-initial-kinetic-energy-of-an-/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-initial-kinetic-energy-of-an-/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-input-power-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-input-power-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-input-power-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-input-power-2/panel.json b/root/organs10/physics/specialists/solved-input-power-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..5b2af182f1434e4f9cf0f560373684df757f2c4b --- /dev/null +++ b/root/organs10/physics/specialists/solved-input-power-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the input power for output power 107.961 and power efficiency 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "W", + "why": "input power for [107.960666, 59.4105] (the system power efficiency (Wikidata Q11945244))" + }, + { + "q": "Tell me the input power with output power of 107.961 and power efficiency of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "W", + "why": "input power for [107.960666, 59.4105] (the system power efficiency (Wikidata Q11945244))" + }, + { + "q": "What is the input power for output power 169.527 and power efficiency 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "W", + "why": "input power for [169.526958, 280.773702] (the system power efficiency (Wikidata Q11945244))" + }, + { + "q": "Tell me the input power with output power of 169.527 and power efficiency of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "W", + "why": "input power for [169.526958, 280.773702] (the system power efficiency (Wikidata Q11945244))" + }, + { + "q": "What is the input power for output power 1.91195 and power efficiency 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "W", + "why": "input power for [1.911952, 82.240072] (the system power efficiency (Wikidata Q11945244))" + }, + { + "q": "Tell me the input power with output power of 1.91195 and power efficiency of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "W", + "why": "input power for [1.911952, 82.240072] (the system power efficiency (Wikidata Q11945244))" + }, + { + "q": "What is the input power for output power 1.64379 and power efficiency 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "W", + "why": "input power for [1.643795, 252.709024] (the system power efficiency (Wikidata Q11945244))" + }, + { + "q": "Tell me the input power with output power of 1.64379 and power efficiency of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "W", + "why": "input power for [1.643795, 252.709024] (the system power efficiency (Wikidata Q11945244))" + }, + { + "q": "What is the input power for output power 18.3169 and power efficiency 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "W", + "why": "input power for [18.316867, 6.838598] (the system power efficiency (Wikidata Q11945244))" + }, + { + "q": "Tell me the input power with output power of 18.3169 and power efficiency of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "W", + "why": "input power for [18.316867, 6.838598] (the system power efficiency (Wikidata Q11945244))" + }, + { + "q": "What is the input power for output power 156.502 and power efficiency 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "W", + "why": "input power for [156.50181, 185.886273] (the system power efficiency (Wikidata Q11945244))" + }, + { + "q": "Tell me the input power with output power of 156.502 and power efficiency of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "W", + "why": "input power for [156.50181, 185.886273] (the system power efficiency (Wikidata Q11945244))" + }, + { + "q": "What is the input power for output power 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the power efficiency is not given" + }, + { + "q": "Tell me the input power with output power of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the power efficiency is not given" + }, + { + "q": "What is the input 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 input 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 input power with output power of -1.91195 and power efficiency of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative output power: no example had one" + }, + { + "q": "Which input 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 output power is 8.50728 and the power efficiency is 17.7888, what is the input power?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "W", + "why": "input power for [8.507283, 17.788778] (the system power efficiency (Wikidata Q11945244))" + }, + { + "q": "Could you work out the input power given output power = 8.50728, power efficiency = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "W", + "why": "input power for [8.507283, 17.788778] (the system power efficiency (Wikidata Q11945244))" + }, + { + "q": "I need the input power for an output power of 8.24951 and a power efficiency of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "W", + "why": "input power for [8.24951, 0.634021] (the system power efficiency (Wikidata Q11945244))" + }, + { + "q": "If the output power is 8.24951 and the power efficiency is 0.634021, what is the input power?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "W", + "why": "input power for [8.24951, 0.634021] (the system power efficiency (Wikidata Q11945244))" + }, + { + "q": "Could you work out the input power given output power = 0.318734, power efficiency = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "W", + "why": "input power for [0.318734, 9.566071] (the system power efficiency (Wikidata Q11945244))" + }, + { + "q": "I need the input power for an output power of 0.318734 and a power efficiency of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "W", + "why": "input power for [0.318734, 9.566071] (the system power efficiency (Wikidata Q11945244))" + }, + { + "q": "If the output power is 0.671697 and the power efficiency is 14.2496, what is the input power?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "W", + "why": "input power for [0.671697, 14.249569] (the system power efficiency (Wikidata Q11945244))" + }, + { + "q": "Could you work out the input power given output power = 0.671697, power efficiency = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "W", + "why": "input power for [0.671697, 14.249569] (the system power efficiency (Wikidata Q11945244))" + }, + { + "q": "I need the input power for an output power of 21.7931 and a power efficiency of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "W", + "why": "input power for [21.793126, 29.510609] (the system power efficiency (Wikidata Q11945244))" + }, + { + "q": "If the output power is 21.7931 and the power efficiency is 29.5106, what is the input power?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "W", + "why": "input power for [21.793126, 29.510609] (the system power efficiency (Wikidata Q11945244))" + }, + { + "q": "Could you work out the input power given output power = 153.15, power efficiency = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "W", + "why": "input power for [153.149965, 18.595315] (the system power efficiency (Wikidata Q11945244))" + }, + { + "q": "I need the input power for an output power of 153.15 and a power efficiency of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "W", + "why": "input power for [153.149965, 18.595315] (the system power efficiency (Wikidata Q11945244))" + }, + { + "q": "If the output power is 8.50728, what is the input power?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the power efficiency is not given" + }, + { + "q": "Could you work out the input power given output power = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the power efficiency is not given" + }, + { + "q": "Do you know the input 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 input 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 input power given output power = -0.318734, power efficiency = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative output power: no example had one" + }, + { + "q": "What is a typical input 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-input-power-2/program.py b/root/organs10/physics/specialists/solved-input-power-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..a1a98a1acc4296dfb7de5f55467e7c38e6bf51b5 --- /dev/null +++ b/root/organs10/physics/specialists/solved-input-power-2/program.py @@ -0,0 +1,7 @@ +"""input power from output power, power efficiency (power efficiency (Wikidata Q11945244)) +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(output_power, power_efficiency): + return 1.0*output_power/power_efficiency diff --git a/root/organs10/physics/specialists/solved-input-power-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-input-power-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-input-power-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-input-power/organ_formula_v2.py b/root/organs10/physics/specialists/solved-input-power/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-input-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-input-power/panel.json b/root/organs10/physics/specialists/solved-input-power/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..c053c5d94d77ee63d6afe4d8f03b5fff3f26c38c --- /dev/null +++ b/root/organs10/physics/specialists/solved-input-power/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the input power for output power 107.961 and efficiency 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "W", + "why": "input power for [107.960666, 59.4105] (the system Efficiency)" + }, + { + "q": "Tell me the input power with output power of 107.961 and efficiency of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "W", + "why": "input power for [107.960666, 59.4105] (the system Efficiency)" + }, + { + "q": "What is the input power for output power 169.527 and efficiency 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "W", + "why": "input power for [169.526958, 280.773702] (the system Efficiency)" + }, + { + "q": "Tell me the input power with output power of 169.527 and efficiency of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "W", + "why": "input power for [169.526958, 280.773702] (the system Efficiency)" + }, + { + "q": "What is the input power for output power 1.91195 and efficiency 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "W", + "why": "input power for [1.911952, 82.240072] (the system Efficiency)" + }, + { + "q": "Tell me the input power with output power of 1.91195 and efficiency of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "W", + "why": "input power for [1.911952, 82.240072] (the system Efficiency)" + }, + { + "q": "What is the input power for output power 1.64379 and efficiency 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "W", + "why": "input power for [1.643795, 252.709024] (the system Efficiency)" + }, + { + "q": "Tell me the input power with output power of 1.64379 and efficiency of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "W", + "why": "input power for [1.643795, 252.709024] (the system Efficiency)" + }, + { + "q": "What is the input power for output power 18.3169 and efficiency 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "W", + "why": "input power for [18.316867, 6.838598] (the system Efficiency)" + }, + { + "q": "Tell me the input power with output power of 18.3169 and efficiency of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "W", + "why": "input power for [18.316867, 6.838598] (the system Efficiency)" + }, + { + "q": "What is the input power for output power 156.502 and efficiency 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "W", + "why": "input power for [156.50181, 185.886273] (the system Efficiency)" + }, + { + "q": "Tell me the input power with output power of 156.502 and efficiency of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "W", + "why": "input power for [156.50181, 185.886273] (the system Efficiency)" + }, + { + "q": "What is the input power for output power 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the efficiency is not given" + }, + { + "q": "Tell me the input power with output power of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the efficiency is not given" + }, + { + "q": "What is the input 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 input 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 input power with output power of -1.91195 and efficiency of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative output power: no example had one" + }, + { + "q": "Which input 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 output power is 8.50728 and the efficiency is 17.7888, what is the input power?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "W", + "why": "input power for [8.507283, 17.788778] (the system Efficiency)" + }, + { + "q": "Could you work out the input power given output power = 8.50728, efficiency = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "W", + "why": "input power for [8.507283, 17.788778] (the system Efficiency)" + }, + { + "q": "I need the input power for an output power of 8.24951 and an efficiency of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "W", + "why": "input power for [8.24951, 0.634021] (the system Efficiency)" + }, + { + "q": "If the output power is 8.24951 and the efficiency is 0.634021, what is the input power?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "W", + "why": "input power for [8.24951, 0.634021] (the system Efficiency)" + }, + { + "q": "Could you work out the input power given output power = 0.318734, efficiency = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "W", + "why": "input power for [0.318734, 9.566071] (the system Efficiency)" + }, + { + "q": "I need the input power for an output power of 0.318734 and an efficiency of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "W", + "why": "input power for [0.318734, 9.566071] (the system Efficiency)" + }, + { + "q": "If the output power is 0.671697 and the efficiency is 14.2496, what is the input power?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "W", + "why": "input power for [0.671697, 14.249569] (the system Efficiency)" + }, + { + "q": "Could you work out the input power given output power = 0.671697, efficiency = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "W", + "why": "input power for [0.671697, 14.249569] (the system Efficiency)" + }, + { + "q": "I need the input power for an output power of 21.7931 and an efficiency of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "W", + "why": "input power for [21.793126, 29.510609] (the system Efficiency)" + }, + { + "q": "If the output power is 21.7931 and the efficiency is 29.5106, what is the input power?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "W", + "why": "input power for [21.793126, 29.510609] (the system Efficiency)" + }, + { + "q": "Could you work out the input power given output power = 153.15, efficiency = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "W", + "why": "input power for [153.149965, 18.595315] (the system Efficiency)" + }, + { + "q": "I need the input power for an output power of 153.15 and an efficiency of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "W", + "why": "input power for [153.149965, 18.595315] (the system Efficiency)" + }, + { + "q": "If the output power is 8.50728, what is the input power?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the efficiency is not given" + }, + { + "q": "Could you work out the input power given output power = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the efficiency is not given" + }, + { + "q": "Do you know the input 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 input 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 input power given output power = -0.318734, efficiency = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative output power: no example had one" + }, + { + "q": "What is a typical input 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-input-power/program.py b/root/organs10/physics/specialists/solved-input-power/program.py new file mode 100644 index 0000000000000000000000000000000000000000..47123ec7af80daa38b3eaaecc045a07228058b8e --- /dev/null +++ b/root/organs10/physics/specialists/solved-input-power/program.py @@ -0,0 +1,7 @@ +"""input power from output power, efficiency (Efficiency) +The maths organ (systems.py): solved from the system O = e*I; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(output_power, efficiency): + return 1.0*output_power/efficiency diff --git a/root/organs10/physics/specialists/solved-input-power/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-input-power/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-input-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-instantaneous-electric-curren/organ_formula_v2.py b/root/organs10/physics/specialists/solved-instantaneous-electric-curren/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-instantaneous-electric-curren/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-instantaneous-electric-curren/panel.json b/root/organs10/physics/specialists/solved-instantaneous-electric-curren/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..52e4c3e8bffb31c46b360f0c6a5f6b383036d593 --- /dev/null +++ b/root/organs10/physics/specialists/solved-instantaneous-electric-curren/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the instantaneous electric current for instantaneous electric power 107.961 and instantaneous voltage 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "A", + "why": "instantaneous electric current for [107.960666, 59.4105] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Tell me the instantaneous electric current with instantaneous electric power of 107.961 and instantaneous voltage of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "A", + "why": "instantaneous electric current for [107.960666, 59.4105] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "What is the instantaneous electric current for instantaneous electric power 169.527 and instantaneous voltage 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "A", + "why": "instantaneous electric current for [169.526958, 280.773702] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Tell me the instantaneous electric current with instantaneous electric power of 169.527 and instantaneous voltage of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "A", + "why": "instantaneous electric current for [169.526958, 280.773702] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "What is the instantaneous electric current for instantaneous electric power 1.91195 and instantaneous voltage 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "A", + "why": "instantaneous electric current for [1.911952, 82.240072] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Tell me the instantaneous electric current with instantaneous electric power of 1.91195 and instantaneous voltage of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "A", + "why": "instantaneous electric current for [1.911952, 82.240072] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "What is the instantaneous electric current for instantaneous electric power 1.64379 and instantaneous voltage 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "A", + "why": "instantaneous electric current for [1.643795, 252.709024] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Tell me the instantaneous electric current with instantaneous electric power of 1.64379 and instantaneous voltage of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "A", + "why": "instantaneous electric current for [1.643795, 252.709024] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "What is the instantaneous electric current for instantaneous electric power 18.3169 and instantaneous voltage 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "A", + "why": "instantaneous electric current for [18.316867, 6.838598] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Tell me the instantaneous electric current with instantaneous electric power of 18.3169 and instantaneous voltage of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "A", + "why": "instantaneous electric current for [18.316867, 6.838598] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "What is the instantaneous electric current for instantaneous electric power 156.502 and instantaneous voltage 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "A", + "why": "instantaneous electric current for [156.50181, 185.886273] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Tell me the instantaneous electric current with instantaneous electric power of 156.502 and instantaneous voltage of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "A", + "why": "instantaneous electric current for [156.50181, 185.886273] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "What is the instantaneous electric current for instantaneous electric power 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the instantaneous voltage is not given" + }, + { + "q": "Tell me the instantaneous electric current with instantaneous electric power of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the instantaneous voltage is not given" + }, + { + "q": "What is the instantaneous electric 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 instantaneous electric 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 instantaneous electric current with instantaneous electric power of -1.91195 and instantaneous voltage of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative instantaneous electric power: no example had one" + }, + { + "q": "Which instantaneous electric 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 instantaneous electric power is 8.50728 and the instantaneous voltage is 17.7888, what is the instantaneous electric current?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "A", + "why": "instantaneous electric current for [8.507283, 17.788778] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Could you work out the instantaneous electric current given instantaneous electric power = 8.50728, instantaneous voltage = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "A", + "why": "instantaneous electric current for [8.507283, 17.788778] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "I need the instantaneous electric current for an instantaneous electric power of 8.24951 and an instantaneous voltage of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "A", + "why": "instantaneous electric current for [8.24951, 0.634021] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "If the instantaneous electric power is 8.24951 and the instantaneous voltage is 0.634021, what is the instantaneous electric current?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "A", + "why": "instantaneous electric current for [8.24951, 0.634021] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Could you work out the instantaneous electric current given instantaneous electric power = 0.318734, instantaneous voltage = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "A", + "why": "instantaneous electric current for [0.318734, 9.566071] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "I need the instantaneous electric current for an instantaneous electric power of 0.318734 and an instantaneous voltage of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "A", + "why": "instantaneous electric current for [0.318734, 9.566071] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "If the instantaneous electric power is 0.671697 and the instantaneous voltage is 14.2496, what is the instantaneous electric current?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "A", + "why": "instantaneous electric current for [0.671697, 14.249569] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Could you work out the instantaneous electric current given instantaneous electric power = 0.671697, instantaneous voltage = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "A", + "why": "instantaneous electric current for [0.671697, 14.249569] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "I need the instantaneous electric current for an instantaneous electric power of 21.7931 and an instantaneous voltage of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "A", + "why": "instantaneous electric current for [21.793126, 29.510609] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "If the instantaneous electric power is 21.7931 and the instantaneous voltage is 29.5106, what is the instantaneous electric current?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "A", + "why": "instantaneous electric current for [21.793126, 29.510609] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Could you work out the instantaneous electric current given instantaneous electric power = 153.15, instantaneous voltage = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "A", + "why": "instantaneous electric current for [153.149965, 18.595315] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "I need the instantaneous electric current for an instantaneous electric power of 153.15 and an instantaneous voltage of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "A", + "why": "instantaneous electric current for [153.149965, 18.595315] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "If the instantaneous electric power is 8.50728, what is the instantaneous electric current?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the instantaneous voltage is not given" + }, + { + "q": "Could you work out the instantaneous electric current given instantaneous electric power = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the instantaneous voltage is not given" + }, + { + "q": "Do you know the instantaneous electric 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 instantaneous electric 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 instantaneous electric current given instantaneous electric power = -0.318734, instantaneous voltage = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative instantaneous electric power: no example had one" + }, + { + "q": "What is a typical instantaneous electric 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-instantaneous-electric-curren/program.py b/root/organs10/physics/specialists/solved-instantaneous-electric-curren/program.py new file mode 100644 index 0000000000000000000000000000000000000000..6d5031d3ee9c88bd332fcf512460915512f1a59f --- /dev/null +++ b/root/organs10/physics/specialists/solved-instantaneous-electric-curren/program.py @@ -0,0 +1,7 @@ +"""instantaneous electric current from instantaneous electric power, instantaneous voltage (instantaneous electric power (Wikidata Q11784325)) +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(instantaneous_electric_power, instantaneous_voltage): + return 1.0*instantaneous_electric_power/instantaneous_voltage diff --git a/root/organs10/physics/specialists/solved-instantaneous-electric-curren/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-instantaneous-electric-curren/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-instantaneous-electric-curren/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-instantaneous-electric-power/organ_formula_v2.py b/root/organs10/physics/specialists/solved-instantaneous-electric-power/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-instantaneous-electric-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-instantaneous-electric-power/panel.json b/root/organs10/physics/specialists/solved-instantaneous-electric-power/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..36a4d6c1729997a15be0133ad0001641d1e57403 --- /dev/null +++ b/root/organs10/physics/specialists/solved-instantaneous-electric-power/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the instantaneous electric power for instantaneous voltage 107.961 and instantaneous electric current 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "W", + "why": "instantaneous electric power for [107.960666, 59.4105] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Tell me the instantaneous electric power with instantaneous voltage of 107.961 and instantaneous electric current of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "W", + "why": "instantaneous electric power for [107.960666, 59.4105] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "What is the instantaneous electric power for instantaneous voltage 169.527 and instantaneous electric current 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "W", + "why": "instantaneous electric power for [169.526958, 280.773702] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Tell me the instantaneous electric power with instantaneous voltage of 169.527 and instantaneous electric current of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "W", + "why": "instantaneous electric power for [169.526958, 280.773702] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "What is the instantaneous electric power for instantaneous voltage 1.91195 and instantaneous electric current 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "W", + "why": "instantaneous electric power for [1.911952, 82.240072] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Tell me the instantaneous electric power with instantaneous voltage of 1.91195 and instantaneous electric current of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "W", + "why": "instantaneous electric power for [1.911952, 82.240072] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "What is the instantaneous electric power for instantaneous voltage 1.64379 and instantaneous electric current 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "W", + "why": "instantaneous electric power for [1.643795, 252.709024] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Tell me the instantaneous electric power with instantaneous voltage of 1.64379 and instantaneous electric current of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "W", + "why": "instantaneous electric power for [1.643795, 252.709024] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "What is the instantaneous electric power for instantaneous voltage 18.3169 and instantaneous electric current 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "W", + "why": "instantaneous electric power for [18.316867, 6.838598] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Tell me the instantaneous electric power with instantaneous voltage of 18.3169 and instantaneous electric current of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "W", + "why": "instantaneous electric power for [18.316867, 6.838598] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "What is the instantaneous electric power for instantaneous voltage 156.502 and instantaneous electric current 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "W", + "why": "instantaneous electric power for [156.50181, 185.886273] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Tell me the instantaneous electric power with instantaneous voltage of 156.502 and instantaneous electric current of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "W", + "why": "instantaneous electric power for [156.50181, 185.886273] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "What is the instantaneous electric power for instantaneous voltage 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the instantaneous electric current is not given" + }, + { + "q": "Tell me the instantaneous electric power with instantaneous voltage of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the instantaneous electric current is not given" + }, + { + "q": "What is the instantaneous electric 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 instantaneous electric 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 instantaneous electric power with instantaneous voltage of -1.91195 and instantaneous electric current of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative instantaneous voltage: no example had one" + }, + { + "q": "Which instantaneous electric 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 instantaneous voltage is 8.50728 and the instantaneous electric current is 17.7888, what is the instantaneous electric power?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "W", + "why": "instantaneous electric power for [8.507283, 17.788778] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Could you work out the instantaneous electric power given instantaneous voltage = 8.50728, instantaneous electric current = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "W", + "why": "instantaneous electric power for [8.507283, 17.788778] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "I need the instantaneous electric power for an instantaneous voltage of 8.24951 and an instantaneous electric current of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "W", + "why": "instantaneous electric power for [8.24951, 0.634021] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "If the instantaneous voltage is 8.24951 and the instantaneous electric current is 0.634021, what is the instantaneous electric power?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "W", + "why": "instantaneous electric power for [8.24951, 0.634021] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Could you work out the instantaneous electric power given instantaneous voltage = 0.318734, instantaneous electric current = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "W", + "why": "instantaneous electric power for [0.318734, 9.566071] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "I need the instantaneous electric power for an instantaneous voltage of 0.318734 and an instantaneous electric current of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "W", + "why": "instantaneous electric power for [0.318734, 9.566071] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "If the instantaneous voltage is 0.671697 and the instantaneous electric current is 14.2496, what is the instantaneous electric power?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "W", + "why": "instantaneous electric power for [0.671697, 14.249569] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Could you work out the instantaneous electric power given instantaneous voltage = 0.671697, instantaneous electric current = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "W", + "why": "instantaneous electric power for [0.671697, 14.249569] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "I need the instantaneous electric power for an instantaneous voltage of 21.7931 and an instantaneous electric current of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "W", + "why": "instantaneous electric power for [21.793126, 29.510609] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "If the instantaneous voltage is 21.7931 and the instantaneous electric current is 29.5106, what is the instantaneous electric power?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "W", + "why": "instantaneous electric power for [21.793126, 29.510609] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Could you work out the instantaneous electric power given instantaneous voltage = 153.15, instantaneous electric current = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "W", + "why": "instantaneous electric power for [153.149965, 18.595315] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "I need the instantaneous electric power for an instantaneous voltage of 153.15 and an instantaneous electric current of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "W", + "why": "instantaneous electric power for [153.149965, 18.595315] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "If the instantaneous voltage is 8.50728, what is the instantaneous electric power?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the instantaneous electric current is not given" + }, + { + "q": "Could you work out the instantaneous electric power given instantaneous voltage = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the instantaneous electric current is not given" + }, + { + "q": "Do you know the instantaneous electric 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 instantaneous electric 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 instantaneous electric power given instantaneous voltage = -0.318734, instantaneous electric current = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative instantaneous voltage: no example had one" + }, + { + "q": "What is a typical instantaneous electric 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-instantaneous-electric-power/program.py b/root/organs10/physics/specialists/solved-instantaneous-electric-power/program.py new file mode 100644 index 0000000000000000000000000000000000000000..a68a0a0f3cc07baa754e14510bfc4175820eecf2 --- /dev/null +++ b/root/organs10/physics/specialists/solved-instantaneous-electric-power/program.py @@ -0,0 +1,7 @@ +"""instantaneous electric power from instantaneous voltage, instantaneous electric current (instantaneous electric power (Wikidata Q11784325)) +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(instantaneous_voltage, instantaneous_electric_current): + return 1.0*instantaneous_electric_current*instantaneous_voltage diff --git a/root/organs10/physics/specialists/solved-instantaneous-electric-power/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-instantaneous-electric-power/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-instantaneous-electric-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-instantaneous-voltage/organ_formula_v2.py b/root/organs10/physics/specialists/solved-instantaneous-voltage/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-instantaneous-voltage/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-instantaneous-voltage/panel.json b/root/organs10/physics/specialists/solved-instantaneous-voltage/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..1ec0559afe12b8a1b41c196a4900604e505cc1b9 --- /dev/null +++ b/root/organs10/physics/specialists/solved-instantaneous-voltage/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the instantaneous voltage for instantaneous electric power 107.961 and instantaneous electric current 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "V", + "why": "instantaneous voltage for [107.960666, 59.4105] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Tell me the instantaneous voltage with instantaneous electric power of 107.961 and instantaneous electric current of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "V", + "why": "instantaneous voltage for [107.960666, 59.4105] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "What is the instantaneous voltage for instantaneous electric power 169.527 and instantaneous electric current 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "V", + "why": "instantaneous voltage for [169.526958, 280.773702] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Tell me the instantaneous voltage with instantaneous electric power of 169.527 and instantaneous electric current of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "V", + "why": "instantaneous voltage for [169.526958, 280.773702] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "What is the instantaneous voltage for instantaneous electric power 1.91195 and instantaneous electric current 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "V", + "why": "instantaneous voltage for [1.911952, 82.240072] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Tell me the instantaneous voltage with instantaneous electric power of 1.91195 and instantaneous electric current of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "V", + "why": "instantaneous voltage for [1.911952, 82.240072] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "What is the instantaneous voltage for instantaneous electric power 1.64379 and instantaneous electric current 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "V", + "why": "instantaneous voltage for [1.643795, 252.709024] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Tell me the instantaneous voltage with instantaneous electric power of 1.64379 and instantaneous electric current of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "V", + "why": "instantaneous voltage for [1.643795, 252.709024] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "What is the instantaneous voltage for instantaneous electric power 18.3169 and instantaneous electric current 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "V", + "why": "instantaneous voltage for [18.316867, 6.838598] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Tell me the instantaneous voltage with instantaneous electric power of 18.3169 and instantaneous electric current of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "V", + "why": "instantaneous voltage for [18.316867, 6.838598] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "What is the instantaneous voltage for instantaneous electric power 156.502 and instantaneous electric current 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "V", + "why": "instantaneous voltage for [156.50181, 185.886273] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Tell me the instantaneous voltage with instantaneous electric power of 156.502 and instantaneous electric current of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "V", + "why": "instantaneous voltage for [156.50181, 185.886273] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "What is the instantaneous voltage for instantaneous electric power 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the instantaneous electric current is not given" + }, + { + "q": "Tell me the instantaneous voltage with instantaneous electric power of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the instantaneous electric current is not given" + }, + { + "q": "What is the instantaneous voltage 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 instantaneous voltage 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 instantaneous voltage with instantaneous electric power of -1.91195 and instantaneous electric current of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative instantaneous electric power: no example had one" + }, + { + "q": "Which instantaneous voltage 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 instantaneous electric power is 8.50728 and the instantaneous electric current is 17.7888, what is the instantaneous voltage?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "V", + "why": "instantaneous voltage for [8.507283, 17.788778] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Could you work out the instantaneous voltage given instantaneous electric power = 8.50728, instantaneous electric current = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "V", + "why": "instantaneous voltage for [8.507283, 17.788778] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "I need the instantaneous voltage for an instantaneous electric power of 8.24951 and an instantaneous electric current of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "V", + "why": "instantaneous voltage for [8.24951, 0.634021] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "If the instantaneous electric power is 8.24951 and the instantaneous electric current is 0.634021, what is the instantaneous voltage?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "V", + "why": "instantaneous voltage for [8.24951, 0.634021] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Could you work out the instantaneous voltage given instantaneous electric power = 0.318734, instantaneous electric current = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "V", + "why": "instantaneous voltage for [0.318734, 9.566071] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "I need the instantaneous voltage for an instantaneous electric power of 0.318734 and an instantaneous electric current of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "V", + "why": "instantaneous voltage for [0.318734, 9.566071] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "If the instantaneous electric power is 0.671697 and the instantaneous electric current is 14.2496, what is the instantaneous voltage?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "V", + "why": "instantaneous voltage for [0.671697, 14.249569] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Could you work out the instantaneous voltage given instantaneous electric power = 0.671697, instantaneous electric current = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "V", + "why": "instantaneous voltage for [0.671697, 14.249569] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "I need the instantaneous voltage for an instantaneous electric power of 21.7931 and an instantaneous electric current of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "V", + "why": "instantaneous voltage for [21.793126, 29.510609] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "If the instantaneous electric power is 21.7931 and the instantaneous electric current is 29.5106, what is the instantaneous voltage?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "V", + "why": "instantaneous voltage for [21.793126, 29.510609] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "Could you work out the instantaneous voltage given instantaneous electric power = 153.15, instantaneous electric current = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "V", + "why": "instantaneous voltage for [153.149965, 18.595315] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "I need the instantaneous voltage for an instantaneous electric power of 153.15 and an instantaneous electric current of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "V", + "why": "instantaneous voltage for [153.149965, 18.595315] (the system instantaneous electric power (Wikidata Q11784325))" + }, + { + "q": "If the instantaneous electric power is 8.50728, what is the instantaneous voltage?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the instantaneous electric current is not given" + }, + { + "q": "Could you work out the instantaneous voltage given instantaneous electric power = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the instantaneous electric current is not given" + }, + { + "q": "Do you know the instantaneous voltage 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 instantaneous voltage 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 instantaneous voltage given instantaneous electric power = -0.318734, instantaneous electric current = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative instantaneous electric power: no example had one" + }, + { + "q": "What is a typical instantaneous voltage?", + "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-instantaneous-voltage/program.py b/root/organs10/physics/specialists/solved-instantaneous-voltage/program.py new file mode 100644 index 0000000000000000000000000000000000000000..5ec98b87afbabc163ab16de39ee9d764a0c1f2ba --- /dev/null +++ b/root/organs10/physics/specialists/solved-instantaneous-voltage/program.py @@ -0,0 +1,7 @@ +"""instantaneous voltage from instantaneous electric power, instantaneous electric current (instantaneous electric power (Wikidata Q11784325)) +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(instantaneous_electric_power, instantaneous_electric_current): + return 1.0*instantaneous_electric_power/instantaneous_electric_current diff --git a/root/organs10/physics/specialists/solved-instantaneous-voltage/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-instantaneous-voltage/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-instantaneous-voltage/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-interest/organ_formula_v2.py b/root/organs10/physics/specialists/solved-interest/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-interest/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-interest/panel.json b/root/organs10/physics/specialists/solved-interest/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..3d0142cbe0aeeefe45a8dd3b2837cdc4ac219db2 --- /dev/null +++ b/root/organs10/physics/specialists/solved-interest/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the interest for principal 107.961, annual rate 59.4105 and time 5.77717?", + "a": "37054.71743803603", + "valid": true, + "kind": "compute", + "expect": 37054.71743803603, + "dtype": "quantity", + "unit": "$", + "why": "interest for [107.960666, 59.4105, 5.777165] (the system Simple interest)" + }, + { + "q": "Tell me the interest with principal of 107.961, annual rate of 59.4105 and time of 5.77717.", + "a": "37054.71743803603", + "valid": true, + "kind": "compute", + "expect": 37054.71743803603, + "dtype": "quantity", + "unit": "$", + "why": "interest for [107.960666, 59.4105, 5.777165] (the system Simple interest)" + }, + { + "q": "What is the interest for principal 85.2416, annual rate 160.878 and time 2.69428?", + "a": "36948.1133481491", + "valid": true, + "kind": "compute", + "expect": 36948.1133481491, + "dtype": "quantity", + "unit": "$", + "why": "interest for [85.241623, 160.878442, 2.69428] (the system Simple interest)" + }, + { + "q": "Tell me the interest with principal of 85.2416, annual rate of 160.878 and time of 2.69428.", + "a": "36948.1133481491", + "valid": true, + "kind": "compute", + "expect": 36948.1133481491, + "dtype": "quantity", + "unit": "$", + "why": "interest for [85.241623, 160.878442, 2.69428] (the system Simple interest)" + }, + { + "q": "What is the interest for principal 45.6157, annual rate 4.97131 and time 94.305?", + "a": "21385.510275340814", + "valid": true, + "kind": "compute", + "expect": 21385.510275340814, + "dtype": "quantity", + "unit": "$", + "why": "interest for [45.61567, 4.971307, 94.305046] (the system Simple interest)" + }, + { + "q": "Tell me the interest with principal of 45.6157, annual rate of 4.97131 and time of 94.305.", + "a": "21385.510275340814", + "valid": true, + "kind": "compute", + "expect": 21385.510275340814, + "dtype": "quantity", + "unit": "$", + "why": "interest for [45.61567, 4.971307, 94.305046] (the system Simple interest)" + }, + { + "q": "What is the interest for principal 10.5638, annual rate 198.839 and time 0.671697?", + "a": "1410.9022889506462", + "valid": true, + "kind": "compute", + "expect": 1410.9022889506462, + "dtype": "quantity", + "unit": "$", + "why": "interest for [10.563843, 198.838955, 0.671697] (the system Simple interest)" + }, + { + "q": "Tell me the interest with principal of 10.5638, annual rate of 198.839 and time of 0.671697.", + "a": "1410.9022889506462", + "valid": true, + "kind": "compute", + "expect": 1410.9022889506462, + "dtype": "quantity", + "unit": "$", + "why": "interest for [10.563843, 198.838955, 0.671697] (the system Simple interest)" + }, + { + "q": "What is the interest for principal 21.7931, annual rate 29.5106 and time 8.50341?", + "a": "5468.782894065434", + "valid": true, + "kind": "compute", + "expect": 5468.782894065434, + "dtype": "quantity", + "unit": "$", + "why": "interest for [21.793126, 29.510609, 8.503407] (the system Simple interest)" + }, + { + "q": "Tell me the interest with principal of 21.7931, annual rate of 29.5106 and time of 8.50341.", + "a": "5468.782894065434", + "valid": true, + "kind": "compute", + "expect": 5468.782894065434, + "dtype": "quantity", + "unit": "$", + "why": "interest for [21.793126, 29.510609, 8.503407] (the system Simple interest)" + }, + { + "q": "What is the interest for principal 223.509, annual rate 17.3414 and time 7.10705?", + "a": "27546.661467730268", + "valid": true, + "kind": "compute", + "expect": 27546.661467730268, + "dtype": "quantity", + "unit": "$", + "why": "interest for [223.508796, 17.341435, 7.107049] (the system Simple interest)" + }, + { + "q": "Tell me the interest with principal of 223.509, annual rate of 17.3414 and time of 7.10705.", + "a": "27546.661467730268", + "valid": true, + "kind": "compute", + "expect": 27546.661467730268, + "dtype": "quantity", + "unit": "$", + "why": "interest for [223.508796, 17.341435, 7.107049] (the system Simple interest)" + }, + { + "q": "What is the interest for principal 107.961 and annual rate 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "Tell me the interest with principal of 85.2416 and annual rate of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "What is the interest 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 interest 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 interest with principal of -45.6157, annual rate of 4.97131 and time of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative principal: no example had one" + }, + { + "q": "Which interest 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 principal is 17.7888, annual rate is 167.625 and the time is 10.3291, what is the interest?", + "a": "30799.61880250525", + "valid": true, + "kind": "compute", + "expect": 30799.61880250525, + "dtype": "quantity", + "unit": "$", + "why": "interest for [17.788778, 167.624948, 10.329054] (the system Simple interest)" + }, + { + "q": "Could you work out the interest given principal = 17.7888, annual rate = 167.625, time = 10.3291?", + "a": "30799.61880250525", + "valid": true, + "kind": "compute", + "expect": 30799.61880250525, + "dtype": "quantity", + "unit": "$", + "why": "interest for [17.788778, 167.624948, 10.329054] (the system Simple interest)" + }, + { + "q": "I need the interest for a principal of 21.511, an annual rate of 173.394 and a time of 251.084, please.", + "a": "936511.6480251561", + "valid": true, + "kind": "compute", + "expect": 936511.6480251561, + "dtype": "quantity", + "unit": "$", + "why": "interest for [21.511016, 173.393638, 251.084037] (the system Simple interest)" + }, + { + "q": "If the principal is 21.511, annual rate is 173.394 and the time is 251.084, what is the interest?", + "a": "936511.6480251561", + "valid": true, + "kind": "compute", + "expect": 936511.6480251561, + "dtype": "quantity", + "unit": "$", + "why": "interest for [21.511016, 173.393638, 251.084037] (the system Simple interest)" + }, + { + "q": "Could you work out the interest given principal = 129.245, annual rate = 1.18359, time = 15.9416?", + "a": "2438.637190275597", + "valid": true, + "kind": "compute", + "expect": 2438.637190275597, + "dtype": "quantity", + "unit": "$", + "why": "interest for [129.244984, 1.18359, 15.941612] (the system Simple interest)" + }, + { + "q": "I need the interest for a principal of 129.245, an annual rate of 1.18359 and a time of 15.9416, please.", + "a": "2438.637190275597", + "valid": true, + "kind": "compute", + "expect": 2438.637190275597, + "dtype": "quantity", + "unit": "$", + "why": "interest for [129.244984, 1.18359, 15.941612] (the system Simple interest)" + }, + { + "q": "If the principal is 20.3966, annual rate is 18.3169 and the time is 6.8386, what is the interest?", + "a": "2554.9073472856903", + "valid": true, + "kind": "compute", + "expect": 2554.9073472856903, + "dtype": "quantity", + "unit": "$", + "why": "interest for [20.396558, 18.316867, 6.838598] (the system Simple interest)" + }, + { + "q": "Could you work out the interest given principal = 20.3966, annual rate = 18.3169, time = 6.8386?", + "a": "2554.9073472856903", + "valid": true, + "kind": "compute", + "expect": 2554.9073472856903, + "dtype": "quantity", + "unit": "$", + "why": "interest for [20.396558, 18.316867, 6.838598] (the system Simple interest)" + }, + { + "q": "I need the interest for a principal of 185.886, an annual rate of 13.2373 and a time of 4.71941, please.", + "a": "11612.754502579606", + "valid": true, + "kind": "compute", + "expect": 11612.754502579606, + "dtype": "quantity", + "unit": "$", + "why": "interest for [185.886273, 13.237311, 4.719415] (the system Simple interest)" + }, + { + "q": "If the principal is 185.886, annual rate is 13.2373 and the time is 4.71941, what is the interest?", + "a": "11612.754502579606", + "valid": true, + "kind": "compute", + "expect": 11612.754502579606, + "dtype": "quantity", + "unit": "$", + "why": "interest for [185.886273, 13.237311, 4.719415] (the system Simple interest)" + }, + { + "q": "Could you work out the interest given principal = 9.1061, annual rate = 24.5721, time = 108.451?", + "a": "24266.511866047753", + "valid": true, + "kind": "compute", + "expect": 24266.511866047753, + "dtype": "quantity", + "unit": "$", + "why": "interest for [9.106104, 24.572086, 108.450792] (the system Simple interest)" + }, + { + "q": "I need the interest for a principal of 9.1061, an annual rate of 24.5721 and a time of 108.451, please.", + "a": "24266.511866047753", + "valid": true, + "kind": "compute", + "expect": 24266.511866047753, + "dtype": "quantity", + "unit": "$", + "why": "interest for [9.106104, 24.572086, 108.450792] (the system Simple interest)" + }, + { + "q": "If the principal is 17.7888 and the annual rate is 167.625, what is the interest?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "Could you work out the interest given principal = 21.511, annual rate = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "Do you know the interest 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 interest 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 interest given principal = -129.245, annual rate = 1.18359, time = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative principal: no example had one" + }, + { + "q": "What is a typical interest?", + "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-interest/program.py b/root/organs10/physics/specialists/solved-interest/program.py new file mode 100644 index 0000000000000000000000000000000000000000..2d545fae5fe532a9c682d92d7fb134888e95ae31 --- /dev/null +++ b/root/organs10/physics/specialists/solved-interest/program.py @@ -0,0 +1,7 @@ +"""interest from principal, annual rate, time (Simple interest) +The maths organ (systems.py): solved from the system I = P*r*t; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(principal, annual_rate, time): + return 1.0*annual_rate*principal*time diff --git a/root/organs10/physics/specialists/solved-interest/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-interest/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-interest/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-internal-energy-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-internal-energy-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-internal-energy-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-internal-energy-2/panel.json b/root/organs10/physics/specialists/solved-internal-energy-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..b56edf94a893dd4608b4df918536da8cb0befaec --- /dev/null +++ b/root/organs10/physics/specialists/solved-internal-energy-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the internal energy for specific internal energy 107.961 and mass 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [107.960666, 59.4105] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "Tell me the internal energy with specific internal energy of 107.961 and mass of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [107.960666, 59.4105] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "What is the internal energy for specific internal energy 169.527 and mass 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [169.526958, 280.773702] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "Tell me the internal energy with specific internal energy of 169.527 and mass of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [169.526958, 280.773702] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "What is the internal energy for specific internal energy 1.91195 and mass 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [1.911952, 82.240072] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "Tell me the internal energy with specific internal energy of 1.91195 and mass of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [1.911952, 82.240072] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "What is the internal energy for specific internal energy 1.64379 and mass 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [1.643795, 252.709024] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "Tell me the internal energy with specific internal energy of 1.64379 and mass of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [1.643795, 252.709024] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "What is the internal energy for specific internal energy 18.3169 and mass 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [18.316867, 6.838598] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "Tell me the internal energy with specific internal energy of 18.3169 and mass of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [18.316867, 6.838598] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "What is the internal energy for specific internal energy 156.502 and mass 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [156.50181, 185.886273] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "Tell me the internal energy with specific internal energy of 156.502 and mass of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [156.50181, 185.886273] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "What is the internal energy for specific internal energy 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Tell me the internal energy with specific internal energy of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "What is the internal energy 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 internal energy 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 internal energy with specific internal energy of -1.91195 and mass of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative specific internal energy: no example had one" + }, + { + "q": "Which internal energy 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 internal energy is 8.50728 and the mass is 17.7888, what is the internal energy?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [8.507283, 17.788778] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "Could you work out the internal energy given specific internal energy = 8.50728, mass = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [8.507283, 17.788778] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "I need the internal energy for a specific internal energy of 8.24951 and a mass of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [8.24951, 0.634021] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "If the specific internal energy is 8.24951 and the mass is 0.634021, what is the internal energy?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [8.24951, 0.634021] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "Could you work out the internal energy given specific internal energy = 0.318734, mass = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [0.318734, 9.566071] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "I need the internal energy for a specific internal energy of 0.318734 and a mass of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [0.318734, 9.566071] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "If the specific internal energy is 0.671697 and the mass is 14.2496, what is the internal energy?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [0.671697, 14.249569] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "Could you work out the internal energy given specific internal energy = 0.671697, mass = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [0.671697, 14.249569] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "I need the internal energy for a specific internal energy of 21.7931 and a mass of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [21.793126, 29.510609] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "If the specific internal energy is 21.7931 and the mass is 29.5106, what is the internal energy?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [21.793126, 29.510609] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "Could you work out the internal energy given specific internal energy = 153.15, mass = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [153.149965, 18.595315] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "I need the internal energy for a specific internal energy of 153.15 and a mass of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [153.149965, 18.595315] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "If the specific internal energy is 8.50728, what is the internal energy?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Could you work out the internal energy given specific internal energy = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Do you know the internal energy 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 internal energy 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 internal energy given specific internal energy = -0.318734, mass = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative specific internal energy: no example had one" + }, + { + "q": "What is a typical internal energy?", + "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-internal-energy-2/program.py b/root/organs10/physics/specialists/solved-internal-energy-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..a6f5180ff47a488980313f24ed38b650d0296785 --- /dev/null +++ b/root/organs10/physics/specialists/solved-internal-energy-2/program.py @@ -0,0 +1,7 @@ +"""internal energy from specific internal energy, mass (specific internal energy (Wikidata Q76357367)) +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(specific_internal_energy, mass): + return 1.0*mass*specific_internal_energy diff --git a/root/organs10/physics/specialists/solved-internal-energy-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-internal-energy-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-internal-energy-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-internal-energy-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-internal-energy-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-internal-energy-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-internal-energy-3/panel.json b/root/organs10/physics/specialists/solved-internal-energy-3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..d4ecba6c3f73de3624afe1a5247e23f95a24a923 --- /dev/null +++ b/root/organs10/physics/specialists/solved-internal-energy-3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the internal energy for molar internal energy 107.961 and amount of substance 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [107.960666, 59.4105] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "Tell me the internal energy with molar internal energy of 107.961 and amount of substance of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [107.960666, 59.4105] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "What is the internal energy for molar internal energy 169.527 and amount of substance 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [169.526958, 280.773702] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "Tell me the internal energy with molar internal energy of 169.527 and amount of substance of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [169.526958, 280.773702] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "What is the internal energy for molar internal energy 1.91195 and amount of substance 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [1.911952, 82.240072] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "Tell me the internal energy with molar internal energy of 1.91195 and amount of substance of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [1.911952, 82.240072] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "What is the internal energy for molar internal energy 1.64379 and amount of substance 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [1.643795, 252.709024] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "Tell me the internal energy with molar internal energy of 1.64379 and amount of substance of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [1.643795, 252.709024] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "What is the internal energy for molar internal energy 18.3169 and amount of substance 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [18.316867, 6.838598] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "Tell me the internal energy with molar internal energy of 18.3169 and amount of substance of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [18.316867, 6.838598] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "What is the internal energy for molar internal energy 156.502 and amount of substance 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [156.50181, 185.886273] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "Tell me the internal energy with molar internal energy of 156.502 and amount of substance of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [156.50181, 185.886273] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "What is the internal energy for molar internal energy 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the amount of substance is not given" + }, + { + "q": "Tell me the internal energy with molar internal energy of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the amount of substance is not given" + }, + { + "q": "What is the internal energy 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 internal energy 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 internal energy with molar internal energy of -1.91195 and amount of substance of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative molar internal energy: no example had one" + }, + { + "q": "Which internal energy 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 internal energy is 8.50728 and the amount of substance is 17.7888, what is the internal energy?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [8.507283, 17.788778] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "Could you work out the internal energy given molar internal energy = 8.50728, amount of substance = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [8.507283, 17.788778] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "I need the internal energy for a molar internal energy of 8.24951 and an amount of substance of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [8.24951, 0.634021] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "If the molar internal energy is 8.24951 and the amount of substance is 0.634021, what is the internal energy?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [8.24951, 0.634021] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "Could you work out the internal energy given molar internal energy = 0.318734, amount of substance = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [0.318734, 9.566071] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "I need the internal energy for a molar internal energy of 0.318734 and an amount of substance of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [0.318734, 9.566071] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "If the molar internal energy is 0.671697 and the amount of substance is 14.2496, what is the internal energy?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [0.671697, 14.249569] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "Could you work out the internal energy given molar internal energy = 0.671697, amount of substance = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [0.671697, 14.249569] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "I need the internal energy for a molar internal energy of 21.7931 and an amount of substance of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [21.793126, 29.510609] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "If the molar internal energy is 21.7931 and the amount of substance is 29.5106, what is the internal energy?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [21.793126, 29.510609] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "Could you work out the internal energy given molar internal energy = 153.15, amount of substance = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [153.149965, 18.595315] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "I need the internal energy for a molar internal energy of 153.15 and an amount of substance of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [153.149965, 18.595315] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "If the molar internal energy is 8.50728, what is the internal energy?", + "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 internal energy given molar internal energy = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the amount of substance is not given" + }, + { + "q": "Do you know the internal energy 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 internal energy 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 internal energy given molar internal energy = -0.318734, amount of substance = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative molar internal energy: no example had one" + }, + { + "q": "What is a typical internal energy?", + "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-internal-energy-3/program.py b/root/organs10/physics/specialists/solved-internal-energy-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..fd3bb715a535bcee0a958a4d6a4a287c50dcd040 --- /dev/null +++ b/root/organs10/physics/specialists/solved-internal-energy-3/program.py @@ -0,0 +1,7 @@ +"""internal energy from molar internal energy, amount of substance (molar internal energy (Wikidata Q88523106)) +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(molar_internal_energy, amount_of_substance): + return 1.0*amount_of_substance*molar_internal_energy diff --git a/root/organs10/physics/specialists/solved-internal-energy-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-internal-energy-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-internal-energy-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-internal-energy-4/organ_formula_v2.py b/root/organs10/physics/specialists/solved-internal-energy-4/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-internal-energy-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-internal-energy-4/panel.json b/root/organs10/physics/specialists/solved-internal-energy-4/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..92669145e6e959e12ef9db03c1ad54d74f726ba3 --- /dev/null +++ b/root/organs10/physics/specialists/solved-internal-energy-4/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the internal energy for Helmholtz free energy 107.961, thermodynamic temperature 59.4105 and entropy 5.77717?", + "a": "451.1849053162336", + "valid": true, + "kind": "compute", + "expect": 451.1849053162336, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [107.960666, 59.4105, 5.777165] (the system Helmholtz free energy (Wikidata Q865821))" + }, + { + "q": "Tell me the internal energy with Helmholtz free energy of 107.961, thermodynamic temperature of 59.4105 and entropy of 5.77717.", + "a": "451.1849053162336", + "valid": true, + "kind": "compute", + "expect": 451.1849053162336, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [107.960666, 59.4105, 5.777165] (the system Helmholtz free energy (Wikidata Q865821))" + }, + { + "q": "What is the internal energy for Helmholtz free energy 85.2416, thermodynamic temperature 160.878 and entropy 2.69428?", + "a": "518.6931715629165", + "valid": true, + "kind": "compute", + "expect": 518.6931715629165, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [85.241623, 160.878442, 2.69428] (the system Helmholtz free energy (Wikidata Q865821))" + }, + { + "q": "Tell me the internal energy with Helmholtz free energy of 85.2416, thermodynamic temperature of 160.878 and entropy of 2.69428.", + "a": "518.6931715629165", + "valid": true, + "kind": "compute", + "expect": 518.6931715629165, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [85.241623, 160.878442, 2.69428] (the system Helmholtz free energy (Wikidata Q865821))" + }, + { + "q": "What is the internal energy for Helmholtz free energy 45.6157, thermodynamic temperature 4.97131 and entropy 94.305?", + "a": "514.4350521883996", + "valid": true, + "kind": "compute", + "expect": 514.4350521883996, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [45.61567, 4.971307, 94.305046] (the system Helmholtz free energy (Wikidata Q865821))" + }, + { + "q": "Tell me the internal energy with Helmholtz free energy of 45.6157, thermodynamic temperature of 4.97131 and entropy of 94.305.", + "a": "514.4350521883996", + "valid": true, + "kind": "compute", + "expect": 514.4350521883996, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [45.61567, 4.971307, 94.305046] (the system Helmholtz free energy (Wikidata Q865821))" + }, + { + "q": "What is the internal energy for Helmholtz free energy 10.5638, thermodynamic temperature 198.839 and entropy 0.671697?", + "a": "144.1234122261961", + "valid": true, + "kind": "compute", + "expect": 144.1234122261961, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [10.563843, 198.838955, 0.671697] (the system Helmholtz free energy (Wikidata Q865821))" + }, + { + "q": "Tell me the internal energy with Helmholtz free energy of 10.5638, thermodynamic temperature of 198.839 and entropy of 0.671697.", + "a": "144.1234122261961", + "valid": true, + "kind": "compute", + "expect": 144.1234122261961, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [10.563843, 198.838955, 0.671697] (the system Helmholtz free energy (Wikidata Q865821))" + }, + { + "q": "What is the internal energy for Helmholtz free energy 21.7931, thermodynamic temperature 29.5106 and entropy 8.50341?", + "a": "272.7338483754556", + "valid": true, + "kind": "compute", + "expect": 272.7338483754556, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [21.793126, 29.510609, 8.503407] (the system Helmholtz free energy (Wikidata Q865821))" + }, + { + "q": "Tell me the internal energy with Helmholtz free energy of 21.7931, thermodynamic temperature of 29.5106 and entropy of 8.50341.", + "a": "272.7338483754556", + "valid": true, + "kind": "compute", + "expect": 272.7338483754556, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [21.793126, 29.510609, 8.503407] (the system Helmholtz free energy (Wikidata Q865821))" + }, + { + "q": "What is the internal energy for Helmholtz free energy 223.509, thermodynamic temperature 17.3414 and entropy 7.10705?", + "a": "346.75522742302365", + "valid": true, + "kind": "compute", + "expect": 346.75522742302365, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [223.508796, 17.341435, 7.107049] (the system Helmholtz free energy (Wikidata Q865821))" + }, + { + "q": "Tell me the internal energy with Helmholtz free energy of 223.509, thermodynamic temperature of 17.3414 and entropy of 7.10705.", + "a": "346.75522742302365", + "valid": true, + "kind": "compute", + "expect": 346.75522742302365, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [223.508796, 17.341435, 7.107049] (the system Helmholtz free energy (Wikidata Q865821))" + }, + { + "q": "What is the internal energy for Helmholtz free energy 107.961 and thermodynamic temperature 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the entropy is not given" + }, + { + "q": "Tell me the internal energy with Helmholtz free energy of 85.2416 and thermodynamic temperature of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the entropy is not given" + }, + { + "q": "What is the internal energy 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 internal energy 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 internal energy with Helmholtz free energy of -45.6157, thermodynamic temperature of 4.97131 and entropy of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Helmholtz free energy: no example had one" + }, + { + "q": "Which internal energy 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 Helmholtz free energy is 17.7888, thermodynamic temperature is 167.625 and the entropy is 10.3291, what is the internal energy?", + "a": "1749.1959934915365", + "valid": true, + "kind": "compute", + "expect": 1749.1959934915365, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [17.788778, 167.624948, 10.329054] (the system Helmholtz free energy (Wikidata Q865821))" + }, + { + "q": "Could you work out the internal energy given Helmholtz free energy = 17.7888, thermodynamic temperature = 167.625, entropy = 10.3291?", + "a": "1749.1959934915365", + "valid": true, + "kind": "compute", + "expect": 1749.1959934915365, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [17.788778, 167.624948, 10.329054] (the system Helmholtz free energy (Wikidata Q865821))" + }, + { + "q": "I need the internal energy for a Helmholtz free energy of 21.511, a thermodynamic temperature of 173.394 and an entropy of 251.084, please.", + "a": "43557.88579373787", + "valid": true, + "kind": "compute", + "expect": 43557.88579373787, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [21.511016, 173.393638, 251.084037] (the system Helmholtz free energy (Wikidata Q865821))" + }, + { + "q": "If the Helmholtz free energy is 21.511, thermodynamic temperature is 173.394 and the entropy is 251.084, what is the internal energy?", + "a": "43557.88579373787", + "valid": true, + "kind": "compute", + "expect": 43557.88579373787, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [21.511016, 173.393638, 251.084037] (the system Helmholtz free energy (Wikidata Q865821))" + }, + { + "q": "Could you work out the internal energy given Helmholtz free energy = 129.245, thermodynamic temperature = 1.18359, entropy = 15.9416?", + "a": "148.11331520188944", + "valid": true, + "kind": "compute", + "expect": 148.11331520188944, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [129.244984, 1.18359, 15.941612] (the system Helmholtz free energy (Wikidata Q865821))" + }, + { + "q": "I need the internal energy for a Helmholtz free energy of 129.245, a thermodynamic temperature of 1.18359 and an entropy of 15.9416, please.", + "a": "148.11331520188944", + "valid": true, + "kind": "compute", + "expect": 148.11331520188944, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [129.244984, 1.18359, 15.941612] (the system Helmholtz free energy (Wikidata Q865821))" + }, + { + "q": "If the Helmholtz free energy is 20.3966, thermodynamic temperature is 18.3169 and the entropy is 6.8386, what is the internal energy?", + "a": "145.6582492752828", + "valid": true, + "kind": "compute", + "expect": 145.6582492752828, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [20.396558, 18.316867, 6.838598] (the system Helmholtz free energy (Wikidata Q865821))" + }, + { + "q": "Could you work out the internal energy given Helmholtz free energy = 20.3966, thermodynamic temperature = 18.3169, entropy = 6.8386?", + "a": "145.6582492752828", + "valid": true, + "kind": "compute", + "expect": 145.6582492752828, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [20.396558, 18.316867, 6.838598] (the system Helmholtz free energy (Wikidata Q865821))" + }, + { + "q": "I need the internal energy for a Helmholtz free energy of 185.886, a thermodynamic temperature of 13.2373 and an entropy of 4.71941, please.", + "a": "248.3586346729236", + "valid": true, + "kind": "compute", + "expect": 248.3586346729236, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [185.886273, 13.237311, 4.719415] (the system Helmholtz free energy (Wikidata Q865821))" + }, + { + "q": "If the Helmholtz free energy is 185.886, thermodynamic temperature is 13.2373 and the entropy is 4.71941, what is the internal energy?", + "a": "248.3586346729236", + "valid": true, + "kind": "compute", + "expect": 248.3586346729236, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [185.886273, 13.237311, 4.719415] (the system Helmholtz free energy (Wikidata Q865821))" + }, + { + "q": "Could you work out the internal energy given Helmholtz free energy = 9.1061, thermodynamic temperature = 24.5721, entropy = 108.451?", + "a": "2673.9682528803705", + "valid": true, + "kind": "compute", + "expect": 2673.9682528803705, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [9.106104, 24.572086, 108.450792] (the system Helmholtz free energy (Wikidata Q865821))" + }, + { + "q": "I need the internal energy for a Helmholtz free energy of 9.1061, a thermodynamic temperature of 24.5721 and an entropy of 108.451, please.", + "a": "2673.9682528803705", + "valid": true, + "kind": "compute", + "expect": 2673.9682528803705, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [9.106104, 24.572086, 108.450792] (the system Helmholtz free energy (Wikidata Q865821))" + }, + { + "q": "If the Helmholtz free energy is 17.7888 and the thermodynamic temperature is 167.625, what is the internal energy?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the entropy is not given" + }, + { + "q": "Could you work out the internal energy given Helmholtz free energy = 21.511, thermodynamic temperature = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the entropy is not given" + }, + { + "q": "Do you know the internal energy 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 internal energy 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 internal energy given Helmholtz free energy = -129.245, thermodynamic temperature = 1.18359, entropy = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Helmholtz free energy: no example had one" + }, + { + "q": "What is a typical internal energy?", + "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-internal-energy-4/program.py b/root/organs10/physics/specialists/solved-internal-energy-4/program.py new file mode 100644 index 0000000000000000000000000000000000000000..af689cb2923a7a32f56b604e8384c5290e7b7ada --- /dev/null +++ b/root/organs10/physics/specialists/solved-internal-energy-4/program.py @@ -0,0 +1,7 @@ +"""internal energy from Helmholtz free energy, thermodynamic temperature, entropy (Helmholtz free energy (Wikidata Q865821)) +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(helmholtz_free_energy, thermodynamic_temperature, entropy): + return 1.0*entropy*thermodynamic_temperature + 1.0*helmholtz_free_energy diff --git a/root/organs10/physics/specialists/solved-internal-energy-4/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-internal-energy-4/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-internal-energy-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-internal-energy/organ_formula_v2.py b/root/organs10/physics/specialists/solved-internal-energy/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-internal-energy/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-internal-energy/panel.json b/root/organs10/physics/specialists/solved-internal-energy/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..d46a859fda050dc9676521a5a9befec9c6043ac1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-internal-energy/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the internal energy for enthalpy 71.0225, pressure 2.57003 and volume 8.50728?", + "a": "49.15849829712309", + "valid": true, + "kind": "compute", + "expect": 49.15849829712309, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [71.022469, 2.57003, 8.507283] (the system enthalpy (Wikidata Q161064))" + }, + { + "q": "Tell me the internal energy with enthalpy of 71.0225, pressure of 2.57003 and volume of 8.50728.", + "a": "49.15849829712309", + "valid": true, + "kind": "compute", + "expect": 49.15849829712309, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [71.022469, 2.57003, 8.507283] (the system enthalpy (Wikidata Q161064))" + }, + { + "q": "What is the internal energy for enthalpy 82.2401, pressure 13.999 and volume 0.348438?", + "a": "77.36228281993988", + "valid": true, + "kind": "compute", + "expect": 77.36228281993988, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [82.240072, 13.999029, 0.348438] (the system enthalpy (Wikidata Q161064))" + }, + { + "q": "Tell me the internal energy with enthalpy of 82.2401, pressure of 13.999 and volume of 0.348438.", + "a": "77.36228281993988", + "valid": true, + "kind": "compute", + "expect": 77.36228281993988, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [82.240072, 13.999029, 0.348438] (the system enthalpy (Wikidata Q161064))" + }, + { + "q": "What is the internal energy for enthalpy 6.98086, pressure 0.551236 and volume 2.88512?", + "a": "5.390475225481023", + "valid": true, + "kind": "compute", + "expect": 5.390475225481023, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [6.980857, 0.551236, 2.88512] (the system enthalpy (Wikidata Q161064))" + }, + { + "q": "Tell me the internal energy with enthalpy of 6.98086, pressure of 0.551236 and volume of 2.88512.", + "a": "5.390475225481023", + "valid": true, + "kind": "compute", + "expect": 5.390475225481023, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [6.980857, 0.551236, 2.88512] (the system enthalpy (Wikidata Q161064))" + }, + { + "q": "What is the internal energy for enthalpy 185.886, pressure 13.2373 and volume 4.71941?", + "a": "123.41391091072475", + "valid": true, + "kind": "compute", + "expect": 123.41391091072475, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [185.886273, 13.237311, 4.719415] (the system enthalpy (Wikidata Q161064))" + }, + { + "q": "Tell me the internal energy with enthalpy of 185.886, pressure of 13.2373 and volume of 4.71941.", + "a": "123.41391091072475", + "valid": true, + "kind": "compute", + "expect": 123.41391091072475, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [185.886273, 13.237311, 4.719415] (the system enthalpy (Wikidata Q161064))" + }, + { + "q": "What is the internal energy for enthalpy 88.6624, pressure 0.633611 and volume 0.869111?", + "a": "88.11168212986358", + "valid": true, + "kind": "compute", + "expect": 88.11168212986358, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [88.662361, 0.633611, 0.869111] (the system enthalpy (Wikidata Q161064))" + }, + { + "q": "Tell me the internal energy with enthalpy of 88.6624, pressure of 0.633611 and volume of 0.869111.", + "a": "88.11168212986358", + "valid": true, + "kind": "compute", + "expect": 88.11168212986358, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [88.662361, 0.633611, 0.869111] (the system enthalpy (Wikidata Q161064))" + }, + { + "q": "What is the internal energy for enthalpy 3.21921, pressure 0.4336 and volume 2.20115?", + "a": "2.2647938602064017", + "valid": true, + "kind": "compute", + "expect": 2.2647938602064017, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [3.219213, 0.4336, 2.201149] (the system enthalpy (Wikidata Q161064))" + }, + { + "q": "Tell me the internal energy with enthalpy of 3.21921, pressure of 0.4336 and volume of 2.20115.", + "a": "2.2647938602064017", + "valid": true, + "kind": "compute", + "expect": 2.2647938602064017, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [3.219213, 0.4336, 2.201149] (the system enthalpy (Wikidata Q161064))" + }, + { + "q": "What is the internal energy for enthalpy 71.0225 and pressure 2.57003?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the volume is not given" + }, + { + "q": "Tell me the internal energy with enthalpy of 82.2401 and pressure of 13.999.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the volume is not given" + }, + { + "q": "What is the internal energy 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 internal energy 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 internal energy with enthalpy of -6.98086, pressure of 0.551236 and volume of 2.88512.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative enthalpy: no example had one" + }, + { + "q": "Which internal energy 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 enthalpy is 8.24951, pressure is 0.634021 and the volume is 6.34623, what is the internal energy?", + "a": "4.2258697413180455", + "valid": true, + "kind": "compute", + "expect": 4.2258697413180455, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [8.24951, 0.634021, 6.346226] (the system enthalpy (Wikidata Q161064))" + }, + { + "q": "Could you work out the internal energy given enthalpy = 8.24951, pressure = 0.634021, volume = 6.34623?", + "a": "4.2258697413180455", + "valid": true, + "kind": "compute", + "expect": 4.2258697413180455, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [8.24951, 0.634021, 6.346226] (the system enthalpy (Wikidata Q161064))" + }, + { + "q": "I need the internal energy for an enthalpy of 126.709, a pressure of 1.70503 and a volume of 2.9896, please.", + "a": "121.61188696128404", + "valid": true, + "kind": "compute", + "expect": 121.61188696128404, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [126.709252, 1.705032, 2.9896] (the system enthalpy (Wikidata Q161064))" + }, + { + "q": "If the enthalpy is 126.709, pressure is 1.70503 and the volume is 2.9896, what is the internal energy?", + "a": "121.61188696128404", + "valid": true, + "kind": "compute", + "expect": 121.61188696128404, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [126.709252, 1.705032, 2.9896] (the system enthalpy (Wikidata Q161064))" + }, + { + "q": "Could you work out the internal energy given enthalpy = 2.34957, pressure = 1.16997, volume = 1.14865?", + "a": "1.0056842074832226", + "valid": true, + "kind": "compute", + "expect": 1.0056842074832226, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [2.349571, 1.169968, 1.148652] (the system enthalpy (Wikidata Q161064))" + }, + { + "q": "I need the internal energy for an enthalpy of 2.34957, a pressure of 1.16997 and a volume of 1.14865, please.", + "a": "1.0056842074832226", + "valid": true, + "kind": "compute", + "expect": 1.0056842074832226, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [2.349571, 1.169968, 1.148652] (the system enthalpy (Wikidata Q161064))" + }, + { + "q": "If the enthalpy is 223.509, pressure is 17.3414 and the volume is 7.10705, what is the internal energy?", + "a": "100.26236519388404", + "valid": true, + "kind": "compute", + "expect": 100.26236519388404, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [223.508796, 17.341435, 7.107049] (the system enthalpy (Wikidata Q161064))" + }, + { + "q": "Could you work out the internal energy given enthalpy = 223.509, pressure = 17.3414, volume = 7.10705?", + "a": "100.26236519388404", + "valid": true, + "kind": "compute", + "expect": 100.26236519388404, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [223.508796, 17.341435, 7.107049] (the system enthalpy (Wikidata Q161064))" + }, + { + "q": "I need the internal energy for an enthalpy of 276.408, a pressure of 0.406608 and a volume of 0.367206, please.", + "a": "276.2582732193404", + "valid": true, + "kind": "compute", + "expect": 276.2582732193404, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [276.407582, 0.406608, 0.367206] (the system enthalpy (Wikidata Q161064))" + }, + { + "q": "If the enthalpy is 276.408, pressure is 0.406608 and the volume is 0.367206, what is the internal energy?", + "a": "276.2582732193404", + "valid": true, + "kind": "compute", + "expect": 276.2582732193404, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [276.407582, 0.406608, 0.367206] (the system enthalpy (Wikidata Q161064))" + }, + { + "q": "Could you work out the internal energy given enthalpy = 133.743, pressure = 16.0123, volume = 5.53629?", + "a": "45.093806527281856", + "valid": true, + "kind": "compute", + "expect": 45.093806527281856, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [133.742727, 16.01232, 5.536295] (the system enthalpy (Wikidata Q161064))" + }, + { + "q": "I need the internal energy for an enthalpy of 133.743, a pressure of 16.0123 and a volume of 5.53629, please.", + "a": "45.093806527281856", + "valid": true, + "kind": "compute", + "expect": 45.093806527281856, + "dtype": "quantity", + "unit": "J", + "why": "internal energy for [133.742727, 16.01232, 5.536295] (the system enthalpy (Wikidata Q161064))" + }, + { + "q": "If the enthalpy is 8.24951 and the pressure is 0.634021, what is the internal energy?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the volume is not given" + }, + { + "q": "Could you work out the internal energy given enthalpy = 126.709, pressure = 1.70503?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the volume is not given" + }, + { + "q": "Do you know the internal energy 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 internal energy 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 internal energy given enthalpy = -2.34957, pressure = 1.16997, volume = 1.14865?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative enthalpy: no example had one" + }, + { + "q": "What is a typical internal energy?", + "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-internal-energy/program.py b/root/organs10/physics/specialists/solved-internal-energy/program.py new file mode 100644 index 0000000000000000000000000000000000000000..119fd6cb5e1226c0e9d3b698436b1c3aa1babfc4 --- /dev/null +++ b/root/organs10/physics/specialists/solved-internal-energy/program.py @@ -0,0 +1,7 @@ +"""internal energy from enthalpy, pressure, volume (enthalpy (Wikidata Q161064)) +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(enthalpy, pressure, volume): + return 1.0*enthalpy - 1.0*pressure*volume diff --git a/root/organs10/physics/specialists/solved-internal-energy/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-internal-energy/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-internal-energy/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-ion-transport-number/organ_formula_v2.py b/root/organs10/physics/specialists/solved-ion-transport-number/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-ion-transport-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-ion-transport-number/panel.json b/root/organs10/physics/specialists/solved-ion-transport-number/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..c9db35271a10a3ee31ba33aa83da129eabd95cfd --- /dev/null +++ b/root/organs10/physics/specialists/solved-ion-transport-number/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the ion transport number for electric current of ion 107.961 and electric current 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "ion transport number for [107.960666, 59.4105] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Tell me the ion transport number with electric current of ion of 107.961 and electric current of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "ion transport number for [107.960666, 59.4105] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "What is the ion transport number for electric current of ion 169.527 and electric current 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "ion transport number for [169.526958, 280.773702] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Tell me the ion transport number with electric current of ion of 169.527 and electric current of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "ion transport number for [169.526958, 280.773702] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "What is the ion transport number for electric current of ion 1.91195 and electric current 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "ion transport number for [1.911952, 82.240072] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Tell me the ion transport number with electric current of ion of 1.91195 and electric current of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "ion transport number for [1.911952, 82.240072] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "What is the ion transport number for electric current of ion 1.64379 and electric current 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "ion transport number for [1.643795, 252.709024] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Tell me the ion transport number with electric current of ion of 1.64379 and electric current of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "ion transport number for [1.643795, 252.709024] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "What is the ion transport number for electric current of ion 18.3169 and electric current 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "ion transport number for [18.316867, 6.838598] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Tell me the ion transport number with electric current of ion of 18.3169 and electric current of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "ion transport number for [18.316867, 6.838598] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "What is the ion transport number for electric current of ion 156.502 and electric current 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "ion transport number for [156.50181, 185.886273] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Tell me the ion transport number with electric current of ion of 156.502 and electric current of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "ion transport number for [156.50181, 185.886273] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "What is the ion transport number for electric current of ion 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current is not given" + }, + { + "q": "Tell me the ion transport number with electric current of ion of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current is not given" + }, + { + "q": "What is the ion transport 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 ion transport 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 ion transport number with electric current of ion of -1.91195 and electric current of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric current of ion: no example had one" + }, + { + "q": "Which ion transport 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 electric current of ion is 8.50728 and the electric current is 17.7888, what is the ion transport number?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "ion transport number for [8.507283, 17.788778] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Could you work out the ion transport number given electric current of ion = 8.50728, electric current = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "ion transport number for [8.507283, 17.788778] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "I need the ion transport number for an electric current of ion of 8.24951 and an electric current of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "ion transport number for [8.24951, 0.634021] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "If the electric current of ion is 8.24951 and the electric current is 0.634021, what is the ion transport number?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "ion transport number for [8.24951, 0.634021] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Could you work out the ion transport number given electric current of ion = 0.318734, electric current = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "ion transport number for [0.318734, 9.566071] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "I need the ion transport number for an electric current of ion of 0.318734 and an electric current of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "ion transport number for [0.318734, 9.566071] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "If the electric current of ion is 0.671697 and the electric current is 14.2496, what is the ion transport number?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "ion transport number for [0.671697, 14.249569] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Could you work out the ion transport number given electric current of ion = 0.671697, electric current = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "ion transport number for [0.671697, 14.249569] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "I need the ion transport number for an electric current of ion of 21.7931 and an electric current of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "ion transport number for [21.793126, 29.510609] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "If the electric current of ion is 21.7931 and the electric current is 29.5106, what is the ion transport number?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "ion transport number for [21.793126, 29.510609] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Could you work out the ion transport number given electric current of ion = 153.15, electric current = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "ion transport number for [153.149965, 18.595315] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "I need the ion transport number for an electric current of ion of 153.15 and an electric current of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "ion transport number for [153.149965, 18.595315] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "If the electric current of ion is 8.50728, what is the ion transport number?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current is not given" + }, + { + "q": "Could you work out the ion transport number given electric current of ion = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current is not given" + }, + { + "q": "Do you know the ion transport 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 ion transport 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 ion transport number given electric current of ion = -0.318734, electric current = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric current of ion: no example had one" + }, + { + "q": "What is a typical ion transport 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-ion-transport-number/program.py b/root/organs10/physics/specialists/solved-ion-transport-number/program.py new file mode 100644 index 0000000000000000000000000000000000000000..b04f5af9d9345285d6f3bf3b32496cfd456b4dc2 --- /dev/null +++ b/root/organs10/physics/specialists/solved-ion-transport-number/program.py @@ -0,0 +1,7 @@ +"""ion transport number from electric current of ion, electric current (ion transport number (Wikidata Q331854)) +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(electric_current_of_ion, electric_current): + return 1.0*electric_current_of_ion/electric_current diff --git a/root/organs10/physics/specialists/solved-ion-transport-number/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-ion-transport-number/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-ion-transport-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-irradiance/organ_formula_v2.py b/root/organs10/physics/specialists/solved-irradiance/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-irradiance/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-irradiance/panel.json b/root/organs10/physics/specialists/solved-irradiance/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..cc7812fd8e4d78da258b4c6f9bbc04cc47924191 --- /dev/null +++ b/root/organs10/physics/specialists/solved-irradiance/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the irradiance for power 107.961, panel area 59.4105 and efficiency 5.77717?", + "a": "0.31454848897160476", + "valid": true, + "kind": "compute", + "expect": 0.31454848897160476, + "dtype": "quantity", + "unit": "W/m^2", + "why": "irradiance for [107.960666, 59.4105, 5.777165] (the system Solar power)" + }, + { + "q": "Tell me the irradiance with power of 107.961, panel area of 59.4105 and efficiency of 5.77717.", + "a": "0.31454848897160476", + "valid": true, + "kind": "compute", + "expect": 0.31454848897160476, + "dtype": "quantity", + "unit": "W/m^2", + "why": "irradiance for [107.960666, 59.4105, 5.777165] (the system Solar power)" + }, + { + "q": "What is the irradiance for power 85.2416, panel area 160.878 and efficiency 2.69428?", + "a": "0.19665778745152376", + "valid": true, + "kind": "compute", + "expect": 0.19665778745152376, + "dtype": "quantity", + "unit": "W/m^2", + "why": "irradiance for [85.241623, 160.878442, 2.69428] (the system Solar power)" + }, + { + "q": "Tell me the irradiance with power of 85.2416, panel area of 160.878 and efficiency of 2.69428.", + "a": "0.19665778745152376", + "valid": true, + "kind": "compute", + "expect": 0.19665778745152376, + "dtype": "quantity", + "unit": "W/m^2", + "why": "irradiance for [85.241623, 160.878442, 2.69428] (the system Solar power)" + }, + { + "q": "What is the irradiance for power 45.6157, panel area 4.97131 and efficiency 94.305?", + "a": "0.09729902784967777", + "valid": true, + "kind": "compute", + "expect": 0.09729902784967777, + "dtype": "quantity", + "unit": "W/m^2", + "why": "irradiance for [45.61567, 4.971307, 94.305046] (the system Solar power)" + }, + { + "q": "Tell me the irradiance with power of 45.6157, panel area of 4.97131 and efficiency of 94.305.", + "a": "0.09729902784967777", + "valid": true, + "kind": "compute", + "expect": 0.09729902784967777, + "dtype": "quantity", + "unit": "W/m^2", + "why": "irradiance for [45.61567, 4.971307, 94.305046] (the system Solar power)" + }, + { + "q": "What is the irradiance for power 10.5638, panel area 198.839 and efficiency 0.671697?", + "a": "0.07909461512056756", + "valid": true, + "kind": "compute", + "expect": 0.07909461512056756, + "dtype": "quantity", + "unit": "W/m^2", + "why": "irradiance for [10.563843, 198.838955, 0.671697] (the system Solar power)" + }, + { + "q": "Tell me the irradiance with power of 10.5638, panel area of 198.839 and efficiency of 0.671697.", + "a": "0.07909461512056756", + "valid": true, + "kind": "compute", + "expect": 0.07909461512056756, + "dtype": "quantity", + "unit": "W/m^2", + "why": "irradiance for [10.563843, 198.838955, 0.671697] (the system Solar power)" + }, + { + "q": "What is the irradiance for power 21.7931, panel area 29.5106 and efficiency 8.50341?", + "a": "0.08684571530973768", + "valid": true, + "kind": "compute", + "expect": 0.08684571530973768, + "dtype": "quantity", + "unit": "W/m^2", + "why": "irradiance for [21.793126, 29.510609, 8.503407] (the system Solar power)" + }, + { + "q": "Tell me the irradiance with power of 21.7931, panel area of 29.5106 and efficiency of 8.50341.", + "a": "0.08684571530973768", + "valid": true, + "kind": "compute", + "expect": 0.08684571530973768, + "dtype": "quantity", + "unit": "W/m^2", + "why": "irradiance for [21.793126, 29.510609, 8.503407] (the system Solar power)" + }, + { + "q": "What is the irradiance for power 223.509, panel area 17.3414 and efficiency 7.10705?", + "a": "1.813511306470856", + "valid": true, + "kind": "compute", + "expect": 1.813511306470856, + "dtype": "quantity", + "unit": "W/m^2", + "why": "irradiance for [223.508796, 17.341435, 7.107049] (the system Solar power)" + }, + { + "q": "Tell me the irradiance with power of 223.509, panel area of 17.3414 and efficiency of 7.10705.", + "a": "1.813511306470856", + "valid": true, + "kind": "compute", + "expect": 1.813511306470856, + "dtype": "quantity", + "unit": "W/m^2", + "why": "irradiance for [223.508796, 17.341435, 7.107049] (the system Solar power)" + }, + { + "q": "What is the irradiance for power 107.961 and panel area 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the efficiency is not given" + }, + { + "q": "Tell me the irradiance with power of 85.2416 and panel area of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the efficiency is not given" + }, + { + "q": "What is the irradiance 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 irradiance 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 irradiance with power of -45.6157, panel area of 4.97131 and efficiency of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative power: no example had one" + }, + { + "q": "Which irradiance 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 17.7888, panel area is 167.625 and the efficiency is 10.3291, what is the irradiance?", + "a": "0.010274173498684694", + "valid": true, + "kind": "compute", + "expect": 0.010274173498684694, + "dtype": "quantity", + "unit": "W/m^2", + "why": "irradiance for [17.788778, 167.624948, 10.329054] (the system Solar power)" + }, + { + "q": "Could you work out the irradiance given power = 17.7888, panel area = 167.625, efficiency = 10.3291?", + "a": "0.010274173498684694", + "valid": true, + "kind": "compute", + "expect": 0.010274173498684694, + "dtype": "quantity", + "unit": "W/m^2", + "why": "irradiance for [17.788778, 167.624948, 10.329054] (the system Solar power)" + }, + { + "q": "I need the irradiance for a power of 21.511, a panel area of 173.394 and an efficiency of 251.084, please.", + "a": "0.000494092950151496", + "valid": true, + "kind": "compute", + "expect": 0.000494092950151496, + "dtype": "quantity", + "unit": "W/m^2", + "why": "irradiance for [21.511016, 173.393638, 251.084037] (the system Solar power)" + }, + { + "q": "If the power is 21.511, panel area is 173.394 and the efficiency is 251.084, what is the irradiance?", + "a": "0.000494092950151496", + "valid": true, + "kind": "compute", + "expect": 0.000494092950151496, + "dtype": "quantity", + "unit": "W/m^2", + "why": "irradiance for [21.511016, 173.393638, 251.084037] (the system Solar power)" + }, + { + "q": "Could you work out the irradiance given power = 129.245, panel area = 1.18359, efficiency = 15.9416?", + "a": "6.849836414751194", + "valid": true, + "kind": "compute", + "expect": 6.849836414751194, + "dtype": "quantity", + "unit": "W/m^2", + "why": "irradiance for [129.244984, 1.18359, 15.941612] (the system Solar power)" + }, + { + "q": "I need the irradiance for a power of 129.245, a panel area of 1.18359 and an efficiency of 15.9416, please.", + "a": "6.849836414751194", + "valid": true, + "kind": "compute", + "expect": 6.849836414751194, + "dtype": "quantity", + "unit": "W/m^2", + "why": "irradiance for [129.244984, 1.18359, 15.941612] (the system Solar power)" + }, + { + "q": "If the power is 20.3966, panel area is 18.3169 and the efficiency is 6.8386, what is the irradiance?", + "a": "0.16283157083473473", + "valid": true, + "kind": "compute", + "expect": 0.16283157083473473, + "dtype": "quantity", + "unit": "W/m^2", + "why": "irradiance for [20.396558, 18.316867, 6.838598] (the system Solar power)" + }, + { + "q": "Could you work out the irradiance given power = 20.3966, panel area = 18.3169, efficiency = 6.8386?", + "a": "0.16283157083473473", + "valid": true, + "kind": "compute", + "expect": 0.16283157083473473, + "dtype": "quantity", + "unit": "W/m^2", + "why": "irradiance for [20.396558, 18.316867, 6.838598] (the system Solar power)" + }, + { + "q": "I need the irradiance for a power of 185.886, a panel area of 13.2373 and an efficiency of 4.71941, please.", + "a": "2.975496158535072", + "valid": true, + "kind": "compute", + "expect": 2.975496158535072, + "dtype": "quantity", + "unit": "W/m^2", + "why": "irradiance for [185.886273, 13.237311, 4.719415] (the system Solar power)" + }, + { + "q": "If the power is 185.886, panel area is 13.2373 and the efficiency is 4.71941, what is the irradiance?", + "a": "2.975496158535072", + "valid": true, + "kind": "compute", + "expect": 2.975496158535072, + "dtype": "quantity", + "unit": "W/m^2", + "why": "irradiance for [185.886273, 13.237311, 4.719415] (the system Solar power)" + }, + { + "q": "Could you work out the irradiance given power = 9.1061, panel area = 24.5721, efficiency = 108.451?", + "a": "0.003417101331511669", + "valid": true, + "kind": "compute", + "expect": 0.003417101331511669, + "dtype": "quantity", + "unit": "W/m^2", + "why": "irradiance for [9.106104, 24.572086, 108.450792] (the system Solar power)" + }, + { + "q": "I need the irradiance for a power of 9.1061, a panel area of 24.5721 and an efficiency of 108.451, please.", + "a": "0.003417101331511669", + "valid": true, + "kind": "compute", + "expect": 0.003417101331511669, + "dtype": "quantity", + "unit": "W/m^2", + "why": "irradiance for [9.106104, 24.572086, 108.450792] (the system Solar power)" + }, + { + "q": "If the power is 17.7888 and the panel area is 167.625, what is the irradiance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the efficiency is not given" + }, + { + "q": "Could you work out the irradiance given power = 21.511, panel area = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the efficiency is not given" + }, + { + "q": "Do you know the irradiance 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 irradiance 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 irradiance given power = -129.245, panel area = 1.18359, efficiency = 15.9416?", + "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 irradiance?", + "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-irradiance/program.py b/root/organs10/physics/specialists/solved-irradiance/program.py new file mode 100644 index 0000000000000000000000000000000000000000..572635a0b7d7863360f3a7c9ee2724150ca37a07 --- /dev/null +++ b/root/organs10/physics/specialists/solved-irradiance/program.py @@ -0,0 +1,7 @@ +"""irradiance from power, panel area, efficiency (Solar power) +The maths organ (systems.py): solved from the system P = G*A*e; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(power, panel_area, efficiency): + return 1.0*power/(efficiency*panel_area) diff --git a/root/organs10/physics/specialists/solved-irradiance/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-irradiance/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-irradiance/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-kinematic-viscosity-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-kinematic-viscosity-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity-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-kinematic-viscosity-2/panel.json b/root/organs10/physics/specialists/solved-kinematic-viscosity-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..bb676cac2e09c1b81f81c6b760e976bb45377ac5 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the kinematic viscosity for relaxation time 107.961, elasticity number 59.4105 and pipe radius 5.77717?", + "a": "18.366531129286077", + "valid": true, + "kind": "compute", + "expect": 18.366531129286077, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [107.960666, 59.4105, 5.777165] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "Tell me the kinematic viscosity with relaxation time of 107.961, elasticity number of 59.4105 and pipe radius of 5.77717.", + "a": "18.366531129286077", + "valid": true, + "kind": "compute", + "expect": 18.366531129286077, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [107.960666, 59.4105, 5.777165] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "What is the kinematic viscosity for relaxation time 85.2416, elasticity number 160.878 and pipe radius 2.69428?", + "a": "13.700346754392312", + "valid": true, + "kind": "compute", + "expect": 13.700346754392312, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [85.241623, 160.878442, 2.69428] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "Tell me the kinematic viscosity with relaxation time of 85.2416, elasticity number of 160.878 and pipe radius of 2.69428.", + "a": "13.700346754392312", + "valid": true, + "kind": "compute", + "expect": 13.700346754392312, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [85.241623, 160.878442, 2.69428] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "What is the kinematic viscosity for relaxation time 45.6157, elasticity number 4.97131 and pipe radius 94.305?", + "a": "969.22906762173", + "valid": true, + "kind": "compute", + "expect": 969.22906762173, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [45.61567, 4.971307, 94.305046] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "Tell me the kinematic viscosity with relaxation time of 45.6157, elasticity number of 4.97131 and pipe radius of 94.305.", + "a": "969.22906762173", + "valid": true, + "kind": "compute", + "expect": 969.22906762173, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [45.61567, 4.971307, 94.305046] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "What is the kinematic viscosity for relaxation time 10.5638, elasticity number 198.839 and pipe radius 0.671697?", + "a": "8.492325283716148", + "valid": true, + "kind": "compute", + "expect": 8.492325283716148, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [10.563843, 198.838955, 0.671697] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "Tell me the kinematic viscosity with relaxation time of 10.5638, elasticity number of 198.839 and pipe radius of 0.671697.", + "a": "8.492325283716148", + "valid": true, + "kind": "compute", + "expect": 8.492325283716148, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [10.563843, 198.838955, 0.671697] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "What is the kinematic viscosity for relaxation time 21.7931, elasticity number 29.5106 and pipe radius 8.50341?", + "a": "97.91395189115711", + "valid": true, + "kind": "compute", + "expect": 97.91395189115711, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [21.793126, 29.510609, 8.503407] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "Tell me the kinematic viscosity with relaxation time of 21.7931, elasticity number of 29.5106 and pipe radius of 8.50341.", + "a": "97.91395189115711", + "valid": true, + "kind": "compute", + "expect": 97.91395189115711, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [21.793126, 29.510609, 8.503407] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "What is the kinematic viscosity for relaxation time 223.509, elasticity number 17.3414 and pipe radius 7.10705?", + "a": "3.9189439520265457", + "valid": true, + "kind": "compute", + "expect": 3.9189439520265457, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [223.508796, 17.341435, 7.107049] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "Tell me the kinematic viscosity with relaxation time of 223.509, elasticity number of 17.3414 and pipe radius of 7.10705.", + "a": "3.9189439520265457", + "valid": true, + "kind": "compute", + "expect": 3.9189439520265457, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [223.508796, 17.341435, 7.107049] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "What is the kinematic viscosity for relaxation time 107.961 and elasticity number 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the pipe radius is not given" + }, + { + "q": "Tell me the kinematic viscosity with relaxation time of 85.2416 and elasticity number of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the pipe radius is not given" + }, + { + "q": "What is the kinematic viscosity 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 kinematic viscosity 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 kinematic viscosity with relaxation time of -45.6157, elasticity number of 4.97131 and pipe radius of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative relaxation time: no example had one" + }, + { + "q": "Which kinematic viscosity 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 relaxation time is 17.7888, elasticity number is 167.625 and the pipe radius is 10.3291, what is the kinematic viscosity?", + "a": "1005.3416429182549", + "valid": true, + "kind": "compute", + "expect": 1005.3416429182549, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [17.788778, 167.624948, 10.329054] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "Could you work out the kinematic viscosity given relaxation time = 17.7888, elasticity number = 167.625, pipe radius = 10.3291?", + "a": "1005.3416429182549", + "valid": true, + "kind": "compute", + "expect": 1005.3416429182549, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [17.788778, 167.624948, 10.329054] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "I need the kinematic viscosity for a relaxation time of 21.511, an elasticity number of 173.394 and a pipe radius of 251.084, please.", + "a": "508171.6653749755", + "valid": true, + "kind": "compute", + "expect": 508171.6653749755, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [21.511016, 173.393638, 251.084037] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "If the relaxation time is 21.511, elasticity number is 173.394 and the pipe radius is 251.084, what is the kinematic viscosity?", + "a": "508171.6653749755", + "valid": true, + "kind": "compute", + "expect": 508171.6653749755, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [21.511016, 173.393638, 251.084037] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "Could you work out the kinematic viscosity given relaxation time = 129.245, elasticity number = 1.18359, pipe radius = 15.9416?", + "a": "2.327298137242035", + "valid": true, + "kind": "compute", + "expect": 2.327298137242035, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [129.244984, 1.18359, 15.941612] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "I need the kinematic viscosity for a relaxation time of 129.245, an elasticity number of 1.18359 and a pipe radius of 15.9416, please.", + "a": "2.327298137242035", + "valid": true, + "kind": "compute", + "expect": 2.327298137242035, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [129.244984, 1.18359, 15.941612] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "If the relaxation time is 20.3966, elasticity number is 18.3169 and the pipe radius is 6.8386, what is the kinematic viscosity?", + "a": "41.997986179011534", + "valid": true, + "kind": "compute", + "expect": 41.997986179011534, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [20.396558, 18.316867, 6.838598] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "Could you work out the kinematic viscosity given relaxation time = 20.3966, elasticity number = 18.3169, pipe radius = 6.8386?", + "a": "41.997986179011534", + "valid": true, + "kind": "compute", + "expect": 41.997986179011534, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [20.396558, 18.316867, 6.838598] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "I need the kinematic viscosity for a relaxation time of 185.886, an elasticity number of 13.2373 and a pipe radius of 4.71941, please.", + "a": "1.5860933957447276", + "valid": true, + "kind": "compute", + "expect": 1.5860933957447276, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [185.886273, 13.237311, 4.719415] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "If the relaxation time is 185.886, elasticity number is 13.2373 and the pipe radius is 4.71941, what is the kinematic viscosity?", + "a": "1.5860933957447276", + "valid": true, + "kind": "compute", + "expect": 1.5860933957447276, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [185.886273, 13.237311, 4.719415] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "Could you work out the kinematic viscosity given relaxation time = 9.1061, elasticity number = 24.5721, pipe radius = 108.451?", + "a": "31737.657518771255", + "valid": true, + "kind": "compute", + "expect": 31737.657518771255, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [9.106104, 24.572086, 108.450792] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "I need the kinematic viscosity for a relaxation time of 9.1061, an elasticity number of 24.5721 and a pipe radius of 108.451, please.", + "a": "31737.657518771255", + "valid": true, + "kind": "compute", + "expect": 31737.657518771255, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [9.106104, 24.572086, 108.450792] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "If the relaxation time is 17.7888 and the elasticity number is 167.625, what is the kinematic viscosity?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the pipe radius is not given" + }, + { + "q": "Could you work out the kinematic viscosity given relaxation time = 21.511, elasticity number = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the pipe radius is not given" + }, + { + "q": "Do you know the kinematic viscosity 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 kinematic viscosity 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 kinematic viscosity given relaxation time = -129.245, elasticity number = 1.18359, pipe radius = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative relaxation time: no example had one" + }, + { + "q": "What is a typical kinematic viscosity?", + "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-kinematic-viscosity-2/program.py b/root/organs10/physics/specialists/solved-kinematic-viscosity-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..d4c76c8bfe3b733e0fe85f2d859fda8c116c08c7 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity-2/program.py @@ -0,0 +1,7 @@ +"""kinematic viscosity from relaxation time, elasticity number, pipe radius (elasticity number (Wikidata Q102310770)) +The maths organ (systems.py): solved from the system q1 = ((q0*q2)/(q3 **(2))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(relaxation_time, elasticity_number, pipe_radius): + return 1.0*elasticity_number*pipe_radius**2/relaxation_time diff --git a/root/organs10/physics/specialists/solved-kinematic-viscosity-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-kinematic-viscosity-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity-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-kinematic-viscosity-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-kinematic-viscosity-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity-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-kinematic-viscosity-3/panel.json b/root/organs10/physics/specialists/solved-kinematic-viscosity-3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..e0baed604dd5e8c1933e21912cfe040369404d6e --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity-3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the kinematic viscosity for Stokes number for vibrating particles 107.961, particle diameter 59.4105 and particle vibration frequency 5.77717?", + "a": "2201439.2974362397", + "valid": true, + "kind": "compute", + "expect": 2201439.2974362397, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [107.960666, 59.4105, 5.777165] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "Tell me the kinematic viscosity with Stokes number for vibrating particles of 107.961, particle diameter of 59.4105 and particle vibration frequency of 5.77717.", + "a": "2201439.2974362397", + "valid": true, + "kind": "compute", + "expect": 2201439.2974362397, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [107.960666, 59.4105, 5.777165] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "What is the kinematic viscosity for Stokes number for vibrating particles 85.2416, particle diameter 160.878 and particle vibration frequency 2.69428?", + "a": "5944154.904280896", + "valid": true, + "kind": "compute", + "expect": 5944154.904280896, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [85.241623, 160.878442, 2.69428] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "Tell me the kinematic viscosity with Stokes number for vibrating particles of 85.2416, particle diameter of 160.878 and particle vibration frequency of 2.69428.", + "a": "5944154.904280896", + "valid": true, + "kind": "compute", + "expect": 5944154.904280896, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [85.241623, 160.878442, 2.69428] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "What is the kinematic viscosity for Stokes number for vibrating particles 45.6157, particle diameter 4.97131 and particle vibration frequency 94.305?", + "a": "106313.94748542417", + "valid": true, + "kind": "compute", + "expect": 106313.94748542417, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [45.61567, 4.971307, 94.305046] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "Tell me the kinematic viscosity with Stokes number for vibrating particles of 45.6157, particle diameter of 4.97131 and particle vibration frequency of 94.305.", + "a": "106313.94748542417", + "valid": true, + "kind": "compute", + "expect": 106313.94748542417, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [45.61567, 4.971307, 94.305046] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "What is the kinematic viscosity for Stokes number for vibrating particles 10.5638, particle diameter 198.839 and particle vibration frequency 0.671697?", + "a": "280542.3371596755", + "valid": true, + "kind": "compute", + "expect": 280542.3371596755, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [10.563843, 198.838955, 0.671697] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "Tell me the kinematic viscosity with Stokes number for vibrating particles of 10.5638, particle diameter of 198.839 and particle vibration frequency of 0.671697.", + "a": "280542.3371596755", + "valid": true, + "kind": "compute", + "expect": 280542.3371596755, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [10.563843, 198.838955, 0.671697] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "What is the kinematic viscosity for Stokes number for vibrating particles 21.7931, particle diameter 29.5106 and particle vibration frequency 8.50341?", + "a": "161387.11183269683", + "valid": true, + "kind": "compute", + "expect": 161387.11183269683, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [21.793126, 29.510609, 8.503407] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "Tell me the kinematic viscosity with Stokes number for vibrating particles of 21.7931, particle diameter of 29.5106 and particle vibration frequency of 8.50341.", + "a": "161387.11183269683", + "valid": true, + "kind": "compute", + "expect": 161387.11183269683, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [21.793126, 29.510609, 8.503407] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "What is the kinematic viscosity for Stokes number for vibrating particles 223.509, particle diameter 17.3414 and particle vibration frequency 7.10705?", + "a": "477698.6391282307", + "valid": true, + "kind": "compute", + "expect": 477698.6391282307, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [223.508796, 17.341435, 7.107049] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "Tell me the kinematic viscosity with Stokes number for vibrating particles of 223.509, particle diameter of 17.3414 and particle vibration frequency of 7.10705.", + "a": "477698.6391282307", + "valid": true, + "kind": "compute", + "expect": 477698.6391282307, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [223.508796, 17.341435, 7.107049] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "What is the kinematic viscosity for Stokes number for vibrating particles 107.961 and particle diameter 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the particle vibration frequency is not given" + }, + { + "q": "Tell me the kinematic viscosity with Stokes number for vibrating particles of 85.2416 and particle diameter of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the particle vibration frequency is not given" + }, + { + "q": "What is the kinematic viscosity 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 kinematic viscosity 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 kinematic viscosity with Stokes number for vibrating particles of -45.6157, particle diameter of 4.97131 and particle vibration frequency of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Stokes number for vibrating particles: no example had one" + }, + { + "q": "Which kinematic viscosity 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 Stokes number for vibrating particles is 17.7888, particle diameter is 167.625 and the particle vibration frequency is 10.3291, what is the kinematic viscosity?", + "a": "5162784.493670278", + "valid": true, + "kind": "compute", + "expect": 5162784.493670278, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [17.788778, 167.624948, 10.329054] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "Could you work out the kinematic viscosity given Stokes number for vibrating particles = 17.7888, particle diameter = 167.625, particle vibration frequency = 10.3291?", + "a": "5162784.493670278", + "valid": true, + "kind": "compute", + "expect": 5162784.493670278, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [17.788778, 167.624948, 10.329054] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "I need the kinematic viscosity for a Stokes number for vibrating particles of 21.511, a particle diameter of 173.394 and a particle vibration frequency of 251.084, please.", + "a": "162385162.06002787", + "valid": true, + "kind": "compute", + "expect": 162385162.06002787, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [21.511016, 173.393638, 251.084037] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "If the Stokes number for vibrating particles is 21.511, particle diameter is 173.394 and the particle vibration frequency is 251.084, what is the kinematic viscosity?", + "a": "162385162.06002787", + "valid": true, + "kind": "compute", + "expect": 162385162.06002787, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [21.511016, 173.393638, 251.084037] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "Could you work out the kinematic viscosity given Stokes number for vibrating particles = 129.245, particle diameter = 1.18359, particle vibration frequency = 15.9416?", + "a": "2886.34650764274", + "valid": true, + "kind": "compute", + "expect": 2886.34650764274, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [129.244984, 1.18359, 15.941612] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "I need the kinematic viscosity for a Stokes number for vibrating particles of 129.245, a particle diameter of 1.18359 and a particle vibration frequency of 15.9416, please.", + "a": "2886.34650764274", + "valid": true, + "kind": "compute", + "expect": 2886.34650764274, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [129.244984, 1.18359, 15.941612] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "If the Stokes number for vibrating particles is 20.3966, particle diameter is 18.3169 and the particle vibration frequency is 6.8386, what is the kinematic viscosity?", + "a": "46797.898135746706", + "valid": true, + "kind": "compute", + "expect": 46797.898135746706, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [20.396558, 18.316867, 6.838598] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "Could you work out the kinematic viscosity given Stokes number for vibrating particles = 20.3966, particle diameter = 18.3169, particle vibration frequency = 6.8386?", + "a": "46797.898135746706", + "valid": true, + "kind": "compute", + "expect": 46797.898135746706, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [20.396558, 18.316867, 6.838598] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "I need the kinematic viscosity for a Stokes number for vibrating particles of 185.886, a particle diameter of 13.2373 and a particle vibration frequency of 4.71941, please.", + "a": "153721.6437896738", + "valid": true, + "kind": "compute", + "expect": 153721.6437896738, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [185.886273, 13.237311, 4.719415] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "If the Stokes number for vibrating particles is 185.886, particle diameter is 13.2373 and the particle vibration frequency is 4.71941, what is the kinematic viscosity?", + "a": "153721.6437896738", + "valid": true, + "kind": "compute", + "expect": 153721.6437896738, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [185.886273, 13.237311, 4.719415] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "Could you work out the kinematic viscosity given Stokes number for vibrating particles = 9.1061, particle diameter = 24.5721, particle vibration frequency = 108.451?", + "a": "596278.8090704964", + "valid": true, + "kind": "compute", + "expect": 596278.8090704964, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [9.106104, 24.572086, 108.450792] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "I need the kinematic viscosity for a Stokes number for vibrating particles of 9.1061, a particle diameter of 24.5721 and a particle vibration frequency of 108.451, please.", + "a": "596278.8090704964", + "valid": true, + "kind": "compute", + "expect": 596278.8090704964, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [9.106104, 24.572086, 108.450792] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "If the Stokes number for vibrating particles is 17.7888 and the particle diameter is 167.625, what is the kinematic viscosity?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the particle vibration frequency is not given" + }, + { + "q": "Could you work out the kinematic viscosity given Stokes number for vibrating particles = 21.511, particle diameter = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the particle vibration frequency is not given" + }, + { + "q": "Do you know the kinematic viscosity 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 kinematic viscosity 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 kinematic viscosity given Stokes number for vibrating particles = -129.245, particle diameter = 1.18359, particle vibration frequency = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Stokes number for vibrating particles: no example had one" + }, + { + "q": "What is a typical kinematic viscosity?", + "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-kinematic-viscosity-3/program.py b/root/organs10/physics/specialists/solved-kinematic-viscosity-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..f72075201abe27e458b2601b84a77096e69fe58c --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity-3/program.py @@ -0,0 +1,7 @@ +"""kinematic viscosity from Stokes number for vibrating particles, particle diameter, particle vibration frequency (Stokes number for vibrating particles (Wikidata Q103820258)) +The maths organ (systems.py): solved from the system q0 = ((q1)/(q2 **(2)*q3)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(stokes_number_for_vibrating_particles, particle_diameter, particle_vibration_frequency): + return 1.0*particle_diameter**2*particle_vibration_frequency*stokes_number_for_vibrating_particles diff --git a/root/organs10/physics/specialists/solved-kinematic-viscosity-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-kinematic-viscosity-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity-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-kinematic-viscosity-4/organ_formula_v2.py b/root/organs10/physics/specialists/solved-kinematic-viscosity-4/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity-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-kinematic-viscosity-4/panel.json b/root/organs10/physics/specialists/solved-kinematic-viscosity-4/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..eb22453dbd464e1316e7fb575a262b8a02f1c898 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity-4/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the kinematic viscosity for dynamic viscosity 107.961 and mass density 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [107.960666, 59.4105] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "Tell me the kinematic viscosity with dynamic viscosity of 107.961 and mass density of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [107.960666, 59.4105] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "What is the kinematic viscosity for dynamic viscosity 169.527 and mass density 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [169.526958, 280.773702] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "Tell me the kinematic viscosity with dynamic viscosity of 169.527 and mass density of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [169.526958, 280.773702] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "What is the kinematic viscosity for dynamic viscosity 1.91195 and mass density 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [1.911952, 82.240072] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "Tell me the kinematic viscosity with dynamic viscosity of 1.91195 and mass density of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [1.911952, 82.240072] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "What is the kinematic viscosity for dynamic viscosity 1.64379 and mass density 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [1.643795, 252.709024] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "Tell me the kinematic viscosity with dynamic viscosity of 1.64379 and mass density of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [1.643795, 252.709024] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "What is the kinematic viscosity for dynamic viscosity 18.3169 and mass density 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [18.316867, 6.838598] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "Tell me the kinematic viscosity with dynamic viscosity of 18.3169 and mass density of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [18.316867, 6.838598] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "What is the kinematic viscosity for dynamic viscosity 156.502 and mass density 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [156.50181, 185.886273] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "Tell me the kinematic viscosity with dynamic viscosity of 156.502 and mass density of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [156.50181, 185.886273] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "What is the kinematic viscosity for dynamic viscosity 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Tell me the kinematic viscosity with dynamic viscosity of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "What is the kinematic viscosity 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 kinematic viscosity 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 kinematic viscosity with dynamic viscosity of -1.91195 and mass density of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative dynamic viscosity: no example had one" + }, + { + "q": "Which kinematic viscosity 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 dynamic viscosity is 8.50728 and the mass density is 17.7888, what is the kinematic viscosity?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [8.507283, 17.788778] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "Could you work out the kinematic viscosity given dynamic viscosity = 8.50728, mass density = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [8.507283, 17.788778] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "I need the kinematic viscosity for a dynamic viscosity of 8.24951 and a mass density of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [8.24951, 0.634021] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "If the dynamic viscosity is 8.24951 and the mass density is 0.634021, what is the kinematic viscosity?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [8.24951, 0.634021] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "Could you work out the kinematic viscosity given dynamic viscosity = 0.318734, mass density = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [0.318734, 9.566071] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "I need the kinematic viscosity for a dynamic viscosity of 0.318734 and a mass density of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [0.318734, 9.566071] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "If the dynamic viscosity is 0.671697 and the mass density is 14.2496, what is the kinematic viscosity?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [0.671697, 14.249569] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "Could you work out the kinematic viscosity given dynamic viscosity = 0.671697, mass density = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [0.671697, 14.249569] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "I need the kinematic viscosity for a dynamic viscosity of 21.7931 and a mass density of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [21.793126, 29.510609] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "If the dynamic viscosity is 21.7931 and the mass density is 29.5106, what is the kinematic viscosity?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [21.793126, 29.510609] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "Could you work out the kinematic viscosity given dynamic viscosity = 153.15, mass density = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [153.149965, 18.595315] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "I need the kinematic viscosity for a dynamic viscosity of 153.15 and a mass density of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [153.149965, 18.595315] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "If the dynamic viscosity is 8.50728, what is the kinematic viscosity?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Could you work out the kinematic viscosity given dynamic viscosity = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Do you know the kinematic viscosity 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 kinematic viscosity 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 kinematic viscosity given dynamic viscosity = -0.318734, mass density = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative dynamic viscosity: no example had one" + }, + { + "q": "What is a typical kinematic viscosity?", + "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-kinematic-viscosity-4/program.py b/root/organs10/physics/specialists/solved-kinematic-viscosity-4/program.py new file mode 100644 index 0000000000000000000000000000000000000000..fe43656f82cdaa359567378533694a215794a43e --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity-4/program.py @@ -0,0 +1,7 @@ +"""kinematic viscosity from dynamic viscosity, mass density (kinematic viscosity (Wikidata Q12793663)) +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(dynamic_viscosity, mass_density): + return 1.0*dynamic_viscosity/mass_density diff --git a/root/organs10/physics/specialists/solved-kinematic-viscosity-4/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-kinematic-viscosity-4/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity-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-kinematic-viscosity-5/organ_formula_v2.py b/root/organs10/physics/specialists/solved-kinematic-viscosity-5/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity-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-kinematic-viscosity-5/panel.json b/root/organs10/physics/specialists/solved-kinematic-viscosity-5/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..dd490630766d68b393036ed36621f94f7949e312 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity-5/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the kinematic viscosity for Womersley number 107.961, pipe radius 59.4105 and angular frequency 5.77717?", + "a": "1.7494843061958951", + "valid": true, + "kind": "compute", + "expect": 1.7494843061958951, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [107.960666, 59.4105, 5.777165] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "Tell me the kinematic viscosity with Womersley number of 107.961, pipe radius of 59.4105 and angular frequency of 5.77717.", + "a": "1.7494843061958951", + "valid": true, + "kind": "compute", + "expect": 1.7494843061958951, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [107.960666, 59.4105, 5.777165] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "What is the kinematic viscosity for Womersley number 85.2416, pipe radius 160.878 and angular frequency 2.69428?", + "a": "9.596988946901526", + "valid": true, + "kind": "compute", + "expect": 9.596988946901526, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [85.241623, 160.878442, 2.69428] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "Tell me the kinematic viscosity with Womersley number of 85.2416, pipe radius of 160.878 and angular frequency of 2.69428.", + "a": "9.596988946901526", + "valid": true, + "kind": "compute", + "expect": 9.596988946901526, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [85.241623, 160.878442, 2.69428] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "What is the kinematic viscosity for Womersley number 45.6157, pipe radius 4.97131 and angular frequency 94.305?", + "a": "1.1200774822275874", + "valid": true, + "kind": "compute", + "expect": 1.1200774822275874, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [45.61567, 4.971307, 94.305046] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "Tell me the kinematic viscosity with Womersley number of 45.6157, pipe radius of 4.97131 and angular frequency of 94.305.", + "a": "1.1200774822275874", + "valid": true, + "kind": "compute", + "expect": 1.1200774822275874, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [45.61567, 4.971307, 94.305046] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "What is the kinematic viscosity for Womersley number 10.5638, pipe radius 198.839 and angular frequency 0.671697?", + "a": "237.97570825784274", + "valid": true, + "kind": "compute", + "expect": 237.97570825784274, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [10.563843, 198.838955, 0.671697] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "Tell me the kinematic viscosity with Womersley number of 10.5638, pipe radius of 198.839 and angular frequency of 0.671697.", + "a": "237.97570825784274", + "valid": true, + "kind": "compute", + "expect": 237.97570825784274, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [10.563843, 198.838955, 0.671697] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "What is the kinematic viscosity for Womersley number 21.7931, pipe radius 29.5106 and angular frequency 8.50341?", + "a": "15.59230174569056", + "valid": true, + "kind": "compute", + "expect": 15.59230174569056, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [21.793126, 29.510609, 8.503407] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "Tell me the kinematic viscosity with Womersley number of 21.7931, pipe radius of 29.5106 and angular frequency of 8.50341.", + "a": "15.59230174569056", + "valid": true, + "kind": "compute", + "expect": 15.59230174569056, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [21.793126, 29.510609, 8.503407] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "What is the kinematic viscosity for Womersley number 223.509, pipe radius 17.3414 and angular frequency 7.10705?", + "a": "0.04278289265936307", + "valid": true, + "kind": "compute", + "expect": 0.04278289265936307, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [223.508796, 17.341435, 7.107049] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "Tell me the kinematic viscosity with Womersley number of 223.509, pipe radius of 17.3414 and angular frequency of 7.10705.", + "a": "0.04278289265936307", + "valid": true, + "kind": "compute", + "expect": 0.04278289265936307, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [223.508796, 17.341435, 7.107049] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "What is the kinematic viscosity for Womersley number 107.961 and pipe radius 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the angular frequency is not given" + }, + { + "q": "Tell me the kinematic viscosity with Womersley number of 85.2416 and pipe radius of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the angular frequency is not given" + }, + { + "q": "What is the kinematic viscosity 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 kinematic viscosity 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 kinematic viscosity with Womersley number of -45.6157, pipe radius of 4.97131 and angular frequency of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Womersley number: no example had one" + }, + { + "q": "Which kinematic viscosity 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 Womersley number is 17.7888, pipe radius is 167.625 and the angular frequency is 10.3291, what is the kinematic viscosity?", + "a": "917.1611324983977", + "valid": true, + "kind": "compute", + "expect": 917.1611324983977, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [17.788778, 167.624948, 10.329054] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "Could you work out the kinematic viscosity given Womersley number = 17.7888, pipe radius = 167.625, angular frequency = 10.3291?", + "a": "917.1611324983977", + "valid": true, + "kind": "compute", + "expect": 917.1611324983977, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [17.788778, 167.624948, 10.329054] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "I need the kinematic viscosity for a Womersley number of 21.511, a pipe radius of 173.394 and an angular frequency of 251.084, please.", + "a": "16314.117355495022", + "valid": true, + "kind": "compute", + "expect": 16314.117355495022, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [21.511016, 173.393638, 251.084037] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "If the Womersley number is 21.511, pipe radius is 173.394 and the angular frequency is 251.084, what is the kinematic viscosity?", + "a": "16314.117355495022", + "valid": true, + "kind": "compute", + "expect": 16314.117355495022, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [21.511016, 173.393638, 251.084037] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "Could you work out the kinematic viscosity given Womersley number = 129.245, pipe radius = 1.18359, angular frequency = 15.9416?", + "a": "0.0013369260273022275", + "valid": true, + "kind": "compute", + "expect": 0.0013369260273022275, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [129.244984, 1.18359, 15.941612] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "I need the kinematic viscosity for a Womersley number of 129.245, a pipe radius of 1.18359 and an angular frequency of 15.9416, please.", + "a": "0.0013369260273022275", + "valid": true, + "kind": "compute", + "expect": 0.0013369260273022275, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [129.244984, 1.18359, 15.941612] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "If the Womersley number is 20.3966, pipe radius is 18.3169 and the angular frequency is 6.8386, what is the kinematic viscosity?", + "a": "5.515129266410021", + "valid": true, + "kind": "compute", + "expect": 5.515129266410021, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [20.396558, 18.316867, 6.838598] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "Could you work out the kinematic viscosity given Womersley number = 20.3966, pipe radius = 18.3169, angular frequency = 6.8386?", + "a": "5.515129266410021", + "valid": true, + "kind": "compute", + "expect": 5.515129266410021, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [20.396558, 18.316867, 6.838598] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "I need the kinematic viscosity for a Womersley number of 185.886, a pipe radius of 13.2373 and an angular frequency of 4.71941, please.", + "a": "0.02393277519774517", + "valid": true, + "kind": "compute", + "expect": 0.02393277519774517, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [185.886273, 13.237311, 4.719415] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "If the Womersley number is 185.886, pipe radius is 13.2373 and the angular frequency is 4.71941, what is the kinematic viscosity?", + "a": "0.02393277519774517", + "valid": true, + "kind": "compute", + "expect": 0.02393277519774517, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [185.886273, 13.237311, 4.719415] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "Could you work out the kinematic viscosity given Womersley number = 9.1061, pipe radius = 24.5721, angular frequency = 108.451?", + "a": "789.6807614492757", + "valid": true, + "kind": "compute", + "expect": 789.6807614492757, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [9.106104, 24.572086, 108.450792] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "I need the kinematic viscosity for a Womersley number of 9.1061, a pipe radius of 24.5721 and an angular frequency of 108.451, please.", + "a": "789.6807614492757", + "valid": true, + "kind": "compute", + "expect": 789.6807614492757, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [9.106104, 24.572086, 108.450792] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "If the Womersley number is 17.7888 and the pipe radius is 167.625, what is the kinematic viscosity?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the angular frequency is not given" + }, + { + "q": "Could you work out the kinematic viscosity given Womersley number = 21.511, pipe radius = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the angular frequency is not given" + }, + { + "q": "Do you know the kinematic viscosity 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 kinematic viscosity 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 kinematic viscosity given Womersley number = -129.245, pipe radius = 1.18359, angular frequency = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Womersley number: no example had one" + }, + { + "q": "What is a typical kinematic viscosity?", + "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-kinematic-viscosity-5/program.py b/root/organs10/physics/specialists/solved-kinematic-viscosity-5/program.py new file mode 100644 index 0000000000000000000000000000000000000000..22234d0b2199f6d7b70b98a885779a6fe8085ed7 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity-5/program.py @@ -0,0 +1,7 @@ +"""kinematic viscosity from Womersley number, pipe radius, angular frequency (Womersley number (Wikidata Q2066584)) +The maths organ (systems.py): solved from the system q0 = q1*sqrt(((q2)/(q3))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(womersley_number, pipe_radius, angular_frequency): + return 1.0*angular_frequency*pipe_radius**2/womersley_number**2 diff --git a/root/organs10/physics/specialists/solved-kinematic-viscosity-5/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-kinematic-viscosity-5/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity-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-kinematic-viscosity-6/organ_formula_v2.py b/root/organs10/physics/specialists/solved-kinematic-viscosity-6/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity-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-kinematic-viscosity-6/panel.json b/root/organs10/physics/specialists/solved-kinematic-viscosity-6/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..f2b21a4045b50f9e3b558ca84cf71b72cfee80fd --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity-6/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the kinematic viscosity for Schmidt number 107.961 and mass diffusivity 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [107.960666, 59.4105] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "Tell me the kinematic viscosity with Schmidt number of 107.961 and mass diffusivity of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [107.960666, 59.4105] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "What is the kinematic viscosity for Schmidt number 169.527 and mass diffusivity 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [169.526958, 280.773702] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "Tell me the kinematic viscosity with Schmidt number of 169.527 and mass diffusivity of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [169.526958, 280.773702] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "What is the kinematic viscosity for Schmidt number 1.91195 and mass diffusivity 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [1.911952, 82.240072] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "Tell me the kinematic viscosity with Schmidt number of 1.91195 and mass diffusivity of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [1.911952, 82.240072] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "What is the kinematic viscosity for Schmidt number 1.64379 and mass diffusivity 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [1.643795, 252.709024] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "Tell me the kinematic viscosity with Schmidt number of 1.64379 and mass diffusivity of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [1.643795, 252.709024] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "What is the kinematic viscosity for Schmidt number 18.3169 and mass diffusivity 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [18.316867, 6.838598] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "Tell me the kinematic viscosity with Schmidt number of 18.3169 and mass diffusivity of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [18.316867, 6.838598] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "What is the kinematic viscosity for Schmidt number 156.502 and mass diffusivity 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [156.50181, 185.886273] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "Tell me the kinematic viscosity with Schmidt number of 156.502 and mass diffusivity of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [156.50181, 185.886273] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "What is the kinematic viscosity for Schmidt number 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass diffusivity is not given" + }, + { + "q": "Tell me the kinematic viscosity with Schmidt number of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass diffusivity is not given" + }, + { + "q": "What is the kinematic viscosity 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 kinematic viscosity 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 kinematic viscosity with Schmidt number of -1.91195 and mass diffusivity of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Schmidt number: no example had one" + }, + { + "q": "Which kinematic viscosity 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 Schmidt number is 8.50728 and the mass diffusivity is 17.7888, what is the kinematic viscosity?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [8.507283, 17.788778] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "Could you work out the kinematic viscosity given Schmidt number = 8.50728, mass diffusivity = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [8.507283, 17.788778] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "I need the kinematic viscosity for a Schmidt number of 8.24951 and a mass diffusivity of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [8.24951, 0.634021] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "If the Schmidt number is 8.24951 and the mass diffusivity is 0.634021, what is the kinematic viscosity?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [8.24951, 0.634021] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "Could you work out the kinematic viscosity given Schmidt number = 0.318734, mass diffusivity = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [0.318734, 9.566071] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "I need the kinematic viscosity for a Schmidt number of 0.318734 and a mass diffusivity of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [0.318734, 9.566071] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "If the Schmidt number is 0.671697 and the mass diffusivity is 14.2496, what is the kinematic viscosity?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [0.671697, 14.249569] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "Could you work out the kinematic viscosity given Schmidt number = 0.671697, mass diffusivity = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [0.671697, 14.249569] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "I need the kinematic viscosity for a Schmidt number of 21.7931 and a mass diffusivity of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [21.793126, 29.510609] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "If the Schmidt number is 21.7931 and the mass diffusivity is 29.5106, what is the kinematic viscosity?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [21.793126, 29.510609] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "Could you work out the kinematic viscosity given Schmidt number = 153.15, mass diffusivity = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [153.149965, 18.595315] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "I need the kinematic viscosity for a Schmidt number of 153.15 and a mass diffusivity of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [153.149965, 18.595315] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "If the Schmidt number is 8.50728, what is the kinematic viscosity?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass diffusivity is not given" + }, + { + "q": "Could you work out the kinematic viscosity given Schmidt number = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass diffusivity is not given" + }, + { + "q": "Do you know the kinematic viscosity 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 kinematic viscosity 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 kinematic viscosity given Schmidt number = -0.318734, mass diffusivity = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Schmidt number: no example had one" + }, + { + "q": "What is a typical kinematic viscosity?", + "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-kinematic-viscosity-6/program.py b/root/organs10/physics/specialists/solved-kinematic-viscosity-6/program.py new file mode 100644 index 0000000000000000000000000000000000000000..d70aa0c9c72a31c6e1bf3d10dafdd4b4771c36db --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity-6/program.py @@ -0,0 +1,7 @@ +"""kinematic viscosity from Schmidt number, mass diffusivity (Schmidt number (Wikidata Q581997)) +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(schmidt_number, mass_diffusivity): + return 1.0*mass_diffusivity*schmidt_number diff --git a/root/organs10/physics/specialists/solved-kinematic-viscosity-6/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-kinematic-viscosity-6/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity-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-kinematic-viscosity-7/organ_formula_v2.py b/root/organs10/physics/specialists/solved-kinematic-viscosity-7/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity-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-kinematic-viscosity-7/panel.json b/root/organs10/physics/specialists/solved-kinematic-viscosity-7/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..612553297d789aee07e747eddee94ebfc5e43f1d --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity-7/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the kinematic viscosity for Prandtl number 107.961 and thermal diffusivity 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [107.960666, 59.4105] (the system Prandtl number (Wikidata Q815306))" + }, + { + "q": "Tell me the kinematic viscosity with Prandtl number of 107.961 and thermal diffusivity of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [107.960666, 59.4105] (the system Prandtl number (Wikidata Q815306))" + }, + { + "q": "What is the kinematic viscosity for Prandtl number 169.527 and thermal diffusivity 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [169.526958, 280.773702] (the system Prandtl number (Wikidata Q815306))" + }, + { + "q": "Tell me the kinematic viscosity with Prandtl number of 169.527 and thermal diffusivity of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [169.526958, 280.773702] (the system Prandtl number (Wikidata Q815306))" + }, + { + "q": "What is the kinematic viscosity for Prandtl number 1.91195 and thermal diffusivity 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [1.911952, 82.240072] (the system Prandtl number (Wikidata Q815306))" + }, + { + "q": "Tell me the kinematic viscosity with Prandtl number of 1.91195 and thermal diffusivity of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [1.911952, 82.240072] (the system Prandtl number (Wikidata Q815306))" + }, + { + "q": "What is the kinematic viscosity for Prandtl number 1.64379 and thermal diffusivity 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [1.643795, 252.709024] (the system Prandtl number (Wikidata Q815306))" + }, + { + "q": "Tell me the kinematic viscosity with Prandtl number of 1.64379 and thermal diffusivity of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [1.643795, 252.709024] (the system Prandtl number (Wikidata Q815306))" + }, + { + "q": "What is the kinematic viscosity for Prandtl number 18.3169 and thermal diffusivity 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [18.316867, 6.838598] (the system Prandtl number (Wikidata Q815306))" + }, + { + "q": "Tell me the kinematic viscosity with Prandtl number of 18.3169 and thermal diffusivity of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [18.316867, 6.838598] (the system Prandtl number (Wikidata Q815306))" + }, + { + "q": "What is the kinematic viscosity for Prandtl number 156.502 and thermal diffusivity 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [156.50181, 185.886273] (the system Prandtl number (Wikidata Q815306))" + }, + { + "q": "Tell me the kinematic viscosity with Prandtl number of 156.502 and thermal diffusivity of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [156.50181, 185.886273] (the system Prandtl number (Wikidata Q815306))" + }, + { + "q": "What is the kinematic viscosity for Prandtl number 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the thermal diffusivity is not given" + }, + { + "q": "Tell me the kinematic viscosity with Prandtl number of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the thermal diffusivity is not given" + }, + { + "q": "What is the kinematic viscosity 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 kinematic viscosity 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 kinematic viscosity with Prandtl number of -1.91195 and thermal diffusivity of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Prandtl number: no example had one" + }, + { + "q": "Which kinematic viscosity 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 Prandtl number is 8.50728 and the thermal diffusivity is 17.7888, what is the kinematic viscosity?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [8.507283, 17.788778] (the system Prandtl number (Wikidata Q815306))" + }, + { + "q": "Could you work out the kinematic viscosity given Prandtl number = 8.50728, thermal diffusivity = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [8.507283, 17.788778] (the system Prandtl number (Wikidata Q815306))" + }, + { + "q": "I need the kinematic viscosity for a Prandtl number of 8.24951 and a thermal diffusivity of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [8.24951, 0.634021] (the system Prandtl number (Wikidata Q815306))" + }, + { + "q": "If the Prandtl number is 8.24951 and the thermal diffusivity is 0.634021, what is the kinematic viscosity?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [8.24951, 0.634021] (the system Prandtl number (Wikidata Q815306))" + }, + { + "q": "Could you work out the kinematic viscosity given Prandtl number = 0.318734, thermal diffusivity = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [0.318734, 9.566071] (the system Prandtl number (Wikidata Q815306))" + }, + { + "q": "I need the kinematic viscosity for a Prandtl number of 0.318734 and a thermal diffusivity of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [0.318734, 9.566071] (the system Prandtl number (Wikidata Q815306))" + }, + { + "q": "If the Prandtl number is 0.671697 and the thermal diffusivity is 14.2496, what is the kinematic viscosity?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [0.671697, 14.249569] (the system Prandtl number (Wikidata Q815306))" + }, + { + "q": "Could you work out the kinematic viscosity given Prandtl number = 0.671697, thermal diffusivity = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [0.671697, 14.249569] (the system Prandtl number (Wikidata Q815306))" + }, + { + "q": "I need the kinematic viscosity for a Prandtl number of 21.7931 and a thermal diffusivity of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [21.793126, 29.510609] (the system Prandtl number (Wikidata Q815306))" + }, + { + "q": "If the Prandtl number is 21.7931 and the thermal diffusivity is 29.5106, what is the kinematic viscosity?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [21.793126, 29.510609] (the system Prandtl number (Wikidata Q815306))" + }, + { + "q": "Could you work out the kinematic viscosity given Prandtl number = 153.15, thermal diffusivity = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [153.149965, 18.595315] (the system Prandtl number (Wikidata Q815306))" + }, + { + "q": "I need the kinematic viscosity for a Prandtl number of 153.15 and a thermal diffusivity of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [153.149965, 18.595315] (the system Prandtl number (Wikidata Q815306))" + }, + { + "q": "If the Prandtl number is 8.50728, what is the kinematic viscosity?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the thermal diffusivity is not given" + }, + { + "q": "Could you work out the kinematic viscosity given Prandtl number = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the thermal diffusivity is not given" + }, + { + "q": "Do you know the kinematic viscosity 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 kinematic viscosity 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 kinematic viscosity given Prandtl number = -0.318734, thermal diffusivity = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Prandtl number: no example had one" + }, + { + "q": "What is a typical kinematic viscosity?", + "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-kinematic-viscosity-7/program.py b/root/organs10/physics/specialists/solved-kinematic-viscosity-7/program.py new file mode 100644 index 0000000000000000000000000000000000000000..b4e57c8c5d48b13c257926442f772a195405c7e3 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity-7/program.py @@ -0,0 +1,7 @@ +"""kinematic viscosity from Prandtl number, thermal diffusivity (Prandtl number (Wikidata Q815306)) +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(prandtl_number, thermal_diffusivity): + return 1.0*prandtl_number*thermal_diffusivity diff --git a/root/organs10/physics/specialists/solved-kinematic-viscosity-7/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-kinematic-viscosity-7/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity-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-kinematic-viscosity/organ_formula_v2.py b/root/organs10/physics/specialists/solved-kinematic-viscosity/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity/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-kinematic-viscosity/panel.json b/root/organs10/physics/specialists/solved-kinematic-viscosity/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..b8f6d66b880d720c30693733dbf44bc86ddfec56 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the kinematic viscosity for boundary layer thickness 107.961, radius of curvature 59.4105, Görtler number 5.77717 and speed 1.89126?", + "a": "47.643343957786655", + "valid": true, + "kind": "compute", + "expect": 47.643343957786655, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [107.960666, 59.4105, 5.777165, 1.891256] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "Tell me the kinematic viscosity with boundary layer thickness of 107.961, radius of curvature of 59.4105, Görtler number of 5.77717 and speed of 1.89126.", + "a": "47.643343957786655", + "valid": true, + "kind": "compute", + "expect": 47.643343957786655, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [107.960666, 59.4105, 5.777165, 1.891256] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "What is the kinematic viscosity for boundary layer thickness 2.69428, radius of curvature 48.921, Görtler number 157.223 and speed 35.6412?", + "a": "0.1433353698519676", + "valid": true, + "kind": "compute", + "expect": 0.1433353698519676, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [2.69428, 48.920991, 157.22256, 35.641157] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "Tell me the kinematic viscosity with boundary layer thickness of 2.69428, radius of curvature of 48.921, Görtler number of 157.223 and speed of 35.6412.", + "a": "0.1433353698519676", + "valid": true, + "kind": "compute", + "expect": 0.1433353698519676, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [2.69428, 48.920991, 157.22256, 35.641157] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "What is the kinematic viscosity for boundary layer thickness 0.318734, radius of curvature 9.56607, Görtler number 126.709 and speed 1.70503?", + "a": "0.0007828890006436852", + "valid": true, + "kind": "compute", + "expect": 0.0007828890006436852, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [0.318734, 9.566071, 126.709252, 1.705032] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "Tell me the kinematic viscosity with boundary layer thickness of 0.318734, radius of curvature of 9.56607, Görtler number of 126.709 and speed of 1.70503.", + "a": "0.0007828890006436852", + "valid": true, + "kind": "compute", + "expect": 0.0007828890006436852, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [0.318734, 9.566071, 126.709252, 1.705032] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "What is the kinematic viscosity for boundary layer thickness 87.781, radius of curvature 13.2084, Görtler number 246.329 and speed 20.3966?", + "a": "18.73771893735698", + "valid": true, + "kind": "compute", + "expect": 18.73771893735698, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [87.781013, 13.208419, 246.329053, 20.396558] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "Tell me the kinematic viscosity with boundary layer thickness of 87.781, radius of curvature of 13.2084, Görtler number of 246.329 and speed of 20.3966.", + "a": "18.73771893735698", + "valid": true, + "kind": "compute", + "expect": 18.73771893735698, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [87.781013, 13.208419, 246.329053, 20.396558] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "What is the kinematic viscosity for boundary layer thickness 156.502, radius of curvature 185.886, Görtler number 13.2373 and speed 4.71941?", + "a": "51.19691146243576", + "valid": true, + "kind": "compute", + "expect": 51.19691146243576, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [156.50181, 185.886273, 13.237311, 4.719415] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "Tell me the kinematic viscosity with boundary layer thickness of 156.502, radius of curvature of 185.886, Görtler number of 13.2373 and speed of 4.71941.", + "a": "51.19691146243576", + "valid": true, + "kind": "compute", + "expect": 51.19691146243576, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [156.50181, 185.886273, 13.237311, 4.719415] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "What is the kinematic viscosity for boundary layer thickness 24.5721, radius of curvature 108.451, Görtler number 1.69476 and speed 49.4843?", + "a": "341.51317645074636", + "valid": true, + "kind": "compute", + "expect": 341.51317645074636, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [24.572086, 108.450792, 1.694755, 49.484326] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "Tell me the kinematic viscosity with boundary layer thickness of 24.5721, radius of curvature of 108.451, Görtler number of 1.69476 and speed of 49.4843.", + "a": "341.51317645074636", + "valid": true, + "kind": "compute", + "expect": 341.51317645074636, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [24.572086, 108.450792, 1.694755, 49.484326] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "What is the kinematic viscosity for boundary layer thickness 107.961, radius of curvature 59.4105 and Görtler number 5.77717?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Tell me the kinematic viscosity with boundary layer thickness of 2.69428, radius of curvature of 48.921 and Görtler number of 157.223.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "What is the kinematic viscosity 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 kinematic viscosity 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 kinematic viscosity with boundary layer thickness of -0.318734, radius of curvature of 9.56607, Görtler number of 126.709 and speed of 1.70503.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative boundary layer thickness: no example had one" + }, + { + "q": "Which kinematic viscosity 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 boundary layer thickness is 167.625, radius of curvature is 10.3291, Görtler number is 2.21571 and the speed is 58.5346, what is the kinematic viscosity?", + "a": "17839.257688064143", + "valid": true, + "kind": "compute", + "expect": 17839.257688064143, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [167.624948, 10.329054, 2.215713, 58.534594] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "Could you work out the kinematic viscosity given boundary layer thickness = 167.625, radius of curvature = 10.3291, Görtler number = 2.21571, speed = 58.5346?", + "a": "17839.257688064143", + "valid": true, + "kind": "compute", + "expect": 17839.257688064143, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [167.624948, 10.329054, 2.215713, 58.534594] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "I need the kinematic viscosity for a boundary layer thickness of 8.53158, a radius of curvature of 124.718, a Görtler number of 1.91195 and a speed of 82.2401, please.", + "a": "95.98108448485434", + "valid": true, + "kind": "compute", + "expect": 95.98108448485434, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [8.531577, 124.718185, 1.911952, 82.240072] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "If the boundary layer thickness is 8.53158, radius of curvature is 124.718, Görtler number is 1.91195 and the speed is 82.2401, what is the kinematic viscosity?", + "a": "95.98108448485434", + "valid": true, + "kind": "compute", + "expect": 95.98108448485434, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [8.531577, 124.718185, 1.911952, 82.240072] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "Could you work out the kinematic viscosity given boundary layer thickness = 81.1967, radius of curvature = 6.98086, Görtler number = 0.551236, speed = 2.88512?", + "a": "1449.3711520342174", + "valid": true, + "kind": "compute", + "expect": 1449.3711520342174, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [81.196705, 6.980857, 0.551236, 2.88512] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "I need the kinematic viscosity for a boundary layer thickness of 81.1967, a radius of curvature of 6.98086, a Görtler number of 0.551236 and a speed of 2.88512, please.", + "a": "1449.3711520342174", + "valid": true, + "kind": "compute", + "expect": 1449.3711520342174, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [81.196705, 6.980857, 0.551236, 2.88512] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "If the boundary layer thickness is 1.16997, radius of curvature is 1.14865, Görtler number is 21.7931 and the speed is 29.5106, what is the kinematic viscosity?", + "a": "1.5989147668236339", + "valid": true, + "kind": "compute", + "expect": 1.5989147668236339, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [1.169968, 1.148652, 21.793126, 29.510609] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "Could you work out the kinematic viscosity given boundary layer thickness = 1.16997, radius of curvature = 1.14865, Görtler number = 21.7931, speed = 29.5106?", + "a": "1.5989147668236339", + "valid": true, + "kind": "compute", + "expect": 1.5989147668236339, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [1.169968, 1.148652, 21.793126, 29.510609] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "I need the kinematic viscosity for a boundary layer thickness of 223.509, a radius of curvature of 17.3414, a Görtler number of 7.10705 and a speed of 30.2507, please.", + "a": "3415.436610753207", + "valid": true, + "kind": "compute", + "expect": 3415.436610753207, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [223.508796, 17.341435, 7.107049, 30.250749] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "If the boundary layer thickness is 223.509, radius of curvature is 17.3414, Görtler number is 7.10705 and the speed is 30.2507, what is the kinematic viscosity?", + "a": "3415.436610753207", + "valid": true, + "kind": "compute", + "expect": 3415.436610753207, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [223.508796, 17.341435, 7.107049, 30.250749] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "Could you work out the kinematic viscosity given boundary layer thickness = 0.869111, radius of curvature = 39.1753, Görtler number = 0.432217, speed = 16.6571?", + "a": "4.988881020355567", + "valid": true, + "kind": "compute", + "expect": 4.988881020355567, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [0.869111, 39.175263, 0.432217, 16.657051] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "I need the kinematic viscosity for a boundary layer thickness of 0.869111, a radius of curvature of 39.1753, a Görtler number of 0.432217 and a speed of 16.6571, please.", + "a": "4.988881020355567", + "valid": true, + "kind": "compute", + "expect": 4.988881020355567, + "dtype": "quantity", + "unit": "m^2/s", + "why": "kinematic viscosity for [0.869111, 39.175263, 0.432217, 16.657051] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "If the boundary layer thickness is 167.625, radius of curvature is 10.3291 and the Görtler number is 2.21571, what is the kinematic viscosity?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Could you work out the kinematic viscosity given boundary layer thickness = 8.53158, radius of curvature = 124.718, Görtler number = 1.91195?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Do you know the kinematic viscosity 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 kinematic viscosity 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 kinematic viscosity given boundary layer thickness = -81.1967, radius of curvature = 6.98086, Görtler number = 0.551236, speed = 2.88512?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative boundary layer thickness: no example had one" + }, + { + "q": "What is a typical kinematic viscosity?", + "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-kinematic-viscosity/program.py b/root/organs10/physics/specialists/solved-kinematic-viscosity/program.py new file mode 100644 index 0000000000000000000000000000000000000000..c92df041c8e2c155087ef7837f036ee3ce7808d4 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity/program.py @@ -0,0 +1,7 @@ +"""kinematic viscosity from boundary layer thickness, radius of curvature, Görtler number, speed (Görtler number (Wikidata Q102723670)) +The maths organ (systems.py): solved from the system q2 = ((q3*q0)/(q4))*sqrt(((q0)/(q1))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(boundary_layer_thickness, radius_of_curvature, g_rtler_number, speed): + return 1.0*boundary_layer_thickness**1.5*radius_of_curvature**(-0.5)*speed/g_rtler_number diff --git a/root/organs10/physics/specialists/solved-kinematic-viscosity/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-kinematic-viscosity/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinematic-viscosity/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-kinetic-energy-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-kinetic-energy-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinetic-energy-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-kinetic-energy-2/panel.json b/root/organs10/physics/specialists/solved-kinetic-energy-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..018275c6afb64f613b858ba03333fc801b1792ec --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinetic-energy-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the kinetic energy for mechanical energy 107.961 and potential energy 59.4105?", + "a": "48.55016569879068", + "valid": true, + "kind": "compute", + "expect": 48.55016569879068, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [107.960666, 59.4105] (the system mechanical energy (Wikidata Q184550))" + }, + { + "q": "Tell me the kinetic energy with mechanical energy of 107.961 and potential energy of 59.4105.", + "a": "48.55016569879068", + "valid": true, + "kind": "compute", + "expect": 48.55016569879068, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [107.960666, 59.4105] (the system mechanical energy (Wikidata Q184550))" + }, + { + "q": "What is the kinetic energy for mechanical energy 157.223 and potential energy 35.6412?", + "a": "121.5814030054458", + "valid": true, + "kind": "compute", + "expect": 121.5814030054458, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [157.22256, 35.641157] (the system mechanical energy (Wikidata Q184550))" + }, + { + "q": "Tell me the kinetic energy with mechanical energy of 157.223 and potential energy of 35.6412.", + "a": "121.5814030054458", + "valid": true, + "kind": "compute", + "expect": 121.5814030054458, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [157.22256, 35.641157] (the system mechanical energy (Wikidata Q184550))" + }, + { + "q": "What is the kinetic energy for mechanical energy 126.709 and potential energy 1.70503?", + "a": "125.00421942160824", + "valid": true, + "kind": "compute", + "expect": 125.00421942160824, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [126.709252, 1.705032] (the system mechanical energy (Wikidata Q184550))" + }, + { + "q": "Tell me the kinetic energy with mechanical energy of 126.709 and potential energy of 1.70503.", + "a": "125.00421942160824", + "valid": true, + "kind": "compute", + "expect": 125.00421942160824, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [126.709252, 1.705032] (the system mechanical energy (Wikidata Q184550))" + }, + { + "q": "What is the kinetic energy for mechanical energy 41.6566 and potential energy 13.8779?", + "a": "27.77877186905885", + "valid": true, + "kind": "compute", + "expect": 27.77877186905885, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [41.656639, 13.877867] (the system mechanical energy (Wikidata Q184550))" + }, + { + "q": "Tell me the kinetic energy with mechanical energy of 41.6566 and potential energy of 13.8779.", + "a": "27.77877186905885", + "valid": true, + "kind": "compute", + "expect": 27.77877186905885, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [41.656639, 13.877867] (the system mechanical energy (Wikidata Q184550))" + }, + { + "q": "What is the kinetic energy for mechanical energy 8.50341 and potential energy 0.58813?", + "a": "7.91527752848457", + "valid": true, + "kind": "compute", + "expect": 7.91527752848457, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [8.503407, 0.58813] (the system mechanical energy (Wikidata Q184550))" + }, + { + "q": "Tell me the kinetic energy with mechanical energy of 8.50341 and potential energy of 0.58813.", + "a": "7.91527752848457", + "valid": true, + "kind": "compute", + "expect": 7.91527752848457, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [8.503407, 0.58813] (the system mechanical energy (Wikidata Q184550))" + }, + { + "q": "What is the kinetic energy for mechanical energy 223.509 and potential energy 17.3414?", + "a": "206.1673613150397", + "valid": true, + "kind": "compute", + "expect": 206.1673613150397, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [223.508796, 17.341435] (the system mechanical energy (Wikidata Q184550))" + }, + { + "q": "Tell me the kinetic energy with mechanical energy of 223.509 and potential energy of 17.3414.", + "a": "206.1673613150397", + "valid": true, + "kind": "compute", + "expect": 206.1673613150397, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [223.508796, 17.341435] (the system mechanical energy (Wikidata Q184550))" + }, + { + "q": "What is the kinetic energy for mechanical energy 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the potential energy is not given" + }, + { + "q": "Tell me the kinetic energy with mechanical energy of 157.223.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the potential energy is not given" + }, + { + "q": "What is the kinetic energy 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 kinetic energy 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 kinetic energy with mechanical energy of -126.709 and potential energy of 1.70503.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mechanical energy: no example had one" + }, + { + "q": "Which kinetic energy 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 mechanical energy is 167.625 and the potential energy is 10.3291, what is the kinetic energy?", + "a": "157.29589332353083", + "valid": true, + "kind": "compute", + "expect": 157.29589332353083, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [167.624948, 10.329054] (the system mechanical energy (Wikidata Q184550))" + }, + { + "q": "Could you work out the kinetic energy given mechanical energy = 167.625, potential energy = 10.3291?", + "a": "157.29589332353083", + "valid": true, + "kind": "compute", + "expect": 157.29589332353083, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [167.624948, 10.329054] (the system mechanical energy (Wikidata Q184550))" + }, + { + "q": "I need the kinetic energy for a mechanical energy of 13.999 and a potential energy of 0.348438, please.", + "a": "13.650591275493905", + "valid": true, + "kind": "compute", + "expect": 13.650591275493905, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [13.999029, 0.348438] (the system mechanical energy (Wikidata Q184550))" + }, + { + "q": "If the mechanical energy is 13.999 and the potential energy is 0.348438, what is the kinetic energy?", + "a": "13.650591275493905", + "valid": true, + "kind": "compute", + "expect": 13.650591275493905, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [13.999029, 0.348438] (the system mechanical energy (Wikidata Q184550))" + }, + { + "q": "Could you work out the kinetic energy given mechanical energy = 81.1967, potential energy = 6.98086?", + "a": "74.21584883614992", + "valid": true, + "kind": "compute", + "expect": 74.21584883614992, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [81.196705, 6.980857] (the system mechanical energy (Wikidata Q184550))" + }, + { + "q": "I need the kinetic energy for a mechanical energy of 81.1967 and a potential energy of 6.98086, please.", + "a": "74.21584883614992", + "valid": true, + "kind": "compute", + "expect": 74.21584883614992, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [81.196705, 6.980857] (the system mechanical energy (Wikidata Q184550))" + }, + { + "q": "If the mechanical energy is 19.4474 and the potential energy is 4.51548, what is the kinetic energy?", + "a": "14.931881955429075", + "valid": true, + "kind": "compute", + "expect": 14.931881955429075, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [19.447357, 4.515475] (the system mechanical energy (Wikidata Q184550))" + }, + { + "q": "Could you work out the kinetic energy given mechanical energy = 19.4474, potential energy = 4.51548?", + "a": "14.931881955429075", + "valid": true, + "kind": "compute", + "expect": 14.931881955429075, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [19.447357, 4.515475] (the system mechanical energy (Wikidata Q184550))" + }, + { + "q": "I need the kinetic energy for a mechanical energy of 13.2373 and a potential energy of 4.71941, please.", + "a": "8.517896269006052", + "valid": true, + "kind": "compute", + "expect": 8.517896269006052, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [13.237311, 4.719415] (the system mechanical energy (Wikidata Q184550))" + }, + { + "q": "If the mechanical energy is 13.2373 and the potential energy is 4.71941, what is the kinetic energy?", + "a": "8.517896269006052", + "valid": true, + "kind": "compute", + "expect": 8.517896269006052, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [13.237311, 4.719415] (the system mechanical energy (Wikidata Q184550))" + }, + { + "q": "Could you work out the kinetic energy given mechanical energy = 21.7946, potential energy = 9.1061?", + "a": "12.68851669739933", + "valid": true, + "kind": "compute", + "expect": 12.68851669739933, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [21.794621, 9.106104] (the system mechanical energy (Wikidata Q184550))" + }, + { + "q": "I need the kinetic energy for a mechanical energy of 21.7946 and a potential energy of 9.1061, please.", + "a": "12.68851669739933", + "valid": true, + "kind": "compute", + "expect": 12.68851669739933, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [21.794621, 9.106104] (the system mechanical energy (Wikidata Q184550))" + }, + { + "q": "If the mechanical energy is 167.625, what is the kinetic energy?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the potential energy is not given" + }, + { + "q": "Could you work out the kinetic energy given mechanical energy = 13.999?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the potential energy is not given" + }, + { + "q": "Do you know the kinetic energy 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 kinetic energy 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 kinetic energy given mechanical energy = -81.1967, potential energy = 6.98086?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mechanical energy: no example had one" + }, + { + "q": "What is a typical kinetic energy?", + "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-kinetic-energy-2/program.py b/root/organs10/physics/specialists/solved-kinetic-energy-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..d4b34a57548333a7d4d63be727b9e0a227689bcb --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinetic-energy-2/program.py @@ -0,0 +1,7 @@ +"""kinetic energy from mechanical energy, potential energy (mechanical energy (Wikidata Q184550)) +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(mechanical_energy, potential_energy): + return 1.0*mechanical_energy - 1.0*potential_energy diff --git a/root/organs10/physics/specialists/solved-kinetic-energy-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-kinetic-energy-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinetic-energy-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-kinetic-energy-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-kinetic-energy-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinetic-energy-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-kinetic-energy-3/panel.json b/root/organs10/physics/specialists/solved-kinetic-energy-3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..1be2c921c8f4fd4666530e2a4154d9bd2ee6ba1f --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinetic-energy-3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the kinetic energy for mass 107.961 and speed 59.4105?", + "a": "190529.38979457287", + "valid": true, + "kind": "compute", + "expect": 190529.38979457287, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [107.960666, 59.4105] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "Tell me the kinetic energy with mass of 107.961 and speed of 59.4105.", + "a": "190529.38979457287", + "valid": true, + "kind": "compute", + "expect": 190529.38979457287, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [107.960666, 59.4105] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "What is the kinetic energy for mass 169.527 and speed 280.774?", + "a": "6682233.21028727", + "valid": true, + "kind": "compute", + "expect": 6682233.21028727, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [169.526958, 280.773702] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "Tell me the kinetic energy with mass of 169.527 and speed of 280.774.", + "a": "6682233.21028727", + "valid": true, + "kind": "compute", + "expect": 6682233.21028727, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [169.526958, 280.773702] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "What is the kinetic energy for mass 1.91195 and speed 82.2401?", + "a": "6465.675398533168", + "valid": true, + "kind": "compute", + "expect": 6465.675398533168, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [1.911952, 82.240072] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "Tell me the kinetic energy with mass of 1.91195 and speed of 82.2401.", + "a": "6465.675398533168", + "valid": true, + "kind": "compute", + "expect": 6465.675398533168, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [1.911952, 82.240072] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "What is the kinetic energy for mass 1.64379 and speed 252.709?", + "a": "52487.91143204169", + "valid": true, + "kind": "compute", + "expect": 52487.91143204169, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [1.643795, 252.709024] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "Tell me the kinetic energy with mass of 1.64379 and speed of 252.709.", + "a": "52487.91143204169", + "valid": true, + "kind": "compute", + "expect": 52487.91143204169, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [1.643795, 252.709024] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "What is the kinetic energy for mass 18.3169 and speed 6.8386?", + "a": "428.30717969297405", + "valid": true, + "kind": "compute", + "expect": 428.30717969297405, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [18.316867, 6.838598] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "Tell me the kinetic energy with mass of 18.3169 and speed of 6.8386.", + "a": "428.30717969297405", + "valid": true, + "kind": "compute", + "expect": 428.30717969297405, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [18.316867, 6.838598] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "What is the kinetic energy for mass 156.502 and speed 185.886?", + "a": "2703858.794076354", + "valid": true, + "kind": "compute", + "expect": 2703858.794076354, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [156.50181, 185.886273] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "Tell me the kinetic energy with mass of 156.502 and speed of 185.886.", + "a": "2703858.794076354", + "valid": true, + "kind": "compute", + "expect": 2703858.794076354, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [156.50181, 185.886273] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "What is the kinetic energy for mass 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Tell me the kinetic energy with mass of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "What is the kinetic energy 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 kinetic energy 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 kinetic energy with mass of -1.91195 and speed 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 kinetic energy 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 speed is 17.7888, what is the kinetic energy?", + "a": "1346.0249297051848", + "valid": true, + "kind": "compute", + "expect": 1346.0249297051848, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [8.507283, 17.788778] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "Could you work out the kinetic energy given mass = 8.50728, speed = 17.7888?", + "a": "1346.0249297051848", + "valid": true, + "kind": "compute", + "expect": 1346.0249297051848, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [8.507283, 17.788778] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "I need the kinetic energy for a mass of 8.24951 and a speed of 0.634021, please.", + "a": "1.6580796880570197", + "valid": true, + "kind": "compute", + "expect": 1.6580796880570197, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [8.24951, 0.634021] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "If the mass is 8.24951 and the speed is 0.634021, what is the kinetic energy?", + "a": "1.6580796880570197", + "valid": true, + "kind": "compute", + "expect": 1.6580796880570197, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [8.24951, 0.634021] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "Could you work out the kinetic energy given mass = 0.318734, speed = 9.56607?", + "a": "14.583630796916376", + "valid": true, + "kind": "compute", + "expect": 14.583630796916376, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [0.318734, 9.566071] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "I need the kinetic energy for a mass of 0.318734 and a speed of 9.56607, please.", + "a": "14.583630796916376", + "valid": true, + "kind": "compute", + "expect": 14.583630796916376, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [0.318734, 9.566071] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "If the mass is 0.671697 and the speed is 14.2496, what is the kinetic energy?", + "a": "68.19413400667608", + "valid": true, + "kind": "compute", + "expect": 68.19413400667608, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [0.671697, 14.249569] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "Could you work out the kinetic energy given mass = 0.671697, speed = 14.2496?", + "a": "68.19413400667608", + "valid": true, + "kind": "compute", + "expect": 68.19413400667608, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [0.671697, 14.249569] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "I need the kinetic energy for a mass of 21.7931 and a speed of 29.5106, please.", + "a": "9489.55566937213", + "valid": true, + "kind": "compute", + "expect": 9489.55566937213, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [21.793126, 29.510609] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "If the mass is 21.7931 and the speed is 29.5106, what is the kinetic energy?", + "a": "9489.55566937213", + "valid": true, + "kind": "compute", + "expect": 9489.55566937213, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [21.793126, 29.510609] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "Could you work out the kinetic energy given mass = 153.15, speed = 18.5953?", + "a": "26478.538349874543", + "valid": true, + "kind": "compute", + "expect": 26478.538349874543, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [153.149965, 18.595315] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "I need the kinetic energy for a mass of 153.15 and a speed of 18.5953, please.", + "a": "26478.538349874543", + "valid": true, + "kind": "compute", + "expect": 26478.538349874543, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [153.149965, 18.595315] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "If the mass is 8.50728, what is the kinetic energy?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Could you work out the kinetic energy given mass = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Do you know the kinetic energy 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 kinetic energy 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 kinetic energy given mass = -0.318734, speed = 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 kinetic energy?", + "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-kinetic-energy-3/program.py b/root/organs10/physics/specialists/solved-kinetic-energy-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..7e40c859218077b731fa3c3e2e2eaa8236c6956b --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinetic-energy-3/program.py @@ -0,0 +1,7 @@ +"""kinetic energy from mass, speed (kinetic energy (Wikidata Q46276)) +The maths organ (systems.py): solved from the system q0 = ((1)/(2))*q1*q2 **(2); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(mass, speed): + return 0.5*mass*speed**2 diff --git a/root/organs10/physics/specialists/solved-kinetic-energy-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-kinetic-energy-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinetic-energy-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-kinetic-energy/organ_formula_v2.py b/root/organs10/physics/specialists/solved-kinetic-energy/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinetic-energy/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-kinetic-energy/panel.json b/root/organs10/physics/specialists/solved-kinetic-energy/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..7262052138c1407a83df413880465a08246c497f --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinetic-energy/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the kinetic energy for mass 107.961 and speed 59.4105?", + "a": "190529.38979457287", + "valid": true, + "kind": "compute", + "expect": 190529.38979457287, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [107.960666, 59.4105] (the system Kinetic energy)" + }, + { + "q": "Tell me the kinetic energy with mass of 107.961 and speed of 59.4105.", + "a": "190529.38979457287", + "valid": true, + "kind": "compute", + "expect": 190529.38979457287, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [107.960666, 59.4105] (the system Kinetic energy)" + }, + { + "q": "What is the kinetic energy for mass 169.527 and speed 280.774?", + "a": "6682233.21028727", + "valid": true, + "kind": "compute", + "expect": 6682233.21028727, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [169.526958, 280.773702] (the system Kinetic energy)" + }, + { + "q": "Tell me the kinetic energy with mass of 169.527 and speed of 280.774.", + "a": "6682233.21028727", + "valid": true, + "kind": "compute", + "expect": 6682233.21028727, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [169.526958, 280.773702] (the system Kinetic energy)" + }, + { + "q": "What is the kinetic energy for mass 1.91195 and speed 82.2401?", + "a": "6465.675398533168", + "valid": true, + "kind": "compute", + "expect": 6465.675398533168, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [1.911952, 82.240072] (the system Kinetic energy)" + }, + { + "q": "Tell me the kinetic energy with mass of 1.91195 and speed of 82.2401.", + "a": "6465.675398533168", + "valid": true, + "kind": "compute", + "expect": 6465.675398533168, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [1.911952, 82.240072] (the system Kinetic energy)" + }, + { + "q": "What is the kinetic energy for mass 1.64379 and speed 252.709?", + "a": "52487.91143204169", + "valid": true, + "kind": "compute", + "expect": 52487.91143204169, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [1.643795, 252.709024] (the system Kinetic energy)" + }, + { + "q": "Tell me the kinetic energy with mass of 1.64379 and speed of 252.709.", + "a": "52487.91143204169", + "valid": true, + "kind": "compute", + "expect": 52487.91143204169, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [1.643795, 252.709024] (the system Kinetic energy)" + }, + { + "q": "What is the kinetic energy for mass 18.3169 and speed 6.8386?", + "a": "428.30717969297405", + "valid": true, + "kind": "compute", + "expect": 428.30717969297405, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [18.316867, 6.838598] (the system Kinetic energy)" + }, + { + "q": "Tell me the kinetic energy with mass of 18.3169 and speed of 6.8386.", + "a": "428.30717969297405", + "valid": true, + "kind": "compute", + "expect": 428.30717969297405, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [18.316867, 6.838598] (the system Kinetic energy)" + }, + { + "q": "What is the kinetic energy for mass 156.502 and speed 185.886?", + "a": "2703858.794076354", + "valid": true, + "kind": "compute", + "expect": 2703858.794076354, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [156.50181, 185.886273] (the system Kinetic energy)" + }, + { + "q": "Tell me the kinetic energy with mass of 156.502 and speed of 185.886.", + "a": "2703858.794076354", + "valid": true, + "kind": "compute", + "expect": 2703858.794076354, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [156.50181, 185.886273] (the system Kinetic energy)" + }, + { + "q": "What is the kinetic energy for mass 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Tell me the kinetic energy with mass of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "What is the kinetic energy 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 kinetic energy 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 kinetic energy with mass of -1.91195 and speed 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 kinetic energy 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 speed is 17.7888, what is the kinetic energy?", + "a": "1346.0249297051848", + "valid": true, + "kind": "compute", + "expect": 1346.0249297051848, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [8.507283, 17.788778] (the system Kinetic energy)" + }, + { + "q": "Could you work out the kinetic energy given mass = 8.50728, speed = 17.7888?", + "a": "1346.0249297051848", + "valid": true, + "kind": "compute", + "expect": 1346.0249297051848, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [8.507283, 17.788778] (the system Kinetic energy)" + }, + { + "q": "I need the kinetic energy for a mass of 8.24951 and a speed of 0.634021, please.", + "a": "1.6580796880570197", + "valid": true, + "kind": "compute", + "expect": 1.6580796880570197, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [8.24951, 0.634021] (the system Kinetic energy)" + }, + { + "q": "If the mass is 8.24951 and the speed is 0.634021, what is the kinetic energy?", + "a": "1.6580796880570197", + "valid": true, + "kind": "compute", + "expect": 1.6580796880570197, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [8.24951, 0.634021] (the system Kinetic energy)" + }, + { + "q": "Could you work out the kinetic energy given mass = 0.318734, speed = 9.56607?", + "a": "14.583630796916376", + "valid": true, + "kind": "compute", + "expect": 14.583630796916376, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [0.318734, 9.566071] (the system Kinetic energy)" + }, + { + "q": "I need the kinetic energy for a mass of 0.318734 and a speed of 9.56607, please.", + "a": "14.583630796916376", + "valid": true, + "kind": "compute", + "expect": 14.583630796916376, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [0.318734, 9.566071] (the system Kinetic energy)" + }, + { + "q": "If the mass is 0.671697 and the speed is 14.2496, what is the kinetic energy?", + "a": "68.19413400667608", + "valid": true, + "kind": "compute", + "expect": 68.19413400667608, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [0.671697, 14.249569] (the system Kinetic energy)" + }, + { + "q": "Could you work out the kinetic energy given mass = 0.671697, speed = 14.2496?", + "a": "68.19413400667608", + "valid": true, + "kind": "compute", + "expect": 68.19413400667608, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [0.671697, 14.249569] (the system Kinetic energy)" + }, + { + "q": "I need the kinetic energy for a mass of 21.7931 and a speed of 29.5106, please.", + "a": "9489.55566937213", + "valid": true, + "kind": "compute", + "expect": 9489.55566937213, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [21.793126, 29.510609] (the system Kinetic energy)" + }, + { + "q": "If the mass is 21.7931 and the speed is 29.5106, what is the kinetic energy?", + "a": "9489.55566937213", + "valid": true, + "kind": "compute", + "expect": 9489.55566937213, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [21.793126, 29.510609] (the system Kinetic energy)" + }, + { + "q": "Could you work out the kinetic energy given mass = 153.15, speed = 18.5953?", + "a": "26478.538349874543", + "valid": true, + "kind": "compute", + "expect": 26478.538349874543, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [153.149965, 18.595315] (the system Kinetic energy)" + }, + { + "q": "I need the kinetic energy for a mass of 153.15 and a speed of 18.5953, please.", + "a": "26478.538349874543", + "valid": true, + "kind": "compute", + "expect": 26478.538349874543, + "dtype": "quantity", + "unit": "J", + "why": "kinetic energy for [153.149965, 18.595315] (the system Kinetic energy)" + }, + { + "q": "If the mass is 8.50728, what is the kinetic energy?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Could you work out the kinetic energy given mass = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Do you know the kinetic energy 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 kinetic energy 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 kinetic energy given mass = -0.318734, speed = 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 kinetic energy?", + "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-kinetic-energy/program.py b/root/organs10/physics/specialists/solved-kinetic-energy/program.py new file mode 100644 index 0000000000000000000000000000000000000000..b1444c03feda80cbdb24e4d025a9f7843a0d72a5 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinetic-energy/program.py @@ -0,0 +1,7 @@ +"""kinetic energy from mass, speed (Kinetic energy) +The maths organ (systems.py): solved from the system E = m*v**2/2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(mass, speed): + return 0.5*mass*speed**2 diff --git a/root/organs10/physics/specialists/solved-kinetic-energy/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-kinetic-energy/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-kinetic-energy/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-knudsen-number/organ_formula_v2.py b/root/organs10/physics/specialists/solved-knudsen-number/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-knudsen-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-knudsen-number/panel.json b/root/organs10/physics/specialists/solved-knudsen-number/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..d8fe8772d94bdf50a918bbb2c7ce40803672e5d8 --- /dev/null +++ b/root/organs10/physics/specialists/solved-knudsen-number/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the Knudsen number for mean free path 107.961 and characteristic length 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "Knudsen number for [107.960666, 59.4105] (the system Knudsen number (Wikidata Q898463))" + }, + { + "q": "Tell me the Knudsen number with mean free path of 107.961 and characteristic length of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "Knudsen number for [107.960666, 59.4105] (the system Knudsen number (Wikidata Q898463))" + }, + { + "q": "What is the Knudsen number for mean free path 169.527 and characteristic length 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "Knudsen number for [169.526958, 280.773702] (the system Knudsen number (Wikidata Q898463))" + }, + { + "q": "Tell me the Knudsen number with mean free path of 169.527 and characteristic length of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "Knudsen number for [169.526958, 280.773702] (the system Knudsen number (Wikidata Q898463))" + }, + { + "q": "What is the Knudsen number for mean free path 1.91195 and characteristic length 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "Knudsen number for [1.911952, 82.240072] (the system Knudsen number (Wikidata Q898463))" + }, + { + "q": "Tell me the Knudsen number with mean free path of 1.91195 and characteristic length of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "Knudsen number for [1.911952, 82.240072] (the system Knudsen number (Wikidata Q898463))" + }, + { + "q": "What is the Knudsen number for mean free path 1.64379 and characteristic length 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "Knudsen number for [1.643795, 252.709024] (the system Knudsen number (Wikidata Q898463))" + }, + { + "q": "Tell me the Knudsen number with mean free path of 1.64379 and characteristic length of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "Knudsen number for [1.643795, 252.709024] (the system Knudsen number (Wikidata Q898463))" + }, + { + "q": "What is the Knudsen number for mean free path 18.3169 and characteristic length 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "Knudsen number for [18.316867, 6.838598] (the system Knudsen number (Wikidata Q898463))" + }, + { + "q": "Tell me the Knudsen number with mean free path of 18.3169 and characteristic length of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "Knudsen number for [18.316867, 6.838598] (the system Knudsen number (Wikidata Q898463))" + }, + { + "q": "What is the Knudsen number for mean free path 156.502 and characteristic length 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "Knudsen number for [156.50181, 185.886273] (the system Knudsen number (Wikidata Q898463))" + }, + { + "q": "Tell me the Knudsen number with mean free path of 156.502 and characteristic length of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "Knudsen number for [156.50181, 185.886273] (the system Knudsen number (Wikidata Q898463))" + }, + { + "q": "What is the Knudsen number for mean free path 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the characteristic length is not given" + }, + { + "q": "Tell me the Knudsen number with mean free path 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 Knudsen 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 Knudsen 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 Knudsen number with mean free path of -1.91195 and characteristic length of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mean free path: no example had one" + }, + { + "q": "Which Knudsen 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 mean free path is 8.50728 and the characteristic length is 17.7888, what is the Knudsen number?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "Knudsen number for [8.507283, 17.788778] (the system Knudsen number (Wikidata Q898463))" + }, + { + "q": "Could you work out the Knudsen number given mean free path = 8.50728, characteristic length = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "Knudsen number for [8.507283, 17.788778] (the system Knudsen number (Wikidata Q898463))" + }, + { + "q": "I need the Knudsen number for a mean free path of 8.24951 and a characteristic length of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "Knudsen number for [8.24951, 0.634021] (the system Knudsen number (Wikidata Q898463))" + }, + { + "q": "If the mean free path is 8.24951 and the characteristic length is 0.634021, what is the Knudsen number?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "Knudsen number for [8.24951, 0.634021] (the system Knudsen number (Wikidata Q898463))" + }, + { + "q": "Could you work out the Knudsen number given mean free path = 0.318734, characteristic length = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "Knudsen number for [0.318734, 9.566071] (the system Knudsen number (Wikidata Q898463))" + }, + { + "q": "I need the Knudsen number for a mean free path of 0.318734 and a characteristic length of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "Knudsen number for [0.318734, 9.566071] (the system Knudsen number (Wikidata Q898463))" + }, + { + "q": "If the mean free path is 0.671697 and the characteristic length is 14.2496, what is the Knudsen number?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "Knudsen number for [0.671697, 14.249569] (the system Knudsen number (Wikidata Q898463))" + }, + { + "q": "Could you work out the Knudsen number given mean free path = 0.671697, characteristic length = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "Knudsen number for [0.671697, 14.249569] (the system Knudsen number (Wikidata Q898463))" + }, + { + "q": "I need the Knudsen number for a mean free path of 21.7931 and a characteristic length of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "Knudsen number for [21.793126, 29.510609] (the system Knudsen number (Wikidata Q898463))" + }, + { + "q": "If the mean free path is 21.7931 and the characteristic length is 29.5106, what is the Knudsen number?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "Knudsen number for [21.793126, 29.510609] (the system Knudsen number (Wikidata Q898463))" + }, + { + "q": "Could you work out the Knudsen number given mean free path = 153.15, characteristic length = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "Knudsen number for [153.149965, 18.595315] (the system Knudsen number (Wikidata Q898463))" + }, + { + "q": "I need the Knudsen number for a mean free path of 153.15 and a characteristic length of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "Knudsen number for [153.149965, 18.595315] (the system Knudsen number (Wikidata Q898463))" + }, + { + "q": "If the mean free path is 8.50728, what is the Knudsen 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 Knudsen number given mean free path = 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 Knudsen 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 Knudsen 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 Knudsen number given mean free path = -0.318734, characteristic length = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mean free path: no example had one" + }, + { + "q": "What is a typical Knudsen 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-knudsen-number/program.py b/root/organs10/physics/specialists/solved-knudsen-number/program.py new file mode 100644 index 0000000000000000000000000000000000000000..14259dd95149d53e0e7225e4de5c38336d88dc4b --- /dev/null +++ b/root/organs10/physics/specialists/solved-knudsen-number/program.py @@ -0,0 +1,7 @@ +"""Knudsen number from mean free path, characteristic length (Knudsen number (Wikidata Q898463)) +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(mean_free_path, characteristic_length): + return 1.0*mean_free_path/characteristic_length diff --git a/root/organs10/physics/specialists/solved-knudsen-number/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-knudsen-number/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-knudsen-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-land-factor/organ_formula_v2.py b/root/organs10/physics/specialists/solved-land-factor/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-land-factor/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-land-factor/panel.json b/root/organs10/physics/specialists/solved-land-factor/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..a86471602ce06a1c16d8002d1a5451aa7a841e54 --- /dev/null +++ b/root/organs10/physics/specialists/solved-land-factor/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the Landé factor for magnetic dipole moment 107.961 and total angular momentum quantum number 59.4105?", + "a": "1.9594526931885098e+23", + "valid": true, + "kind": "compute", + "expect": 1.9594526931885098e+23, + "dtype": "quantity", + "unit": "1", + "why": "Landé factor for [107.960666, 59.4105] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "Tell me the Landé factor with magnetic dipole moment of 107.961 and total angular momentum quantum number of 59.4105.", + "a": "1.9594526931885098e+23", + "valid": true, + "kind": "compute", + "expect": 1.9594526931885098e+23, + "dtype": "quantity", + "unit": "1", + "why": "Landé factor for [107.960666, 59.4105] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "What is the Landé factor for magnetic dipole moment 169.527 and total angular momentum quantum number 280.774?", + "a": "6.5105065004630335e+22", + "valid": true, + "kind": "compute", + "expect": 6.5105065004630335e+22, + "dtype": "quantity", + "unit": "1", + "why": "Landé factor for [169.526958, 280.773702] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "Tell me the Landé factor with magnetic dipole moment of 169.527 and total angular momentum quantum number of 280.774.", + "a": "6.5105065004630335e+22", + "valid": true, + "kind": "compute", + "expect": 6.5105065004630335e+22, + "dtype": "quantity", + "unit": "1", + "why": "Landé factor for [169.526958, 280.773702] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "What is the Landé factor for magnetic dipole moment 1.91195 and total angular momentum quantum number 82.2401?", + "a": "2.5068357942134256e+21", + "valid": true, + "kind": "compute", + "expect": 2.5068357942134256e+21, + "dtype": "quantity", + "unit": "1", + "why": "Landé factor for [1.911952, 82.240072] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "Tell me the Landé factor with magnetic dipole moment of 1.91195 and total angular momentum quantum number of 82.2401.", + "a": "2.5068357942134256e+21", + "valid": true, + "kind": "compute", + "expect": 2.5068357942134256e+21, + "dtype": "quantity", + "unit": "1", + "why": "Landé factor for [1.911952, 82.240072] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "What is the Landé factor for magnetic dipole moment 1.64379 and total angular momentum quantum number 252.709?", + "a": "7.013898423239804e+20", + "valid": true, + "kind": "compute", + "expect": 7.013898423239804e+20, + "dtype": "quantity", + "unit": "1", + "why": "Landé factor for [1.643795, 252.709024] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "Tell me the Landé factor with magnetic dipole moment of 1.64379 and total angular momentum quantum number of 252.709.", + "a": "7.013898423239804e+20", + "valid": true, + "kind": "compute", + "expect": 7.013898423239804e+20, + "dtype": "quantity", + "unit": "1", + "why": "Landé factor for [1.643795, 252.709024] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "What is the Landé factor for magnetic dipole moment 18.3169 and total angular momentum quantum number 6.8386?", + "a": "2.8881287567290528e+23", + "valid": true, + "kind": "compute", + "expect": 2.8881287567290528e+23, + "dtype": "quantity", + "unit": "1", + "why": "Landé factor for [18.316867, 6.838598] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "Tell me the Landé factor with magnetic dipole moment of 18.3169 and total angular momentum quantum number of 6.8386.", + "a": "2.8881287567290528e+23", + "valid": true, + "kind": "compute", + "expect": 2.8881287567290528e+23, + "dtype": "quantity", + "unit": "1", + "why": "Landé factor for [18.316867, 6.838598] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "What is the Landé factor for magnetic dipole moment 156.502 and total angular momentum quantum number 185.886?", + "a": "9.078298984438388e+22", + "valid": true, + "kind": "compute", + "expect": 9.078298984438388e+22, + "dtype": "quantity", + "unit": "1", + "why": "Landé factor for [156.50181, 185.886273] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "Tell me the Landé factor with magnetic dipole moment of 156.502 and total angular momentum quantum number of 185.886.", + "a": "9.078298984438388e+22", + "valid": true, + "kind": "compute", + "expect": 9.078298984438388e+22, + "dtype": "quantity", + "unit": "1", + "why": "Landé factor for [156.50181, 185.886273] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "What is the Landé factor for magnetic dipole moment 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total angular momentum quantum number is not given" + }, + { + "q": "Tell me the Landé factor with magnetic dipole moment of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total angular momentum quantum number is not given" + }, + { + "q": "What is the Landé factor 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 Landé factor 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 Landé factor with magnetic dipole moment of -1.91195 and total angular momentum quantum number of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative magnetic dipole moment: no example had one" + }, + { + "q": "Which Landé factor 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 magnetic dipole moment is 8.50728 and the total angular momentum quantum number is 17.7888, what is the Landé factor?", + "a": "5.156763107645098e+22", + "valid": true, + "kind": "compute", + "expect": 5.156763107645098e+22, + "dtype": "quantity", + "unit": "1", + "why": "Landé factor for [8.507283, 17.788778] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "Could you work out the Landé factor given magnetic dipole moment = 8.50728, total angular momentum quantum number = 17.7888?", + "a": "5.156763107645098e+22", + "valid": true, + "kind": "compute", + "expect": 5.156763107645098e+22, + "dtype": "quantity", + "unit": "1", + "why": "Landé factor for [8.507283, 17.788778] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "I need the Landé factor for a magnetic dipole moment of 8.24951 and a total angular momentum quantum number of 0.634021, please.", + "a": "1.4029977004997686e+24", + "valid": true, + "kind": "compute", + "expect": 1.4029977004997686e+24, + "dtype": "quantity", + "unit": "1", + "why": "Landé factor for [8.24951, 0.634021] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "If the magnetic dipole moment is 8.24951 and the total angular momentum quantum number is 0.634021, what is the Landé factor?", + "a": "1.4029977004997686e+24", + "valid": true, + "kind": "compute", + "expect": 1.4029977004997686e+24, + "dtype": "quantity", + "unit": "1", + "why": "Landé factor for [8.24951, 0.634021] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "Could you work out the Landé factor given magnetic dipole moment = 0.318734, total angular momentum quantum number = 9.56607?", + "a": "3.592752461613454e+21", + "valid": true, + "kind": "compute", + "expect": 3.592752461613454e+21, + "dtype": "quantity", + "unit": "1", + "why": "Landé factor for [0.318734, 9.566071] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "I need the Landé factor for a magnetic dipole moment of 0.318734 and a total angular momentum quantum number of 9.56607, please.", + "a": "3.592752461613454e+21", + "valid": true, + "kind": "compute", + "expect": 3.592752461613454e+21, + "dtype": "quantity", + "unit": "1", + "why": "Landé factor for [0.318734, 9.566071] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "If the magnetic dipole moment is 0.671697 and the total angular momentum quantum number is 14.2496, what is the Landé factor?", + "a": "5.082814223882805e+21", + "valid": true, + "kind": "compute", + "expect": 5.082814223882805e+21, + "dtype": "quantity", + "unit": "1", + "why": "Landé factor for [0.671697, 14.249569] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "Could you work out the Landé factor given magnetic dipole moment = 0.671697, total angular momentum quantum number = 14.2496?", + "a": "5.082814223882805e+21", + "valid": true, + "kind": "compute", + "expect": 5.082814223882805e+21, + "dtype": "quantity", + "unit": "1", + "why": "Landé factor for [0.671697, 14.249569] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "I need the Landé factor for a magnetic dipole moment of 21.7931 and a total angular momentum quantum number of 29.5106, please.", + "a": "7.962946716927578e+22", + "valid": true, + "kind": "compute", + "expect": 7.962946716927578e+22, + "dtype": "quantity", + "unit": "1", + "why": "Landé factor for [21.793126, 29.510609] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "If the magnetic dipole moment is 21.7931 and the total angular momentum quantum number is 29.5106, what is the Landé factor?", + "a": "7.962946716927578e+22", + "valid": true, + "kind": "compute", + "expect": 7.962946716927578e+22, + "dtype": "quantity", + "unit": "1", + "why": "Landé factor for [21.793126, 29.510609] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "Could you work out the Landé factor given magnetic dipole moment = 153.15, total angular momentum quantum number = 18.5953?", + "a": "8.880671155361362e+23", + "valid": true, + "kind": "compute", + "expect": 8.880671155361362e+23, + "dtype": "quantity", + "unit": "1", + "why": "Landé factor for [153.149965, 18.595315] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "I need the Landé factor for a magnetic dipole moment of 153.15 and a total angular momentum quantum number of 18.5953, please.", + "a": "8.880671155361362e+23", + "valid": true, + "kind": "compute", + "expect": 8.880671155361362e+23, + "dtype": "quantity", + "unit": "1", + "why": "Landé factor for [153.149965, 18.595315] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "If the magnetic dipole moment is 8.50728, what is the Landé factor?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total angular momentum quantum number is not given" + }, + { + "q": "Could you work out the Landé factor given magnetic dipole moment = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total angular momentum quantum number is not given" + }, + { + "q": "Do you know the Landé factor 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 Landé factor 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 Landé factor given magnetic dipole moment = -0.318734, total angular momentum quantum number = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative magnetic dipole moment: no example had one" + }, + { + "q": "What is a typical Landé factor?", + "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-land-factor/program.py b/root/organs10/physics/specialists/solved-land-factor/program.py new file mode 100644 index 0000000000000000000000000000000000000000..d318535812891441434722c5b9ca6f3d82519801 --- /dev/null +++ b/root/organs10/physics/specialists/solved-land-factor/program.py @@ -0,0 +1,7 @@ +"""Landé factor from magnetic dipole moment, total angular momentum quantum number (Landé factor (Wikidata Q1191684)) +The maths organ (systems.py): solved from the system q1 = ((q2)/(q3*q0)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(magnetic_dipole_moment, total_angular_momentum_quantum_number): + return 1.07828220253772e+23*magnetic_dipole_moment/total_angular_momentum_quantum_number diff --git a/root/organs10/physics/specialists/solved-land-factor/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-land-factor/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-land-factor/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-landau-ginzburg-number/organ_formula_v2.py b/root/organs10/physics/specialists/solved-landau-ginzburg-number/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-landau-ginzburg-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-landau-ginzburg-number/panel.json b/root/organs10/physics/specialists/solved-landau-ginzburg-number/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..4ee62e34bfa713aacf112889d17d192acfae5673 --- /dev/null +++ b/root/organs10/physics/specialists/solved-landau-ginzburg-number/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the Landau–Ginzburg number for London penetration depth 107.961 and superconducting coherence length 59.4105?", + "a": "1.2849533113937956", + "valid": true, + "kind": "compute", + "expect": 1.2849533113937956, + "dtype": "quantity", + "unit": "1", + "why": "Landau–Ginzburg number for [107.960666, 59.4105] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Tell me the Landau–Ginzburg number with London penetration depth of 107.961 and superconducting coherence length of 59.4105.", + "a": "1.2849533113937956", + "valid": true, + "kind": "compute", + "expect": 1.2849533113937956, + "dtype": "quantity", + "unit": "1", + "why": "Landau–Ginzburg number for [107.960666, 59.4105] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "What is the Landau–Ginzburg number for London penetration depth 169.527 and superconducting coherence length 280.774?", + "a": "0.42694048780569277", + "valid": true, + "kind": "compute", + "expect": 0.42694048780569277, + "dtype": "quantity", + "unit": "1", + "why": "Landau–Ginzburg number for [169.526958, 280.773702] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Tell me the Landau–Ginzburg number with London penetration depth of 169.527 and superconducting coherence length of 280.774.", + "a": "0.42694048780569277", + "valid": true, + "kind": "compute", + "expect": 0.42694048780569277, + "dtype": "quantity", + "unit": "1", + "why": "Landau–Ginzburg number for [169.526958, 280.773702] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "What is the Landau–Ginzburg number for London penetration depth 1.91195 and superconducting coherence length 82.2401?", + "a": "0.01643911570864928", + "valid": true, + "kind": "compute", + "expect": 0.01643911570864928, + "dtype": "quantity", + "unit": "1", + "why": "Landau–Ginzburg number for [1.911952, 82.240072] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Tell me the Landau–Ginzburg number with London penetration depth of 1.91195 and superconducting coherence length of 82.2401.", + "a": "0.01643911570864928", + "valid": true, + "kind": "compute", + "expect": 0.01643911570864928, + "dtype": "quantity", + "unit": "1", + "why": "Landau–Ginzburg number for [1.911952, 82.240072] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "What is the Landau–Ginzburg number for London penetration depth 1.64379 and superconducting coherence length 252.709?", + "a": "0.00459951497479437", + "valid": true, + "kind": "compute", + "expect": 0.00459951497479437, + "dtype": "quantity", + "unit": "1", + "why": "Landau–Ginzburg number for [1.643795, 252.709024] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Tell me the Landau–Ginzburg number with London penetration depth of 1.64379 and superconducting coherence length of 252.709.", + "a": "0.00459951497479437", + "valid": true, + "kind": "compute", + "expect": 0.00459951497479437, + "dtype": "quantity", + "unit": "1", + "why": "Landau–Ginzburg number for [1.643795, 252.709024] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "What is the Landau–Ginzburg number for London penetration depth 18.3169 and superconducting coherence length 6.8386?", + "a": "1.893952644323225", + "valid": true, + "kind": "compute", + "expect": 1.893952644323225, + "dtype": "quantity", + "unit": "1", + "why": "Landau–Ginzburg number for [18.316867, 6.838598] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Tell me the Landau–Ginzburg number with London penetration depth of 18.3169 and superconducting coherence length of 6.8386.", + "a": "1.893952644323225", + "valid": true, + "kind": "compute", + "expect": 1.893952644323225, + "dtype": "quantity", + "unit": "1", + "why": "Landau–Ginzburg number for [18.316867, 6.838598] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "What is the Landau–Ginzburg number for London penetration depth 156.502 and superconducting coherence length 185.886?", + "a": "0.5953290111278429", + "valid": true, + "kind": "compute", + "expect": 0.5953290111278429, + "dtype": "quantity", + "unit": "1", + "why": "Landau–Ginzburg number for [156.50181, 185.886273] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Tell me the Landau–Ginzburg number with London penetration depth of 156.502 and superconducting coherence length of 185.886.", + "a": "0.5953290111278429", + "valid": true, + "kind": "compute", + "expect": 0.5953290111278429, + "dtype": "quantity", + "unit": "1", + "why": "Landau–Ginzburg number for [156.50181, 185.886273] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "What is the Landau–Ginzburg number for London penetration depth 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the superconducting coherence length is not given" + }, + { + "q": "Tell me the Landau–Ginzburg number with London penetration depth of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the superconducting coherence length is not given" + }, + { + "q": "What is the Landau–Ginzburg 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 Landau–Ginzburg 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 Landau–Ginzburg number with London penetration depth of -1.91195 and superconducting coherence length of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative London penetration depth: no example had one" + }, + { + "q": "Which Landau–Ginzburg 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 London penetration depth is 8.50728 and the superconducting coherence length is 17.7888, what is the Landau–Ginzburg number?", + "a": "0.338165848773796", + "valid": true, + "kind": "compute", + "expect": 0.338165848773796, + "dtype": "quantity", + "unit": "1", + "why": "Landau–Ginzburg number for [8.507283, 17.788778] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Could you work out the Landau–Ginzburg number given London penetration depth = 8.50728, superconducting coherence length = 17.7888?", + "a": "0.338165848773796", + "valid": true, + "kind": "compute", + "expect": 0.338165848773796, + "dtype": "quantity", + "unit": "1", + "why": "Landau–Ginzburg number for [8.507283, 17.788778] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "I need the Landau–Ginzburg number for a London penetration depth of 8.24951 and a superconducting coherence length of 0.634021, please.", + "a": "9.200459635498946", + "valid": true, + "kind": "compute", + "expect": 9.200459635498946, + "dtype": "quantity", + "unit": "1", + "why": "Landau–Ginzburg number for [8.24951, 0.634021] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "If the London penetration depth is 8.24951 and the superconducting coherence length is 0.634021, what is the Landau–Ginzburg number?", + "a": "9.200459635498946", + "valid": true, + "kind": "compute", + "expect": 9.200459635498946, + "dtype": "quantity", + "unit": "1", + "why": "Landau–Ginzburg number for [8.24951, 0.634021] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Could you work out the Landau–Ginzburg number given London penetration depth = 0.318734, superconducting coherence length = 9.56607?", + "a": "0.023560248168360776", + "valid": true, + "kind": "compute", + "expect": 0.023560248168360776, + "dtype": "quantity", + "unit": "1", + "why": "Landau–Ginzburg number for [0.318734, 9.566071] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "I need the Landau–Ginzburg number for a London penetration depth of 0.318734 and a superconducting coherence length of 9.56607, please.", + "a": "0.023560248168360776", + "valid": true, + "kind": "compute", + "expect": 0.023560248168360776, + "dtype": "quantity", + "unit": "1", + "why": "Landau–Ginzburg number for [0.318734, 9.566071] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "If the London penetration depth is 0.671697 and the superconducting coherence length is 14.2496, what is the Landau–Ginzburg number?", + "a": "0.03333164914305671", + "valid": true, + "kind": "compute", + "expect": 0.03333164914305671, + "dtype": "quantity", + "unit": "1", + "why": "Landau–Ginzburg number for [0.671697, 14.249569] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Could you work out the Landau–Ginzburg number given London penetration depth = 0.671697, superconducting coherence length = 14.2496?", + "a": "0.03333164914305671", + "valid": true, + "kind": "compute", + "expect": 0.03333164914305671, + "dtype": "quantity", + "unit": "1", + "why": "Landau–Ginzburg number for [0.671697, 14.249569] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "I need the Landau–Ginzburg number for a London penetration depth of 21.7931 and a superconducting coherence length of 29.5106, please.", + "a": "0.5221873836473414", + "valid": true, + "kind": "compute", + "expect": 0.5221873836473414, + "dtype": "quantity", + "unit": "1", + "why": "Landau–Ginzburg number for [21.793126, 29.510609] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "If the London penetration depth is 21.7931 and the superconducting coherence length is 29.5106, what is the Landau–Ginzburg number?", + "a": "0.5221873836473414", + "valid": true, + "kind": "compute", + "expect": 0.5221873836473414, + "dtype": "quantity", + "unit": "1", + "why": "Landau–Ginzburg number for [21.793126, 29.510609] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Could you work out the Landau–Ginzburg number given London penetration depth = 153.15, superconducting coherence length = 18.5953?", + "a": "5.823691405334237", + "valid": true, + "kind": "compute", + "expect": 5.823691405334237, + "dtype": "quantity", + "unit": "1", + "why": "Landau–Ginzburg number for [153.149965, 18.595315] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "I need the Landau–Ginzburg number for a London penetration depth of 153.15 and a superconducting coherence length of 18.5953, please.", + "a": "5.823691405334237", + "valid": true, + "kind": "compute", + "expect": 5.823691405334237, + "dtype": "quantity", + "unit": "1", + "why": "Landau–Ginzburg number for [153.149965, 18.595315] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "If the London penetration depth is 8.50728, what is the Landau–Ginzburg number?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the superconducting coherence length is not given" + }, + { + "q": "Could you work out the Landau–Ginzburg number given London penetration depth = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the superconducting coherence length is not given" + }, + { + "q": "Do you know the Landau–Ginzburg 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 Landau–Ginzburg 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 Landau–Ginzburg number given London penetration depth = -0.318734, superconducting coherence length = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative London penetration depth: no example had one" + }, + { + "q": "What is a typical Landau–Ginzburg 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-landau-ginzburg-number/program.py b/root/organs10/physics/specialists/solved-landau-ginzburg-number/program.py new file mode 100644 index 0000000000000000000000000000000000000000..b8da7f1fde977e60b5daf15ab0bf7125cf794747 --- /dev/null +++ b/root/organs10/physics/specialists/solved-landau-ginzburg-number/program.py @@ -0,0 +1,7 @@ +"""Landau–Ginzburg number from London penetration depth, superconducting coherence length (Landau–Ginzburg number (Wikidata Q105421034)) +The maths organ (systems.py): solved from the system q1 = ((q0)/(q2*sqrt(2))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(london_penetration_depth, superconducting_coherence_length): + return 0.707106781186548*london_penetration_depth/superconducting_coherence_length diff --git a/root/organs10/physics/specialists/solved-landau-ginzburg-number/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-landau-ginzburg-number/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-landau-ginzburg-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-latent-heat-of-phase-transiti/organ_formula_v2.py b/root/organs10/physics/specialists/solved-latent-heat-of-phase-transiti/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-latent-heat-of-phase-transiti/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-latent-heat-of-phase-transiti/panel.json b/root/organs10/physics/specialists/solved-latent-heat-of-phase-transiti/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..bb9705491a2642fa10fefa400e4018007844975d --- /dev/null +++ b/root/organs10/physics/specialists/solved-latent-heat-of-phase-transiti/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the latent heat of phase transition for specific latent heat of phase transition 107.961 and mass 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "J", + "why": "latent heat of phase transition for [107.960666, 59.4105] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "Tell me the latent heat of phase transition with specific latent heat of phase transition of 107.961 and mass of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "J", + "why": "latent heat of phase transition for [107.960666, 59.4105] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "What is the latent heat of phase transition for specific latent heat of phase transition 169.527 and mass 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "J", + "why": "latent heat of phase transition for [169.526958, 280.773702] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "Tell me the latent heat of phase transition with specific latent heat of phase transition of 169.527 and mass of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "J", + "why": "latent heat of phase transition for [169.526958, 280.773702] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "What is the latent heat of phase transition for specific latent heat of phase transition 1.91195 and mass 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "J", + "why": "latent heat of phase transition for [1.911952, 82.240072] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "Tell me the latent heat of phase transition with specific latent heat of phase transition of 1.91195 and mass of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "J", + "why": "latent heat of phase transition for [1.911952, 82.240072] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "What is the latent heat of phase transition for specific latent heat of phase transition 1.64379 and mass 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "J", + "why": "latent heat of phase transition for [1.643795, 252.709024] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "Tell me the latent heat of phase transition with specific latent heat of phase transition of 1.64379 and mass of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "J", + "why": "latent heat of phase transition for [1.643795, 252.709024] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "What is the latent heat of phase transition for specific latent heat of phase transition 18.3169 and mass 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "J", + "why": "latent heat of phase transition for [18.316867, 6.838598] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "Tell me the latent heat of phase transition with specific latent heat of phase transition of 18.3169 and mass of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "J", + "why": "latent heat of phase transition for [18.316867, 6.838598] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "What is the latent heat of phase transition for specific latent heat of phase transition 156.502 and mass 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "J", + "why": "latent heat of phase transition for [156.50181, 185.886273] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "Tell me the latent heat of phase transition with specific latent heat of phase transition of 156.502 and mass of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "J", + "why": "latent heat of phase transition for [156.50181, 185.886273] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "What is the latent heat of phase transition for specific latent heat of phase transition 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Tell me the latent heat of phase transition with specific latent heat of phase transition of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "What is the latent heat of phase transition 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 latent heat of phase transition 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 latent heat of phase transition with specific latent heat of phase transition of -1.91195 and mass of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative specific latent heat of phase transition: no example had one" + }, + { + "q": "Which latent heat of phase transition 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 latent heat of phase transition is 8.50728 and the mass is 17.7888, what is the latent heat of phase transition?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "J", + "why": "latent heat of phase transition for [8.507283, 17.788778] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "Could you work out the latent heat of phase transition given specific latent heat of phase transition = 8.50728, mass = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "J", + "why": "latent heat of phase transition for [8.507283, 17.788778] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "I need the latent heat of phase transition for a specific latent heat of phase transition of 8.24951 and a mass of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "J", + "why": "latent heat of phase transition for [8.24951, 0.634021] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "If the specific latent heat of phase transition is 8.24951 and the mass is 0.634021, what is the latent heat of phase transition?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "J", + "why": "latent heat of phase transition for [8.24951, 0.634021] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "Could you work out the latent heat of phase transition given specific latent heat of phase transition = 0.318734, mass = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "J", + "why": "latent heat of phase transition for [0.318734, 9.566071] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "I need the latent heat of phase transition for a specific latent heat of phase transition of 0.318734 and a mass of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "J", + "why": "latent heat of phase transition for [0.318734, 9.566071] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "If the specific latent heat of phase transition is 0.671697 and the mass is 14.2496, what is the latent heat of phase transition?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "J", + "why": "latent heat of phase transition for [0.671697, 14.249569] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "Could you work out the latent heat of phase transition given specific latent heat of phase transition = 0.671697, mass = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "J", + "why": "latent heat of phase transition for [0.671697, 14.249569] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "I need the latent heat of phase transition for a specific latent heat of phase transition of 21.7931 and a mass of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "J", + "why": "latent heat of phase transition for [21.793126, 29.510609] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "If the specific latent heat of phase transition is 21.7931 and the mass is 29.5106, what is the latent heat of phase transition?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "J", + "why": "latent heat of phase transition for [21.793126, 29.510609] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "Could you work out the latent heat of phase transition given specific latent heat of phase transition = 153.15, mass = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "J", + "why": "latent heat of phase transition for [153.149965, 18.595315] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "I need the latent heat of phase transition for a specific latent heat of phase transition of 153.15 and a mass of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "J", + "why": "latent heat of phase transition for [153.149965, 18.595315] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "If the specific latent heat of phase transition is 8.50728, what is the latent heat of phase transition?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Could you work out the latent heat of phase transition given specific latent heat of phase transition = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Do you know the latent heat of phase transition 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 latent heat of phase transition 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 latent heat of phase transition given specific latent heat of phase transition = -0.318734, mass = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative specific latent heat of phase transition: no example had one" + }, + { + "q": "What is a typical latent heat of phase transition?", + "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-latent-heat-of-phase-transiti/program.py b/root/organs10/physics/specialists/solved-latent-heat-of-phase-transiti/program.py new file mode 100644 index 0000000000000000000000000000000000000000..c27feb614364652782a2204119928625c4a0460f --- /dev/null +++ b/root/organs10/physics/specialists/solved-latent-heat-of-phase-transiti/program.py @@ -0,0 +1,7 @@ +"""latent heat of phase transition from specific latent heat of phase transition, mass (specific latent heat of phase transition (Wikidata Q104217205)) +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(specific_latent_heat_of_phase_transition, mass): + return 1.0*mass*specific_latent_heat_of_phase_transition diff --git a/root/organs10/physics/specialists/solved-latent-heat-of-phase-transiti/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-latent-heat-of-phase-transiti/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-latent-heat-of-phase-transiti/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-lattice-plane-spacing/organ_formula_v2.py b/root/organs10/physics/specialists/solved-lattice-plane-spacing/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-lattice-plane-spacing/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-lattice-plane-spacing/panel.json b/root/organs10/physics/specialists/solved-lattice-plane-spacing/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..0b7e7a5e291ee208b7cc5638c570aa4847580c3a --- /dev/null +++ b/root/organs10/physics/specialists/solved-lattice-plane-spacing/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the lattice plane spacing for Bragg angle 107.961, electromagnetic wavelength 59.4105 and order of reflexion 5.77717?", + "a": "188.30847222978662", + "valid": true, + "kind": "compute", + "expect": 188.30847222978662, + "dtype": "quantity", + "unit": "m", + "why": "lattice plane spacing for [107.960666, 59.4105, 5.777165] (the system Bragg angle (Wikidata Q105488118))" + }, + { + "q": "Tell me the lattice plane spacing with Bragg angle of 107.961, electromagnetic wavelength of 59.4105 and order of reflexion of 5.77717.", + "a": "188.30847222978662", + "valid": true, + "kind": "compute", + "expect": 188.30847222978662, + "dtype": "quantity", + "unit": "m", + "why": "lattice plane spacing for [107.960666, 59.4105, 5.777165] (the system Bragg angle (Wikidata Q105488118))" + }, + { + "q": "What is the lattice plane spacing for Bragg angle 8.24951, electromagnetic wavelength 0.634021 and order of reflexion 6.34623?", + "a": "2.180141916146952", + "valid": true, + "kind": "compute", + "expect": 2.180141916146952, + "dtype": "quantity", + "unit": "m", + "why": "lattice plane spacing for [8.24951, 0.634021, 6.346226] (the system Bragg angle (Wikidata Q105488118))" + }, + { + "q": "Tell me the lattice plane spacing with Bragg angle of 8.24951, electromagnetic wavelength of 0.634021 and order of reflexion of 6.34623.", + "a": "2.180141916146952", + "valid": true, + "kind": "compute", + "expect": 2.180141916146952, + "dtype": "quantity", + "unit": "m", + "why": "lattice plane spacing for [8.24951, 0.634021, 6.346226] (the system Bragg angle (Wikidata Q105488118))" + }, + { + "q": "What is the lattice plane spacing for Bragg angle 126.709, electromagnetic wavelength 1.70503 and order of reflexion 2.9896?", + "a": "2.9457784328679377", + "valid": true, + "kind": "compute", + "expect": 2.9457784328679377, + "dtype": "quantity", + "unit": "m", + "why": "lattice plane spacing for [126.709252, 1.705032, 2.9896] (the system Bragg angle (Wikidata Q105488118))" + }, + { + "q": "Tell me the lattice plane spacing with Bragg angle of 126.709, electromagnetic wavelength of 1.70503 and order of reflexion of 2.9896.", + "a": "2.9457784328679377", + "valid": true, + "kind": "compute", + "expect": 2.9457784328679377, + "dtype": "quantity", + "unit": "m", + "why": "lattice plane spacing for [126.709252, 1.705032, 2.9896] (the system Bragg angle (Wikidata Q105488118))" + }, + { + "q": "What is the lattice plane spacing for Bragg angle 20.3966, electromagnetic wavelength 18.3169 and order of reflexion 6.8386?", + "a": "62.64857963245239", + "valid": true, + "kind": "compute", + "expect": 62.64857963245239, + "dtype": "quantity", + "unit": "m", + "why": "lattice plane spacing for [20.396558, 18.316867, 6.838598] (the system Bragg angle (Wikidata Q105488118))" + }, + { + "q": "Tell me the lattice plane spacing with Bragg angle of 20.3966, electromagnetic wavelength of 18.3169 and order of reflexion of 6.8386.", + "a": "62.64857963245239", + "valid": true, + "kind": "compute", + "expect": 62.64857963245239, + "dtype": "quantity", + "unit": "m", + "why": "lattice plane spacing for [20.396558, 18.316867, 6.838598] (the system Bragg angle (Wikidata Q105488118))" + }, + { + "q": "What is the lattice plane spacing for Bragg angle 75.8534, electromagnetic wavelength 0.558623 and order of reflexion 21.7946?", + "a": "13.846152947606877", + "valid": true, + "kind": "compute", + "expect": 13.846152947606877, + "dtype": "quantity", + "unit": "m", + "why": "lattice plane spacing for [75.853435, 0.558623, 21.794621] (the system Bragg angle (Wikidata Q105488118))" + }, + { + "q": "Tell me the lattice plane spacing with Bragg angle of 75.8534, electromagnetic wavelength of 0.558623 and order of reflexion of 21.7946.", + "a": "13.846152947606877", + "valid": true, + "kind": "compute", + "expect": 13.846152947606877, + "dtype": "quantity", + "unit": "m", + "why": "lattice plane spacing for [75.853435, 0.558623, 21.794621] (the system Bragg angle (Wikidata Q105488118))" + }, + { + "q": "What is the lattice plane spacing for Bragg angle 169.843, electromagnetic wavelength 12.6647 and order of reflexion 34.8151?", + "a": "1125.5161401541316", + "valid": true, + "kind": "compute", + "expect": 1125.5161401541316, + "dtype": "quantity", + "unit": "m", + "why": "lattice plane spacing for [169.843153, 12.664671, 34.81508] (the system Bragg angle (Wikidata Q105488118))" + }, + { + "q": "Tell me the lattice plane spacing with Bragg angle of 169.843, electromagnetic wavelength of 12.6647 and order of reflexion of 34.8151.", + "a": "1125.5161401541316", + "valid": true, + "kind": "compute", + "expect": 1125.5161401541316, + "dtype": "quantity", + "unit": "m", + "why": "lattice plane spacing for [169.843153, 12.664671, 34.81508] (the system Bragg angle (Wikidata Q105488118))" + }, + { + "q": "What is the lattice plane spacing for Bragg angle 107.961 and electromagnetic wavelength 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the order of reflexion is not given" + }, + { + "q": "Tell me the lattice plane spacing with Bragg angle of 8.24951 and electromagnetic wavelength of 0.634021.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the order of reflexion is not given" + }, + { + "q": "What is the lattice plane spacing 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 lattice plane spacing 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 lattice plane spacing with Bragg angle of -126.709, electromagnetic wavelength of 1.70503 and order of reflexion of 2.9896.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Bragg angle: no example had one" + }, + { + "q": "Which lattice plane spacing 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 Bragg angle is 2.21571, electromagnetic wavelength is 58.5346 and the order of reflexion is 22.652, what is the lattice plane spacing?", + "a": "829.5860198819495", + "valid": true, + "kind": "compute", + "expect": 829.5860198819495, + "dtype": "quantity", + "unit": "m", + "why": "lattice plane spacing for [2.215713, 58.534594, 22.652023] (the system Bragg angle (Wikidata Q105488118))" + }, + { + "q": "Could you work out the lattice plane spacing given Bragg angle = 2.21571, electromagnetic wavelength = 58.5346, order of reflexion = 22.652?", + "a": "829.5860198819495", + "valid": true, + "kind": "compute", + "expect": 829.5860198819495, + "dtype": "quantity", + "unit": "m", + "why": "lattice plane spacing for [2.215713, 58.534594, 22.652023] (the system Bragg angle (Wikidata Q105488118))" + }, + { + "q": "I need the lattice plane spacing for a Bragg angle of 82.2401, an electromagnetic wavelength of 13.999 and an order of reflexion of 0.348438, please.", + "a": "4.601229234858962", + "valid": true, + "kind": "compute", + "expect": 4.601229234858962, + "dtype": "quantity", + "unit": "m", + "why": "lattice plane spacing for [82.240072, 13.999029, 0.348438] (the system Bragg angle (Wikidata Q105488118))" + }, + { + "q": "If the Bragg angle is 82.2401, electromagnetic wavelength is 13.999 and the order of reflexion is 0.348438, what is the lattice plane spacing?", + "a": "4.601229234858962", + "valid": true, + "kind": "compute", + "expect": 4.601229234858962, + "dtype": "quantity", + "unit": "m", + "why": "lattice plane spacing for [82.240072, 13.999029, 0.348438] (the system Bragg angle (Wikidata Q105488118))" + }, + { + "q": "Could you work out the lattice plane spacing given Bragg angle = 6.98086, electromagnetic wavelength = 0.551236, order of reflexion = 2.88512?", + "a": "1.2377764180442794", + "valid": true, + "kind": "compute", + "expect": 1.2377764180442794, + "dtype": "quantity", + "unit": "m", + "why": "lattice plane spacing for [6.980857, 0.551236, 2.88512] (the system Bragg angle (Wikidata Q105488118))" + }, + { + "q": "I need the lattice plane spacing for a Bragg angle of 6.98086, an electromagnetic wavelength of 0.551236 and an order of reflexion of 2.88512, please.", + "a": "1.2377764180442794", + "valid": true, + "kind": "compute", + "expect": 1.2377764180442794, + "dtype": "quantity", + "unit": "m", + "why": "lattice plane spacing for [6.980857, 0.551236, 2.88512] (the system Bragg angle (Wikidata Q105488118))" + }, + { + "q": "If the Bragg angle is 21.7931, electromagnetic wavelength is 29.5106 and the order of reflexion is 8.50341, what is the lattice plane spacing?", + "a": "637.7780311190642", + "valid": true, + "kind": "compute", + "expect": 637.7780311190642, + "dtype": "quantity", + "unit": "m", + "why": "lattice plane spacing for [21.793126, 29.510609, 8.503407] (the system Bragg angle (Wikidata Q105488118))" + }, + { + "q": "Could you work out the lattice plane spacing given Bragg angle = 21.7931, electromagnetic wavelength = 29.5106, order of reflexion = 8.50341?", + "a": "637.7780311190642", + "valid": true, + "kind": "compute", + "expect": 637.7780311190642, + "dtype": "quantity", + "unit": "m", + "why": "lattice plane spacing for [21.793126, 29.510609, 8.503407] (the system Bragg angle (Wikidata Q105488118))" + }, + { + "q": "I need the lattice plane spacing for a Bragg angle of 1.45005, an electromagnetic wavelength of 76.5154 and an order of reflexion of 3.14491, please.", + "a": "121.19962247457137", + "valid": true, + "kind": "compute", + "expect": 121.19962247457137, + "dtype": "quantity", + "unit": "m", + "why": "lattice plane spacing for [1.450045, 76.515411, 3.144912] (the system Bragg angle (Wikidata Q105488118))" + }, + { + "q": "If the Bragg angle is 1.45005, electromagnetic wavelength is 76.5154 and the order of reflexion is 3.14491, what is the lattice plane spacing?", + "a": "121.19962247457137", + "valid": true, + "kind": "compute", + "expect": 121.19962247457137, + "dtype": "quantity", + "unit": "m", + "why": "lattice plane spacing for [1.450045, 76.515411, 3.144912] (the system Bragg angle (Wikidata Q105488118))" + }, + { + "q": "Could you work out the lattice plane spacing given Bragg angle = 241.598, electromagnetic wavelength = 1.13479, order of reflexion = 0.744193?", + "a": "1.4090278345895624", + "valid": true, + "kind": "compute", + "expect": 1.4090278345895624, + "dtype": "quantity", + "unit": "m", + "why": "lattice plane spacing for [241.598281, 1.134791, 0.744193] (the system Bragg angle (Wikidata Q105488118))" + }, + { + "q": "I need the lattice plane spacing for a Bragg angle of 241.598, an electromagnetic wavelength of 1.13479 and an order of reflexion of 0.744193, please.", + "a": "1.4090278345895624", + "valid": true, + "kind": "compute", + "expect": 1.4090278345895624, + "dtype": "quantity", + "unit": "m", + "why": "lattice plane spacing for [241.598281, 1.134791, 0.744193] (the system Bragg angle (Wikidata Q105488118))" + }, + { + "q": "If the Bragg angle is 2.21571 and the electromagnetic wavelength is 58.5346, what is the lattice plane spacing?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the order of reflexion is not given" + }, + { + "q": "Could you work out the lattice plane spacing given Bragg angle = 82.2401, electromagnetic wavelength = 13.999?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the order of reflexion is not given" + }, + { + "q": "Do you know the lattice plane spacing 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 lattice plane spacing 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 lattice plane spacing given Bragg angle = -6.98086, electromagnetic wavelength = 0.551236, order of reflexion = 2.88512?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Bragg angle: no example had one" + }, + { + "q": "What is a typical lattice plane spacing?", + "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-lattice-plane-spacing/program.py b/root/organs10/physics/specialists/solved-lattice-plane-spacing/program.py new file mode 100644 index 0000000000000000000000000000000000000000..abd958379fa5b0a0ac64aa457439945477362f64 --- /dev/null +++ b/root/organs10/physics/specialists/solved-lattice-plane-spacing/program.py @@ -0,0 +1,7 @@ +"""lattice plane spacing from Bragg angle, electromagnetic wavelength, order of reflexion (Bragg angle (Wikidata Q105488118)) +The maths organ (systems.py): solved from the system 2*q1*sin (q0) = q3*q2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(bragg_angle, electromagnetic_wavelength, order_of_reflexion): + return 0.5*electromagnetic_wavelength*order_of_reflexion/math.sin(1.0*bragg_angle) diff --git a/root/organs10/physics/specialists/solved-lattice-plane-spacing/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-lattice-plane-spacing/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-lattice-plane-spacing/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-length-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-length-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-length-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-length-2/panel.json b/root/organs10/physics/specialists/solved-length-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..7aa49ec6b21b126a89f228389147ea78c7ca3dd7 --- /dev/null +++ b/root/organs10/physics/specialists/solved-length-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the length for linear strain 107.961 and change in length 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "m", + "why": "length for [107.960666, 59.4105] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "Tell me the length with linear strain of 107.961 and change in length of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "m", + "why": "length for [107.960666, 59.4105] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "What is the length for linear strain 169.527 and change in length 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "m", + "why": "length for [169.526958, 280.773702] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "Tell me the length with linear strain of 169.527 and change in length of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "m", + "why": "length for [169.526958, 280.773702] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "What is the length for linear strain 1.91195 and change in length 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "m", + "why": "length for [1.911952, 82.240072] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "Tell me the length with linear strain of 1.91195 and change in length of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "m", + "why": "length for [1.911952, 82.240072] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "What is the length for linear strain 1.64379 and change in length 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "m", + "why": "length for [1.643795, 252.709024] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "Tell me the length with linear strain of 1.64379 and change in length of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "m", + "why": "length for [1.643795, 252.709024] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "What is the length for linear strain 18.3169 and change in length 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "m", + "why": "length for [18.316867, 6.838598] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "Tell me the length with linear strain of 18.3169 and change in length of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "m", + "why": "length for [18.316867, 6.838598] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "What is the length for linear strain 156.502 and change in length 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "m", + "why": "length for [156.50181, 185.886273] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "Tell me the length with linear strain of 156.502 and change in length of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "m", + "why": "length for [156.50181, 185.886273] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "What is the length for linear strain 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the change in length is not given" + }, + { + "q": "Tell me the length with linear strain of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the change in length is not given" + }, + { + "q": "What is the 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 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 length with linear strain of -1.91195 and change in length of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative linear strain: no example had one" + }, + { + "q": "Which 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 linear strain is 8.50728 and the change in length is 17.7888, what is the length?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "m", + "why": "length for [8.507283, 17.788778] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "Could you work out the length given linear strain = 8.50728, change in length = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "m", + "why": "length for [8.507283, 17.788778] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "I need the length for a linear strain of 8.24951 and a change in length of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "m", + "why": "length for [8.24951, 0.634021] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "If the linear strain is 8.24951 and the change in length is 0.634021, what is the length?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "m", + "why": "length for [8.24951, 0.634021] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "Could you work out the length given linear strain = 0.318734, change in length = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "m", + "why": "length for [0.318734, 9.566071] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "I need the length for a linear strain of 0.318734 and a change in length of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "m", + "why": "length for [0.318734, 9.566071] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "If the linear strain is 0.671697 and the change in length is 14.2496, what is the length?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "m", + "why": "length for [0.671697, 14.249569] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "Could you work out the length given linear strain = 0.671697, change in length = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "m", + "why": "length for [0.671697, 14.249569] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "I need the length for a linear strain of 21.7931 and a change in length of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "m", + "why": "length for [21.793126, 29.510609] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "If the linear strain is 21.7931 and the change in length is 29.5106, what is the length?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "m", + "why": "length for [21.793126, 29.510609] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "Could you work out the length given linear strain = 153.15, change in length = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "m", + "why": "length for [153.149965, 18.595315] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "I need the length for a linear strain of 153.15 and a change in length of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "m", + "why": "length for [153.149965, 18.595315] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "If the linear strain is 8.50728, what is the length?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the change in length is not given" + }, + { + "q": "Could you work out the length given linear strain = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the change in length is not given" + }, + { + "q": "Do you know the 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 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 length given linear strain = -0.318734, change in length = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative linear strain: no example had one" + }, + { + "q": "What is a typical 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-length-2/program.py b/root/organs10/physics/specialists/solved-length-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..29880abe933fb7a90edc5c9ce0a311a7aa72f2ad --- /dev/null +++ b/root/organs10/physics/specialists/solved-length-2/program.py @@ -0,0 +1,7 @@ +"""length from linear strain, change in length (linear strain (Wikidata Q1990546)) +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_strain, change_in_length): + return 1.0*change_in_length/linear_strain diff --git a/root/organs10/physics/specialists/solved-length-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-length-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-length-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-length-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-length-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-length-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-length-3/panel.json b/root/organs10/physics/specialists/solved-length-3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..3e8bca150402c5b74351216cc04e5fc99d9d1ae9 --- /dev/null +++ b/root/organs10/physics/specialists/solved-length-3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the length for linear absorption coefficient 107.961 and decadic absorbance 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "m", + "why": "length for [107.960666, 59.4105] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Tell me the length with linear absorption coefficient of 107.961 and decadic absorbance of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "m", + "why": "length for [107.960666, 59.4105] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "What is the length for linear absorption coefficient 169.527 and decadic absorbance 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "m", + "why": "length for [169.526958, 280.773702] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Tell me the length with linear absorption coefficient of 169.527 and decadic absorbance of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "m", + "why": "length for [169.526958, 280.773702] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "What is the length for linear absorption coefficient 1.91195 and decadic absorbance 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "m", + "why": "length for [1.911952, 82.240072] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Tell me the length with linear absorption coefficient of 1.91195 and decadic absorbance of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "m", + "why": "length for [1.911952, 82.240072] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "What is the length for linear absorption coefficient 1.64379 and decadic absorbance 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "m", + "why": "length for [1.643795, 252.709024] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Tell me the length with linear absorption coefficient of 1.64379 and decadic absorbance of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "m", + "why": "length for [1.643795, 252.709024] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "What is the length for linear absorption coefficient 18.3169 and decadic absorbance 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "m", + "why": "length for [18.316867, 6.838598] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Tell me the length with linear absorption coefficient of 18.3169 and decadic absorbance of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "m", + "why": "length for [18.316867, 6.838598] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "What is the length for linear absorption coefficient 156.502 and decadic absorbance 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "m", + "why": "length for [156.50181, 185.886273] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Tell me the length with linear absorption coefficient of 156.502 and decadic absorbance of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "m", + "why": "length for [156.50181, 185.886273] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "What is the length for linear absorption coefficient 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the decadic absorbance is not given" + }, + { + "q": "Tell me the length with linear absorption coefficient of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the decadic absorbance is not given" + }, + { + "q": "What is the 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 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 length with linear absorption coefficient of -1.91195 and decadic absorbance 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 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 linear absorption coefficient is 8.50728 and the decadic absorbance is 17.7888, what is the length?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "m", + "why": "length for [8.507283, 17.788778] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Could you work out the length given linear absorption coefficient = 8.50728, decadic absorbance = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "m", + "why": "length for [8.507283, 17.788778] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "I need the length for a linear absorption coefficient of 8.24951 and a decadic absorbance of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "m", + "why": "length for [8.24951, 0.634021] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "If the linear absorption coefficient is 8.24951 and the decadic absorbance is 0.634021, what is the length?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "m", + "why": "length for [8.24951, 0.634021] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Could you work out the length given linear absorption coefficient = 0.318734, decadic absorbance = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "m", + "why": "length for [0.318734, 9.566071] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "I need the length for a linear absorption coefficient of 0.318734 and a decadic absorbance of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "m", + "why": "length for [0.318734, 9.566071] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "If the linear absorption coefficient is 0.671697 and the decadic absorbance is 14.2496, what is the length?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "m", + "why": "length for [0.671697, 14.249569] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Could you work out the length given linear absorption coefficient = 0.671697, decadic absorbance = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "m", + "why": "length for [0.671697, 14.249569] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "I need the length for a linear absorption coefficient of 21.7931 and a decadic absorbance of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "m", + "why": "length for [21.793126, 29.510609] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "If the linear absorption coefficient is 21.7931 and the decadic absorbance is 29.5106, what is the length?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "m", + "why": "length for [21.793126, 29.510609] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Could you work out the length given linear absorption coefficient = 153.15, decadic absorbance = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "m", + "why": "length for [153.149965, 18.595315] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "I need the length for a linear absorption coefficient of 153.15 and a decadic absorbance of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "m", + "why": "length for [153.149965, 18.595315] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "If the linear absorption coefficient is 8.50728, what is the length?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the decadic absorbance is not given" + }, + { + "q": "Could you work out the length given linear absorption coefficient = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the decadic absorbance is not given" + }, + { + "q": "Do you know the 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 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 length given linear absorption coefficient = -0.318734, decadic absorbance = 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 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-length-3/program.py b/root/organs10/physics/specialists/solved-length-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..b18bf21aa2456adb8329f3dd9e4e809c242558a8 --- /dev/null +++ b/root/organs10/physics/specialists/solved-length-3/program.py @@ -0,0 +1,7 @@ +"""length from linear absorption coefficient, decadic absorbance (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, decadic_absorbance): + return 1.0*decadic_absorbance/linear_absorption_coefficient diff --git a/root/organs10/physics/specialists/solved-length-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-length-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-length-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-length-4/organ_formula_v2.py b/root/organs10/physics/specialists/solved-length-4/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-length-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-length-4/panel.json b/root/organs10/physics/specialists/solved-length-4/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..049f42150bf78716cc1e2e23f0429e5a084da1d3 --- /dev/null +++ b/root/organs10/physics/specialists/solved-length-4/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the length for Péclet number for heat transfer 107.961, speed 59.4105 and thermal diffusivity 5.77717?", + "a": "10.498254293702432", + "valid": true, + "kind": "compute", + "expect": 10.498254293702432, + "dtype": "quantity", + "unit": "m", + "why": "length for [107.960666, 59.4105, 5.777165] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "Tell me the length with Péclet number for heat transfer of 107.961, speed of 59.4105 and thermal diffusivity of 5.77717.", + "a": "10.498254293702432", + "valid": true, + "kind": "compute", + "expect": 10.498254293702432, + "dtype": "quantity", + "unit": "m", + "why": "length for [107.960666, 59.4105, 5.777165] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "What is the length for Péclet number for heat transfer 85.2416, speed 160.878 and thermal diffusivity 2.69428?", + "a": "1.4275672119863798", + "valid": true, + "kind": "compute", + "expect": 1.4275672119863798, + "dtype": "quantity", + "unit": "m", + "why": "length for [85.241623, 160.878442, 2.69428] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "Tell me the length with Péclet number for heat transfer of 85.2416, speed of 160.878 and thermal diffusivity of 2.69428.", + "a": "1.4275672119863798", + "valid": true, + "kind": "compute", + "expect": 1.4275672119863798, + "dtype": "quantity", + "unit": "m", + "why": "length for [85.241623, 160.878442, 2.69428] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "What is the length for Péclet number for heat transfer 45.6157, speed 4.97131 and thermal diffusivity 94.305?", + "a": "865.3232325447225", + "valid": true, + "kind": "compute", + "expect": 865.3232325447225, + "dtype": "quantity", + "unit": "m", + "why": "length for [45.61567, 4.971307, 94.305046] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "Tell me the length with Péclet number for heat transfer of 45.6157, speed of 4.97131 and thermal diffusivity of 94.305.", + "a": "865.3232325447225", + "valid": true, + "kind": "compute", + "expect": 865.3232325447225, + "dtype": "quantity", + "unit": "m", + "why": "length for [45.61567, 4.971307, 94.305046] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "What is the length for Péclet number for heat transfer 10.5638, speed 198.839 and thermal diffusivity 0.671697?", + "a": "0.03568568130708054", + "valid": true, + "kind": "compute", + "expect": 0.03568568130708054, + "dtype": "quantity", + "unit": "m", + "why": "length for [10.563843, 198.838955, 0.671697] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "Tell me the length with Péclet number for heat transfer of 10.5638, speed of 198.839 and thermal diffusivity of 0.671697.", + "a": "0.03568568130708054", + "valid": true, + "kind": "compute", + "expect": 0.03568568130708054, + "dtype": "quantity", + "unit": "m", + "why": "length for [10.563843, 198.838955, 0.671697] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "What is the length for Péclet number for heat transfer 21.7931, speed 29.5106 and thermal diffusivity 8.50341?", + "a": "6.279634237980215", + "valid": true, + "kind": "compute", + "expect": 6.279634237980215, + "dtype": "quantity", + "unit": "m", + "why": "length for [21.793126, 29.510609, 8.503407] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "Tell me the length with Péclet number for heat transfer of 21.7931, speed of 29.5106 and thermal diffusivity of 8.50341.", + "a": "6.279634237980215", + "valid": true, + "kind": "compute", + "expect": 6.279634237980215, + "dtype": "quantity", + "unit": "m", + "why": "length for [21.793126, 29.510609, 8.503407] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "What is the length for Péclet number for heat transfer 223.509, speed 17.3414 and thermal diffusivity 7.10705?", + "a": "91.60072422473009", + "valid": true, + "kind": "compute", + "expect": 91.60072422473009, + "dtype": "quantity", + "unit": "m", + "why": "length for [223.508796, 17.341435, 7.107049] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "Tell me the length with Péclet number for heat transfer of 223.509, speed of 17.3414 and thermal diffusivity of 7.10705.", + "a": "91.60072422473009", + "valid": true, + "kind": "compute", + "expect": 91.60072422473009, + "dtype": "quantity", + "unit": "m", + "why": "length for [223.508796, 17.341435, 7.107049] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "What is the length for Péclet number for heat transfer 107.961 and speed 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the thermal diffusivity is not given" + }, + { + "q": "Tell me the length with Péclet number for heat transfer of 85.2416 and speed of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the thermal diffusivity is not given" + }, + { + "q": "What is the 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 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 length with Péclet number for heat transfer of -45.6157, speed of 4.97131 and thermal diffusivity of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Péclet number for heat transfer: no example had one" + }, + { + "q": "Which 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 Péclet number for heat transfer is 17.7888, speed is 167.625 and the thermal diffusivity is 10.3291, what is the length?", + "a": "1.0961450581531387", + "valid": true, + "kind": "compute", + "expect": 1.0961450581531387, + "dtype": "quantity", + "unit": "m", + "why": "length for [17.788778, 167.624948, 10.329054] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "Could you work out the length given Péclet number for heat transfer = 17.7888, speed = 167.625, thermal diffusivity = 10.3291?", + "a": "1.0961450581531387", + "valid": true, + "kind": "compute", + "expect": 1.0961450581531387, + "dtype": "quantity", + "unit": "m", + "why": "length for [17.788778, 167.624948, 10.329054] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "I need the length for a Péclet number for heat transfer of 21.511, a speed of 173.394 and a thermal diffusivity of 251.084, please.", + "a": "31.149197612202414", + "valid": true, + "kind": "compute", + "expect": 31.149197612202414, + "dtype": "quantity", + "unit": "m", + "why": "length for [21.511016, 173.393638, 251.084037] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "If the Péclet number for heat transfer is 21.511, speed is 173.394 and the thermal diffusivity is 251.084, what is the length?", + "a": "31.149197612202414", + "valid": true, + "kind": "compute", + "expect": 31.149197612202414, + "dtype": "quantity", + "unit": "m", + "why": "length for [21.511016, 173.393638, 251.084037] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "Could you work out the length given Péclet number for heat transfer = 129.245, speed = 1.18359, thermal diffusivity = 15.9416?", + "a": "1740.7830274187056", + "valid": true, + "kind": "compute", + "expect": 1740.7830274187056, + "dtype": "quantity", + "unit": "m", + "why": "length for [129.244984, 1.18359, 15.941612] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "I need the length for a Péclet number for heat transfer of 129.245, a speed of 1.18359 and a thermal diffusivity of 15.9416, please.", + "a": "1740.7830274187056", + "valid": true, + "kind": "compute", + "expect": 1740.7830274187056, + "dtype": "quantity", + "unit": "m", + "why": "length for [129.244984, 1.18359, 15.941612] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "If the Péclet number for heat transfer is 20.3966, speed is 18.3169 and the thermal diffusivity is 6.8386, what is the length?", + "a": "7.615050191987504", + "valid": true, + "kind": "compute", + "expect": 7.615050191987504, + "dtype": "quantity", + "unit": "m", + "why": "length for [20.396558, 18.316867, 6.838598] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "Could you work out the length given Péclet number for heat transfer = 20.3966, speed = 18.3169, thermal diffusivity = 6.8386?", + "a": "7.615050191987504", + "valid": true, + "kind": "compute", + "expect": 7.615050191987504, + "dtype": "quantity", + "unit": "m", + "why": "length for [20.396558, 18.316867, 6.838598] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "I need the length for a Péclet number for heat transfer of 185.886, a speed of 13.2373 and a thermal diffusivity of 4.71941, please.", + "a": "66.27285731134774", + "valid": true, + "kind": "compute", + "expect": 66.27285731134774, + "dtype": "quantity", + "unit": "m", + "why": "length for [185.886273, 13.237311, 4.719415] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "If the Péclet number for heat transfer is 185.886, speed is 13.2373 and the thermal diffusivity is 4.71941, what is the length?", + "a": "66.27285731134774", + "valid": true, + "kind": "compute", + "expect": 66.27285731134774, + "dtype": "quantity", + "unit": "m", + "why": "length for [185.886273, 13.237311, 4.719415] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "Could you work out the length given Péclet number for heat transfer = 9.1061, speed = 24.5721, thermal diffusivity = 108.451?", + "a": "40.19049097830896", + "valid": true, + "kind": "compute", + "expect": 40.19049097830896, + "dtype": "quantity", + "unit": "m", + "why": "length for [9.106104, 24.572086, 108.450792] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "I need the length for a Péclet number for heat transfer of 9.1061, a speed of 24.5721 and a thermal diffusivity of 108.451, please.", + "a": "40.19049097830896", + "valid": true, + "kind": "compute", + "expect": 40.19049097830896, + "dtype": "quantity", + "unit": "m", + "why": "length for [9.106104, 24.572086, 108.450792] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "If the Péclet number for heat transfer is 17.7888 and the speed is 167.625, what is the length?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the thermal diffusivity is not given" + }, + { + "q": "Could you work out the length given Péclet number for heat transfer = 21.511, speed = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the thermal diffusivity is not given" + }, + { + "q": "Do you know the 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 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 length given Péclet number for heat transfer = -129.245, speed = 1.18359, thermal diffusivity = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Péclet number for heat transfer: no example had one" + }, + { + "q": "What is a typical 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-length-4/program.py b/root/organs10/physics/specialists/solved-length-4/program.py new file mode 100644 index 0000000000000000000000000000000000000000..bc74f03c852dc80eeb504d57776882897c8347e8 --- /dev/null +++ b/root/organs10/physics/specialists/solved-length-4/program.py @@ -0,0 +1,7 @@ +"""length from Péclet number for heat transfer, speed, thermal diffusivity (Péclet number for heat transfer (Wikidata Q899769)) +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(p_clet_number_for_heat_transfer, speed, thermal_diffusivity): + return 1.0*p_clet_number_for_heat_transfer*thermal_diffusivity/speed diff --git a/root/organs10/physics/specialists/solved-length-4/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-length-4/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-length-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-length-5/organ_formula_v2.py b/root/organs10/physics/specialists/solved-length-5/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-length-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-length-5/panel.json b/root/organs10/physics/specialists/solved-length-5/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..5f76956c1ef67e1721d02c3d40a551096a24ff06 --- /dev/null +++ b/root/organs10/physics/specialists/solved-length-5/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the length for magnetic flux density 107.961, force 59.4105 and electric current 5.77717?", + "a": "0.09525393194179609", + "valid": true, + "kind": "compute", + "expect": 0.09525393194179609, + "dtype": "quantity", + "unit": "m", + "why": "length for [107.960666, 59.4105, 5.777165] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Tell me the length with magnetic flux density of 107.961, force of 59.4105 and electric current of 5.77717.", + "a": "0.09525393194179609", + "valid": true, + "kind": "compute", + "expect": 0.09525393194179609, + "dtype": "quantity", + "unit": "m", + "why": "length for [107.960666, 59.4105, 5.777165] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "What is the length for magnetic flux density 85.2416, force 160.878 and electric current 2.69428?", + "a": "0.7004924122686708", + "valid": true, + "kind": "compute", + "expect": 0.7004924122686708, + "dtype": "quantity", + "unit": "m", + "why": "length for [85.241623, 160.878442, 2.69428] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Tell me the length with magnetic flux density of 85.2416, force of 160.878 and electric current of 2.69428.", + "a": "0.7004924122686708", + "valid": true, + "kind": "compute", + "expect": 0.7004924122686708, + "dtype": "quantity", + "unit": "m", + "why": "length for [85.241623, 160.878442, 2.69428] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "What is the length for magnetic flux density 45.6157, force 4.97131 and electric current 94.305?", + "a": "0.001155637526406431", + "valid": true, + "kind": "compute", + "expect": 0.001155637526406431, + "dtype": "quantity", + "unit": "m", + "why": "length for [45.61567, 4.971307, 94.305046] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Tell me the length with magnetic flux density of 45.6157, force of 4.97131 and electric current of 94.305.", + "a": "0.001155637526406431", + "valid": true, + "kind": "compute", + "expect": 0.001155637526406431, + "dtype": "quantity", + "unit": "m", + "why": "length for [45.61567, 4.971307, 94.305046] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "What is the length for magnetic flux density 10.5638, force 198.839 and electric current 0.671697?", + "a": "28.022443831038363", + "valid": true, + "kind": "compute", + "expect": 28.022443831038363, + "dtype": "quantity", + "unit": "m", + "why": "length for [10.563843, 198.838955, 0.671697] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Tell me the length with magnetic flux density of 10.5638, force of 198.839 and electric current of 0.671697.", + "a": "28.022443831038363", + "valid": true, + "kind": "compute", + "expect": 28.022443831038363, + "dtype": "quantity", + "unit": "m", + "why": "length for [10.563843, 198.838955, 0.671697] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "What is the length for magnetic flux density 21.7931, force 29.5106 and electric current 8.50341?", + "a": "0.15924494359111602", + "valid": true, + "kind": "compute", + "expect": 0.15924494359111602, + "dtype": "quantity", + "unit": "m", + "why": "length for [21.793126, 29.510609, 8.503407] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Tell me the length with magnetic flux density of 21.7931, force of 29.5106 and electric current of 8.50341.", + "a": "0.15924494359111602", + "valid": true, + "kind": "compute", + "expect": 0.15924494359111602, + "dtype": "quantity", + "unit": "m", + "why": "length for [21.793126, 29.510609, 8.503407] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "What is the length for magnetic flux density 223.509, force 17.3414 and electric current 7.10705?", + "a": "0.010916944254137492", + "valid": true, + "kind": "compute", + "expect": 0.010916944254137492, + "dtype": "quantity", + "unit": "m", + "why": "length for [223.508796, 17.341435, 7.107049] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Tell me the length with magnetic flux density of 223.509, force of 17.3414 and electric current of 7.10705.", + "a": "0.010916944254137492", + "valid": true, + "kind": "compute", + "expect": 0.010916944254137492, + "dtype": "quantity", + "unit": "m", + "why": "length for [223.508796, 17.341435, 7.107049] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "What is the length for magnetic flux density 107.961 and force 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current is not given" + }, + { + "q": "Tell me the length with magnetic flux density of 85.2416 and force of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current is not given" + }, + { + "q": "What is the 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 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 length with magnetic flux density of -45.6157, force of 4.97131 and electric current of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative magnetic flux density: no example had one" + }, + { + "q": "Which 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 magnetic flux density is 17.7888, force is 167.625 and the electric current is 10.3291, what is the length?", + "a": "0.9122880156800318", + "valid": true, + "kind": "compute", + "expect": 0.9122880156800318, + "dtype": "quantity", + "unit": "m", + "why": "length for [17.788778, 167.624948, 10.329054] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Could you work out the length given magnetic flux density = 17.7888, force = 167.625, electric current = 10.3291?", + "a": "0.9122880156800318", + "valid": true, + "kind": "compute", + "expect": 0.9122880156800318, + "dtype": "quantity", + "unit": "m", + "why": "length for [17.788778, 167.624948, 10.329054] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "I need the length for a magnetic flux density of 21.511, a force of 173.394 and an electric current of 251.084, please.", + "a": "0.032103555682225955", + "valid": true, + "kind": "compute", + "expect": 0.032103555682225955, + "dtype": "quantity", + "unit": "m", + "why": "length for [21.511016, 173.393638, 251.084037] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "If the magnetic flux density is 21.511, force is 173.394 and the electric current is 251.084, what is the length?", + "a": "0.032103555682225955", + "valid": true, + "kind": "compute", + "expect": 0.032103555682225955, + "dtype": "quantity", + "unit": "m", + "why": "length for [21.511016, 173.393638, 251.084037] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Could you work out the length given magnetic flux density = 129.245, force = 1.18359, electric current = 15.9416?", + "a": "0.0005744541302673633", + "valid": true, + "kind": "compute", + "expect": 0.0005744541302673633, + "dtype": "quantity", + "unit": "m", + "why": "length for [129.244984, 1.18359, 15.941612] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "I need the length for a magnetic flux density of 129.245, a force of 1.18359 and an electric current of 15.9416, please.", + "a": "0.0005744541302673633", + "valid": true, + "kind": "compute", + "expect": 0.0005744541302673633, + "dtype": "quantity", + "unit": "m", + "why": "length for [129.244984, 1.18359, 15.941612] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "If the magnetic flux density is 20.3966, force is 18.3169 and the electric current is 6.8386, what is the length?", + "a": "0.13131889807531302", + "valid": true, + "kind": "compute", + "expect": 0.13131889807531302, + "dtype": "quantity", + "unit": "m", + "why": "length for [20.396558, 18.316867, 6.838598] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Could you work out the length given magnetic flux density = 20.3966, force = 18.3169, electric current = 6.8386?", + "a": "0.13131889807531302", + "valid": true, + "kind": "compute", + "expect": 0.13131889807531302, + "dtype": "quantity", + "unit": "m", + "why": "length for [20.396558, 18.316867, 6.838598] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "I need the length for a magnetic flux density of 185.886, a force of 13.2373 and an electric current of 4.71941, please.", + "a": "0.015089133629806127", + "valid": true, + "kind": "compute", + "expect": 0.015089133629806127, + "dtype": "quantity", + "unit": "m", + "why": "length for [185.886273, 13.237311, 4.719415] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "If the magnetic flux density is 185.886, force is 13.2373 and the electric current is 4.71941, what is the length?", + "a": "0.015089133629806127", + "valid": true, + "kind": "compute", + "expect": 0.015089133629806127, + "dtype": "quantity", + "unit": "m", + "why": "length for [185.886273, 13.237311, 4.719415] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Could you work out the length given magnetic flux density = 9.1061, force = 24.5721, electric current = 108.451?", + "a": "0.024881507432683664", + "valid": true, + "kind": "compute", + "expect": 0.024881507432683664, + "dtype": "quantity", + "unit": "m", + "why": "length for [9.106104, 24.572086, 108.450792] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "I need the length for a magnetic flux density of 9.1061, a force of 24.5721 and an electric current of 108.451, please.", + "a": "0.024881507432683664", + "valid": true, + "kind": "compute", + "expect": 0.024881507432683664, + "dtype": "quantity", + "unit": "m", + "why": "length for [9.106104, 24.572086, 108.450792] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "If the magnetic flux density is 17.7888 and the force is 167.625, what is the length?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current is not given" + }, + { + "q": "Could you work out the length given magnetic flux density = 21.511, force = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current is not given" + }, + { + "q": "Do you know the 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 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 length given magnetic flux density = -129.245, force = 1.18359, electric current = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative magnetic flux density: no example had one" + }, + { + "q": "What is a typical 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-length-5/program.py b/root/organs10/physics/specialists/solved-length-5/program.py new file mode 100644 index 0000000000000000000000000000000000000000..58cccfa77843ba55b180cb66efb5715681b83969 --- /dev/null +++ b/root/organs10/physics/specialists/solved-length-5/program.py @@ -0,0 +1,7 @@ +"""length from magnetic flux density, force, electric current (magnetic field of test current (Wikidata Q104145274)) +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(magnetic_flux_density, force, electric_current): + return 1.0*force/(electric_current*magnetic_flux_density) diff --git a/root/organs10/physics/specialists/solved-length-5/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-length-5/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-length-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-length-6/organ_formula_v2.py b/root/organs10/physics/specialists/solved-length-6/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-length-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-length-6/panel.json b/root/organs10/physics/specialists/solved-length-6/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..2654f2cd2cc968d9e83c973afb4fbf05329fe51a --- /dev/null +++ b/root/organs10/physics/specialists/solved-length-6/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the length for Fourier number for mass transfer 107.961, mass diffusivity 59.4105 and duration 5.77717?", + "a": "1.7830199331543142", + "valid": true, + "kind": "compute", + "expect": 1.7830199331543142, + "dtype": "quantity", + "unit": "m", + "why": "length for [107.960666, 59.4105, 5.777165] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "Tell me the length with Fourier number for mass transfer of 107.961, mass diffusivity of 59.4105 and duration of 5.77717.", + "a": "1.7830199331543142", + "valid": true, + "kind": "compute", + "expect": 1.7830199331543142, + "dtype": "quantity", + "unit": "m", + "why": "length for [107.960666, 59.4105, 5.777165] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "What is the length for Fourier number for mass transfer 85.2416, mass diffusivity 160.878 and duration 2.69428?", + "a": "2.2549889893264097", + "valid": true, + "kind": "compute", + "expect": 2.2549889893264097, + "dtype": "quantity", + "unit": "m", + "why": "length for [85.241623, 160.878442, 2.69428] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "Tell me the length with Fourier number for mass transfer of 85.2416, mass diffusivity of 160.878 and duration of 2.69428.", + "a": "2.2549889893264097", + "valid": true, + "kind": "compute", + "expect": 2.2549889893264097, + "dtype": "quantity", + "unit": "m", + "why": "length for [85.241623, 160.878442, 2.69428] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "What is the length for Fourier number for mass transfer 45.6157, mass diffusivity 4.97131 and duration 94.305?", + "a": "3.2058688335739625", + "valid": true, + "kind": "compute", + "expect": 3.2058688335739625, + "dtype": "quantity", + "unit": "m", + "why": "length for [45.61567, 4.971307, 94.305046] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "Tell me the length with Fourier number for mass transfer of 45.6157, mass diffusivity of 4.97131 and duration of 94.305.", + "a": "3.2058688335739625", + "valid": true, + "kind": "compute", + "expect": 3.2058688335739625, + "dtype": "quantity", + "unit": "m", + "why": "length for [45.61567, 4.971307, 94.305046] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "What is the length for Fourier number for mass transfer 10.5638, mass diffusivity 198.839 and duration 0.671697?", + "a": "3.5557117055734495", + "valid": true, + "kind": "compute", + "expect": 3.5557117055734495, + "dtype": "quantity", + "unit": "m", + "why": "length for [10.563843, 198.838955, 0.671697] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "Tell me the length with Fourier number for mass transfer of 10.5638, mass diffusivity of 198.839 and duration of 0.671697.", + "a": "3.5557117055734495", + "valid": true, + "kind": "compute", + "expect": 3.5557117055734495, + "dtype": "quantity", + "unit": "m", + "why": "length for [10.563843, 198.838955, 0.671697] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "What is the length for Fourier number for mass transfer 21.7931, mass diffusivity 29.5106 and duration 8.50341?", + "a": "3.3933276952741207", + "valid": true, + "kind": "compute", + "expect": 3.3933276952741207, + "dtype": "quantity", + "unit": "m", + "why": "length for [21.793126, 29.510609, 8.503407] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "Tell me the length with Fourier number for mass transfer of 21.7931, mass diffusivity of 29.5106 and duration of 8.50341.", + "a": "3.3933276952741207", + "valid": true, + "kind": "compute", + "expect": 3.3933276952741207, + "dtype": "quantity", + "unit": "m", + "why": "length for [21.793126, 29.510609, 8.503407] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "What is the length for Fourier number for mass transfer 223.509, mass diffusivity 17.3414 and duration 7.10705?", + "a": "0.7425742174289913", + "valid": true, + "kind": "compute", + "expect": 0.7425742174289913, + "dtype": "quantity", + "unit": "m", + "why": "length for [223.508796, 17.341435, 7.107049] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "Tell me the length with Fourier number for mass transfer of 223.509, mass diffusivity of 17.3414 and duration of 7.10705.", + "a": "0.7425742174289913", + "valid": true, + "kind": "compute", + "expect": 0.7425742174289913, + "dtype": "quantity", + "unit": "m", + "why": "length for [223.508796, 17.341435, 7.107049] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "What is the length for Fourier number for mass transfer 107.961 and mass diffusivity 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the duration is not given" + }, + { + "q": "Tell me the length with Fourier number for mass transfer of 85.2416 and mass diffusivity of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the duration is not given" + }, + { + "q": "What is the 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 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 length with Fourier number for mass transfer of -45.6157, mass diffusivity of 4.97131 and duration of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Fourier number for mass transfer: no example had one" + }, + { + "q": "Which 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 Fourier number for mass transfer is 17.7888, mass diffusivity is 167.625 and the duration is 10.3291, what is the length?", + "a": "9.865669269087086", + "valid": true, + "kind": "compute", + "expect": 9.865669269087086, + "dtype": "quantity", + "unit": "m", + "why": "length for [17.788778, 167.624948, 10.329054] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "Could you work out the length given Fourier number for mass transfer = 17.7888, mass diffusivity = 167.625, duration = 10.3291?", + "a": "9.865669269087086", + "valid": true, + "kind": "compute", + "expect": 9.865669269087086, + "dtype": "quantity", + "unit": "m", + "why": "length for [17.788778, 167.624948, 10.329054] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "I need the length for a Fourier number for mass transfer of 21.511, a mass diffusivity of 173.394 and a duration of 251.084, please.", + "a": "44.987894844967585", + "valid": true, + "kind": "compute", + "expect": 44.987894844967585, + "dtype": "quantity", + "unit": "m", + "why": "length for [21.511016, 173.393638, 251.084037] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "If the Fourier number for mass transfer is 21.511, mass diffusivity is 173.394 and the duration is 251.084, what is the length?", + "a": "44.987894844967585", + "valid": true, + "kind": "compute", + "expect": 44.987894844967585, + "dtype": "quantity", + "unit": "m", + "why": "length for [21.511016, 173.393638, 251.084037] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "Could you work out the length given Fourier number for mass transfer = 129.245, mass diffusivity = 1.18359, duration = 15.9416?", + "a": "0.3820849222738104", + "valid": true, + "kind": "compute", + "expect": 0.3820849222738104, + "dtype": "quantity", + "unit": "m", + "why": "length for [129.244984, 1.18359, 15.941612] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "I need the length for a Fourier number for mass transfer of 129.245, a mass diffusivity of 1.18359 and a duration of 15.9416, please.", + "a": "0.3820849222738104", + "valid": true, + "kind": "compute", + "expect": 0.3820849222738104, + "dtype": "quantity", + "unit": "m", + "why": "length for [129.244984, 1.18359, 15.941612] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "If the Fourier number for mass transfer is 20.3966, mass diffusivity is 18.3169 and the duration is 6.8386, what is the length?", + "a": "2.478167708976587", + "valid": true, + "kind": "compute", + "expect": 2.478167708976587, + "dtype": "quantity", + "unit": "m", + "why": "length for [20.396558, 18.316867, 6.838598] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "Could you work out the length given Fourier number for mass transfer = 20.3966, mass diffusivity = 18.3169, duration = 6.8386?", + "a": "2.478167708976587", + "valid": true, + "kind": "compute", + "expect": 2.478167708976587, + "dtype": "quantity", + "unit": "m", + "why": "length for [20.396558, 18.316867, 6.838598] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "I need the length for a Fourier number for mass transfer of 185.886, a mass diffusivity of 13.2373 and a duration of 4.71941, please.", + "a": "0.5797226957580199", + "valid": true, + "kind": "compute", + "expect": 0.5797226957580199, + "dtype": "quantity", + "unit": "m", + "why": "length for [185.886273, 13.237311, 4.719415] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "If the Fourier number for mass transfer is 185.886, mass diffusivity is 13.2373 and the duration is 4.71941, what is the length?", + "a": "0.5797226957580199", + "valid": true, + "kind": "compute", + "expect": 0.5797226957580199, + "dtype": "quantity", + "unit": "m", + "why": "length for [185.886273, 13.237311, 4.719415] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "Could you work out the length given Fourier number for mass transfer = 9.1061, mass diffusivity = 24.5721, duration = 108.451?", + "a": "17.1068903294115", + "valid": true, + "kind": "compute", + "expect": 17.1068903294115, + "dtype": "quantity", + "unit": "m", + "why": "length for [9.106104, 24.572086, 108.450792] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "I need the length for a Fourier number for mass transfer of 9.1061, a mass diffusivity of 24.5721 and a duration of 108.451, please.", + "a": "17.1068903294115", + "valid": true, + "kind": "compute", + "expect": 17.1068903294115, + "dtype": "quantity", + "unit": "m", + "why": "length for [9.106104, 24.572086, 108.450792] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "If the Fourier number for mass transfer is 17.7888 and the mass diffusivity is 167.625, what is the length?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the duration is not given" + }, + { + "q": "Could you work out the length given Fourier number for mass transfer = 21.511, mass diffusivity = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the duration is not given" + }, + { + "q": "Do you know the 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 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 length given Fourier number for mass transfer = -129.245, mass diffusivity = 1.18359, duration = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Fourier number for mass transfer: no example had one" + }, + { + "q": "What is a typical 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-length-6/program.py b/root/organs10/physics/specialists/solved-length-6/program.py new file mode 100644 index 0000000000000000000000000000000000000000..de62898a5863fc16a9c5e9752ff7206313d62e29 --- /dev/null +++ b/root/organs10/physics/specialists/solved-length-6/program.py @@ -0,0 +1,7 @@ +"""length from Fourier number for mass transfer, mass diffusivity, duration (Fourier number for mass transfer (Wikidata Q104542186)) +The maths organ (systems.py): solved from the system q0 = ((q1*q2)/(q3 **(2))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(fourier_number_for_mass_transfer, mass_diffusivity, duration): + return 1.0*duration**0.5*fourier_number_for_mass_transfer**(-0.5)*mass_diffusivity**0.5 diff --git a/root/organs10/physics/specialists/solved-length-6/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-length-6/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-length-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-length/organ_formula_v2.py b/root/organs10/physics/specialists/solved-length/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-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-length/panel.json b/root/organs10/physics/specialists/solved-length/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..93114fac7b4fe2940ae307a3c7b5cafb6a8e83b0 --- /dev/null +++ b/root/organs10/physics/specialists/solved-length/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the length for area 107.961 and width 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m", + "why": "length for [107.960666, 59.4105] (the system Rectangle)" + }, + { + "q": "Tell me the length with area of 107.961 and width of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m", + "why": "length for [107.960666, 59.4105] (the system Rectangle)" + }, + { + "q": "What is the length for area 169.527 and width 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m", + "why": "length for [169.526958, 280.773702] (the system Rectangle)" + }, + { + "q": "Tell me the length with area of 169.527 and width of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m", + "why": "length for [169.526958, 280.773702] (the system Rectangle)" + }, + { + "q": "What is the length for area 1.91195 and width 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m", + "why": "length for [1.911952, 82.240072] (the system Rectangle)" + }, + { + "q": "Tell me the length with area of 1.91195 and width of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m", + "why": "length for [1.911952, 82.240072] (the system Rectangle)" + }, + { + "q": "What is the length for area 1.64379 and width 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m", + "why": "length for [1.643795, 252.709024] (the system Rectangle)" + }, + { + "q": "Tell me the length with area of 1.64379 and width of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m", + "why": "length for [1.643795, 252.709024] (the system Rectangle)" + }, + { + "q": "What is the length for area 18.3169 and width 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m", + "why": "length for [18.316867, 6.838598] (the system Rectangle)" + }, + { + "q": "Tell me the length with area of 18.3169 and width of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m", + "why": "length for [18.316867, 6.838598] (the system Rectangle)" + }, + { + "q": "What is the length for area 156.502 and width 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m", + "why": "length for [156.50181, 185.886273] (the system Rectangle)" + }, + { + "q": "Tell me the length with area of 156.502 and width of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m", + "why": "length for [156.50181, 185.886273] (the system Rectangle)" + }, + { + "q": "What is the length for area 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the width is not given" + }, + { + "q": "Tell me the length with area of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the width is not given" + }, + { + "q": "What is the 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 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 length with area of -1.91195 and width of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative area: no example had one" + }, + { + "q": "Which 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 area is 8.50728 and the width is 17.7888, what is the length?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m", + "why": "length for [8.507283, 17.788778] (the system Rectangle)" + }, + { + "q": "Could you work out the length given area = 8.50728, width = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m", + "why": "length for [8.507283, 17.788778] (the system Rectangle)" + }, + { + "q": "I need the length for an area of 8.24951 and a width of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m", + "why": "length for [8.24951, 0.634021] (the system Rectangle)" + }, + { + "q": "If the area is 8.24951 and the width is 0.634021, what is the length?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m", + "why": "length for [8.24951, 0.634021] (the system Rectangle)" + }, + { + "q": "Could you work out the length given area = 0.318734, width = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m", + "why": "length for [0.318734, 9.566071] (the system Rectangle)" + }, + { + "q": "I need the length for an area of 0.318734 and a width of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m", + "why": "length for [0.318734, 9.566071] (the system Rectangle)" + }, + { + "q": "If the area is 0.671697 and the width is 14.2496, what is the length?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m", + "why": "length for [0.671697, 14.249569] (the system Rectangle)" + }, + { + "q": "Could you work out the length given area = 0.671697, width = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m", + "why": "length for [0.671697, 14.249569] (the system Rectangle)" + }, + { + "q": "I need the length for an area of 21.7931 and a width of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m", + "why": "length for [21.793126, 29.510609] (the system Rectangle)" + }, + { + "q": "If the area is 21.7931 and the width is 29.5106, what is the length?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m", + "why": "length for [21.793126, 29.510609] (the system Rectangle)" + }, + { + "q": "Could you work out the length given area = 153.15, width = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m", + "why": "length for [153.149965, 18.595315] (the system Rectangle)" + }, + { + "q": "I need the length for an area of 153.15 and a width of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m", + "why": "length for [153.149965, 18.595315] (the system Rectangle)" + }, + { + "q": "If the area is 8.50728, what is the length?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the width is not given" + }, + { + "q": "Could you work out the length given area = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the width is not given" + }, + { + "q": "Do you know the 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 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 length given area = -0.318734, width = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative area: no example had one" + }, + { + "q": "What is a typical 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-length/program.py b/root/organs10/physics/specialists/solved-length/program.py new file mode 100644 index 0000000000000000000000000000000000000000..2bd7e4fba07ab7344d069f018f97057483a5d500 --- /dev/null +++ b/root/organs10/physics/specialists/solved-length/program.py @@ -0,0 +1,7 @@ +"""length from area, width (Rectangle) +The maths organ (systems.py): solved from the system A = l*w; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(area, width): + return 1.0*area/width diff --git a/root/organs10/physics/specialists/solved-length/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-length/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-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-lethargy/organ_formula_v2.py b/root/organs10/physics/specialists/solved-lethargy/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-lethargy/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-lethargy/panel.json b/root/organs10/physics/specialists/solved-lethargy/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..dc4e1b29f66cc90f8dea5b4490562b59849c9887 --- /dev/null +++ b/root/organs10/physics/specialists/solved-lethargy/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the lethargy for reference energy 107.961 and neutron kinetic energy 59.4105?", + "a": "0.5972959744213924", + "valid": true, + "kind": "compute", + "expect": 0.5972959744213924, + "dtype": "quantity", + "unit": "1", + "why": "lethargy for [107.960666, 59.4105] (the system lethargy (Wikidata Q25508781))" + }, + { + "q": "Tell me the lethargy with reference energy of 107.961 and neutron kinetic energy of 59.4105.", + "a": "0.5972959744213924", + "valid": true, + "kind": "compute", + "expect": 0.5972959744213924, + "dtype": "quantity", + "unit": "1", + "why": "lethargy for [107.960666, 59.4105] (the system lethargy (Wikidata Q25508781))" + }, + { + "q": "What is the lethargy for reference energy 157.223 and neutron kinetic energy 35.6412?", + "a": "1.4841613122343253", + "valid": true, + "kind": "compute", + "expect": 1.4841613122343253, + "dtype": "quantity", + "unit": "1", + "why": "lethargy for [157.22256, 35.641157] (the system lethargy (Wikidata Q25508781))" + }, + { + "q": "Tell me the lethargy with reference energy of 157.223 and neutron kinetic energy of 35.6412.", + "a": "1.4841613122343253", + "valid": true, + "kind": "compute", + "expect": 1.4841613122343253, + "dtype": "quantity", + "unit": "1", + "why": "lethargy for [157.22256, 35.641157] (the system lethargy (Wikidata Q25508781))" + }, + { + "q": "What is the lethargy for reference energy 126.709 and neutron kinetic energy 1.70503?", + "a": "4.3083110050515625", + "valid": true, + "kind": "compute", + "expect": 4.3083110050515625, + "dtype": "quantity", + "unit": "1", + "why": "lethargy for [126.709252, 1.705032] (the system lethargy (Wikidata Q25508781))" + }, + { + "q": "Tell me the lethargy with reference energy of 126.709 and neutron kinetic energy of 1.70503.", + "a": "4.3083110050515625", + "valid": true, + "kind": "compute", + "expect": 4.3083110050515625, + "dtype": "quantity", + "unit": "1", + "why": "lethargy for [126.709252, 1.705032] (the system lethargy (Wikidata Q25508781))" + }, + { + "q": "What is the lethargy for reference energy 41.6566 and neutron kinetic energy 13.8779?", + "a": "1.0991654637733572", + "valid": true, + "kind": "compute", + "expect": 1.0991654637733572, + "dtype": "quantity", + "unit": "1", + "why": "lethargy for [41.656639, 13.877867] (the system lethargy (Wikidata Q25508781))" + }, + { + "q": "Tell me the lethargy with reference energy of 41.6566 and neutron kinetic energy of 13.8779.", + "a": "1.0991654637733572", + "valid": true, + "kind": "compute", + "expect": 1.0991654637733572, + "dtype": "quantity", + "unit": "1", + "why": "lethargy for [41.656639, 13.877867] (the system lethargy (Wikidata Q25508781))" + }, + { + "q": "What is the lethargy for reference energy 8.50341 and neutron kinetic energy 0.58813?", + "a": "2.671274770421334", + "valid": true, + "kind": "compute", + "expect": 2.671274770421334, + "dtype": "quantity", + "unit": "1", + "why": "lethargy for [8.503407, 0.58813] (the system lethargy (Wikidata Q25508781))" + }, + { + "q": "Tell me the lethargy with reference energy of 8.50341 and neutron kinetic energy of 0.58813.", + "a": "2.671274770421334", + "valid": true, + "kind": "compute", + "expect": 2.671274770421334, + "dtype": "quantity", + "unit": "1", + "why": "lethargy for [8.503407, 0.58813] (the system lethargy (Wikidata Q25508781))" + }, + { + "q": "What is the lethargy for reference energy 223.509 and neutron kinetic energy 17.3414?", + "a": "2.5563520461834144", + "valid": true, + "kind": "compute", + "expect": 2.5563520461834144, + "dtype": "quantity", + "unit": "1", + "why": "lethargy for [223.508796, 17.341435] (the system lethargy (Wikidata Q25508781))" + }, + { + "q": "Tell me the lethargy with reference energy of 223.509 and neutron kinetic energy of 17.3414.", + "a": "2.5563520461834144", + "valid": true, + "kind": "compute", + "expect": 2.5563520461834144, + "dtype": "quantity", + "unit": "1", + "why": "lethargy for [223.508796, 17.341435] (the system lethargy (Wikidata Q25508781))" + }, + { + "q": "What is the lethargy for reference energy 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the neutron kinetic energy is not given" + }, + { + "q": "Tell me the lethargy with reference energy of 157.223.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the neutron kinetic energy is not given" + }, + { + "q": "What is the lethargy 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 lethargy 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 lethargy with reference energy of -126.709 and neutron kinetic energy of 1.70503.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative reference energy: no example had one" + }, + { + "q": "Which lethargy 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 reference energy is 167.625 and the neutron kinetic energy is 10.3291, what is the lethargy?", + "a": "2.7867682841143013", + "valid": true, + "kind": "compute", + "expect": 2.7867682841143013, + "dtype": "quantity", + "unit": "1", + "why": "lethargy for [167.624948, 10.329054] (the system lethargy (Wikidata Q25508781))" + }, + { + "q": "Could you work out the lethargy given reference energy = 167.625, neutron kinetic energy = 10.3291?", + "a": "2.7867682841143013", + "valid": true, + "kind": "compute", + "expect": 2.7867682841143013, + "dtype": "quantity", + "unit": "1", + "why": "lethargy for [167.624948, 10.329054] (the system lethargy (Wikidata Q25508781))" + }, + { + "q": "I need the lethargy for a reference energy of 13.999 and a neutron kinetic energy of 0.348438, please.", + "a": "3.6932838842102296", + "valid": true, + "kind": "compute", + "expect": 3.6932838842102296, + "dtype": "quantity", + "unit": "1", + "why": "lethargy for [13.999029, 0.348438] (the system lethargy (Wikidata Q25508781))" + }, + { + "q": "If the reference energy is 13.999 and the neutron kinetic energy is 0.348438, what is the lethargy?", + "a": "3.6932838842102296", + "valid": true, + "kind": "compute", + "expect": 3.6932838842102296, + "dtype": "quantity", + "unit": "1", + "why": "lethargy for [13.999029, 0.348438] (the system lethargy (Wikidata Q25508781))" + }, + { + "q": "Could you work out the lethargy given reference energy = 81.1967, neutron kinetic energy = 6.98086?", + "a": "2.4537030469806664", + "valid": true, + "kind": "compute", + "expect": 2.4537030469806664, + "dtype": "quantity", + "unit": "1", + "why": "lethargy for [81.196705, 6.980857] (the system lethargy (Wikidata Q25508781))" + }, + { + "q": "I need the lethargy for a reference energy of 81.1967 and a neutron kinetic energy of 6.98086, please.", + "a": "2.4537030469806664", + "valid": true, + "kind": "compute", + "expect": 2.4537030469806664, + "dtype": "quantity", + "unit": "1", + "why": "lethargy for [81.196705, 6.980857] (the system lethargy (Wikidata Q25508781))" + }, + { + "q": "If the reference energy is 19.4474 and the neutron kinetic energy is 4.51548, what is the lethargy?", + "a": "1.460200792909474", + "valid": true, + "kind": "compute", + "expect": 1.460200792909474, + "dtype": "quantity", + "unit": "1", + "why": "lethargy for [19.447357, 4.515475] (the system lethargy (Wikidata Q25508781))" + }, + { + "q": "Could you work out the lethargy given reference energy = 19.4474, neutron kinetic energy = 4.51548?", + "a": "1.460200792909474", + "valid": true, + "kind": "compute", + "expect": 1.460200792909474, + "dtype": "quantity", + "unit": "1", + "why": "lethargy for [19.447357, 4.515475] (the system lethargy (Wikidata Q25508781))" + }, + { + "q": "I need the lethargy for a reference energy of 13.2373 and a neutron kinetic energy of 4.71941, please.", + "a": "1.0313546287241806", + "valid": true, + "kind": "compute", + "expect": 1.0313546287241806, + "dtype": "quantity", + "unit": "1", + "why": "lethargy for [13.237311, 4.719415] (the system lethargy (Wikidata Q25508781))" + }, + { + "q": "If the reference energy is 13.2373 and the neutron kinetic energy is 4.71941, what is the lethargy?", + "a": "1.0313546287241806", + "valid": true, + "kind": "compute", + "expect": 1.0313546287241806, + "dtype": "quantity", + "unit": "1", + "why": "lethargy for [13.237311, 4.719415] (the system lethargy (Wikidata Q25508781))" + }, + { + "q": "Could you work out the lethargy given reference energy = 21.7946, neutron kinetic energy = 9.1061?", + "a": "0.8727182246332803", + "valid": true, + "kind": "compute", + "expect": 0.8727182246332803, + "dtype": "quantity", + "unit": "1", + "why": "lethargy for [21.794621, 9.106104] (the system lethargy (Wikidata Q25508781))" + }, + { + "q": "I need the lethargy for a reference energy of 21.7946 and a neutron kinetic energy of 9.1061, please.", + "a": "0.8727182246332803", + "valid": true, + "kind": "compute", + "expect": 0.8727182246332803, + "dtype": "quantity", + "unit": "1", + "why": "lethargy for [21.794621, 9.106104] (the system lethargy (Wikidata Q25508781))" + }, + { + "q": "If the reference energy is 167.625, what is the lethargy?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the neutron kinetic energy is not given" + }, + { + "q": "Could you work out the lethargy given reference energy = 13.999?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the neutron kinetic energy is not given" + }, + { + "q": "Do you know the lethargy 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 lethargy 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 lethargy given reference energy = -81.1967, neutron kinetic energy = 6.98086?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative reference energy: no example had one" + }, + { + "q": "What is a typical lethargy?", + "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-lethargy/program.py b/root/organs10/physics/specialists/solved-lethargy/program.py new file mode 100644 index 0000000000000000000000000000000000000000..fe82b5b2973b4e4febac2f872a79e9344d722afa --- /dev/null +++ b/root/organs10/physics/specialists/solved-lethargy/program.py @@ -0,0 +1,7 @@ +"""lethargy from reference energy, neutron kinetic energy (lethargy (Wikidata Q25508781)) +The maths organ (systems.py): solved from the system q1 = log (((q0)/(q2))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(reference_energy, neutron_kinetic_energy): + return -1.0*math.log(neutron_kinetic_energy) + 1.0*math.log(reference_energy) diff --git a/root/organs10/physics/specialists/solved-lethargy/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-lethargy/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-lethargy/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-lever-arm/organ_formula_v2.py b/root/organs10/physics/specialists/solved-lever-arm/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-lever-arm/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-lever-arm/panel.json b/root/organs10/physics/specialists/solved-lever-arm/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..4baa7f091cbe2e36d4ecb88f20596bc5b0b9fcd7 --- /dev/null +++ b/root/organs10/physics/specialists/solved-lever-arm/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the lever arm for torque 107.961 and force 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m", + "why": "lever arm for [107.960666, 59.4105] (the system Torque)" + }, + { + "q": "Tell me the lever arm with torque of 107.961 and force of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m", + "why": "lever arm for [107.960666, 59.4105] (the system Torque)" + }, + { + "q": "What is the lever arm for torque 169.527 and force 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m", + "why": "lever arm for [169.526958, 280.773702] (the system Torque)" + }, + { + "q": "Tell me the lever arm with torque of 169.527 and force of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m", + "why": "lever arm for [169.526958, 280.773702] (the system Torque)" + }, + { + "q": "What is the lever arm for torque 1.91195 and force 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m", + "why": "lever arm for [1.911952, 82.240072] (the system Torque)" + }, + { + "q": "Tell me the lever arm with torque of 1.91195 and force of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m", + "why": "lever arm for [1.911952, 82.240072] (the system Torque)" + }, + { + "q": "What is the lever arm for torque 1.64379 and force 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m", + "why": "lever arm for [1.643795, 252.709024] (the system Torque)" + }, + { + "q": "Tell me the lever arm with torque of 1.64379 and force of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m", + "why": "lever arm for [1.643795, 252.709024] (the system Torque)" + }, + { + "q": "What is the lever arm for torque 18.3169 and force 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m", + "why": "lever arm for [18.316867, 6.838598] (the system Torque)" + }, + { + "q": "Tell me the lever arm with torque of 18.3169 and force of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m", + "why": "lever arm for [18.316867, 6.838598] (the system Torque)" + }, + { + "q": "What is the lever arm for torque 156.502 and force 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m", + "why": "lever arm for [156.50181, 185.886273] (the system Torque)" + }, + { + "q": "Tell me the lever arm with torque of 156.502 and force of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m", + "why": "lever arm for [156.50181, 185.886273] (the system Torque)" + }, + { + "q": "What is the lever arm for torque 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the force is not given" + }, + { + "q": "Tell me the lever arm with torque of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the force is not given" + }, + { + "q": "What is the lever arm 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 lever arm 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 lever arm with torque of -1.91195 and force of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative torque: no example had one" + }, + { + "q": "Which lever arm 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 torque is 8.50728 and the force is 17.7888, what is the lever arm?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m", + "why": "lever arm for [8.507283, 17.788778] (the system Torque)" + }, + { + "q": "Could you work out the lever arm given torque = 8.50728, force = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m", + "why": "lever arm for [8.507283, 17.788778] (the system Torque)" + }, + { + "q": "I need the lever arm for a torque 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": "lever arm for [8.24951, 0.634021] (the system Torque)" + }, + { + "q": "If the torque is 8.24951 and the force is 0.634021, what is the lever arm?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m", + "why": "lever arm for [8.24951, 0.634021] (the system Torque)" + }, + { + "q": "Could you work out the lever arm given torque = 0.318734, force = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m", + "why": "lever arm for [0.318734, 9.566071] (the system Torque)" + }, + { + "q": "I need the lever arm for a torque 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": "lever arm for [0.318734, 9.566071] (the system Torque)" + }, + { + "q": "If the torque is 0.671697 and the force is 14.2496, what is the lever arm?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m", + "why": "lever arm for [0.671697, 14.249569] (the system Torque)" + }, + { + "q": "Could you work out the lever arm given torque = 0.671697, force = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m", + "why": "lever arm for [0.671697, 14.249569] (the system Torque)" + }, + { + "q": "I need the lever arm for a torque 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": "lever arm for [21.793126, 29.510609] (the system Torque)" + }, + { + "q": "If the torque is 21.7931 and the force is 29.5106, what is the lever arm?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m", + "why": "lever arm for [21.793126, 29.510609] (the system Torque)" + }, + { + "q": "Could you work out the lever arm given torque = 153.15, force = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m", + "why": "lever arm for [153.149965, 18.595315] (the system Torque)" + }, + { + "q": "I need the lever arm for a torque 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": "lever arm for [153.149965, 18.595315] (the system Torque)" + }, + { + "q": "If the torque is 8.50728, what is the lever arm?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the force is not given" + }, + { + "q": "Could you work out the lever arm given torque = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the force is not given" + }, + { + "q": "Do you know the lever arm 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 lever arm 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 lever arm given torque = -0.318734, force = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative torque: no example had one" + }, + { + "q": "What is a typical lever arm?", + "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-lever-arm/program.py b/root/organs10/physics/specialists/solved-lever-arm/program.py new file mode 100644 index 0000000000000000000000000000000000000000..57731d1aaaf0cfc07027116ad19d1c10482e23d6 --- /dev/null +++ b/root/organs10/physics/specialists/solved-lever-arm/program.py @@ -0,0 +1,7 @@ +"""lever arm from torque, force (Torque) +The maths organ (systems.py): solved from the system M = F*r; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(torque, force): + return 1.0*torque/force diff --git a/root/organs10/physics/specialists/solved-lever-arm/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-lever-arm/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-lever-arm/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-light-distance/organ_formula_v2.py b/root/organs10/physics/specialists/solved-light-distance/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-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-light-distance/panel.json b/root/organs10/physics/specialists/solved-light-distance/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..b750395c7e21ca533518805ad08e2eae45804b19 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-distance/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the light distance for light time 107.961?", + "a": "32365793394.46847", + "valid": true, + "kind": "compute", + "expect": 32365793394.46847, + "dtype": "quantity", + "unit": "m", + "why": "light distance for [107.960666] (the system Light travel)" + }, + { + "q": "Tell me the light distance with light time of 107.961.", + "a": "32365793394.46847", + "valid": true, + "kind": "compute", + "expect": 32365793394.46847, + "dtype": "quantity", + "unit": "m", + "why": "light distance for [107.960666] (the system Light travel)" + }, + { + "q": "What is the light distance for light time 22.652?", + "a": "6790905592.941433", + "valid": true, + "kind": "compute", + "expect": 6790905592.941433, + "dtype": "quantity", + "unit": "m", + "why": "light distance for [22.652023] (the system Light travel)" + }, + { + "q": "Tell me the light distance with light time of 22.652.", + "a": "6790905592.941433", + "valid": true, + "kind": "compute", + "expect": 6790905592.941433, + "dtype": "quantity", + "unit": "m", + "why": "light distance for [22.652023] (the system Light travel)" + }, + { + "q": "What is the light distance for light time 173.394?", + "a": "51982105059.08884", + "valid": true, + "kind": "compute", + "expect": 51982105059.08884, + "dtype": "quantity", + "unit": "m", + "why": "light distance for [173.393638] (the system Light travel)" + }, + { + "q": "Tell me the light distance with light time of 173.394.", + "a": "51982105059.08884", + "valid": true, + "kind": "compute", + "expect": 51982105059.08884, + "dtype": "quantity", + "unit": "m", + "why": "light distance for [173.393638] (the system Light travel)" + }, + { + "q": "What is the light distance for light time 126.709?", + "a": "37986478048.5176", + "valid": true, + "kind": "compute", + "expect": 37986478048.5176, + "dtype": "quantity", + "unit": "m", + "why": "light distance for [126.709252] (the system Light travel)" + }, + { + "q": "Tell me the light distance with light time of 126.709.", + "a": "37986478048.5176", + "valid": true, + "kind": "compute", + "expect": 37986478048.5176, + "dtype": "quantity", + "unit": "m", + "why": "light distance for [126.709252] (the system Light travel)" + }, + { + "q": "What is the light distance for light time 0.671697?", + "a": "201369754.55801836", + "valid": true, + "kind": "compute", + "expect": 201369754.55801836, + "dtype": "quantity", + "unit": "m", + "why": "light distance for [0.671697] (the system Light travel)" + }, + { + "q": "Tell me the light distance with light time of 0.671697.", + "a": "201369754.55801836", + "valid": true, + "kind": "compute", + "expect": 201369754.55801836, + "dtype": "quantity", + "unit": "m", + "why": "light distance for [0.671697] (the system Light travel)" + }, + { + "q": "What is the light distance for light time 1.16997?", + "a": "350747584.2934841", + "valid": true, + "kind": "compute", + "expect": 350747584.2934841, + "dtype": "quantity", + "unit": "m", + "why": "light distance for [1.169968] (the system Light travel)" + }, + { + "q": "Tell me the light distance with light time of 1.16997.", + "a": "350747584.2934841", + "valid": true, + "kind": "compute", + "expect": 350747584.2934841, + "dtype": "quantity", + "unit": "m", + "why": "light distance for [1.169968] (the system Light travel)" + }, + { + "q": "What is the light distance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the light time is not given" + }, + { + "q": "Could you work out the light distance, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the light time is not given" + }, + { + "q": "What is the light 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 light 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 light distance with light time of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative light time: no example had one" + }, + { + "q": "Which light 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 light time is 2.57003, what is the light distance?", + "a": "770475584.8428262", + "valid": true, + "kind": "compute", + "expect": 770475584.8428262, + "dtype": "quantity", + "unit": "m", + "why": "light distance for [2.57003] (the system Light travel)" + }, + { + "q": "Could you work out the light distance given light time = 2.57003?", + "a": "770475584.8428262", + "valid": true, + "kind": "compute", + "expect": 770475584.8428262, + "dtype": "quantity", + "unit": "m", + "why": "light distance for [2.57003] (the system Light travel)" + }, + { + "q": "I need the light distance for a light time of 48.921, please.", + "a": "14666144097.718481", + "valid": true, + "kind": "compute", + "expect": 14666144097.718481, + "dtype": "quantity", + "unit": "m", + "why": "light distance for [48.920991] (the system Light travel)" + }, + { + "q": "If the light time is 48.921, what is the light distance?", + "a": "14666144097.718481", + "valid": true, + "kind": "compute", + "expect": 14666144097.718481, + "dtype": "quantity", + "unit": "m", + "why": "light distance for [48.920991] (the system Light travel)" + }, + { + "q": "Could you work out the light distance given light time = 0.348438?", + "a": "104458985.4381788", + "valid": true, + "kind": "compute", + "expect": 104458985.4381788, + "dtype": "quantity", + "unit": "m", + "why": "light distance for [0.348438] (the system Light travel)" + }, + { + "q": "I need the light distance for a light time of 0.348438, please.", + "a": "104458985.4381788", + "valid": true, + "kind": "compute", + "expect": 104458985.4381788, + "dtype": "quantity", + "unit": "m", + "why": "light distance for [0.348438] (the system Light travel)" + }, + { + "q": "If the light time is 252.709, what is the light distance?", + "a": "75760259538.54745", + "valid": true, + "kind": "compute", + "expect": 75760259538.54745, + "dtype": "quantity", + "unit": "m", + "why": "light distance for [252.709024] (the system Light travel)" + }, + { + "q": "Could you work out the light distance given light time = 252.709?", + "a": "75760259538.54745", + "valid": true, + "kind": "compute", + "expect": 75760259538.54745, + "dtype": "quantity", + "unit": "m", + "why": "light distance for [252.709024] (the system Light travel)" + }, + { + "q": "I need the light distance for a light time of 20.3966, please.", + "a": "6114734246.156489", + "valid": true, + "kind": "compute", + "expect": 6114734246.156489, + "dtype": "quantity", + "unit": "m", + "why": "light distance for [20.396558] (the system Light travel)" + }, + { + "q": "If the light time is 20.3966, what is the light distance?", + "a": "6114734246.156489", + "valid": true, + "kind": "compute", + "expect": 6114734246.156489, + "dtype": "quantity", + "unit": "m", + "why": "light distance for [20.396558] (the system Light travel)" + }, + { + "q": "Could you work out the light distance given light time = 134.993?", + "a": "40469870565.819176", + "valid": true, + "kind": "compute", + "expect": 40469870565.819176, + "dtype": "quantity", + "unit": "m", + "why": "light distance for [134.992958] (the system Light travel)" + }, + { + "q": "I need the light distance for a light time of 134.993, please.", + "a": "40469870565.819176", + "valid": true, + "kind": "compute", + "expect": 40469870565.819176, + "dtype": "quantity", + "unit": "m", + "why": "light distance for [134.992958] (the system Light travel)" + }, + { + "q": "I need the light distance, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the light time is not given" + }, + { + "q": "Can you tell me the light distance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the light time is not given" + }, + { + "q": "Do you know the light 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 light 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 light distance given light time = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative light time: no example had one" + }, + { + "q": "What is a typical light 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-light-distance/program.py b/root/organs10/physics/specialists/solved-light-distance/program.py new file mode 100644 index 0000000000000000000000000000000000000000..caddf6f5f43c0695513fa5c2ef4e2be246a75ea9 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-distance/program.py @@ -0,0 +1,7 @@ +"""light distance from light time (Light travel) +The maths organ (systems.py): solved from the system d = c*t; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(light_time): + return 299792458.0*light_time diff --git a/root/organs10/physics/specialists/solved-light-distance/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-light-distance/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-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"(?= 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-light-frequency-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-light-frequency-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-frequency-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-light-frequency-2/panel.json b/root/organs10/physics/specialists/solved-light-frequency-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..41895c886de11ea60f11e638727a52e933dec445 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-frequency-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the light frequency for wavenumber in vacuum 107.961?", + "a": "23751346495.791645", + "valid": true, + "kind": "compute", + "expect": 23751346495.791645, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [107.960666] (the system wavenumber in vacuum (Wikidata Q80168855))" + }, + { + "q": "Tell me the light frequency with wavenumber in vacuum of 107.961.", + "a": "23751346495.791645", + "valid": true, + "kind": "compute", + "expect": 23751346495.791645, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [107.960666] (the system wavenumber in vacuum (Wikidata Q80168855))" + }, + { + "q": "What is the light frequency for wavenumber in vacuum 22.652?", + "a": "4983445015.308274", + "valid": true, + "kind": "compute", + "expect": 4983445015.308274, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [22.652023] (the system wavenumber in vacuum (Wikidata Q80168855))" + }, + { + "q": "Tell me the light frequency with wavenumber in vacuum of 22.652.", + "a": "4983445015.308274", + "valid": true, + "kind": "compute", + "expect": 4983445015.308274, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [22.652023] (the system wavenumber in vacuum (Wikidata Q80168855))" + }, + { + "q": "What is the light frequency for wavenumber in vacuum 173.394?", + "a": "38146600449.16655", + "valid": true, + "kind": "compute", + "expect": 38146600449.16655, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [173.393638] (the system wavenumber in vacuum (Wikidata Q80168855))" + }, + { + "q": "Tell me the light frequency with wavenumber in vacuum of 173.394.", + "a": "38146600449.16655", + "valid": true, + "kind": "compute", + "expect": 38146600449.16655, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [173.393638] (the system wavenumber in vacuum (Wikidata Q80168855))" + }, + { + "q": "What is the light frequency for wavenumber in vacuum 126.709?", + "a": "27876035396.040123", + "valid": true, + "kind": "compute", + "expect": 27876035396.040123, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [126.709252] (the system wavenumber in vacuum (Wikidata Q80168855))" + }, + { + "q": "Tell me the light frequency with wavenumber in vacuum of 126.709.", + "a": "27876035396.040123", + "valid": true, + "kind": "compute", + "expect": 27876035396.040123, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [126.709252] (the system wavenumber in vacuum (Wikidata Q80168855))" + }, + { + "q": "What is the light frequency for wavenumber in vacuum 0.671697?", + "a": "147773383.95472258", + "valid": true, + "kind": "compute", + "expect": 147773383.95472258, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [0.671697] (the system wavenumber in vacuum (Wikidata Q80168855))" + }, + { + "q": "Tell me the light frequency with wavenumber in vacuum of 0.671697.", + "a": "147773383.95472258", + "valid": true, + "kind": "compute", + "expect": 147773383.95472258, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [0.671697] (the system wavenumber in vacuum (Wikidata Q80168855))" + }, + { + "q": "What is the light frequency for wavenumber in vacuum 1.16997?", + "a": "257392961.31514588", + "valid": true, + "kind": "compute", + "expect": 257392961.31514588, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [1.169968] (the system wavenumber in vacuum (Wikidata Q80168855))" + }, + { + "q": "Tell me the light frequency with wavenumber in vacuum of 1.16997.", + "a": "257392961.31514588", + "valid": true, + "kind": "compute", + "expect": 257392961.31514588, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [1.169968] (the system wavenumber in vacuum (Wikidata Q80168855))" + }, + { + "q": "What is the light frequency?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the wavenumber in vacuum is not given" + }, + { + "q": "Could you work out the light frequency, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the wavenumber in vacuum is not given" + }, + { + "q": "What is the light 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 light 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 light frequency with wavenumber in vacuum of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative wavenumber in vacuum: no example had one" + }, + { + "q": "Which light 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 wavenumber in vacuum is 2.57003, what is the light frequency?", + "a": "565406580.9268016", + "valid": true, + "kind": "compute", + "expect": 565406580.9268016, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [2.57003] (the system wavenumber in vacuum (Wikidata Q80168855))" + }, + { + "q": "Could you work out the light frequency given wavenumber in vacuum = 2.57003?", + "a": "565406580.9268016", + "valid": true, + "kind": "compute", + "expect": 565406580.9268016, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [2.57003] (the system wavenumber in vacuum (Wikidata Q80168855))" + }, + { + "q": "I need the light frequency for a wavenumber in vacuum of 48.921, please.", + "a": "10762617989.202604", + "valid": true, + "kind": "compute", + "expect": 10762617989.202604, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [48.920991] (the system wavenumber in vacuum (Wikidata Q80168855))" + }, + { + "q": "If the wavenumber in vacuum is 48.921, what is the light frequency?", + "a": "10762617989.202604", + "valid": true, + "kind": "compute", + "expect": 10762617989.202604, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [48.920991] (the system wavenumber in vacuum (Wikidata Q80168855))" + }, + { + "q": "Could you work out the light frequency given wavenumber in vacuum = 0.348438?", + "a": "76656287.3186067", + "valid": true, + "kind": "compute", + "expect": 76656287.3186067, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [0.348438] (the system wavenumber in vacuum (Wikidata Q80168855))" + }, + { + "q": "I need the light frequency for a wavenumber in vacuum of 0.348438, please.", + "a": "76656287.3186067", + "valid": true, + "kind": "compute", + "expect": 76656287.3186067, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [0.348438] (the system wavenumber in vacuum (Wikidata Q80168855))" + }, + { + "q": "If the wavenumber in vacuum is 252.709, what is the light frequency?", + "a": "55595985334.89606", + "valid": true, + "kind": "compute", + "expect": 55595985334.89606, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [252.709024] (the system wavenumber in vacuum (Wikidata Q80168855))" + }, + { + "q": "Could you work out the light frequency given wavenumber in vacuum = 252.709?", + "a": "55595985334.89606", + "valid": true, + "kind": "compute", + "expect": 55595985334.89606, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [252.709024] (the system wavenumber in vacuum (Wikidata Q80168855))" + }, + { + "q": "I need the light frequency for a wavenumber in vacuum of 20.3966, please.", + "a": "4487242751.631956", + "valid": true, + "kind": "compute", + "expect": 4487242751.631956, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [20.396558] (the system wavenumber in vacuum (Wikidata Q80168855))" + }, + { + "q": "If the wavenumber in vacuum is 20.3966, what is the light frequency?", + "a": "4487242751.631956", + "valid": true, + "kind": "compute", + "expect": 4487242751.631956, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [20.396558] (the system wavenumber in vacuum (Wikidata Q80168855))" + }, + { + "q": "Could you work out the light frequency given wavenumber in vacuum = 134.993?", + "a": "29698450667.76236", + "valid": true, + "kind": "compute", + "expect": 29698450667.76236, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [134.992958] (the system wavenumber in vacuum (Wikidata Q80168855))" + }, + { + "q": "I need the light frequency for a wavenumber in vacuum of 134.993, please.", + "a": "29698450667.76236", + "valid": true, + "kind": "compute", + "expect": 29698450667.76236, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [134.992958] (the system wavenumber in vacuum (Wikidata Q80168855))" + }, + { + "q": "I need the light frequency, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the wavenumber in vacuum is not given" + }, + { + "q": "Can you tell me the light frequency?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the wavenumber in vacuum is not given" + }, + { + "q": "Do you know the light 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 light 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 light frequency given wavenumber in vacuum = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative wavenumber in vacuum: no example had one" + }, + { + "q": "What is a typical light 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-light-frequency-2/program.py b/root/organs10/physics/specialists/solved-light-frequency-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..5d919884b926d74f0595913892134ca24b67bdaa --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-frequency-2/program.py @@ -0,0 +1,7 @@ +"""light frequency from wavenumber in vacuum (wavenumber in vacuum (Wikidata Q80168855)) +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(wavenumber_in_vacuum): + return 220000000.0*wavenumber_in_vacuum diff --git a/root/organs10/physics/specialists/solved-light-frequency-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-light-frequency-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-frequency-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-light-frequency-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-light-frequency-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-frequency-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-light-frequency-3/panel.json b/root/organs10/physics/specialists/solved-light-frequency-3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..69d4d44bffdf0279f928e1aa7b95771c448b6bba --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-frequency-3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the light frequency for wavelength in vacuum 107.961?", + "a": "2037779.2058473695", + "valid": true, + "kind": "compute", + "expect": 2037779.2058473695, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [107.960666] (the system wavelength in vacuum (Wikidata Q80173663))" + }, + { + "q": "Tell me the light frequency with wavelength in vacuum of 107.961.", + "a": "2037779.2058473695", + "valid": true, + "kind": "compute", + "expect": 2037779.2058473695, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [107.960666] (the system wavelength in vacuum (Wikidata Q80173663))" + }, + { + "q": "What is the light frequency for wavelength in vacuum 22.652?", + "a": "9712156.921832915", + "valid": true, + "kind": "compute", + "expect": 9712156.921832915, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [22.652023] (the system wavelength in vacuum (Wikidata Q80173663))" + }, + { + "q": "Tell me the light frequency with wavelength in vacuum of 22.652.", + "a": "9712156.921832915", + "valid": true, + "kind": "compute", + "expect": 9712156.921832915, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [22.652023] (the system wavelength in vacuum (Wikidata Q80173663))" + }, + { + "q": "What is the light frequency for wavelength in vacuum 173.394?", + "a": "1268789.339812782", + "valid": true, + "kind": "compute", + "expect": 1268789.339812782, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [173.393638] (the system wavelength in vacuum (Wikidata Q80173663))" + }, + { + "q": "Tell me the light frequency with wavelength in vacuum of 173.394.", + "a": "1268789.339812782", + "valid": true, + "kind": "compute", + "expect": 1268789.339812782, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [173.393638] (the system wavelength in vacuum (Wikidata Q80173663))" + }, + { + "q": "What is the light frequency for wavelength in vacuum 126.709?", + "a": "1736258.3779354568", + "valid": true, + "kind": "compute", + "expect": 1736258.3779354568, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [126.709252] (the system wavelength in vacuum (Wikidata Q80173663))" + }, + { + "q": "Tell me the light frequency with wavelength in vacuum of 126.709.", + "a": "1736258.3779354568", + "valid": true, + "kind": "compute", + "expect": 1736258.3779354568, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [126.709252] (the system wavelength in vacuum (Wikidata Q80173663))" + }, + { + "q": "What is the light frequency for wavelength in vacuum 0.671697?", + "a": "327528535.2795985", + "valid": true, + "kind": "compute", + "expect": 327528535.2795985, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [0.671697] (the system wavelength in vacuum (Wikidata Q80173663))" + }, + { + "q": "Tell me the light frequency with wavelength in vacuum of 0.671697.", + "a": "327528535.2795985", + "valid": true, + "kind": "compute", + "expect": 327528535.2795985, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [0.671697] (the system wavelength in vacuum (Wikidata Q80173663))" + }, + { + "q": "What is the light frequency for wavelength in vacuum 1.16997?", + "a": "188039330.02946487", + "valid": true, + "kind": "compute", + "expect": 188039330.02946487, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [1.169968] (the system wavelength in vacuum (Wikidata Q80173663))" + }, + { + "q": "Tell me the light frequency with wavelength in vacuum of 1.16997.", + "a": "188039330.02946487", + "valid": true, + "kind": "compute", + "expect": 188039330.02946487, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [1.169968] (the system wavelength in vacuum (Wikidata Q80173663))" + }, + { + "q": "What is the light frequency?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the wavelength in vacuum is not given" + }, + { + "q": "Could you work out the light frequency, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the wavelength in vacuum is not given" + }, + { + "q": "What is the light 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 light 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 light frequency with wavelength in vacuum of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative wavelength in vacuum: no example had one" + }, + { + "q": "Which light 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 wavelength in vacuum is 2.57003, what is the light frequency?", + "a": "85602116.48167203", + "valid": true, + "kind": "compute", + "expect": 85602116.48167203, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [2.57003] (the system wavelength in vacuum (Wikidata Q80173663))" + }, + { + "q": "Could you work out the light frequency given wavelength in vacuum = 2.57003?", + "a": "85602116.48167203", + "valid": true, + "kind": "compute", + "expect": 85602116.48167203, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [2.57003] (the system wavelength in vacuum (Wikidata Q80173663))" + }, + { + "q": "I need the light frequency for a wavelength in vacuum of 48.921, please.", + "a": "4497047.0984435575", + "valid": true, + "kind": "compute", + "expect": 4497047.0984435575, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [48.920991] (the system wavelength in vacuum (Wikidata Q80173663))" + }, + { + "q": "If the wavelength in vacuum is 48.921, what is the light frequency?", + "a": "4497047.0984435575", + "valid": true, + "kind": "compute", + "expect": 4497047.0984435575, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [48.920991] (the system wavelength in vacuum (Wikidata Q80173663))" + }, + { + "q": "Could you work out the light frequency given wavelength in vacuum = 0.348438?", + "a": "631389827.1492717", + "valid": true, + "kind": "compute", + "expect": 631389827.1492717, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [0.348438] (the system wavelength in vacuum (Wikidata Q80173663))" + }, + { + "q": "I need the light frequency for a wavelength in vacuum of 0.348438, please.", + "a": "631389827.1492717", + "valid": true, + "kind": "compute", + "expect": 631389827.1492717, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [0.348438] (the system wavelength in vacuum (Wikidata Q80173663))" + }, + { + "q": "If the wavelength in vacuum is 252.709, what is the light frequency?", + "a": "870566.457424052", + "valid": true, + "kind": "compute", + "expect": 870566.457424052, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [252.709024] (the system wavelength in vacuum (Wikidata Q80173663))" + }, + { + "q": "Could you work out the light frequency given wavelength in vacuum = 252.709?", + "a": "870566.457424052", + "valid": true, + "kind": "compute", + "expect": 870566.457424052, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [252.709024] (the system wavelength in vacuum (Wikidata Q80173663))" + }, + { + "q": "I need the light frequency for a wavelength in vacuum of 20.3966, please.", + "a": "10786133.641287291", + "valid": true, + "kind": "compute", + "expect": 10786133.641287291, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [20.396558] (the system wavelength in vacuum (Wikidata Q80173663))" + }, + { + "q": "If the wavelength in vacuum is 20.3966, what is the light frequency?", + "a": "10786133.641287291", + "valid": true, + "kind": "compute", + "expect": 10786133.641287291, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [20.396558] (the system wavelength in vacuum (Wikidata Q80173663))" + }, + { + "q": "Could you work out the light frequency given wavelength in vacuum = 134.993?", + "a": "1629714.645435634", + "valid": true, + "kind": "compute", + "expect": 1629714.645435634, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [134.992958] (the system wavelength in vacuum (Wikidata Q80173663))" + }, + { + "q": "I need the light frequency for a wavelength in vacuum of 134.993, please.", + "a": "1629714.645435634", + "valid": true, + "kind": "compute", + "expect": 1629714.645435634, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [134.992958] (the system wavelength in vacuum (Wikidata Q80173663))" + }, + { + "q": "I need the light frequency, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the wavelength in vacuum is not given" + }, + { + "q": "Can you tell me the light frequency?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the wavelength in vacuum is not given" + }, + { + "q": "Do you know the light 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 light 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 light frequency given wavelength in vacuum = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative wavelength in vacuum: no example had one" + }, + { + "q": "What is a typical light 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-light-frequency-3/program.py b/root/organs10/physics/specialists/solved-light-frequency-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..6af818928566428f2e521d8cdec7cea8f4218c8b --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-frequency-3/program.py @@ -0,0 +1,7 @@ +"""light frequency from wavelength in vacuum (wavelength in vacuum (Wikidata Q80173663)) +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(wavelength_in_vacuum): + return 220000000.0/wavelength_in_vacuum diff --git a/root/organs10/physics/specialists/solved-light-frequency-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-light-frequency-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-frequency-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-light-frequency-4/organ_formula_v2.py b/root/organs10/physics/specialists/solved-light-frequency-4/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-frequency-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-light-frequency-4/panel.json b/root/organs10/physics/specialists/solved-light-frequency-4/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..aff73f74f38d8fd553b8ae9285b8d4c3de2686c9 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-frequency-4/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the light frequency for light wavenumber 107.961 and speed of light in a medium 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [107.960666, 59.4105] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Tell me the light frequency with light wavenumber of 107.961 and speed of light in a medium of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [107.960666, 59.4105] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "What is the light frequency for light wavenumber 169.527 and speed of light in a medium 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [169.526958, 280.773702] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Tell me the light frequency with light wavenumber of 169.527 and speed of light in a medium of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [169.526958, 280.773702] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "What is the light frequency for light wavenumber 1.91195 and speed of light in a medium 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [1.911952, 82.240072] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Tell me the light frequency with light wavenumber of 1.91195 and speed of light in a medium of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [1.911952, 82.240072] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "What is the light frequency for light wavenumber 1.64379 and speed of light in a medium 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [1.643795, 252.709024] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Tell me the light frequency with light wavenumber of 1.64379 and speed of light in a medium of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [1.643795, 252.709024] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "What is the light frequency for light wavenumber 18.3169 and speed of light in a medium 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [18.316867, 6.838598] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Tell me the light frequency with light wavenumber of 18.3169 and speed of light in a medium of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [18.316867, 6.838598] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "What is the light frequency for light wavenumber 156.502 and speed of light in a medium 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [156.50181, 185.886273] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Tell me the light frequency with light wavenumber of 156.502 and speed of light in a medium of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [156.50181, 185.886273] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "What is the light frequency for light wavenumber 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed of light in a medium is not given" + }, + { + "q": "Tell me the light frequency with light wavenumber of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed of light in a medium is not given" + }, + { + "q": "What is the light 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 light 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 light frequency with light wavenumber of -1.91195 and speed of light in a medium of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative light wavenumber: no example had one" + }, + { + "q": "Which light 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 light wavenumber is 8.50728 and the speed of light in a medium is 17.7888, what is the light frequency?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [8.507283, 17.788778] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Could you work out the light frequency given light wavenumber = 8.50728, speed of light in a medium = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [8.507283, 17.788778] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "I need the light frequency for a light wavenumber of 8.24951 and a speed of light in a medium of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [8.24951, 0.634021] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "If the light wavenumber is 8.24951 and the speed of light in a medium is 0.634021, what is the light frequency?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [8.24951, 0.634021] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Could you work out the light frequency given light wavenumber = 0.318734, speed of light in a medium = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [0.318734, 9.566071] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "I need the light frequency for a light wavenumber of 0.318734 and a speed of light in a medium of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [0.318734, 9.566071] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "If the light wavenumber is 0.671697 and the speed of light in a medium is 14.2496, what is the light frequency?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [0.671697, 14.249569] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Could you work out the light frequency given light wavenumber = 0.671697, speed of light in a medium = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [0.671697, 14.249569] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "I need the light frequency for a light wavenumber of 21.7931 and a speed of light in a medium of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [21.793126, 29.510609] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "If the light wavenumber is 21.7931 and the speed of light in a medium is 29.5106, what is the light frequency?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [21.793126, 29.510609] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Could you work out the light frequency given light wavenumber = 153.15, speed of light in a medium = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [153.149965, 18.595315] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "I need the light frequency for a light wavenumber of 153.15 and a speed of light in a medium of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [153.149965, 18.595315] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "If the light wavenumber is 8.50728, what is the light frequency?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed of light in a medium is not given" + }, + { + "q": "Could you work out the light frequency given light wavenumber = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed of light in a medium is not given" + }, + { + "q": "Do you know the light 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 light 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 light frequency given light wavenumber = -0.318734, speed of light in a medium = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative light wavenumber: no example had one" + }, + { + "q": "What is a typical light 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-light-frequency-4/program.py b/root/organs10/physics/specialists/solved-light-frequency-4/program.py new file mode 100644 index 0000000000000000000000000000000000000000..4d3cf52e42af39b265e5c3c80599e2d2604fbd70 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-frequency-4/program.py @@ -0,0 +1,7 @@ +"""light frequency from light wavenumber, speed of light in a medium (light wavenumber (Wikidata Q106008527)) +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(light_wavenumber, speed_of_light_in_a_medium): + return 1.0*light_wavenumber*speed_of_light_in_a_medium diff --git a/root/organs10/physics/specialists/solved-light-frequency-4/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-light-frequency-4/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-frequency-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-light-frequency-5/organ_formula_v2.py b/root/organs10/physics/specialists/solved-light-frequency-5/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-frequency-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-light-frequency-5/panel.json b/root/organs10/physics/specialists/solved-light-frequency-5/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..93c8241965495b2cc1c56ee4ba07ec6907848c01 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-frequency-5/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the light frequency for photon energy 107.961?", + "a": "1.629331794536271e+35", + "valid": true, + "kind": "compute", + "expect": 1.629331794536271e+35, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [107.960666] (the system photon energy (Wikidata Q25303639))" + }, + { + "q": "Tell me the light frequency with photon energy of 107.961.", + "a": "1.629331794536271e+35", + "valid": true, + "kind": "compute", + "expect": 1.629331794536271e+35, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [107.960666] (the system photon energy (Wikidata Q25303639))" + }, + { + "q": "What is the light frequency for photon energy 22.652?", + "a": "3.4186210921573406e+34", + "valid": true, + "kind": "compute", + "expect": 3.4186210921573406e+34, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [22.652023] (the system photon energy (Wikidata Q25303639))" + }, + { + "q": "Tell me the light frequency with photon energy of 22.652.", + "a": "3.4186210921573406e+34", + "valid": true, + "kind": "compute", + "expect": 3.4186210921573406e+34, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [22.652023] (the system photon energy (Wikidata Q25303639))" + }, + { + "q": "What is the light frequency for photon energy 173.394?", + "a": "2.616839806379451e+35", + "valid": true, + "kind": "compute", + "expect": 2.616839806379451e+35, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [173.393638] (the system photon energy (Wikidata Q25303639))" + }, + { + "q": "Tell me the light frequency with photon energy of 173.394.", + "a": "2.616839806379451e+35", + "valid": true, + "kind": "compute", + "expect": 2.616839806379451e+35, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [173.393638] (the system photon energy (Wikidata Q25303639))" + }, + { + "q": "What is the light frequency for photon energy 126.709?", + "a": "1.9122836166123986e+35", + "valid": true, + "kind": "compute", + "expect": 1.9122836166123986e+35, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [126.709252] (the system photon energy (Wikidata Q25303639))" + }, + { + "q": "Tell me the light frequency with photon energy of 126.709.", + "a": "1.9122836166123986e+35", + "valid": true, + "kind": "compute", + "expect": 1.9122836166123986e+35, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [126.709252] (the system photon energy (Wikidata Q25303639))" + }, + { + "q": "What is the light frequency for photon energy 0.671697?", + "a": "1.0137188344513707e+33", + "valid": true, + "kind": "compute", + "expect": 1.0137188344513707e+33, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [0.671697] (the system photon energy (Wikidata Q25303639))" + }, + { + "q": "Tell me the light frequency with photon energy of 0.671697.", + "a": "1.0137188344513707e+33", + "valid": true, + "kind": "compute", + "expect": 1.0137188344513707e+33, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [0.671697] (the system photon energy (Wikidata Q25303639))" + }, + { + "q": "What is the light frequency for photon energy 1.16997?", + "a": "1.765704254430033e+33", + "valid": true, + "kind": "compute", + "expect": 1.765704254430033e+33, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [1.169968] (the system photon energy (Wikidata Q25303639))" + }, + { + "q": "Tell me the light frequency with photon energy of 1.16997.", + "a": "1.765704254430033e+33", + "valid": true, + "kind": "compute", + "expect": 1.765704254430033e+33, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [1.169968] (the system photon energy (Wikidata Q25303639))" + }, + { + "q": "What is the light frequency?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the photon energy is not given" + }, + { + "q": "Could you work out the light frequency, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the photon energy is not given" + }, + { + "q": "What is the light 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 light 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 light frequency with photon energy of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative photon energy: no example had one" + }, + { + "q": "Which light 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 photon energy is 2.57003, what is the light frequency?", + "a": "3.878663970934494e+33", + "valid": true, + "kind": "compute", + "expect": 3.878663970934494e+33, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [2.57003] (the system photon energy (Wikidata Q25303639))" + }, + { + "q": "Could you work out the light frequency given photon energy = 2.57003?", + "a": "3.878663970934494e+33", + "valid": true, + "kind": "compute", + "expect": 3.878663970934494e+33, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [2.57003] (the system photon energy (Wikidata Q25303639))" + }, + { + "q": "I need the light frequency for a photon energy of 48.921, please.", + "a": "7.383108020997001e+34", + "valid": true, + "kind": "compute", + "expect": 7.383108020997001e+34, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [48.920991] (the system photon energy (Wikidata Q25303639))" + }, + { + "q": "If the photon energy is 48.921, what is the light frequency?", + "a": "7.383108020997001e+34", + "valid": true, + "kind": "compute", + "expect": 7.383108020997001e+34, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [48.920991] (the system photon energy (Wikidata Q25303639))" + }, + { + "q": "Could you work out the light frequency given photon energy = 0.348438?", + "a": "5.258587179528683e+32", + "valid": true, + "kind": "compute", + "expect": 5.258587179528683e+32, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [0.348438] (the system photon energy (Wikidata Q25303639))" + }, + { + "q": "I need the light frequency for a photon energy of 0.348438, please.", + "a": "5.258587179528683e+32", + "valid": true, + "kind": "compute", + "expect": 5.258587179528683e+32, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [0.348438] (the system photon energy (Wikidata Q25303639))" + }, + { + "q": "If the photon energy is 252.709, what is the light frequency?", + "a": "3.813859840357618e+35", + "valid": true, + "kind": "compute", + "expect": 3.813859840357618e+35, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [252.709024] (the system photon energy (Wikidata Q25303639))" + }, + { + "q": "Could you work out the light frequency given photon energy = 252.709?", + "a": "3.813859840357618e+35", + "valid": true, + "kind": "compute", + "expect": 3.813859840357618e+35, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [252.709024] (the system photon energy (Wikidata Q25303639))" + }, + { + "q": "I need the light frequency for a photon energy of 20.3966, please.", + "a": "3.078228548571677e+34", + "valid": true, + "kind": "compute", + "expect": 3.078228548571677e+34, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [20.396558] (the system photon energy (Wikidata Q25303639))" + }, + { + "q": "If the photon energy is 20.3966, what is the light frequency?", + "a": "3.078228548571677e+34", + "valid": true, + "kind": "compute", + "expect": 3.078228548571677e+34, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [20.396558] (the system photon energy (Wikidata Q25303639))" + }, + { + "q": "Could you work out the light frequency given photon energy = 134.993?", + "a": "2.0373004928384102e+35", + "valid": true, + "kind": "compute", + "expect": 2.0373004928384102e+35, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [134.992958] (the system photon energy (Wikidata Q25303639))" + }, + { + "q": "I need the light frequency for a photon energy of 134.993, please.", + "a": "2.0373004928384102e+35", + "valid": true, + "kind": "compute", + "expect": 2.0373004928384102e+35, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [134.992958] (the system photon energy (Wikidata Q25303639))" + }, + { + "q": "I need the light frequency, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the photon energy is not given" + }, + { + "q": "Can you tell me the light frequency?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the photon energy is not given" + }, + { + "q": "Do you know the light 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 light 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 light frequency given photon energy = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative photon energy: no example had one" + }, + { + "q": "What is a typical light 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-light-frequency-5/program.py b/root/organs10/physics/specialists/solved-light-frequency-5/program.py new file mode 100644 index 0000000000000000000000000000000000000000..51fe92b17ed74cc9431c4843bb6f617427a0a331 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-frequency-5/program.py @@ -0,0 +1,7 @@ +"""light frequency from photon energy (photon energy (Wikidata Q25303639)) +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(photon_energy): + return 1.50919020469636e+33*photon_energy diff --git a/root/organs10/physics/specialists/solved-light-frequency-5/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-light-frequency-5/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-frequency-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-light-frequency/organ_formula_v2.py b/root/organs10/physics/specialists/solved-light-frequency/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-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-light-frequency/panel.json b/root/organs10/physics/specialists/solved-light-frequency/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..0440eb2092d590a7796b8506fddce448630ce4c7 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-frequency/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the light frequency for period 107.961?", + "a": "0.00926263275385168", + "valid": true, + "kind": "compute", + "expect": 0.00926263275385168, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [107.960666] (the system light frequency (Wikidata Q106008771))" + }, + { + "q": "Tell me the light frequency with period of 107.961.", + "a": "0.00926263275385168", + "valid": true, + "kind": "compute", + "expect": 0.00926263275385168, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [107.960666] (the system light frequency (Wikidata Q106008771))" + }, + { + "q": "What is the light frequency for period 22.652?", + "a": "0.044146167826513254", + "valid": true, + "kind": "compute", + "expect": 0.044146167826513254, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [22.652023] (the system light frequency (Wikidata Q106008771))" + }, + { + "q": "Tell me the light frequency with period of 22.652.", + "a": "0.044146167826513254", + "valid": true, + "kind": "compute", + "expect": 0.044146167826513254, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [22.652023] (the system light frequency (Wikidata Q106008771))" + }, + { + "q": "What is the light frequency for period 173.394?", + "a": "0.0057672242718762815", + "valid": true, + "kind": "compute", + "expect": 0.0057672242718762815, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [173.393638] (the system light frequency (Wikidata Q106008771))" + }, + { + "q": "Tell me the light frequency with period of 173.394.", + "a": "0.0057672242718762815", + "valid": true, + "kind": "compute", + "expect": 0.0057672242718762815, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [173.393638] (the system light frequency (Wikidata Q106008771))" + }, + { + "q": "What is the light frequency for period 126.709?", + "a": "0.007892083536070259", + "valid": true, + "kind": "compute", + "expect": 0.007892083536070259, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [126.709252] (the system light frequency (Wikidata Q106008771))" + }, + { + "q": "Tell me the light frequency with period of 126.709.", + "a": "0.007892083536070259", + "valid": true, + "kind": "compute", + "expect": 0.007892083536070259, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [126.709252] (the system light frequency (Wikidata Q106008771))" + }, + { + "q": "What is the light frequency for period 0.671697?", + "a": "1.4887660694527203", + "valid": true, + "kind": "compute", + "expect": 1.4887660694527203, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [0.671697] (the system light frequency (Wikidata Q106008771))" + }, + { + "q": "Tell me the light frequency with period of 0.671697.", + "a": "1.4887660694527203", + "valid": true, + "kind": "compute", + "expect": 1.4887660694527203, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [0.671697] (the system light frequency (Wikidata Q106008771))" + }, + { + "q": "What is the light frequency for period 1.16997?", + "a": "0.8547242274066585", + "valid": true, + "kind": "compute", + "expect": 0.8547242274066585, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [1.169968] (the system light frequency (Wikidata Q106008771))" + }, + { + "q": "Tell me the light frequency with period of 1.16997.", + "a": "0.8547242274066585", + "valid": true, + "kind": "compute", + "expect": 0.8547242274066585, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [1.169968] (the system light frequency (Wikidata Q106008771))" + }, + { + "q": "What is the light frequency?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the period is not given" + }, + { + "q": "Could you work out the light frequency, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the period is not given" + }, + { + "q": "What is the light 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 light 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 light frequency with period of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative period: no example had one" + }, + { + "q": "Which light 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 period is 2.57003, what is the light frequency?", + "a": "0.38910052946214563", + "valid": true, + "kind": "compute", + "expect": 0.38910052946214563, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [2.57003] (the system light frequency (Wikidata Q106008771))" + }, + { + "q": "Could you work out the light frequency given period = 2.57003?", + "a": "0.38910052946214563", + "valid": true, + "kind": "compute", + "expect": 0.38910052946214563, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [2.57003] (the system light frequency (Wikidata Q106008771))" + }, + { + "q": "I need the light frequency for a period of 48.921, please.", + "a": "0.02044112317474344", + "valid": true, + "kind": "compute", + "expect": 0.02044112317474344, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [48.920991] (the system light frequency (Wikidata Q106008771))" + }, + { + "q": "If the period is 48.921, what is the light frequency?", + "a": "0.02044112317474344", + "valid": true, + "kind": "compute", + "expect": 0.02044112317474344, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [48.920991] (the system light frequency (Wikidata Q106008771))" + }, + { + "q": "Could you work out the light frequency given period = 0.348438?", + "a": "2.869953759769417", + "valid": true, + "kind": "compute", + "expect": 2.869953759769417, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [0.348438] (the system light frequency (Wikidata Q106008771))" + }, + { + "q": "I need the light frequency for a period of 0.348438, please.", + "a": "2.869953759769417", + "valid": true, + "kind": "compute", + "expect": 2.869953759769417, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [0.348438] (the system light frequency (Wikidata Q106008771))" + }, + { + "q": "If the period is 252.709, what is the light frequency?", + "a": "0.0039571202610184176", + "valid": true, + "kind": "compute", + "expect": 0.0039571202610184176, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [252.709024] (the system light frequency (Wikidata Q106008771))" + }, + { + "q": "Could you work out the light frequency given period = 252.709?", + "a": "0.0039571202610184176", + "valid": true, + "kind": "compute", + "expect": 0.0039571202610184176, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [252.709024] (the system light frequency (Wikidata Q106008771))" + }, + { + "q": "I need the light frequency for a period of 20.3966, please.", + "a": "0.04902788018766951", + "valid": true, + "kind": "compute", + "expect": 0.04902788018766951, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [20.396558] (the system light frequency (Wikidata Q106008771))" + }, + { + "q": "If the period is 20.3966, what is the light frequency?", + "a": "0.04902788018766951", + "valid": true, + "kind": "compute", + "expect": 0.04902788018766951, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [20.396558] (the system light frequency (Wikidata Q106008771))" + }, + { + "q": "Could you work out the light frequency given period = 134.993?", + "a": "0.007407793842889246", + "valid": true, + "kind": "compute", + "expect": 0.007407793842889246, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [134.992958] (the system light frequency (Wikidata Q106008771))" + }, + { + "q": "I need the light frequency for a period of 134.993, please.", + "a": "0.007407793842889246", + "valid": true, + "kind": "compute", + "expect": 0.007407793842889246, + "dtype": "quantity", + "unit": "Hz", + "why": "light frequency for [134.992958] (the system light frequency (Wikidata Q106008771))" + }, + { + "q": "I need the light frequency, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the period is not given" + }, + { + "q": "Can you tell me the light frequency?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the period is not given" + }, + { + "q": "Do you know the light 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 light 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 light frequency given period = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative period: no example had one" + }, + { + "q": "What is a typical light 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-light-frequency/program.py b/root/organs10/physics/specialists/solved-light-frequency/program.py new file mode 100644 index 0000000000000000000000000000000000000000..7a2a34dd900d3605215b3e7622726d8ef93b1fbf --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-frequency/program.py @@ -0,0 +1,7 @@ +"""light frequency from period (light frequency (Wikidata Q106008771)) +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): + return 1.0/period diff --git a/root/organs10/physics/specialists/solved-light-frequency/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-light-frequency/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-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-light-time/organ_formula_v2.py b/root/organs10/physics/specialists/solved-light-time/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-time/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-light-time/panel.json b/root/organs10/physics/specialists/solved-light-time/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..5d8b6a0eff3ca361c08e7b9ed76216218cf8d438 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-time/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the light time for light distance 107.961?", + "a": "3.6011801834575174e-07", + "valid": true, + "kind": "compute", + "expect": 3.6011801834575174e-07, + "dtype": "quantity", + "unit": "s", + "why": "light time for [107.960666] (the system Light travel)" + }, + { + "q": "Tell me the light time with light distance of 107.961.", + "a": "3.6011801834575174e-07", + "valid": true, + "kind": "compute", + "expect": 3.6011801834575174e-07, + "dtype": "quantity", + "unit": "s", + "why": "light time for [107.960666] (the system Light travel)" + }, + { + "q": "What is the light time for light distance 22.652?", + "a": "7.555901488641114e-08", + "valid": true, + "kind": "compute", + "expect": 7.555901488641114e-08, + "dtype": "quantity", + "unit": "s", + "why": "light time for [22.652023] (the system Light travel)" + }, + { + "q": "Tell me the light time with light distance of 22.652.", + "a": "7.555901488641114e-08", + "valid": true, + "kind": "compute", + "expect": 7.555901488641114e-08, + "dtype": "quantity", + "unit": "s", + "why": "light time for [22.652023] (the system Light travel)" + }, + { + "q": "What is the light time for light distance 173.394?", + "a": "5.783789210778028e-07", + "valid": true, + "kind": "compute", + "expect": 5.783789210778028e-07, + "dtype": "quantity", + "unit": "s", + "why": "light time for [173.393638] (the system Light travel)" + }, + { + "q": "Tell me the light time with light distance of 173.394.", + "a": "5.783789210778028e-07", + "valid": true, + "kind": "compute", + "expect": 5.783789210778028e-07, + "dtype": "quantity", + "unit": "s", + "why": "light time for [173.393638] (the system Light travel)" + }, + { + "q": "What is the light time for light distance 126.709?", + "a": "4.2265656929962647e-07", + "valid": true, + "kind": "compute", + "expect": 4.2265656929962647e-07, + "dtype": "quantity", + "unit": "s", + "why": "light time for [126.709252] (the system Light travel)" + }, + { + "q": "Tell me the light time with light distance of 126.709.", + "a": "4.2265656929962647e-07", + "valid": true, + "kind": "compute", + "expect": 4.2265656929962647e-07, + "dtype": "quantity", + "unit": "s", + "why": "light time for [126.709252] (the system Light travel)" + }, + { + "q": "What is the light time for light distance 0.671697?", + "a": "2.2405406869648247e-09", + "valid": true, + "kind": "compute", + "expect": 2.2405406869648247e-09, + "dtype": "quantity", + "unit": "s", + "why": "light time for [0.671697] (the system Light travel)" + }, + { + "q": "Tell me the light time with light distance of 0.671697.", + "a": "2.2405406869648247e-09", + "valid": true, + "kind": "compute", + "expect": 2.2405406869648247e-09, + "dtype": "quantity", + "unit": "s", + "why": "light time for [0.671697] (the system Light travel)" + }, + { + "q": "What is the light time for light distance 1.16997?", + "a": "3.902593193248163e-09", + "valid": true, + "kind": "compute", + "expect": 3.902593193248163e-09, + "dtype": "quantity", + "unit": "s", + "why": "light time for [1.169968] (the system Light travel)" + }, + { + "q": "Tell me the light time with light distance of 1.16997.", + "a": "3.902593193248163e-09", + "valid": true, + "kind": "compute", + "expect": 3.902593193248163e-09, + "dtype": "quantity", + "unit": "s", + "why": "light time for [1.169968] (the system Light travel)" + }, + { + "q": "What is the light time?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the light distance is not given" + }, + { + "q": "Could you work out the light time, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the light distance is not given" + }, + { + "q": "What is the light time 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 light time 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 light time with light distance of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative light distance: no example had one" + }, + { + "q": "Which light time 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 light distance is 2.57003, what is the light time?", + "a": "8.572697026633149e-09", + "valid": true, + "kind": "compute", + "expect": 8.572697026633149e-09, + "dtype": "quantity", + "unit": "s", + "why": "light time for [2.57003] (the system Light travel)" + }, + { + "q": "Could you work out the light time given light distance = 2.57003?", + "a": "8.572697026633149e-09", + "valid": true, + "kind": "compute", + "expect": 8.572697026633149e-09, + "dtype": "quantity", + "unit": "s", + "why": "light time for [2.57003] (the system Light travel)" + }, + { + "q": "I need the light time for a light distance of 48.921, please.", + "a": "1.6318286052416912e-07", + "valid": true, + "kind": "compute", + "expect": 1.6318286052416912e-07, + "dtype": "quantity", + "unit": "s", + "why": "light time for [48.920991] (the system Light travel)" + }, + { + "q": "If the light distance is 48.921, what is the light time?", + "a": "1.6318286052416912e-07", + "valid": true, + "kind": "compute", + "expect": 1.6318286052416912e-07, + "dtype": "quantity", + "unit": "s", + "why": "light time for [48.920991] (the system Light travel)" + }, + { + "q": "Could you work out the light time given light distance = 0.348438?", + "a": "1.1622629600309372e-09", + "valid": true, + "kind": "compute", + "expect": 1.1622629600309372e-09, + "dtype": "quantity", + "unit": "s", + "why": "light time for [0.348438] (the system Light travel)" + }, + { + "q": "I need the light time for a light distance of 0.348438, please.", + "a": "1.1622629600309372e-09", + "valid": true, + "kind": "compute", + "expect": 1.1622629600309372e-09, + "dtype": "quantity", + "unit": "s", + "why": "light time for [0.348438] (the system Light travel)" + }, + { + "q": "If the light distance is 252.709, what is the light time?", + "a": "8.42946570222015e-07", + "valid": true, + "kind": "compute", + "expect": 8.42946570222015e-07, + "dtype": "quantity", + "unit": "s", + "why": "light time for [252.709024] (the system Light travel)" + }, + { + "q": "Could you work out the light time given light distance = 252.709?", + "a": "8.42946570222015e-07", + "valid": true, + "kind": "compute", + "expect": 8.42946570222015e-07, + "dtype": "quantity", + "unit": "s", + "why": "light time for [252.709024] (the system Light travel)" + }, + { + "q": "I need the light time for a light distance of 20.3966, please.", + "a": "6.803559401738998e-08", + "valid": true, + "kind": "compute", + "expect": 6.803559401738998e-08, + "dtype": "quantity", + "unit": "s", + "why": "light time for [20.396558] (the system Light travel)" + }, + { + "q": "If the light distance is 20.3966, what is the light time?", + "a": "6.803559401738998e-08", + "valid": true, + "kind": "compute", + "expect": 6.803559401738998e-08, + "dtype": "quantity", + "unit": "s", + "why": "light time for [20.396558] (the system Light travel)" + }, + { + "q": "Could you work out the light time given light distance = 134.993?", + "a": "4.5028803753541383e-07", + "valid": true, + "kind": "compute", + "expect": 4.5028803753541383e-07, + "dtype": "quantity", + "unit": "s", + "why": "light time for [134.992958] (the system Light travel)" + }, + { + "q": "I need the light time for a light distance of 134.993, please.", + "a": "4.5028803753541383e-07", + "valid": true, + "kind": "compute", + "expect": 4.5028803753541383e-07, + "dtype": "quantity", + "unit": "s", + "why": "light time for [134.992958] (the system Light travel)" + }, + { + "q": "I need the light time, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the light distance is not given" + }, + { + "q": "Can you tell me the light time?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the light distance is not given" + }, + { + "q": "Do you know the light time 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 light time 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 light time given light distance = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative light distance: no example had one" + }, + { + "q": "What is a typical light time?", + "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-light-time/program.py b/root/organs10/physics/specialists/solved-light-time/program.py new file mode 100644 index 0000000000000000000000000000000000000000..ffd9c6a2f6d9c9aba96ae5aba04abf1a8e26266b --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-time/program.py @@ -0,0 +1,7 @@ +"""light time from light distance (Light travel) +The maths organ (systems.py): solved from the system d = c*t; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(light_distance): + return 3.33564095198152e-9*light_distance diff --git a/root/organs10/physics/specialists/solved-light-time/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-light-time/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-time/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-light-wavenumber/organ_formula_v2.py b/root/organs10/physics/specialists/solved-light-wavenumber/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-wavenumber/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-light-wavenumber/panel.json b/root/organs10/physics/specialists/solved-light-wavenumber/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..c1f0568d111567251d2aca1fe5e81951f6e7f6a4 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-wavenumber/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the light wavenumber for light frequency 107.961 and speed of light in a medium 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1/m", + "why": "light wavenumber for [107.960666, 59.4105] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Tell me the light wavenumber with light frequency of 107.961 and speed of light in a medium of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1/m", + "why": "light wavenumber for [107.960666, 59.4105] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "What is the light wavenumber for light frequency 169.527 and speed of light in a medium 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1/m", + "why": "light wavenumber for [169.526958, 280.773702] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Tell me the light wavenumber with light frequency of 169.527 and speed of light in a medium of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1/m", + "why": "light wavenumber for [169.526958, 280.773702] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "What is the light wavenumber for light frequency 1.91195 and speed of light in a medium 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1/m", + "why": "light wavenumber for [1.911952, 82.240072] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Tell me the light wavenumber with light frequency of 1.91195 and speed of light in a medium of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1/m", + "why": "light wavenumber for [1.911952, 82.240072] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "What is the light wavenumber for light frequency 1.64379 and speed of light in a medium 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1/m", + "why": "light wavenumber for [1.643795, 252.709024] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Tell me the light wavenumber with light frequency of 1.64379 and speed of light in a medium of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1/m", + "why": "light wavenumber for [1.643795, 252.709024] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "What is the light wavenumber for light frequency 18.3169 and speed of light in a medium 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1/m", + "why": "light wavenumber for [18.316867, 6.838598] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Tell me the light wavenumber with light frequency of 18.3169 and speed of light in a medium of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1/m", + "why": "light wavenumber for [18.316867, 6.838598] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "What is the light wavenumber for light frequency 156.502 and speed of light in a medium 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1/m", + "why": "light wavenumber for [156.50181, 185.886273] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Tell me the light wavenumber with light frequency of 156.502 and speed of light in a medium of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1/m", + "why": "light wavenumber for [156.50181, 185.886273] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "What is the light wavenumber for light frequency 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed of light in a medium is not given" + }, + { + "q": "Tell me the light wavenumber with light frequency of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed of light in a medium is not given" + }, + { + "q": "What is the light wavenumber 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 light wavenumber 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 light wavenumber with light frequency of -1.91195 and speed of light in a medium of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative light frequency: no example had one" + }, + { + "q": "Which light wavenumber 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 light frequency is 8.50728 and the speed of light in a medium is 17.7888, what is the light wavenumber?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1/m", + "why": "light wavenumber for [8.507283, 17.788778] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Could you work out the light wavenumber given light frequency = 8.50728, speed of light in a medium = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1/m", + "why": "light wavenumber for [8.507283, 17.788778] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "I need the light wavenumber for a light frequency of 8.24951 and a speed of light in a medium of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1/m", + "why": "light wavenumber for [8.24951, 0.634021] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "If the light frequency is 8.24951 and the speed of light in a medium is 0.634021, what is the light wavenumber?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1/m", + "why": "light wavenumber for [8.24951, 0.634021] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Could you work out the light wavenumber given light frequency = 0.318734, speed of light in a medium = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1/m", + "why": "light wavenumber for [0.318734, 9.566071] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "I need the light wavenumber for a light frequency of 0.318734 and a speed of light in a medium of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1/m", + "why": "light wavenumber for [0.318734, 9.566071] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "If the light frequency is 0.671697 and the speed of light in a medium is 14.2496, what is the light wavenumber?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1/m", + "why": "light wavenumber for [0.671697, 14.249569] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Could you work out the light wavenumber given light frequency = 0.671697, speed of light in a medium = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1/m", + "why": "light wavenumber for [0.671697, 14.249569] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "I need the light wavenumber for a light frequency of 21.7931 and a speed of light in a medium of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1/m", + "why": "light wavenumber for [21.793126, 29.510609] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "If the light frequency is 21.7931 and the speed of light in a medium is 29.5106, what is the light wavenumber?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1/m", + "why": "light wavenumber for [21.793126, 29.510609] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Could you work out the light wavenumber given light frequency = 153.15, speed of light in a medium = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1/m", + "why": "light wavenumber for [153.149965, 18.595315] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "I need the light wavenumber for a light frequency of 153.15 and a speed of light in a medium of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1/m", + "why": "light wavenumber for [153.149965, 18.595315] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "If the light frequency is 8.50728, what is the light wavenumber?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed of light in a medium is not given" + }, + { + "q": "Could you work out the light wavenumber given light frequency = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed of light in a medium is not given" + }, + { + "q": "Do you know the light wavenumber 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 light wavenumber 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 light wavenumber given light frequency = -0.318734, speed of light in a medium = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative light frequency: no example had one" + }, + { + "q": "What is a typical light wavenumber?", + "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-light-wavenumber/program.py b/root/organs10/physics/specialists/solved-light-wavenumber/program.py new file mode 100644 index 0000000000000000000000000000000000000000..9b629a0adb698f3fd6743b08e89640af3f3f7caa --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-wavenumber/program.py @@ -0,0 +1,7 @@ +"""light wavenumber from light frequency, speed of light in a medium (light wavenumber (Wikidata Q106008527)) +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(light_frequency, speed_of_light_in_a_medium): + return 1.0*light_frequency/speed_of_light_in_a_medium diff --git a/root/organs10/physics/specialists/solved-light-wavenumber/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-light-wavenumber/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-light-wavenumber/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-linear-absorption-coefficient-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-linear-absorption-coefficient-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-absorption-coefficient-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-linear-absorption-coefficient-2/panel.json b/root/organs10/physics/specialists/solved-linear-absorption-coefficient-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..4029288ee8cdc14f3dd1d7408924dc1c1196796c --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-absorption-coefficient-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the linear absorption coefficient for mass absorption coefficient 107.961 and mass density 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [107.960666, 59.4105] (the system mass absorption coefficient (Wikidata Q86202147))" + }, + { + "q": "Tell me the linear absorption coefficient with mass absorption coefficient of 107.961 and mass density of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [107.960666, 59.4105] (the system mass absorption coefficient (Wikidata Q86202147))" + }, + { + "q": "What is the linear absorption coefficient for mass absorption coefficient 169.527 and mass density 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [169.526958, 280.773702] (the system mass absorption coefficient (Wikidata Q86202147))" + }, + { + "q": "Tell me the linear absorption coefficient with mass absorption coefficient of 169.527 and mass density of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [169.526958, 280.773702] (the system mass absorption coefficient (Wikidata Q86202147))" + }, + { + "q": "What is the linear absorption coefficient for mass absorption coefficient 1.91195 and mass density 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [1.911952, 82.240072] (the system mass absorption coefficient (Wikidata Q86202147))" + }, + { + "q": "Tell me the linear absorption coefficient with mass absorption coefficient of 1.91195 and mass density of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [1.911952, 82.240072] (the system mass absorption coefficient (Wikidata Q86202147))" + }, + { + "q": "What is the linear absorption coefficient for mass absorption coefficient 1.64379 and mass density 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [1.643795, 252.709024] (the system mass absorption coefficient (Wikidata Q86202147))" + }, + { + "q": "Tell me the linear absorption coefficient with mass absorption coefficient of 1.64379 and mass density of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [1.643795, 252.709024] (the system mass absorption coefficient (Wikidata Q86202147))" + }, + { + "q": "What is the linear absorption coefficient for mass absorption coefficient 18.3169 and mass density 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [18.316867, 6.838598] (the system mass absorption coefficient (Wikidata Q86202147))" + }, + { + "q": "Tell me the linear absorption coefficient with mass absorption coefficient of 18.3169 and mass density of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [18.316867, 6.838598] (the system mass absorption coefficient (Wikidata Q86202147))" + }, + { + "q": "What is the linear absorption coefficient for mass absorption coefficient 156.502 and mass density 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [156.50181, 185.886273] (the system mass absorption coefficient (Wikidata Q86202147))" + }, + { + "q": "Tell me the linear absorption coefficient with mass absorption coefficient of 156.502 and mass density of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [156.50181, 185.886273] (the system mass absorption coefficient (Wikidata Q86202147))" + }, + { + "q": "What is the linear absorption coefficient for mass absorption coefficient 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Tell me the linear absorption coefficient with mass absorption coefficient of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "What is the linear absorption coefficient 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 linear absorption coefficient 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 linear absorption coefficient with mass absorption coefficient of -1.91195 and mass density of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mass absorption coefficient: no example had one" + }, + { + "q": "Which linear absorption coefficient 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 absorption coefficient is 8.50728 and the mass density is 17.7888, what is the linear absorption coefficient?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [8.507283, 17.788778] (the system mass absorption coefficient (Wikidata Q86202147))" + }, + { + "q": "Could you work out the linear absorption coefficient given mass absorption coefficient = 8.50728, mass density = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [8.507283, 17.788778] (the system mass absorption coefficient (Wikidata Q86202147))" + }, + { + "q": "I need the linear absorption coefficient for a mass absorption coefficient of 8.24951 and a mass density of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [8.24951, 0.634021] (the system mass absorption coefficient (Wikidata Q86202147))" + }, + { + "q": "If the mass absorption coefficient is 8.24951 and the mass density is 0.634021, what is the linear absorption coefficient?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [8.24951, 0.634021] (the system mass absorption coefficient (Wikidata Q86202147))" + }, + { + "q": "Could you work out the linear absorption coefficient given mass absorption coefficient = 0.318734, mass density = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [0.318734, 9.566071] (the system mass absorption coefficient (Wikidata Q86202147))" + }, + { + "q": "I need the linear absorption coefficient for a mass absorption coefficient of 0.318734 and a mass density of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [0.318734, 9.566071] (the system mass absorption coefficient (Wikidata Q86202147))" + }, + { + "q": "If the mass absorption coefficient is 0.671697 and the mass density is 14.2496, what is the linear absorption coefficient?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [0.671697, 14.249569] (the system mass absorption coefficient (Wikidata Q86202147))" + }, + { + "q": "Could you work out the linear absorption coefficient given mass absorption coefficient = 0.671697, mass density = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [0.671697, 14.249569] (the system mass absorption coefficient (Wikidata Q86202147))" + }, + { + "q": "I need the linear absorption coefficient for a mass absorption coefficient of 21.7931 and a mass density of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [21.793126, 29.510609] (the system mass absorption coefficient (Wikidata Q86202147))" + }, + { + "q": "If the mass absorption coefficient is 21.7931 and the mass density is 29.5106, what is the linear absorption coefficient?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [21.793126, 29.510609] (the system mass absorption coefficient (Wikidata Q86202147))" + }, + { + "q": "Could you work out the linear absorption coefficient given mass absorption coefficient = 153.15, mass density = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [153.149965, 18.595315] (the system mass absorption coefficient (Wikidata Q86202147))" + }, + { + "q": "I need the linear absorption coefficient for a mass absorption coefficient of 153.15 and a mass density of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [153.149965, 18.595315] (the system mass absorption coefficient (Wikidata Q86202147))" + }, + { + "q": "If the mass absorption coefficient is 8.50728, what is the linear absorption coefficient?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Could you work out the linear absorption coefficient given mass absorption coefficient = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Do you know the linear absorption coefficient 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 linear absorption coefficient 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 linear absorption coefficient given mass absorption coefficient = -0.318734, mass density = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mass absorption coefficient: no example had one" + }, + { + "q": "What is a typical linear absorption coefficient?", + "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-linear-absorption-coefficient-2/program.py b/root/organs10/physics/specialists/solved-linear-absorption-coefficient-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..abc2b81d77b6d01972a73ace7a7a7d297fd28e12 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-absorption-coefficient-2/program.py @@ -0,0 +1,7 @@ +"""linear absorption coefficient from mass absorption coefficient, mass density (mass absorption coefficient (Wikidata Q86202147)) +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(mass_absorption_coefficient, mass_density): + return 1.0*mass_absorption_coefficient*mass_density diff --git a/root/organs10/physics/specialists/solved-linear-absorption-coefficient-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-linear-absorption-coefficient-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-absorption-coefficient-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-linear-absorption-coefficient/organ_formula_v2.py b/root/organs10/physics/specialists/solved-linear-absorption-coefficient/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-absorption-coefficient/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-linear-absorption-coefficient/panel.json b/root/organs10/physics/specialists/solved-linear-absorption-coefficient/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..7c19e377638c63c08fff855af42333c39f1d8fb6 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-absorption-coefficient/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the linear absorption coefficient for decadic absorbance 107.961 and length 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [107.960666, 59.4105] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Tell me the linear absorption coefficient with decadic absorbance of 107.961 and length of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [107.960666, 59.4105] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "What is the linear absorption coefficient for decadic absorbance 169.527 and length 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [169.526958, 280.773702] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Tell me the linear absorption coefficient with decadic absorbance of 169.527 and length of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [169.526958, 280.773702] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "What is the linear absorption coefficient for decadic absorbance 1.91195 and length 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [1.911952, 82.240072] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Tell me the linear absorption coefficient with decadic absorbance of 1.91195 and length of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [1.911952, 82.240072] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "What is the linear absorption coefficient for decadic absorbance 1.64379 and length 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [1.643795, 252.709024] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Tell me the linear absorption coefficient with decadic absorbance of 1.64379 and length of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [1.643795, 252.709024] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "What is the linear absorption coefficient for decadic absorbance 18.3169 and length 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [18.316867, 6.838598] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Tell me the linear absorption coefficient with decadic absorbance of 18.3169 and length of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [18.316867, 6.838598] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "What is the linear absorption coefficient for decadic absorbance 156.502 and length 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [156.50181, 185.886273] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Tell me the linear absorption coefficient with decadic absorbance of 156.502 and length of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [156.50181, 185.886273] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "What is the linear absorption coefficient for decadic absorbance 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the length is not given" + }, + { + "q": "Tell me the linear absorption coefficient with decadic absorbance of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the length is not given" + }, + { + "q": "What is the linear absorption coefficient 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 linear absorption coefficient 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 linear absorption coefficient with decadic absorbance of -1.91195 and length of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative decadic absorbance: no example had one" + }, + { + "q": "Which linear absorption coefficient 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 decadic absorbance is 8.50728 and the length is 17.7888, what is the linear absorption coefficient?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [8.507283, 17.788778] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Could you work out the linear absorption coefficient given decadic absorbance = 8.50728, length = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [8.507283, 17.788778] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "I need the linear absorption coefficient for a decadic absorbance of 8.24951 and a length of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [8.24951, 0.634021] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "If the decadic absorbance is 8.24951 and the length is 0.634021, what is the linear absorption coefficient?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [8.24951, 0.634021] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Could you work out the linear absorption coefficient given decadic absorbance = 0.318734, length = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [0.318734, 9.566071] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "I need the linear absorption coefficient for a decadic absorbance of 0.318734 and a length of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [0.318734, 9.566071] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "If the decadic absorbance is 0.671697 and the length is 14.2496, what is the linear absorption coefficient?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [0.671697, 14.249569] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Could you work out the linear absorption coefficient given decadic absorbance = 0.671697, length = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [0.671697, 14.249569] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "I need the linear absorption coefficient for a decadic absorbance of 21.7931 and a length of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [21.793126, 29.510609] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "If the decadic absorbance is 21.7931 and the length is 29.5106, what is the linear absorption coefficient?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [21.793126, 29.510609] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "Could you work out the linear absorption coefficient given decadic absorbance = 153.15, length = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [153.149965, 18.595315] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "I need the linear absorption coefficient for a decadic absorbance of 153.15 and a length of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1/m", + "why": "linear absorption coefficient for [153.149965, 18.595315] (the system linear absorption coefficient (Wikidata Q86204782))" + }, + { + "q": "If the decadic absorbance is 8.50728, what is the linear absorption coefficient?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the length is not given" + }, + { + "q": "Could you work out the linear absorption coefficient given decadic absorbance = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the length is not given" + }, + { + "q": "Do you know the linear absorption coefficient 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 linear absorption coefficient 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 linear absorption coefficient given decadic absorbance = -0.318734, length = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative decadic absorbance: no example had one" + }, + { + "q": "What is a typical linear absorption coefficient?", + "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-linear-absorption-coefficient/program.py b/root/organs10/physics/specialists/solved-linear-absorption-coefficient/program.py new file mode 100644 index 0000000000000000000000000000000000000000..882dd00986c0c3da735819d1dffcbe279454a207 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-absorption-coefficient/program.py @@ -0,0 +1,7 @@ +"""linear absorption coefficient from decadic absorbance, 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(decadic_absorbance, length): + return 1.0*decadic_absorbance/length diff --git a/root/organs10/physics/specialists/solved-linear-absorption-coefficient/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-linear-absorption-coefficient/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-absorption-coefficient/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-linear-attenuation-coefficien-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-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-linear-attenuation-coefficien-2/panel.json b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..fde7202a8309e269a6ade8846ba85bde7e53d2fb --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the linear attenuation coefficient for mass attenuation coefficient 107.961 and mass density 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [107.960666, 59.4105] (the system mass attenuation coefficient (Wikidata Q1907558))" + }, + { + "q": "Tell me the linear attenuation coefficient with mass attenuation coefficient of 107.961 and mass density of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [107.960666, 59.4105] (the system mass attenuation coefficient (Wikidata Q1907558))" + }, + { + "q": "What is the linear attenuation coefficient for mass attenuation coefficient 169.527 and mass density 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [169.526958, 280.773702] (the system mass attenuation coefficient (Wikidata Q1907558))" + }, + { + "q": "Tell me the linear attenuation coefficient with mass attenuation coefficient of 169.527 and mass density of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [169.526958, 280.773702] (the system mass attenuation coefficient (Wikidata Q1907558))" + }, + { + "q": "What is the linear attenuation coefficient for mass attenuation coefficient 1.91195 and mass density 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [1.911952, 82.240072] (the system mass attenuation coefficient (Wikidata Q1907558))" + }, + { + "q": "Tell me the linear attenuation coefficient with mass attenuation coefficient of 1.91195 and mass density of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [1.911952, 82.240072] (the system mass attenuation coefficient (Wikidata Q1907558))" + }, + { + "q": "What is the linear attenuation coefficient for mass attenuation coefficient 1.64379 and mass density 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [1.643795, 252.709024] (the system mass attenuation coefficient (Wikidata Q1907558))" + }, + { + "q": "Tell me the linear attenuation coefficient with mass attenuation coefficient of 1.64379 and mass density of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [1.643795, 252.709024] (the system mass attenuation coefficient (Wikidata Q1907558))" + }, + { + "q": "What is the linear attenuation coefficient for mass attenuation coefficient 18.3169 and mass density 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [18.316867, 6.838598] (the system mass attenuation coefficient (Wikidata Q1907558))" + }, + { + "q": "Tell me the linear attenuation coefficient with mass attenuation coefficient of 18.3169 and mass density of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [18.316867, 6.838598] (the system mass attenuation coefficient (Wikidata Q1907558))" + }, + { + "q": "What is the linear attenuation coefficient for mass attenuation coefficient 156.502 and mass density 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [156.50181, 185.886273] (the system mass attenuation coefficient (Wikidata Q1907558))" + }, + { + "q": "Tell me the linear attenuation coefficient with mass attenuation coefficient of 156.502 and mass density of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [156.50181, 185.886273] (the system mass attenuation coefficient (Wikidata Q1907558))" + }, + { + "q": "What is the linear attenuation coefficient for mass attenuation coefficient 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Tell me the linear attenuation coefficient with mass attenuation coefficient of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "What is the linear attenuation coefficient 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 linear attenuation coefficient 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 linear attenuation coefficient with mass attenuation coefficient of -1.91195 and mass density of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mass attenuation coefficient: no example had one" + }, + { + "q": "Which linear attenuation coefficient 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 attenuation coefficient is 8.50728 and the mass density is 17.7888, what is the linear attenuation coefficient?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [8.507283, 17.788778] (the system mass attenuation coefficient (Wikidata Q1907558))" + }, + { + "q": "Could you work out the linear attenuation coefficient given mass attenuation coefficient = 8.50728, mass density = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [8.507283, 17.788778] (the system mass attenuation coefficient (Wikidata Q1907558))" + }, + { + "q": "I need the linear attenuation coefficient for a mass attenuation coefficient of 8.24951 and a mass density of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [8.24951, 0.634021] (the system mass attenuation coefficient (Wikidata Q1907558))" + }, + { + "q": "If the mass attenuation coefficient is 8.24951 and the mass density is 0.634021, what is the linear attenuation coefficient?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [8.24951, 0.634021] (the system mass attenuation coefficient (Wikidata Q1907558))" + }, + { + "q": "Could you work out the linear attenuation coefficient given mass attenuation coefficient = 0.318734, mass density = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [0.318734, 9.566071] (the system mass attenuation coefficient (Wikidata Q1907558))" + }, + { + "q": "I need the linear attenuation coefficient for a mass attenuation coefficient of 0.318734 and a mass density of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [0.318734, 9.566071] (the system mass attenuation coefficient (Wikidata Q1907558))" + }, + { + "q": "If the mass attenuation coefficient is 0.671697 and the mass density is 14.2496, what is the linear attenuation coefficient?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [0.671697, 14.249569] (the system mass attenuation coefficient (Wikidata Q1907558))" + }, + { + "q": "Could you work out the linear attenuation coefficient given mass attenuation coefficient = 0.671697, mass density = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [0.671697, 14.249569] (the system mass attenuation coefficient (Wikidata Q1907558))" + }, + { + "q": "I need the linear attenuation coefficient for a mass attenuation coefficient of 21.7931 and a mass density of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [21.793126, 29.510609] (the system mass attenuation coefficient (Wikidata Q1907558))" + }, + { + "q": "If the mass attenuation coefficient is 21.7931 and the mass density is 29.5106, what is the linear attenuation coefficient?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [21.793126, 29.510609] (the system mass attenuation coefficient (Wikidata Q1907558))" + }, + { + "q": "Could you work out the linear attenuation coefficient given mass attenuation coefficient = 153.15, mass density = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [153.149965, 18.595315] (the system mass attenuation coefficient (Wikidata Q1907558))" + }, + { + "q": "I need the linear attenuation coefficient for a mass attenuation coefficient of 153.15 and a mass density of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [153.149965, 18.595315] (the system mass attenuation coefficient (Wikidata Q1907558))" + }, + { + "q": "If the mass attenuation coefficient is 8.50728, what is the linear attenuation coefficient?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Could you work out the linear attenuation coefficient given mass attenuation coefficient = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Do you know the linear attenuation coefficient 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 linear attenuation coefficient 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 linear attenuation coefficient given mass attenuation coefficient = -0.318734, mass density = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mass attenuation coefficient: no example had one" + }, + { + "q": "What is a typical linear attenuation coefficient?", + "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-linear-attenuation-coefficien-2/program.py b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..080ba232cacff6d266eae29930114ec9125d9f67 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-2/program.py @@ -0,0 +1,7 @@ +"""linear attenuation coefficient from mass attenuation coefficient, mass density (mass attenuation coefficient (Wikidata Q1907558)) +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(mass_attenuation_coefficient, mass_density): + return 1.0*mass_attenuation_coefficient*mass_density diff --git a/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-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-linear-attenuation-coefficien-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-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-linear-attenuation-coefficien-3/panel.json b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..3f6680596b209d53775a5a8edffe4b4fff5a99d3 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the linear attenuation coefficient for mass attenuation coefficient 107.961 and mass density 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [107.960666, 59.4105] (the system mass attenuation coefficient (Wikidata Q98591983))" + }, + { + "q": "Tell me the linear attenuation coefficient with mass attenuation coefficient of 107.961 and mass density of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [107.960666, 59.4105] (the system mass attenuation coefficient (Wikidata Q98591983))" + }, + { + "q": "What is the linear attenuation coefficient for mass attenuation coefficient 169.527 and mass density 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [169.526958, 280.773702] (the system mass attenuation coefficient (Wikidata Q98591983))" + }, + { + "q": "Tell me the linear attenuation coefficient with mass attenuation coefficient of 169.527 and mass density of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [169.526958, 280.773702] (the system mass attenuation coefficient (Wikidata Q98591983))" + }, + { + "q": "What is the linear attenuation coefficient for mass attenuation coefficient 1.91195 and mass density 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [1.911952, 82.240072] (the system mass attenuation coefficient (Wikidata Q98591983))" + }, + { + "q": "Tell me the linear attenuation coefficient with mass attenuation coefficient of 1.91195 and mass density of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [1.911952, 82.240072] (the system mass attenuation coefficient (Wikidata Q98591983))" + }, + { + "q": "What is the linear attenuation coefficient for mass attenuation coefficient 1.64379 and mass density 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [1.643795, 252.709024] (the system mass attenuation coefficient (Wikidata Q98591983))" + }, + { + "q": "Tell me the linear attenuation coefficient with mass attenuation coefficient of 1.64379 and mass density of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [1.643795, 252.709024] (the system mass attenuation coefficient (Wikidata Q98591983))" + }, + { + "q": "What is the linear attenuation coefficient for mass attenuation coefficient 18.3169 and mass density 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [18.316867, 6.838598] (the system mass attenuation coefficient (Wikidata Q98591983))" + }, + { + "q": "Tell me the linear attenuation coefficient with mass attenuation coefficient of 18.3169 and mass density of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [18.316867, 6.838598] (the system mass attenuation coefficient (Wikidata Q98591983))" + }, + { + "q": "What is the linear attenuation coefficient for mass attenuation coefficient 156.502 and mass density 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [156.50181, 185.886273] (the system mass attenuation coefficient (Wikidata Q98591983))" + }, + { + "q": "Tell me the linear attenuation coefficient with mass attenuation coefficient of 156.502 and mass density of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [156.50181, 185.886273] (the system mass attenuation coefficient (Wikidata Q98591983))" + }, + { + "q": "What is the linear attenuation coefficient for mass attenuation coefficient 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Tell me the linear attenuation coefficient with mass attenuation coefficient of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "What is the linear attenuation coefficient 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 linear attenuation coefficient 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 linear attenuation coefficient with mass attenuation coefficient of -1.91195 and mass density of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mass attenuation coefficient: no example had one" + }, + { + "q": "Which linear attenuation coefficient 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 attenuation coefficient is 8.50728 and the mass density is 17.7888, what is the linear attenuation coefficient?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [8.507283, 17.788778] (the system mass attenuation coefficient (Wikidata Q98591983))" + }, + { + "q": "Could you work out the linear attenuation coefficient given mass attenuation coefficient = 8.50728, mass density = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [8.507283, 17.788778] (the system mass attenuation coefficient (Wikidata Q98591983))" + }, + { + "q": "I need the linear attenuation coefficient for a mass attenuation coefficient of 8.24951 and a mass density of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [8.24951, 0.634021] (the system mass attenuation coefficient (Wikidata Q98591983))" + }, + { + "q": "If the mass attenuation coefficient is 8.24951 and the mass density is 0.634021, what is the linear attenuation coefficient?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [8.24951, 0.634021] (the system mass attenuation coefficient (Wikidata Q98591983))" + }, + { + "q": "Could you work out the linear attenuation coefficient given mass attenuation coefficient = 0.318734, mass density = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [0.318734, 9.566071] (the system mass attenuation coefficient (Wikidata Q98591983))" + }, + { + "q": "I need the linear attenuation coefficient for a mass attenuation coefficient of 0.318734 and a mass density of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [0.318734, 9.566071] (the system mass attenuation coefficient (Wikidata Q98591983))" + }, + { + "q": "If the mass attenuation coefficient is 0.671697 and the mass density is 14.2496, what is the linear attenuation coefficient?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [0.671697, 14.249569] (the system mass attenuation coefficient (Wikidata Q98591983))" + }, + { + "q": "Could you work out the linear attenuation coefficient given mass attenuation coefficient = 0.671697, mass density = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [0.671697, 14.249569] (the system mass attenuation coefficient (Wikidata Q98591983))" + }, + { + "q": "I need the linear attenuation coefficient for a mass attenuation coefficient of 21.7931 and a mass density of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [21.793126, 29.510609] (the system mass attenuation coefficient (Wikidata Q98591983))" + }, + { + "q": "If the mass attenuation coefficient is 21.7931 and the mass density is 29.5106, what is the linear attenuation coefficient?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [21.793126, 29.510609] (the system mass attenuation coefficient (Wikidata Q98591983))" + }, + { + "q": "Could you work out the linear attenuation coefficient given mass attenuation coefficient = 153.15, mass density = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [153.149965, 18.595315] (the system mass attenuation coefficient (Wikidata Q98591983))" + }, + { + "q": "I need the linear attenuation coefficient for a mass attenuation coefficient of 153.15 and a mass density of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [153.149965, 18.595315] (the system mass attenuation coefficient (Wikidata Q98591983))" + }, + { + "q": "If the mass attenuation coefficient is 8.50728, what is the linear attenuation coefficient?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Could you work out the linear attenuation coefficient given mass attenuation coefficient = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Do you know the linear attenuation coefficient 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 linear attenuation coefficient 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 linear attenuation coefficient given mass attenuation coefficient = -0.318734, mass density = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mass attenuation coefficient: no example had one" + }, + { + "q": "What is a typical linear attenuation coefficient?", + "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-linear-attenuation-coefficien-3/program.py b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..088a023b08dfb2e450b2c828ca21c81ce6239b6c --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-3/program.py @@ -0,0 +1,7 @@ +"""linear attenuation coefficient from mass attenuation coefficient, mass density (mass attenuation coefficient (Wikidata Q98591983)) +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(mass_attenuation_coefficient, mass_density): + return 1.0*mass_attenuation_coefficient*mass_density diff --git a/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-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-linear-attenuation-coefficien-4/organ_formula_v2.py b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-4/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-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-linear-attenuation-coefficien-4/panel.json b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-4/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..bfc8447b2117783c968405fa62bf5bc1c95324bb --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-4/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the linear attenuation coefficient for atomic attenuation coefficient 107.961 and number density of atoms 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [107.960666, 59.4105] (the system atomic attenuation coefficient (Wikidata Q98592911))" + }, + { + "q": "Tell me the linear attenuation coefficient with atomic attenuation coefficient of 107.961 and number density of atoms of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [107.960666, 59.4105] (the system atomic attenuation coefficient (Wikidata Q98592911))" + }, + { + "q": "What is the linear attenuation coefficient for atomic attenuation coefficient 169.527 and number density of atoms 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [169.526958, 280.773702] (the system atomic attenuation coefficient (Wikidata Q98592911))" + }, + { + "q": "Tell me the linear attenuation coefficient with atomic attenuation coefficient of 169.527 and number density of atoms of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [169.526958, 280.773702] (the system atomic attenuation coefficient (Wikidata Q98592911))" + }, + { + "q": "What is the linear attenuation coefficient for atomic attenuation coefficient 1.91195 and number density of atoms 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [1.911952, 82.240072] (the system atomic attenuation coefficient (Wikidata Q98592911))" + }, + { + "q": "Tell me the linear attenuation coefficient with atomic attenuation coefficient of 1.91195 and number density of atoms of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [1.911952, 82.240072] (the system atomic attenuation coefficient (Wikidata Q98592911))" + }, + { + "q": "What is the linear attenuation coefficient for atomic attenuation coefficient 1.64379 and number density of atoms 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [1.643795, 252.709024] (the system atomic attenuation coefficient (Wikidata Q98592911))" + }, + { + "q": "Tell me the linear attenuation coefficient with atomic attenuation coefficient of 1.64379 and number density of atoms of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [1.643795, 252.709024] (the system atomic attenuation coefficient (Wikidata Q98592911))" + }, + { + "q": "What is the linear attenuation coefficient for atomic attenuation coefficient 18.3169 and number density of atoms 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [18.316867, 6.838598] (the system atomic attenuation coefficient (Wikidata Q98592911))" + }, + { + "q": "Tell me the linear attenuation coefficient with atomic attenuation coefficient of 18.3169 and number density of atoms of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [18.316867, 6.838598] (the system atomic attenuation coefficient (Wikidata Q98592911))" + }, + { + "q": "What is the linear attenuation coefficient for atomic attenuation coefficient 156.502 and number density of atoms 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [156.50181, 185.886273] (the system atomic attenuation coefficient (Wikidata Q98592911))" + }, + { + "q": "Tell me the linear attenuation coefficient with atomic attenuation coefficient of 156.502 and number density of atoms of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [156.50181, 185.886273] (the system atomic attenuation coefficient (Wikidata Q98592911))" + }, + { + "q": "What is the linear attenuation coefficient for atomic attenuation coefficient 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the number density of atoms is not given" + }, + { + "q": "Tell me the linear attenuation coefficient with atomic attenuation coefficient of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the number density of atoms is not given" + }, + { + "q": "What is the linear attenuation coefficient 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 linear attenuation coefficient 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 linear attenuation coefficient with atomic attenuation coefficient of -1.91195 and number density of atoms of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative atomic attenuation coefficient: no example had one" + }, + { + "q": "Which linear attenuation coefficient 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 atomic attenuation coefficient is 8.50728 and the number density of atoms is 17.7888, what is the linear attenuation coefficient?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [8.507283, 17.788778] (the system atomic attenuation coefficient (Wikidata Q98592911))" + }, + { + "q": "Could you work out the linear attenuation coefficient given atomic attenuation coefficient = 8.50728, number density of atoms = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [8.507283, 17.788778] (the system atomic attenuation coefficient (Wikidata Q98592911))" + }, + { + "q": "I need the linear attenuation coefficient for an atomic attenuation coefficient of 8.24951 and a number density of atoms of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [8.24951, 0.634021] (the system atomic attenuation coefficient (Wikidata Q98592911))" + }, + { + "q": "If the atomic attenuation coefficient is 8.24951 and the number density of atoms is 0.634021, what is the linear attenuation coefficient?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [8.24951, 0.634021] (the system atomic attenuation coefficient (Wikidata Q98592911))" + }, + { + "q": "Could you work out the linear attenuation coefficient given atomic attenuation coefficient = 0.318734, number density of atoms = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [0.318734, 9.566071] (the system atomic attenuation coefficient (Wikidata Q98592911))" + }, + { + "q": "I need the linear attenuation coefficient for an atomic attenuation coefficient of 0.318734 and a number density of atoms of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [0.318734, 9.566071] (the system atomic attenuation coefficient (Wikidata Q98592911))" + }, + { + "q": "If the atomic attenuation coefficient is 0.671697 and the number density of atoms is 14.2496, what is the linear attenuation coefficient?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [0.671697, 14.249569] (the system atomic attenuation coefficient (Wikidata Q98592911))" + }, + { + "q": "Could you work out the linear attenuation coefficient given atomic attenuation coefficient = 0.671697, number density of atoms = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [0.671697, 14.249569] (the system atomic attenuation coefficient (Wikidata Q98592911))" + }, + { + "q": "I need the linear attenuation coefficient for an atomic attenuation coefficient of 21.7931 and a number density of atoms of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [21.793126, 29.510609] (the system atomic attenuation coefficient (Wikidata Q98592911))" + }, + { + "q": "If the atomic attenuation coefficient is 21.7931 and the number density of atoms is 29.5106, what is the linear attenuation coefficient?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [21.793126, 29.510609] (the system atomic attenuation coefficient (Wikidata Q98592911))" + }, + { + "q": "Could you work out the linear attenuation coefficient given atomic attenuation coefficient = 153.15, number density of atoms = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [153.149965, 18.595315] (the system atomic attenuation coefficient (Wikidata Q98592911))" + }, + { + "q": "I need the linear attenuation coefficient for an atomic attenuation coefficient of 153.15 and a number density of atoms of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [153.149965, 18.595315] (the system atomic attenuation coefficient (Wikidata Q98592911))" + }, + { + "q": "If the atomic attenuation coefficient is 8.50728, what is the linear attenuation coefficient?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the number density of atoms is not given" + }, + { + "q": "Could you work out the linear attenuation coefficient given atomic attenuation coefficient = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the number density of atoms is not given" + }, + { + "q": "Do you know the linear attenuation coefficient 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 linear attenuation coefficient 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 linear attenuation coefficient given atomic attenuation coefficient = -0.318734, number density of atoms = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative atomic attenuation coefficient: no example had one" + }, + { + "q": "What is a typical linear attenuation coefficient?", + "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-linear-attenuation-coefficien-4/program.py b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-4/program.py new file mode 100644 index 0000000000000000000000000000000000000000..621222cf80c978f5ea5cccab1a1e8ecbb517af54 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-4/program.py @@ -0,0 +1,7 @@ +"""linear attenuation coefficient from atomic attenuation coefficient, number density of atoms (atomic attenuation coefficient (Wikidata Q98592911)) +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(atomic_attenuation_coefficient, number_density_of_atoms): + return 1.0*atomic_attenuation_coefficient*number_density_of_atoms diff --git a/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-4/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-4/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien-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-linear-attenuation-coefficien/organ_formula_v2.py b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien/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-linear-attenuation-coefficien/panel.json b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..70062311b5787bb44db848e0d1ee98a025397785 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the linear attenuation coefficient for molar attenuation coefficient 107.961 and amount-of-substance concentration 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [107.960666, 59.4105] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "Tell me the linear attenuation coefficient with molar attenuation coefficient of 107.961 and amount-of-substance concentration of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [107.960666, 59.4105] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "What is the linear attenuation coefficient for molar attenuation coefficient 169.527 and amount-of-substance concentration 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [169.526958, 280.773702] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "Tell me the linear attenuation coefficient with molar attenuation coefficient of 169.527 and amount-of-substance concentration of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [169.526958, 280.773702] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "What is the linear attenuation coefficient for molar attenuation coefficient 1.91195 and amount-of-substance concentration 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [1.911952, 82.240072] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "Tell me the linear attenuation coefficient with molar attenuation coefficient of 1.91195 and amount-of-substance concentration of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [1.911952, 82.240072] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "What is the linear attenuation coefficient for molar attenuation coefficient 1.64379 and amount-of-substance concentration 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [1.643795, 252.709024] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "Tell me the linear attenuation coefficient with molar attenuation coefficient of 1.64379 and amount-of-substance concentration of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [1.643795, 252.709024] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "What is the linear attenuation coefficient for molar attenuation coefficient 18.3169 and amount-of-substance concentration 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [18.316867, 6.838598] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "Tell me the linear attenuation coefficient with molar attenuation coefficient of 18.3169 and amount-of-substance concentration of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [18.316867, 6.838598] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "What is the linear attenuation coefficient for molar attenuation coefficient 156.502 and amount-of-substance concentration 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [156.50181, 185.886273] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "Tell me the linear attenuation coefficient with molar attenuation coefficient of 156.502 and amount-of-substance concentration of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [156.50181, 185.886273] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "What is the linear attenuation coefficient for molar attenuation coefficient 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the amount-of-substance concentration is not given" + }, + { + "q": "Tell me the linear attenuation coefficient with molar attenuation coefficient of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the amount-of-substance concentration is not given" + }, + { + "q": "What is the linear attenuation coefficient 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 linear attenuation coefficient 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 linear attenuation coefficient with molar attenuation coefficient of -1.91195 and amount-of-substance concentration of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative molar attenuation coefficient: no example had one" + }, + { + "q": "Which linear attenuation coefficient 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 attenuation coefficient is 8.50728 and the amount-of-substance concentration is 17.7888, what is the linear attenuation coefficient?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [8.507283, 17.788778] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "Could you work out the linear attenuation coefficient given molar attenuation coefficient = 8.50728, amount-of-substance concentration = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [8.507283, 17.788778] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "I need the linear attenuation coefficient for a molar attenuation coefficient of 8.24951 and an amount-of-substance concentration of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [8.24951, 0.634021] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "If the molar attenuation coefficient is 8.24951 and the amount-of-substance concentration is 0.634021, what is the linear attenuation coefficient?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [8.24951, 0.634021] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "Could you work out the linear attenuation coefficient given molar attenuation coefficient = 0.318734, amount-of-substance concentration = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [0.318734, 9.566071] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "I need the linear attenuation coefficient for a molar attenuation coefficient of 0.318734 and an amount-of-substance concentration of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [0.318734, 9.566071] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "If the molar attenuation coefficient is 0.671697 and the amount-of-substance concentration is 14.2496, what is the linear attenuation coefficient?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [0.671697, 14.249569] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "Could you work out the linear attenuation coefficient given molar attenuation coefficient = 0.671697, amount-of-substance concentration = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [0.671697, 14.249569] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "I need the linear attenuation coefficient for a molar attenuation coefficient of 21.7931 and an amount-of-substance concentration of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [21.793126, 29.510609] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "If the molar attenuation coefficient is 21.7931 and the amount-of-substance concentration is 29.5106, what is the linear attenuation coefficient?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [21.793126, 29.510609] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "Could you work out the linear attenuation coefficient given molar attenuation coefficient = 153.15, amount-of-substance concentration = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [153.149965, 18.595315] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "I need the linear attenuation coefficient for a molar attenuation coefficient of 153.15 and an amount-of-substance concentration of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "1/m", + "why": "linear attenuation coefficient for [153.149965, 18.595315] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "If the molar attenuation coefficient is 8.50728, what is the linear attenuation coefficient?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the amount-of-substance concentration is not given" + }, + { + "q": "Could you work out the linear attenuation coefficient given molar attenuation coefficient = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the amount-of-substance concentration is not given" + }, + { + "q": "Do you know the linear attenuation coefficient 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 linear attenuation coefficient 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 linear attenuation coefficient given molar attenuation coefficient = -0.318734, amount-of-substance concentration = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative molar attenuation coefficient: no example had one" + }, + { + "q": "What is a typical linear attenuation coefficient?", + "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-linear-attenuation-coefficien/program.py b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien/program.py new file mode 100644 index 0000000000000000000000000000000000000000..bd066f48a214efd636f38e0729ea52943b6588c4 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien/program.py @@ -0,0 +1,7 @@ +"""linear attenuation coefficient from molar attenuation coefficient, amount-of-substance concentration (molar attenuation coefficient (Wikidata Q98592828)) +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(molar_attenuation_coefficient, amount_of_substance_concentration): + return 1.0*amount_of_substance_concentration*molar_attenuation_coefficient diff --git a/root/organs10/physics/specialists/solved-linear-attenuation-coefficien/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-attenuation-coefficien/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-linear-stopping-power-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-linear-stopping-power-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-stopping-power-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-linear-stopping-power-2/panel.json b/root/organs10/physics/specialists/solved-linear-stopping-power-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..4db3834fbd1cb637ce04a53b03656169035fabee --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-stopping-power-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the linear stopping power for total mass stopping power 107.961 and mass density 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [107.960666, 59.4105] (the system total mass stopping power (Wikidata Q98642795))" + }, + { + "q": "Tell me the linear stopping power with total mass stopping power of 107.961 and mass density of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [107.960666, 59.4105] (the system total mass stopping power (Wikidata Q98642795))" + }, + { + "q": "What is the linear stopping power for total mass stopping power 169.527 and mass density 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [169.526958, 280.773702] (the system total mass stopping power (Wikidata Q98642795))" + }, + { + "q": "Tell me the linear stopping power with total mass stopping power of 169.527 and mass density of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [169.526958, 280.773702] (the system total mass stopping power (Wikidata Q98642795))" + }, + { + "q": "What is the linear stopping power for total mass stopping power 1.91195 and mass density 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [1.911952, 82.240072] (the system total mass stopping power (Wikidata Q98642795))" + }, + { + "q": "Tell me the linear stopping power with total mass stopping power of 1.91195 and mass density of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [1.911952, 82.240072] (the system total mass stopping power (Wikidata Q98642795))" + }, + { + "q": "What is the linear stopping power for total mass stopping power 1.64379 and mass density 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [1.643795, 252.709024] (the system total mass stopping power (Wikidata Q98642795))" + }, + { + "q": "Tell me the linear stopping power with total mass stopping power of 1.64379 and mass density of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [1.643795, 252.709024] (the system total mass stopping power (Wikidata Q98642795))" + }, + { + "q": "What is the linear stopping power for total mass stopping power 18.3169 and mass density 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [18.316867, 6.838598] (the system total mass stopping power (Wikidata Q98642795))" + }, + { + "q": "Tell me the linear stopping power with total mass stopping power of 18.3169 and mass density of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [18.316867, 6.838598] (the system total mass stopping power (Wikidata Q98642795))" + }, + { + "q": "What is the linear stopping power for total mass stopping power 156.502 and mass density 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [156.50181, 185.886273] (the system total mass stopping power (Wikidata Q98642795))" + }, + { + "q": "Tell me the linear stopping power with total mass stopping power of 156.502 and mass density of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [156.50181, 185.886273] (the system total mass stopping power (Wikidata Q98642795))" + }, + { + "q": "What is the linear stopping power for total mass stopping power 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Tell me the linear stopping power with total mass stopping power of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "What is the linear stopping 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 linear stopping 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 linear stopping power with total mass stopping power of -1.91195 and mass density of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative total mass stopping power: no example had one" + }, + { + "q": "Which linear stopping 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 total mass stopping power is 8.50728 and the mass density is 17.7888, what is the linear stopping power?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [8.507283, 17.788778] (the system total mass stopping power (Wikidata Q98642795))" + }, + { + "q": "Could you work out the linear stopping power given total mass stopping power = 8.50728, mass density = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [8.507283, 17.788778] (the system total mass stopping power (Wikidata Q98642795))" + }, + { + "q": "I need the linear stopping power for a total mass stopping power of 8.24951 and a mass density of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [8.24951, 0.634021] (the system total mass stopping power (Wikidata Q98642795))" + }, + { + "q": "If the total mass stopping power is 8.24951 and the mass density is 0.634021, what is the linear stopping power?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [8.24951, 0.634021] (the system total mass stopping power (Wikidata Q98642795))" + }, + { + "q": "Could you work out the linear stopping power given total mass stopping power = 0.318734, mass density = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [0.318734, 9.566071] (the system total mass stopping power (Wikidata Q98642795))" + }, + { + "q": "I need the linear stopping power for a total mass stopping power of 0.318734 and a mass density of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [0.318734, 9.566071] (the system total mass stopping power (Wikidata Q98642795))" + }, + { + "q": "If the total mass stopping power is 0.671697 and the mass density is 14.2496, what is the linear stopping power?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [0.671697, 14.249569] (the system total mass stopping power (Wikidata Q98642795))" + }, + { + "q": "Could you work out the linear stopping power given total mass stopping power = 0.671697, mass density = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [0.671697, 14.249569] (the system total mass stopping power (Wikidata Q98642795))" + }, + { + "q": "I need the linear stopping power for a total mass stopping power of 21.7931 and a mass density of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [21.793126, 29.510609] (the system total mass stopping power (Wikidata Q98642795))" + }, + { + "q": "If the total mass stopping power is 21.7931 and the mass density is 29.5106, what is the linear stopping power?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [21.793126, 29.510609] (the system total mass stopping power (Wikidata Q98642795))" + }, + { + "q": "Could you work out the linear stopping power given total mass stopping power = 153.15, mass density = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [153.149965, 18.595315] (the system total mass stopping power (Wikidata Q98642795))" + }, + { + "q": "I need the linear stopping power for a total mass stopping power of 153.15 and a mass density of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [153.149965, 18.595315] (the system total mass stopping power (Wikidata Q98642795))" + }, + { + "q": "If the total mass stopping power is 8.50728, what is the linear stopping power?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Could you work out the linear stopping power given total mass stopping power = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Do you know the linear stopping 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 linear stopping 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 linear stopping power given total mass stopping power = -0.318734, mass density = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative total mass stopping power: no example had one" + }, + { + "q": "What is a typical linear stopping 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-linear-stopping-power-2/program.py b/root/organs10/physics/specialists/solved-linear-stopping-power-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..d7d1b96a8f449d9b86e304fc7f5f90d40a868d60 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-stopping-power-2/program.py @@ -0,0 +1,7 @@ +"""linear stopping power from total mass stopping power, mass density (total mass stopping power (Wikidata Q98642795)) +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(total_mass_stopping_power, mass_density): + return 1.0*mass_density*total_mass_stopping_power diff --git a/root/organs10/physics/specialists/solved-linear-stopping-power-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-linear-stopping-power-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-stopping-power-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-linear-stopping-power/organ_formula_v2.py b/root/organs10/physics/specialists/solved-linear-stopping-power/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-stopping-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-linear-stopping-power/panel.json b/root/organs10/physics/specialists/solved-linear-stopping-power/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..b17853875ad900e072498d11521c0c4287ddd507 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-stopping-power/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the linear stopping power for total atomic stopping power 107.961 and number density of atoms 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [107.960666, 59.4105] (the system total atomic stopping power (Wikidata Q112725871))" + }, + { + "q": "Tell me the linear stopping power with total atomic stopping power of 107.961 and number density of atoms of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [107.960666, 59.4105] (the system total atomic stopping power (Wikidata Q112725871))" + }, + { + "q": "What is the linear stopping power for total atomic stopping power 169.527 and number density of atoms 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [169.526958, 280.773702] (the system total atomic stopping power (Wikidata Q112725871))" + }, + { + "q": "Tell me the linear stopping power with total atomic stopping power of 169.527 and number density of atoms of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [169.526958, 280.773702] (the system total atomic stopping power (Wikidata Q112725871))" + }, + { + "q": "What is the linear stopping power for total atomic stopping power 1.91195 and number density of atoms 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [1.911952, 82.240072] (the system total atomic stopping power (Wikidata Q112725871))" + }, + { + "q": "Tell me the linear stopping power with total atomic stopping power of 1.91195 and number density of atoms of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [1.911952, 82.240072] (the system total atomic stopping power (Wikidata Q112725871))" + }, + { + "q": "What is the linear stopping power for total atomic stopping power 1.64379 and number density of atoms 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [1.643795, 252.709024] (the system total atomic stopping power (Wikidata Q112725871))" + }, + { + "q": "Tell me the linear stopping power with total atomic stopping power of 1.64379 and number density of atoms of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [1.643795, 252.709024] (the system total atomic stopping power (Wikidata Q112725871))" + }, + { + "q": "What is the linear stopping power for total atomic stopping power 18.3169 and number density of atoms 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [18.316867, 6.838598] (the system total atomic stopping power (Wikidata Q112725871))" + }, + { + "q": "Tell me the linear stopping power with total atomic stopping power of 18.3169 and number density of atoms of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [18.316867, 6.838598] (the system total atomic stopping power (Wikidata Q112725871))" + }, + { + "q": "What is the linear stopping power for total atomic stopping power 156.502 and number density of atoms 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [156.50181, 185.886273] (the system total atomic stopping power (Wikidata Q112725871))" + }, + { + "q": "Tell me the linear stopping power with total atomic stopping power of 156.502 and number density of atoms of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [156.50181, 185.886273] (the system total atomic stopping power (Wikidata Q112725871))" + }, + { + "q": "What is the linear stopping power for total atomic stopping power 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the number density of atoms is not given" + }, + { + "q": "Tell me the linear stopping power with total atomic stopping power of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the number density of atoms is not given" + }, + { + "q": "What is the linear stopping 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 linear stopping 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 linear stopping power with total atomic stopping power of -1.91195 and number density of atoms of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative total atomic stopping power: no example had one" + }, + { + "q": "Which linear stopping 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 total atomic stopping power is 8.50728 and the number density of atoms is 17.7888, what is the linear stopping power?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [8.507283, 17.788778] (the system total atomic stopping power (Wikidata Q112725871))" + }, + { + "q": "Could you work out the linear stopping power given total atomic stopping power = 8.50728, number density of atoms = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [8.507283, 17.788778] (the system total atomic stopping power (Wikidata Q112725871))" + }, + { + "q": "I need the linear stopping power for a total atomic stopping power of 8.24951 and a number density of atoms of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [8.24951, 0.634021] (the system total atomic stopping power (Wikidata Q112725871))" + }, + { + "q": "If the total atomic stopping power is 8.24951 and the number density of atoms is 0.634021, what is the linear stopping power?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [8.24951, 0.634021] (the system total atomic stopping power (Wikidata Q112725871))" + }, + { + "q": "Could you work out the linear stopping power given total atomic stopping power = 0.318734, number density of atoms = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [0.318734, 9.566071] (the system total atomic stopping power (Wikidata Q112725871))" + }, + { + "q": "I need the linear stopping power for a total atomic stopping power of 0.318734 and a number density of atoms of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [0.318734, 9.566071] (the system total atomic stopping power (Wikidata Q112725871))" + }, + { + "q": "If the total atomic stopping power is 0.671697 and the number density of atoms is 14.2496, what is the linear stopping power?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [0.671697, 14.249569] (the system total atomic stopping power (Wikidata Q112725871))" + }, + { + "q": "Could you work out the linear stopping power given total atomic stopping power = 0.671697, number density of atoms = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [0.671697, 14.249569] (the system total atomic stopping power (Wikidata Q112725871))" + }, + { + "q": "I need the linear stopping power for a total atomic stopping power of 21.7931 and a number density of atoms of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [21.793126, 29.510609] (the system total atomic stopping power (Wikidata Q112725871))" + }, + { + "q": "If the total atomic stopping power is 21.7931 and the number density of atoms is 29.5106, what is the linear stopping power?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [21.793126, 29.510609] (the system total atomic stopping power (Wikidata Q112725871))" + }, + { + "q": "Could you work out the linear stopping power given total atomic stopping power = 153.15, number density of atoms = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [153.149965, 18.595315] (the system total atomic stopping power (Wikidata Q112725871))" + }, + { + "q": "I need the linear stopping power for a total atomic stopping power of 153.15 and a number density of atoms of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "N", + "why": "linear stopping power for [153.149965, 18.595315] (the system total atomic stopping power (Wikidata Q112725871))" + }, + { + "q": "If the total atomic stopping power is 8.50728, what is the linear stopping power?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the number density of atoms is not given" + }, + { + "q": "Could you work out the linear stopping power given total atomic stopping power = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the number density of atoms is not given" + }, + { + "q": "Do you know the linear stopping 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 linear stopping 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 linear stopping power given total atomic stopping power = -0.318734, number density of atoms = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative total atomic stopping power: no example had one" + }, + { + "q": "What is a typical linear stopping 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-linear-stopping-power/program.py b/root/organs10/physics/specialists/solved-linear-stopping-power/program.py new file mode 100644 index 0000000000000000000000000000000000000000..15ad6ca6f40b49c8c6d6b872e43932c6ef63e370 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-stopping-power/program.py @@ -0,0 +1,7 @@ +"""linear stopping power from total atomic stopping power, number density of atoms (total atomic stopping power (Wikidata Q112725871)) +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(total_atomic_stopping_power, number_density_of_atoms): + return 1.0*number_density_of_atoms*total_atomic_stopping_power diff --git a/root/organs10/physics/specialists/solved-linear-stopping-power/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-linear-stopping-power/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-stopping-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-linear-strain-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-linear-strain-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-strain-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-linear-strain-2/panel.json b/root/organs10/physics/specialists/solved-linear-strain-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..6d3d882fbe4db6d5ecb72ab58f666d4b1fd96817 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-strain-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the linear strain for Young's modulus 107.961 and normal stress 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [107.960666, 59.4105] (the system Young's modulus (Wikidata Q2091584))" + }, + { + "q": "Tell me the linear strain with Young's modulus of 107.961 and normal stress of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [107.960666, 59.4105] (the system Young's modulus (Wikidata Q2091584))" + }, + { + "q": "What is the linear strain for Young's modulus 169.527 and normal stress 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [169.526958, 280.773702] (the system Young's modulus (Wikidata Q2091584))" + }, + { + "q": "Tell me the linear strain with Young's modulus of 169.527 and normal stress of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [169.526958, 280.773702] (the system Young's modulus (Wikidata Q2091584))" + }, + { + "q": "What is the linear strain for Young's modulus 1.91195 and normal stress 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [1.911952, 82.240072] (the system Young's modulus (Wikidata Q2091584))" + }, + { + "q": "Tell me the linear strain with Young's modulus of 1.91195 and normal stress of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [1.911952, 82.240072] (the system Young's modulus (Wikidata Q2091584))" + }, + { + "q": "What is the linear strain for Young's modulus 1.64379 and normal stress 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [1.643795, 252.709024] (the system Young's modulus (Wikidata Q2091584))" + }, + { + "q": "Tell me the linear strain with Young's modulus of 1.64379 and normal stress of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [1.643795, 252.709024] (the system Young's modulus (Wikidata Q2091584))" + }, + { + "q": "What is the linear strain for Young's modulus 18.3169 and normal stress 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [18.316867, 6.838598] (the system Young's modulus (Wikidata Q2091584))" + }, + { + "q": "Tell me the linear strain with Young's modulus of 18.3169 and normal stress of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [18.316867, 6.838598] (the system Young's modulus (Wikidata Q2091584))" + }, + { + "q": "What is the linear strain for Young's modulus 156.502 and normal stress 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [156.50181, 185.886273] (the system Young's modulus (Wikidata Q2091584))" + }, + { + "q": "Tell me the linear strain with Young's modulus of 156.502 and normal stress of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [156.50181, 185.886273] (the system Young's modulus (Wikidata Q2091584))" + }, + { + "q": "What is the linear strain for Young's modulus 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the normal stress is not given" + }, + { + "q": "Tell me the linear strain with Young's modulus of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the normal stress is not given" + }, + { + "q": "What is the linear strain 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 linear strain 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 linear strain with Young's modulus of -1.91195 and normal stress of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Young's modulus: no example had one" + }, + { + "q": "Which linear strain 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 Young's modulus is 8.50728 and the normal stress is 17.7888, what is the linear strain?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [8.507283, 17.788778] (the system Young's modulus (Wikidata Q2091584))" + }, + { + "q": "Could you work out the linear strain given Young's modulus = 8.50728, normal stress = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [8.507283, 17.788778] (the system Young's modulus (Wikidata Q2091584))" + }, + { + "q": "I need the linear strain for a Young's modulus of 8.24951 and a normal stress of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [8.24951, 0.634021] (the system Young's modulus (Wikidata Q2091584))" + }, + { + "q": "If the Young's modulus is 8.24951 and the normal stress is 0.634021, what is the linear strain?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [8.24951, 0.634021] (the system Young's modulus (Wikidata Q2091584))" + }, + { + "q": "Could you work out the linear strain given Young's modulus = 0.318734, normal stress = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [0.318734, 9.566071] (the system Young's modulus (Wikidata Q2091584))" + }, + { + "q": "I need the linear strain for a Young's modulus of 0.318734 and a normal stress of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [0.318734, 9.566071] (the system Young's modulus (Wikidata Q2091584))" + }, + { + "q": "If the Young's modulus is 0.671697 and the normal stress is 14.2496, what is the linear strain?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [0.671697, 14.249569] (the system Young's modulus (Wikidata Q2091584))" + }, + { + "q": "Could you work out the linear strain given Young's modulus = 0.671697, normal stress = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [0.671697, 14.249569] (the system Young's modulus (Wikidata Q2091584))" + }, + { + "q": "I need the linear strain for a Young's modulus of 21.7931 and a normal stress of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [21.793126, 29.510609] (the system Young's modulus (Wikidata Q2091584))" + }, + { + "q": "If the Young's modulus is 21.7931 and the normal stress is 29.5106, what is the linear strain?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [21.793126, 29.510609] (the system Young's modulus (Wikidata Q2091584))" + }, + { + "q": "Could you work out the linear strain given Young's modulus = 153.15, normal stress = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [153.149965, 18.595315] (the system Young's modulus (Wikidata Q2091584))" + }, + { + "q": "I need the linear strain for a Young's modulus of 153.15 and a normal stress of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [153.149965, 18.595315] (the system Young's modulus (Wikidata Q2091584))" + }, + { + "q": "If the Young's modulus is 8.50728, what is the linear strain?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the normal stress is not given" + }, + { + "q": "Could you work out the linear strain given Young's modulus = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the normal stress is not given" + }, + { + "q": "Do you know the linear strain 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 linear strain 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 linear strain given Young's modulus = -0.318734, normal stress = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Young's modulus: no example had one" + }, + { + "q": "What is a typical linear strain?", + "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-linear-strain-2/program.py b/root/organs10/physics/specialists/solved-linear-strain-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..f0a5bf00312ed14cace360b23dae45ae23dfdff3 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-strain-2/program.py @@ -0,0 +1,7 @@ +"""linear strain from Young's modulus, normal stress (Young's modulus (Wikidata Q2091584)) +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(young_s_modulus, normal_stress): + return 1.0*normal_stress/young_s_modulus diff --git a/root/organs10/physics/specialists/solved-linear-strain-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-linear-strain-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-strain-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-linear-strain/organ_formula_v2.py b/root/organs10/physics/specialists/solved-linear-strain/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-strain/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-linear-strain/panel.json b/root/organs10/physics/specialists/solved-linear-strain/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..74c329d9d8ad056e13bb2b3b615f71481f1955d3 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-strain/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the linear strain for change in length 107.961 and length 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [107.960666, 59.4105] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "Tell me the linear strain with change in length of 107.961 and length of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [107.960666, 59.4105] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "What is the linear strain for change in length 169.527 and length 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [169.526958, 280.773702] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "Tell me the linear strain with change in length of 169.527 and length of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [169.526958, 280.773702] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "What is the linear strain for change in length 1.91195 and length 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [1.911952, 82.240072] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "Tell me the linear strain with change in length of 1.91195 and length of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [1.911952, 82.240072] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "What is the linear strain for change in length 1.64379 and length 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [1.643795, 252.709024] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "Tell me the linear strain with change in length of 1.64379 and length of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [1.643795, 252.709024] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "What is the linear strain for change in length 18.3169 and length 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [18.316867, 6.838598] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "Tell me the linear strain with change in length of 18.3169 and length of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [18.316867, 6.838598] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "What is the linear strain for change in length 156.502 and length 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [156.50181, 185.886273] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "Tell me the linear strain with change in length of 156.502 and length of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [156.50181, 185.886273] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "What is the linear strain for change in length 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the length is not given" + }, + { + "q": "Tell me the linear strain with change in length of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the length is not given" + }, + { + "q": "What is the linear strain 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 linear strain 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 linear strain with change in length of -1.91195 and length of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative change in length: no example had one" + }, + { + "q": "Which linear strain 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 change in length is 8.50728 and the length is 17.7888, what is the linear strain?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [8.507283, 17.788778] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "Could you work out the linear strain given change in length = 8.50728, length = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [8.507283, 17.788778] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "I need the linear strain for a change in length of 8.24951 and a length of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [8.24951, 0.634021] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "If the change in length is 8.24951 and the length is 0.634021, what is the linear strain?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [8.24951, 0.634021] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "Could you work out the linear strain given change in length = 0.318734, length = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [0.318734, 9.566071] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "I need the linear strain for a change in length of 0.318734 and a length of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [0.318734, 9.566071] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "If the change in length is 0.671697 and the length is 14.2496, what is the linear strain?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [0.671697, 14.249569] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "Could you work out the linear strain given change in length = 0.671697, length = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [0.671697, 14.249569] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "I need the linear strain for a change in length of 21.7931 and a length of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [21.793126, 29.510609] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "If the change in length is 21.7931 and the length is 29.5106, what is the linear strain?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [21.793126, 29.510609] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "Could you work out the linear strain given change in length = 153.15, length = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [153.149965, 18.595315] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "I need the linear strain for a change in length of 153.15 and a length of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "linear strain for [153.149965, 18.595315] (the system linear strain (Wikidata Q1990546))" + }, + { + "q": "If the change in length is 8.50728, what is the linear strain?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the length is not given" + }, + { + "q": "Could you work out the linear strain given change in length = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the length is not given" + }, + { + "q": "Do you know the linear strain 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 linear strain 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 linear strain given change in length = -0.318734, length = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative change in length: no example had one" + }, + { + "q": "What is a typical linear strain?", + "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-linear-strain/program.py b/root/organs10/physics/specialists/solved-linear-strain/program.py new file mode 100644 index 0000000000000000000000000000000000000000..c7fed1375ae5d88a0379247c06e57f7de000e4b2 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-strain/program.py @@ -0,0 +1,7 @@ +"""linear strain from change in length, length (linear strain (Wikidata Q1990546)) +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(change_in_length, length): + return 1.0*change_in_length/length diff --git a/root/organs10/physics/specialists/solved-linear-strain/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-linear-strain/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-linear-strain/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-liquid-density/organ_formula_v2.py b/root/organs10/physics/specialists/solved-liquid-density/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-liquid-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-liquid-density/panel.json b/root/organs10/physics/specialists/solved-liquid-density/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..c803e58af0132f62f933bfb41a87b8c10e136278 --- /dev/null +++ b/root/organs10/physics/specialists/solved-liquid-density/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the liquid density for liquid phase mass flow rate 107.961, gas phase mass flow rate 59.4105, gas density 5.77717 and Lockhart-Martinelli parameter 1.89126?", + "a": "5.333581457163758", + "valid": true, + "kind": "compute", + "expect": 5.333581457163758, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "liquid density for [107.960666, 59.4105, 5.777165, 1.891256] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Tell me the liquid density with liquid phase mass flow rate of 107.961, gas phase mass flow rate of 59.4105, gas density of 5.77717 and Lockhart-Martinelli parameter of 1.89126.", + "a": "5.333581457163758", + "valid": true, + "kind": "compute", + "expect": 5.333581457163758, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "liquid density for [107.960666, 59.4105, 5.777165, 1.891256] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "What is the liquid density for liquid phase mass flow rate 2.69428, gas phase mass flow rate 48.921, gas density 157.223 and Lockhart-Martinelli parameter 35.6412?", + "a": "0.00037541031273561514", + "valid": true, + "kind": "compute", + "expect": 0.00037541031273561514, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "liquid density for [2.69428, 48.920991, 157.22256, 35.641157] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Tell me the liquid density with liquid phase mass flow rate of 2.69428, gas phase mass flow rate of 48.921, gas density of 157.223 and Lockhart-Martinelli parameter of 35.6412.", + "a": "0.00037541031273561514", + "valid": true, + "kind": "compute", + "expect": 0.00037541031273561514, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "liquid density for [2.69428, 48.920991, 157.22256, 35.641157] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "What is the liquid density for liquid phase mass flow rate 0.318734, gas phase mass flow rate 9.56607, gas density 126.709 and Lockhart-Martinelli parameter 1.70503?", + "a": "0.048387455202092436", + "valid": true, + "kind": "compute", + "expect": 0.048387455202092436, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "liquid density for [0.318734, 9.566071, 126.709252, 1.705032] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Tell me the liquid density with liquid phase mass flow rate of 0.318734, gas phase mass flow rate of 9.56607, gas density of 126.709 and Lockhart-Martinelli parameter of 1.70503.", + "a": "0.048387455202092436", + "valid": true, + "kind": "compute", + "expect": 0.048387455202092436, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "liquid density for [0.318734, 9.566071, 126.709252, 1.705032] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "What is the liquid density for liquid phase mass flow rate 87.781, gas phase mass flow rate 13.2084, gas density 246.329 and Lockhart-Martinelli parameter 20.3966?", + "a": "26.151789271152218", + "valid": true, + "kind": "compute", + "expect": 26.151789271152218, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "liquid density for [87.781013, 13.208419, 246.329053, 20.396558] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Tell me the liquid density with liquid phase mass flow rate of 87.781, gas phase mass flow rate of 13.2084, gas density of 246.329 and Lockhart-Martinelli parameter of 20.3966.", + "a": "26.151789271152218", + "valid": true, + "kind": "compute", + "expect": 26.151789271152218, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "liquid density for [87.781013, 13.208419, 246.329053, 20.396558] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "What is the liquid density for liquid phase mass flow rate 156.502, gas phase mass flow rate 185.886, gas density 13.2373 and Lockhart-Martinelli parameter 4.71941?", + "a": "0.4212768191772553", + "valid": true, + "kind": "compute", + "expect": 0.4212768191772553, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "liquid density for [156.50181, 185.886273, 13.237311, 4.719415] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Tell me the liquid density with liquid phase mass flow rate of 156.502, gas phase mass flow rate of 185.886, gas density of 13.2373 and Lockhart-Martinelli parameter of 4.71941.", + "a": "0.4212768191772553", + "valid": true, + "kind": "compute", + "expect": 0.4212768191772553, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "liquid density for [156.50181, 185.886273, 13.237311, 4.719415] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "What is the liquid density for liquid phase mass flow rate 24.5721, gas phase mass flow rate 108.451, gas density 1.69476 and Lockhart-Martinelli parameter 49.4843?", + "a": "3.5529584598920255e-05", + "valid": true, + "kind": "compute", + "expect": 3.5529584598920255e-05, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "liquid density for [24.572086, 108.450792, 1.694755, 49.484326] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Tell me the liquid density with liquid phase mass flow rate of 24.5721, gas phase mass flow rate of 108.451, gas density of 1.69476 and Lockhart-Martinelli parameter of 49.4843.", + "a": "3.5529584598920255e-05", + "valid": true, + "kind": "compute", + "expect": 3.5529584598920255e-05, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "liquid density for [24.572086, 108.450792, 1.694755, 49.484326] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "What is the liquid density for liquid phase mass flow rate 107.961, gas phase mass flow rate 59.4105 and gas density 5.77717?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Lockhart-Martinelli parameter is not given" + }, + { + "q": "Tell me the liquid density with liquid phase mass flow rate of 2.69428, gas phase mass flow rate of 48.921 and gas density of 157.223.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Lockhart-Martinelli parameter is not given" + }, + { + "q": "What is the liquid 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 liquid 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 liquid density with liquid phase mass flow rate of -0.318734, gas phase mass flow rate of 9.56607, gas density of 126.709 and Lockhart-Martinelli parameter of 1.70503.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative liquid phase mass flow rate: no example had one" + }, + { + "q": "Which liquid 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 liquid phase mass flow rate is 167.625, gas phase mass flow rate is 10.3291, gas density is 2.21571 and the Lockhart-Martinelli parameter is 58.5346, what is the liquid density?", + "a": "0.17031170021970637", + "valid": true, + "kind": "compute", + "expect": 0.17031170021970637, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "liquid density for [167.624948, 10.329054, 2.215713, 58.534594] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Could you work out the liquid density given liquid phase mass flow rate = 167.625, gas phase mass flow rate = 10.3291, gas density = 2.21571, Lockhart-Martinelli parameter = 58.5346?", + "a": "0.17031170021970637", + "valid": true, + "kind": "compute", + "expect": 0.17031170021970637, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "liquid density for [167.624948, 10.329054, 2.215713, 58.534594] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "I need the liquid density for a liquid phase mass flow rate of 8.53158, a gas phase mass flow rate of 124.718, a gas density of 1.91195 and a Lockhart-Martinelli parameter of 82.2401, please.", + "a": "1.3228453095894445e-06", + "valid": true, + "kind": "compute", + "expect": 1.3228453095894445e-06, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "liquid density for [8.531577, 124.718185, 1.911952, 82.240072] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "If the liquid phase mass flow rate is 8.53158, gas phase mass flow rate is 124.718, gas density is 1.91195 and the Lockhart-Martinelli parameter is 82.2401, what is the liquid density?", + "a": "1.3228453095894445e-06", + "valid": true, + "kind": "compute", + "expect": 1.3228453095894445e-06, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "liquid density for [8.531577, 124.718185, 1.911952, 82.240072] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Could you work out the liquid density given liquid phase mass flow rate = 81.1967, gas phase mass flow rate = 6.98086, gas density = 0.551236, Lockhart-Martinelli parameter = 2.88512?", + "a": "8.959199650337547", + "valid": true, + "kind": "compute", + "expect": 8.959199650337547, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "liquid density for [81.196705, 6.980857, 0.551236, 2.88512] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "I need the liquid density for a liquid phase mass flow rate of 81.1967, a gas phase mass flow rate of 6.98086, a gas density of 0.551236 and a Lockhart-Martinelli parameter of 2.88512, please.", + "a": "8.959199650337547", + "valid": true, + "kind": "compute", + "expect": 8.959199650337547, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "liquid density for [81.196705, 6.980857, 0.551236, 2.88512] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "If the liquid phase mass flow rate is 1.16997, gas phase mass flow rate is 1.14865, gas density is 21.7931 and the Lockhart-Martinelli parameter is 29.5106, what is the liquid density?", + "a": "0.02596175262545714", + "valid": true, + "kind": "compute", + "expect": 0.02596175262545714, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "liquid density for [1.169968, 1.148652, 21.793126, 29.510609] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Could you work out the liquid density given liquid phase mass flow rate = 1.16997, gas phase mass flow rate = 1.14865, gas density = 21.7931, Lockhart-Martinelli parameter = 29.5106?", + "a": "0.02596175262545714", + "valid": true, + "kind": "compute", + "expect": 0.02596175262545714, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "liquid density for [1.169968, 1.148652, 21.793126, 29.510609] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "I need the liquid density for a liquid phase mass flow rate of 223.509, a gas phase mass flow rate of 17.3414, a gas density of 7.10705 and a Lockhart-Martinelli parameter of 30.2507, please.", + "a": "1.290138165923251", + "valid": true, + "kind": "compute", + "expect": 1.290138165923251, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "liquid density for [223.508796, 17.341435, 7.107049, 30.250749] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "If the liquid phase mass flow rate is 223.509, gas phase mass flow rate is 17.3414, gas density is 7.10705 and the Lockhart-Martinelli parameter is 30.2507, what is the liquid density?", + "a": "1.290138165923251", + "valid": true, + "kind": "compute", + "expect": 1.290138165923251, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "liquid density for [223.508796, 17.341435, 7.107049, 30.250749] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Could you work out the liquid density given liquid phase mass flow rate = 0.869111, gas phase mass flow rate = 39.1753, gas density = 0.432217, Lockhart-Martinelli parameter = 16.6571?", + "a": "7.667134381615377e-07", + "valid": true, + "kind": "compute", + "expect": 7.667134381615377e-07, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "liquid density for [0.869111, 39.175263, 0.432217, 16.657051] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "I need the liquid density for a liquid phase mass flow rate of 0.869111, a gas phase mass flow rate of 39.1753, a gas density of 0.432217 and a Lockhart-Martinelli parameter of 16.6571, please.", + "a": "7.667134381615377e-07", + "valid": true, + "kind": "compute", + "expect": 7.667134381615377e-07, + "dtype": "quantity", + "unit": "kg/m^3", + "why": "liquid density for [0.869111, 39.175263, 0.432217, 16.657051] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "If the liquid phase mass flow rate is 167.625, gas phase mass flow rate is 10.3291 and the gas density is 2.21571, what is the liquid density?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Lockhart-Martinelli parameter is not given" + }, + { + "q": "Could you work out the liquid density given liquid phase mass flow rate = 8.53158, gas phase mass flow rate = 124.718, gas density = 1.91195?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Lockhart-Martinelli parameter is not given" + }, + { + "q": "Do you know the liquid 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 liquid 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 liquid density given liquid phase mass flow rate = -81.1967, gas phase mass flow rate = 6.98086, gas density = 0.551236, Lockhart-Martinelli parameter = 2.88512?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative liquid phase mass flow rate: no example had one" + }, + { + "q": "What is a typical liquid 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-liquid-density/program.py b/root/organs10/physics/specialists/solved-liquid-density/program.py new file mode 100644 index 0000000000000000000000000000000000000000..ed2938e993bcb324fa8168674046ea8bf4d8c1ca --- /dev/null +++ b/root/organs10/physics/specialists/solved-liquid-density/program.py @@ -0,0 +1,7 @@ +"""liquid density from liquid phase mass flow rate, gas phase mass flow rate, gas density, Lockhart-Martinelli parameter (Lockhart-Martinelli parameter (Wikidata Q29211)) +The maths organ (systems.py): solved from the system q4 = ((q0)/(q1))*sqrt(((q2)/(q3))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(liquid_phase_mass_flow_rate, gas_phase_mass_flow_rate, gas_density, lockhart_martinelli_parameter): + return 1.0*gas_density*liquid_phase_mass_flow_rate**2/(gas_phase_mass_flow_rate**2*lockhart_martinelli_parameter**2) diff --git a/root/organs10/physics/specialists/solved-liquid-density/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-liquid-density/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-liquid-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-liquid-phase-mass-flow-rate/organ_formula_v2.py b/root/organs10/physics/specialists/solved-liquid-phase-mass-flow-rate/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-liquid-phase-mass-flow-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-liquid-phase-mass-flow-rate/panel.json b/root/organs10/physics/specialists/solved-liquid-phase-mass-flow-rate/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..a8c5222e0631c0be39a0e93c47f79597697cfe0f --- /dev/null +++ b/root/organs10/physics/specialists/solved-liquid-phase-mass-flow-rate/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the liquid phase mass flow rate for gas phase mass flow rate 107.961, gas density 59.4105, liquid density 5.77717 and Lockhart-Martinelli parameter 1.89126?", + "a": "63.67098555287779", + "valid": true, + "kind": "compute", + "expect": 63.67098555287779, + "dtype": "quantity", + "unit": "kg/s", + "why": "liquid phase mass flow rate for [107.960666, 59.4105, 5.777165, 1.891256] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Tell me the liquid phase mass flow rate with gas phase mass flow rate of 107.961, gas density of 59.4105, liquid density of 5.77717 and Lockhart-Martinelli parameter of 1.89126.", + "a": "63.67098555287779", + "valid": true, + "kind": "compute", + "expect": 63.67098555287779, + "dtype": "quantity", + "unit": "kg/s", + "why": "liquid phase mass flow rate for [107.960666, 59.4105, 5.777165, 1.891256] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "What is the liquid phase mass flow rate for gas phase mass flow rate 2.69428, gas density 48.921, liquid density 157.223 and Lockhart-Martinelli parameter 35.6412?", + "a": "172.14892429019324", + "valid": true, + "kind": "compute", + "expect": 172.14892429019324, + "dtype": "quantity", + "unit": "kg/s", + "why": "liquid phase mass flow rate for [2.69428, 48.920991, 157.22256, 35.641157] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Tell me the liquid phase mass flow rate with gas phase mass flow rate of 2.69428, gas density of 48.921, liquid density of 157.223 and Lockhart-Martinelli parameter of 35.6412.", + "a": "172.14892429019324", + "valid": true, + "kind": "compute", + "expect": 172.14892429019324, + "dtype": "quantity", + "unit": "kg/s", + "why": "liquid phase mass flow rate for [2.69428, 48.920991, 157.22256, 35.641157] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "What is the liquid phase mass flow rate for gas phase mass flow rate 0.318734, gas density 9.56607, liquid density 126.709 and Lockhart-Martinelli parameter 1.70503?", + "a": "1.9778732286593947", + "valid": true, + "kind": "compute", + "expect": 1.9778732286593947, + "dtype": "quantity", + "unit": "kg/s", + "why": "liquid phase mass flow rate for [0.318734, 9.566071, 126.709252, 1.705032] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Tell me the liquid phase mass flow rate with gas phase mass flow rate of 0.318734, gas density of 9.56607, liquid density of 126.709 and Lockhart-Martinelli parameter of 1.70503.", + "a": "1.9778732286593947", + "valid": true, + "kind": "compute", + "expect": 1.9778732286593947, + "dtype": "quantity", + "unit": "kg/s", + "why": "liquid phase mass flow rate for [0.318734, 9.566071, 126.709252, 1.705032] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "What is the liquid phase mass flow rate for gas phase mass flow rate 87.781, gas density 13.2084, liquid density 246.329 and Lockhart-Martinelli parameter 20.3966?", + "a": "7731.965049977583", + "valid": true, + "kind": "compute", + "expect": 7731.965049977583, + "dtype": "quantity", + "unit": "kg/s", + "why": "liquid phase mass flow rate for [87.781013, 13.208419, 246.329053, 20.396558] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Tell me the liquid phase mass flow rate with gas phase mass flow rate of 87.781, gas density of 13.2084, liquid density of 246.329 and Lockhart-Martinelli parameter of 20.3966.", + "a": "7731.965049977583", + "valid": true, + "kind": "compute", + "expect": 7731.965049977583, + "dtype": "quantity", + "unit": "kg/s", + "why": "liquid phase mass flow rate for [87.781013, 13.208419, 246.329053, 20.396558] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "What is the liquid phase mass flow rate for gas phase mass flow rate 156.502, gas density 185.886, liquid density 13.2373 and Lockhart-Martinelli parameter 4.71941?", + "a": "197.09869212501442", + "valid": true, + "kind": "compute", + "expect": 197.09869212501442, + "dtype": "quantity", + "unit": "kg/s", + "why": "liquid phase mass flow rate for [156.50181, 185.886273, 13.237311, 4.719415] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Tell me the liquid phase mass flow rate with gas phase mass flow rate of 156.502, gas density of 185.886, liquid density of 13.2373 and Lockhart-Martinelli parameter of 4.71941.", + "a": "197.09869212501442", + "valid": true, + "kind": "compute", + "expect": 197.09869212501442, + "dtype": "quantity", + "unit": "kg/s", + "why": "liquid phase mass flow rate for [156.50181, 185.886273, 13.237311, 4.719415] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "What is the liquid phase mass flow rate for gas phase mass flow rate 24.5721, gas density 108.451, liquid density 1.69476 and Lockhart-Martinelli parameter 49.4843?", + "a": "152.0011078183671", + "valid": true, + "kind": "compute", + "expect": 152.0011078183671, + "dtype": "quantity", + "unit": "kg/s", + "why": "liquid phase mass flow rate for [24.572086, 108.450792, 1.694755, 49.484326] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Tell me the liquid phase mass flow rate with gas phase mass flow rate of 24.5721, gas density of 108.451, liquid density of 1.69476 and Lockhart-Martinelli parameter of 49.4843.", + "a": "152.0011078183671", + "valid": true, + "kind": "compute", + "expect": 152.0011078183671, + "dtype": "quantity", + "unit": "kg/s", + "why": "liquid phase mass flow rate for [24.572086, 108.450792, 1.694755, 49.484326] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "What is the liquid phase mass flow rate for gas phase mass flow rate 107.961, gas density 59.4105 and liquid density 5.77717?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Lockhart-Martinelli parameter is not given" + }, + { + "q": "Tell me the liquid phase mass flow rate with gas phase mass flow rate of 2.69428, gas density of 48.921 and liquid density of 157.223.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Lockhart-Martinelli parameter is not given" + }, + { + "q": "What is the liquid phase mass flow 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 liquid phase mass flow 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 liquid phase mass flow rate with gas phase mass flow rate of -0.318734, gas density of 9.56607, liquid density of 126.709 and Lockhart-Martinelli parameter of 1.70503.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative gas phase mass flow rate: no example had one" + }, + { + "q": "Which liquid phase mass flow 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 gas phase mass flow rate is 167.625, gas density is 10.3291, liquid density is 2.21571 and the Lockhart-Martinelli parameter is 58.5346, what is the liquid phase mass flow rate?", + "a": "4544.412554175463", + "valid": true, + "kind": "compute", + "expect": 4544.412554175463, + "dtype": "quantity", + "unit": "kg/s", + "why": "liquid phase mass flow rate for [167.624948, 10.329054, 2.215713, 58.534594] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Could you work out the liquid phase mass flow rate given gas phase mass flow rate = 167.625, gas density = 10.3291, liquid density = 2.21571, Lockhart-Martinelli parameter = 58.5346?", + "a": "4544.412554175463", + "valid": true, + "kind": "compute", + "expect": 4544.412554175463, + "dtype": "quantity", + "unit": "kg/s", + "why": "liquid phase mass flow rate for [167.624948, 10.329054, 2.215713, 58.534594] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "I need the liquid phase mass flow rate for a gas phase mass flow rate of 8.53158, a gas density of 124.718, a liquid density of 1.91195 and a Lockhart-Martinelli parameter of 82.2401, please.", + "a": "86.87331310958893", + "valid": true, + "kind": "compute", + "expect": 86.87331310958893, + "dtype": "quantity", + "unit": "kg/s", + "why": "liquid phase mass flow rate for [8.531577, 124.718185, 1.911952, 82.240072] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "If the gas phase mass flow rate is 8.53158, gas density is 124.718, liquid density is 1.91195 and the Lockhart-Martinelli parameter is 82.2401, what is the liquid phase mass flow rate?", + "a": "86.87331310958893", + "valid": true, + "kind": "compute", + "expect": 86.87331310958893, + "dtype": "quantity", + "unit": "kg/s", + "why": "liquid phase mass flow rate for [8.531577, 124.718185, 1.911952, 82.240072] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Could you work out the liquid phase mass flow rate given gas phase mass flow rate = 81.1967, gas density = 6.98086, liquid density = 0.551236, Lockhart-Martinelli parameter = 2.88512?", + "a": "65.8288991564326", + "valid": true, + "kind": "compute", + "expect": 65.8288991564326, + "dtype": "quantity", + "unit": "kg/s", + "why": "liquid phase mass flow rate for [81.196705, 6.980857, 0.551236, 2.88512] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "I need the liquid phase mass flow rate for a gas phase mass flow rate of 81.1967, a gas density of 6.98086, a liquid density of 0.551236 and a Lockhart-Martinelli parameter of 2.88512, please.", + "a": "65.8288991564326", + "valid": true, + "kind": "compute", + "expect": 65.8288991564326, + "dtype": "quantity", + "unit": "kg/s", + "why": "liquid phase mass flow rate for [81.196705, 6.980857, 0.551236, 2.88512] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "If the gas phase mass flow rate is 1.16997, gas density is 1.14865, liquid density is 21.7931 and the Lockhart-Martinelli parameter is 29.5106, what is the liquid phase mass flow rate?", + "a": "150.38953968954334", + "valid": true, + "kind": "compute", + "expect": 150.38953968954334, + "dtype": "quantity", + "unit": "kg/s", + "why": "liquid phase mass flow rate for [1.169968, 1.148652, 21.793126, 29.510609] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Could you work out the liquid phase mass flow rate given gas phase mass flow rate = 1.16997, gas density = 1.14865, liquid density = 21.7931, Lockhart-Martinelli parameter = 29.5106?", + "a": "150.38953968954334", + "valid": true, + "kind": "compute", + "expect": 150.38953968954334, + "dtype": "quantity", + "unit": "kg/s", + "why": "liquid phase mass flow rate for [1.169968, 1.148652, 21.793126, 29.510609] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "I need the liquid phase mass flow rate for a gas phase mass flow rate of 223.509, a gas density of 17.3414, a liquid density of 7.10705 and a Lockhart-Martinelli parameter of 30.2507, please.", + "a": "4328.454926039116", + "valid": true, + "kind": "compute", + "expect": 4328.454926039116, + "dtype": "quantity", + "unit": "kg/s", + "why": "liquid phase mass flow rate for [223.508796, 17.341435, 7.107049, 30.250749] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "If the gas phase mass flow rate is 223.509, gas density is 17.3414, liquid density is 7.10705 and the Lockhart-Martinelli parameter is 30.2507, what is the liquid phase mass flow rate?", + "a": "4328.454926039116", + "valid": true, + "kind": "compute", + "expect": 4328.454926039116, + "dtype": "quantity", + "unit": "kg/s", + "why": "liquid phase mass flow rate for [223.508796, 17.341435, 7.107049, 30.250749] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Could you work out the liquid phase mass flow rate given gas phase mass flow rate = 0.869111, gas density = 39.1753, liquid density = 0.432217, Lockhart-Martinelli parameter = 16.6571?", + "a": "1.5206130685773502", + "valid": true, + "kind": "compute", + "expect": 1.5206130685773502, + "dtype": "quantity", + "unit": "kg/s", + "why": "liquid phase mass flow rate for [0.869111, 39.175263, 0.432217, 16.657051] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "I need the liquid phase mass flow rate for a gas phase mass flow rate of 0.869111, a gas density of 39.1753, a liquid density of 0.432217 and a Lockhart-Martinelli parameter of 16.6571, please.", + "a": "1.5206130685773502", + "valid": true, + "kind": "compute", + "expect": 1.5206130685773502, + "dtype": "quantity", + "unit": "kg/s", + "why": "liquid phase mass flow rate for [0.869111, 39.175263, 0.432217, 16.657051] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "If the gas phase mass flow rate is 167.625, gas density is 10.3291 and the liquid density is 2.21571, what is the liquid phase mass flow rate?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Lockhart-Martinelli parameter is not given" + }, + { + "q": "Could you work out the liquid phase mass flow rate given gas phase mass flow rate = 8.53158, gas density = 124.718, liquid density = 1.91195?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Lockhart-Martinelli parameter is not given" + }, + { + "q": "Do you know the liquid phase mass flow 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 liquid phase mass flow 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 liquid phase mass flow rate given gas phase mass flow rate = -81.1967, gas density = 6.98086, liquid density = 0.551236, Lockhart-Martinelli parameter = 2.88512?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative gas phase mass flow rate: no example had one" + }, + { + "q": "What is a typical liquid phase mass flow 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-liquid-phase-mass-flow-rate/program.py b/root/organs10/physics/specialists/solved-liquid-phase-mass-flow-rate/program.py new file mode 100644 index 0000000000000000000000000000000000000000..97d00616fc3d6a146348cc8abdc84f43509acb76 --- /dev/null +++ b/root/organs10/physics/specialists/solved-liquid-phase-mass-flow-rate/program.py @@ -0,0 +1,7 @@ +"""liquid phase mass flow rate from gas phase mass flow rate, gas density, liquid density, Lockhart-Martinelli parameter (Lockhart-Martinelli parameter (Wikidata Q29211)) +The maths organ (systems.py): solved from the system q4 = ((q0)/(q1))*sqrt(((q2)/(q3))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(gas_phase_mass_flow_rate, gas_density, liquid_density, lockhart_martinelli_parameter): + return 1.0*gas_density**(-0.5)*gas_phase_mass_flow_rate*liquid_density**0.5*lockhart_martinelli_parameter diff --git a/root/organs10/physics/specialists/solved-liquid-phase-mass-flow-rate/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-liquid-phase-mass-flow-rate/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-liquid-phase-mass-flow-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-lockhart-martinelli-parameter/organ_formula_v2.py b/root/organs10/physics/specialists/solved-lockhart-martinelli-parameter/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-lockhart-martinelli-parameter/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-lockhart-martinelli-parameter/panel.json b/root/organs10/physics/specialists/solved-lockhart-martinelli-parameter/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..7c1575646d498bb724a71b1f521b5471235d083f --- /dev/null +++ b/root/organs10/physics/specialists/solved-lockhart-martinelli-parameter/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the Lockhart-Martinelli parameter for liquid phase mass flow rate 107.961, gas phase mass flow rate 59.4105, gas density 5.77717 and liquid density 1.89126?", + "a": "3.176029967438684", + "valid": true, + "kind": "compute", + "expect": 3.176029967438684, + "dtype": "quantity", + "unit": "1", + "why": "Lockhart-Martinelli parameter for [107.960666, 59.4105, 5.777165, 1.891256] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Tell me the Lockhart-Martinelli parameter with liquid phase mass flow rate of 107.961, gas phase mass flow rate of 59.4105, gas density of 5.77717 and liquid density of 1.89126.", + "a": "3.176029967438684", + "valid": true, + "kind": "compute", + "expect": 3.176029967438684, + "dtype": "quantity", + "unit": "1", + "why": "Lockhart-Martinelli parameter for [107.960666, 59.4105, 5.777165, 1.891256] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "What is the Lockhart-Martinelli parameter for liquid phase mass flow rate 2.69428, gas phase mass flow rate 48.921, gas density 157.223 and liquid density 35.6412?", + "a": "0.11567220043544774", + "valid": true, + "kind": "compute", + "expect": 0.11567220043544774, + "dtype": "quantity", + "unit": "1", + "why": "Lockhart-Martinelli parameter for [2.69428, 48.920991, 157.22256, 35.641157] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Tell me the Lockhart-Martinelli parameter with liquid phase mass flow rate of 2.69428, gas phase mass flow rate of 48.921, gas density of 157.223 and liquid density of 35.6412.", + "a": "0.11567220043544774", + "valid": true, + "kind": "compute", + "expect": 0.11567220043544774, + "dtype": "quantity", + "unit": "1", + "why": "Lockhart-Martinelli parameter for [2.69428, 48.920991, 157.22256, 35.641157] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "What is the Lockhart-Martinelli parameter for liquid phase mass flow rate 0.318734, gas phase mass flow rate 9.56607, gas density 126.709 and liquid density 1.70503?", + "a": "0.28723192342839077", + "valid": true, + "kind": "compute", + "expect": 0.28723192342839077, + "dtype": "quantity", + "unit": "1", + "why": "Lockhart-Martinelli parameter for [0.318734, 9.566071, 126.709252, 1.705032] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Tell me the Lockhart-Martinelli parameter with liquid phase mass flow rate of 0.318734, gas phase mass flow rate of 9.56607, gas density of 126.709 and liquid density of 1.70503.", + "a": "0.28723192342839077", + "valid": true, + "kind": "compute", + "expect": 0.28723192342839077, + "dtype": "quantity", + "unit": "1", + "why": "Lockhart-Martinelli parameter for [0.318734, 9.566071, 126.709252, 1.705032] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "What is the Lockhart-Martinelli parameter for liquid phase mass flow rate 87.781, gas phase mass flow rate 13.2084, gas density 246.329 and liquid density 20.3966?", + "a": "23.09559450800325", + "valid": true, + "kind": "compute", + "expect": 23.09559450800325, + "dtype": "quantity", + "unit": "1", + "why": "Lockhart-Martinelli parameter for [87.781013, 13.208419, 246.329053, 20.396558] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Tell me the Lockhart-Martinelli parameter with liquid phase mass flow rate of 87.781, gas phase mass flow rate of 13.2084, gas density of 246.329 and liquid density of 20.3966.", + "a": "23.09559450800325", + "valid": true, + "kind": "compute", + "expect": 23.09559450800325, + "dtype": "quantity", + "unit": "1", + "why": "Lockhart-Martinelli parameter for [87.781013, 13.208419, 246.329053, 20.396558] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "What is the Lockhart-Martinelli parameter for liquid phase mass flow rate 156.502, gas phase mass flow rate 185.886, gas density 13.2373 and liquid density 4.71941?", + "a": "1.4100283890399907", + "valid": true, + "kind": "compute", + "expect": 1.4100283890399907, + "dtype": "quantity", + "unit": "1", + "why": "Lockhart-Martinelli parameter for [156.50181, 185.886273, 13.237311, 4.719415] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Tell me the Lockhart-Martinelli parameter with liquid phase mass flow rate of 156.502, gas phase mass flow rate of 185.886, gas density of 13.2373 and liquid density of 4.71941.", + "a": "1.4100283890399907", + "valid": true, + "kind": "compute", + "expect": 1.4100283890399907, + "dtype": "quantity", + "unit": "1", + "why": "Lockhart-Martinelli parameter for [156.50181, 185.886273, 13.237311, 4.719415] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "What is the Lockhart-Martinelli parameter for liquid phase mass flow rate 24.5721, gas phase mass flow rate 108.451, gas density 1.69476 and liquid density 49.4843?", + "a": "0.04193038944723047", + "valid": true, + "kind": "compute", + "expect": 0.04193038944723047, + "dtype": "quantity", + "unit": "1", + "why": "Lockhart-Martinelli parameter for [24.572086, 108.450792, 1.694755, 49.484326] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Tell me the Lockhart-Martinelli parameter with liquid phase mass flow rate of 24.5721, gas phase mass flow rate of 108.451, gas density of 1.69476 and liquid density of 49.4843.", + "a": "0.04193038944723047", + "valid": true, + "kind": "compute", + "expect": 0.04193038944723047, + "dtype": "quantity", + "unit": "1", + "why": "Lockhart-Martinelli parameter for [24.572086, 108.450792, 1.694755, 49.484326] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "What is the Lockhart-Martinelli parameter for liquid phase mass flow rate 107.961, gas phase mass flow rate 59.4105 and gas density 5.77717?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the liquid density is not given" + }, + { + "q": "Tell me the Lockhart-Martinelli parameter with liquid phase mass flow rate of 2.69428, gas phase mass flow rate of 48.921 and gas density of 157.223.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the liquid density is not given" + }, + { + "q": "What is the Lockhart-Martinelli parameter 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 Lockhart-Martinelli parameter 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 Lockhart-Martinelli parameter with liquid phase mass flow rate of -0.318734, gas phase mass flow rate of 9.56607, gas density of 126.709 and liquid density of 1.70503.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative liquid phase mass flow rate: no example had one" + }, + { + "q": "Which Lockhart-Martinelli parameter 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 liquid phase mass flow rate is 167.625, gas phase mass flow rate is 10.3291, gas density is 2.21571 and the liquid density is 58.5346, what is the Lockhart-Martinelli parameter?", + "a": "3.1573923217219315", + "valid": true, + "kind": "compute", + "expect": 3.1573923217219315, + "dtype": "quantity", + "unit": "1", + "why": "Lockhart-Martinelli parameter for [167.624948, 10.329054, 2.215713, 58.534594] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Could you work out the Lockhart-Martinelli parameter given liquid phase mass flow rate = 167.625, gas phase mass flow rate = 10.3291, gas density = 2.21571, liquid density = 58.5346?", + "a": "3.1573923217219315", + "valid": true, + "kind": "compute", + "expect": 3.1573923217219315, + "dtype": "quantity", + "unit": "1", + "why": "Lockhart-Martinelli parameter for [167.624948, 10.329054, 2.215713, 58.534594] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "I need the Lockhart-Martinelli parameter for a liquid phase mass flow rate of 8.53158, a gas phase mass flow rate of 124.718, a gas density of 1.91195 and a liquid density of 82.2401, please.", + "a": "0.010430287306558943", + "valid": true, + "kind": "compute", + "expect": 0.010430287306558943, + "dtype": "quantity", + "unit": "1", + "why": "Lockhart-Martinelli parameter for [8.531577, 124.718185, 1.911952, 82.240072] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "If the liquid phase mass flow rate is 8.53158, gas phase mass flow rate is 124.718, gas density is 1.91195 and the liquid density is 82.2401, what is the Lockhart-Martinelli parameter?", + "a": "0.010430287306558943", + "valid": true, + "kind": "compute", + "expect": 0.010430287306558943, + "dtype": "quantity", + "unit": "1", + "why": "Lockhart-Martinelli parameter for [8.531577, 124.718185, 1.911952, 82.240072] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Could you work out the Lockhart-Martinelli parameter given liquid phase mass flow rate = 81.1967, gas phase mass flow rate = 6.98086, gas density = 0.551236, liquid density = 2.88512?", + "a": "5.084128449126445", + "valid": true, + "kind": "compute", + "expect": 5.084128449126445, + "dtype": "quantity", + "unit": "1", + "why": "Lockhart-Martinelli parameter for [81.196705, 6.980857, 0.551236, 2.88512] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "I need the Lockhart-Martinelli parameter for a liquid phase mass flow rate of 81.1967, a gas phase mass flow rate of 6.98086, a gas density of 0.551236 and a liquid density of 2.88512, please.", + "a": "5.084128449126445", + "valid": true, + "kind": "compute", + "expect": 5.084128449126445, + "dtype": "quantity", + "unit": "1", + "why": "Lockhart-Martinelli parameter for [81.196705, 6.980857, 0.551236, 2.88512] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "If the liquid phase mass flow rate is 1.16997, gas phase mass flow rate is 1.14865, gas density is 21.7931 and the liquid density is 29.5106, what is the Lockhart-Martinelli parameter?", + "a": "0.8752983044967498", + "valid": true, + "kind": "compute", + "expect": 0.8752983044967498, + "dtype": "quantity", + "unit": "1", + "why": "Lockhart-Martinelli parameter for [1.169968, 1.148652, 21.793126, 29.510609] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Could you work out the Lockhart-Martinelli parameter given liquid phase mass flow rate = 1.16997, gas phase mass flow rate = 1.14865, gas density = 21.7931, liquid density = 29.5106?", + "a": "0.8752983044967498", + "valid": true, + "kind": "compute", + "expect": 0.8752983044967498, + "dtype": "quantity", + "unit": "1", + "why": "Lockhart-Martinelli parameter for [1.169968, 1.148652, 21.793126, 29.510609] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "I need the Lockhart-Martinelli parameter for a liquid phase mass flow rate of 223.509, a gas phase mass flow rate of 17.3414, a gas density of 7.10705 and a liquid density of 30.2507, please.", + "a": "6.24721107122818", + "valid": true, + "kind": "compute", + "expect": 6.24721107122818, + "dtype": "quantity", + "unit": "1", + "why": "Lockhart-Martinelli parameter for [223.508796, 17.341435, 7.107049, 30.250749] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "If the liquid phase mass flow rate is 223.509, gas phase mass flow rate is 17.3414, gas density is 7.10705 and the liquid density is 30.2507, what is the Lockhart-Martinelli parameter?", + "a": "6.24721107122818", + "valid": true, + "kind": "compute", + "expect": 6.24721107122818, + "dtype": "quantity", + "unit": "1", + "why": "Lockhart-Martinelli parameter for [223.508796, 17.341435, 7.107049, 30.250749] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "Could you work out the Lockhart-Martinelli parameter given liquid phase mass flow rate = 0.869111, gas phase mass flow rate = 39.1753, gas density = 0.432217, liquid density = 16.6571?", + "a": "0.00357367943335233", + "valid": true, + "kind": "compute", + "expect": 0.00357367943335233, + "dtype": "quantity", + "unit": "1", + "why": "Lockhart-Martinelli parameter for [0.869111, 39.175263, 0.432217, 16.657051] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "I need the Lockhart-Martinelli parameter for a liquid phase mass flow rate of 0.869111, a gas phase mass flow rate of 39.1753, a gas density of 0.432217 and a liquid density of 16.6571, please.", + "a": "0.00357367943335233", + "valid": true, + "kind": "compute", + "expect": 0.00357367943335233, + "dtype": "quantity", + "unit": "1", + "why": "Lockhart-Martinelli parameter for [0.869111, 39.175263, 0.432217, 16.657051] (the system Lockhart-Martinelli parameter (Wikidata Q29211))" + }, + { + "q": "If the liquid phase mass flow rate is 167.625, gas phase mass flow rate is 10.3291 and the gas density is 2.21571, what is the Lockhart-Martinelli parameter?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the liquid density is not given" + }, + { + "q": "Could you work out the Lockhart-Martinelli parameter given liquid phase mass flow rate = 8.53158, gas phase mass flow rate = 124.718, gas density = 1.91195?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the liquid density is not given" + }, + { + "q": "Do you know the Lockhart-Martinelli parameter 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 Lockhart-Martinelli parameter 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 Lockhart-Martinelli parameter given liquid phase mass flow rate = -81.1967, gas phase mass flow rate = 6.98086, gas density = 0.551236, liquid density = 2.88512?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative liquid phase mass flow rate: no example had one" + }, + { + "q": "What is a typical Lockhart-Martinelli parameter?", + "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-lockhart-martinelli-parameter/program.py b/root/organs10/physics/specialists/solved-lockhart-martinelli-parameter/program.py new file mode 100644 index 0000000000000000000000000000000000000000..38a73e786ea95386b51f50c75ea57d76d6044c1d --- /dev/null +++ b/root/organs10/physics/specialists/solved-lockhart-martinelli-parameter/program.py @@ -0,0 +1,7 @@ +"""Lockhart-Martinelli parameter from liquid phase mass flow rate, gas phase mass flow rate, gas density, liquid density (Lockhart-Martinelli parameter (Wikidata Q29211)) +The maths organ (systems.py): solved from the system q4 = ((q0)/(q1))*sqrt(((q2)/(q3))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(liquid_phase_mass_flow_rate, gas_phase_mass_flow_rate, gas_density, liquid_density): + return 1.0*gas_density**0.5*liquid_density**(-0.5)*liquid_phase_mass_flow_rate/gas_phase_mass_flow_rate diff --git a/root/organs10/physics/specialists/solved-lockhart-martinelli-parameter/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-lockhart-martinelli-parameter/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-lockhart-martinelli-parameter/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-london-penetration-depth/organ_formula_v2.py b/root/organs10/physics/specialists/solved-london-penetration-depth/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-london-penetration-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-london-penetration-depth/panel.json b/root/organs10/physics/specialists/solved-london-penetration-depth/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..5c77e4808ba851716f960bc348d016b84c5968e7 --- /dev/null +++ b/root/organs10/physics/specialists/solved-london-penetration-depth/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the London penetration depth for Landau–Ginzburg number 107.961 and superconducting coherence length 59.4105?", + "a": "9070.76177480816", + "valid": true, + "kind": "compute", + "expect": 9070.76177480816, + "dtype": "quantity", + "unit": "m", + "why": "London penetration depth for [107.960666, 59.4105] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Tell me the London penetration depth with Landau–Ginzburg number of 107.961 and superconducting coherence length of 59.4105.", + "a": "9070.76177480816", + "valid": true, + "kind": "compute", + "expect": 9070.76177480816, + "dtype": "quantity", + "unit": "m", + "why": "London penetration depth for [107.960666, 59.4105] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "What is the London penetration depth for Landau–Ginzburg number 169.527 and superconducting coherence length 280.774?", + "a": "67314.74327975998", + "valid": true, + "kind": "compute", + "expect": 67314.74327975998, + "dtype": "quantity", + "unit": "m", + "why": "London penetration depth for [169.526958, 280.773702] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Tell me the London penetration depth with Landau–Ginzburg number of 169.527 and superconducting coherence length of 280.774.", + "a": "67314.74327975998", + "valid": true, + "kind": "compute", + "expect": 67314.74327975998, + "dtype": "quantity", + "unit": "m", + "why": "London penetration depth for [169.526958, 280.773702] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "What is the London penetration depth for Landau–Ginzburg number 1.91195 and superconducting coherence length 82.2401?", + "a": "222.3695975363748", + "valid": true, + "kind": "compute", + "expect": 222.3695975363748, + "dtype": "quantity", + "unit": "m", + "why": "London penetration depth for [1.911952, 82.240072] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Tell me the London penetration depth with Landau–Ginzburg number of 1.91195 and superconducting coherence length of 82.2401.", + "a": "222.3695975363748", + "valid": true, + "kind": "compute", + "expect": 222.3695975363748, + "dtype": "quantity", + "unit": "m", + "why": "London penetration depth for [1.911952, 82.240072] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "What is the London penetration depth for Landau–Ginzburg number 1.64379 and superconducting coherence length 252.709?", + "a": "587.4670794069988", + "valid": true, + "kind": "compute", + "expect": 587.4670794069988, + "dtype": "quantity", + "unit": "m", + "why": "London penetration depth for [1.643795, 252.709024] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Tell me the London penetration depth with Landau–Ginzburg number of 1.64379 and superconducting coherence length of 252.709.", + "a": "587.4670794069988", + "valid": true, + "kind": "compute", + "expect": 587.4670794069988, + "dtype": "quantity", + "unit": "m", + "why": "London penetration depth for [1.643795, 252.709024] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "What is the London penetration depth for Landau–Ginzburg number 18.3169 and superconducting coherence length 6.8386?", + "a": "177.14678270108837", + "valid": true, + "kind": "compute", + "expect": 177.14678270108837, + "dtype": "quantity", + "unit": "m", + "why": "London penetration depth for [18.316867, 6.838598] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Tell me the London penetration depth with Landau–Ginzburg number of 18.3169 and superconducting coherence length of 6.8386.", + "a": "177.14678270108837", + "valid": true, + "kind": "compute", + "expect": 177.14678270108837, + "dtype": "quantity", + "unit": "m", + "why": "London penetration depth for [18.316867, 6.838598] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "What is the London penetration depth for Landau–Ginzburg number 156.502 and superconducting coherence length 185.886?", + "a": "41141.64773863522", + "valid": true, + "kind": "compute", + "expect": 41141.64773863522, + "dtype": "quantity", + "unit": "m", + "why": "London penetration depth for [156.50181, 185.886273] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Tell me the London penetration depth with Landau–Ginzburg number of 156.502 and superconducting coherence length of 185.886.", + "a": "41141.64773863522", + "valid": true, + "kind": "compute", + "expect": 41141.64773863522, + "dtype": "quantity", + "unit": "m", + "why": "London penetration depth for [156.50181, 185.886273] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "What is the London penetration depth for Landau–Ginzburg number 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the superconducting coherence length is not given" + }, + { + "q": "Tell me the London penetration depth with Landau–Ginzburg number of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the superconducting coherence length is not given" + }, + { + "q": "What is the London penetration 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 London penetration 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 London penetration depth with Landau–Ginzburg number of -1.91195 and superconducting coherence length of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Landau–Ginzburg number: no example had one" + }, + { + "q": "Which London penetration 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 Landau–Ginzburg number is 8.50728 and the superconducting coherence length is 17.7888, what is the London penetration depth?", + "a": "214.0188266147403", + "valid": true, + "kind": "compute", + "expect": 214.0188266147403, + "dtype": "quantity", + "unit": "m", + "why": "London penetration depth for [8.507283, 17.788778] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Could you work out the London penetration depth given Landau–Ginzburg number = 8.50728, superconducting coherence length = 17.7888?", + "a": "214.0188266147403", + "valid": true, + "kind": "compute", + "expect": 214.0188266147403, + "dtype": "quantity", + "unit": "m", + "why": "London penetration depth for [8.507283, 17.788778] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "I need the London penetration depth for a Landau–Ginzburg number of 8.24951 and a superconducting coherence length of 0.634021, please.", + "a": "7.396849261148381", + "valid": true, + "kind": "compute", + "expect": 7.396849261148381, + "dtype": "quantity", + "unit": "m", + "why": "London penetration depth for [8.24951, 0.634021] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "If the Landau–Ginzburg number is 8.24951 and the superconducting coherence length is 0.634021, what is the London penetration depth?", + "a": "7.396849261148381", + "valid": true, + "kind": "compute", + "expect": 7.396849261148381, + "dtype": "quantity", + "unit": "m", + "why": "London penetration depth for [8.24951, 0.634021] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Could you work out the London penetration depth given Landau–Ginzburg number = 0.318734, superconducting coherence length = 9.56607?", + "a": "4.311983150875595", + "valid": true, + "kind": "compute", + "expect": 4.311983150875595, + "dtype": "quantity", + "unit": "m", + "why": "London penetration depth for [0.318734, 9.566071] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "I need the London penetration depth for a Landau–Ginzburg number of 0.318734 and a superconducting coherence length of 9.56607, please.", + "a": "4.311983150875595", + "valid": true, + "kind": "compute", + "expect": 4.311983150875595, + "dtype": "quantity", + "unit": "m", + "why": "London penetration depth for [0.318734, 9.566071] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "If the Landau–Ginzburg number is 0.671697 and the superconducting coherence length is 14.2496, what is the London penetration depth?", + "a": "13.535997762215288", + "valid": true, + "kind": "compute", + "expect": 13.535997762215288, + "dtype": "quantity", + "unit": "m", + "why": "London penetration depth for [0.671697, 14.249569] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Could you work out the London penetration depth given Landau–Ginzburg number = 0.671697, superconducting coherence length = 14.2496?", + "a": "13.535997762215288", + "valid": true, + "kind": "compute", + "expect": 13.535997762215288, + "dtype": "quantity", + "unit": "m", + "why": "London penetration depth for [0.671697, 14.249569] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "I need the London penetration depth for a Landau–Ginzburg number of 21.7931 and a superconducting coherence length of 29.5106, please.", + "a": "909.5209443618436", + "valid": true, + "kind": "compute", + "expect": 909.5209443618436, + "dtype": "quantity", + "unit": "m", + "why": "London penetration depth for [21.793126, 29.510609] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "If the Landau–Ginzburg number is 21.7931 and the superconducting coherence length is 29.5106, what is the London penetration depth?", + "a": "909.5209443618436", + "valid": true, + "kind": "compute", + "expect": 909.5209443618436, + "dtype": "quantity", + "unit": "m", + "why": "London penetration depth for [21.793126, 29.510609] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Could you work out the London penetration depth given Landau–Ginzburg number = 153.15, superconducting coherence length = 18.5953?", + "a": "4027.4990876038737", + "valid": true, + "kind": "compute", + "expect": 4027.4990876038737, + "dtype": "quantity", + "unit": "m", + "why": "London penetration depth for [153.149965, 18.595315] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "I need the London penetration depth for a Landau–Ginzburg number of 153.15 and a superconducting coherence length of 18.5953, please.", + "a": "4027.4990876038737", + "valid": true, + "kind": "compute", + "expect": 4027.4990876038737, + "dtype": "quantity", + "unit": "m", + "why": "London penetration depth for [153.149965, 18.595315] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "If the Landau–Ginzburg number is 8.50728, what is the London penetration depth?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the superconducting coherence length is not given" + }, + { + "q": "Could you work out the London penetration depth given Landau–Ginzburg number = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the superconducting coherence length is not given" + }, + { + "q": "Do you know the London penetration 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 London penetration 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 London penetration depth given Landau–Ginzburg number = -0.318734, superconducting coherence length = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Landau–Ginzburg number: no example had one" + }, + { + "q": "What is a typical London penetration 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-london-penetration-depth/program.py b/root/organs10/physics/specialists/solved-london-penetration-depth/program.py new file mode 100644 index 0000000000000000000000000000000000000000..5fc51e0b7cc6357cc3c09be844dd1a8940d7dba7 --- /dev/null +++ b/root/organs10/physics/specialists/solved-london-penetration-depth/program.py @@ -0,0 +1,7 @@ +"""London penetration depth from Landau–Ginzburg number, superconducting coherence length (Landau–Ginzburg number (Wikidata Q105421034)) +The maths organ (systems.py): solved from the system q1 = ((q0)/(q2*sqrt(2))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(landau_ginzburg_number, superconducting_coherence_length): + return 1.4142135623731*landau_ginzburg_number*superconducting_coherence_length diff --git a/root/organs10/physics/specialists/solved-london-penetration-depth/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-london-penetration-depth/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-london-penetration-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-lorentz-number/organ_formula_v2.py b/root/organs10/physics/specialists/solved-lorentz-number/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-lorentz-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-lorentz-number/panel.json b/root/organs10/physics/specialists/solved-lorentz-number/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..f20d3d2372199923be63072743cbf9504680d069 --- /dev/null +++ b/root/organs10/physics/specialists/solved-lorentz-number/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the Lorentz number for electrical conductivity 107.961, voltage difference 59.4105, thermal conductivity 5.77717 and temperature difference 1.89126?", + "a": "34876.02488282469", + "valid": true, + "kind": "compute", + "expect": 34876.02488282469, + "dtype": "quantity", + "unit": "1", + "why": "Lorentz number for [107.960666, 59.4105, 5.777165, 1.891256] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "Tell me the Lorentz number with electrical conductivity of 107.961, voltage difference of 59.4105, thermal conductivity of 5.77717 and temperature difference of 1.89126.", + "a": "34876.02488282469", + "valid": true, + "kind": "compute", + "expect": 34876.02488282469, + "dtype": "quantity", + "unit": "1", + "why": "Lorentz number for [107.960666, 59.4105, 5.777165, 1.891256] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "What is the Lorentz number for electrical conductivity 2.69428, voltage difference 48.921, thermal conductivity 157.223 and temperature difference 35.6412?", + "a": "1.150711723874745", + "valid": true, + "kind": "compute", + "expect": 1.150711723874745, + "dtype": "quantity", + "unit": "1", + "why": "Lorentz number for [2.69428, 48.920991, 157.22256, 35.641157] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "Tell me the Lorentz number with electrical conductivity of 2.69428, voltage difference of 48.921, thermal conductivity of 157.223 and temperature difference of 35.6412.", + "a": "1.150711723874745", + "valid": true, + "kind": "compute", + "expect": 1.150711723874745, + "dtype": "quantity", + "unit": "1", + "why": "Lorentz number for [2.69428, 48.920991, 157.22256, 35.641157] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "What is the Lorentz number for electrical conductivity 0.318734, voltage difference 9.56607, thermal conductivity 126.709 and temperature difference 1.70503?", + "a": "0.13500650656818805", + "valid": true, + "kind": "compute", + "expect": 0.13500650656818805, + "dtype": "quantity", + "unit": "1", + "why": "Lorentz number for [0.318734, 9.566071, 126.709252, 1.705032] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "Tell me the Lorentz number with electrical conductivity of 0.318734, voltage difference of 9.56607, thermal conductivity of 126.709 and temperature difference of 1.70503.", + "a": "0.13500650656818805", + "valid": true, + "kind": "compute", + "expect": 0.13500650656818805, + "dtype": "quantity", + "unit": "1", + "why": "Lorentz number for [0.318734, 9.566071, 126.709252, 1.705032] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "What is the Lorentz number for electrical conductivity 87.781, voltage difference 13.2084, thermal conductivity 246.329 and temperature difference 20.3966?", + "a": "3.0481035018869886", + "valid": true, + "kind": "compute", + "expect": 3.0481035018869886, + "dtype": "quantity", + "unit": "1", + "why": "Lorentz number for [87.781013, 13.208419, 246.329053, 20.396558] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "Tell me the Lorentz number with electrical conductivity of 87.781, voltage difference of 13.2084, thermal conductivity of 246.329 and temperature difference of 20.3966.", + "a": "3.0481035018869886", + "valid": true, + "kind": "compute", + "expect": 3.0481035018869886, + "dtype": "quantity", + "unit": "1", + "why": "Lorentz number for [87.781013, 13.208419, 246.329053, 20.396558] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "What is the Lorentz number for electrical conductivity 156.502, voltage difference 185.886, thermal conductivity 13.2373 and temperature difference 4.71941?", + "a": "86561.75987783127", + "valid": true, + "kind": "compute", + "expect": 86561.75987783127, + "dtype": "quantity", + "unit": "1", + "why": "Lorentz number for [156.50181, 185.886273, 13.237311, 4.719415] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "Tell me the Lorentz number with electrical conductivity of 156.502, voltage difference of 185.886, thermal conductivity of 13.2373 and temperature difference of 4.71941.", + "a": "86561.75987783127", + "valid": true, + "kind": "compute", + "expect": 86561.75987783127, + "dtype": "quantity", + "unit": "1", + "why": "Lorentz number for [156.50181, 185.886273, 13.237311, 4.719415] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "What is the Lorentz number for electrical conductivity 24.5721, voltage difference 108.451, thermal conductivity 1.69476 and temperature difference 49.4843?", + "a": "3446.1402376603787", + "valid": true, + "kind": "compute", + "expect": 3446.1402376603787, + "dtype": "quantity", + "unit": "1", + "why": "Lorentz number for [24.572086, 108.450792, 1.694755, 49.484326] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "Tell me the Lorentz number with electrical conductivity of 24.5721, voltage difference of 108.451, thermal conductivity of 1.69476 and temperature difference of 49.4843.", + "a": "3446.1402376603787", + "valid": true, + "kind": "compute", + "expect": 3446.1402376603787, + "dtype": "quantity", + "unit": "1", + "why": "Lorentz number for [24.572086, 108.450792, 1.694755, 49.484326] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "What is the Lorentz number for electrical conductivity 107.961, voltage difference 59.4105 and thermal conductivity 5.77717?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the temperature difference is not given" + }, + { + "q": "Tell me the Lorentz number with electrical conductivity of 2.69428, voltage difference of 48.921 and thermal conductivity of 157.223.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the temperature difference is not given" + }, + { + "q": "What is the Lorentz 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 Lorentz 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 Lorentz number with electrical conductivity of -0.318734, voltage difference of 9.56607, thermal conductivity of 126.709 and temperature difference of 1.70503.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electrical conductivity: no example had one" + }, + { + "q": "Which Lorentz 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 electrical conductivity is 167.625, voltage difference is 10.3291, thermal conductivity is 2.21571 and the temperature difference is 58.5346, what is the Lorentz number?", + "a": "137.8902772511644", + "valid": true, + "kind": "compute", + "expect": 137.8902772511644, + "dtype": "quantity", + "unit": "1", + "why": "Lorentz number for [167.624948, 10.329054, 2.215713, 58.534594] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "Could you work out the Lorentz number given electrical conductivity = 167.625, voltage difference = 10.3291, thermal conductivity = 2.21571, temperature difference = 58.5346?", + "a": "137.8902772511644", + "valid": true, + "kind": "compute", + "expect": 137.8902772511644, + "dtype": "quantity", + "unit": "1", + "why": "Lorentz number for [167.624948, 10.329054, 2.215713, 58.534594] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "I need the Lorentz number for an electrical conductivity of 8.53158, a voltage difference of 124.718, a thermal conductivity of 1.91195 and a temperature difference of 82.2401, please.", + "a": "843.9728658653045", + "valid": true, + "kind": "compute", + "expect": 843.9728658653045, + "dtype": "quantity", + "unit": "1", + "why": "Lorentz number for [8.531577, 124.718185, 1.911952, 82.240072] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "If the electrical conductivity is 8.53158, voltage difference is 124.718, thermal conductivity is 1.91195 and the temperature difference is 82.2401, what is the Lorentz number?", + "a": "843.9728658653045", + "valid": true, + "kind": "compute", + "expect": 843.9728658653045, + "dtype": "quantity", + "unit": "1", + "why": "Lorentz number for [8.531577, 124.718185, 1.911952, 82.240072] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "Could you work out the Lorentz number given electrical conductivity = 81.1967, voltage difference = 6.98086, thermal conductivity = 0.551236, temperature difference = 2.88512?", + "a": "2488.02400826893", + "valid": true, + "kind": "compute", + "expect": 2488.02400826893, + "dtype": "quantity", + "unit": "1", + "why": "Lorentz number for [81.196705, 6.980857, 0.551236, 2.88512] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "I need the Lorentz number for an electrical conductivity of 81.1967, a voltage difference of 6.98086, a thermal conductivity of 0.551236 and a temperature difference of 2.88512, please.", + "a": "2488.02400826893", + "valid": true, + "kind": "compute", + "expect": 2488.02400826893, + "dtype": "quantity", + "unit": "1", + "why": "Lorentz number for [81.196705, 6.980857, 0.551236, 2.88512] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "If the electrical conductivity is 1.16997, voltage difference is 1.14865, thermal conductivity is 21.7931 and the temperature difference is 29.5106, what is the Lorentz number?", + "a": "0.002400233255339044", + "valid": true, + "kind": "compute", + "expect": 0.002400233255339044, + "dtype": "quantity", + "unit": "1", + "why": "Lorentz number for [1.169968, 1.148652, 21.793126, 29.510609] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "Could you work out the Lorentz number given electrical conductivity = 1.16997, voltage difference = 1.14865, thermal conductivity = 21.7931, temperature difference = 29.5106?", + "a": "0.002400233255339044", + "valid": true, + "kind": "compute", + "expect": 0.002400233255339044, + "dtype": "quantity", + "unit": "1", + "why": "Lorentz number for [1.169968, 1.148652, 21.793126, 29.510609] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "I need the Lorentz number for an electrical conductivity of 223.509, a voltage difference of 17.3414, a thermal conductivity of 7.10705 and a temperature difference of 30.2507, please.", + "a": "312.63617484684903", + "valid": true, + "kind": "compute", + "expect": 312.63617484684903, + "dtype": "quantity", + "unit": "1", + "why": "Lorentz number for [223.508796, 17.341435, 7.107049, 30.250749] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "If the electrical conductivity is 223.509, voltage difference is 17.3414, thermal conductivity is 7.10705 and the temperature difference is 30.2507, what is the Lorentz number?", + "a": "312.63617484684903", + "valid": true, + "kind": "compute", + "expect": 312.63617484684903, + "dtype": "quantity", + "unit": "1", + "why": "Lorentz number for [223.508796, 17.341435, 7.107049, 30.250749] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "Could you work out the Lorentz number given electrical conductivity = 0.869111, voltage difference = 39.1753, thermal conductivity = 0.432217, temperature difference = 16.6571?", + "a": "185.26739451910058", + "valid": true, + "kind": "compute", + "expect": 185.26739451910058, + "dtype": "quantity", + "unit": "1", + "why": "Lorentz number for [0.869111, 39.175263, 0.432217, 16.657051] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "I need the Lorentz number for an electrical conductivity of 0.869111, a voltage difference of 39.1753, a thermal conductivity of 0.432217 and a temperature difference of 16.6571, please.", + "a": "185.26739451910058", + "valid": true, + "kind": "compute", + "expect": 185.26739451910058, + "dtype": "quantity", + "unit": "1", + "why": "Lorentz number for [0.869111, 39.175263, 0.432217, 16.657051] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "If the electrical conductivity is 167.625, voltage difference is 10.3291 and the thermal conductivity is 2.21571, what is the Lorentz number?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the temperature difference is not given" + }, + { + "q": "Could you work out the Lorentz number given electrical conductivity = 8.53158, voltage difference = 124.718, thermal conductivity = 1.91195?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the temperature difference is not given" + }, + { + "q": "Do you know the Lorentz 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 Lorentz 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 Lorentz number given electrical conductivity = -81.1967, voltage difference = 6.98086, thermal conductivity = 0.551236, temperature difference = 2.88512?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electrical conductivity: no example had one" + }, + { + "q": "What is a typical Lorentz 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-lorentz-number/program.py b/root/organs10/physics/specialists/solved-lorentz-number/program.py new file mode 100644 index 0000000000000000000000000000000000000000..db3c2211f509fcadd90f88237399aaf9e10c2e6b --- /dev/null +++ b/root/organs10/physics/specialists/solved-lorentz-number/program.py @@ -0,0 +1,7 @@ +"""Lorentz number from electrical conductivity, voltage difference, thermal conductivity, temperature difference (Lorentz number (Wikidata Q104901522)) +The maths organ (systems.py): solved from the system q0 = ((q1*( q2 ) **(2))/(q3*q4)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(electrical_conductivity, voltage_difference, thermal_conductivity, temperature_difference): + return 1.0*electrical_conductivity*voltage_difference**2/(temperature_difference*thermal_conductivity) diff --git a/root/organs10/physics/specialists/solved-lorentz-number/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-lorentz-number/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-lorentz-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-loss-factor/organ_formula_v2.py b/root/organs10/physics/specialists/solved-loss-factor/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-loss-factor/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-loss-factor/panel.json b/root/organs10/physics/specialists/solved-loss-factor/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..2cd9b36f101df634868ec341297565f99f8d4ffc --- /dev/null +++ b/root/organs10/physics/specialists/solved-loss-factor/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the loss factor for quality factor 107.961?", + "a": "0.00926263275385168", + "valid": true, + "kind": "compute", + "expect": 0.00926263275385168, + "dtype": "quantity", + "unit": "1", + "why": "loss factor for [107.960666] (the system loss factor (Wikidata Q79468728))" + }, + { + "q": "Tell me the loss factor with quality factor of 107.961.", + "a": "0.00926263275385168", + "valid": true, + "kind": "compute", + "expect": 0.00926263275385168, + "dtype": "quantity", + "unit": "1", + "why": "loss factor for [107.960666] (the system loss factor (Wikidata Q79468728))" + }, + { + "q": "What is the loss factor for quality factor 22.652?", + "a": "0.044146167826513254", + "valid": true, + "kind": "compute", + "expect": 0.044146167826513254, + "dtype": "quantity", + "unit": "1", + "why": "loss factor for [22.652023] (the system loss factor (Wikidata Q79468728))" + }, + { + "q": "Tell me the loss factor with quality factor of 22.652.", + "a": "0.044146167826513254", + "valid": true, + "kind": "compute", + "expect": 0.044146167826513254, + "dtype": "quantity", + "unit": "1", + "why": "loss factor for [22.652023] (the system loss factor (Wikidata Q79468728))" + }, + { + "q": "What is the loss factor for quality factor 173.394?", + "a": "0.0057672242718762815", + "valid": true, + "kind": "compute", + "expect": 0.0057672242718762815, + "dtype": "quantity", + "unit": "1", + "why": "loss factor for [173.393638] (the system loss factor (Wikidata Q79468728))" + }, + { + "q": "Tell me the loss factor with quality factor of 173.394.", + "a": "0.0057672242718762815", + "valid": true, + "kind": "compute", + "expect": 0.0057672242718762815, + "dtype": "quantity", + "unit": "1", + "why": "loss factor for [173.393638] (the system loss factor (Wikidata Q79468728))" + }, + { + "q": "What is the loss factor for quality factor 126.709?", + "a": "0.007892083536070259", + "valid": true, + "kind": "compute", + "expect": 0.007892083536070259, + "dtype": "quantity", + "unit": "1", + "why": "loss factor for [126.709252] (the system loss factor (Wikidata Q79468728))" + }, + { + "q": "Tell me the loss factor with quality factor of 126.709.", + "a": "0.007892083536070259", + "valid": true, + "kind": "compute", + "expect": 0.007892083536070259, + "dtype": "quantity", + "unit": "1", + "why": "loss factor for [126.709252] (the system loss factor (Wikidata Q79468728))" + }, + { + "q": "What is the loss factor for quality factor 0.671697?", + "a": "1.4887660694527203", + "valid": true, + "kind": "compute", + "expect": 1.4887660694527203, + "dtype": "quantity", + "unit": "1", + "why": "loss factor for [0.671697] (the system loss factor (Wikidata Q79468728))" + }, + { + "q": "Tell me the loss factor with quality factor of 0.671697.", + "a": "1.4887660694527203", + "valid": true, + "kind": "compute", + "expect": 1.4887660694527203, + "dtype": "quantity", + "unit": "1", + "why": "loss factor for [0.671697] (the system loss factor (Wikidata Q79468728))" + }, + { + "q": "What is the loss factor for quality factor 1.16997?", + "a": "0.8547242274066585", + "valid": true, + "kind": "compute", + "expect": 0.8547242274066585, + "dtype": "quantity", + "unit": "1", + "why": "loss factor for [1.169968] (the system loss factor (Wikidata Q79468728))" + }, + { + "q": "Tell me the loss factor with quality factor of 1.16997.", + "a": "0.8547242274066585", + "valid": true, + "kind": "compute", + "expect": 0.8547242274066585, + "dtype": "quantity", + "unit": "1", + "why": "loss factor for [1.169968] (the system loss factor (Wikidata Q79468728))" + }, + { + "q": "What is the loss factor?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the quality factor is not given" + }, + { + "q": "Could you work out the loss factor, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the quality factor is not given" + }, + { + "q": "What is the loss factor 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 loss factor 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 loss factor with quality factor of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative quality factor: no example had one" + }, + { + "q": "Which loss factor 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 quality factor is 2.57003, what is the loss factor?", + "a": "0.38910052946214563", + "valid": true, + "kind": "compute", + "expect": 0.38910052946214563, + "dtype": "quantity", + "unit": "1", + "why": "loss factor for [2.57003] (the system loss factor (Wikidata Q79468728))" + }, + { + "q": "Could you work out the loss factor given quality factor = 2.57003?", + "a": "0.38910052946214563", + "valid": true, + "kind": "compute", + "expect": 0.38910052946214563, + "dtype": "quantity", + "unit": "1", + "why": "loss factor for [2.57003] (the system loss factor (Wikidata Q79468728))" + }, + { + "q": "I need the loss factor for a quality factor of 48.921, please.", + "a": "0.02044112317474344", + "valid": true, + "kind": "compute", + "expect": 0.02044112317474344, + "dtype": "quantity", + "unit": "1", + "why": "loss factor for [48.920991] (the system loss factor (Wikidata Q79468728))" + }, + { + "q": "If the quality factor is 48.921, what is the loss factor?", + "a": "0.02044112317474344", + "valid": true, + "kind": "compute", + "expect": 0.02044112317474344, + "dtype": "quantity", + "unit": "1", + "why": "loss factor for [48.920991] (the system loss factor (Wikidata Q79468728))" + }, + { + "q": "Could you work out the loss factor given quality factor = 0.348438?", + "a": "2.869953759769417", + "valid": true, + "kind": "compute", + "expect": 2.869953759769417, + "dtype": "quantity", + "unit": "1", + "why": "loss factor for [0.348438] (the system loss factor (Wikidata Q79468728))" + }, + { + "q": "I need the loss factor for a quality factor of 0.348438, please.", + "a": "2.869953759769417", + "valid": true, + "kind": "compute", + "expect": 2.869953759769417, + "dtype": "quantity", + "unit": "1", + "why": "loss factor for [0.348438] (the system loss factor (Wikidata Q79468728))" + }, + { + "q": "If the quality factor is 252.709, what is the loss factor?", + "a": "0.0039571202610184176", + "valid": true, + "kind": "compute", + "expect": 0.0039571202610184176, + "dtype": "quantity", + "unit": "1", + "why": "loss factor for [252.709024] (the system loss factor (Wikidata Q79468728))" + }, + { + "q": "Could you work out the loss factor given quality factor = 252.709?", + "a": "0.0039571202610184176", + "valid": true, + "kind": "compute", + "expect": 0.0039571202610184176, + "dtype": "quantity", + "unit": "1", + "why": "loss factor for [252.709024] (the system loss factor (Wikidata Q79468728))" + }, + { + "q": "I need the loss factor for a quality factor of 20.3966, please.", + "a": "0.04902788018766951", + "valid": true, + "kind": "compute", + "expect": 0.04902788018766951, + "dtype": "quantity", + "unit": "1", + "why": "loss factor for [20.396558] (the system loss factor (Wikidata Q79468728))" + }, + { + "q": "If the quality factor is 20.3966, what is the loss factor?", + "a": "0.04902788018766951", + "valid": true, + "kind": "compute", + "expect": 0.04902788018766951, + "dtype": "quantity", + "unit": "1", + "why": "loss factor for [20.396558] (the system loss factor (Wikidata Q79468728))" + }, + { + "q": "Could you work out the loss factor given quality factor = 134.993?", + "a": "0.007407793842889246", + "valid": true, + "kind": "compute", + "expect": 0.007407793842889246, + "dtype": "quantity", + "unit": "1", + "why": "loss factor for [134.992958] (the system loss factor (Wikidata Q79468728))" + }, + { + "q": "I need the loss factor for a quality factor of 134.993, please.", + "a": "0.007407793842889246", + "valid": true, + "kind": "compute", + "expect": 0.007407793842889246, + "dtype": "quantity", + "unit": "1", + "why": "loss factor for [134.992958] (the system loss factor (Wikidata Q79468728))" + }, + { + "q": "I need the loss factor, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the quality factor is not given" + }, + { + "q": "Can you tell me the loss factor?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the quality factor is not given" + }, + { + "q": "Do you know the loss factor 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 loss factor 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 loss factor given quality factor = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative quality factor: no example had one" + }, + { + "q": "What is a typical loss factor?", + "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-loss-factor/program.py b/root/organs10/physics/specialists/solved-loss-factor/program.py new file mode 100644 index 0000000000000000000000000000000000000000..64a68c1a7c426330cd4e18fa623bd83e14584a17 --- /dev/null +++ b/root/organs10/physics/specialists/solved-loss-factor/program.py @@ -0,0 +1,7 @@ +"""loss factor from quality factor (loss factor (Wikidata Q79468728)) +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(quality_factor): + return 1.0/quality_factor diff --git a/root/organs10/physics/specialists/solved-loss-factor/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-loss-factor/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-loss-factor/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-mach-number/organ_formula_v2.py b/root/organs10/physics/specialists/solved-mach-number/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-mach-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-mach-number/panel.json b/root/organs10/physics/specialists/solved-mach-number/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..5450efc66317cc3158d6eed90e4008693899c3db --- /dev/null +++ b/root/organs10/physics/specialists/solved-mach-number/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the Mach number for speed 107.961 and speed of sound 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "Mach number for [107.960666, 59.4105] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Tell me the Mach number with speed of 107.961 and speed of sound of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "Mach number for [107.960666, 59.4105] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "What is the Mach number for speed 169.527 and speed of sound 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "Mach number for [169.526958, 280.773702] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Tell me the Mach number with speed of 169.527 and speed of sound of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "Mach number for [169.526958, 280.773702] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "What is the Mach number for speed 1.91195 and speed of sound 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "Mach number for [1.911952, 82.240072] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Tell me the Mach number with speed of 1.91195 and speed of sound of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "Mach number for [1.911952, 82.240072] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "What is the Mach number for speed 1.64379 and speed of sound 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "Mach number for [1.643795, 252.709024] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Tell me the Mach number with speed of 1.64379 and speed of sound of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "Mach number for [1.643795, 252.709024] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "What is the Mach number for speed 18.3169 and speed of sound 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "Mach number for [18.316867, 6.838598] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Tell me the Mach number with speed of 18.3169 and speed of sound of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "Mach number for [18.316867, 6.838598] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "What is the Mach number for speed 156.502 and speed of sound 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "Mach number for [156.50181, 185.886273] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Tell me the Mach number with speed of 156.502 and speed of sound of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "Mach number for [156.50181, 185.886273] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "What is the Mach number for speed 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed of sound is not given" + }, + { + "q": "Tell me the Mach number with speed of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed of sound is not given" + }, + { + "q": "What is the Mach 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 Mach 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 Mach number with speed of -1.91195 and speed of sound 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 Mach 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 speed is 8.50728 and the speed of sound is 17.7888, what is the Mach number?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "Mach number for [8.507283, 17.788778] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Could you work out the Mach number given speed = 8.50728, speed of sound = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "Mach number for [8.507283, 17.788778] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "I need the Mach number for a speed of 8.24951 and a speed of sound of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "Mach number for [8.24951, 0.634021] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "If the speed is 8.24951 and the speed of sound is 0.634021, what is the Mach number?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "Mach number for [8.24951, 0.634021] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Could you work out the Mach number given speed = 0.318734, speed of sound = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "Mach number for [0.318734, 9.566071] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "I need the Mach number for a speed of 0.318734 and a speed of sound of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "Mach number for [0.318734, 9.566071] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "If the speed is 0.671697 and the speed of sound is 14.2496, what is the Mach number?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "Mach number for [0.671697, 14.249569] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Could you work out the Mach number given speed = 0.671697, speed of sound = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "Mach number for [0.671697, 14.249569] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "I need the Mach number for a speed of 21.7931 and a speed of sound of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "Mach number for [21.793126, 29.510609] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "If the speed is 21.7931 and the speed of sound is 29.5106, what is the Mach number?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "Mach number for [21.793126, 29.510609] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Could you work out the Mach number given speed = 153.15, speed of sound = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "Mach number for [153.149965, 18.595315] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "I need the Mach number for a speed of 153.15 and a speed of sound of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "Mach number for [153.149965, 18.595315] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "If the speed is 8.50728, what is the Mach number?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed of sound is not given" + }, + { + "q": "Could you work out the Mach number given speed = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed of sound is not given" + }, + { + "q": "Do you know the Mach 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 Mach 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 Mach number given speed = -0.318734, speed of sound = 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 Mach 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-mach-number/program.py b/root/organs10/physics/specialists/solved-mach-number/program.py new file mode 100644 index 0000000000000000000000000000000000000000..f085cf36122ca21c57bc0dcbca77f55d856f3500 --- /dev/null +++ b/root/organs10/physics/specialists/solved-mach-number/program.py @@ -0,0 +1,7 @@ +"""Mach number from speed, speed of sound (Mach number (Wikidata Q160669)) +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, speed_of_sound): + return 1.0*speed/speed_of_sound diff --git a/root/organs10/physics/specialists/solved-mach-number/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-mach-number/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-mach-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-magnetic-dipole-moment-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-dipole-moment-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-dipole-moment-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-magnetic-dipole-moment-2/panel.json b/root/organs10/physics/specialists/solved-magnetic-dipole-moment-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..e9ff4938b44d758e7b710ab7211bc5167b6568b1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-dipole-moment-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic dipole moment for nuclear g factor 107.961 and nuclear spin quantum number 59.4105?", + "a": "3.2395712308709e-23", + "valid": true, + "kind": "compute", + "expect": 3.2395712308709e-23, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [107.960666, 59.4105] (the system nuclear g factor (Wikidata Q97591250))" + }, + { + "q": "Tell me the magnetic dipole moment with nuclear g factor of 107.961 and nuclear spin quantum number of 59.4105.", + "a": "3.2395712308709e-23", + "valid": true, + "kind": "compute", + "expect": 3.2395712308709e-23, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [107.960666, 59.4105] (the system nuclear g factor (Wikidata Q97591250))" + }, + { + "q": "What is the magnetic dipole moment for nuclear g factor 169.527 and nuclear spin quantum number 280.774?", + "a": "2.4041079586965854e-22", + "valid": true, + "kind": "compute", + "expect": 2.4041079586965854e-22, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [169.526958, 280.773702] (the system nuclear g factor (Wikidata Q97591250))" + }, + { + "q": "Tell me the magnetic dipole moment with nuclear g factor of 169.527 and nuclear spin quantum number of 280.774.", + "a": "2.4041079586965854e-22", + "valid": true, + "kind": "compute", + "expect": 2.4041079586965854e-22, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [169.526958, 280.773702] (the system nuclear g factor (Wikidata Q97591250))" + }, + { + "q": "What is the magnetic dipole moment for nuclear g factor 1.91195 and nuclear spin quantum number 82.2401?", + "a": "7.941804323423716e-25", + "valid": true, + "kind": "compute", + "expect": 7.941804323423716e-25, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [1.911952, 82.240072] (the system nuclear g factor (Wikidata Q97591250))" + }, + { + "q": "Tell me the magnetic dipole moment with nuclear g factor of 1.91195 and nuclear spin quantum number of 82.2401.", + "a": "7.941804323423716e-25", + "valid": true, + "kind": "compute", + "expect": 7.941804323423716e-25, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [1.911952, 82.240072] (the system nuclear g factor (Wikidata Q97591250))" + }, + { + "q": "What is the magnetic dipole moment for nuclear g factor 1.64379 and nuclear spin quantum number 252.709?", + "a": "2.0981054257385274e-24", + "valid": true, + "kind": "compute", + "expect": 2.0981054257385274e-24, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [1.643795, 252.709024] (the system nuclear g factor (Wikidata Q97591250))" + }, + { + "q": "Tell me the magnetic dipole moment with nuclear g factor of 1.64379 and nuclear spin quantum number of 252.709.", + "a": "2.0981054257385274e-24", + "valid": true, + "kind": "compute", + "expect": 2.0981054257385274e-24, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [1.643795, 252.709024] (the system nuclear g factor (Wikidata Q97591250))" + }, + { + "q": "What is the magnetic dipole moment for nuclear g factor 18.3169 and nuclear spin quantum number 6.8386?", + "a": "6.32669708594482e-25", + "valid": true, + "kind": "compute", + "expect": 6.32669708594482e-25, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [18.316867, 6.838598] (the system nuclear g factor (Wikidata Q97591250))" + }, + { + "q": "Tell me the magnetic dipole moment with nuclear g factor of 18.3169 and nuclear spin quantum number of 6.8386.", + "a": "6.32669708594482e-25", + "valid": true, + "kind": "compute", + "expect": 6.32669708594482e-25, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [18.316867, 6.838598] (the system nuclear g factor (Wikidata Q97591250))" + }, + { + "q": "What is the magnetic dipole moment for nuclear g factor 156.502 and nuclear spin quantum number 185.886?", + "a": "1.469350664404658e-22", + "valid": true, + "kind": "compute", + "expect": 1.469350664404658e-22, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [156.50181, 185.886273] (the system nuclear g factor (Wikidata Q97591250))" + }, + { + "q": "Tell me the magnetic dipole moment with nuclear g factor of 156.502 and nuclear spin quantum number of 185.886.", + "a": "1.469350664404658e-22", + "valid": true, + "kind": "compute", + "expect": 1.469350664404658e-22, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [156.50181, 185.886273] (the system nuclear g factor (Wikidata Q97591250))" + }, + { + "q": "What is the magnetic dipole moment for nuclear g factor 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the nuclear spin quantum number is not given" + }, + { + "q": "Tell me the magnetic dipole moment with nuclear g factor of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the nuclear spin quantum number is not given" + }, + { + "q": "What is the magnetic dipole moment 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 magnetic dipole moment 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 magnetic dipole moment with nuclear g factor of -1.91195 and nuclear spin quantum number of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative nuclear g factor: no example had one" + }, + { + "q": "Which magnetic dipole moment 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 nuclear g factor is 8.50728 and the nuclear spin quantum number is 17.7888, what is the magnetic dipole moment?", + "a": "7.64356126617076e-25", + "valid": true, + "kind": "compute", + "expect": 7.64356126617076e-25, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [8.507283, 17.788778] (the system nuclear g factor (Wikidata Q97591250))" + }, + { + "q": "Could you work out the magnetic dipole moment given nuclear g factor = 8.50728, nuclear spin quantum number = 17.7888?", + "a": "7.64356126617076e-25", + "valid": true, + "kind": "compute", + "expect": 7.64356126617076e-25, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [8.507283, 17.788778] (the system nuclear g factor (Wikidata Q97591250))" + }, + { + "q": "I need the magnetic dipole moment for a nuclear g factor of 8.24951 and a nuclear spin quantum number of 0.634021, please.", + "a": "2.641742850314438e-26", + "valid": true, + "kind": "compute", + "expect": 2.641742850314438e-26, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [8.24951, 0.634021] (the system nuclear g factor (Wikidata Q97591250))" + }, + { + "q": "If the nuclear g factor is 8.24951 and the nuclear spin quantum number is 0.634021, what is the magnetic dipole moment?", + "a": "2.641742850314438e-26", + "valid": true, + "kind": "compute", + "expect": 2.641742850314438e-26, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [8.24951, 0.634021] (the system nuclear g factor (Wikidata Q97591250))" + }, + { + "q": "Could you work out the magnetic dipole moment given nuclear g factor = 0.318734, nuclear spin quantum number = 9.56607?", + "a": "1.5400003781790495e-26", + "valid": true, + "kind": "compute", + "expect": 1.5400003781790495e-26, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [0.318734, 9.566071] (the system nuclear g factor (Wikidata Q97591250))" + }, + { + "q": "I need the magnetic dipole moment for a nuclear g factor of 0.318734 and a nuclear spin quantum number of 9.56607, please.", + "a": "1.5400003781790495e-26", + "valid": true, + "kind": "compute", + "expect": 1.5400003781790495e-26, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [0.318734, 9.566071] (the system nuclear g factor (Wikidata Q97591250))" + }, + { + "q": "If the nuclear g factor is 0.671697 and the nuclear spin quantum number is 14.2496, what is the magnetic dipole moment?", + "a": "4.83430499226543e-26", + "valid": true, + "kind": "compute", + "expect": 4.83430499226543e-26, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [0.671697, 14.249569] (the system nuclear g factor (Wikidata Q97591250))" + }, + { + "q": "Could you work out the magnetic dipole moment given nuclear g factor = 0.671697, nuclear spin quantum number = 14.2496?", + "a": "4.83430499226543e-26", + "valid": true, + "kind": "compute", + "expect": 4.83430499226543e-26, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [0.671697, 14.249569] (the system nuclear g factor (Wikidata Q97591250))" + }, + { + "q": "I need the magnetic dipole moment for a nuclear g factor of 21.7931 and a nuclear spin quantum number of 29.5106, please.", + "a": "3.2483025774221433e-24", + "valid": true, + "kind": "compute", + "expect": 3.2483025774221433e-24, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [21.793126, 29.510609] (the system nuclear g factor (Wikidata Q97591250))" + }, + { + "q": "If the nuclear g factor is 21.7931 and the nuclear spin quantum number is 29.5106, what is the magnetic dipole moment?", + "a": "3.2483025774221433e-24", + "valid": true, + "kind": "compute", + "expect": 3.2483025774221433e-24, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [21.793126, 29.510609] (the system nuclear g factor (Wikidata Q97591250))" + }, + { + "q": "Could you work out the magnetic dipole moment given nuclear g factor = 153.15, nuclear spin quantum number = 18.5953?", + "a": "1.438398505051275e-23", + "valid": true, + "kind": "compute", + "expect": 1.438398505051275e-23, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [153.149965, 18.595315] (the system nuclear g factor (Wikidata Q97591250))" + }, + { + "q": "I need the magnetic dipole moment for a nuclear g factor of 153.15 and a nuclear spin quantum number of 18.5953, please.", + "a": "1.438398505051275e-23", + "valid": true, + "kind": "compute", + "expect": 1.438398505051275e-23, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [153.149965, 18.595315] (the system nuclear g factor (Wikidata Q97591250))" + }, + { + "q": "If the nuclear g factor is 8.50728, what is the magnetic dipole moment?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the nuclear spin quantum number is not given" + }, + { + "q": "Could you work out the magnetic dipole moment given nuclear g factor = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the nuclear spin quantum number is not given" + }, + { + "q": "Do you know the magnetic dipole moment 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 magnetic dipole moment 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 magnetic dipole moment given nuclear g factor = -0.318734, nuclear spin quantum number = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative nuclear g factor: no example had one" + }, + { + "q": "What is a typical magnetic dipole moment?", + "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-magnetic-dipole-moment-2/program.py b/root/organs10/physics/specialists/solved-magnetic-dipole-moment-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..b973ad4b327cb0d7c702179f63cf65b287887041 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-dipole-moment-2/program.py @@ -0,0 +1,7 @@ +"""magnetic dipole moment from nuclear g factor, nuclear spin quantum number (nuclear g factor (Wikidata Q97591250)) +The maths organ (systems.py): solved from the system q1 = ((q2)/(q3*q0)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(nuclear_g_factor, nuclear_spin_quantum_number): + return 5.05078369899999e-27*nuclear_g_factor*nuclear_spin_quantum_number diff --git a/root/organs10/physics/specialists/solved-magnetic-dipole-moment-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-dipole-moment-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-dipole-moment-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-magnetic-dipole-moment-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-dipole-moment-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-dipole-moment-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-magnetic-dipole-moment-3/panel.json b/root/organs10/physics/specialists/solved-magnetic-dipole-moment-3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..e116b329ca62209723c6e1171906db00cbfdd0a9 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-dipole-moment-3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic dipole moment for Landé factor 107.961 and total angular momentum quantum number 59.4105?", + "a": "5.948347423707203e-20", + "valid": true, + "kind": "compute", + "expect": 5.948347423707203e-20, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [107.960666, 59.4105] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "Tell me the magnetic dipole moment with Landé factor of 107.961 and total angular momentum quantum number of 59.4105.", + "a": "5.948347423707203e-20", + "valid": true, + "kind": "compute", + "expect": 5.948347423707203e-20, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [107.960666, 59.4105] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "What is the magnetic dipole moment for Landé factor 169.527 and total angular momentum quantum number 280.774?", + "a": "4.414309290733637e-19", + "valid": true, + "kind": "compute", + "expect": 4.414309290733637e-19, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [169.526958, 280.773702] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "Tell me the magnetic dipole moment with Landé factor of 169.527 and total angular momentum quantum number of 280.774.", + "a": "4.414309290733637e-19", + "valid": true, + "kind": "compute", + "expect": 4.414309290733637e-19, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [169.526958, 280.773702] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "What is the magnetic dipole moment for Landé factor 1.91195 and total angular momentum quantum number 82.2401?", + "a": "1.4582365356456266e-21", + "valid": true, + "kind": "compute", + "expect": 1.4582365356456266e-21, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [1.911952, 82.240072] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "Tell me the magnetic dipole moment with Landé factor of 1.91195 and total angular momentum quantum number of 82.2401.", + "a": "1.4582365356456266e-21", + "valid": true, + "kind": "compute", + "expect": 1.4582365356456266e-21, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [1.911952, 82.240072] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "What is the magnetic dipole moment for Landé factor 1.64379 and total angular momentum quantum number 252.709?", + "a": "3.852441917291253e-21", + "valid": true, + "kind": "compute", + "expect": 3.852441917291253e-21, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [1.643795, 252.709024] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "Tell me the magnetic dipole moment with Landé factor of 1.64379 and total angular momentum quantum number of 252.709.", + "a": "3.852441917291253e-21", + "valid": true, + "kind": "compute", + "expect": 3.852441917291253e-21, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [1.643795, 252.709024] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "What is the magnetic dipole moment for Landé factor 18.3169 and total angular momentum quantum number 6.8386?", + "a": "1.1616781860863325e-21", + "valid": true, + "kind": "compute", + "expect": 1.1616781860863325e-21, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [18.316867, 6.838598] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "Tell me the magnetic dipole moment with Landé factor of 18.3169 and total angular momentum quantum number of 6.8386.", + "a": "1.1616781860863325e-21", + "valid": true, + "kind": "compute", + "expect": 1.1616781860863325e-21, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [18.316867, 6.838598] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "What is the magnetic dipole moment for Landé factor 156.502 and total angular momentum quantum number 185.886?", + "a": "2.697952172141086e-19", + "valid": true, + "kind": "compute", + "expect": 2.697952172141086e-19, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [156.50181, 185.886273] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "Tell me the magnetic dipole moment with Landé factor of 156.502 and total angular momentum quantum number of 185.886.", + "a": "2.697952172141086e-19", + "valid": true, + "kind": "compute", + "expect": 2.697952172141086e-19, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [156.50181, 185.886273] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "What is the magnetic dipole moment for Landé factor 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total angular momentum quantum number is not given" + }, + { + "q": "Tell me the magnetic dipole moment with Landé factor of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total angular momentum quantum number is not given" + }, + { + "q": "What is the magnetic dipole moment 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 magnetic dipole moment 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 magnetic dipole moment with Landé factor of -1.91195 and total angular momentum quantum number of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Landé factor: no example had one" + }, + { + "q": "Which magnetic dipole moment 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 Landé factor is 8.50728 and the total angular momentum quantum number is 17.7888, what is the magnetic dipole moment?", + "a": "1.4034745565187698e-21", + "valid": true, + "kind": "compute", + "expect": 1.4034745565187698e-21, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [8.507283, 17.788778] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "Could you work out the magnetic dipole moment given Landé factor = 8.50728, total angular momentum quantum number = 17.7888?", + "a": "1.4034745565187698e-21", + "valid": true, + "kind": "compute", + "expect": 1.4034745565187698e-21, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [8.507283, 17.788778] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "I need the magnetic dipole moment for a Landé factor of 8.24951 and a total angular momentum quantum number of 0.634021, please.", + "a": "4.850643235753267e-23", + "valid": true, + "kind": "compute", + "expect": 4.850643235753267e-23, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [8.24951, 0.634021] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "If the Landé factor is 8.24951 and the total angular momentum quantum number is 0.634021, what is the magnetic dipole moment?", + "a": "4.850643235753267e-23", + "valid": true, + "kind": "compute", + "expect": 4.850643235753267e-23, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [8.24951, 0.634021] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "Could you work out the magnetic dipole moment given Landé factor = 0.318734, total angular momentum quantum number = 9.56607?", + "a": "2.8276758339982065e-23", + "valid": true, + "kind": "compute", + "expect": 2.8276758339982065e-23, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [0.318734, 9.566071] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "I need the magnetic dipole moment for a Landé factor of 0.318734 and a total angular momentum quantum number of 9.56607, please.", + "a": "2.8276758339982065e-23", + "valid": true, + "kind": "compute", + "expect": 2.8276758339982065e-23, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [0.318734, 9.566071] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "If the Landé factor is 0.671697 and the total angular momentum quantum number is 14.2496, what is the magnetic dipole moment?", + "a": "8.876522106422825e-23", + "valid": true, + "kind": "compute", + "expect": 8.876522106422825e-23, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [0.671697, 14.249569] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "Could you work out the magnetic dipole moment given Landé factor = 0.671697, total angular momentum quantum number = 14.2496?", + "a": "8.876522106422825e-23", + "valid": true, + "kind": "compute", + "expect": 8.876522106422825e-23, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [0.671697, 14.249569] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "I need the magnetic dipole moment for a Landé factor of 21.7931 and a total angular momentum quantum number of 29.5106, please.", + "a": "5.964379509147604e-21", + "valid": true, + "kind": "compute", + "expect": 5.964379509147604e-21, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [21.793126, 29.510609] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "If the Landé factor is 21.7931 and the total angular momentum quantum number is 29.5106, what is the magnetic dipole moment?", + "a": "5.964379509147604e-21", + "valid": true, + "kind": "compute", + "expect": 5.964379509147604e-21, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [21.793126, 29.510609] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "Could you work out the magnetic dipole moment given Landé factor = 153.15, total angular momentum quantum number = 18.5953?", + "a": "2.6411192815432843e-20", + "valid": true, + "kind": "compute", + "expect": 2.6411192815432843e-20, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [153.149965, 18.595315] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "I need the magnetic dipole moment for a Landé factor of 153.15 and a total angular momentum quantum number of 18.5953, please.", + "a": "2.6411192815432843e-20", + "valid": true, + "kind": "compute", + "expect": 2.6411192815432843e-20, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [153.149965, 18.595315] (the system Landé factor (Wikidata Q1191684))" + }, + { + "q": "If the Landé factor is 8.50728, what is the magnetic dipole moment?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total angular momentum quantum number is not given" + }, + { + "q": "Could you work out the magnetic dipole moment given Landé factor = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total angular momentum quantum number is not given" + }, + { + "q": "Do you know the magnetic dipole moment 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 magnetic dipole moment 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 magnetic dipole moment given Landé factor = -0.318734, total angular momentum quantum number = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Landé factor: no example had one" + }, + { + "q": "What is a typical magnetic dipole moment?", + "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-magnetic-dipole-moment-3/program.py b/root/organs10/physics/specialists/solved-magnetic-dipole-moment-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..ba0a45ba5707df6661cf890f4d22ec0f541e2e03 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-dipole-moment-3/program.py @@ -0,0 +1,7 @@ +"""magnetic dipole moment from Landé factor, total angular momentum quantum number (Landé factor (Wikidata Q1191684)) +The maths organ (systems.py): solved from the system q1 = ((q2)/(q3*q0)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(land__factor, total_angular_momentum_quantum_number): + return 9.27401006570002e-24*land__factor*total_angular_momentum_quantum_number diff --git a/root/organs10/physics/specialists/solved-magnetic-dipole-moment-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-dipole-moment-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-dipole-moment-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-magnetic-dipole-moment-4/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-dipole-moment-4/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-dipole-moment-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-magnetic-dipole-moment-4/panel.json b/root/organs10/physics/specialists/solved-magnetic-dipole-moment-4/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..3b940d2aa9124a72816b49a74d0da2ebf28a05eb --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-dipole-moment-4/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic dipole moment for total angular momentum 107.961?", + "a": "19010357821838.31", + "valid": true, + "kind": "compute", + "expect": 19010357821838.31, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [107.960666] (the system electron gyromagnetic ratio (Wikidata Q97543076))" + }, + { + "q": "Tell me the magnetic dipole moment with total angular momentum of 107.961.", + "a": "19010357821838.31", + "valid": true, + "kind": "compute", + "expect": 19010357821838.31, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [107.960666] (the system electron gyromagnetic ratio (Wikidata Q97543076))" + }, + { + "q": "What is the magnetic dipole moment for total angular momentum 22.652?", + "a": "3988703248603.3022", + "valid": true, + "kind": "compute", + "expect": 3988703248603.3022, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [22.652023] (the system electron gyromagnetic ratio (Wikidata Q97543076))" + }, + { + "q": "Tell me the magnetic dipole moment with total angular momentum of 22.652.", + "a": "3988703248603.3022", + "valid": true, + "kind": "compute", + "expect": 3988703248603.3022, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [22.652023] (the system electron gyromagnetic ratio (Wikidata Q97543076))" + }, + { + "q": "What is the magnetic dipole moment for total angular momentum 173.394?", + "a": "30532185800659.53", + "valid": true, + "kind": "compute", + "expect": 30532185800659.53, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [173.393638] (the system electron gyromagnetic ratio (Wikidata Q97543076))" + }, + { + "q": "Tell me the magnetic dipole moment with total angular momentum of 173.394.", + "a": "30532185800659.53", + "valid": true, + "kind": "compute", + "expect": 30532185800659.53, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [173.393638] (the system electron gyromagnetic ratio (Wikidata Q97543076))" + }, + { + "q": "What is the magnetic dipole moment for total angular momentum 126.709?", + "a": "22311720627158.91", + "valid": true, + "kind": "compute", + "expect": 22311720627158.91, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [126.709252] (the system electron gyromagnetic ratio (Wikidata Q97543076))" + }, + { + "q": "Tell me the magnetic dipole moment with total angular momentum of 126.709.", + "a": "22311720627158.91", + "valid": true, + "kind": "compute", + "expect": 22311720627158.91, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [126.709252] (the system electron gyromagnetic ratio (Wikidata Q97543076))" + }, + { + "q": "What is the magnetic dipole moment for total angular momentum 0.671697?", + "a": "118276448285.613", + "valid": true, + "kind": "compute", + "expect": 118276448285.613, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [0.671697] (the system electron gyromagnetic ratio (Wikidata Q97543076))" + }, + { + "q": "Tell me the magnetic dipole moment with total angular momentum of 0.671697.", + "a": "118276448285.613", + "valid": true, + "kind": "compute", + "expect": 118276448285.613, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [0.671697] (the system electron gyromagnetic ratio (Wikidata Q97543076))" + }, + { + "q": "What is the magnetic dipole moment for total angular momentum 1.16997?", + "a": "206014943038.72385", + "valid": true, + "kind": "compute", + "expect": 206014943038.72385, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [1.169968] (the system electron gyromagnetic ratio (Wikidata Q97543076))" + }, + { + "q": "Tell me the magnetic dipole moment with total angular momentum of 1.16997.", + "a": "206014943038.72385", + "valid": true, + "kind": "compute", + "expect": 206014943038.72385, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [1.169968] (the system electron gyromagnetic ratio (Wikidata Q97543076))" + }, + { + "q": "What is the magnetic dipole moment?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total angular momentum is not given" + }, + { + "q": "Could you work out the magnetic dipole moment, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total angular momentum is not given" + }, + { + "q": "What is the magnetic dipole moment 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 magnetic dipole moment 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 magnetic dipole moment with total angular momentum of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative total angular momentum: no example had one" + }, + { + "q": "Which magnetic dipole moment 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 angular momentum is 2.57003, what is the magnetic dipole moment?", + "a": "452546192281.9893", + "valid": true, + "kind": "compute", + "expect": 452546192281.9893, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [2.57003] (the system electron gyromagnetic ratio (Wikidata Q97543076))" + }, + { + "q": "Could you work out the magnetic dipole moment given total angular momentum = 2.57003?", + "a": "452546192281.9893", + "valid": true, + "kind": "compute", + "expect": 452546192281.9893, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [2.57003] (the system electron gyromagnetic ratio (Wikidata Q97543076))" + }, + { + "q": "I need the magnetic dipole moment for a total angular momentum of 48.921, please.", + "a": "8614299787624.564", + "valid": true, + "kind": "compute", + "expect": 8614299787624.564, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [48.920991] (the system electron gyromagnetic ratio (Wikidata Q97543076))" + }, + { + "q": "If the total angular momentum is 48.921, what is the magnetic dipole moment?", + "a": "8614299787624.564", + "valid": true, + "kind": "compute", + "expect": 8614299787624.564, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [48.920991] (the system electron gyromagnetic ratio (Wikidata Q97543076))" + }, + { + "q": "Could you work out the magnetic dipole moment given total angular momentum = 0.348438?", + "a": "61354982610.29384", + "valid": true, + "kind": "compute", + "expect": 61354982610.29384, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [0.348438] (the system electron gyromagnetic ratio (Wikidata Q97543076))" + }, + { + "q": "I need the magnetic dipole moment for a total angular momentum of 0.348438, please.", + "a": "61354982610.29384", + "valid": true, + "kind": "compute", + "expect": 61354982610.29384, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [0.348438] (the system electron gyromagnetic ratio (Wikidata Q97543076))" + }, + { + "q": "If the total angular momentum is 252.709, what is the magnetic dipole moment?", + "a": "44498511899580.71", + "valid": true, + "kind": "compute", + "expect": 44498511899580.71, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [252.709024] (the system electron gyromagnetic ratio (Wikidata Q97543076))" + }, + { + "q": "Could you work out the magnetic dipole moment given total angular momentum = 252.709?", + "a": "44498511899580.71", + "valid": true, + "kind": "compute", + "expect": 44498511899580.71, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [252.709024] (the system electron gyromagnetic ratio (Wikidata Q97543076))" + }, + { + "q": "I need the magnetic dipole moment for a total angular momentum of 20.3966, please.", + "a": "3591547551086.77", + "valid": true, + "kind": "compute", + "expect": 3591547551086.77, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [20.396558] (the system electron gyromagnetic ratio (Wikidata Q97543076))" + }, + { + "q": "If the total angular momentum is 20.3966, what is the magnetic dipole moment?", + "a": "3591547551086.77", + "valid": true, + "kind": "compute", + "expect": 3591547551086.77, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [20.396558] (the system electron gyromagnetic ratio (Wikidata Q97543076))" + }, + { + "q": "Could you work out the magnetic dipole moment given total angular momentum = 134.993?", + "a": "23770364936927.24", + "valid": true, + "kind": "compute", + "expect": 23770364936927.24, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [134.992958] (the system electron gyromagnetic ratio (Wikidata Q97543076))" + }, + { + "q": "I need the magnetic dipole moment for a total angular momentum of 134.993, please.", + "a": "23770364936927.24", + "valid": true, + "kind": "compute", + "expect": 23770364936927.24, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [134.992958] (the system electron gyromagnetic ratio (Wikidata Q97543076))" + }, + { + "q": "I need the magnetic dipole moment, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total angular momentum is not given" + }, + { + "q": "Can you tell me the magnetic dipole moment?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total angular momentum is not given" + }, + { + "q": "Do you know the magnetic dipole moment 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 magnetic dipole moment 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 magnetic dipole moment given total angular momentum = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative total angular momentum: no example had one" + }, + { + "q": "What is a typical magnetic dipole moment?", + "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-magnetic-dipole-moment-4/program.py b/root/organs10/physics/specialists/solved-magnetic-dipole-moment-4/program.py new file mode 100644 index 0000000000000000000000000000000000000000..73848ff8dc24eebc34ac36928323af752b81f4f7 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-dipole-moment-4/program.py @@ -0,0 +1,7 @@ +"""magnetic dipole moment from total angular momentum (electron gyromagnetic ratio (Wikidata Q97543076)) +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_angular_momentum): + return 176085963023.0*total_angular_momentum diff --git a/root/organs10/physics/specialists/solved-magnetic-dipole-moment-4/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-dipole-moment-4/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-dipole-moment-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-magnetic-dipole-moment/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-dipole-moment/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-dipole-moment/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-magnetic-dipole-moment/panel.json b/root/organs10/physics/specialists/solved-magnetic-dipole-moment/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..c3ed3a6cd9a0b73038da45f0f5e9102c3cc3c1be --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-dipole-moment/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic dipole moment for total angular momentum 107.961 and gyromagnetic ratio 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [107.960666, 59.4105] (the system gyromagnetic ratio (Wikidata Q634552))" + }, + { + "q": "Tell me the magnetic dipole moment with total angular momentum of 107.961 and gyromagnetic ratio of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [107.960666, 59.4105] (the system gyromagnetic ratio (Wikidata Q634552))" + }, + { + "q": "What is the magnetic dipole moment for total angular momentum 169.527 and gyromagnetic ratio 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [169.526958, 280.773702] (the system gyromagnetic ratio (Wikidata Q634552))" + }, + { + "q": "Tell me the magnetic dipole moment with total angular momentum of 169.527 and gyromagnetic ratio of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [169.526958, 280.773702] (the system gyromagnetic ratio (Wikidata Q634552))" + }, + { + "q": "What is the magnetic dipole moment for total angular momentum 1.91195 and gyromagnetic ratio 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [1.911952, 82.240072] (the system gyromagnetic ratio (Wikidata Q634552))" + }, + { + "q": "Tell me the magnetic dipole moment with total angular momentum of 1.91195 and gyromagnetic ratio of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [1.911952, 82.240072] (the system gyromagnetic ratio (Wikidata Q634552))" + }, + { + "q": "What is the magnetic dipole moment for total angular momentum 1.64379 and gyromagnetic ratio 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [1.643795, 252.709024] (the system gyromagnetic ratio (Wikidata Q634552))" + }, + { + "q": "Tell me the magnetic dipole moment with total angular momentum of 1.64379 and gyromagnetic ratio of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [1.643795, 252.709024] (the system gyromagnetic ratio (Wikidata Q634552))" + }, + { + "q": "What is the magnetic dipole moment for total angular momentum 18.3169 and gyromagnetic ratio 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [18.316867, 6.838598] (the system gyromagnetic ratio (Wikidata Q634552))" + }, + { + "q": "Tell me the magnetic dipole moment with total angular momentum of 18.3169 and gyromagnetic ratio of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [18.316867, 6.838598] (the system gyromagnetic ratio (Wikidata Q634552))" + }, + { + "q": "What is the magnetic dipole moment for total angular momentum 156.502 and gyromagnetic ratio 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [156.50181, 185.886273] (the system gyromagnetic ratio (Wikidata Q634552))" + }, + { + "q": "Tell me the magnetic dipole moment with total angular momentum of 156.502 and gyromagnetic ratio of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [156.50181, 185.886273] (the system gyromagnetic ratio (Wikidata Q634552))" + }, + { + "q": "What is the magnetic dipole moment for total angular momentum 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the gyromagnetic ratio is not given" + }, + { + "q": "Tell me the magnetic dipole moment with total angular momentum of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the gyromagnetic ratio is not given" + }, + { + "q": "What is the magnetic dipole moment 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 magnetic dipole moment 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 magnetic dipole moment with total angular momentum of -1.91195 and gyromagnetic ratio of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative total angular momentum: no example had one" + }, + { + "q": "Which magnetic dipole moment 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 angular momentum is 8.50728 and the gyromagnetic ratio is 17.7888, what is the magnetic dipole moment?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [8.507283, 17.788778] (the system gyromagnetic ratio (Wikidata Q634552))" + }, + { + "q": "Could you work out the magnetic dipole moment given total angular momentum = 8.50728, gyromagnetic ratio = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [8.507283, 17.788778] (the system gyromagnetic ratio (Wikidata Q634552))" + }, + { + "q": "I need the magnetic dipole moment for a total angular momentum of 8.24951 and a gyromagnetic ratio of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [8.24951, 0.634021] (the system gyromagnetic ratio (Wikidata Q634552))" + }, + { + "q": "If the total angular momentum is 8.24951 and the gyromagnetic ratio is 0.634021, what is the magnetic dipole moment?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [8.24951, 0.634021] (the system gyromagnetic ratio (Wikidata Q634552))" + }, + { + "q": "Could you work out the magnetic dipole moment given total angular momentum = 0.318734, gyromagnetic ratio = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [0.318734, 9.566071] (the system gyromagnetic ratio (Wikidata Q634552))" + }, + { + "q": "I need the magnetic dipole moment for a total angular momentum of 0.318734 and a gyromagnetic ratio of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [0.318734, 9.566071] (the system gyromagnetic ratio (Wikidata Q634552))" + }, + { + "q": "If the total angular momentum is 0.671697 and the gyromagnetic ratio is 14.2496, what is the magnetic dipole moment?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [0.671697, 14.249569] (the system gyromagnetic ratio (Wikidata Q634552))" + }, + { + "q": "Could you work out the magnetic dipole moment given total angular momentum = 0.671697, gyromagnetic ratio = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [0.671697, 14.249569] (the system gyromagnetic ratio (Wikidata Q634552))" + }, + { + "q": "I need the magnetic dipole moment for a total angular momentum of 21.7931 and a gyromagnetic ratio of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [21.793126, 29.510609] (the system gyromagnetic ratio (Wikidata Q634552))" + }, + { + "q": "If the total angular momentum is 21.7931 and the gyromagnetic ratio is 29.5106, what is the magnetic dipole moment?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [21.793126, 29.510609] (the system gyromagnetic ratio (Wikidata Q634552))" + }, + { + "q": "Could you work out the magnetic dipole moment given total angular momentum = 153.15, gyromagnetic ratio = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [153.149965, 18.595315] (the system gyromagnetic ratio (Wikidata Q634552))" + }, + { + "q": "I need the magnetic dipole moment for a total angular momentum of 153.15 and a gyromagnetic ratio of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "m^2*A", + "why": "magnetic dipole moment for [153.149965, 18.595315] (the system gyromagnetic ratio (Wikidata Q634552))" + }, + { + "q": "If the total angular momentum is 8.50728, what is the magnetic dipole moment?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the gyromagnetic ratio is not given" + }, + { + "q": "Could you work out the magnetic dipole moment given total angular momentum = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the gyromagnetic ratio is not given" + }, + { + "q": "Do you know the magnetic dipole moment 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 magnetic dipole moment 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 magnetic dipole moment given total angular momentum = -0.318734, gyromagnetic ratio = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative total angular momentum: no example had one" + }, + { + "q": "What is a typical magnetic dipole moment?", + "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-magnetic-dipole-moment/program.py b/root/organs10/physics/specialists/solved-magnetic-dipole-moment/program.py new file mode 100644 index 0000000000000000000000000000000000000000..0259fa68a20962cacf6d911b4ffa3df9accc70bf --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-dipole-moment/program.py @@ -0,0 +1,7 @@ +"""magnetic dipole moment from total angular momentum, gyromagnetic ratio (gyromagnetic ratio (Wikidata Q634552)) +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_angular_momentum, gyromagnetic_ratio): + return 1.0*gyromagnetic_ratio*total_angular_momentum diff --git a/root/organs10/physics/specialists/solved-magnetic-dipole-moment/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-dipole-moment/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-dipole-moment/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-magnetic-field-strength-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-field-strength-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-field-strength-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-magnetic-field-strength-2/panel.json b/root/organs10/physics/specialists/solved-magnetic-field-strength-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..d99444b41414442509eb0e46534484471c590a67 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-field-strength-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic field strength for Poynting vector 107.961 and electric field strength 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [107.960666, 59.4105] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "Tell me the magnetic field strength with Poynting vector of 107.961 and electric field strength of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [107.960666, 59.4105] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "What is the magnetic field strength for Poynting vector 169.527 and electric field strength 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [169.526958, 280.773702] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "Tell me the magnetic field strength with Poynting vector of 169.527 and electric field strength of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [169.526958, 280.773702] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "What is the magnetic field strength for Poynting vector 1.91195 and electric field strength 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [1.911952, 82.240072] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "Tell me the magnetic field strength with Poynting vector of 1.91195 and electric field strength of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [1.911952, 82.240072] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "What is the magnetic field strength for Poynting vector 1.64379 and electric field strength 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [1.643795, 252.709024] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "Tell me the magnetic field strength with Poynting vector of 1.64379 and electric field strength of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [1.643795, 252.709024] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "What is the magnetic field strength for Poynting vector 18.3169 and electric field strength 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [18.316867, 6.838598] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "Tell me the magnetic field strength with Poynting vector of 18.3169 and electric field strength of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [18.316867, 6.838598] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "What is the magnetic field strength for Poynting vector 156.502 and electric field strength 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [156.50181, 185.886273] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "Tell me the magnetic field strength with Poynting vector of 156.502 and electric field strength of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [156.50181, 185.886273] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "What is the magnetic field strength for Poynting vector 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric field strength is not given" + }, + { + "q": "Tell me the magnetic field strength with Poynting vector of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric field strength is not given" + }, + { + "q": "What is the magnetic field strength 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 magnetic field strength 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 magnetic field strength with Poynting vector of -1.91195 and electric field strength of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Poynting vector: no example had one" + }, + { + "q": "Which magnetic field strength 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 Poynting vector is 8.50728 and the electric field strength is 17.7888, what is the magnetic field strength?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [8.507283, 17.788778] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "Could you work out the magnetic field strength given Poynting vector = 8.50728, electric field strength = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [8.507283, 17.788778] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "I need the magnetic field strength for a Poynting vector of 8.24951 and an electric field strength of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [8.24951, 0.634021] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "If the Poynting vector is 8.24951 and the electric field strength is 0.634021, what is the magnetic field strength?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [8.24951, 0.634021] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "Could you work out the magnetic field strength given Poynting vector = 0.318734, electric field strength = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [0.318734, 9.566071] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "I need the magnetic field strength for a Poynting vector of 0.318734 and an electric field strength of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [0.318734, 9.566071] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "If the Poynting vector is 0.671697 and the electric field strength is 14.2496, what is the magnetic field strength?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [0.671697, 14.249569] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "Could you work out the magnetic field strength given Poynting vector = 0.671697, electric field strength = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [0.671697, 14.249569] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "I need the magnetic field strength for a Poynting vector of 21.7931 and an electric field strength of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [21.793126, 29.510609] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "If the Poynting vector is 21.7931 and the electric field strength is 29.5106, what is the magnetic field strength?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [21.793126, 29.510609] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "Could you work out the magnetic field strength given Poynting vector = 153.15, electric field strength = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [153.149965, 18.595315] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "I need the magnetic field strength for a Poynting vector of 153.15 and an electric field strength of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [153.149965, 18.595315] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "If the Poynting vector is 8.50728, what is the magnetic field strength?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric field strength is not given" + }, + { + "q": "Could you work out the magnetic field strength given Poynting vector = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric field strength is not given" + }, + { + "q": "Do you know the magnetic field strength 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 magnetic field strength 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 magnetic field strength given Poynting vector = -0.318734, electric field strength = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Poynting vector: no example had one" + }, + { + "q": "What is a typical magnetic field strength?", + "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-magnetic-field-strength-2/program.py b/root/organs10/physics/specialists/solved-magnetic-field-strength-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..c0a9a7eea7f1323ae864f0a8781d81add213f407 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-field-strength-2/program.py @@ -0,0 +1,7 @@ +"""magnetic field strength from Poynting vector, electric field strength (Poynting vector (Wikidata Q504186)) +The maths organ (systems.py): solved from the system q1 = q2 * q3; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(poynting_vector, electric_field_strength): + return 1.0*poynting_vector/electric_field_strength diff --git a/root/organs10/physics/specialists/solved-magnetic-field-strength-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-field-strength-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-field-strength-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-magnetic-field-strength-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-field-strength-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-field-strength-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-magnetic-field-strength-3/panel.json b/root/organs10/physics/specialists/solved-magnetic-field-strength-3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..0537fcaa3c7346cdf74fa9ac0da00c058c37f8da --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-field-strength-3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic field strength for permeability tensor 107.961 and magnetic flux density 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [107.960666, 59.4105] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "Tell me the magnetic field strength with permeability tensor of 107.961 and magnetic flux density of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [107.960666, 59.4105] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "What is the magnetic field strength for permeability tensor 169.527 and magnetic flux density 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [169.526958, 280.773702] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "Tell me the magnetic field strength with permeability tensor of 169.527 and magnetic flux density of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [169.526958, 280.773702] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "What is the magnetic field strength for permeability tensor 1.91195 and magnetic flux density 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [1.911952, 82.240072] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "Tell me the magnetic field strength with permeability tensor of 1.91195 and magnetic flux density of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [1.911952, 82.240072] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "What is the magnetic field strength for permeability tensor 1.64379 and magnetic flux density 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [1.643795, 252.709024] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "Tell me the magnetic field strength with permeability tensor of 1.64379 and magnetic flux density of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [1.643795, 252.709024] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "What is the magnetic field strength for permeability tensor 18.3169 and magnetic flux density 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [18.316867, 6.838598] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "Tell me the magnetic field strength with permeability tensor of 18.3169 and magnetic flux density of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [18.316867, 6.838598] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "What is the magnetic field strength for permeability tensor 156.502 and magnetic flux density 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [156.50181, 185.886273] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "Tell me the magnetic field strength with permeability tensor of 156.502 and magnetic flux density of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [156.50181, 185.886273] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "What is the magnetic field strength for permeability tensor 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic flux density is not given" + }, + { + "q": "Tell me the magnetic field strength with permeability tensor of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic flux density is not given" + }, + { + "q": "What is the magnetic field strength 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 magnetic field strength 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 magnetic field strength with permeability tensor of -1.91195 and magnetic flux density of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative permeability tensor: no example had one" + }, + { + "q": "Which magnetic field strength 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 tensor is 8.50728 and the magnetic flux density is 17.7888, what is the magnetic field strength?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [8.507283, 17.788778] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "Could you work out the magnetic field strength given permeability tensor = 8.50728, magnetic flux density = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [8.507283, 17.788778] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "I need the magnetic field strength for a permeability tensor of 8.24951 and a magnetic flux density of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [8.24951, 0.634021] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "If the permeability tensor is 8.24951 and the magnetic flux density is 0.634021, what is the magnetic field strength?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [8.24951, 0.634021] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "Could you work out the magnetic field strength given permeability tensor = 0.318734, magnetic flux density = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [0.318734, 9.566071] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "I need the magnetic field strength for a permeability tensor of 0.318734 and a magnetic flux density of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [0.318734, 9.566071] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "If the permeability tensor is 0.671697 and the magnetic flux density is 14.2496, what is the magnetic field strength?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [0.671697, 14.249569] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "Could you work out the magnetic field strength given permeability tensor = 0.671697, magnetic flux density = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [0.671697, 14.249569] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "I need the magnetic field strength for a permeability tensor of 21.7931 and a magnetic flux density of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [21.793126, 29.510609] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "If the permeability tensor is 21.7931 and the magnetic flux density is 29.5106, what is the magnetic field strength?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [21.793126, 29.510609] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "Could you work out the magnetic field strength given permeability tensor = 153.15, magnetic flux density = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [153.149965, 18.595315] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "I need the magnetic field strength for a permeability tensor of 153.15 and a magnetic flux density of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [153.149965, 18.595315] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "If the permeability tensor is 8.50728, what is the magnetic field strength?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic flux density is not given" + }, + { + "q": "Could you work out the magnetic field strength given permeability tensor = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic flux density is not given" + }, + { + "q": "Do you know the magnetic field strength 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 magnetic field strength 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 magnetic field strength given permeability tensor = -0.318734, magnetic flux density = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative permeability tensor: no example had one" + }, + { + "q": "What is a typical magnetic field strength?", + "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-magnetic-field-strength-3/program.py b/root/organs10/physics/specialists/solved-magnetic-field-strength-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..361eb6bba6b06a8f252d05b5eba42ca77bc3fc80 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-field-strength-3/program.py @@ -0,0 +1,7 @@ +"""magnetic field strength from permeability tensor, magnetic flux density (permeability tensor (Wikidata Q118895841)) +The maths organ (systems.py): solved from the system q2 = q1 * q3; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(permeability_tensor, magnetic_flux_density): + return 1.0*magnetic_flux_density/permeability_tensor diff --git a/root/organs10/physics/specialists/solved-magnetic-field-strength-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-field-strength-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-field-strength-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-magnetic-field-strength-4/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-field-strength-4/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-field-strength-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-magnetic-field-strength-4/panel.json b/root/organs10/physics/specialists/solved-magnetic-field-strength-4/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..a33d9fadc4ad8df442c47b92934088ae04a76be1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-field-strength-4/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic field strength for magnetic flux density 107.961 and magnetic permeability 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [107.960666, 59.4105] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Tell me the magnetic field strength with magnetic flux density of 107.961 and magnetic permeability of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [107.960666, 59.4105] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "What is the magnetic field strength for magnetic flux density 169.527 and magnetic permeability 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [169.526958, 280.773702] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Tell me the magnetic field strength with magnetic flux density of 169.527 and magnetic permeability of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [169.526958, 280.773702] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "What is the magnetic field strength for magnetic flux density 1.91195 and magnetic permeability 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [1.911952, 82.240072] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Tell me the magnetic field strength with magnetic flux density of 1.91195 and magnetic permeability of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [1.911952, 82.240072] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "What is the magnetic field strength for magnetic flux density 1.64379 and magnetic permeability 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [1.643795, 252.709024] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Tell me the magnetic field strength with magnetic flux density of 1.64379 and magnetic permeability of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [1.643795, 252.709024] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "What is the magnetic field strength for magnetic flux density 18.3169 and magnetic permeability 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [18.316867, 6.838598] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Tell me the magnetic field strength with magnetic flux density of 18.3169 and magnetic permeability of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [18.316867, 6.838598] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "What is the magnetic field strength for magnetic flux density 156.502 and magnetic permeability 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [156.50181, 185.886273] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Tell me the magnetic field strength with magnetic flux density of 156.502 and magnetic permeability of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [156.50181, 185.886273] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "What is the magnetic field strength for magnetic flux density 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "Tell me the magnetic field strength with magnetic flux density of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "What is the magnetic field strength 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 magnetic field strength 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 magnetic field strength with magnetic flux density of -1.91195 and magnetic permeability of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative magnetic flux density: no example had one" + }, + { + "q": "Which magnetic field strength 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 magnetic flux density is 8.50728 and the magnetic permeability is 17.7888, what is the magnetic field strength?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [8.507283, 17.788778] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Could you work out the magnetic field strength given magnetic flux density = 8.50728, magnetic permeability = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [8.507283, 17.788778] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "I need the magnetic field strength for a magnetic flux density of 8.24951 and a magnetic permeability of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [8.24951, 0.634021] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "If the magnetic flux density is 8.24951 and the magnetic permeability is 0.634021, what is the magnetic field strength?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [8.24951, 0.634021] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Could you work out the magnetic field strength given magnetic flux density = 0.318734, magnetic permeability = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [0.318734, 9.566071] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "I need the magnetic field strength for a magnetic flux density of 0.318734 and a magnetic permeability of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [0.318734, 9.566071] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "If the magnetic flux density is 0.671697 and the magnetic permeability is 14.2496, what is the magnetic field strength?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [0.671697, 14.249569] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Could you work out the magnetic field strength given magnetic flux density = 0.671697, magnetic permeability = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [0.671697, 14.249569] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "I need the magnetic field strength for a magnetic flux density of 21.7931 and a magnetic permeability of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [21.793126, 29.510609] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "If the magnetic flux density is 21.7931 and the magnetic permeability is 29.5106, what is the magnetic field strength?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [21.793126, 29.510609] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Could you work out the magnetic field strength given magnetic flux density = 153.15, magnetic permeability = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [153.149965, 18.595315] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "I need the magnetic field strength for a magnetic flux density of 153.15 and a magnetic permeability of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [153.149965, 18.595315] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "If the magnetic flux density is 8.50728, what is the magnetic field strength?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "Could you work out the magnetic field strength given magnetic flux density = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "Do you know the magnetic field strength 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 magnetic field strength 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 magnetic field strength given magnetic flux density = -0.318734, magnetic permeability = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative magnetic flux density: no example had one" + }, + { + "q": "What is a typical magnetic field strength?", + "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-magnetic-field-strength-4/program.py b/root/organs10/physics/specialists/solved-magnetic-field-strength-4/program.py new file mode 100644 index 0000000000000000000000000000000000000000..29b72469434dce4f83b63fd613f53548d58f6064 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-field-strength-4/program.py @@ -0,0 +1,7 @@ +"""magnetic field strength from magnetic flux density, magnetic permeability (magnetic permeability (Wikidata Q28352)) +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(magnetic_flux_density, magnetic_permeability): + return 1.0*magnetic_flux_density/magnetic_permeability diff --git a/root/organs10/physics/specialists/solved-magnetic-field-strength-4/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-field-strength-4/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-field-strength-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-magnetic-field-strength/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-field-strength/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-field-strength/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-magnetic-field-strength/panel.json b/root/organs10/physics/specialists/solved-magnetic-field-strength/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..652ceca5ae98fedd2799ae3a8a4eea8466d0f787 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-field-strength/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic field strength for electric surface current 107.961, Ampère number 59.4105 and characteristic length 5.77717?", + "a": "0.31454848897160476", + "valid": true, + "kind": "compute", + "expect": 0.31454848897160476, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [107.960666, 59.4105, 5.777165] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Tell me the magnetic field strength with electric surface current of 107.961, Ampère number of 59.4105 and characteristic length of 5.77717.", + "a": "0.31454848897160476", + "valid": true, + "kind": "compute", + "expect": 0.31454848897160476, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [107.960666, 59.4105, 5.777165] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "What is the magnetic field strength for electric surface current 85.2416, Ampère number 160.878 and characteristic length 2.69428?", + "a": "0.19665778745152376", + "valid": true, + "kind": "compute", + "expect": 0.19665778745152376, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [85.241623, 160.878442, 2.69428] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Tell me the magnetic field strength with electric surface current of 85.2416, Ampère number of 160.878 and characteristic length of 2.69428.", + "a": "0.19665778745152376", + "valid": true, + "kind": "compute", + "expect": 0.19665778745152376, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [85.241623, 160.878442, 2.69428] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "What is the magnetic field strength for electric surface current 45.6157, Ampère number 4.97131 and characteristic length 94.305?", + "a": "0.09729902784967777", + "valid": true, + "kind": "compute", + "expect": 0.09729902784967777, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [45.61567, 4.971307, 94.305046] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Tell me the magnetic field strength with electric surface current of 45.6157, Ampère number of 4.97131 and characteristic length of 94.305.", + "a": "0.09729902784967777", + "valid": true, + "kind": "compute", + "expect": 0.09729902784967777, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [45.61567, 4.971307, 94.305046] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "What is the magnetic field strength for electric surface current 10.5638, Ampère number 198.839 and characteristic length 0.671697?", + "a": "0.07909461512056756", + "valid": true, + "kind": "compute", + "expect": 0.07909461512056756, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [10.563843, 198.838955, 0.671697] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Tell me the magnetic field strength with electric surface current of 10.5638, Ampère number of 198.839 and characteristic length of 0.671697.", + "a": "0.07909461512056756", + "valid": true, + "kind": "compute", + "expect": 0.07909461512056756, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [10.563843, 198.838955, 0.671697] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "What is the magnetic field strength for electric surface current 21.7931, Ampère number 29.5106 and characteristic length 8.50341?", + "a": "0.08684571530973768", + "valid": true, + "kind": "compute", + "expect": 0.08684571530973768, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [21.793126, 29.510609, 8.503407] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Tell me the magnetic field strength with electric surface current of 21.7931, Ampère number of 29.5106 and characteristic length of 8.50341.", + "a": "0.08684571530973768", + "valid": true, + "kind": "compute", + "expect": 0.08684571530973768, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [21.793126, 29.510609, 8.503407] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "What is the magnetic field strength for electric surface current 223.509, Ampère number 17.3414 and characteristic length 7.10705?", + "a": "1.813511306470856", + "valid": true, + "kind": "compute", + "expect": 1.813511306470856, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [223.508796, 17.341435, 7.107049] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Tell me the magnetic field strength with electric surface current of 223.509, Ampère number of 17.3414 and characteristic length of 7.10705.", + "a": "1.813511306470856", + "valid": true, + "kind": "compute", + "expect": 1.813511306470856, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [223.508796, 17.341435, 7.107049] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "What is the magnetic field strength for electric surface current 107.961 and Ampère number 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the characteristic length is not given" + }, + { + "q": "Tell me the magnetic field strength with electric surface current of 85.2416 and Ampère number of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the characteristic length is not given" + }, + { + "q": "What is the magnetic field strength 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 magnetic field strength 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 magnetic field strength with electric surface current of -45.6157, Ampère number of 4.97131 and characteristic length of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric surface current: no example had one" + }, + { + "q": "Which magnetic field strength 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 electric surface current is 17.7888, Ampère number is 167.625 and the characteristic length is 10.3291, what is the magnetic field strength?", + "a": "0.010274173498684694", + "valid": true, + "kind": "compute", + "expect": 0.010274173498684694, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [17.788778, 167.624948, 10.329054] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Could you work out the magnetic field strength given electric surface current = 17.7888, Ampère number = 167.625, characteristic length = 10.3291?", + "a": "0.010274173498684694", + "valid": true, + "kind": "compute", + "expect": 0.010274173498684694, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [17.788778, 167.624948, 10.329054] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "I need the magnetic field strength for an electric surface current of 21.511, an Ampère number of 173.394 and a characteristic length of 251.084, please.", + "a": "0.000494092950151496", + "valid": true, + "kind": "compute", + "expect": 0.000494092950151496, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [21.511016, 173.393638, 251.084037] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "If the electric surface current is 21.511, Ampère number is 173.394 and the characteristic length is 251.084, what is the magnetic field strength?", + "a": "0.000494092950151496", + "valid": true, + "kind": "compute", + "expect": 0.000494092950151496, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [21.511016, 173.393638, 251.084037] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Could you work out the magnetic field strength given electric surface current = 129.245, Ampère number = 1.18359, characteristic length = 15.9416?", + "a": "6.849836414751194", + "valid": true, + "kind": "compute", + "expect": 6.849836414751194, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [129.244984, 1.18359, 15.941612] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "I need the magnetic field strength for an electric surface current of 129.245, an Ampère number of 1.18359 and a characteristic length of 15.9416, please.", + "a": "6.849836414751194", + "valid": true, + "kind": "compute", + "expect": 6.849836414751194, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [129.244984, 1.18359, 15.941612] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "If the electric surface current is 20.3966, Ampère number is 18.3169 and the characteristic length is 6.8386, what is the magnetic field strength?", + "a": "0.16283157083473473", + "valid": true, + "kind": "compute", + "expect": 0.16283157083473473, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [20.396558, 18.316867, 6.838598] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Could you work out the magnetic field strength given electric surface current = 20.3966, Ampère number = 18.3169, characteristic length = 6.8386?", + "a": "0.16283157083473473", + "valid": true, + "kind": "compute", + "expect": 0.16283157083473473, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [20.396558, 18.316867, 6.838598] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "I need the magnetic field strength for an electric surface current of 185.886, an Ampère number of 13.2373 and a characteristic length of 4.71941, please.", + "a": "2.975496158535072", + "valid": true, + "kind": "compute", + "expect": 2.975496158535072, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [185.886273, 13.237311, 4.719415] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "If the electric surface current is 185.886, Ampère number is 13.2373 and the characteristic length is 4.71941, what is the magnetic field strength?", + "a": "2.975496158535072", + "valid": true, + "kind": "compute", + "expect": 2.975496158535072, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [185.886273, 13.237311, 4.719415] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Could you work out the magnetic field strength given electric surface current = 9.1061, Ampère number = 24.5721, characteristic length = 108.451?", + "a": "0.003417101331511669", + "valid": true, + "kind": "compute", + "expect": 0.003417101331511669, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [9.106104, 24.572086, 108.450792] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "I need the magnetic field strength for an electric surface current of 9.1061, an Ampère number of 24.5721 and a characteristic length of 108.451, please.", + "a": "0.003417101331511669", + "valid": true, + "kind": "compute", + "expect": 0.003417101331511669, + "dtype": "quantity", + "unit": "A/m", + "why": "magnetic field strength for [9.106104, 24.572086, 108.450792] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "If the electric surface current is 17.7888 and the Ampère number is 167.625, what is the magnetic field strength?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the characteristic length is not given" + }, + { + "q": "Could you work out the magnetic field strength given electric surface current = 21.511, Ampère number = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the characteristic length is not given" + }, + { + "q": "Do you know the magnetic field strength 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 magnetic field strength 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 magnetic field strength given electric surface current = -129.245, Ampère number = 1.18359, characteristic length = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric surface current: no example had one" + }, + { + "q": "What is a typical magnetic field strength?", + "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-magnetic-field-strength/program.py b/root/organs10/physics/specialists/solved-magnetic-field-strength/program.py new file mode 100644 index 0000000000000000000000000000000000000000..aa8cc73828702a986eb187522439b396ce1237aa --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-field-strength/program.py @@ -0,0 +1,7 @@ +"""magnetic field strength from electric surface current, Ampère number, characteristic length (Ampère number (Wikidata Q105404651)) +The maths organ (systems.py): solved from the system q1 = ((q0)/(q2*q3)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(electric_surface_current, amp_re_number, characteristic_length): + return 1.0*electric_surface_current/(amp_re_number*characteristic_length) diff --git a/root/organs10/physics/specialists/solved-magnetic-field-strength/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-field-strength/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-field-strength/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-magnetic-flux-density-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-flux-density-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-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-magnetic-flux-density-2/panel.json b/root/organs10/physics/specialists/solved-magnetic-flux-density-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..75571be654c58b8e0d9041528d7f1ebc08d4e6ba --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic flux density for magnetic pressure number 107.961, pressure 59.4105 and magnetic permeability 5.77717?", + "a": "2.5215709714484005", + "valid": true, + "kind": "compute", + "expect": 2.5215709714484005, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [107.960666, 59.4105, 5.777165] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Tell me the magnetic flux density with magnetic pressure number of 107.961, pressure of 59.4105 and magnetic permeability of 5.77717.", + "a": "2.5215709714484005", + "valid": true, + "kind": "compute", + "expect": 2.5215709714484005, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [107.960666, 59.4105, 5.777165] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "What is the magnetic flux density for magnetic pressure number 85.2416, pressure 160.878 and magnetic permeability 2.69428?", + "a": "3.1890360117074077", + "valid": true, + "kind": "compute", + "expect": 3.1890360117074077, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [85.241623, 160.878442, 2.69428] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Tell me the magnetic flux density with magnetic pressure number of 85.2416, pressure of 160.878 and magnetic permeability of 2.69428.", + "a": "3.1890360117074077", + "valid": true, + "kind": "compute", + "expect": 3.1890360117074077, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [85.241623, 160.878442, 2.69428] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "What is the magnetic flux density for magnetic pressure number 45.6157, pressure 4.97131 and magnetic permeability 94.305?", + "a": "4.533783183629512", + "valid": true, + "kind": "compute", + "expect": 4.533783183629512, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [45.61567, 4.971307, 94.305046] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Tell me the magnetic flux density with magnetic pressure number of 45.6157, pressure of 4.97131 and magnetic permeability of 94.305.", + "a": "4.533783183629512", + "valid": true, + "kind": "compute", + "expect": 4.533783183629512, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [45.61567, 4.971307, 94.305046] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "What is the magnetic flux density for magnetic pressure number 10.5638, pressure 198.839 and magnetic permeability 0.671697?", + "a": "5.028535717910741", + "valid": true, + "kind": "compute", + "expect": 5.028535717910741, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [10.563843, 198.838955, 0.671697] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Tell me the magnetic flux density with magnetic pressure number of 10.5638, pressure of 198.839 and magnetic permeability of 0.671697.", + "a": "5.028535717910741", + "valid": true, + "kind": "compute", + "expect": 5.028535717910741, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [10.563843, 198.838955, 0.671697] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "What is the magnetic flux density for magnetic pressure number 21.7931, pressure 29.5106 and magnetic permeability 8.50341?", + "a": "4.798890048232899", + "valid": true, + "kind": "compute", + "expect": 4.798890048232899, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [21.793126, 29.510609, 8.503407] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Tell me the magnetic flux density with magnetic pressure number of 21.7931, pressure of 29.5106 and magnetic permeability of 8.50341.", + "a": "4.798890048232899", + "valid": true, + "kind": "compute", + "expect": 4.798890048232899, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [21.793126, 29.510609, 8.503407] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "What is the magnetic flux density for magnetic pressure number 223.509, pressure 17.3414 and magnetic permeability 7.10705?", + "a": "1.050158529356667", + "valid": true, + "kind": "compute", + "expect": 1.050158529356667, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [223.508796, 17.341435, 7.107049] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Tell me the magnetic flux density with magnetic pressure number of 223.509, pressure of 17.3414 and magnetic permeability of 7.10705.", + "a": "1.050158529356667", + "valid": true, + "kind": "compute", + "expect": 1.050158529356667, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [223.508796, 17.341435, 7.107049] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "What is the magnetic flux density for magnetic pressure number 107.961 and pressure 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "Tell me the magnetic flux density with magnetic pressure number of 85.2416 and pressure of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "What is the magnetic flux 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 magnetic flux 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 magnetic flux density with magnetic pressure number of -45.6157, pressure of 4.97131 and magnetic permeability of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative magnetic pressure number: no example had one" + }, + { + "q": "Which magnetic flux 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 magnetic pressure number is 17.7888, pressure is 167.625 and the magnetic permeability is 10.3291, what is the magnetic flux density?", + "a": "13.952163282230416", + "valid": true, + "kind": "compute", + "expect": 13.952163282230416, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [17.788778, 167.624948, 10.329054] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Could you work out the magnetic flux density given magnetic pressure number = 17.7888, pressure = 167.625, magnetic permeability = 10.3291?", + "a": "13.952163282230416", + "valid": true, + "kind": "compute", + "expect": 13.952163282230416, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [17.788778, 167.624948, 10.329054] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "I need the magnetic flux density for a magnetic pressure number of 21.511, a pressure of 173.394 and a magnetic permeability of 251.084, please.", + "a": "63.62249103236781", + "valid": true, + "kind": "compute", + "expect": 63.62249103236781, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [21.511016, 173.393638, 251.084037] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "If the magnetic pressure number is 21.511, pressure is 173.394 and the magnetic permeability is 251.084, what is the magnetic flux density?", + "a": "63.62249103236781", + "valid": true, + "kind": "compute", + "expect": 63.62249103236781, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [21.511016, 173.393638, 251.084037] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Could you work out the magnetic flux density given magnetic pressure number = 129.245, pressure = 1.18359, magnetic permeability = 15.9416?", + "a": "0.5403496790578926", + "valid": true, + "kind": "compute", + "expect": 0.5403496790578926, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [129.244984, 1.18359, 15.941612] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "I need the magnetic flux density for a magnetic pressure number of 129.245, a pressure of 1.18359 and a magnetic permeability of 15.9416, please.", + "a": "0.5403496790578926", + "valid": true, + "kind": "compute", + "expect": 0.5403496790578926, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [129.244984, 1.18359, 15.941612] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "If the magnetic pressure number is 20.3966, pressure is 18.3169 and the magnetic permeability is 6.8386, what is the magnetic flux density?", + "a": "3.5046583838697503", + "valid": true, + "kind": "compute", + "expect": 3.5046583838697503, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [20.396558, 18.316867, 6.838598] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Could you work out the magnetic flux density given magnetic pressure number = 20.3966, pressure = 18.3169, magnetic permeability = 6.8386?", + "a": "3.5046583838697503", + "valid": true, + "kind": "compute", + "expect": 3.5046583838697503, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [20.396558, 18.316867, 6.838598] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "I need the magnetic flux density for a magnetic pressure number of 185.886, a pressure of 13.2373 and a magnetic permeability of 4.71941, please.", + "a": "0.8198516987564833", + "valid": true, + "kind": "compute", + "expect": 0.8198516987564833, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [185.886273, 13.237311, 4.719415] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "If the magnetic pressure number is 185.886, pressure is 13.2373 and the magnetic permeability is 4.71941, what is the magnetic flux density?", + "a": "0.8198516987564833", + "valid": true, + "kind": "compute", + "expect": 0.8198516987564833, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [185.886273, 13.237311, 4.719415] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Could you work out the magnetic flux density given magnetic pressure number = 9.1061, pressure = 24.5721, magnetic permeability = 108.451?", + "a": "24.19279631388288", + "valid": true, + "kind": "compute", + "expect": 24.19279631388288, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [9.106104, 24.572086, 108.450792] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "I need the magnetic flux density for a magnetic pressure number of 9.1061, a pressure of 24.5721 and a magnetic permeability of 108.451, please.", + "a": "24.19279631388288", + "valid": true, + "kind": "compute", + "expect": 24.19279631388288, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [9.106104, 24.572086, 108.450792] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "If the magnetic pressure number is 17.7888 and the pressure is 167.625, what is the magnetic flux density?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "Could you work out the magnetic flux density given magnetic pressure number = 21.511, pressure = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "Do you know the magnetic flux 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 magnetic flux 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 magnetic flux density given magnetic pressure number = -129.245, pressure = 1.18359, magnetic permeability = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative magnetic pressure number: no example had one" + }, + { + "q": "What is a typical magnetic flux 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-magnetic-flux-density-2/program.py b/root/organs10/physics/specialists/solved-magnetic-flux-density-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..e08385622efce61dc3b7e969dde03fc5e146a337 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density-2/program.py @@ -0,0 +1,7 @@ +"""magnetic flux density from magnetic pressure number, pressure, magnetic permeability (magnetic pressure number (Wikidata Q105102313)) +The maths organ (systems.py): solved from the system q0 = ((2*q1*q2)/(q3 **(2))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(magnetic_pressure_number, pressure, magnetic_permeability): + return 1.4142135623731*magnetic_permeability**0.5*magnetic_pressure_number**(-0.5)*pressure**0.5 diff --git a/root/organs10/physics/specialists/solved-magnetic-flux-density-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-flux-density-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-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-magnetic-flux-density-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-flux-density-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-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-magnetic-flux-density-3/panel.json b/root/organs10/physics/specialists/solved-magnetic-flux-density-3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..6248cf164c5a0a38229d1c77906720de6bb93ef3 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density-3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic flux density for electric field parameter 107.961, electric field strength 59.4105 and speed 5.77717?", + "a": "0.09525393194179609", + "valid": true, + "kind": "compute", + "expect": 0.09525393194179609, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [107.960666, 59.4105, 5.777165] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Tell me the magnetic flux density with electric field parameter of 107.961, electric field strength of 59.4105 and speed of 5.77717.", + "a": "0.09525393194179609", + "valid": true, + "kind": "compute", + "expect": 0.09525393194179609, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [107.960666, 59.4105, 5.777165] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "What is the magnetic flux density for electric field parameter 85.2416, electric field strength 160.878 and speed 2.69428?", + "a": "0.7004924122686708", + "valid": true, + "kind": "compute", + "expect": 0.7004924122686708, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [85.241623, 160.878442, 2.69428] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Tell me the magnetic flux density with electric field parameter of 85.2416, electric field strength of 160.878 and speed of 2.69428.", + "a": "0.7004924122686708", + "valid": true, + "kind": "compute", + "expect": 0.7004924122686708, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [85.241623, 160.878442, 2.69428] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "What is the magnetic flux density for electric field parameter 45.6157, electric field strength 4.97131 and speed 94.305?", + "a": "0.001155637526406431", + "valid": true, + "kind": "compute", + "expect": 0.001155637526406431, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [45.61567, 4.971307, 94.305046] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Tell me the magnetic flux density with electric field parameter of 45.6157, electric field strength of 4.97131 and speed of 94.305.", + "a": "0.001155637526406431", + "valid": true, + "kind": "compute", + "expect": 0.001155637526406431, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [45.61567, 4.971307, 94.305046] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "What is the magnetic flux density for electric field parameter 10.5638, electric field strength 198.839 and speed 0.671697?", + "a": "28.022443831038363", + "valid": true, + "kind": "compute", + "expect": 28.022443831038363, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [10.563843, 198.838955, 0.671697] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Tell me the magnetic flux density with electric field parameter of 10.5638, electric field strength of 198.839 and speed of 0.671697.", + "a": "28.022443831038363", + "valid": true, + "kind": "compute", + "expect": 28.022443831038363, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [10.563843, 198.838955, 0.671697] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "What is the magnetic flux density for electric field parameter 21.7931, electric field strength 29.5106 and speed 8.50341?", + "a": "0.15924494359111602", + "valid": true, + "kind": "compute", + "expect": 0.15924494359111602, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [21.793126, 29.510609, 8.503407] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Tell me the magnetic flux density with electric field parameter of 21.7931, electric field strength of 29.5106 and speed of 8.50341.", + "a": "0.15924494359111602", + "valid": true, + "kind": "compute", + "expect": 0.15924494359111602, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [21.793126, 29.510609, 8.503407] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "What is the magnetic flux density for electric field parameter 223.509, electric field strength 17.3414 and speed 7.10705?", + "a": "0.010916944254137492", + "valid": true, + "kind": "compute", + "expect": 0.010916944254137492, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [223.508796, 17.341435, 7.107049] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Tell me the magnetic flux density with electric field parameter of 223.509, electric field strength of 17.3414 and speed of 7.10705.", + "a": "0.010916944254137492", + "valid": true, + "kind": "compute", + "expect": 0.010916944254137492, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [223.508796, 17.341435, 7.107049] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "What is the magnetic flux density for electric field parameter 107.961 and electric field strength 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Tell me the magnetic flux density with electric field parameter of 85.2416 and electric field strength of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "What is the magnetic flux 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 magnetic flux 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 magnetic flux density with electric field parameter of -45.6157, electric field strength of 4.97131 and speed of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric field parameter: no example had one" + }, + { + "q": "Which magnetic flux 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 electric field parameter is 17.7888, electric field strength is 167.625 and the speed is 10.3291, what is the magnetic flux density?", + "a": "0.9122880156800318", + "valid": true, + "kind": "compute", + "expect": 0.9122880156800318, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [17.788778, 167.624948, 10.329054] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Could you work out the magnetic flux density given electric field parameter = 17.7888, electric field strength = 167.625, speed = 10.3291?", + "a": "0.9122880156800318", + "valid": true, + "kind": "compute", + "expect": 0.9122880156800318, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [17.788778, 167.624948, 10.329054] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "I need the magnetic flux density for an electric field parameter of 21.511, an electric field strength of 173.394 and a speed of 251.084, please.", + "a": "0.032103555682225955", + "valid": true, + "kind": "compute", + "expect": 0.032103555682225955, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [21.511016, 173.393638, 251.084037] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "If the electric field parameter is 21.511, electric field strength is 173.394 and the speed is 251.084, what is the magnetic flux density?", + "a": "0.032103555682225955", + "valid": true, + "kind": "compute", + "expect": 0.032103555682225955, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [21.511016, 173.393638, 251.084037] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Could you work out the magnetic flux density given electric field parameter = 129.245, electric field strength = 1.18359, speed = 15.9416?", + "a": "0.0005744541302673633", + "valid": true, + "kind": "compute", + "expect": 0.0005744541302673633, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [129.244984, 1.18359, 15.941612] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "I need the magnetic flux density for an electric field parameter of 129.245, an electric field strength of 1.18359 and a speed of 15.9416, please.", + "a": "0.0005744541302673633", + "valid": true, + "kind": "compute", + "expect": 0.0005744541302673633, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [129.244984, 1.18359, 15.941612] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "If the electric field parameter is 20.3966, electric field strength is 18.3169 and the speed is 6.8386, what is the magnetic flux density?", + "a": "0.13131889807531302", + "valid": true, + "kind": "compute", + "expect": 0.13131889807531302, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [20.396558, 18.316867, 6.838598] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Could you work out the magnetic flux density given electric field parameter = 20.3966, electric field strength = 18.3169, speed = 6.8386?", + "a": "0.13131889807531302", + "valid": true, + "kind": "compute", + "expect": 0.13131889807531302, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [20.396558, 18.316867, 6.838598] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "I need the magnetic flux density for an electric field parameter of 185.886, an electric field strength of 13.2373 and a speed of 4.71941, please.", + "a": "0.015089133629806127", + "valid": true, + "kind": "compute", + "expect": 0.015089133629806127, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [185.886273, 13.237311, 4.719415] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "If the electric field parameter is 185.886, electric field strength is 13.2373 and the speed is 4.71941, what is the magnetic flux density?", + "a": "0.015089133629806127", + "valid": true, + "kind": "compute", + "expect": 0.015089133629806127, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [185.886273, 13.237311, 4.719415] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Could you work out the magnetic flux density given electric field parameter = 9.1061, electric field strength = 24.5721, speed = 108.451?", + "a": "0.024881507432683664", + "valid": true, + "kind": "compute", + "expect": 0.024881507432683664, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [9.106104, 24.572086, 108.450792] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "I need the magnetic flux density for an electric field parameter of 9.1061, an electric field strength of 24.5721 and a speed of 108.451, please.", + "a": "0.024881507432683664", + "valid": true, + "kind": "compute", + "expect": 0.024881507432683664, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [9.106104, 24.572086, 108.450792] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "If the electric field parameter is 17.7888 and the electric field strength is 167.625, what is the magnetic flux density?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Could you work out the magnetic flux density given electric field parameter = 21.511, electric field strength = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Do you know the magnetic flux 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 magnetic flux 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 magnetic flux density given electric field parameter = -129.245, electric field strength = 1.18359, speed = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric field parameter: no example had one" + }, + { + "q": "What is a typical magnetic flux 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-magnetic-flux-density-3/program.py b/root/organs10/physics/specialists/solved-magnetic-flux-density-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..a0452783d8ddfb542e768fdec1841d634f82c732 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density-3/program.py @@ -0,0 +1,7 @@ +"""magnetic flux density from electric field parameter, electric field strength, speed (electric field parameter (Wikidata Q105221927)) +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(electric_field_parameter, electric_field_strength, speed): + return 1.0*electric_field_strength/(electric_field_parameter*speed) diff --git a/root/organs10/physics/specialists/solved-magnetic-flux-density-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-flux-density-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-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-magnetic-flux-density-4/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-flux-density-4/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density-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-magnetic-flux-density-4/panel.json b/root/organs10/physics/specialists/solved-magnetic-flux-density-4/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..2e8a5c4872faf9acdae9161036e0ddf31c503c2c --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density-4/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic flux density for Hartmann number 107.961, characteristic length 59.4105, electrical conductivity 5.77717 and dynamic viscosity 1.89126?", + "a": "1.039728862378095", + "valid": true, + "kind": "compute", + "expect": 1.039728862378095, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [107.960666, 59.4105, 5.777165, 1.891256] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "Tell me the magnetic flux density with Hartmann number of 107.961, characteristic length of 59.4105, electrical conductivity of 5.77717 and dynamic viscosity of 1.89126.", + "a": "1.039728862378095", + "valid": true, + "kind": "compute", + "expect": 1.039728862378095, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [107.960666, 59.4105, 5.777165, 1.891256] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "What is the magnetic flux density for Hartmann number 2.69428, characteristic length 48.921, electrical conductivity 157.223 and dynamic viscosity 35.6412?", + "a": "0.026222007006629376", + "valid": true, + "kind": "compute", + "expect": 0.026222007006629376, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [2.69428, 48.920991, 157.22256, 35.641157] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "Tell me the magnetic flux density with Hartmann number of 2.69428, characteristic length of 48.921, electrical conductivity of 157.223 and dynamic viscosity of 35.6412.", + "a": "0.026222007006629376", + "valid": true, + "kind": "compute", + "expect": 0.026222007006629376, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [2.69428, 48.920991, 157.22256, 35.641157] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "What is the magnetic flux density for Hartmann number 0.318734, characteristic length 9.56607, electrical conductivity 126.709 and dynamic viscosity 1.70503?", + "a": "0.003865066856979319", + "valid": true, + "kind": "compute", + "expect": 0.003865066856979319, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [0.318734, 9.566071, 126.709252, 1.705032] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "Tell me the magnetic flux density with Hartmann number of 0.318734, characteristic length of 9.56607, electrical conductivity of 126.709 and dynamic viscosity of 1.70503.", + "a": "0.003865066856979319", + "valid": true, + "kind": "compute", + "expect": 0.003865066856979319, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [0.318734, 9.566071, 126.709252, 1.705032] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "What is the magnetic flux density for Hartmann number 87.781, characteristic length 13.2084, electrical conductivity 246.329 and dynamic viscosity 20.3966?", + "a": "1.9123632619055841", + "valid": true, + "kind": "compute", + "expect": 1.9123632619055841, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [87.781013, 13.208419, 246.329053, 20.396558] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "Tell me the magnetic flux density with Hartmann number of 87.781, characteristic length of 13.2084, electrical conductivity of 246.329 and dynamic viscosity of 20.3966.", + "a": "1.9123632619055841", + "valid": true, + "kind": "compute", + "expect": 1.9123632619055841, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [87.781013, 13.208419, 246.329053, 20.396558] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "What is the magnetic flux density for Hartmann number 156.502, characteristic length 185.886, electrical conductivity 13.2373 and dynamic viscosity 4.71941?", + "a": "0.5027085046589135", + "valid": true, + "kind": "compute", + "expect": 0.5027085046589135, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [156.50181, 185.886273, 13.237311, 4.719415] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "Tell me the magnetic flux density with Hartmann number of 156.502, characteristic length of 185.886, electrical conductivity of 13.2373 and dynamic viscosity of 4.71941.", + "a": "0.5027085046589135", + "valid": true, + "kind": "compute", + "expect": 0.5027085046589135, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [156.50181, 185.886273, 13.237311, 4.719415] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "What is the magnetic flux density for Hartmann number 24.5721, characteristic length 108.451, electrical conductivity 1.69476 and dynamic viscosity 49.4843?", + "a": "1.2243051950230301", + "valid": true, + "kind": "compute", + "expect": 1.2243051950230301, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [24.572086, 108.450792, 1.694755, 49.484326] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "Tell me the magnetic flux density with Hartmann number of 24.5721, characteristic length of 108.451, electrical conductivity of 1.69476 and dynamic viscosity of 49.4843.", + "a": "1.2243051950230301", + "valid": true, + "kind": "compute", + "expect": 1.2243051950230301, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [24.572086, 108.450792, 1.694755, 49.484326] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "What is the magnetic flux density for Hartmann number 107.961, characteristic length 59.4105 and electrical conductivity 5.77717?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the dynamic viscosity is not given" + }, + { + "q": "Tell me the magnetic flux density with Hartmann number of 2.69428, characteristic length of 48.921 and electrical conductivity of 157.223.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the dynamic viscosity is not given" + }, + { + "q": "What is the magnetic flux 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 magnetic flux 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 magnetic flux density with Hartmann number of -0.318734, characteristic length of 9.56607, electrical conductivity of 126.709 and dynamic viscosity of 1.70503.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Hartmann number: no example had one" + }, + { + "q": "Which magnetic flux 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 Hartmann number is 167.625, characteristic length is 10.3291, electrical conductivity is 2.21571 and the dynamic viscosity is 58.5346, what is the magnetic flux density?", + "a": "83.41182583971526", + "valid": true, + "kind": "compute", + "expect": 83.41182583971526, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [167.624948, 10.329054, 2.215713, 58.534594] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "Could you work out the magnetic flux density given Hartmann number = 167.625, characteristic length = 10.3291, electrical conductivity = 2.21571, dynamic viscosity = 58.5346?", + "a": "83.41182583971526", + "valid": true, + "kind": "compute", + "expect": 83.41182583971526, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [167.624948, 10.329054, 2.215713, 58.534594] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "I need the magnetic flux density for a Hartmann number of 8.53158, a characteristic length of 124.718, an electrical conductivity of 1.91195 and a dynamic viscosity of 82.2401, please.", + "a": "0.4486449888731755", + "valid": true, + "kind": "compute", + "expect": 0.4486449888731755, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [8.531577, 124.718185, 1.911952, 82.240072] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "If the Hartmann number is 8.53158, characteristic length is 124.718, electrical conductivity is 1.91195 and the dynamic viscosity is 82.2401, what is the magnetic flux density?", + "a": "0.4486449888731755", + "valid": true, + "kind": "compute", + "expect": 0.4486449888731755, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [8.531577, 124.718185, 1.911952, 82.240072] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "Could you work out the magnetic flux density given Hartmann number = 81.1967, characteristic length = 6.98086, electrical conductivity = 0.551236, dynamic viscosity = 2.88512?", + "a": "26.60987734405743", + "valid": true, + "kind": "compute", + "expect": 26.60987734405743, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [81.196705, 6.980857, 0.551236, 2.88512] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "I need the magnetic flux density for a Hartmann number of 81.1967, a characteristic length of 6.98086, an electrical conductivity of 0.551236 and a dynamic viscosity of 2.88512, please.", + "a": "26.60987734405743", + "valid": true, + "kind": "compute", + "expect": 26.60987734405743, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [81.196705, 6.980857, 0.551236, 2.88512] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "If the Hartmann number is 1.16997, characteristic length is 1.14865, electrical conductivity is 21.7931 and the dynamic viscosity is 29.5106, what is the magnetic flux density?", + "a": "1.1852629650829185", + "valid": true, + "kind": "compute", + "expect": 1.1852629650829185, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [1.169968, 1.148652, 21.793126, 29.510609] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "Could you work out the magnetic flux density given Hartmann number = 1.16997, characteristic length = 1.14865, electrical conductivity = 21.7931, dynamic viscosity = 29.5106?", + "a": "1.1852629650829185", + "valid": true, + "kind": "compute", + "expect": 1.1852629650829185, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [1.169968, 1.148652, 21.793126, 29.510609] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "I need the magnetic flux density for a Hartmann number of 223.509, a characteristic length of 17.3414, an electrical conductivity of 7.10705 and a dynamic viscosity of 30.2507, please.", + "a": "26.59089746903788", + "valid": true, + "kind": "compute", + "expect": 26.59089746903788, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [223.508796, 17.341435, 7.107049, 30.250749] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "If the Hartmann number is 223.509, characteristic length is 17.3414, electrical conductivity is 7.10705 and the dynamic viscosity is 30.2507, what is the magnetic flux density?", + "a": "26.59089746903788", + "valid": true, + "kind": "compute", + "expect": 26.59089746903788, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [223.508796, 17.341435, 7.107049, 30.250749] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "Could you work out the magnetic flux density given Hartmann number = 0.869111, characteristic length = 39.1753, electrical conductivity = 0.432217, dynamic viscosity = 16.6571?", + "a": "0.13772459360779798", + "valid": true, + "kind": "compute", + "expect": 0.13772459360779798, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [0.869111, 39.175263, 0.432217, 16.657051] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "I need the magnetic flux density for a Hartmann number of 0.869111, a characteristic length of 39.1753, an electrical conductivity of 0.432217 and a dynamic viscosity of 16.6571, please.", + "a": "0.13772459360779798", + "valid": true, + "kind": "compute", + "expect": 0.13772459360779798, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [0.869111, 39.175263, 0.432217, 16.657051] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "If the Hartmann number is 167.625, characteristic length is 10.3291 and the electrical conductivity is 2.21571, what is the magnetic flux density?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the dynamic viscosity is not given" + }, + { + "q": "Could you work out the magnetic flux density given Hartmann number = 8.53158, characteristic length = 124.718, electrical conductivity = 1.91195?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the dynamic viscosity is not given" + }, + { + "q": "Do you know the magnetic flux 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 magnetic flux 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 magnetic flux density given Hartmann number = -81.1967, characteristic length = 6.98086, electrical conductivity = 0.551236, dynamic viscosity = 2.88512?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Hartmann number: no example had one" + }, + { + "q": "What is a typical magnetic flux 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-magnetic-flux-density-4/program.py b/root/organs10/physics/specialists/solved-magnetic-flux-density-4/program.py new file mode 100644 index 0000000000000000000000000000000000000000..cb89762017142a0ae06e533a332d04d81e6e7e2a --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density-4/program.py @@ -0,0 +1,7 @@ +"""magnetic flux density from Hartmann number, characteristic length, electrical conductivity, dynamic viscosity (Hartmann number (Wikidata Q1587280)) +The maths organ (systems.py): solved from the system q0 = q1*q2*sqrt(((q3)/(q4))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(hartmann_number, characteristic_length, electrical_conductivity, dynamic_viscosity): + return 1.0*dynamic_viscosity**0.5*electrical_conductivity**(-0.5)*hartmann_number/characteristic_length diff --git a/root/organs10/physics/specialists/solved-magnetic-flux-density-4/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-flux-density-4/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density-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-magnetic-flux-density-5/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-flux-density-5/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density-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-magnetic-flux-density-5/panel.json b/root/organs10/physics/specialists/solved-magnetic-flux-density-5/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..78f40656744a85815802da8716b1f5e588d887be --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density-5/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic flux density for Alfvén wave speed 107.961, mass density 59.4105 and magnetic permeability 5.77717?", + "a": "2000.112989052058", + "valid": true, + "kind": "compute", + "expect": 2000.112989052058, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [107.960666, 59.4105, 5.777165] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "Tell me the magnetic flux density with Alfvén wave speed of 107.961, mass density of 59.4105 and magnetic permeability of 5.77717.", + "a": "2000.112989052058", + "valid": true, + "kind": "compute", + "expect": 2000.112989052058, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [107.960666, 59.4105, 5.777165] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "What is the magnetic flux density for Alfvén wave speed 85.2416, mass density 160.878 and magnetic permeability 2.69428?", + "a": "1774.6878974590418", + "valid": true, + "kind": "compute", + "expect": 1774.6878974590418, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [85.241623, 160.878442, 2.69428] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "Tell me the magnetic flux density with Alfvén wave speed of 85.2416, mass density of 160.878 and magnetic permeability of 2.69428.", + "a": "1774.6878974590418", + "valid": true, + "kind": "compute", + "expect": 1774.6878974590418, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [85.241623, 160.878442, 2.69428] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "What is the magnetic flux density for Alfvén wave speed 45.6157, mass density 4.97131 and magnetic permeability 94.305?", + "a": "987.6813159711828", + "valid": true, + "kind": "compute", + "expect": 987.6813159711828, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [45.61567, 4.971307, 94.305046] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "Tell me the magnetic flux density with Alfvén wave speed of 45.6157, mass density of 4.97131 and magnetic permeability of 94.305.", + "a": "987.6813159711828", + "valid": true, + "kind": "compute", + "expect": 987.6813159711828, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [45.61567, 4.971307, 94.305046] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "What is the magnetic flux density for Alfvén wave speed 10.5638, mass density 198.839 and magnetic permeability 0.671697?", + "a": "122.08419188185322", + "valid": true, + "kind": "compute", + "expect": 122.08419188185322, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [10.563843, 198.838955, 0.671697] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "Tell me the magnetic flux density with Alfvén wave speed of 10.5638, mass density of 198.839 and magnetic permeability of 0.671697.", + "a": "122.08419188185322", + "valid": true, + "kind": "compute", + "expect": 122.08419188185322, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [10.563843, 198.838955, 0.671697] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "What is the magnetic flux density for Alfvén wave speed 21.7931, mass density 29.5106 and magnetic permeability 8.50341?", + "a": "345.2272836396581", + "valid": true, + "kind": "compute", + "expect": 345.2272836396581, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [21.793126, 29.510609, 8.503407] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "Tell me the magnetic flux density with Alfvén wave speed of 21.7931, mass density of 29.5106 and magnetic permeability of 8.50341.", + "a": "345.2272836396581", + "valid": true, + "kind": "compute", + "expect": 345.2272836396581, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [21.793126, 29.510609, 8.503407] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "What is the magnetic flux density for Alfvén wave speed 223.509, mass density 17.3414 and magnetic permeability 7.10705?", + "a": "2481.3143990572535", + "valid": true, + "kind": "compute", + "expect": 2481.3143990572535, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [223.508796, 17.341435, 7.107049] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "Tell me the magnetic flux density with Alfvén wave speed of 223.509, mass density of 17.3414 and magnetic permeability of 7.10705.", + "a": "2481.3143990572535", + "valid": true, + "kind": "compute", + "expect": 2481.3143990572535, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [223.508796, 17.341435, 7.107049] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "What is the magnetic flux density for Alfvén wave speed 107.961 and mass density 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "Tell me the magnetic flux density with Alfvén wave speed of 85.2416 and mass density of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "What is the magnetic flux 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 magnetic flux 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 magnetic flux density with Alfvén wave speed of -45.6157, mass density of 4.97131 and magnetic permeability of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Alfvén wave speed: no example had one" + }, + { + "q": "Which magnetic flux 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 Alfvén wave speed is 17.7888, mass density is 167.625 and the magnetic permeability is 10.3291, what is the magnetic flux density?", + "a": "740.1942888796797", + "valid": true, + "kind": "compute", + "expect": 740.1942888796797, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [17.788778, 167.624948, 10.329054] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "Could you work out the magnetic flux density given Alfvén wave speed = 17.7888, mass density = 167.625, magnetic permeability = 10.3291?", + "a": "740.1942888796797", + "valid": true, + "kind": "compute", + "expect": 740.1942888796797, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [17.788778, 167.624948, 10.329054] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "I need the magnetic flux density for an Alfvén wave speed of 21.511, a mass density of 173.394 and a magnetic permeability of 251.084, please.", + "a": "4488.353473947946", + "valid": true, + "kind": "compute", + "expect": 4488.353473947946, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [21.511016, 173.393638, 251.084037] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "If the Alfvén wave speed is 21.511, mass density is 173.394 and the magnetic permeability is 251.084, what is the magnetic flux density?", + "a": "4488.353473947946", + "valid": true, + "kind": "compute", + "expect": 4488.353473947946, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [21.511016, 173.393638, 251.084037] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "Could you work out the magnetic flux density given Alfvén wave speed = 129.245, mass density = 1.18359, magnetic permeability = 15.9416?", + "a": "561.4103882766713", + "valid": true, + "kind": "compute", + "expect": 561.4103882766713, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [129.244984, 1.18359, 15.941612] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "I need the magnetic flux density for an Alfvén wave speed of 129.245, a mass density of 1.18359 and a magnetic permeability of 15.9416, please.", + "a": "561.4103882766713", + "valid": true, + "kind": "compute", + "expect": 561.4103882766713, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [129.244984, 1.18359, 15.941612] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "If the Alfvén wave speed is 20.3966, mass density is 18.3169 and the magnetic permeability is 6.8386, what is the magnetic flux density?", + "a": "228.27903056645135", + "valid": true, + "kind": "compute", + "expect": 228.27903056645135, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [20.396558, 18.316867, 6.838598] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "Could you work out the magnetic flux density given Alfvén wave speed = 20.3966, mass density = 18.3169, magnetic permeability = 6.8386?", + "a": "228.27903056645135", + "valid": true, + "kind": "compute", + "expect": 228.27903056645135, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [20.396558, 18.316867, 6.838598] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "I need the magnetic flux density for an Alfvén wave speed of 185.886, a mass density of 13.2373 and a magnetic permeability of 4.71941, please.", + "a": "1469.2350565280553", + "valid": true, + "kind": "compute", + "expect": 1469.2350565280553, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [185.886273, 13.237311, 4.719415] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "If the Alfvén wave speed is 185.886, mass density is 13.2373 and the magnetic permeability is 4.71941, what is the magnetic flux density?", + "a": "1469.2350565280553", + "valid": true, + "kind": "compute", + "expect": 1469.2350565280553, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [185.886273, 13.237311, 4.719415] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "Could you work out the magnetic flux density given Alfvén wave speed = 9.1061, mass density = 24.5721, magnetic permeability = 108.451?", + "a": "470.07805809522335", + "valid": true, + "kind": "compute", + "expect": 470.07805809522335, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [9.106104, 24.572086, 108.450792] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "I need the magnetic flux density for an Alfvén wave speed of 9.1061, a mass density of 24.5721 and a magnetic permeability of 108.451, please.", + "a": "470.07805809522335", + "valid": true, + "kind": "compute", + "expect": 470.07805809522335, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [9.106104, 24.572086, 108.450792] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "If the Alfvén wave speed is 17.7888 and the mass density is 167.625, what is the magnetic flux density?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "Could you work out the magnetic flux density given Alfvén wave speed = 21.511, mass density = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "Do you know the magnetic flux 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 magnetic flux 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 magnetic flux density given Alfvén wave speed = -129.245, mass density = 1.18359, magnetic permeability = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Alfvén wave speed: no example had one" + }, + { + "q": "What is a typical magnetic flux 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-magnetic-flux-density-5/program.py b/root/organs10/physics/specialists/solved-magnetic-flux-density-5/program.py new file mode 100644 index 0000000000000000000000000000000000000000..10b3244c0155f58823ec8d9e37f4aef1d5b1dc7f --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density-5/program.py @@ -0,0 +1,7 @@ +"""magnetic flux density from Alfvén wave speed, mass density, magnetic permeability (Alfvén wave speed (Wikidata Q2646422)) +The maths organ (systems.py): solved from the system q0 = ((q1)/(sqrt(q2*q3))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(alfv_n_wave_speed, mass_density, magnetic_permeability): + return 1.0*alfv_n_wave_speed*magnetic_permeability**0.5*mass_density**0.5 diff --git a/root/organs10/physics/specialists/solved-magnetic-flux-density-5/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-flux-density-5/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density-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-magnetic-flux-density-6/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-flux-density-6/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density-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-magnetic-flux-density-6/panel.json b/root/organs10/physics/specialists/solved-magnetic-flux-density-6/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..99a384976db5116a1f1e8fae0e6465a27b8ac700 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density-6/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic flux density for permeability tensor 107.961 and magnetic field strength 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [107.960666, 59.4105] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "Tell me the magnetic flux density with permeability tensor of 107.961 and magnetic field strength of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [107.960666, 59.4105] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "What is the magnetic flux density for permeability tensor 169.527 and magnetic field strength 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [169.526958, 280.773702] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "Tell me the magnetic flux density with permeability tensor of 169.527 and magnetic field strength of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [169.526958, 280.773702] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "What is the magnetic flux density for permeability tensor 1.91195 and magnetic field strength 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [1.911952, 82.240072] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "Tell me the magnetic flux density with permeability tensor of 1.91195 and magnetic field strength of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [1.911952, 82.240072] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "What is the magnetic flux density for permeability tensor 1.64379 and magnetic field strength 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [1.643795, 252.709024] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "Tell me the magnetic flux density with permeability tensor of 1.64379 and magnetic field strength of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [1.643795, 252.709024] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "What is the magnetic flux density for permeability tensor 18.3169 and magnetic field strength 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [18.316867, 6.838598] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "Tell me the magnetic flux density with permeability tensor of 18.3169 and magnetic field strength of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [18.316867, 6.838598] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "What is the magnetic flux density for permeability tensor 156.502 and magnetic field strength 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [156.50181, 185.886273] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "Tell me the magnetic flux density with permeability tensor of 156.502 and magnetic field strength of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [156.50181, 185.886273] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "What is the magnetic flux density for permeability tensor 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic field strength is not given" + }, + { + "q": "Tell me the magnetic flux density with permeability tensor of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic field strength is not given" + }, + { + "q": "What is the magnetic flux 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 magnetic flux 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 magnetic flux density with permeability tensor of -1.91195 and magnetic field strength of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative permeability tensor: no example had one" + }, + { + "q": "Which magnetic flux 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 permeability tensor is 8.50728 and the magnetic field strength is 17.7888, what is the magnetic flux density?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [8.507283, 17.788778] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "Could you work out the magnetic flux density given permeability tensor = 8.50728, magnetic field strength = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [8.507283, 17.788778] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "I need the magnetic flux density for a permeability tensor of 8.24951 and a magnetic field strength of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [8.24951, 0.634021] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "If the permeability tensor is 8.24951 and the magnetic field strength is 0.634021, what is the magnetic flux density?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [8.24951, 0.634021] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "Could you work out the magnetic flux density given permeability tensor = 0.318734, magnetic field strength = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [0.318734, 9.566071] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "I need the magnetic flux density for a permeability tensor of 0.318734 and a magnetic field strength of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [0.318734, 9.566071] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "If the permeability tensor is 0.671697 and the magnetic field strength is 14.2496, what is the magnetic flux density?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [0.671697, 14.249569] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "Could you work out the magnetic flux density given permeability tensor = 0.671697, magnetic field strength = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [0.671697, 14.249569] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "I need the magnetic flux density for a permeability tensor of 21.7931 and a magnetic field strength of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [21.793126, 29.510609] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "If the permeability tensor is 21.7931 and the magnetic field strength is 29.5106, what is the magnetic flux density?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [21.793126, 29.510609] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "Could you work out the magnetic flux density given permeability tensor = 153.15, magnetic field strength = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [153.149965, 18.595315] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "I need the magnetic flux density for a permeability tensor of 153.15 and a magnetic field strength of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [153.149965, 18.595315] (the system permeability tensor (Wikidata Q118895841))" + }, + { + "q": "If the permeability tensor is 8.50728, what is the magnetic flux density?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic field strength is not given" + }, + { + "q": "Could you work out the magnetic flux density given permeability tensor = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic field strength is not given" + }, + { + "q": "Do you know the magnetic flux 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 magnetic flux 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 magnetic flux density given permeability tensor = -0.318734, magnetic field strength = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative permeability tensor: no example had one" + }, + { + "q": "What is a typical magnetic flux 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-magnetic-flux-density-6/program.py b/root/organs10/physics/specialists/solved-magnetic-flux-density-6/program.py new file mode 100644 index 0000000000000000000000000000000000000000..d9da275e9b29f5254e1e44054e2434f3bfd8be10 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density-6/program.py @@ -0,0 +1,7 @@ +"""magnetic flux density from permeability tensor, magnetic field strength (permeability tensor (Wikidata Q118895841)) +The maths organ (systems.py): solved from the system q2 = q1 * q3; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(permeability_tensor, magnetic_field_strength): + return 1.0*magnetic_field_strength*permeability_tensor diff --git a/root/organs10/physics/specialists/solved-magnetic-flux-density-6/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-flux-density-6/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density-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-magnetic-flux-density-7/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-flux-density-7/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density-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-magnetic-flux-density-7/panel.json b/root/organs10/physics/specialists/solved-magnetic-flux-density-7/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..eb0c8801a4b47d0dbd33ced4997ac6d45684b73a --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density-7/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic flux density for magnetic field strength 107.961 and magnetic permeability 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [107.960666, 59.4105] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Tell me the magnetic flux density with magnetic field strength of 107.961 and magnetic permeability of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [107.960666, 59.4105] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "What is the magnetic flux density for magnetic field strength 169.527 and magnetic permeability 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [169.526958, 280.773702] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Tell me the magnetic flux density with magnetic field strength of 169.527 and magnetic permeability of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [169.526958, 280.773702] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "What is the magnetic flux density for magnetic field strength 1.91195 and magnetic permeability 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [1.911952, 82.240072] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Tell me the magnetic flux density with magnetic field strength of 1.91195 and magnetic permeability of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [1.911952, 82.240072] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "What is the magnetic flux density for magnetic field strength 1.64379 and magnetic permeability 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [1.643795, 252.709024] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Tell me the magnetic flux density with magnetic field strength of 1.64379 and magnetic permeability of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [1.643795, 252.709024] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "What is the magnetic flux density for magnetic field strength 18.3169 and magnetic permeability 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [18.316867, 6.838598] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Tell me the magnetic flux density with magnetic field strength of 18.3169 and magnetic permeability of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [18.316867, 6.838598] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "What is the magnetic flux density for magnetic field strength 156.502 and magnetic permeability 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [156.50181, 185.886273] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Tell me the magnetic flux density with magnetic field strength of 156.502 and magnetic permeability of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [156.50181, 185.886273] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "What is the magnetic flux density for magnetic field strength 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "Tell me the magnetic flux density with magnetic field strength of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "What is the magnetic flux 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 magnetic flux 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 magnetic flux density with magnetic field strength of -1.91195 and magnetic permeability of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative magnetic field strength: no example had one" + }, + { + "q": "Which magnetic flux 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 magnetic field strength is 8.50728 and the magnetic permeability is 17.7888, what is the magnetic flux density?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [8.507283, 17.788778] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Could you work out the magnetic flux density given magnetic field strength = 8.50728, magnetic permeability = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [8.507283, 17.788778] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "I need the magnetic flux density for a magnetic field strength of 8.24951 and a magnetic permeability of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [8.24951, 0.634021] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "If the magnetic field strength is 8.24951 and the magnetic permeability is 0.634021, what is the magnetic flux density?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [8.24951, 0.634021] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Could you work out the magnetic flux density given magnetic field strength = 0.318734, magnetic permeability = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [0.318734, 9.566071] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "I need the magnetic flux density for a magnetic field strength of 0.318734 and a magnetic permeability of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [0.318734, 9.566071] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "If the magnetic field strength is 0.671697 and the magnetic permeability is 14.2496, what is the magnetic flux density?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [0.671697, 14.249569] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Could you work out the magnetic flux density given magnetic field strength = 0.671697, magnetic permeability = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [0.671697, 14.249569] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "I need the magnetic flux density for a magnetic field strength of 21.7931 and a magnetic permeability of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [21.793126, 29.510609] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "If the magnetic field strength is 21.7931 and the magnetic permeability is 29.5106, what is the magnetic flux density?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [21.793126, 29.510609] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Could you work out the magnetic flux density given magnetic field strength = 153.15, magnetic permeability = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [153.149965, 18.595315] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "I need the magnetic flux density for a magnetic field strength of 153.15 and a magnetic permeability of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [153.149965, 18.595315] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "If the magnetic field strength is 8.50728, what is the magnetic flux density?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "Could you work out the magnetic flux density given magnetic field strength = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "Do you know the magnetic flux 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 magnetic flux 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 magnetic flux density given magnetic field strength = -0.318734, magnetic permeability = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative magnetic field strength: no example had one" + }, + { + "q": "What is a typical magnetic flux 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-magnetic-flux-density-7/program.py b/root/organs10/physics/specialists/solved-magnetic-flux-density-7/program.py new file mode 100644 index 0000000000000000000000000000000000000000..5cc2d93dd7778ae589f6d7803b6a3679c0fc3477 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density-7/program.py @@ -0,0 +1,7 @@ +"""magnetic flux density from magnetic field strength, magnetic permeability (magnetic permeability (Wikidata Q28352)) +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(magnetic_field_strength, magnetic_permeability): + return 1.0*magnetic_field_strength*magnetic_permeability diff --git a/root/organs10/physics/specialists/solved-magnetic-flux-density-7/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-flux-density-7/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density-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-magnetic-flux-density-8/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-flux-density-8/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density-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-magnetic-flux-density-8/panel.json b/root/organs10/physics/specialists/solved-magnetic-flux-density-8/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..e59a030103dd5b90d8db0d1851b0652cf36f1d00 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density-8/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic flux density for Alfvén number 107.961, speed 59.4105, mass density 5.77717 and magnetic permeability 1.89126?", + "a": "1.8189893274293893", + "valid": true, + "kind": "compute", + "expect": 1.8189893274293893, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [107.960666, 59.4105, 5.777165, 1.891256] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Tell me the magnetic flux density with Alfvén number of 107.961, speed of 59.4105, mass density of 5.77717 and magnetic permeability of 1.89126.", + "a": "1.8189893274293893", + "valid": true, + "kind": "compute", + "expect": 1.8189893274293893, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [107.960666, 59.4105, 5.777165, 1.891256] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "What is the magnetic flux density for Alfvén number 2.69428, speed 48.921, mass density 157.223 and magnetic permeability 35.6412?", + "a": "1359.2078266683284", + "valid": true, + "kind": "compute", + "expect": 1359.2078266683284, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [2.69428, 48.920991, 157.22256, 35.641157] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Tell me the magnetic flux density with Alfvén number of 2.69428, speed of 48.921, mass density of 157.223 and magnetic permeability of 35.6412.", + "a": "1359.2078266683284", + "valid": true, + "kind": "compute", + "expect": 1359.2078266683284, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [2.69428, 48.920991, 157.22256, 35.641157] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "What is the magnetic flux density for Alfvén number 0.318734, speed 9.56607, mass density 126.709 and magnetic permeability 1.70503?", + "a": "441.1391682643939", + "valid": true, + "kind": "compute", + "expect": 441.1391682643939, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [0.318734, 9.566071, 126.709252, 1.705032] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Tell me the magnetic flux density with Alfvén number of 0.318734, speed of 9.56607, mass density of 126.709 and magnetic permeability of 1.70503.", + "a": "441.1391682643939", + "valid": true, + "kind": "compute", + "expect": 441.1391682643939, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [0.318734, 9.566071, 126.709252, 1.705032] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "What is the magnetic flux density for Alfvén number 87.781, speed 13.2084, mass density 246.329 and magnetic permeability 20.3966?", + "a": "10.66562946918316", + "valid": true, + "kind": "compute", + "expect": 10.66562946918316, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [87.781013, 13.208419, 246.329053, 20.396558] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Tell me the magnetic flux density with Alfvén number of 87.781, speed of 13.2084, mass density of 246.329 and magnetic permeability of 20.3966.", + "a": "10.66562946918316", + "valid": true, + "kind": "compute", + "expect": 10.66562946918316, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [87.781013, 13.208419, 246.329053, 20.396558] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "What is the magnetic flux density for Alfvén number 156.502, speed 185.886, mass density 13.2373 and magnetic permeability 4.71941?", + "a": "9.387974864913806", + "valid": true, + "kind": "compute", + "expect": 9.387974864913806, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [156.50181, 185.886273, 13.237311, 4.719415] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Tell me the magnetic flux density with Alfvén number of 156.502, speed of 185.886, mass density of 13.2373 and magnetic permeability of 4.71941.", + "a": "9.387974864913806", + "valid": true, + "kind": "compute", + "expect": 9.387974864913806, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [156.50181, 185.886273, 13.237311, 4.719415] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "What is the magnetic flux density for Alfvén number 24.5721, speed 108.451, mass density 1.69476 and magnetic permeability 49.4843?", + "a": "40.418293205152864", + "valid": true, + "kind": "compute", + "expect": 40.418293205152864, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [24.572086, 108.450792, 1.694755, 49.484326] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Tell me the magnetic flux density with Alfvén number of 24.5721, speed of 108.451, mass density of 1.69476 and magnetic permeability of 49.4843.", + "a": "40.418293205152864", + "valid": true, + "kind": "compute", + "expect": 40.418293205152864, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [24.572086, 108.450792, 1.694755, 49.484326] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "What is the magnetic flux density for Alfvén number 107.961, speed 59.4105 and mass density 5.77717?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "Tell me the magnetic flux density with Alfvén number of 2.69428, speed of 48.921 and mass density of 157.223.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "What is the magnetic flux 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 magnetic flux 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 magnetic flux density with Alfvén number of -0.318734, speed of 9.56607, mass density of 126.709 and magnetic permeability of 1.70503.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Alfvén number: no example had one" + }, + { + "q": "Which magnetic flux 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 Alfvén number is 167.625, speed is 10.3291, mass density is 2.21571 and the magnetic permeability is 58.5346, what is the magnetic flux density?", + "a": "0.7017541420461954", + "valid": true, + "kind": "compute", + "expect": 0.7017541420461954, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [167.624948, 10.329054, 2.215713, 58.534594] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Could you work out the magnetic flux density given Alfvén number = 167.625, speed = 10.3291, mass density = 2.21571, magnetic permeability = 58.5346?", + "a": "0.7017541420461954", + "valid": true, + "kind": "compute", + "expect": 0.7017541420461954, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [167.624948, 10.329054, 2.215713, 58.534594] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "I need the magnetic flux density for an Alfvén number of 8.53158, a speed of 124.718, a mass density of 1.91195 and a magnetic permeability of 82.2401, please.", + "a": "183.30767952901425", + "valid": true, + "kind": "compute", + "expect": 183.30767952901425, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [8.531577, 124.718185, 1.911952, 82.240072] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "If the Alfvén number is 8.53158, speed is 124.718, mass density is 1.91195 and the magnetic permeability is 82.2401, what is the magnetic flux density?", + "a": "183.30767952901425", + "valid": true, + "kind": "compute", + "expect": 183.30767952901425, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [8.531577, 124.718185, 1.911952, 82.240072] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Could you work out the magnetic flux density given Alfvén number = 81.1967, speed = 6.98086, mass density = 0.551236, magnetic permeability = 2.88512?", + "a": "0.1084228805469045", + "valid": true, + "kind": "compute", + "expect": 0.1084228805469045, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [81.196705, 6.980857, 0.551236, 2.88512] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "I need the magnetic flux density for an Alfvén number of 81.1967, a speed of 6.98086, a mass density of 0.551236 and a magnetic permeability of 2.88512, please.", + "a": "0.1084228805469045", + "valid": true, + "kind": "compute", + "expect": 0.1084228805469045, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [81.196705, 6.980857, 0.551236, 2.88512] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "If the Alfvén number is 1.16997, speed is 1.14865, mass density is 21.7931 and the magnetic permeability is 29.5106, what is the magnetic flux density?", + "a": "24.89794208480241", + "valid": true, + "kind": "compute", + "expect": 24.89794208480241, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [1.169968, 1.148652, 21.793126, 29.510609] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Could you work out the magnetic flux density given Alfvén number = 1.16997, speed = 1.14865, mass density = 21.7931, magnetic permeability = 29.5106?", + "a": "24.89794208480241", + "valid": true, + "kind": "compute", + "expect": 24.89794208480241, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [1.169968, 1.148652, 21.793126, 29.510609] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "I need the magnetic flux density for an Alfvén number of 223.509, a speed of 17.3414, a mass density of 7.10705 and a magnetic permeability of 30.2507, please.", + "a": "1.1376355121339767", + "valid": true, + "kind": "compute", + "expect": 1.1376355121339767, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [223.508796, 17.341435, 7.107049, 30.250749] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "If the Alfvén number is 223.509, speed is 17.3414, mass density is 7.10705 and the magnetic permeability is 30.2507, what is the magnetic flux density?", + "a": "1.1376355121339767", + "valid": true, + "kind": "compute", + "expect": 1.1376355121339767, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [223.508796, 17.341435, 7.107049, 30.250749] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Could you work out the magnetic flux density given Alfvén number = 0.869111, speed = 39.1753, mass density = 0.432217, magnetic permeability = 16.6571?", + "a": "120.94463573061672", + "valid": true, + "kind": "compute", + "expect": 120.94463573061672, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [0.869111, 39.175263, 0.432217, 16.657051] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "I need the magnetic flux density for an Alfvén number of 0.869111, a speed of 39.1753, a mass density of 0.432217 and a magnetic permeability of 16.6571, please.", + "a": "120.94463573061672", + "valid": true, + "kind": "compute", + "expect": 120.94463573061672, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [0.869111, 39.175263, 0.432217, 16.657051] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "If the Alfvén number is 167.625, speed is 10.3291 and the mass density is 2.21571, what is the magnetic flux density?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "Could you work out the magnetic flux density given Alfvén number = 8.53158, speed = 124.718, mass density = 1.91195?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "Do you know the magnetic flux 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 magnetic flux 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 magnetic flux density given Alfvén number = -81.1967, speed = 6.98086, mass density = 0.551236, magnetic permeability = 2.88512?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Alfvén number: no example had one" + }, + { + "q": "What is a typical magnetic flux 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-magnetic-flux-density-8/program.py b/root/organs10/physics/specialists/solved-magnetic-flux-density-8/program.py new file mode 100644 index 0000000000000000000000000000000000000000..513203a280808d24febb3ee92d78e70eeb3d83bc --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density-8/program.py @@ -0,0 +1,7 @@ +"""magnetic flux density from Alfvén number, speed, mass density, magnetic permeability (Alfvén number (Wikidata Q3342997)) +The maths organ (systems.py): solved from the system q0 = ((q1)/(q2 / sqrt(q3*q4))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(alfv_n_number, speed, mass_density, magnetic_permeability): + return 1.0*magnetic_permeability**0.5*mass_density**0.5*speed/alfv_n_number diff --git a/root/organs10/physics/specialists/solved-magnetic-flux-density-8/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-flux-density-8/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density-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-magnetic-flux-density/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-flux-density/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-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-magnetic-flux-density/panel.json b/root/organs10/physics/specialists/solved-magnetic-flux-density/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..cf1cf81bd3eb106a50728ccf1064582d8c45afd3 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic flux density for force 107.961, electric current 59.4105 and length 5.77717?", + "a": "0.31454848897160476", + "valid": true, + "kind": "compute", + "expect": 0.31454848897160476, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [107.960666, 59.4105, 5.777165] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Tell me the magnetic flux density with force of 107.961, electric current of 59.4105 and length of 5.77717.", + "a": "0.31454848897160476", + "valid": true, + "kind": "compute", + "expect": 0.31454848897160476, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [107.960666, 59.4105, 5.777165] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "What is the magnetic flux density for force 85.2416, electric current 160.878 and length 2.69428?", + "a": "0.19665778745152376", + "valid": true, + "kind": "compute", + "expect": 0.19665778745152376, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [85.241623, 160.878442, 2.69428] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Tell me the magnetic flux density with force of 85.2416, electric current of 160.878 and length of 2.69428.", + "a": "0.19665778745152376", + "valid": true, + "kind": "compute", + "expect": 0.19665778745152376, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [85.241623, 160.878442, 2.69428] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "What is the magnetic flux density for force 45.6157, electric current 4.97131 and length 94.305?", + "a": "0.09729902784967777", + "valid": true, + "kind": "compute", + "expect": 0.09729902784967777, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [45.61567, 4.971307, 94.305046] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Tell me the magnetic flux density with force of 45.6157, electric current of 4.97131 and length of 94.305.", + "a": "0.09729902784967777", + "valid": true, + "kind": "compute", + "expect": 0.09729902784967777, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [45.61567, 4.971307, 94.305046] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "What is the magnetic flux density for force 10.5638, electric current 198.839 and length 0.671697?", + "a": "0.07909461512056756", + "valid": true, + "kind": "compute", + "expect": 0.07909461512056756, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [10.563843, 198.838955, 0.671697] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Tell me the magnetic flux density with force of 10.5638, electric current of 198.839 and length of 0.671697.", + "a": "0.07909461512056756", + "valid": true, + "kind": "compute", + "expect": 0.07909461512056756, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [10.563843, 198.838955, 0.671697] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "What is the magnetic flux density for force 21.7931, electric current 29.5106 and length 8.50341?", + "a": "0.08684571530973768", + "valid": true, + "kind": "compute", + "expect": 0.08684571530973768, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [21.793126, 29.510609, 8.503407] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Tell me the magnetic flux density with force of 21.7931, electric current of 29.5106 and length of 8.50341.", + "a": "0.08684571530973768", + "valid": true, + "kind": "compute", + "expect": 0.08684571530973768, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [21.793126, 29.510609, 8.503407] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "What is the magnetic flux density for force 223.509, electric current 17.3414 and length 7.10705?", + "a": "1.813511306470856", + "valid": true, + "kind": "compute", + "expect": 1.813511306470856, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [223.508796, 17.341435, 7.107049] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Tell me the magnetic flux density with force of 223.509, electric current of 17.3414 and length of 7.10705.", + "a": "1.813511306470856", + "valid": true, + "kind": "compute", + "expect": 1.813511306470856, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [223.508796, 17.341435, 7.107049] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "What is the magnetic flux density for force 107.961 and electric current 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the length is not given" + }, + { + "q": "Tell me the magnetic flux density with force of 85.2416 and electric current of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the length is not given" + }, + { + "q": "What is the magnetic flux 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 magnetic flux 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 magnetic flux density with force of -45.6157, electric current of 4.97131 and length of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative force: no example had one" + }, + { + "q": "Which magnetic flux 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 force is 17.7888, electric current is 167.625 and the length is 10.3291, what is the magnetic flux density?", + "a": "0.010274173498684694", + "valid": true, + "kind": "compute", + "expect": 0.010274173498684694, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [17.788778, 167.624948, 10.329054] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Could you work out the magnetic flux density given force = 17.7888, electric current = 167.625, length = 10.3291?", + "a": "0.010274173498684694", + "valid": true, + "kind": "compute", + "expect": 0.010274173498684694, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [17.788778, 167.624948, 10.329054] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "I need the magnetic flux density for a force of 21.511, an electric current of 173.394 and a length of 251.084, please.", + "a": "0.000494092950151496", + "valid": true, + "kind": "compute", + "expect": 0.000494092950151496, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [21.511016, 173.393638, 251.084037] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "If the force is 21.511, electric current is 173.394 and the length is 251.084, what is the magnetic flux density?", + "a": "0.000494092950151496", + "valid": true, + "kind": "compute", + "expect": 0.000494092950151496, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [21.511016, 173.393638, 251.084037] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Could you work out the magnetic flux density given force = 129.245, electric current = 1.18359, length = 15.9416?", + "a": "6.849836414751194", + "valid": true, + "kind": "compute", + "expect": 6.849836414751194, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [129.244984, 1.18359, 15.941612] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "I need the magnetic flux density for a force of 129.245, an electric current of 1.18359 and a length of 15.9416, please.", + "a": "6.849836414751194", + "valid": true, + "kind": "compute", + "expect": 6.849836414751194, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [129.244984, 1.18359, 15.941612] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "If the force is 20.3966, electric current is 18.3169 and the length is 6.8386, what is the magnetic flux density?", + "a": "0.16283157083473473", + "valid": true, + "kind": "compute", + "expect": 0.16283157083473473, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [20.396558, 18.316867, 6.838598] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Could you work out the magnetic flux density given force = 20.3966, electric current = 18.3169, length = 6.8386?", + "a": "0.16283157083473473", + "valid": true, + "kind": "compute", + "expect": 0.16283157083473473, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [20.396558, 18.316867, 6.838598] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "I need the magnetic flux density for a force of 185.886, an electric current of 13.2373 and a length of 4.71941, please.", + "a": "2.975496158535072", + "valid": true, + "kind": "compute", + "expect": 2.975496158535072, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [185.886273, 13.237311, 4.719415] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "If the force is 185.886, electric current is 13.2373 and the length is 4.71941, what is the magnetic flux density?", + "a": "2.975496158535072", + "valid": true, + "kind": "compute", + "expect": 2.975496158535072, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [185.886273, 13.237311, 4.719415] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Could you work out the magnetic flux density given force = 9.1061, electric current = 24.5721, length = 108.451?", + "a": "0.003417101331511669", + "valid": true, + "kind": "compute", + "expect": 0.003417101331511669, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [9.106104, 24.572086, 108.450792] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "I need the magnetic flux density for a force of 9.1061, an electric current of 24.5721 and a length of 108.451, please.", + "a": "0.003417101331511669", + "valid": true, + "kind": "compute", + "expect": 0.003417101331511669, + "dtype": "quantity", + "unit": "kg/s^2/A", + "why": "magnetic flux density for [9.106104, 24.572086, 108.450792] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "If the force is 17.7888 and the electric current is 167.625, what is the magnetic flux density?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the length is not given" + }, + { + "q": "Could you work out the magnetic flux density given force = 21.511, electric current = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the length is not given" + }, + { + "q": "Do you know the magnetic flux 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 magnetic flux 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 magnetic flux density given force = -129.245, electric current = 1.18359, length = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative force: no example had one" + }, + { + "q": "What is a typical magnetic flux 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-magnetic-flux-density/program.py b/root/organs10/physics/specialists/solved-magnetic-flux-density/program.py new file mode 100644 index 0000000000000000000000000000000000000000..3d78db659c026a8b730fc270cc0d1351c3837cb2 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-density/program.py @@ -0,0 +1,7 @@ +"""magnetic flux density from force, electric current, length (magnetic field of test current (Wikidata Q104145274)) +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(force, electric_current, length): + return 1.0*force/(electric_current*length) diff --git a/root/organs10/physics/specialists/solved-magnetic-flux-density/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-flux-density/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-flux-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-magnetic-permeability-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-permeability-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-permeability-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-magnetic-permeability-2/panel.json b/root/organs10/physics/specialists/solved-magnetic-permeability-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..f10b89484d36a74674a91f72b2a409bc39f193f6 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-permeability-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic permeability for Roberts number 107.961, thermal diffusivity 59.4105 and electrical conductivity 5.77717?", + "a": "0.31454848897160476", + "valid": true, + "kind": "compute", + "expect": 0.31454848897160476, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [107.960666, 59.4105, 5.777165] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Tell me the magnetic permeability with Roberts number of 107.961, thermal diffusivity of 59.4105 and electrical conductivity of 5.77717.", + "a": "0.31454848897160476", + "valid": true, + "kind": "compute", + "expect": 0.31454848897160476, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [107.960666, 59.4105, 5.777165] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "What is the magnetic permeability for Roberts number 85.2416, thermal diffusivity 160.878 and electrical conductivity 2.69428?", + "a": "0.19665778745152376", + "valid": true, + "kind": "compute", + "expect": 0.19665778745152376, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [85.241623, 160.878442, 2.69428] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Tell me the magnetic permeability with Roberts number of 85.2416, thermal diffusivity of 160.878 and electrical conductivity of 2.69428.", + "a": "0.19665778745152376", + "valid": true, + "kind": "compute", + "expect": 0.19665778745152376, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [85.241623, 160.878442, 2.69428] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "What is the magnetic permeability for Roberts number 45.6157, thermal diffusivity 4.97131 and electrical conductivity 94.305?", + "a": "0.09729902784967777", + "valid": true, + "kind": "compute", + "expect": 0.09729902784967777, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [45.61567, 4.971307, 94.305046] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Tell me the magnetic permeability with Roberts number of 45.6157, thermal diffusivity of 4.97131 and electrical conductivity of 94.305.", + "a": "0.09729902784967777", + "valid": true, + "kind": "compute", + "expect": 0.09729902784967777, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [45.61567, 4.971307, 94.305046] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "What is the magnetic permeability for Roberts number 10.5638, thermal diffusivity 198.839 and electrical conductivity 0.671697?", + "a": "0.07909461512056756", + "valid": true, + "kind": "compute", + "expect": 0.07909461512056756, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [10.563843, 198.838955, 0.671697] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Tell me the magnetic permeability with Roberts number of 10.5638, thermal diffusivity of 198.839 and electrical conductivity of 0.671697.", + "a": "0.07909461512056756", + "valid": true, + "kind": "compute", + "expect": 0.07909461512056756, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [10.563843, 198.838955, 0.671697] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "What is the magnetic permeability for Roberts number 21.7931, thermal diffusivity 29.5106 and electrical conductivity 8.50341?", + "a": "0.08684571530973768", + "valid": true, + "kind": "compute", + "expect": 0.08684571530973768, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [21.793126, 29.510609, 8.503407] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Tell me the magnetic permeability with Roberts number of 21.7931, thermal diffusivity of 29.5106 and electrical conductivity of 8.50341.", + "a": "0.08684571530973768", + "valid": true, + "kind": "compute", + "expect": 0.08684571530973768, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [21.793126, 29.510609, 8.503407] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "What is the magnetic permeability for Roberts number 223.509, thermal diffusivity 17.3414 and electrical conductivity 7.10705?", + "a": "1.813511306470856", + "valid": true, + "kind": "compute", + "expect": 1.813511306470856, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [223.508796, 17.341435, 7.107049] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Tell me the magnetic permeability with Roberts number of 223.509, thermal diffusivity of 17.3414 and electrical conductivity of 7.10705.", + "a": "1.813511306470856", + "valid": true, + "kind": "compute", + "expect": 1.813511306470856, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [223.508796, 17.341435, 7.107049] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "What is the magnetic permeability for Roberts number 107.961 and thermal diffusivity 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electrical conductivity is not given" + }, + { + "q": "Tell me the magnetic permeability with Roberts number of 85.2416 and thermal diffusivity of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electrical conductivity is not given" + }, + { + "q": "What is the magnetic permeability 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 magnetic permeability 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 magnetic permeability with Roberts number of -45.6157, thermal diffusivity of 4.97131 and electrical conductivity of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Roberts number: no example had one" + }, + { + "q": "Which magnetic permeability 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 Roberts number is 17.7888, thermal diffusivity is 167.625 and the electrical conductivity is 10.3291, what is the magnetic permeability?", + "a": "0.010274173498684694", + "valid": true, + "kind": "compute", + "expect": 0.010274173498684694, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [17.788778, 167.624948, 10.329054] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Could you work out the magnetic permeability given Roberts number = 17.7888, thermal diffusivity = 167.625, electrical conductivity = 10.3291?", + "a": "0.010274173498684694", + "valid": true, + "kind": "compute", + "expect": 0.010274173498684694, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [17.788778, 167.624948, 10.329054] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "I need the magnetic permeability for a Roberts number of 21.511, a thermal diffusivity of 173.394 and an electrical conductivity of 251.084, please.", + "a": "0.000494092950151496", + "valid": true, + "kind": "compute", + "expect": 0.000494092950151496, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [21.511016, 173.393638, 251.084037] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "If the Roberts number is 21.511, thermal diffusivity is 173.394 and the electrical conductivity is 251.084, what is the magnetic permeability?", + "a": "0.000494092950151496", + "valid": true, + "kind": "compute", + "expect": 0.000494092950151496, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [21.511016, 173.393638, 251.084037] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Could you work out the magnetic permeability given Roberts number = 129.245, thermal diffusivity = 1.18359, electrical conductivity = 15.9416?", + "a": "6.849836414751194", + "valid": true, + "kind": "compute", + "expect": 6.849836414751194, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [129.244984, 1.18359, 15.941612] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "I need the magnetic permeability for a Roberts number of 129.245, a thermal diffusivity of 1.18359 and an electrical conductivity of 15.9416, please.", + "a": "6.849836414751194", + "valid": true, + "kind": "compute", + "expect": 6.849836414751194, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [129.244984, 1.18359, 15.941612] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "If the Roberts number is 20.3966, thermal diffusivity is 18.3169 and the electrical conductivity is 6.8386, what is the magnetic permeability?", + "a": "0.16283157083473473", + "valid": true, + "kind": "compute", + "expect": 0.16283157083473473, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [20.396558, 18.316867, 6.838598] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Could you work out the magnetic permeability given Roberts number = 20.3966, thermal diffusivity = 18.3169, electrical conductivity = 6.8386?", + "a": "0.16283157083473473", + "valid": true, + "kind": "compute", + "expect": 0.16283157083473473, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [20.396558, 18.316867, 6.838598] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "I need the magnetic permeability for a Roberts number of 185.886, a thermal diffusivity of 13.2373 and an electrical conductivity of 4.71941, please.", + "a": "2.975496158535072", + "valid": true, + "kind": "compute", + "expect": 2.975496158535072, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [185.886273, 13.237311, 4.719415] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "If the Roberts number is 185.886, thermal diffusivity is 13.2373 and the electrical conductivity is 4.71941, what is the magnetic permeability?", + "a": "2.975496158535072", + "valid": true, + "kind": "compute", + "expect": 2.975496158535072, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [185.886273, 13.237311, 4.719415] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Could you work out the magnetic permeability given Roberts number = 9.1061, thermal diffusivity = 24.5721, electrical conductivity = 108.451?", + "a": "0.003417101331511669", + "valid": true, + "kind": "compute", + "expect": 0.003417101331511669, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [9.106104, 24.572086, 108.450792] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "I need the magnetic permeability for a Roberts number of 9.1061, a thermal diffusivity of 24.5721 and an electrical conductivity of 108.451, please.", + "a": "0.003417101331511669", + "valid": true, + "kind": "compute", + "expect": 0.003417101331511669, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [9.106104, 24.572086, 108.450792] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "If the Roberts number is 17.7888 and the thermal diffusivity is 167.625, what is the magnetic permeability?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electrical conductivity is not given" + }, + { + "q": "Could you work out the magnetic permeability given Roberts number = 21.511, thermal diffusivity = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electrical conductivity is not given" + }, + { + "q": "Do you know the magnetic permeability 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 magnetic permeability 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 magnetic permeability given Roberts number = -129.245, thermal diffusivity = 1.18359, electrical conductivity = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Roberts number: no example had one" + }, + { + "q": "What is a typical magnetic permeability?", + "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-magnetic-permeability-2/program.py b/root/organs10/physics/specialists/solved-magnetic-permeability-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..a45f474b2a5f4fe2e6852804514904f29f2f4d5a --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-permeability-2/program.py @@ -0,0 +1,7 @@ +"""magnetic permeability from Roberts number, thermal diffusivity, electrical conductivity (Roberts number (Wikidata Q105190387)) +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(roberts_number, thermal_diffusivity, electrical_conductivity): + return 1.0*roberts_number/(electrical_conductivity*thermal_diffusivity) diff --git a/root/organs10/physics/specialists/solved-magnetic-permeability-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-permeability-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-permeability-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-magnetic-permeability-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-permeability-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-permeability-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-magnetic-permeability-3/panel.json b/root/organs10/physics/specialists/solved-magnetic-permeability-3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..31abc964c3e32fa06d0677910ee227bd93b218e1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-permeability-3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic permeability for Alfvén wave speed 107.961, magnetic flux density 59.4105 and mass density 5.77717?", + "a": "0.05241801442393724", + "valid": true, + "kind": "compute", + "expect": 0.05241801442393724, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [107.960666, 59.4105, 5.777165] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "Tell me the magnetic permeability with Alfvén wave speed of 107.961, magnetic flux density of 59.4105 and mass density of 5.77717.", + "a": "0.05241801442393724", + "valid": true, + "kind": "compute", + "expect": 0.05241801442393724, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [107.960666, 59.4105, 5.777165] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "What is the magnetic permeability for Alfvén wave speed 85.2416, magnetic flux density 160.878 and mass density 2.69428?", + "a": "1.3220551695554001", + "valid": true, + "kind": "compute", + "expect": 1.3220551695554001, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [85.241623, 160.878442, 2.69428] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "Tell me the magnetic permeability with Alfvén wave speed of 85.2416, magnetic flux density of 160.878 and mass density of 2.69428.", + "a": "1.3220551695554001", + "valid": true, + "kind": "compute", + "expect": 1.3220551695554001, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [85.241623, 160.878442, 2.69428] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "What is the magnetic permeability for Alfvén wave speed 45.6157, magnetic flux density 4.97131 and mass density 94.305?", + "a": "0.00012594420909810302", + "valid": true, + "kind": "compute", + "expect": 0.00012594420909810302, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [45.61567, 4.971307, 94.305046] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "Tell me the magnetic permeability with Alfvén wave speed of 45.6157, magnetic flux density of 4.97131 and mass density of 94.305.", + "a": "0.00012594420909810302", + "valid": true, + "kind": "compute", + "expect": 0.00012594420909810302, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [45.61567, 4.971307, 94.305046] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "What is the magnetic permeability for Alfvén wave speed 10.5638, magnetic flux density 198.839 and mass density 0.671697?", + "a": "527.4551686635131", + "valid": true, + "kind": "compute", + "expect": 527.4551686635131, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [10.563843, 198.838955, 0.671697] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "Tell me the magnetic permeability with Alfvén wave speed of 10.5638, magnetic flux density of 198.839 and mass density of 0.671697.", + "a": "527.4551686635131", + "valid": true, + "kind": "compute", + "expect": 527.4551686635131, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [10.563843, 198.838955, 0.671697] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "What is the magnetic permeability for Alfvén wave speed 21.7931, magnetic flux density 29.5106 and mass density 8.50341?", + "a": "0.2156374952922911", + "valid": true, + "kind": "compute", + "expect": 0.2156374952922911, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [21.793126, 29.510609, 8.503407] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "Tell me the magnetic permeability with Alfvén wave speed of 21.7931, magnetic flux density of 29.5106 and mass density of 8.50341.", + "a": "0.2156374952922911", + "valid": true, + "kind": "compute", + "expect": 0.2156374952922911, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [21.793126, 29.510609, 8.503407] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "What is the magnetic permeability for Alfvén wave speed 223.509, magnetic flux density 17.3414 and mass density 7.10705?", + "a": "0.0008470157874618325", + "valid": true, + "kind": "compute", + "expect": 0.0008470157874618325, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [223.508796, 17.341435, 7.107049] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "Tell me the magnetic permeability with Alfvén wave speed of 223.509, magnetic flux density of 17.3414 and mass density of 7.10705.", + "a": "0.0008470157874618325", + "valid": true, + "kind": "compute", + "expect": 0.0008470157874618325, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [223.508796, 17.341435, 7.107049] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "What is the magnetic permeability for Alfvén wave speed 107.961 and magnetic flux density 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Tell me the magnetic permeability with Alfvén wave speed of 85.2416 and magnetic flux density of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "What is the magnetic permeability 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 magnetic permeability 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 magnetic permeability with Alfvén wave speed of -45.6157, magnetic flux density of 4.97131 and mass density of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Alfvén wave speed: no example had one" + }, + { + "q": "Which magnetic permeability 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 Alfvén wave speed is 17.7888, magnetic flux density is 167.625 and the mass density is 10.3291, what is the magnetic permeability?", + "a": "8.596556205266598", + "valid": true, + "kind": "compute", + "expect": 8.596556205266598, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [17.788778, 167.624948, 10.329054] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "Could you work out the magnetic permeability given Alfvén wave speed = 17.7888, magnetic flux density = 167.625, mass density = 10.3291?", + "a": "8.596556205266598", + "valid": true, + "kind": "compute", + "expect": 8.596556205266598, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [17.788778, 167.624948, 10.329054] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "I need the magnetic permeability for an Alfvén wave speed of 21.511, a magnetic flux density of 173.394 and a mass density of 251.084, please.", + "a": "0.25877682223623494", + "valid": true, + "kind": "compute", + "expect": 0.25877682223623494, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [21.511016, 173.393638, 251.084037] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "If the Alfvén wave speed is 21.511, magnetic flux density is 173.394 and the mass density is 251.084, what is the magnetic permeability?", + "a": "0.25877682223623494", + "valid": true, + "kind": "compute", + "expect": 0.25877682223623494, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [21.511016, 173.393638, 251.084037] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "Could you work out the magnetic permeability given Alfvén wave speed = 129.245, magnetic flux density = 1.18359, mass density = 15.9416?", + "a": "5.260692712073857e-06", + "valid": true, + "kind": "compute", + "expect": 5.260692712073857e-06, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [129.244984, 1.18359, 15.941612] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "I need the magnetic permeability for an Alfvén wave speed of 129.245, a magnetic flux density of 1.18359 and a mass density of 15.9416, please.", + "a": "5.260692712073857e-06", + "valid": true, + "kind": "compute", + "expect": 5.260692712073857e-06, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [129.244984, 1.18359, 15.941612] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "If the Alfvén wave speed is 20.3966, magnetic flux density is 18.3169 and the mass density is 6.8386, what is the magnetic permeability?", + "a": "0.11792925051906678", + "valid": true, + "kind": "compute", + "expect": 0.11792925051906678, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [20.396558, 18.316867, 6.838598] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "Could you work out the magnetic permeability given Alfvén wave speed = 20.3966, magnetic flux density = 18.3169, mass density = 6.8386?", + "a": "0.11792925051906678", + "valid": true, + "kind": "compute", + "expect": 0.11792925051906678, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [20.396558, 18.316867, 6.838598] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "I need the magnetic permeability for an Alfvén wave speed of 185.886, a magnetic flux density of 13.2373 and a mass density of 4.71941, please.", + "a": "0.0010745255833685137", + "valid": true, + "kind": "compute", + "expect": 0.0010745255833685137, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [185.886273, 13.237311, 4.719415] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "If the Alfvén wave speed is 185.886, magnetic flux density is 13.2373 and the mass density is 4.71941, what is the magnetic permeability?", + "a": "0.0010745255833685137", + "valid": true, + "kind": "compute", + "expect": 0.0010745255833685137, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [185.886273, 13.237311, 4.719415] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "Could you work out the magnetic permeability given Alfvén wave speed = 9.1061, magnetic flux density = 24.5721, mass density = 108.451?", + "a": "0.0671407369189349", + "valid": true, + "kind": "compute", + "expect": 0.0671407369189349, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [9.106104, 24.572086, 108.450792] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "I need the magnetic permeability for an Alfvén wave speed of 9.1061, a magnetic flux density of 24.5721 and a mass density of 108.451, please.", + "a": "0.0671407369189349", + "valid": true, + "kind": "compute", + "expect": 0.0671407369189349, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [9.106104, 24.572086, 108.450792] (the system Alfvén wave speed (Wikidata Q2646422))" + }, + { + "q": "If the Alfvén wave speed is 17.7888 and the magnetic flux density is 167.625, what is the magnetic permeability?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Could you work out the magnetic permeability given Alfvén wave speed = 21.511, magnetic flux density = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Do you know the magnetic permeability 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 magnetic permeability 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 magnetic permeability given Alfvén wave speed = -129.245, magnetic flux density = 1.18359, mass density = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Alfvén wave speed: no example had one" + }, + { + "q": "What is a typical magnetic permeability?", + "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-magnetic-permeability-3/program.py b/root/organs10/physics/specialists/solved-magnetic-permeability-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..1743cc11e4cfb8890a31e8c991911b1126bbad9a --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-permeability-3/program.py @@ -0,0 +1,7 @@ +"""magnetic permeability from Alfvén wave speed, magnetic flux density, mass density (Alfvén wave speed (Wikidata Q2646422)) +The maths organ (systems.py): solved from the system q0 = ((q1)/(sqrt(q2*q3))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(alfv_n_wave_speed, magnetic_flux_density, mass_density): + return 1.0*magnetic_flux_density**2/(alfv_n_wave_speed**2*mass_density) diff --git a/root/organs10/physics/specialists/solved-magnetic-permeability-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-permeability-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-permeability-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-magnetic-permeability-4/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-permeability-4/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-permeability-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-magnetic-permeability-4/panel.json b/root/organs10/physics/specialists/solved-magnetic-permeability-4/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..2cc552ecdecdb51b75bd5bcd437c5c5aacb98859 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-permeability-4/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic permeability for magnetic flux density 107.961 and magnetic field strength 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [107.960666, 59.4105] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Tell me the magnetic permeability with magnetic flux density of 107.961 and magnetic field strength of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [107.960666, 59.4105] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "What is the magnetic permeability for magnetic flux density 169.527 and magnetic field strength 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [169.526958, 280.773702] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Tell me the magnetic permeability with magnetic flux density of 169.527 and magnetic field strength of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [169.526958, 280.773702] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "What is the magnetic permeability for magnetic flux density 1.91195 and magnetic field strength 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [1.911952, 82.240072] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Tell me the magnetic permeability with magnetic flux density of 1.91195 and magnetic field strength of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [1.911952, 82.240072] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "What is the magnetic permeability for magnetic flux density 1.64379 and magnetic field strength 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [1.643795, 252.709024] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Tell me the magnetic permeability with magnetic flux density of 1.64379 and magnetic field strength of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [1.643795, 252.709024] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "What is the magnetic permeability for magnetic flux density 18.3169 and magnetic field strength 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [18.316867, 6.838598] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Tell me the magnetic permeability with magnetic flux density of 18.3169 and magnetic field strength of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [18.316867, 6.838598] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "What is the magnetic permeability for magnetic flux density 156.502 and magnetic field strength 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [156.50181, 185.886273] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Tell me the magnetic permeability with magnetic flux density of 156.502 and magnetic field strength of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [156.50181, 185.886273] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "What is the magnetic permeability for magnetic flux density 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic field strength is not given" + }, + { + "q": "Tell me the magnetic permeability with magnetic flux density of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic field strength is not given" + }, + { + "q": "What is the magnetic permeability 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 magnetic permeability 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 magnetic permeability with magnetic flux density of -1.91195 and magnetic field strength of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative magnetic flux density: no example had one" + }, + { + "q": "Which magnetic permeability 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 magnetic flux density is 8.50728 and the magnetic field strength is 17.7888, what is the magnetic permeability?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [8.507283, 17.788778] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Could you work out the magnetic permeability given magnetic flux density = 8.50728, magnetic field strength = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [8.507283, 17.788778] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "I need the magnetic permeability for a magnetic flux density of 8.24951 and a magnetic field strength of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [8.24951, 0.634021] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "If the magnetic flux density is 8.24951 and the magnetic field strength is 0.634021, what is the magnetic permeability?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [8.24951, 0.634021] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Could you work out the magnetic permeability given magnetic flux density = 0.318734, magnetic field strength = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [0.318734, 9.566071] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "I need the magnetic permeability for a magnetic flux density of 0.318734 and a magnetic field strength of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [0.318734, 9.566071] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "If the magnetic flux density is 0.671697 and the magnetic field strength is 14.2496, what is the magnetic permeability?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [0.671697, 14.249569] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Could you work out the magnetic permeability given magnetic flux density = 0.671697, magnetic field strength = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [0.671697, 14.249569] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "I need the magnetic permeability for a magnetic flux density of 21.7931 and a magnetic field strength of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [21.793126, 29.510609] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "If the magnetic flux density is 21.7931 and the magnetic field strength is 29.5106, what is the magnetic permeability?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [21.793126, 29.510609] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "Could you work out the magnetic permeability given magnetic flux density = 153.15, magnetic field strength = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [153.149965, 18.595315] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "I need the magnetic permeability for a magnetic flux density of 153.15 and a magnetic field strength of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [153.149965, 18.595315] (the system magnetic permeability (Wikidata Q28352))" + }, + { + "q": "If the magnetic flux density is 8.50728, what is the magnetic permeability?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic field strength is not given" + }, + { + "q": "Could you work out the magnetic permeability given magnetic flux density = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic field strength is not given" + }, + { + "q": "Do you know the magnetic permeability 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 magnetic permeability 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 magnetic permeability given magnetic flux density = -0.318734, magnetic field strength = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative magnetic flux density: no example had one" + }, + { + "q": "What is a typical magnetic permeability?", + "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-magnetic-permeability-4/program.py b/root/organs10/physics/specialists/solved-magnetic-permeability-4/program.py new file mode 100644 index 0000000000000000000000000000000000000000..a7889390960976ea81b6e4d84fd1d2addcf5bc23 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-permeability-4/program.py @@ -0,0 +1,7 @@ +"""magnetic permeability from magnetic flux density, magnetic field strength (magnetic permeability (Wikidata Q28352)) +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(magnetic_flux_density, magnetic_field_strength): + return 1.0*magnetic_flux_density/magnetic_field_strength diff --git a/root/organs10/physics/specialists/solved-magnetic-permeability-4/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-permeability-4/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-permeability-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-magnetic-permeability-5/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-permeability-5/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-permeability-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-magnetic-permeability-5/panel.json b/root/organs10/physics/specialists/solved-magnetic-permeability-5/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..4381aaa3d8162460badace1638b0c171f72278a6 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-permeability-5/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic permeability for Alfvén number 107.961, speed 59.4105, magnetic flux density 5.77717 and mass density 1.89126?", + "a": "58.27522051995708", + "valid": true, + "kind": "compute", + "expect": 58.27522051995708, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [107.960666, 59.4105, 5.777165, 1.891256] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Tell me the magnetic permeability with Alfvén number of 107.961, speed of 59.4105, magnetic flux density of 5.77717 and mass density of 1.89126.", + "a": "58.27522051995708", + "valid": true, + "kind": "compute", + "expect": 58.27522051995708, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [107.960666, 59.4105, 5.777165, 1.891256] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "What is the magnetic permeability for Alfvén number 2.69428, speed 48.921, magnetic flux density 157.223 and mass density 35.6412?", + "a": "2.103646966548199", + "valid": true, + "kind": "compute", + "expect": 2.103646966548199, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [2.69428, 48.920991, 157.22256, 35.641157] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Tell me the magnetic permeability with Alfvén number of 2.69428, speed of 48.921, magnetic flux density of 157.223 and mass density of 35.6412.", + "a": "2.103646966548199", + "valid": true, + "kind": "compute", + "expect": 2.103646966548199, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [2.69428, 48.920991, 157.22256, 35.641157] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "What is the magnetic permeability for Alfvén number 0.318734, speed 9.56607, magnetic flux density 126.709 and mass density 1.70503?", + "a": "10.453789225532404", + "valid": true, + "kind": "compute", + "expect": 10.453789225532404, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [0.318734, 9.566071, 126.709252, 1.705032] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Tell me the magnetic permeability with Alfvén number of 0.318734, speed of 9.56607, magnetic flux density of 126.709 and mass density of 1.70503.", + "a": "10.453789225532404", + "valid": true, + "kind": "compute", + "expect": 10.453789225532404, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [0.318734, 9.566071, 126.709252, 1.705032] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "What is the magnetic permeability for Alfvén number 87.781, speed 13.2084, magnetic flux density 246.329 and mass density 20.3966?", + "a": "131393.51469070322", + "valid": true, + "kind": "compute", + "expect": 131393.51469070322, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [87.781013, 13.208419, 246.329053, 20.396558] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Tell me the magnetic permeability with Alfvén number of 87.781, speed of 13.2084, magnetic flux density of 246.329 and mass density of 20.3966.", + "a": "131393.51469070322", + "valid": true, + "kind": "compute", + "expect": 131393.51469070322, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [87.781013, 13.208419, 246.329053, 20.396558] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "What is the magnetic permeability for Alfvén number 156.502, speed 185.886, magnetic flux density 13.2373 and mass density 4.71941?", + "a": "26.318157899759974", + "valid": true, + "kind": "compute", + "expect": 26.318157899759974, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [156.50181, 185.886273, 13.237311, 4.719415] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Tell me the magnetic permeability with Alfvén number of 156.502, speed of 185.886, magnetic flux density of 13.2373 and mass density of 4.71941.", + "a": "26.318157899759974", + "valid": true, + "kind": "compute", + "expect": 26.318157899759974, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [156.50181, 185.886273, 13.237311, 4.719415] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "What is the magnetic permeability for Alfvén number 24.5721, speed 108.451, magnetic flux density 1.69476 and mass density 49.4843?", + "a": "0.002979645918447246", + "valid": true, + "kind": "compute", + "expect": 0.002979645918447246, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [24.572086, 108.450792, 1.694755, 49.484326] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Tell me the magnetic permeability with Alfvén number of 24.5721, speed of 108.451, magnetic flux density of 1.69476 and mass density of 49.4843.", + "a": "0.002979645918447246", + "valid": true, + "kind": "compute", + "expect": 0.002979645918447246, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [24.572086, 108.450792, 1.694755, 49.484326] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "What is the magnetic permeability for Alfvén number 107.961, speed 59.4105 and magnetic flux density 5.77717?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Tell me the magnetic permeability with Alfvén number of 2.69428, speed of 48.921 and magnetic flux density of 157.223.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "What is the magnetic permeability 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 magnetic permeability 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 magnetic permeability with Alfvén number of -0.318734, speed of 9.56607, magnetic flux density of 126.709 and mass density of 1.70503.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Alfvén number: no example had one" + }, + { + "q": "Which magnetic permeability 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 Alfvén number is 167.625, speed is 10.3291, magnetic flux density is 2.21571 and the mass density is 58.5346, what is the magnetic permeability?", + "a": "22.088724076337826", + "valid": true, + "kind": "compute", + "expect": 22.088724076337826, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [167.624948, 10.329054, 2.215713, 58.534594] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Could you work out the magnetic permeability given Alfvén number = 167.625, speed = 10.3291, magnetic flux density = 2.21571, mass density = 58.5346?", + "a": "22.088724076337826", + "valid": true, + "kind": "compute", + "expect": 22.088724076337826, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [167.624948, 10.329054, 2.215713, 58.534594] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "I need the magnetic permeability for an Alfvén number of 8.53158, a speed of 124.718, a magnetic flux density of 1.91195 and a mass density of 82.2401, please.", + "a": "0.00020800294023674552", + "valid": true, + "kind": "compute", + "expect": 0.00020800294023674552, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [8.531577, 124.718185, 1.911952, 82.240072] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "If the Alfvén number is 8.53158, speed is 124.718, magnetic flux density is 1.91195 and the mass density is 82.2401, what is the magnetic permeability?", + "a": "0.00020800294023674552", + "valid": true, + "kind": "compute", + "expect": 0.00020800294023674552, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [8.531577, 124.718185, 1.911952, 82.240072] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Could you work out the magnetic permeability given Alfvén number = 81.1967, speed = 6.98086, magnetic flux density = 0.551236, mass density = 2.88512?", + "a": "14.2485438856672", + "valid": true, + "kind": "compute", + "expect": 14.2485438856672, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [81.196705, 6.980857, 0.551236, 2.88512] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "I need the magnetic permeability for an Alfvén number of 81.1967, a speed of 6.98086, a magnetic flux density of 0.551236 and a mass density of 2.88512, please.", + "a": "14.2485438856672", + "valid": true, + "kind": "compute", + "expect": 14.2485438856672, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [81.196705, 6.980857, 0.551236, 2.88512] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "If the Alfvén number is 1.16997, speed is 1.14865, magnetic flux density is 21.7931 and the mass density is 29.5106, what is the magnetic permeability?", + "a": "16.696741138284228", + "valid": true, + "kind": "compute", + "expect": 16.696741138284228, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [1.169968, 1.148652, 21.793126, 29.510609] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Could you work out the magnetic permeability given Alfvén number = 1.16997, speed = 1.14865, magnetic flux density = 21.7931, mass density = 29.5106?", + "a": "16.696741138284228", + "valid": true, + "kind": "compute", + "expect": 16.696741138284228, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [1.169968, 1.148652, 21.793126, 29.510609] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "I need the magnetic permeability for an Alfvén number of 223.509, a speed of 17.3414, a magnetic flux density of 7.10705 and a mass density of 30.2507, please.", + "a": "277.37140016922496", + "valid": true, + "kind": "compute", + "expect": 277.37140016922496, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [223.508796, 17.341435, 7.107049, 30.250749] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "If the Alfvén number is 223.509, speed is 17.3414, magnetic flux density is 7.10705 and the mass density is 30.2507, what is the magnetic permeability?", + "a": "277.37140016922496", + "valid": true, + "kind": "compute", + "expect": 277.37140016922496, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [223.508796, 17.341435, 7.107049, 30.250749] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Could you work out the magnetic permeability given Alfvén number = 0.869111, speed = 39.1753, magnetic flux density = 0.432217, mass density = 16.6571?", + "a": "5.519927697130205e-06", + "valid": true, + "kind": "compute", + "expect": 5.519927697130205e-06, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [0.869111, 39.175263, 0.432217, 16.657051] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "I need the magnetic permeability for an Alfvén number of 0.869111, a speed of 39.1753, a magnetic flux density of 0.432217 and a mass density of 16.6571, please.", + "a": "5.519927697130205e-06", + "valid": true, + "kind": "compute", + "expect": 5.519927697130205e-06, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [0.869111, 39.175263, 0.432217, 16.657051] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "If the Alfvén number is 167.625, speed is 10.3291 and the magnetic flux density is 2.21571, what is the magnetic permeability?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Could you work out the magnetic permeability given Alfvén number = 8.53158, speed = 124.718, magnetic flux density = 1.91195?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Do you know the magnetic permeability 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 magnetic permeability 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 magnetic permeability given Alfvén number = -81.1967, speed = 6.98086, magnetic flux density = 0.551236, mass density = 2.88512?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Alfvén number: no example had one" + }, + { + "q": "What is a typical magnetic permeability?", + "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-magnetic-permeability-5/program.py b/root/organs10/physics/specialists/solved-magnetic-permeability-5/program.py new file mode 100644 index 0000000000000000000000000000000000000000..d224449859d910ff76a615e98c3aaea8b7a1b4c0 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-permeability-5/program.py @@ -0,0 +1,7 @@ +"""magnetic permeability from Alfvén number, speed, magnetic flux density, mass density (Alfvén number (Wikidata Q3342997)) +The maths organ (systems.py): solved from the system q0 = ((q1)/(q2 / sqrt(q3*q4))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(alfv_n_number, speed, magnetic_flux_density, mass_density): + return 1.0*alfv_n_number**2*magnetic_flux_density**2/(mass_density*speed**2) diff --git a/root/organs10/physics/specialists/solved-magnetic-permeability-5/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-permeability-5/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-permeability-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-magnetic-permeability/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-permeability/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-permeability/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-magnetic-permeability/panel.json b/root/organs10/physics/specialists/solved-magnetic-permeability/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..ac8a77ccdf5c5b51dbf9776fdc2b31cc85213e3a --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-permeability/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic permeability for magnetic pressure number 107.961, pressure 59.4105 and magnetic flux density 5.77717?", + "a": "30.32507160910141", + "valid": true, + "kind": "compute", + "expect": 30.32507160910141, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [107.960666, 59.4105, 5.777165] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Tell me the magnetic permeability with magnetic pressure number of 107.961, pressure of 59.4105 and magnetic flux density of 5.77717.", + "a": "30.32507160910141", + "valid": true, + "kind": "compute", + "expect": 30.32507160910141, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [107.960666, 59.4105, 5.777165] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "What is the magnetic permeability for magnetic pressure number 85.2416, pressure 160.878 and magnetic flux density 2.69428?", + "a": "1.9231328083280346", + "valid": true, + "kind": "compute", + "expect": 1.9231328083280346, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [85.241623, 160.878442, 2.69428] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Tell me the magnetic permeability with magnetic pressure number of 85.2416, pressure of 160.878 and magnetic flux density of 2.69428.", + "a": "1.9231328083280346", + "valid": true, + "kind": "compute", + "expect": 1.9231328083280346, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [85.241623, 160.878442, 2.69428] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "What is the magnetic permeability for magnetic pressure number 45.6157, pressure 4.97131 and magnetic flux density 94.305?", + "a": "40802.173643709364", + "valid": true, + "kind": "compute", + "expect": 40802.173643709364, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [45.61567, 4.971307, 94.305046] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Tell me the magnetic permeability with magnetic pressure number of 45.6157, pressure of 4.97131 and magnetic flux density of 94.305.", + "a": "40802.173643709364", + "valid": true, + "kind": "compute", + "expect": 40802.173643709364, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [45.61567, 4.971307, 94.305046] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "What is the magnetic permeability for magnetic pressure number 10.5638, pressure 198.839 and magnetic flux density 0.671697?", + "a": "0.011984986103356994", + "valid": true, + "kind": "compute", + "expect": 0.011984986103356994, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [10.563843, 198.838955, 0.671697] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Tell me the magnetic permeability with magnetic pressure number of 10.5638, pressure of 198.839 and magnetic flux density of 0.671697.", + "a": "0.011984986103356994", + "valid": true, + "kind": "compute", + "expect": 0.011984986103356994, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [10.563843, 198.838955, 0.671697] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "What is the magnetic permeability for magnetic pressure number 21.7931, pressure 29.5106 and magnetic flux density 8.50341?", + "a": "26.699143467388478", + "valid": true, + "kind": "compute", + "expect": 26.699143467388478, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [21.793126, 29.510609, 8.503407] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Tell me the magnetic permeability with magnetic pressure number of 21.7931, pressure of 29.5106 and magnetic flux density of 8.50341.", + "a": "26.699143467388478", + "valid": true, + "kind": "compute", + "expect": 26.699143467388478, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [21.793126, 29.510609, 8.503407] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "What is the magnetic permeability for magnetic pressure number 223.509, pressure 17.3414 and magnetic flux density 7.10705?", + "a": "325.5054253726801", + "valid": true, + "kind": "compute", + "expect": 325.5054253726801, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [223.508796, 17.341435, 7.107049] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Tell me the magnetic permeability with magnetic pressure number of 223.509, pressure of 17.3414 and magnetic flux density of 7.10705.", + "a": "325.5054253726801", + "valid": true, + "kind": "compute", + "expect": 325.5054253726801, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [223.508796, 17.341435, 7.107049] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "What is the magnetic permeability for magnetic pressure number 107.961 and pressure 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic flux density is not given" + }, + { + "q": "Tell me the magnetic permeability with magnetic pressure number of 85.2416 and pressure of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic flux density is not given" + }, + { + "q": "What is the magnetic permeability 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 magnetic permeability 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 magnetic permeability with magnetic pressure number of -45.6157, pressure of 4.97131 and magnetic flux density of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative magnetic pressure number: no example had one" + }, + { + "q": "Which magnetic permeability 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 magnetic pressure number is 17.7888, pressure is 167.625 and the magnetic flux density is 10.3291, what is the magnetic permeability?", + "a": "5.661071003489753", + "valid": true, + "kind": "compute", + "expect": 5.661071003489753, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [17.788778, 167.624948, 10.329054] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Could you work out the magnetic permeability given magnetic pressure number = 17.7888, pressure = 167.625, magnetic flux density = 10.3291?", + "a": "5.661071003489753", + "valid": true, + "kind": "compute", + "expect": 5.661071003489753, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [17.788778, 167.624948, 10.329054] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "I need the magnetic permeability for a magnetic pressure number of 21.511, a pressure of 173.394 and a magnetic flux density of 251.084, please.", + "a": "3910.5331480078466", + "valid": true, + "kind": "compute", + "expect": 3910.5331480078466, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [21.511016, 173.393638, 251.084037] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "If the magnetic pressure number is 21.511, pressure is 173.394 and the magnetic flux density is 251.084, what is the magnetic permeability?", + "a": "3910.5331480078466", + "valid": true, + "kind": "compute", + "expect": 3910.5331480078466, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [21.511016, 173.393638, 251.084037] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Could you work out the magnetic permeability given magnetic pressure number = 129.245, pressure = 1.18359, magnetic flux density = 15.9416?", + "a": "13875.443389225476", + "valid": true, + "kind": "compute", + "expect": 13875.443389225476, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [129.244984, 1.18359, 15.941612] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "I need the magnetic permeability for a magnetic pressure number of 129.245, a pressure of 1.18359 and a magnetic flux density of 15.9416, please.", + "a": "13875.443389225476", + "valid": true, + "kind": "compute", + "expect": 13875.443389225476, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [129.244984, 1.18359, 15.941612] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "If the magnetic pressure number is 20.3966, pressure is 18.3169 and the magnetic flux density is 6.8386, what is the magnetic permeability?", + "a": "26.03813374028582", + "valid": true, + "kind": "compute", + "expect": 26.03813374028582, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [20.396558, 18.316867, 6.838598] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Could you work out the magnetic permeability given magnetic pressure number = 20.3966, pressure = 18.3169, magnetic flux density = 6.8386?", + "a": "26.03813374028582", + "valid": true, + "kind": "compute", + "expect": 26.03813374028582, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [20.396558, 18.316867, 6.838598] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "I need the magnetic permeability for a magnetic pressure number of 185.886, a pressure of 13.2373 and a magnetic flux density of 4.71941, please.", + "a": "156.38455201940317", + "valid": true, + "kind": "compute", + "expect": 156.38455201940317, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [185.886273, 13.237311, 4.719415] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "If the magnetic pressure number is 185.886, pressure is 13.2373 and the magnetic flux density is 4.71941, what is the magnetic permeability?", + "a": "156.38455201940317", + "valid": true, + "kind": "compute", + "expect": 156.38455201940317, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [185.886273, 13.237311, 4.719415] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Could you work out the magnetic permeability given magnetic pressure number = 9.1061, pressure = 24.5721, magnetic flux density = 108.451?", + "a": "2179.345284040078", + "valid": true, + "kind": "compute", + "expect": 2179.345284040078, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [9.106104, 24.572086, 108.450792] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "I need the magnetic permeability for a magnetic pressure number of 9.1061, a pressure of 24.5721 and a magnetic flux density of 108.451, please.", + "a": "2179.345284040078", + "valid": true, + "kind": "compute", + "expect": 2179.345284040078, + "dtype": "quantity", + "unit": "m*kg/s^2/A^2", + "why": "magnetic permeability for [9.106104, 24.572086, 108.450792] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "If the magnetic pressure number is 17.7888 and the pressure is 167.625, what is the magnetic permeability?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic flux density is not given" + }, + { + "q": "Could you work out the magnetic permeability given magnetic pressure number = 21.511, pressure = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic flux density is not given" + }, + { + "q": "Do you know the magnetic permeability 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 magnetic permeability 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 magnetic permeability given magnetic pressure number = -129.245, pressure = 1.18359, magnetic flux density = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative magnetic pressure number: no example had one" + }, + { + "q": "What is a typical magnetic permeability?", + "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-magnetic-permeability/program.py b/root/organs10/physics/specialists/solved-magnetic-permeability/program.py new file mode 100644 index 0000000000000000000000000000000000000000..c10b0514df516c3ff2188eef83d1cd913167ffcc --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-permeability/program.py @@ -0,0 +1,7 @@ +"""magnetic permeability from magnetic pressure number, pressure, magnetic flux density (magnetic pressure number (Wikidata Q105102313)) +The maths organ (systems.py): solved from the system q0 = ((2*q1*q2)/(q3 **(2))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(magnetic_pressure_number, pressure, magnetic_flux_density): + return 0.5*magnetic_flux_density**2*magnetic_pressure_number/pressure diff --git a/root/organs10/physics/specialists/solved-magnetic-permeability/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-permeability/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-permeability/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-magnetic-pressure-number/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-pressure-number/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-pressure-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-magnetic-pressure-number/panel.json b/root/organs10/physics/specialists/solved-magnetic-pressure-number/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..c551714bf88432af82467a64df096383ce89387a --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-pressure-number/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic pressure number for pressure 107.961, magnetic permeability 59.4105 and magnetic flux density 5.77717?", + "a": "384.35211397508664", + "valid": true, + "kind": "compute", + "expect": 384.35211397508664, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure number for [107.960666, 59.4105, 5.777165] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Tell me the magnetic pressure number with pressure of 107.961, magnetic permeability of 59.4105 and magnetic flux density of 5.77717.", + "a": "384.35211397508664", + "valid": true, + "kind": "compute", + "expect": 384.35211397508664, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure number for [107.960666, 59.4105, 5.777165] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "What is the magnetic pressure number for pressure 85.2416, magnetic permeability 160.878 and magnetic flux density 2.69428?", + "a": "3778.279997142926", + "valid": true, + "kind": "compute", + "expect": 3778.279997142926, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure number for [85.241623, 160.878442, 2.69428] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Tell me the magnetic pressure number with pressure of 85.2416, magnetic permeability of 160.878 and magnetic flux density of 2.69428.", + "a": "3778.279997142926", + "valid": true, + "kind": "compute", + "expect": 3778.279997142926, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure number for [85.241623, 160.878442, 2.69428] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "What is the magnetic pressure number for pressure 45.6157, magnetic permeability 4.97131 and magnetic flux density 94.305?", + "a": "0.05099702231625496", + "valid": true, + "kind": "compute", + "expect": 0.05099702231625496, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure number for [45.61567, 4.971307, 94.305046] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Tell me the magnetic pressure number with pressure of 45.6157, magnetic permeability of 4.97131 and magnetic flux density of 94.305.", + "a": "0.05099702231625496", + "valid": true, + "kind": "compute", + "expect": 0.05099702231625496, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure number for [45.61567, 4.971307, 94.305046] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "What is the magnetic pressure number for pressure 10.5638, magnetic permeability 198.839 and magnetic flux density 0.671697?", + "a": "9311.214260483912", + "valid": true, + "kind": "compute", + "expect": 9311.214260483912, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure number for [10.563843, 198.838955, 0.671697] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Tell me the magnetic pressure number with pressure of 10.5638, magnetic permeability of 198.839 and magnetic flux density of 0.671697.", + "a": "9311.214260483912", + "valid": true, + "kind": "compute", + "expect": 9311.214260483912, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure number for [10.563843, 198.838955, 0.671697] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "What is the magnetic pressure number for pressure 21.7931, magnetic permeability 29.5106 and magnetic flux density 8.50341?", + "a": "17.788599206895288", + "valid": true, + "kind": "compute", + "expect": 17.788599206895288, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure number for [21.793126, 29.510609, 8.503407] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Tell me the magnetic pressure number with pressure of 21.7931, magnetic permeability of 29.5106 and magnetic flux density of 8.50341.", + "a": "17.788599206895288", + "valid": true, + "kind": "compute", + "expect": 17.788599206895288, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure number for [21.793126, 29.510609, 8.503407] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "What is the magnetic pressure number for pressure 223.509, magnetic permeability 17.3414 and magnetic flux density 7.10705?", + "a": "153.47265554808402", + "valid": true, + "kind": "compute", + "expect": 153.47265554808402, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure number for [223.508796, 17.341435, 7.107049] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Tell me the magnetic pressure number with pressure of 223.509, magnetic permeability of 17.3414 and magnetic flux density of 7.10705.", + "a": "153.47265554808402", + "valid": true, + "kind": "compute", + "expect": 153.47265554808402, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure number for [223.508796, 17.341435, 7.107049] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "What is the magnetic pressure number for pressure 107.961 and magnetic permeability 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic flux density is not given" + }, + { + "q": "Tell me the magnetic pressure number with pressure of 85.2416 and magnetic permeability of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic flux density is not given" + }, + { + "q": "What is the magnetic pressure 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 magnetic pressure 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 magnetic pressure number with pressure of -45.6157, magnetic permeability of 4.97131 and magnetic flux density of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative pressure: no example had one" + }, + { + "q": "Which magnetic pressure 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 pressure is 17.7888, magnetic permeability is 167.625 and the magnetic flux density is 10.3291, what is the magnetic pressure number?", + "a": "55.89766089759717", + "valid": true, + "kind": "compute", + "expect": 55.89766089759717, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure number for [17.788778, 167.624948, 10.329054] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Could you work out the magnetic pressure number given pressure = 17.7888, magnetic permeability = 167.625, magnetic flux density = 10.3291?", + "a": "55.89766089759717", + "valid": true, + "kind": "compute", + "expect": 55.89766089759717, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure number for [17.788778, 167.624948, 10.329054] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "I need the magnetic pressure number for a pressure of 21.511, a magnetic permeability of 173.394 and a magnetic flux density of 251.084, please.", + "a": "0.11832754908616887", + "valid": true, + "kind": "compute", + "expect": 0.11832754908616887, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure number for [21.511016, 173.393638, 251.084037] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "If the pressure is 21.511, magnetic permeability is 173.394 and the magnetic flux density is 251.084, what is the magnetic pressure number?", + "a": "0.11832754908616887", + "valid": true, + "kind": "compute", + "expect": 0.11832754908616887, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure number for [21.511016, 173.393638, 251.084037] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Could you work out the magnetic pressure number given pressure = 129.245, magnetic permeability = 1.18359, magnetic flux density = 15.9416?", + "a": "1.203872579761125", + "valid": true, + "kind": "compute", + "expect": 1.203872579761125, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure number for [129.244984, 1.18359, 15.941612] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "I need the magnetic pressure number for a pressure of 129.245, a magnetic permeability of 1.18359 and a magnetic flux density of 15.9416, please.", + "a": "1.203872579761125", + "valid": true, + "kind": "compute", + "expect": 1.203872579761125, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure number for [129.244984, 1.18359, 15.941612] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "If the pressure is 20.3966, magnetic permeability is 18.3169 and the magnetic flux density is 6.8386, what is the magnetic pressure number?", + "a": "15.977319298121383", + "valid": true, + "kind": "compute", + "expect": 15.977319298121383, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure number for [20.396558, 18.316867, 6.838598] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Could you work out the magnetic pressure number given pressure = 20.3966, magnetic permeability = 18.3169, magnetic flux density = 6.8386?", + "a": "15.977319298121383", + "valid": true, + "kind": "compute", + "expect": 15.977319298121383, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure number for [20.396558, 18.316867, 6.838598] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "I need the magnetic pressure number for a pressure of 185.886, a magnetic permeability of 13.2373 and a magnetic flux density of 4.71941, please.", + "a": "220.9534507484427", + "valid": true, + "kind": "compute", + "expect": 220.9534507484427, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure number for [185.886273, 13.237311, 4.719415] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "If the pressure is 185.886, magnetic permeability is 13.2373 and the magnetic flux density is 4.71941, what is the magnetic pressure number?", + "a": "220.9534507484427", + "valid": true, + "kind": "compute", + "expect": 220.9534507484427, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure number for [185.886273, 13.237311, 4.719415] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "Could you work out the magnetic pressure number given pressure = 9.1061, magnetic permeability = 24.5721, magnetic flux density = 108.451?", + "a": "0.038048642689099736", + "valid": true, + "kind": "compute", + "expect": 0.038048642689099736, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure number for [9.106104, 24.572086, 108.450792] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "I need the magnetic pressure number for a pressure of 9.1061, a magnetic permeability of 24.5721 and a magnetic flux density of 108.451, please.", + "a": "0.038048642689099736", + "valid": true, + "kind": "compute", + "expect": 0.038048642689099736, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure number for [9.106104, 24.572086, 108.450792] (the system magnetic pressure number (Wikidata Q105102313))" + }, + { + "q": "If the pressure is 17.7888 and the magnetic permeability is 167.625, what is the magnetic pressure number?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic flux density is not given" + }, + { + "q": "Could you work out the magnetic pressure number given pressure = 21.511, magnetic permeability = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic flux density is not given" + }, + { + "q": "Do you know the magnetic pressure 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 magnetic pressure 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 magnetic pressure number given pressure = -129.245, magnetic permeability = 1.18359, magnetic flux density = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative pressure: no example had one" + }, + { + "q": "What is a typical magnetic pressure 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-magnetic-pressure-number/program.py b/root/organs10/physics/specialists/solved-magnetic-pressure-number/program.py new file mode 100644 index 0000000000000000000000000000000000000000..85f698e4afdbb1097f7634b83299fe1e5df28c5a --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-pressure-number/program.py @@ -0,0 +1,7 @@ +"""magnetic pressure number from pressure, magnetic permeability, magnetic flux density (magnetic pressure number (Wikidata Q105102313)) +The maths organ (systems.py): solved from the system q0 = ((2*q1*q2)/(q3 **(2))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(pressure, magnetic_permeability, magnetic_flux_density): + return 2.0*magnetic_permeability*pressure/magnetic_flux_density**2 diff --git a/root/organs10/physics/specialists/solved-magnetic-pressure-number/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-pressure-number/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-pressure-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-magnetic-pressure-ratio/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-pressure-ratio/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-pressure-ratio/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-magnetic-pressure-ratio/panel.json b/root/organs10/physics/specialists/solved-magnetic-pressure-ratio/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..63ea6543394386a3bcf9dc65c2eff1ff957fae85 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-pressure-ratio/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic pressure ratio for magnetic pressure 107.961 and plasma pressure 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure ratio for [107.960666, 59.4105] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "Tell me the magnetic pressure ratio with magnetic pressure of 107.961 and plasma pressure of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure ratio for [107.960666, 59.4105] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "What is the magnetic pressure ratio for magnetic pressure 169.527 and plasma pressure 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure ratio for [169.526958, 280.773702] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "Tell me the magnetic pressure ratio with magnetic pressure of 169.527 and plasma pressure of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure ratio for [169.526958, 280.773702] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "What is the magnetic pressure ratio for magnetic pressure 1.91195 and plasma pressure 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure ratio for [1.911952, 82.240072] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "Tell me the magnetic pressure ratio with magnetic pressure of 1.91195 and plasma pressure of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure ratio for [1.911952, 82.240072] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "What is the magnetic pressure ratio for magnetic pressure 1.64379 and plasma pressure 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure ratio for [1.643795, 252.709024] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "Tell me the magnetic pressure ratio with magnetic pressure of 1.64379 and plasma pressure of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure ratio for [1.643795, 252.709024] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "What is the magnetic pressure ratio for magnetic pressure 18.3169 and plasma pressure 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure ratio for [18.316867, 6.838598] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "Tell me the magnetic pressure ratio with magnetic pressure of 18.3169 and plasma pressure of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure ratio for [18.316867, 6.838598] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "What is the magnetic pressure ratio for magnetic pressure 156.502 and plasma pressure 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure ratio for [156.50181, 185.886273] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "Tell me the magnetic pressure ratio with magnetic pressure of 156.502 and plasma pressure of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure ratio for [156.50181, 185.886273] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "What is the magnetic pressure ratio for magnetic pressure 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the plasma pressure is not given" + }, + { + "q": "Tell me the magnetic pressure ratio with magnetic pressure of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the plasma pressure is not given" + }, + { + "q": "What is the magnetic pressure ratio 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 magnetic pressure ratio 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 magnetic pressure ratio with magnetic pressure of -1.91195 and plasma pressure of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative magnetic pressure: no example had one" + }, + { + "q": "Which magnetic pressure ratio 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 magnetic pressure is 8.50728 and the plasma pressure is 17.7888, what is the magnetic pressure ratio?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure ratio for [8.507283, 17.788778] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "Could you work out the magnetic pressure ratio given magnetic pressure = 8.50728, plasma pressure = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure ratio for [8.507283, 17.788778] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "I need the magnetic pressure ratio for a magnetic pressure of 8.24951 and a plasma pressure of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure ratio for [8.24951, 0.634021] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "If the magnetic pressure is 8.24951 and the plasma pressure is 0.634021, what is the magnetic pressure ratio?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure ratio for [8.24951, 0.634021] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "Could you work out the magnetic pressure ratio given magnetic pressure = 0.318734, plasma pressure = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure ratio for [0.318734, 9.566071] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "I need the magnetic pressure ratio for a magnetic pressure of 0.318734 and a plasma pressure of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure ratio for [0.318734, 9.566071] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "If the magnetic pressure is 0.671697 and the plasma pressure is 14.2496, what is the magnetic pressure ratio?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure ratio for [0.671697, 14.249569] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "Could you work out the magnetic pressure ratio given magnetic pressure = 0.671697, plasma pressure = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure ratio for [0.671697, 14.249569] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "I need the magnetic pressure ratio for a magnetic pressure of 21.7931 and a plasma pressure of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure ratio for [21.793126, 29.510609] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "If the magnetic pressure is 21.7931 and the plasma pressure is 29.5106, what is the magnetic pressure ratio?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure ratio for [21.793126, 29.510609] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "Could you work out the magnetic pressure ratio given magnetic pressure = 153.15, plasma pressure = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure ratio for [153.149965, 18.595315] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "I need the magnetic pressure ratio for a magnetic pressure of 153.15 and a plasma pressure of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "1", + "why": "magnetic pressure ratio for [153.149965, 18.595315] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "If the magnetic pressure is 8.50728, what is the magnetic pressure ratio?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the plasma pressure is not given" + }, + { + "q": "Could you work out the magnetic pressure ratio given magnetic pressure = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the plasma pressure is not given" + }, + { + "q": "Do you know the magnetic pressure ratio 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 magnetic pressure ratio 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 magnetic pressure ratio given magnetic pressure = -0.318734, plasma pressure = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative magnetic pressure: no example had one" + }, + { + "q": "What is a typical magnetic pressure ratio?", + "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-magnetic-pressure-ratio/program.py b/root/organs10/physics/specialists/solved-magnetic-pressure-ratio/program.py new file mode 100644 index 0000000000000000000000000000000000000000..d93a9e6efe97ae3aa57b727e7990b643001fec55 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-pressure-ratio/program.py @@ -0,0 +1,7 @@ +"""magnetic pressure ratio from magnetic pressure, plasma pressure (magnetic pressure ratio (Wikidata Q2098466)) +The maths organ (systems.py): solved from the system q1 = ((q2)/(q0)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(magnetic_pressure, plasma_pressure): + return 1.0*plasma_pressure/magnetic_pressure diff --git a/root/organs10/physics/specialists/solved-magnetic-pressure-ratio/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-pressure-ratio/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-pressure-ratio/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-magnetic-pressure/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-pressure/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-pressure/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-magnetic-pressure/panel.json b/root/organs10/physics/specialists/solved-magnetic-pressure/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..4a728d84a191a4dafae5be5e2f9da1b883196b7c --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-pressure/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic pressure for magnetic pressure ratio 107.961 and plasma pressure 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "Pa", + "why": "magnetic pressure for [107.960666, 59.4105] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "Tell me the magnetic pressure with magnetic pressure ratio of 107.961 and plasma pressure of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "Pa", + "why": "magnetic pressure for [107.960666, 59.4105] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "What is the magnetic pressure for magnetic pressure ratio 169.527 and plasma pressure 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "Pa", + "why": "magnetic pressure for [169.526958, 280.773702] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "Tell me the magnetic pressure with magnetic pressure ratio of 169.527 and plasma pressure of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "Pa", + "why": "magnetic pressure for [169.526958, 280.773702] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "What is the magnetic pressure for magnetic pressure ratio 1.91195 and plasma pressure 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "Pa", + "why": "magnetic pressure for [1.911952, 82.240072] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "Tell me the magnetic pressure with magnetic pressure ratio of 1.91195 and plasma pressure of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "Pa", + "why": "magnetic pressure for [1.911952, 82.240072] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "What is the magnetic pressure for magnetic pressure ratio 1.64379 and plasma pressure 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "Pa", + "why": "magnetic pressure for [1.643795, 252.709024] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "Tell me the magnetic pressure with magnetic pressure ratio of 1.64379 and plasma pressure of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "Pa", + "why": "magnetic pressure for [1.643795, 252.709024] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "What is the magnetic pressure for magnetic pressure ratio 18.3169 and plasma pressure 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "Pa", + "why": "magnetic pressure for [18.316867, 6.838598] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "Tell me the magnetic pressure with magnetic pressure ratio of 18.3169 and plasma pressure of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "Pa", + "why": "magnetic pressure for [18.316867, 6.838598] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "What is the magnetic pressure for magnetic pressure ratio 156.502 and plasma pressure 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "Pa", + "why": "magnetic pressure for [156.50181, 185.886273] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "Tell me the magnetic pressure with magnetic pressure ratio of 156.502 and plasma pressure of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "Pa", + "why": "magnetic pressure for [156.50181, 185.886273] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "What is the magnetic pressure for magnetic pressure ratio 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the plasma pressure is not given" + }, + { + "q": "Tell me the magnetic pressure with magnetic pressure ratio of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the plasma pressure is not given" + }, + { + "q": "What is the magnetic pressure 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 magnetic pressure 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 magnetic pressure with magnetic pressure ratio of -1.91195 and plasma pressure of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative magnetic pressure ratio: no example had one" + }, + { + "q": "Which magnetic pressure 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 magnetic pressure ratio is 8.50728 and the plasma pressure is 17.7888, what is the magnetic pressure?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "Pa", + "why": "magnetic pressure for [8.507283, 17.788778] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "Could you work out the magnetic pressure given magnetic pressure ratio = 8.50728, plasma pressure = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "Pa", + "why": "magnetic pressure for [8.507283, 17.788778] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "I need the magnetic pressure for a magnetic pressure ratio of 8.24951 and a plasma pressure of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "Pa", + "why": "magnetic pressure for [8.24951, 0.634021] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "If the magnetic pressure ratio is 8.24951 and the plasma pressure is 0.634021, what is the magnetic pressure?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "Pa", + "why": "magnetic pressure for [8.24951, 0.634021] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "Could you work out the magnetic pressure given magnetic pressure ratio = 0.318734, plasma pressure = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "Pa", + "why": "magnetic pressure for [0.318734, 9.566071] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "I need the magnetic pressure for a magnetic pressure ratio of 0.318734 and a plasma pressure of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "Pa", + "why": "magnetic pressure for [0.318734, 9.566071] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "If the magnetic pressure ratio is 0.671697 and the plasma pressure is 14.2496, what is the magnetic pressure?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "Pa", + "why": "magnetic pressure for [0.671697, 14.249569] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "Could you work out the magnetic pressure given magnetic pressure ratio = 0.671697, plasma pressure = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "Pa", + "why": "magnetic pressure for [0.671697, 14.249569] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "I need the magnetic pressure for a magnetic pressure ratio of 21.7931 and a plasma pressure of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "Pa", + "why": "magnetic pressure for [21.793126, 29.510609] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "If the magnetic pressure ratio is 21.7931 and the plasma pressure is 29.5106, what is the magnetic pressure?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "Pa", + "why": "magnetic pressure for [21.793126, 29.510609] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "Could you work out the magnetic pressure given magnetic pressure ratio = 153.15, plasma pressure = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "Pa", + "why": "magnetic pressure for [153.149965, 18.595315] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "I need the magnetic pressure for a magnetic pressure ratio of 153.15 and a plasma pressure of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "Pa", + "why": "magnetic pressure for [153.149965, 18.595315] (the system magnetic pressure ratio (Wikidata Q2098466))" + }, + { + "q": "If the magnetic pressure ratio is 8.50728, what is the magnetic pressure?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the plasma pressure is not given" + }, + { + "q": "Could you work out the magnetic pressure given magnetic pressure ratio = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the plasma pressure is not given" + }, + { + "q": "Do you know the magnetic pressure 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 magnetic pressure 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 magnetic pressure given magnetic pressure ratio = -0.318734, plasma pressure = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative magnetic pressure ratio: no example had one" + }, + { + "q": "What is a typical magnetic pressure?", + "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-magnetic-pressure/program.py b/root/organs10/physics/specialists/solved-magnetic-pressure/program.py new file mode 100644 index 0000000000000000000000000000000000000000..5476b6bf678be88a3681af1e52398fb48c7b633a --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-pressure/program.py @@ -0,0 +1,7 @@ +"""magnetic pressure from magnetic pressure ratio, plasma pressure (magnetic pressure ratio (Wikidata Q2098466)) +The maths organ (systems.py): solved from the system q1 = ((q2)/(q0)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(magnetic_pressure_ratio, plasma_pressure): + return 1.0*plasma_pressure/magnetic_pressure_ratio diff --git a/root/organs10/physics/specialists/solved-magnetic-pressure/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-pressure/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-pressure/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-magnetic-reluctance-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-reluctance-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-reluctance-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-magnetic-reluctance-2/panel.json b/root/organs10/physics/specialists/solved-magnetic-reluctance-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..e3055f6dad79e397b97b584855ae22ddb2672d6f --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-reluctance-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic reluctance for permeance 107.961?", + "a": "0.00926263275385168", + "valid": true, + "kind": "compute", + "expect": 0.00926263275385168, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [107.960666] (the system permeance (Wikidata Q77997985))" + }, + { + "q": "Tell me the magnetic reluctance with permeance of 107.961.", + "a": "0.00926263275385168", + "valid": true, + "kind": "compute", + "expect": 0.00926263275385168, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [107.960666] (the system permeance (Wikidata Q77997985))" + }, + { + "q": "What is the magnetic reluctance for permeance 22.652?", + "a": "0.044146167826513254", + "valid": true, + "kind": "compute", + "expect": 0.044146167826513254, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [22.652023] (the system permeance (Wikidata Q77997985))" + }, + { + "q": "Tell me the magnetic reluctance with permeance of 22.652.", + "a": "0.044146167826513254", + "valid": true, + "kind": "compute", + "expect": 0.044146167826513254, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [22.652023] (the system permeance (Wikidata Q77997985))" + }, + { + "q": "What is the magnetic reluctance for permeance 173.394?", + "a": "0.0057672242718762815", + "valid": true, + "kind": "compute", + "expect": 0.0057672242718762815, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [173.393638] (the system permeance (Wikidata Q77997985))" + }, + { + "q": "Tell me the magnetic reluctance with permeance of 173.394.", + "a": "0.0057672242718762815", + "valid": true, + "kind": "compute", + "expect": 0.0057672242718762815, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [173.393638] (the system permeance (Wikidata Q77997985))" + }, + { + "q": "What is the magnetic reluctance for permeance 126.709?", + "a": "0.007892083536070259", + "valid": true, + "kind": "compute", + "expect": 0.007892083536070259, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [126.709252] (the system permeance (Wikidata Q77997985))" + }, + { + "q": "Tell me the magnetic reluctance with permeance of 126.709.", + "a": "0.007892083536070259", + "valid": true, + "kind": "compute", + "expect": 0.007892083536070259, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [126.709252] (the system permeance (Wikidata Q77997985))" + }, + { + "q": "What is the magnetic reluctance for permeance 0.671697?", + "a": "1.4887660694527203", + "valid": true, + "kind": "compute", + "expect": 1.4887660694527203, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [0.671697] (the system permeance (Wikidata Q77997985))" + }, + { + "q": "Tell me the magnetic reluctance with permeance of 0.671697.", + "a": "1.4887660694527203", + "valid": true, + "kind": "compute", + "expect": 1.4887660694527203, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [0.671697] (the system permeance (Wikidata Q77997985))" + }, + { + "q": "What is the magnetic reluctance for permeance 1.16997?", + "a": "0.8547242274066585", + "valid": true, + "kind": "compute", + "expect": 0.8547242274066585, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [1.169968] (the system permeance (Wikidata Q77997985))" + }, + { + "q": "Tell me the magnetic reluctance with permeance of 1.16997.", + "a": "0.8547242274066585", + "valid": true, + "kind": "compute", + "expect": 0.8547242274066585, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [1.169968] (the system permeance (Wikidata Q77997985))" + }, + { + "q": "What is the magnetic reluctance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the permeance is not given" + }, + { + "q": "Could you work out the magnetic reluctance, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the permeance is not given" + }, + { + "q": "What is the magnetic reluctance 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 magnetic reluctance 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 magnetic reluctance with permeance of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative permeance: no example had one" + }, + { + "q": "Which magnetic reluctance 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 permeance is 2.57003, what is the magnetic reluctance?", + "a": "0.38910052946214563", + "valid": true, + "kind": "compute", + "expect": 0.38910052946214563, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [2.57003] (the system permeance (Wikidata Q77997985))" + }, + { + "q": "Could you work out the magnetic reluctance given permeance = 2.57003?", + "a": "0.38910052946214563", + "valid": true, + "kind": "compute", + "expect": 0.38910052946214563, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [2.57003] (the system permeance (Wikidata Q77997985))" + }, + { + "q": "I need the magnetic reluctance for a permeance of 48.921, please.", + "a": "0.02044112317474344", + "valid": true, + "kind": "compute", + "expect": 0.02044112317474344, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [48.920991] (the system permeance (Wikidata Q77997985))" + }, + { + "q": "If the permeance is 48.921, what is the magnetic reluctance?", + "a": "0.02044112317474344", + "valid": true, + "kind": "compute", + "expect": 0.02044112317474344, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [48.920991] (the system permeance (Wikidata Q77997985))" + }, + { + "q": "Could you work out the magnetic reluctance given permeance = 0.348438?", + "a": "2.869953759769417", + "valid": true, + "kind": "compute", + "expect": 2.869953759769417, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [0.348438] (the system permeance (Wikidata Q77997985))" + }, + { + "q": "I need the magnetic reluctance for a permeance of 0.348438, please.", + "a": "2.869953759769417", + "valid": true, + "kind": "compute", + "expect": 2.869953759769417, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [0.348438] (the system permeance (Wikidata Q77997985))" + }, + { + "q": "If the permeance is 252.709, what is the magnetic reluctance?", + "a": "0.0039571202610184176", + "valid": true, + "kind": "compute", + "expect": 0.0039571202610184176, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [252.709024] (the system permeance (Wikidata Q77997985))" + }, + { + "q": "Could you work out the magnetic reluctance given permeance = 252.709?", + "a": "0.0039571202610184176", + "valid": true, + "kind": "compute", + "expect": 0.0039571202610184176, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [252.709024] (the system permeance (Wikidata Q77997985))" + }, + { + "q": "I need the magnetic reluctance for a permeance of 20.3966, please.", + "a": "0.04902788018766951", + "valid": true, + "kind": "compute", + "expect": 0.04902788018766951, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [20.396558] (the system permeance (Wikidata Q77997985))" + }, + { + "q": "If the permeance is 20.3966, what is the magnetic reluctance?", + "a": "0.04902788018766951", + "valid": true, + "kind": "compute", + "expect": 0.04902788018766951, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [20.396558] (the system permeance (Wikidata Q77997985))" + }, + { + "q": "Could you work out the magnetic reluctance given permeance = 134.993?", + "a": "0.007407793842889246", + "valid": true, + "kind": "compute", + "expect": 0.007407793842889246, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [134.992958] (the system permeance (Wikidata Q77997985))" + }, + { + "q": "I need the magnetic reluctance for a permeance of 134.993, please.", + "a": "0.007407793842889246", + "valid": true, + "kind": "compute", + "expect": 0.007407793842889246, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [134.992958] (the system permeance (Wikidata Q77997985))" + }, + { + "q": "I need the magnetic reluctance, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the permeance is not given" + }, + { + "q": "Can you tell me the magnetic reluctance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the permeance is not given" + }, + { + "q": "Do you know the magnetic reluctance 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 magnetic reluctance 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 magnetic reluctance given permeance = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative permeance: no example had one" + }, + { + "q": "What is a typical magnetic reluctance?", + "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-magnetic-reluctance-2/program.py b/root/organs10/physics/specialists/solved-magnetic-reluctance-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..aa4d8777e68974394ee5e05f1428688eeb95fa25 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-reluctance-2/program.py @@ -0,0 +1,7 @@ +"""magnetic reluctance from permeance (permeance (Wikidata Q77997985)) +The maths organ (systems.py): solved from the system q1 = ((1)/(q0)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(permeance): + return 1.0/permeance diff --git a/root/organs10/physics/specialists/solved-magnetic-reluctance-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-reluctance-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-reluctance-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-magnetic-reluctance/organ_formula_v2.py b/root/organs10/physics/specialists/solved-magnetic-reluctance/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-reluctance/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-magnetic-reluctance/panel.json b/root/organs10/physics/specialists/solved-magnetic-reluctance/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..d99039018589cf1556f61f3f2f7a9ac0e3015272 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-reluctance/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the magnetic reluctance for magnetic tension 107.961 and total magnetic flux 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [107.960666, 59.4105] (the system magnetic reluctance (Wikidata Q863390))" + }, + { + "q": "Tell me the magnetic reluctance with magnetic tension of 107.961 and total magnetic flux of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [107.960666, 59.4105] (the system magnetic reluctance (Wikidata Q863390))" + }, + { + "q": "What is the magnetic reluctance for magnetic tension 169.527 and total magnetic flux 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [169.526958, 280.773702] (the system magnetic reluctance (Wikidata Q863390))" + }, + { + "q": "Tell me the magnetic reluctance with magnetic tension of 169.527 and total magnetic flux of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [169.526958, 280.773702] (the system magnetic reluctance (Wikidata Q863390))" + }, + { + "q": "What is the magnetic reluctance for magnetic tension 1.91195 and total magnetic flux 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [1.911952, 82.240072] (the system magnetic reluctance (Wikidata Q863390))" + }, + { + "q": "Tell me the magnetic reluctance with magnetic tension of 1.91195 and total magnetic flux of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [1.911952, 82.240072] (the system magnetic reluctance (Wikidata Q863390))" + }, + { + "q": "What is the magnetic reluctance for magnetic tension 1.64379 and total magnetic flux 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [1.643795, 252.709024] (the system magnetic reluctance (Wikidata Q863390))" + }, + { + "q": "Tell me the magnetic reluctance with magnetic tension of 1.64379 and total magnetic flux of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [1.643795, 252.709024] (the system magnetic reluctance (Wikidata Q863390))" + }, + { + "q": "What is the magnetic reluctance for magnetic tension 18.3169 and total magnetic flux 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [18.316867, 6.838598] (the system magnetic reluctance (Wikidata Q863390))" + }, + { + "q": "Tell me the magnetic reluctance with magnetic tension of 18.3169 and total magnetic flux of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [18.316867, 6.838598] (the system magnetic reluctance (Wikidata Q863390))" + }, + { + "q": "What is the magnetic reluctance for magnetic tension 156.502 and total magnetic flux 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [156.50181, 185.886273] (the system magnetic reluctance (Wikidata Q863390))" + }, + { + "q": "Tell me the magnetic reluctance with magnetic tension of 156.502 and total magnetic flux of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [156.50181, 185.886273] (the system magnetic reluctance (Wikidata Q863390))" + }, + { + "q": "What is the magnetic reluctance for magnetic tension 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total magnetic flux is not given" + }, + { + "q": "Tell me the magnetic reluctance with magnetic tension of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total magnetic flux is not given" + }, + { + "q": "What is the magnetic reluctance 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 magnetic reluctance 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 magnetic reluctance with magnetic tension of -1.91195 and total magnetic flux of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative magnetic tension: no example had one" + }, + { + "q": "Which magnetic reluctance 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 magnetic tension is 8.50728 and the total magnetic flux is 17.7888, what is the magnetic reluctance?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [8.507283, 17.788778] (the system magnetic reluctance (Wikidata Q863390))" + }, + { + "q": "Could you work out the magnetic reluctance given magnetic tension = 8.50728, total magnetic flux = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [8.507283, 17.788778] (the system magnetic reluctance (Wikidata Q863390))" + }, + { + "q": "I need the magnetic reluctance for a magnetic tension of 8.24951 and a total magnetic flux of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [8.24951, 0.634021] (the system magnetic reluctance (Wikidata Q863390))" + }, + { + "q": "If the magnetic tension is 8.24951 and the total magnetic flux is 0.634021, what is the magnetic reluctance?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [8.24951, 0.634021] (the system magnetic reluctance (Wikidata Q863390))" + }, + { + "q": "Could you work out the magnetic reluctance given magnetic tension = 0.318734, total magnetic flux = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [0.318734, 9.566071] (the system magnetic reluctance (Wikidata Q863390))" + }, + { + "q": "I need the magnetic reluctance for a magnetic tension of 0.318734 and a total magnetic flux of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [0.318734, 9.566071] (the system magnetic reluctance (Wikidata Q863390))" + }, + { + "q": "If the magnetic tension is 0.671697 and the total magnetic flux is 14.2496, what is the magnetic reluctance?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [0.671697, 14.249569] (the system magnetic reluctance (Wikidata Q863390))" + }, + { + "q": "Could you work out the magnetic reluctance given magnetic tension = 0.671697, total magnetic flux = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [0.671697, 14.249569] (the system magnetic reluctance (Wikidata Q863390))" + }, + { + "q": "I need the magnetic reluctance for a magnetic tension of 21.7931 and a total magnetic flux of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [21.793126, 29.510609] (the system magnetic reluctance (Wikidata Q863390))" + }, + { + "q": "If the magnetic tension is 21.7931 and the total magnetic flux is 29.5106, what is the magnetic reluctance?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [21.793126, 29.510609] (the system magnetic reluctance (Wikidata Q863390))" + }, + { + "q": "Could you work out the magnetic reluctance given magnetic tension = 153.15, total magnetic flux = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [153.149965, 18.595315] (the system magnetic reluctance (Wikidata Q863390))" + }, + { + "q": "I need the magnetic reluctance for a magnetic tension of 153.15 and a total magnetic flux of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "s^2*A^2/m^2/kg", + "why": "magnetic reluctance for [153.149965, 18.595315] (the system magnetic reluctance (Wikidata Q863390))" + }, + { + "q": "If the magnetic tension is 8.50728, what is the magnetic reluctance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total magnetic flux is not given" + }, + { + "q": "Could you work out the magnetic reluctance given magnetic tension = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total magnetic flux is not given" + }, + { + "q": "Do you know the magnetic reluctance 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 magnetic reluctance 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 magnetic reluctance given magnetic tension = -0.318734, total magnetic flux = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative magnetic tension: no example had one" + }, + { + "q": "What is a typical magnetic reluctance?", + "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-magnetic-reluctance/program.py b/root/organs10/physics/specialists/solved-magnetic-reluctance/program.py new file mode 100644 index 0000000000000000000000000000000000000000..f9a80564f81016bb6644fb262083060aef1bd2e6 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-reluctance/program.py @@ -0,0 +1,7 @@ +"""magnetic reluctance from magnetic tension, total magnetic flux (magnetic reluctance (Wikidata Q863390)) +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(magnetic_tension, total_magnetic_flux): + return 1.0*magnetic_tension/total_magnetic_flux diff --git a/root/organs10/physics/specialists/solved-magnetic-reluctance/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-magnetic-reluctance/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-magnetic-reluctance/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"(?