diff --git a/root/organs10/physics/specialists/solved-amount-of-substance-3/program.py b/root/organs10/physics/specialists/solved-amount-of-substance-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..533ab22924d9255f47656c14c5ea42dcf08a9571 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-3/program.py @@ -0,0 +1,7 @@ +"""amount of substance from molar heat capacity, heat capacity (molar heat capacity (Wikidata Q2937190)) +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_heat_capacity, heat_capacity): + return 1.0*heat_capacity/molar_heat_capacity diff --git a/root/organs10/physics/specialists/solved-amount-of-substance-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-amount-of-substance-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-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-amount-of-substance-4/organ_formula_v2.py b/root/organs10/physics/specialists/solved-amount-of-substance-4/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-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-amount-of-substance-4/panel.json b/root/organs10/physics/specialists/solved-amount-of-substance-4/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..ee6c056d12c790b33bc89d323470b453a439c724 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-4/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the amount of substance for molar volume 107.961 and volume 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [107.960666, 59.4105] (the system molar volume (Wikidata Q487112))" + }, + { + "q": "Tell me the amount of substance with molar volume of 107.961 and volume of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [107.960666, 59.4105] (the system molar volume (Wikidata Q487112))" + }, + { + "q": "What is the amount of substance for molar volume 169.527 and volume 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [169.526958, 280.773702] (the system molar volume (Wikidata Q487112))" + }, + { + "q": "Tell me the amount of substance with molar volume of 169.527 and volume of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [169.526958, 280.773702] (the system molar volume (Wikidata Q487112))" + }, + { + "q": "What is the amount of substance for molar volume 1.91195 and volume 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [1.911952, 82.240072] (the system molar volume (Wikidata Q487112))" + }, + { + "q": "Tell me the amount of substance with molar volume of 1.91195 and volume of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [1.911952, 82.240072] (the system molar volume (Wikidata Q487112))" + }, + { + "q": "What is the amount of substance for molar volume 1.64379 and volume 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [1.643795, 252.709024] (the system molar volume (Wikidata Q487112))" + }, + { + "q": "Tell me the amount of substance with molar volume of 1.64379 and volume of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [1.643795, 252.709024] (the system molar volume (Wikidata Q487112))" + }, + { + "q": "What is the amount of substance for molar volume 18.3169 and volume 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [18.316867, 6.838598] (the system molar volume (Wikidata Q487112))" + }, + { + "q": "Tell me the amount of substance with molar volume of 18.3169 and volume of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [18.316867, 6.838598] (the system molar volume (Wikidata Q487112))" + }, + { + "q": "What is the amount of substance for molar volume 156.502 and volume 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [156.50181, 185.886273] (the system molar volume (Wikidata Q487112))" + }, + { + "q": "Tell me the amount of substance with molar volume of 156.502 and volume of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [156.50181, 185.886273] (the system molar volume (Wikidata Q487112))" + }, + { + "q": "What is the amount of substance for molar volume 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the volume is not given" + }, + { + "q": "Tell me the amount of substance with molar volume of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the volume is not given" + }, + { + "q": "What is the 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 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 amount of substance with molar volume of -1.91195 and volume of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative molar volume: no example had one" + }, + { + "q": "Which 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 molar volume is 8.50728 and the volume is 17.7888, what is the amount of substance?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [8.507283, 17.788778] (the system molar volume (Wikidata Q487112))" + }, + { + "q": "Could you work out the amount of substance given molar volume = 8.50728, volume = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [8.507283, 17.788778] (the system molar volume (Wikidata Q487112))" + }, + { + "q": "I need the amount of substance for a molar volume of 8.24951 and a volume of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [8.24951, 0.634021] (the system molar volume (Wikidata Q487112))" + }, + { + "q": "If the molar volume is 8.24951 and the volume is 0.634021, what is the amount of substance?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [8.24951, 0.634021] (the system molar volume (Wikidata Q487112))" + }, + { + "q": "Could you work out the amount of substance given molar volume = 0.318734, volume = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [0.318734, 9.566071] (the system molar volume (Wikidata Q487112))" + }, + { + "q": "I need the amount of substance for a molar volume of 0.318734 and a volume of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [0.318734, 9.566071] (the system molar volume (Wikidata Q487112))" + }, + { + "q": "If the molar volume is 0.671697 and the volume is 14.2496, what is the amount of substance?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [0.671697, 14.249569] (the system molar volume (Wikidata Q487112))" + }, + { + "q": "Could you work out the amount of substance given molar volume = 0.671697, volume = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [0.671697, 14.249569] (the system molar volume (Wikidata Q487112))" + }, + { + "q": "I need the amount of substance for a molar volume of 21.7931 and a volume of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [21.793126, 29.510609] (the system molar volume (Wikidata Q487112))" + }, + { + "q": "If the molar volume is 21.7931 and the volume is 29.5106, what is the amount of substance?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [21.793126, 29.510609] (the system molar volume (Wikidata Q487112))" + }, + { + "q": "Could you work out the amount of substance given molar volume = 153.15, volume = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [153.149965, 18.595315] (the system molar volume (Wikidata Q487112))" + }, + { + "q": "I need the amount of substance for a molar volume of 153.15 and a volume of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [153.149965, 18.595315] (the system molar volume (Wikidata Q487112))" + }, + { + "q": "If the molar volume is 8.50728, what is the amount of substance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the volume is not given" + }, + { + "q": "Could you work out the amount of substance given molar volume = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the volume is not given" + }, + { + "q": "Do you know the 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 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 amount of substance given molar volume = -0.318734, volume = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative molar volume: no example had one" + }, + { + "q": "What is a typical 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-amount-of-substance-4/program.py b/root/organs10/physics/specialists/solved-amount-of-substance-4/program.py new file mode 100644 index 0000000000000000000000000000000000000000..40a6e33fa2ed794ca57de791ae8881da232e5d37 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-4/program.py @@ -0,0 +1,7 @@ +"""amount of substance from molar volume, volume (molar volume (Wikidata Q487112)) +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_volume, volume): + return 1.0*volume/molar_volume diff --git a/root/organs10/physics/specialists/solved-amount-of-substance-4/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-amount-of-substance-4/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-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-amount-of-substance-5/organ_formula_v2.py b/root/organs10/physics/specialists/solved-amount-of-substance-5/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-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-amount-of-substance-5/panel.json b/root/organs10/physics/specialists/solved-amount-of-substance-5/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..09b449270c3badb51dc17853c0672eaa90453d37 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-5/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the amount of substance for molar internal energy 107.961 and internal energy 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [107.960666, 59.4105] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "Tell me the amount of substance with molar internal energy of 107.961 and internal energy of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [107.960666, 59.4105] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "What is the amount of substance for molar internal energy 169.527 and internal energy 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [169.526958, 280.773702] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "Tell me the amount of substance with molar internal energy of 169.527 and internal energy of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [169.526958, 280.773702] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "What is the amount of substance for molar internal energy 1.91195 and internal energy 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [1.911952, 82.240072] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "Tell me the amount of substance with molar internal energy of 1.91195 and internal energy of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [1.911952, 82.240072] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "What is the amount of substance for molar internal energy 1.64379 and internal energy 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [1.643795, 252.709024] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "Tell me the amount of substance with molar internal energy of 1.64379 and internal energy of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [1.643795, 252.709024] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "What is the amount of substance for molar internal energy 18.3169 and internal energy 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [18.316867, 6.838598] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "Tell me the amount of substance with molar internal energy of 18.3169 and internal energy of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [18.316867, 6.838598] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "What is the amount of substance for molar internal energy 156.502 and internal energy 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [156.50181, 185.886273] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "Tell me the amount of substance with molar internal energy of 156.502 and internal energy of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [156.50181, 185.886273] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "What is the amount of substance for molar internal energy 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the internal energy is not given" + }, + { + "q": "Tell me the amount of substance with molar internal energy of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the internal energy is not given" + }, + { + "q": "What is the 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 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 amount of substance with molar internal energy of -1.91195 and internal energy 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 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 molar internal energy is 8.50728 and the internal energy is 17.7888, what is the amount of substance?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [8.507283, 17.788778] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "Could you work out the amount of substance given molar internal energy = 8.50728, internal energy = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [8.507283, 17.788778] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "I need the amount of substance for a molar internal energy of 8.24951 and an internal energy of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [8.24951, 0.634021] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "If the molar internal energy is 8.24951 and the internal energy is 0.634021, what is the amount of substance?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [8.24951, 0.634021] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "Could you work out the amount of substance given molar internal energy = 0.318734, internal energy = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [0.318734, 9.566071] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "I need the amount of substance for a molar internal energy of 0.318734 and an internal energy of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [0.318734, 9.566071] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "If the molar internal energy is 0.671697 and the internal energy is 14.2496, what is the amount of substance?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [0.671697, 14.249569] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "Could you work out the amount of substance given molar internal energy = 0.671697, internal energy = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [0.671697, 14.249569] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "I need the amount of substance for a molar internal energy of 21.7931 and an internal energy of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [21.793126, 29.510609] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "If the molar internal energy is 21.7931 and the internal energy is 29.5106, what is the amount of substance?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [21.793126, 29.510609] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "Could you work out the amount of substance given molar internal energy = 153.15, internal energy = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [153.149965, 18.595315] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "I need the amount of substance for a molar internal energy of 153.15 and an internal energy of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [153.149965, 18.595315] (the system molar internal energy (Wikidata Q88523106))" + }, + { + "q": "If the molar internal energy is 8.50728, what is the amount of substance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the internal energy is not given" + }, + { + "q": "Could you work out the amount of substance given molar internal energy = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the internal energy is not given" + }, + { + "q": "Do you know the 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 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 amount of substance given molar internal energy = -0.318734, internal energy = 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 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-amount-of-substance-5/program.py b/root/organs10/physics/specialists/solved-amount-of-substance-5/program.py new file mode 100644 index 0000000000000000000000000000000000000000..f296ad559b0a82a58a29422802457e3b25c7f996 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-5/program.py @@ -0,0 +1,7 @@ +"""amount of substance from molar internal energy, internal energy (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, internal_energy): + return 1.0*internal_energy/molar_internal_energy diff --git a/root/organs10/physics/specialists/solved-amount-of-substance-5/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-amount-of-substance-5/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-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-amount-of-substance-6/organ_formula_v2.py b/root/organs10/physics/specialists/solved-amount-of-substance-6/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-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-amount-of-substance-6/panel.json b/root/organs10/physics/specialists/solved-amount-of-substance-6/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..d2d5fe6ee7c2caad33e5bcec5eb9ea7be9f90b97 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-6/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the amount of substance for molar optical rotatory power 107.961, angle of optical rotation 59.4105 and cross-sectional area 5.77717?", + "a": "3.1791600820256143", + "valid": true, + "kind": "compute", + "expect": 3.1791600820256143, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [107.960666, 59.4105, 5.777165] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Tell me the amount of substance with molar optical rotatory power of 107.961, angle of optical rotation of 59.4105 and cross-sectional area of 5.77717.", + "a": "3.1791600820256143", + "valid": true, + "kind": "compute", + "expect": 3.1791600820256143, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [107.960666, 59.4105, 5.777165] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "What is the amount of substance for molar optical rotatory power 85.2416, angle of optical rotation 160.878 and cross-sectional area 2.69428?", + "a": "5.084975341983345", + "valid": true, + "kind": "compute", + "expect": 5.084975341983345, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [85.241623, 160.878442, 2.69428] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Tell me the amount of substance with molar optical rotatory power of 85.2416, angle of optical rotation of 160.878 and cross-sectional area of 2.69428.", + "a": "5.084975341983345", + "valid": true, + "kind": "compute", + "expect": 5.084975341983345, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [85.241623, 160.878442, 2.69428] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "What is the amount of substance for molar optical rotatory power 45.6157, angle of optical rotation 4.97131 and cross-sectional area 94.305?", + "a": "10.27759497808088", + "valid": true, + "kind": "compute", + "expect": 10.27759497808088, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [45.61567, 4.971307, 94.305046] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Tell me the amount of substance with molar optical rotatory power of 45.6157, angle of optical rotation of 4.97131 and cross-sectional area of 94.305.", + "a": "10.27759497808088", + "valid": true, + "kind": "compute", + "expect": 10.27759497808088, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [45.61567, 4.971307, 94.305046] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "What is the amount of substance for molar optical rotatory power 10.5638, angle of optical rotation 198.839 and cross-sectional area 0.671697?", + "a": "12.643085733152049", + "valid": true, + "kind": "compute", + "expect": 12.643085733152049, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [10.563843, 198.838955, 0.671697] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Tell me the amount of substance with molar optical rotatory power of 10.5638, angle of optical rotation of 198.839 and cross-sectional area of 0.671697.", + "a": "12.643085733152049", + "valid": true, + "kind": "compute", + "expect": 12.643085733152049, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [10.563843, 198.838955, 0.671697] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "What is the amount of substance for molar optical rotatory power 21.7931, angle of optical rotation 29.5106 and cross-sectional area 8.50341?", + "a": "11.514672847514374", + "valid": true, + "kind": "compute", + "expect": 11.514672847514374, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [21.793126, 29.510609, 8.503407] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Tell me the amount of substance with molar optical rotatory power of 21.7931, angle of optical rotation of 29.5106 and cross-sectional area of 8.50341.", + "a": "11.514672847514374", + "valid": true, + "kind": "compute", + "expect": 11.514672847514374, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [21.793126, 29.510609, 8.503407] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "What is the amount of substance for molar optical rotatory power 223.509, angle of optical rotation 17.3414 and cross-sectional area 7.10705?", + "a": "0.5514164683902788", + "valid": true, + "kind": "compute", + "expect": 0.5514164683902788, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [223.508796, 17.341435, 7.107049] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Tell me the amount of substance with molar optical rotatory power of 223.509, angle of optical rotation of 17.3414 and cross-sectional area of 7.10705.", + "a": "0.5514164683902788", + "valid": true, + "kind": "compute", + "expect": 0.5514164683902788, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [223.508796, 17.341435, 7.107049] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "What is the amount of substance for molar optical rotatory power 107.961 and angle of optical rotation 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the cross-sectional area is not given" + }, + { + "q": "Tell me the amount of substance with molar optical rotatory power of 85.2416 and angle of optical rotation of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the cross-sectional area is not given" + }, + { + "q": "What is the 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 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 amount of substance with molar optical rotatory power of -45.6157, angle of optical rotation of 4.97131 and cross-sectional area of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative molar optical rotatory power: no example had one" + }, + { + "q": "Which 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 molar optical rotatory power is 17.7888, angle of optical rotation is 167.625 and the cross-sectional area is 10.3291, what is the amount of substance?", + "a": "97.3314301270093", + "valid": true, + "kind": "compute", + "expect": 97.3314301270093, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [17.788778, 167.624948, 10.329054] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Could you work out the amount of substance given molar optical rotatory power = 17.7888, angle of optical rotation = 167.625, cross-sectional area = 10.3291?", + "a": "97.3314301270093", + "valid": true, + "kind": "compute", + "expect": 97.3314301270093, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [17.788778, 167.624948, 10.329054] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "I need the amount of substance for a molar optical rotatory power of 21.511, an angle of optical rotation of 173.394 and a cross-sectional area of 251.084, please.", + "a": "2023.9106825818615", + "valid": true, + "kind": "compute", + "expect": 2023.9106825818615, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [21.511016, 173.393638, 251.084037] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "If the molar optical rotatory power is 21.511, angle of optical rotation is 173.394 and the cross-sectional area is 251.084, what is the amount of substance?", + "a": "2023.9106825818615", + "valid": true, + "kind": "compute", + "expect": 2023.9106825818615, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [21.511016, 173.393638, 251.084037] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Could you work out the amount of substance given molar optical rotatory power = 129.245, angle of optical rotation = 1.18359, cross-sectional area = 15.9416?", + "a": "0.1459888878289837", + "valid": true, + "kind": "compute", + "expect": 0.1459888878289837, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [129.244984, 1.18359, 15.941612] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "I need the amount of substance for a molar optical rotatory power of 129.245, an angle of optical rotation of 1.18359 and a cross-sectional area of 15.9416, please.", + "a": "0.1459888878289837", + "valid": true, + "kind": "compute", + "expect": 0.1459888878289837, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [129.244984, 1.18359, 15.941612] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "If the molar optical rotatory power is 20.3966, angle of optical rotation is 18.3169 and the cross-sectional area is 6.8386, what is the amount of substance?", + "a": "6.141315193814264", + "valid": true, + "kind": "compute", + "expect": 6.141315193814264, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [20.396558, 18.316867, 6.838598] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Could you work out the amount of substance given molar optical rotatory power = 20.3966, angle of optical rotation = 18.3169, cross-sectional area = 6.8386?", + "a": "6.141315193814264", + "valid": true, + "kind": "compute", + "expect": 6.141315193814264, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [20.396558, 18.316867, 6.838598] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "I need the amount of substance for a molar optical rotatory power of 185.886, an angle of optical rotation of 13.2373 and a cross-sectional area of 4.71941, please.", + "a": "0.3360784039769457", + "valid": true, + "kind": "compute", + "expect": 0.3360784039769457, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [185.886273, 13.237311, 4.719415] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "If the molar optical rotatory power is 185.886, angle of optical rotation is 13.2373 and the cross-sectional area is 4.71941, what is the amount of substance?", + "a": "0.3360784039769457", + "valid": true, + "kind": "compute", + "expect": 0.3360784039769457, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [185.886273, 13.237311, 4.719415] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Could you work out the amount of substance given molar optical rotatory power = 9.1061, angle of optical rotation = 24.5721, cross-sectional area = 108.451?", + "a": "292.64569674251265", + "valid": true, + "kind": "compute", + "expect": 292.64569674251265, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [9.106104, 24.572086, 108.450792] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "I need the amount of substance for a molar optical rotatory power of 9.1061, an angle of optical rotation of 24.5721 and a cross-sectional area of 108.451, please.", + "a": "292.64569674251265", + "valid": true, + "kind": "compute", + "expect": 292.64569674251265, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [9.106104, 24.572086, 108.450792] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "If the molar optical rotatory power is 17.7888 and the angle of optical rotation is 167.625, what is the amount of substance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the cross-sectional area is not given" + }, + { + "q": "Could you work out the amount of substance given molar optical rotatory power = 21.511, angle of optical rotation = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the cross-sectional area is not given" + }, + { + "q": "Do you know the 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 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 amount of substance given molar optical rotatory power = -129.245, angle of optical rotation = 1.18359, cross-sectional area = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative molar optical rotatory power: no example had one" + }, + { + "q": "What is a typical 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-amount-of-substance-6/program.py b/root/organs10/physics/specialists/solved-amount-of-substance-6/program.py new file mode 100644 index 0000000000000000000000000000000000000000..ffec86b53eb229c73bb90d11c84e4bf7372647b6 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-6/program.py @@ -0,0 +1,7 @@ +"""amount of substance from molar optical rotatory power, angle of optical rotation, cross-sectional area (molar optical rotatory power (Wikidata Q96346994)) +The maths organ (systems.py): solved from the system q0 = q1*((q2)/(q3)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(molar_optical_rotatory_power, angle_of_optical_rotation, cross_sectional_area): + return 1.0*angle_of_optical_rotation*cross_sectional_area/molar_optical_rotatory_power diff --git a/root/organs10/physics/specialists/solved-amount-of-substance-6/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-amount-of-substance-6/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-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-amount-of-substance-7/organ_formula_v2.py b/root/organs10/physics/specialists/solved-amount-of-substance-7/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-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-amount-of-substance-7/panel.json b/root/organs10/physics/specialists/solved-amount-of-substance-7/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..0b4793da9dd848e4d3bfb156202463740deee4cc --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-7/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the amount of substance for mean energy imparted 107.961 and radiation-chemical yield 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [107.960666, 59.4105] (the system radiation-chemical yield (Wikidata Q109061714))" + }, + { + "q": "Tell me the amount of substance with mean energy imparted of 107.961 and radiation-chemical yield of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [107.960666, 59.4105] (the system radiation-chemical yield (Wikidata Q109061714))" + }, + { + "q": "What is the amount of substance for mean energy imparted 169.527 and radiation-chemical yield 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [169.526958, 280.773702] (the system radiation-chemical yield (Wikidata Q109061714))" + }, + { + "q": "Tell me the amount of substance with mean energy imparted of 169.527 and radiation-chemical yield of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [169.526958, 280.773702] (the system radiation-chemical yield (Wikidata Q109061714))" + }, + { + "q": "What is the amount of substance for mean energy imparted 1.91195 and radiation-chemical yield 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [1.911952, 82.240072] (the system radiation-chemical yield (Wikidata Q109061714))" + }, + { + "q": "Tell me the amount of substance with mean energy imparted of 1.91195 and radiation-chemical yield of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [1.911952, 82.240072] (the system radiation-chemical yield (Wikidata Q109061714))" + }, + { + "q": "What is the amount of substance for mean energy imparted 1.64379 and radiation-chemical yield 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [1.643795, 252.709024] (the system radiation-chemical yield (Wikidata Q109061714))" + }, + { + "q": "Tell me the amount of substance with mean energy imparted of 1.64379 and radiation-chemical yield of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [1.643795, 252.709024] (the system radiation-chemical yield (Wikidata Q109061714))" + }, + { + "q": "What is the amount of substance for mean energy imparted 18.3169 and radiation-chemical yield 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [18.316867, 6.838598] (the system radiation-chemical yield (Wikidata Q109061714))" + }, + { + "q": "Tell me the amount of substance with mean energy imparted of 18.3169 and radiation-chemical yield of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [18.316867, 6.838598] (the system radiation-chemical yield (Wikidata Q109061714))" + }, + { + "q": "What is the amount of substance for mean energy imparted 156.502 and radiation-chemical yield 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [156.50181, 185.886273] (the system radiation-chemical yield (Wikidata Q109061714))" + }, + { + "q": "Tell me the amount of substance with mean energy imparted of 156.502 and radiation-chemical yield of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [156.50181, 185.886273] (the system radiation-chemical yield (Wikidata Q109061714))" + }, + { + "q": "What is the amount of substance for mean energy imparted 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radiation-chemical yield is not given" + }, + { + "q": "Tell me the amount of substance with mean energy imparted of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radiation-chemical yield is not given" + }, + { + "q": "What is the 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 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 amount of substance with mean energy imparted of -1.91195 and radiation-chemical yield of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mean energy imparted: no example had one" + }, + { + "q": "Which 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 mean energy imparted is 8.50728 and the radiation-chemical yield is 17.7888, what is the amount of substance?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [8.507283, 17.788778] (the system radiation-chemical yield (Wikidata Q109061714))" + }, + { + "q": "Could you work out the amount of substance given mean energy imparted = 8.50728, radiation-chemical yield = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [8.507283, 17.788778] (the system radiation-chemical yield (Wikidata Q109061714))" + }, + { + "q": "I need the amount of substance for a mean energy imparted of 8.24951 and a radiation-chemical yield of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [8.24951, 0.634021] (the system radiation-chemical yield (Wikidata Q109061714))" + }, + { + "q": "If the mean energy imparted is 8.24951 and the radiation-chemical yield is 0.634021, what is the amount of substance?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [8.24951, 0.634021] (the system radiation-chemical yield (Wikidata Q109061714))" + }, + { + "q": "Could you work out the amount of substance given mean energy imparted = 0.318734, radiation-chemical yield = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [0.318734, 9.566071] (the system radiation-chemical yield (Wikidata Q109061714))" + }, + { + "q": "I need the amount of substance for a mean energy imparted of 0.318734 and a radiation-chemical yield of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [0.318734, 9.566071] (the system radiation-chemical yield (Wikidata Q109061714))" + }, + { + "q": "If the mean energy imparted is 0.671697 and the radiation-chemical yield is 14.2496, what is the amount of substance?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [0.671697, 14.249569] (the system radiation-chemical yield (Wikidata Q109061714))" + }, + { + "q": "Could you work out the amount of substance given mean energy imparted = 0.671697, radiation-chemical yield = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [0.671697, 14.249569] (the system radiation-chemical yield (Wikidata Q109061714))" + }, + { + "q": "I need the amount of substance for a mean energy imparted of 21.7931 and a radiation-chemical yield of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [21.793126, 29.510609] (the system radiation-chemical yield (Wikidata Q109061714))" + }, + { + "q": "If the mean energy imparted is 21.7931 and the radiation-chemical yield is 29.5106, what is the amount of substance?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [21.793126, 29.510609] (the system radiation-chemical yield (Wikidata Q109061714))" + }, + { + "q": "Could you work out the amount of substance given mean energy imparted = 153.15, radiation-chemical yield = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [153.149965, 18.595315] (the system radiation-chemical yield (Wikidata Q109061714))" + }, + { + "q": "I need the amount of substance for a mean energy imparted of 153.15 and a radiation-chemical yield of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [153.149965, 18.595315] (the system radiation-chemical yield (Wikidata Q109061714))" + }, + { + "q": "If the mean energy imparted is 8.50728, what is the amount of substance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radiation-chemical yield is not given" + }, + { + "q": "Could you work out the amount of substance given mean energy imparted = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radiation-chemical yield is not given" + }, + { + "q": "Do you know the 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 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 amount of substance given mean energy imparted = -0.318734, radiation-chemical yield = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mean energy imparted: no example had one" + }, + { + "q": "What is a typical 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-amount-of-substance-7/program.py b/root/organs10/physics/specialists/solved-amount-of-substance-7/program.py new file mode 100644 index 0000000000000000000000000000000000000000..dca2854daa40fe3581e8cede76b2321193e1f51c --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-7/program.py @@ -0,0 +1,7 @@ +"""amount of substance from mean energy imparted, radiation-chemical yield (radiation-chemical yield (Wikidata Q109061714)) +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(mean_energy_imparted, radiation_chemical_yield): + return 1.0*mean_energy_imparted*radiation_chemical_yield diff --git a/root/organs10/physics/specialists/solved-amount-of-substance-7/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-amount-of-substance-7/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-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-amount-of-substance-8/organ_formula_v2.py b/root/organs10/physics/specialists/solved-amount-of-substance-8/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-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-amount-of-substance-8/panel.json b/root/organs10/physics/specialists/solved-amount-of-substance-8/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..8d504255088fd099e07d543c32a3da3a8181645c --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-8/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the amount of substance for amount-of-substance fraction 107.961 and amount of substance of component 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [107.960666, 59.4105] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Tell me the amount of substance with amount-of-substance fraction of 107.961 and amount of substance of component of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [107.960666, 59.4105] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "What is the amount of substance for amount-of-substance fraction 169.527 and amount of substance of component 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [169.526958, 280.773702] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Tell me the amount of substance with amount-of-substance fraction of 169.527 and amount of substance of component of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [169.526958, 280.773702] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "What is the amount of substance for amount-of-substance fraction 1.91195 and amount of substance of component 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [1.911952, 82.240072] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Tell me the amount of substance with amount-of-substance fraction of 1.91195 and amount of substance of component of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [1.911952, 82.240072] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "What is the amount of substance for amount-of-substance fraction 1.64379 and amount of substance of component 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [1.643795, 252.709024] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Tell me the amount of substance with amount-of-substance fraction of 1.64379 and amount of substance of component of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [1.643795, 252.709024] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "What is the amount of substance for amount-of-substance fraction 18.3169 and amount of substance of component 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [18.316867, 6.838598] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Tell me the amount of substance with amount-of-substance fraction of 18.3169 and amount of substance of component of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [18.316867, 6.838598] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "What is the amount of substance for amount-of-substance fraction 156.502 and amount of substance of component 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [156.50181, 185.886273] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Tell me the amount of substance with amount-of-substance fraction of 156.502 and amount of substance of component of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [156.50181, 185.886273] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "What is the amount of substance for amount-of-substance fraction 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the amount of substance of component is not given" + }, + { + "q": "Tell me the amount of substance with amount-of-substance fraction of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the amount of substance of component is not given" + }, + { + "q": "What is the 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 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 amount of substance with amount-of-substance fraction of -1.91195 and amount of substance of component of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative amount-of-substance fraction: no example had one" + }, + { + "q": "Which 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 amount-of-substance fraction is 8.50728 and the amount of substance of component is 17.7888, what is the amount of substance?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [8.507283, 17.788778] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Could you work out the amount of substance given amount-of-substance fraction = 8.50728, amount of substance of component = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [8.507283, 17.788778] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "I need the amount of substance for an amount-of-substance fraction of 8.24951 and an amount of substance of component of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [8.24951, 0.634021] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "If the amount-of-substance fraction is 8.24951 and the amount of substance of component is 0.634021, what is the amount of substance?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [8.24951, 0.634021] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Could you work out the amount of substance given amount-of-substance fraction = 0.318734, amount of substance of component = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [0.318734, 9.566071] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "I need the amount of substance for an amount-of-substance fraction of 0.318734 and an amount of substance of component of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [0.318734, 9.566071] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "If the amount-of-substance fraction is 0.671697 and the amount of substance of component is 14.2496, what is the amount of substance?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [0.671697, 14.249569] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Could you work out the amount of substance given amount-of-substance fraction = 0.671697, amount of substance of component = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [0.671697, 14.249569] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "I need the amount of substance for an amount-of-substance fraction of 21.7931 and an amount of substance of component of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [21.793126, 29.510609] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "If the amount-of-substance fraction is 21.7931 and the amount of substance of component is 29.5106, what is the amount of substance?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [21.793126, 29.510609] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Could you work out the amount of substance given amount-of-substance fraction = 153.15, amount of substance of component = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [153.149965, 18.595315] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "I need the amount of substance for an amount-of-substance fraction of 153.15 and an amount of substance of component of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [153.149965, 18.595315] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "If the amount-of-substance fraction is 8.50728, what is the amount of substance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the amount of substance of component is not given" + }, + { + "q": "Could you work out the amount of substance given amount-of-substance fraction = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the amount of substance of component is not given" + }, + { + "q": "Do you know the 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 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 amount of substance given amount-of-substance fraction = -0.318734, amount of substance of component = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative amount-of-substance fraction: no example had one" + }, + { + "q": "What is a typical 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-amount-of-substance-8/program.py b/root/organs10/physics/specialists/solved-amount-of-substance-8/program.py new file mode 100644 index 0000000000000000000000000000000000000000..ca891ea2ae1a6c099dffd25651802fdbb0219775 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-8/program.py @@ -0,0 +1,7 @@ +"""amount of substance from amount-of-substance fraction, amount of substance of component (amount-of-substance fraction (Wikidata Q125264)) +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(amount_of_substance_fraction, amount_of_substance_of_component): + return 1.0*amount_of_substance_of_component/amount_of_substance_fraction diff --git a/root/organs10/physics/specialists/solved-amount-of-substance-8/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-amount-of-substance-8/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-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-amount-of-substance-9/organ_formula_v2.py b/root/organs10/physics/specialists/solved-amount-of-substance-9/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-9/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-amount-of-substance-9/panel.json b/root/organs10/physics/specialists/solved-amount-of-substance-9/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..21fb3937e733e37449652cad39fedb4d25f564bb --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-9/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the amount of substance for molar entropy 107.961 and entropy 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [107.960666, 59.4105] (the system molar entropy (Wikidata Q68972876))" + }, + { + "q": "Tell me the amount of substance with molar entropy of 107.961 and entropy of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [107.960666, 59.4105] (the system molar entropy (Wikidata Q68972876))" + }, + { + "q": "What is the amount of substance for molar entropy 169.527 and entropy 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [169.526958, 280.773702] (the system molar entropy (Wikidata Q68972876))" + }, + { + "q": "Tell me the amount of substance with molar entropy of 169.527 and entropy of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [169.526958, 280.773702] (the system molar entropy (Wikidata Q68972876))" + }, + { + "q": "What is the amount of substance for molar entropy 1.91195 and entropy 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [1.911952, 82.240072] (the system molar entropy (Wikidata Q68972876))" + }, + { + "q": "Tell me the amount of substance with molar entropy of 1.91195 and entropy of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [1.911952, 82.240072] (the system molar entropy (Wikidata Q68972876))" + }, + { + "q": "What is the amount of substance for molar entropy 1.64379 and entropy 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [1.643795, 252.709024] (the system molar entropy (Wikidata Q68972876))" + }, + { + "q": "Tell me the amount of substance with molar entropy of 1.64379 and entropy of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [1.643795, 252.709024] (the system molar entropy (Wikidata Q68972876))" + }, + { + "q": "What is the amount of substance for molar entropy 18.3169 and entropy 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [18.316867, 6.838598] (the system molar entropy (Wikidata Q68972876))" + }, + { + "q": "Tell me the amount of substance with molar entropy of 18.3169 and entropy of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [18.316867, 6.838598] (the system molar entropy (Wikidata Q68972876))" + }, + { + "q": "What is the amount of substance for molar entropy 156.502 and entropy 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [156.50181, 185.886273] (the system molar entropy (Wikidata Q68972876))" + }, + { + "q": "Tell me the amount of substance with molar entropy of 156.502 and entropy of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [156.50181, 185.886273] (the system molar entropy (Wikidata Q68972876))" + }, + { + "q": "What is the amount of substance for molar entropy 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the entropy is not given" + }, + { + "q": "Tell me the amount of substance with molar entropy of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the entropy is not given" + }, + { + "q": "What is the 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 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 amount of substance with molar entropy of -1.91195 and entropy of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative molar entropy: no example had one" + }, + { + "q": "Which 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 molar entropy is 8.50728 and the entropy is 17.7888, what is the amount of substance?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [8.507283, 17.788778] (the system molar entropy (Wikidata Q68972876))" + }, + { + "q": "Could you work out the amount of substance given molar entropy = 8.50728, entropy = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [8.507283, 17.788778] (the system molar entropy (Wikidata Q68972876))" + }, + { + "q": "I need the amount of substance for a molar entropy of 8.24951 and an entropy of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [8.24951, 0.634021] (the system molar entropy (Wikidata Q68972876))" + }, + { + "q": "If the molar entropy is 8.24951 and the entropy is 0.634021, what is the amount of substance?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [8.24951, 0.634021] (the system molar entropy (Wikidata Q68972876))" + }, + { + "q": "Could you work out the amount of substance given molar entropy = 0.318734, entropy = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [0.318734, 9.566071] (the system molar entropy (Wikidata Q68972876))" + }, + { + "q": "I need the amount of substance for a molar entropy of 0.318734 and an entropy of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [0.318734, 9.566071] (the system molar entropy (Wikidata Q68972876))" + }, + { + "q": "If the molar entropy is 0.671697 and the entropy is 14.2496, what is the amount of substance?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [0.671697, 14.249569] (the system molar entropy (Wikidata Q68972876))" + }, + { + "q": "Could you work out the amount of substance given molar entropy = 0.671697, entropy = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [0.671697, 14.249569] (the system molar entropy (Wikidata Q68972876))" + }, + { + "q": "I need the amount of substance for a molar entropy of 21.7931 and an entropy of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [21.793126, 29.510609] (the system molar entropy (Wikidata Q68972876))" + }, + { + "q": "If the molar entropy is 21.7931 and the entropy is 29.5106, what is the amount of substance?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [21.793126, 29.510609] (the system molar entropy (Wikidata Q68972876))" + }, + { + "q": "Could you work out the amount of substance given molar entropy = 153.15, entropy = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [153.149965, 18.595315] (the system molar entropy (Wikidata Q68972876))" + }, + { + "q": "I need the amount of substance for a molar entropy of 153.15 and an entropy of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance for [153.149965, 18.595315] (the system molar entropy (Wikidata Q68972876))" + }, + { + "q": "If the molar entropy is 8.50728, what is the amount of substance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the entropy is not given" + }, + { + "q": "Could you work out the amount of substance given molar entropy = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the entropy is not given" + }, + { + "q": "Do you know the 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 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 amount of substance given molar entropy = -0.318734, entropy = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative molar entropy: no example had one" + }, + { + "q": "What is a typical 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-amount-of-substance-9/program.py b/root/organs10/physics/specialists/solved-amount-of-substance-9/program.py new file mode 100644 index 0000000000000000000000000000000000000000..4c1dd5b2fd7a54b1186665e06a1bb654ddc17e2d --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-9/program.py @@ -0,0 +1,7 @@ +"""amount of substance from molar entropy, entropy (molar entropy (Wikidata Q68972876)) +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_entropy, entropy): + return 1.0*entropy/molar_entropy diff --git a/root/organs10/physics/specialists/solved-amount-of-substance-9/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-amount-of-substance-9/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-9/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-amount-of-substance-concentra-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-amount-of-substance-concentra-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-concentra-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-amount-of-substance-concentra-2/panel.json b/root/organs10/physics/specialists/solved-amount-of-substance-concentra-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..785f9a06843d7b57c824a6db8ef5dd1123c29a90 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-concentra-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the amount-of-substance concentration for normality 107.961 and valence 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [107.960666, 59.4105] (the system normality (Wikidata Q155854))" + }, + { + "q": "Tell me the amount-of-substance concentration with normality of 107.961 and valence of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [107.960666, 59.4105] (the system normality (Wikidata Q155854))" + }, + { + "q": "What is the amount-of-substance concentration for normality 169.527 and valence 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [169.526958, 280.773702] (the system normality (Wikidata Q155854))" + }, + { + "q": "Tell me the amount-of-substance concentration with normality of 169.527 and valence of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [169.526958, 280.773702] (the system normality (Wikidata Q155854))" + }, + { + "q": "What is the amount-of-substance concentration for normality 1.91195 and valence 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [1.911952, 82.240072] (the system normality (Wikidata Q155854))" + }, + { + "q": "Tell me the amount-of-substance concentration with normality of 1.91195 and valence of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [1.911952, 82.240072] (the system normality (Wikidata Q155854))" + }, + { + "q": "What is the amount-of-substance concentration for normality 1.64379 and valence 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [1.643795, 252.709024] (the system normality (Wikidata Q155854))" + }, + { + "q": "Tell me the amount-of-substance concentration with normality of 1.64379 and valence of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [1.643795, 252.709024] (the system normality (Wikidata Q155854))" + }, + { + "q": "What is the amount-of-substance concentration for normality 18.3169 and valence 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [18.316867, 6.838598] (the system normality (Wikidata Q155854))" + }, + { + "q": "Tell me the amount-of-substance concentration with normality of 18.3169 and valence of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [18.316867, 6.838598] (the system normality (Wikidata Q155854))" + }, + { + "q": "What is the amount-of-substance concentration for normality 156.502 and valence 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [156.50181, 185.886273] (the system normality (Wikidata Q155854))" + }, + { + "q": "Tell me the amount-of-substance concentration with normality of 156.502 and valence of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [156.50181, 185.886273] (the system normality (Wikidata Q155854))" + }, + { + "q": "What is the amount-of-substance concentration for normality 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the valence is not given" + }, + { + "q": "Tell me the amount-of-substance concentration with normality of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the valence is not given" + }, + { + "q": "What is the amount-of-substance concentration 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 amount-of-substance concentration 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 amount-of-substance concentration with normality of -1.91195 and valence of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative normality: no example had one" + }, + { + "q": "Which amount-of-substance concentration 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 normality is 8.50728 and the valence is 17.7888, what is the amount-of-substance concentration?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [8.507283, 17.788778] (the system normality (Wikidata Q155854))" + }, + { + "q": "Could you work out the amount-of-substance concentration given normality = 8.50728, valence = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [8.507283, 17.788778] (the system normality (Wikidata Q155854))" + }, + { + "q": "I need the amount-of-substance concentration for a normality of 8.24951 and a valence of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [8.24951, 0.634021] (the system normality (Wikidata Q155854))" + }, + { + "q": "If the normality is 8.24951 and the valence is 0.634021, what is the amount-of-substance concentration?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [8.24951, 0.634021] (the system normality (Wikidata Q155854))" + }, + { + "q": "Could you work out the amount-of-substance concentration given normality = 0.318734, valence = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [0.318734, 9.566071] (the system normality (Wikidata Q155854))" + }, + { + "q": "I need the amount-of-substance concentration for a normality of 0.318734 and a valence of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [0.318734, 9.566071] (the system normality (Wikidata Q155854))" + }, + { + "q": "If the normality is 0.671697 and the valence is 14.2496, what is the amount-of-substance concentration?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [0.671697, 14.249569] (the system normality (Wikidata Q155854))" + }, + { + "q": "Could you work out the amount-of-substance concentration given normality = 0.671697, valence = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [0.671697, 14.249569] (the system normality (Wikidata Q155854))" + }, + { + "q": "I need the amount-of-substance concentration for a normality of 21.7931 and a valence of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [21.793126, 29.510609] (the system normality (Wikidata Q155854))" + }, + { + "q": "If the normality is 21.7931 and the valence is 29.5106, what is the amount-of-substance concentration?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [21.793126, 29.510609] (the system normality (Wikidata Q155854))" + }, + { + "q": "Could you work out the amount-of-substance concentration given normality = 153.15, valence = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [153.149965, 18.595315] (the system normality (Wikidata Q155854))" + }, + { + "q": "I need the amount-of-substance concentration for a normality of 153.15 and a valence of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [153.149965, 18.595315] (the system normality (Wikidata Q155854))" + }, + { + "q": "If the normality is 8.50728, what is the amount-of-substance concentration?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the valence is not given" + }, + { + "q": "Could you work out the amount-of-substance concentration given normality = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the valence is not given" + }, + { + "q": "Do you know the amount-of-substance concentration 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 amount-of-substance concentration 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 amount-of-substance concentration given normality = -0.318734, valence = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative normality: no example had one" + }, + { + "q": "What is a typical amount-of-substance concentration?", + "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-amount-of-substance-concentra-2/program.py b/root/organs10/physics/specialists/solved-amount-of-substance-concentra-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..06de6700d695ef88118d9bfef174fe1966c4f448 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-concentra-2/program.py @@ -0,0 +1,7 @@ +"""amount-of-substance concentration from normality, valence (normality (Wikidata Q155854)) +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(normality, valence): + return 1.0*normality/valence diff --git a/root/organs10/physics/specialists/solved-amount-of-substance-concentra-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-amount-of-substance-concentra-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-concentra-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-amount-of-substance-concentra-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-amount-of-substance-concentra-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-concentra-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-amount-of-substance-concentra-3/panel.json b/root/organs10/physics/specialists/solved-amount-of-substance-concentra-3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..7af8f7e5d6b00ff347e36f4df48caedb1d4f2c87 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-concentra-3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the amount-of-substance concentration for molar conductivity 107.961 and electrolytic conductivity 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [107.960666, 59.4105] (the system molar conductivity (Wikidata Q1943278))" + }, + { + "q": "Tell me the amount-of-substance concentration with molar conductivity of 107.961 and electrolytic conductivity of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [107.960666, 59.4105] (the system molar conductivity (Wikidata Q1943278))" + }, + { + "q": "What is the amount-of-substance concentration for molar conductivity 169.527 and electrolytic conductivity 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [169.526958, 280.773702] (the system molar conductivity (Wikidata Q1943278))" + }, + { + "q": "Tell me the amount-of-substance concentration with molar conductivity of 169.527 and electrolytic conductivity of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [169.526958, 280.773702] (the system molar conductivity (Wikidata Q1943278))" + }, + { + "q": "What is the amount-of-substance concentration for molar conductivity 1.91195 and electrolytic conductivity 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [1.911952, 82.240072] (the system molar conductivity (Wikidata Q1943278))" + }, + { + "q": "Tell me the amount-of-substance concentration with molar conductivity of 1.91195 and electrolytic conductivity of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [1.911952, 82.240072] (the system molar conductivity (Wikidata Q1943278))" + }, + { + "q": "What is the amount-of-substance concentration for molar conductivity 1.64379 and electrolytic conductivity 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [1.643795, 252.709024] (the system molar conductivity (Wikidata Q1943278))" + }, + { + "q": "Tell me the amount-of-substance concentration with molar conductivity of 1.64379 and electrolytic conductivity of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [1.643795, 252.709024] (the system molar conductivity (Wikidata Q1943278))" + }, + { + "q": "What is the amount-of-substance concentration for molar conductivity 18.3169 and electrolytic conductivity 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [18.316867, 6.838598] (the system molar conductivity (Wikidata Q1943278))" + }, + { + "q": "Tell me the amount-of-substance concentration with molar conductivity of 18.3169 and electrolytic conductivity of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [18.316867, 6.838598] (the system molar conductivity (Wikidata Q1943278))" + }, + { + "q": "What is the amount-of-substance concentration for molar conductivity 156.502 and electrolytic conductivity 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [156.50181, 185.886273] (the system molar conductivity (Wikidata Q1943278))" + }, + { + "q": "Tell me the amount-of-substance concentration with molar conductivity of 156.502 and electrolytic conductivity of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [156.50181, 185.886273] (the system molar conductivity (Wikidata Q1943278))" + }, + { + "q": "What is the amount-of-substance concentration for molar conductivity 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electrolytic conductivity is not given" + }, + { + "q": "Tell me the amount-of-substance concentration with molar conductivity of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electrolytic conductivity is not given" + }, + { + "q": "What is the amount-of-substance concentration 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 amount-of-substance concentration 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 amount-of-substance concentration with molar conductivity of -1.91195 and electrolytic conductivity of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative molar conductivity: no example had one" + }, + { + "q": "Which amount-of-substance concentration 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 conductivity is 8.50728 and the electrolytic conductivity is 17.7888, what is the amount-of-substance concentration?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [8.507283, 17.788778] (the system molar conductivity (Wikidata Q1943278))" + }, + { + "q": "Could you work out the amount-of-substance concentration given molar conductivity = 8.50728, electrolytic conductivity = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [8.507283, 17.788778] (the system molar conductivity (Wikidata Q1943278))" + }, + { + "q": "I need the amount-of-substance concentration for a molar conductivity of 8.24951 and an electrolytic conductivity of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [8.24951, 0.634021] (the system molar conductivity (Wikidata Q1943278))" + }, + { + "q": "If the molar conductivity is 8.24951 and the electrolytic conductivity is 0.634021, what is the amount-of-substance concentration?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [8.24951, 0.634021] (the system molar conductivity (Wikidata Q1943278))" + }, + { + "q": "Could you work out the amount-of-substance concentration given molar conductivity = 0.318734, electrolytic conductivity = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [0.318734, 9.566071] (the system molar conductivity (Wikidata Q1943278))" + }, + { + "q": "I need the amount-of-substance concentration for a molar conductivity of 0.318734 and an electrolytic conductivity of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [0.318734, 9.566071] (the system molar conductivity (Wikidata Q1943278))" + }, + { + "q": "If the molar conductivity is 0.671697 and the electrolytic conductivity is 14.2496, what is the amount-of-substance concentration?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [0.671697, 14.249569] (the system molar conductivity (Wikidata Q1943278))" + }, + { + "q": "Could you work out the amount-of-substance concentration given molar conductivity = 0.671697, electrolytic conductivity = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [0.671697, 14.249569] (the system molar conductivity (Wikidata Q1943278))" + }, + { + "q": "I need the amount-of-substance concentration for a molar conductivity of 21.7931 and an electrolytic conductivity of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [21.793126, 29.510609] (the system molar conductivity (Wikidata Q1943278))" + }, + { + "q": "If the molar conductivity is 21.7931 and the electrolytic conductivity is 29.5106, what is the amount-of-substance concentration?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [21.793126, 29.510609] (the system molar conductivity (Wikidata Q1943278))" + }, + { + "q": "Could you work out the amount-of-substance concentration given molar conductivity = 153.15, electrolytic conductivity = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [153.149965, 18.595315] (the system molar conductivity (Wikidata Q1943278))" + }, + { + "q": "I need the amount-of-substance concentration for a molar conductivity of 153.15 and an electrolytic conductivity of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [153.149965, 18.595315] (the system molar conductivity (Wikidata Q1943278))" + }, + { + "q": "If the molar conductivity is 8.50728, what is the amount-of-substance concentration?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electrolytic conductivity is not given" + }, + { + "q": "Could you work out the amount-of-substance concentration given molar conductivity = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electrolytic conductivity is not given" + }, + { + "q": "Do you know the amount-of-substance concentration 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 amount-of-substance concentration 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 amount-of-substance concentration given molar conductivity = -0.318734, electrolytic conductivity = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative molar conductivity: no example had one" + }, + { + "q": "What is a typical amount-of-substance concentration?", + "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-amount-of-substance-concentra-3/program.py b/root/organs10/physics/specialists/solved-amount-of-substance-concentra-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..841d0e7ef21a37b0a6c59e78348a2251e27f0131 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-concentra-3/program.py @@ -0,0 +1,7 @@ +"""amount-of-substance concentration from molar conductivity, electrolytic conductivity (molar conductivity (Wikidata Q1943278)) +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(molar_conductivity, electrolytic_conductivity): + return 1.0*electrolytic_conductivity/molar_conductivity diff --git a/root/organs10/physics/specialists/solved-amount-of-substance-concentra-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-amount-of-substance-concentra-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-concentra-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-amount-of-substance-concentra-4/organ_formula_v2.py b/root/organs10/physics/specialists/solved-amount-of-substance-concentra-4/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-concentra-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-amount-of-substance-concentra-4/panel.json b/root/organs10/physics/specialists/solved-amount-of-substance-concentra-4/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..e6462fe6e5be0845532823de6c0b89a0c8914b84 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-concentra-4/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the amount-of-substance concentration for molar attenuation coefficient 107.961 and linear attenuation coefficient 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [107.960666, 59.4105] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "Tell me the amount-of-substance concentration with molar attenuation coefficient of 107.961 and linear attenuation coefficient of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [107.960666, 59.4105] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "What is the amount-of-substance concentration for molar attenuation coefficient 169.527 and linear attenuation coefficient 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [169.526958, 280.773702] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "Tell me the amount-of-substance concentration with molar attenuation coefficient of 169.527 and linear attenuation coefficient of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [169.526958, 280.773702] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "What is the amount-of-substance concentration for molar attenuation coefficient 1.91195 and linear attenuation coefficient 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [1.911952, 82.240072] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "Tell me the amount-of-substance concentration with molar attenuation coefficient of 1.91195 and linear attenuation coefficient of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [1.911952, 82.240072] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "What is the amount-of-substance concentration for molar attenuation coefficient 1.64379 and linear attenuation coefficient 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [1.643795, 252.709024] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "Tell me the amount-of-substance concentration with molar attenuation coefficient of 1.64379 and linear attenuation coefficient of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [1.643795, 252.709024] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "What is the amount-of-substance concentration for molar attenuation coefficient 18.3169 and linear attenuation coefficient 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [18.316867, 6.838598] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "Tell me the amount-of-substance concentration with molar attenuation coefficient of 18.3169 and linear attenuation coefficient of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [18.316867, 6.838598] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "What is the amount-of-substance concentration for molar attenuation coefficient 156.502 and linear attenuation coefficient 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [156.50181, 185.886273] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "Tell me the amount-of-substance concentration with molar attenuation coefficient of 156.502 and linear attenuation coefficient of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [156.50181, 185.886273] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "What is the amount-of-substance concentration for molar attenuation coefficient 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the linear attenuation coefficient is not given" + }, + { + "q": "Tell me the amount-of-substance concentration with molar attenuation coefficient of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the linear attenuation coefficient is not given" + }, + { + "q": "What is the amount-of-substance concentration 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 amount-of-substance concentration 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 amount-of-substance concentration with molar attenuation coefficient of -1.91195 and linear attenuation coefficient 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 amount-of-substance concentration 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 linear attenuation coefficient is 17.7888, what is the amount-of-substance concentration?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [8.507283, 17.788778] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "Could you work out the amount-of-substance concentration given molar attenuation coefficient = 8.50728, linear attenuation coefficient = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [8.507283, 17.788778] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "I need the amount-of-substance concentration for a molar attenuation coefficient of 8.24951 and a linear attenuation coefficient of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [8.24951, 0.634021] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "If the molar attenuation coefficient is 8.24951 and the linear attenuation coefficient is 0.634021, what is the amount-of-substance concentration?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [8.24951, 0.634021] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "Could you work out the amount-of-substance concentration given molar attenuation coefficient = 0.318734, linear attenuation coefficient = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [0.318734, 9.566071] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "I need the amount-of-substance concentration for a molar attenuation coefficient of 0.318734 and a linear attenuation coefficient of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [0.318734, 9.566071] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "If the molar attenuation coefficient is 0.671697 and the linear attenuation coefficient is 14.2496, what is the amount-of-substance concentration?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [0.671697, 14.249569] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "Could you work out the amount-of-substance concentration given molar attenuation coefficient = 0.671697, linear attenuation coefficient = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [0.671697, 14.249569] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "I need the amount-of-substance concentration for a molar attenuation coefficient of 21.7931 and a linear attenuation coefficient of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [21.793126, 29.510609] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "If the molar attenuation coefficient is 21.7931 and the linear attenuation coefficient is 29.5106, what is the amount-of-substance concentration?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [21.793126, 29.510609] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "Could you work out the amount-of-substance concentration given molar attenuation coefficient = 153.15, linear attenuation coefficient = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [153.149965, 18.595315] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "I need the amount-of-substance concentration for a molar attenuation coefficient of 153.15 and a linear attenuation coefficient of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [153.149965, 18.595315] (the system molar attenuation coefficient (Wikidata Q98592828))" + }, + { + "q": "If the molar attenuation coefficient is 8.50728, what is the amount-of-substance concentration?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the linear attenuation coefficient is not given" + }, + { + "q": "Could you work out the amount-of-substance concentration given molar attenuation coefficient = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the linear attenuation coefficient is not given" + }, + { + "q": "Do you know the amount-of-substance concentration 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 amount-of-substance concentration 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 amount-of-substance concentration given molar attenuation coefficient = -0.318734, linear attenuation coefficient = 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 amount-of-substance concentration?", + "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-amount-of-substance-concentra-4/program.py b/root/organs10/physics/specialists/solved-amount-of-substance-concentra-4/program.py new file mode 100644 index 0000000000000000000000000000000000000000..afa7ceb1716f494a217582b7dbef779c74cbcfc0 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-concentra-4/program.py @@ -0,0 +1,7 @@ +"""amount-of-substance concentration from molar attenuation coefficient, linear attenuation coefficient (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, linear_attenuation_coefficient): + return 1.0*linear_attenuation_coefficient/molar_attenuation_coefficient diff --git a/root/organs10/physics/specialists/solved-amount-of-substance-concentra-4/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-amount-of-substance-concentra-4/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-concentra-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-amount-of-substance-concentra/organ_formula_v2.py b/root/organs10/physics/specialists/solved-amount-of-substance-concentra/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-concentra/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-amount-of-substance-concentra/panel.json b/root/organs10/physics/specialists/solved-amount-of-substance-concentra/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..d636afe389b6f4d9177a4aded27fb654a270e93e --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-concentra/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the amount-of-substance concentration for amount of substance of component 107.961 and volume 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [107.960666, 59.4105] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "Tell me the amount-of-substance concentration with amount of substance of component of 107.961 and volume of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [107.960666, 59.4105] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "What is the amount-of-substance concentration for amount of substance of component 169.527 and volume 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [169.526958, 280.773702] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "Tell me the amount-of-substance concentration with amount of substance of component of 169.527 and volume of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [169.526958, 280.773702] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "What is the amount-of-substance concentration for amount of substance of component 1.91195 and volume 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [1.911952, 82.240072] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "Tell me the amount-of-substance concentration with amount of substance of component of 1.91195 and volume of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [1.911952, 82.240072] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "What is the amount-of-substance concentration for amount of substance of component 1.64379 and volume 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [1.643795, 252.709024] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "Tell me the amount-of-substance concentration with amount of substance of component of 1.64379 and volume of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [1.643795, 252.709024] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "What is the amount-of-substance concentration for amount of substance of component 18.3169 and volume 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [18.316867, 6.838598] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "Tell me the amount-of-substance concentration with amount of substance of component of 18.3169 and volume of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [18.316867, 6.838598] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "What is the amount-of-substance concentration for amount of substance of component 156.502 and volume 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [156.50181, 185.886273] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "Tell me the amount-of-substance concentration with amount of substance of component of 156.502 and volume of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [156.50181, 185.886273] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "What is the amount-of-substance concentration for amount of substance of component 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the volume is not given" + }, + { + "q": "Tell me the amount-of-substance concentration with amount of substance of component of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the volume is not given" + }, + { + "q": "What is the amount-of-substance concentration 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 amount-of-substance concentration 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 amount-of-substance concentration with amount of substance of component of -1.91195 and volume of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative amount of substance of component: no example had one" + }, + { + "q": "Which amount-of-substance concentration 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 amount of substance of component is 8.50728 and the volume is 17.7888, what is the amount-of-substance concentration?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [8.507283, 17.788778] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "Could you work out the amount-of-substance concentration given amount of substance of component = 8.50728, volume = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [8.507283, 17.788778] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "I need the amount-of-substance concentration for an amount of substance of component of 8.24951 and a volume of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [8.24951, 0.634021] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "If the amount of substance of component is 8.24951 and the volume is 0.634021, what is the amount-of-substance concentration?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [8.24951, 0.634021] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "Could you work out the amount-of-substance concentration given amount of substance of component = 0.318734, volume = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [0.318734, 9.566071] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "I need the amount-of-substance concentration for an amount of substance of component of 0.318734 and a volume of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [0.318734, 9.566071] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "If the amount of substance of component is 0.671697 and the volume is 14.2496, what is the amount-of-substance concentration?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [0.671697, 14.249569] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "Could you work out the amount-of-substance concentration given amount of substance of component = 0.671697, volume = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [0.671697, 14.249569] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "I need the amount-of-substance concentration for an amount of substance of component of 21.7931 and a volume of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [21.793126, 29.510609] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "If the amount of substance of component is 21.7931 and the volume is 29.5106, what is the amount-of-substance concentration?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [21.793126, 29.510609] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "Could you work out the amount-of-substance concentration given amount of substance of component = 153.15, volume = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [153.149965, 18.595315] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "I need the amount-of-substance concentration for an amount of substance of component of 153.15 and a volume of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "mol/m^3", + "why": "amount-of-substance concentration for [153.149965, 18.595315] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "If the amount of substance of component is 8.50728, what is the amount-of-substance concentration?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the volume is not given" + }, + { + "q": "Could you work out the amount-of-substance concentration given amount of substance of component = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the volume is not given" + }, + { + "q": "Do you know the amount-of-substance concentration 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 amount-of-substance concentration 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 amount-of-substance concentration given amount of substance of component = -0.318734, volume = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative amount of substance of component: no example had one" + }, + { + "q": "What is a typical amount-of-substance concentration?", + "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-amount-of-substance-concentra/program.py b/root/organs10/physics/specialists/solved-amount-of-substance-concentra/program.py new file mode 100644 index 0000000000000000000000000000000000000000..3e85ca3d53f9664fc0affce37d64a65045de319a --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-concentra/program.py @@ -0,0 +1,7 @@ +"""amount-of-substance concentration from amount of substance of component, volume (amount-of-substance concentration (Wikidata Q672821)) +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(amount_of_substance_of_component, volume): + return 1.0*amount_of_substance_of_component/volume diff --git a/root/organs10/physics/specialists/solved-amount-of-substance-concentra/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-amount-of-substance-concentra/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-concentra/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-amount-of-substance-fraction-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-amount-of-substance-fraction-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-fraction-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-amount-of-substance-fraction-2/panel.json b/root/organs10/physics/specialists/solved-amount-of-substance-fraction-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..d35f5fd19a8e925526895101d3878e49adeb17c0 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-fraction-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the amount-of-substance fraction for partial pressure 107.961 and pressure 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [107.960666, 59.4105] (the system partial pressure (Wikidata Q27165))" + }, + { + "q": "Tell me the amount-of-substance fraction with partial pressure of 107.961 and pressure of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [107.960666, 59.4105] (the system partial pressure (Wikidata Q27165))" + }, + { + "q": "What is the amount-of-substance fraction for partial pressure 169.527 and pressure 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [169.526958, 280.773702] (the system partial pressure (Wikidata Q27165))" + }, + { + "q": "Tell me the amount-of-substance fraction with partial pressure of 169.527 and pressure of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [169.526958, 280.773702] (the system partial pressure (Wikidata Q27165))" + }, + { + "q": "What is the amount-of-substance fraction for partial pressure 1.91195 and pressure 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [1.911952, 82.240072] (the system partial pressure (Wikidata Q27165))" + }, + { + "q": "Tell me the amount-of-substance fraction with partial pressure of 1.91195 and pressure of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [1.911952, 82.240072] (the system partial pressure (Wikidata Q27165))" + }, + { + "q": "What is the amount-of-substance fraction for partial pressure 1.64379 and pressure 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [1.643795, 252.709024] (the system partial pressure (Wikidata Q27165))" + }, + { + "q": "Tell me the amount-of-substance fraction with partial pressure of 1.64379 and pressure of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [1.643795, 252.709024] (the system partial pressure (Wikidata Q27165))" + }, + { + "q": "What is the amount-of-substance fraction for partial pressure 18.3169 and pressure 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [18.316867, 6.838598] (the system partial pressure (Wikidata Q27165))" + }, + { + "q": "Tell me the amount-of-substance fraction with partial pressure of 18.3169 and pressure of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [18.316867, 6.838598] (the system partial pressure (Wikidata Q27165))" + }, + { + "q": "What is the amount-of-substance fraction for partial pressure 156.502 and pressure 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [156.50181, 185.886273] (the system partial pressure (Wikidata Q27165))" + }, + { + "q": "Tell me the amount-of-substance fraction with partial pressure of 156.502 and pressure of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [156.50181, 185.886273] (the system partial pressure (Wikidata Q27165))" + }, + { + "q": "What is the amount-of-substance fraction for partial pressure 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the pressure is not given" + }, + { + "q": "Tell me the amount-of-substance fraction with partial pressure of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the pressure is not given" + }, + { + "q": "What is the amount-of-substance fraction 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 amount-of-substance fraction 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 amount-of-substance fraction with partial pressure of -1.91195 and pressure of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative partial pressure: no example had one" + }, + { + "q": "Which amount-of-substance fraction 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 partial pressure is 8.50728 and the pressure is 17.7888, what is the amount-of-substance fraction?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [8.507283, 17.788778] (the system partial pressure (Wikidata Q27165))" + }, + { + "q": "Could you work out the amount-of-substance fraction given partial pressure = 8.50728, pressure = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [8.507283, 17.788778] (the system partial pressure (Wikidata Q27165))" + }, + { + "q": "I need the amount-of-substance fraction for a partial pressure of 8.24951 and a pressure of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [8.24951, 0.634021] (the system partial pressure (Wikidata Q27165))" + }, + { + "q": "If the partial pressure is 8.24951 and the pressure is 0.634021, what is the amount-of-substance fraction?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [8.24951, 0.634021] (the system partial pressure (Wikidata Q27165))" + }, + { + "q": "Could you work out the amount-of-substance fraction given partial pressure = 0.318734, pressure = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [0.318734, 9.566071] (the system partial pressure (Wikidata Q27165))" + }, + { + "q": "I need the amount-of-substance fraction for a partial pressure of 0.318734 and a pressure of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [0.318734, 9.566071] (the system partial pressure (Wikidata Q27165))" + }, + { + "q": "If the partial pressure is 0.671697 and the pressure is 14.2496, what is the amount-of-substance fraction?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [0.671697, 14.249569] (the system partial pressure (Wikidata Q27165))" + }, + { + "q": "Could you work out the amount-of-substance fraction given partial pressure = 0.671697, pressure = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [0.671697, 14.249569] (the system partial pressure (Wikidata Q27165))" + }, + { + "q": "I need the amount-of-substance fraction for a partial pressure of 21.7931 and a pressure of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [21.793126, 29.510609] (the system partial pressure (Wikidata Q27165))" + }, + { + "q": "If the partial pressure is 21.7931 and the pressure is 29.5106, what is the amount-of-substance fraction?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [21.793126, 29.510609] (the system partial pressure (Wikidata Q27165))" + }, + { + "q": "Could you work out the amount-of-substance fraction given partial pressure = 153.15, pressure = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [153.149965, 18.595315] (the system partial pressure (Wikidata Q27165))" + }, + { + "q": "I need the amount-of-substance fraction for a partial pressure of 153.15 and a pressure of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [153.149965, 18.595315] (the system partial pressure (Wikidata Q27165))" + }, + { + "q": "If the partial pressure is 8.50728, what is the amount-of-substance fraction?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the pressure is not given" + }, + { + "q": "Could you work out the amount-of-substance fraction given partial pressure = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the pressure is not given" + }, + { + "q": "Do you know the amount-of-substance fraction 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 amount-of-substance fraction 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 amount-of-substance fraction given partial pressure = -0.318734, pressure = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative partial pressure: no example had one" + }, + { + "q": "What is a typical amount-of-substance fraction?", + "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-amount-of-substance-fraction-2/program.py b/root/organs10/physics/specialists/solved-amount-of-substance-fraction-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..d26c30269515cb677f571024bdb6b4ea1f05504e --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-fraction-2/program.py @@ -0,0 +1,7 @@ +"""amount-of-substance fraction from partial pressure, pressure (partial pressure (Wikidata Q27165)) +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(partial_pressure, pressure): + return 1.0*partial_pressure/pressure diff --git a/root/organs10/physics/specialists/solved-amount-of-substance-fraction-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-amount-of-substance-fraction-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-fraction-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-amount-of-substance-fraction-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-amount-of-substance-fraction-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-fraction-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-amount-of-substance-fraction-3/panel.json b/root/organs10/physics/specialists/solved-amount-of-substance-fraction-3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..580e2bdaa18fdbd72cb1590da93eb0f92bc25d3f --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-fraction-3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the amount-of-substance fraction for absolute activity of pure substance 107.961, activity factor 59.4105 and absolute chemical activity 5.77717?", + "a": "0.0009007120628380035", + "valid": true, + "kind": "compute", + "expect": 0.0009007120628380035, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [107.960666, 59.4105, 5.777165] (the system activity factor (Wikidata Q89335167))" + }, + { + "q": "Tell me the amount-of-substance fraction with absolute activity of pure substance of 107.961, activity factor of 59.4105 and absolute chemical activity of 5.77717.", + "a": "0.0009007120628380035", + "valid": true, + "kind": "compute", + "expect": 0.0009007120628380035, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [107.960666, 59.4105, 5.777165] (the system activity factor (Wikidata Q89335167))" + }, + { + "q": "What is the amount-of-substance fraction for absolute activity of pure substance 85.2416, activity factor 160.878 and absolute chemical activity 2.69428?", + "a": "0.00019646859918324614", + "valid": true, + "kind": "compute", + "expect": 0.00019646859918324614, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [85.241623, 160.878442, 2.69428] (the system activity factor (Wikidata Q89335167))" + }, + { + "q": "Tell me the amount-of-substance fraction with absolute activity of pure substance of 85.2416, activity factor of 160.878 and absolute chemical activity of 2.69428.", + "a": "0.00019646859918324614", + "valid": true, + "kind": "compute", + "expect": 0.00019646859918324614, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [85.241623, 160.878442, 2.69428] (the system activity factor (Wikidata Q89335167))" + }, + { + "q": "What is the amount-of-substance fraction for absolute activity of pure substance 45.6157, activity factor 4.97131 and absolute chemical activity 94.305?", + "a": "0.41586296491008656", + "valid": true, + "kind": "compute", + "expect": 0.41586296491008656, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [45.61567, 4.971307, 94.305046] (the system activity factor (Wikidata Q89335167))" + }, + { + "q": "Tell me the amount-of-substance fraction with absolute activity of pure substance of 45.6157, activity factor of 4.97131 and absolute chemical activity of 94.305.", + "a": "0.41586296491008656", + "valid": true, + "kind": "compute", + "expect": 0.41586296491008656, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [45.61567, 4.971307, 94.305046] (the system activity factor (Wikidata Q89335167))" + }, + { + "q": "What is the amount-of-substance fraction for absolute activity of pure substance 10.5638, activity factor 198.839 and absolute chemical activity 0.671697?", + "a": "0.00031977914540554195", + "valid": true, + "kind": "compute", + "expect": 0.00031977914540554195, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [10.563843, 198.838955, 0.671697] (the system activity factor (Wikidata Q89335167))" + }, + { + "q": "Tell me the amount-of-substance fraction with absolute activity of pure substance of 10.5638, activity factor of 198.839 and absolute chemical activity of 0.671697.", + "a": "0.00031977914540554195", + "valid": true, + "kind": "compute", + "expect": 0.00031977914540554195, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [10.563843, 198.838955, 0.671697] (the system activity factor (Wikidata Q89335167))" + }, + { + "q": "What is the amount-of-substance fraction for absolute activity of pure substance 21.7931, activity factor 29.5106 and absolute chemical activity 8.50341?", + "a": "0.01322194266129286", + "valid": true, + "kind": "compute", + "expect": 0.01322194266129286, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [21.793126, 29.510609, 8.503407] (the system activity factor (Wikidata Q89335167))" + }, + { + "q": "Tell me the amount-of-substance fraction with absolute activity of pure substance of 21.7931, activity factor of 29.5106 and absolute chemical activity of 8.50341.", + "a": "0.01322194266129286", + "valid": true, + "kind": "compute", + "expect": 0.01322194266129286, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [21.793126, 29.510609, 8.503407] (the system activity factor (Wikidata Q89335167))" + }, + { + "q": "What is the amount-of-substance fraction for absolute activity of pure substance 223.509, activity factor 17.3414 and absolute chemical activity 7.10705?", + "a": "0.0018336213959416821", + "valid": true, + "kind": "compute", + "expect": 0.0018336213959416821, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [223.508796, 17.341435, 7.107049] (the system activity factor (Wikidata Q89335167))" + }, + { + "q": "Tell me the amount-of-substance fraction with absolute activity of pure substance of 223.509, activity factor of 17.3414 and absolute chemical activity of 7.10705.", + "a": "0.0018336213959416821", + "valid": true, + "kind": "compute", + "expect": 0.0018336213959416821, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [223.508796, 17.341435, 7.107049] (the system activity factor (Wikidata Q89335167))" + }, + { + "q": "What is the amount-of-substance fraction for absolute activity of pure substance 107.961 and activity factor 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the absolute chemical activity is not given" + }, + { + "q": "Tell me the amount-of-substance fraction with absolute activity of pure substance of 85.2416 and activity factor of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the absolute chemical activity is not given" + }, + { + "q": "What is the amount-of-substance fraction 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 amount-of-substance fraction 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 amount-of-substance fraction with absolute activity of pure substance of -45.6157, activity factor of 4.97131 and absolute chemical activity of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative absolute activity of pure substance: no example had one" + }, + { + "q": "Which amount-of-substance fraction 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 absolute activity of pure substance is 17.7888, activity factor is 167.625 and the absolute chemical activity is 10.3291, what is the amount-of-substance fraction?", + "a": "0.0034639833311190396", + "valid": true, + "kind": "compute", + "expect": 0.0034639833311190396, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [17.788778, 167.624948, 10.329054] (the system activity factor (Wikidata Q89335167))" + }, + { + "q": "Could you work out the amount-of-substance fraction given absolute activity of pure substance = 17.7888, activity factor = 167.625, absolute chemical activity = 10.3291?", + "a": "0.0034639833311190396", + "valid": true, + "kind": "compute", + "expect": 0.0034639833311190396, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [17.788778, 167.624948, 10.329054] (the system activity factor (Wikidata Q89335167))" + }, + { + "q": "I need the amount-of-substance fraction for an absolute activity of pure substance of 21.511, an activity factor of 173.394 and an absolute chemical activity of 251.084, please.", + "a": "0.06731704184793699", + "valid": true, + "kind": "compute", + "expect": 0.06731704184793699, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [21.511016, 173.393638, 251.084037] (the system activity factor (Wikidata Q89335167))" + }, + { + "q": "If the absolute activity of pure substance is 21.511, activity factor is 173.394 and the absolute chemical activity is 251.084, what is the amount-of-substance fraction?", + "a": "0.06731704184793699", + "valid": true, + "kind": "compute", + "expect": 0.06731704184793699, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [21.511016, 173.393638, 251.084037] (the system activity factor (Wikidata Q89335167))" + }, + { + "q": "Could you work out the amount-of-substance fraction given absolute activity of pure substance = 129.245, activity factor = 1.18359, absolute chemical activity = 15.9416?", + "a": "0.10421188487480892", + "valid": true, + "kind": "compute", + "expect": 0.10421188487480892, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [129.244984, 1.18359, 15.941612] (the system activity factor (Wikidata Q89335167))" + }, + { + "q": "I need the amount-of-substance fraction for an absolute activity of pure substance of 129.245, an activity factor of 1.18359 and an absolute chemical activity of 15.9416, please.", + "a": "0.10421188487480892", + "valid": true, + "kind": "compute", + "expect": 0.10421188487480892, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [129.244984, 1.18359, 15.941612] (the system activity factor (Wikidata Q89335167))" + }, + { + "q": "If the absolute activity of pure substance is 20.3966, activity factor is 18.3169 and the absolute chemical activity is 6.8386, what is the amount-of-substance fraction?", + "a": "0.018304547714967156", + "valid": true, + "kind": "compute", + "expect": 0.018304547714967156, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [20.396558, 18.316867, 6.838598] (the system activity factor (Wikidata Q89335167))" + }, + { + "q": "Could you work out the amount-of-substance fraction given absolute activity of pure substance = 20.3966, activity factor = 18.3169, absolute chemical activity = 6.8386?", + "a": "0.018304547714967156", + "valid": true, + "kind": "compute", + "expect": 0.018304547714967156, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [20.396558, 18.316867, 6.838598] (the system activity factor (Wikidata Q89335167))" + }, + { + "q": "I need the amount-of-substance fraction for an absolute activity of pure substance of 185.886, an activity factor of 13.2373 and an absolute chemical activity of 4.71941, please.", + "a": "0.0019179666725273497", + "valid": true, + "kind": "compute", + "expect": 0.0019179666725273497, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [185.886273, 13.237311, 4.719415] (the system activity factor (Wikidata Q89335167))" + }, + { + "q": "If the absolute activity of pure substance is 185.886, activity factor is 13.2373 and the absolute chemical activity is 4.71941, what is the amount-of-substance fraction?", + "a": "0.0019179666725273497", + "valid": true, + "kind": "compute", + "expect": 0.0019179666725273497, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [185.886273, 13.237311, 4.719415] (the system activity factor (Wikidata Q89335167))" + }, + { + "q": "Could you work out the amount-of-substance fraction given absolute activity of pure substance = 9.1061, activity factor = 24.5721, absolute chemical activity = 108.451?", + "a": "0.4846833487934037", + "valid": true, + "kind": "compute", + "expect": 0.4846833487934037, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [9.106104, 24.572086, 108.450792] (the system activity factor (Wikidata Q89335167))" + }, + { + "q": "I need the amount-of-substance fraction for an absolute activity of pure substance of 9.1061, an activity factor of 24.5721 and an absolute chemical activity of 108.451, please.", + "a": "0.4846833487934037", + "valid": true, + "kind": "compute", + "expect": 0.4846833487934037, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [9.106104, 24.572086, 108.450792] (the system activity factor (Wikidata Q89335167))" + }, + { + "q": "If the absolute activity of pure substance is 17.7888 and the activity factor is 167.625, what is the amount-of-substance fraction?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the absolute chemical activity is not given" + }, + { + "q": "Could you work out the amount-of-substance fraction given absolute activity of pure substance = 21.511, activity factor = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the absolute chemical activity is not given" + }, + { + "q": "Do you know the amount-of-substance fraction 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 amount-of-substance fraction 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 amount-of-substance fraction given absolute activity of pure substance = -129.245, activity factor = 1.18359, absolute chemical activity = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative absolute activity of pure substance: no example had one" + }, + { + "q": "What is a typical amount-of-substance fraction?", + "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-amount-of-substance-fraction-3/program.py b/root/organs10/physics/specialists/solved-amount-of-substance-fraction-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..b5bb5f9cf49851dbe949ef2a2bce9a71e04c33c4 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-fraction-3/program.py @@ -0,0 +1,7 @@ +"""amount-of-substance fraction from absolute activity of pure substance, activity factor, absolute chemical activity (activity factor (Wikidata Q89335167)) +The maths organ (systems.py): solved from the system q1 = ((q2)/(q0*q3)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(absolute_activity_of_pure_substance, activity_factor, absolute_chemical_activity): + return 1.0*absolute_chemical_activity/(absolute_activity_of_pure_substance*activity_factor) diff --git a/root/organs10/physics/specialists/solved-amount-of-substance-fraction-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-amount-of-substance-fraction-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-fraction-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-amount-of-substance-fraction/organ_formula_v2.py b/root/organs10/physics/specialists/solved-amount-of-substance-fraction/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-fraction/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-amount-of-substance-fraction/panel.json b/root/organs10/physics/specialists/solved-amount-of-substance-fraction/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..1c99ebc17c950803423535cd53feb57c6c7b0d27 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-fraction/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the amount-of-substance fraction for amount of substance of component 107.961 and amount of substance 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [107.960666, 59.4105] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Tell me the amount-of-substance fraction with amount of substance of component of 107.961 and amount of substance of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [107.960666, 59.4105] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "What is the amount-of-substance fraction for amount of substance of component 169.527 and amount of substance 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [169.526958, 280.773702] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Tell me the amount-of-substance fraction with amount of substance of component of 169.527 and amount of substance of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [169.526958, 280.773702] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "What is the amount-of-substance fraction for amount of substance of component 1.91195 and amount of substance 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [1.911952, 82.240072] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Tell me the amount-of-substance fraction with amount of substance of component of 1.91195 and amount of substance of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [1.911952, 82.240072] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "What is the amount-of-substance fraction for amount of substance of component 1.64379 and amount of substance 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [1.643795, 252.709024] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Tell me the amount-of-substance fraction with amount of substance of component of 1.64379 and amount of substance of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [1.643795, 252.709024] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "What is the amount-of-substance fraction for amount of substance of component 18.3169 and amount of substance 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [18.316867, 6.838598] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Tell me the amount-of-substance fraction with amount of substance of component of 18.3169 and amount of substance of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [18.316867, 6.838598] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "What is the amount-of-substance fraction for amount of substance of component 156.502 and amount of substance 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [156.50181, 185.886273] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Tell me the amount-of-substance fraction with amount of substance of component of 156.502 and amount of substance of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [156.50181, 185.886273] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "What is the amount-of-substance fraction for amount of substance of component 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 amount-of-substance fraction with amount of substance of component 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 amount-of-substance fraction 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 amount-of-substance fraction 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 amount-of-substance fraction with amount of substance of component 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 amount of substance of component: no example had one" + }, + { + "q": "Which amount-of-substance fraction 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 amount of substance of component is 8.50728 and the amount of substance is 17.7888, what is the amount-of-substance fraction?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [8.507283, 17.788778] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Could you work out the amount-of-substance fraction given amount of substance of component = 8.50728, amount of substance = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [8.507283, 17.788778] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "I need the amount-of-substance fraction for an amount of substance of component of 8.24951 and an amount of substance of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [8.24951, 0.634021] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "If the amount of substance of component is 8.24951 and the amount of substance is 0.634021, what is the amount-of-substance fraction?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [8.24951, 0.634021] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Could you work out the amount-of-substance fraction given amount of substance of component = 0.318734, amount of substance = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [0.318734, 9.566071] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "I need the amount-of-substance fraction for an amount of substance of component of 0.318734 and an amount of substance of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [0.318734, 9.566071] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "If the amount of substance of component is 0.671697 and the amount of substance is 14.2496, what is the amount-of-substance fraction?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [0.671697, 14.249569] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Could you work out the amount-of-substance fraction given amount of substance of component = 0.671697, amount of substance = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [0.671697, 14.249569] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "I need the amount-of-substance fraction for an amount of substance of component of 21.7931 and an amount of substance of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [21.793126, 29.510609] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "If the amount of substance of component is 21.7931 and the amount of substance is 29.5106, what is the amount-of-substance fraction?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [21.793126, 29.510609] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Could you work out the amount-of-substance fraction given amount of substance of component = 153.15, amount of substance = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [153.149965, 18.595315] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "I need the amount-of-substance fraction for an amount of substance of component of 153.15 and an amount of substance of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "amount-of-substance fraction for [153.149965, 18.595315] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "If the amount of substance of component is 8.50728, what is the amount-of-substance fraction?", + "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 amount-of-substance fraction given amount of substance of component = 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 amount-of-substance fraction 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 amount-of-substance fraction 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 amount-of-substance fraction given amount of substance of component = -0.318734, amount of substance = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative amount of substance of component: no example had one" + }, + { + "q": "What is a typical amount-of-substance fraction?", + "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-amount-of-substance-fraction/program.py b/root/organs10/physics/specialists/solved-amount-of-substance-fraction/program.py new file mode 100644 index 0000000000000000000000000000000000000000..4052bdfc31571214a9f08601da8790ff5a693fb5 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-fraction/program.py @@ -0,0 +1,7 @@ +"""amount-of-substance fraction from amount of substance of component, amount of substance (amount-of-substance fraction (Wikidata Q125264)) +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(amount_of_substance_of_component, amount_of_substance): + return 1.0*amount_of_substance_of_component/amount_of_substance diff --git a/root/organs10/physics/specialists/solved-amount-of-substance-fraction/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-amount-of-substance-fraction/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-fraction/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-amount-of-substance-of-compon-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-amount-of-substance-of-compon-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-of-compon-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-amount-of-substance-of-compon-2/panel.json b/root/organs10/physics/specialists/solved-amount-of-substance-of-compon-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..9e838fe2c0d0de93e89d3a0c564a46c5fd89d8f6 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-of-compon-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the amount of substance of component for amount-of-substance concentration 107.961 and volume 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [107.960666, 59.4105] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "Tell me the amount of substance of component with amount-of-substance concentration of 107.961 and volume of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [107.960666, 59.4105] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "What is the amount of substance of component for amount-of-substance concentration 169.527 and volume 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [169.526958, 280.773702] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "Tell me the amount of substance of component with amount-of-substance concentration of 169.527 and volume of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [169.526958, 280.773702] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "What is the amount of substance of component for amount-of-substance concentration 1.91195 and volume 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [1.911952, 82.240072] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "Tell me the amount of substance of component with amount-of-substance concentration of 1.91195 and volume of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [1.911952, 82.240072] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "What is the amount of substance of component for amount-of-substance concentration 1.64379 and volume 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [1.643795, 252.709024] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "Tell me the amount of substance of component with amount-of-substance concentration of 1.64379 and volume of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [1.643795, 252.709024] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "What is the amount of substance of component for amount-of-substance concentration 18.3169 and volume 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [18.316867, 6.838598] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "Tell me the amount of substance of component with amount-of-substance concentration of 18.3169 and volume of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [18.316867, 6.838598] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "What is the amount of substance of component for amount-of-substance concentration 156.502 and volume 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [156.50181, 185.886273] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "Tell me the amount of substance of component with amount-of-substance concentration of 156.502 and volume of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [156.50181, 185.886273] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "What is the amount of substance of component for amount-of-substance concentration 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the volume is not given" + }, + { + "q": "Tell me the amount of substance of component with amount-of-substance concentration of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the volume is not given" + }, + { + "q": "What is the amount of substance of component 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 amount of substance of component 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 amount of substance of component with amount-of-substance concentration of -1.91195 and volume of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative amount-of-substance concentration: no example had one" + }, + { + "q": "Which amount of substance of component 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 amount-of-substance concentration is 8.50728 and the volume is 17.7888, what is the amount of substance of component?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [8.507283, 17.788778] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "Could you work out the amount of substance of component given amount-of-substance concentration = 8.50728, volume = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [8.507283, 17.788778] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "I need the amount of substance of component for an amount-of-substance concentration of 8.24951 and a volume of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [8.24951, 0.634021] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "If the amount-of-substance concentration is 8.24951 and the volume is 0.634021, what is the amount of substance of component?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [8.24951, 0.634021] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "Could you work out the amount of substance of component given amount-of-substance concentration = 0.318734, volume = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [0.318734, 9.566071] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "I need the amount of substance of component for an amount-of-substance concentration of 0.318734 and a volume of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [0.318734, 9.566071] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "If the amount-of-substance concentration is 0.671697 and the volume is 14.2496, what is the amount of substance of component?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [0.671697, 14.249569] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "Could you work out the amount of substance of component given amount-of-substance concentration = 0.671697, volume = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [0.671697, 14.249569] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "I need the amount of substance of component for an amount-of-substance concentration of 21.7931 and a volume of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [21.793126, 29.510609] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "If the amount-of-substance concentration is 21.7931 and the volume is 29.5106, what is the amount of substance of component?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [21.793126, 29.510609] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "Could you work out the amount of substance of component given amount-of-substance concentration = 153.15, volume = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [153.149965, 18.595315] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "I need the amount of substance of component for an amount-of-substance concentration of 153.15 and a volume of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [153.149965, 18.595315] (the system amount-of-substance concentration (Wikidata Q672821))" + }, + { + "q": "If the amount-of-substance concentration is 8.50728, what is the amount of substance of component?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the volume is not given" + }, + { + "q": "Could you work out the amount of substance of component given amount-of-substance concentration = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the volume is not given" + }, + { + "q": "Do you know the amount of substance of component 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 amount of substance of component 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 amount of substance of component given amount-of-substance concentration = -0.318734, volume = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative amount-of-substance concentration: no example had one" + }, + { + "q": "What is a typical amount of substance of component?", + "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-amount-of-substance-of-compon-2/program.py b/root/organs10/physics/specialists/solved-amount-of-substance-of-compon-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..29bd08bf7fc96676d079159ef695291827475279 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-of-compon-2/program.py @@ -0,0 +1,7 @@ +"""amount of substance of component from amount-of-substance concentration, volume (amount-of-substance concentration (Wikidata Q672821)) +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(amount_of_substance_concentration, volume): + return 1.0*amount_of_substance_concentration*volume diff --git a/root/organs10/physics/specialists/solved-amount-of-substance-of-compon-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-amount-of-substance-of-compon-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-of-compon-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-amount-of-substance-of-compon-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-amount-of-substance-of-compon-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-of-compon-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-amount-of-substance-of-compon-3/panel.json b/root/organs10/physics/specialists/solved-amount-of-substance-of-compon-3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..945899b11eb45a0d5b8768dbf77337b32f0d631c --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-of-compon-3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the amount of substance of component for amount-of-substance fraction 107.961 and amount of substance 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [107.960666, 59.4105] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Tell me the amount of substance of component with amount-of-substance fraction of 107.961 and amount of substance of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [107.960666, 59.4105] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "What is the amount of substance of component for amount-of-substance fraction 169.527 and amount of substance 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [169.526958, 280.773702] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Tell me the amount of substance of component with amount-of-substance fraction of 169.527 and amount of substance of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [169.526958, 280.773702] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "What is the amount of substance of component for amount-of-substance fraction 1.91195 and amount of substance 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [1.911952, 82.240072] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Tell me the amount of substance of component with amount-of-substance fraction of 1.91195 and amount of substance of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [1.911952, 82.240072] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "What is the amount of substance of component for amount-of-substance fraction 1.64379 and amount of substance 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [1.643795, 252.709024] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Tell me the amount of substance of component with amount-of-substance fraction of 1.64379 and amount of substance of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [1.643795, 252.709024] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "What is the amount of substance of component for amount-of-substance fraction 18.3169 and amount of substance 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [18.316867, 6.838598] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Tell me the amount of substance of component with amount-of-substance fraction of 18.3169 and amount of substance of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [18.316867, 6.838598] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "What is the amount of substance of component for amount-of-substance fraction 156.502 and amount of substance 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [156.50181, 185.886273] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Tell me the amount of substance of component with amount-of-substance fraction of 156.502 and amount of substance of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [156.50181, 185.886273] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "What is the amount of substance of component for amount-of-substance fraction 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 amount of substance of component with amount-of-substance fraction 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 amount of substance of component 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 amount of substance of component 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 amount of substance of component with amount-of-substance fraction 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 amount-of-substance fraction: no example had one" + }, + { + "q": "Which amount of substance of component 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 amount-of-substance fraction is 8.50728 and the amount of substance is 17.7888, what is the amount of substance of component?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [8.507283, 17.788778] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Could you work out the amount of substance of component given amount-of-substance fraction = 8.50728, amount of substance = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [8.507283, 17.788778] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "I need the amount of substance of component for an amount-of-substance fraction 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": "mol", + "why": "amount of substance of component for [8.24951, 0.634021] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "If the amount-of-substance fraction is 8.24951 and the amount of substance is 0.634021, what is the amount of substance of component?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [8.24951, 0.634021] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Could you work out the amount of substance of component given amount-of-substance fraction = 0.318734, amount of substance = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [0.318734, 9.566071] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "I need the amount of substance of component for an amount-of-substance fraction 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": "mol", + "why": "amount of substance of component for [0.318734, 9.566071] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "If the amount-of-substance fraction is 0.671697 and the amount of substance is 14.2496, what is the amount of substance of component?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [0.671697, 14.249569] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Could you work out the amount of substance of component given amount-of-substance fraction = 0.671697, amount of substance = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [0.671697, 14.249569] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "I need the amount of substance of component for an amount-of-substance fraction 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": "mol", + "why": "amount of substance of component for [21.793126, 29.510609] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "If the amount-of-substance fraction is 21.7931 and the amount of substance is 29.5106, what is the amount of substance of component?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [21.793126, 29.510609] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "Could you work out the amount of substance of component given amount-of-substance fraction = 153.15, amount of substance = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [153.149965, 18.595315] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "I need the amount of substance of component for an amount-of-substance fraction 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": "mol", + "why": "amount of substance of component for [153.149965, 18.595315] (the system amount-of-substance fraction (Wikidata Q125264))" + }, + { + "q": "If the amount-of-substance fraction is 8.50728, what is the amount of substance of component?", + "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 amount of substance of component given amount-of-substance fraction = 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 amount of substance of component 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 amount of substance of component 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 amount of substance of component given amount-of-substance fraction = -0.318734, amount of substance = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative amount-of-substance fraction: no example had one" + }, + { + "q": "What is a typical amount of substance of component?", + "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-amount-of-substance-of-compon-3/program.py b/root/organs10/physics/specialists/solved-amount-of-substance-of-compon-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..2cc213fd7568c316d50569a624fff17fae872214 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-of-compon-3/program.py @@ -0,0 +1,7 @@ +"""amount of substance of component from amount-of-substance fraction, amount of substance (amount-of-substance fraction (Wikidata Q125264)) +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(amount_of_substance_fraction, amount_of_substance): + return 1.0*amount_of_substance*amount_of_substance_fraction diff --git a/root/organs10/physics/specialists/solved-amount-of-substance-of-compon-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-amount-of-substance-of-compon-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-of-compon-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-amount-of-substance-of-compon/organ_formula_v2.py b/root/organs10/physics/specialists/solved-amount-of-substance-of-compon/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-of-compon/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-amount-of-substance-of-compon/panel.json b/root/organs10/physics/specialists/solved-amount-of-substance-of-compon/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..0bdebd604a323402c52005d9a68a5486fd0dad49 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-of-compon/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the amount of substance of component for molality 107.961 and mass of component 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [107.960666, 59.4105] (the system molality (Wikidata Q172623))" + }, + { + "q": "Tell me the amount of substance of component with molality of 107.961 and mass of component of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [107.960666, 59.4105] (the system molality (Wikidata Q172623))" + }, + { + "q": "What is the amount of substance of component for molality 169.527 and mass of component 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [169.526958, 280.773702] (the system molality (Wikidata Q172623))" + }, + { + "q": "Tell me the amount of substance of component with molality of 169.527 and mass of component of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [169.526958, 280.773702] (the system molality (Wikidata Q172623))" + }, + { + "q": "What is the amount of substance of component for molality 1.91195 and mass of component 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [1.911952, 82.240072] (the system molality (Wikidata Q172623))" + }, + { + "q": "Tell me the amount of substance of component with molality of 1.91195 and mass of component of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [1.911952, 82.240072] (the system molality (Wikidata Q172623))" + }, + { + "q": "What is the amount of substance of component for molality 1.64379 and mass of component 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [1.643795, 252.709024] (the system molality (Wikidata Q172623))" + }, + { + "q": "Tell me the amount of substance of component with molality of 1.64379 and mass of component of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [1.643795, 252.709024] (the system molality (Wikidata Q172623))" + }, + { + "q": "What is the amount of substance of component for molality 18.3169 and mass of component 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [18.316867, 6.838598] (the system molality (Wikidata Q172623))" + }, + { + "q": "Tell me the amount of substance of component with molality of 18.3169 and mass of component of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [18.316867, 6.838598] (the system molality (Wikidata Q172623))" + }, + { + "q": "What is the amount of substance of component for molality 156.502 and mass of component 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [156.50181, 185.886273] (the system molality (Wikidata Q172623))" + }, + { + "q": "Tell me the amount of substance of component with molality of 156.502 and mass of component of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [156.50181, 185.886273] (the system molality (Wikidata Q172623))" + }, + { + "q": "What is the amount of substance of component for molality 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass of component is not given" + }, + { + "q": "Tell me the amount of substance of component with molality of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass of component is not given" + }, + { + "q": "What is the amount of substance of component 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 amount of substance of component 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 amount of substance of component with molality of -1.91195 and mass of component of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative molality: no example had one" + }, + { + "q": "Which amount of substance of component 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 molality is 8.50728 and the mass of component is 17.7888, what is the amount of substance of component?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [8.507283, 17.788778] (the system molality (Wikidata Q172623))" + }, + { + "q": "Could you work out the amount of substance of component given molality = 8.50728, mass of component = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [8.507283, 17.788778] (the system molality (Wikidata Q172623))" + }, + { + "q": "I need the amount of substance of component for a molality of 8.24951 and a mass of component of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [8.24951, 0.634021] (the system molality (Wikidata Q172623))" + }, + { + "q": "If the molality is 8.24951 and the mass of component is 0.634021, what is the amount of substance of component?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [8.24951, 0.634021] (the system molality (Wikidata Q172623))" + }, + { + "q": "Could you work out the amount of substance of component given molality = 0.318734, mass of component = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [0.318734, 9.566071] (the system molality (Wikidata Q172623))" + }, + { + "q": "I need the amount of substance of component for a molality of 0.318734 and a mass of component of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [0.318734, 9.566071] (the system molality (Wikidata Q172623))" + }, + { + "q": "If the molality is 0.671697 and the mass of component is 14.2496, what is the amount of substance of component?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [0.671697, 14.249569] (the system molality (Wikidata Q172623))" + }, + { + "q": "Could you work out the amount of substance of component given molality = 0.671697, mass of component = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [0.671697, 14.249569] (the system molality (Wikidata Q172623))" + }, + { + "q": "I need the amount of substance of component for a molality of 21.7931 and a mass of component of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [21.793126, 29.510609] (the system molality (Wikidata Q172623))" + }, + { + "q": "If the molality is 21.7931 and the mass of component is 29.5106, what is the amount of substance of component?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [21.793126, 29.510609] (the system molality (Wikidata Q172623))" + }, + { + "q": "Could you work out the amount of substance of component given molality = 153.15, mass of component = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [153.149965, 18.595315] (the system molality (Wikidata Q172623))" + }, + { + "q": "I need the amount of substance of component for a molality of 153.15 and a mass of component of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "mol", + "why": "amount of substance of component for [153.149965, 18.595315] (the system molality (Wikidata Q172623))" + }, + { + "q": "If the molality is 8.50728, what is the amount of substance of component?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass of component is not given" + }, + { + "q": "Could you work out the amount of substance of component given molality = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass of component is not given" + }, + { + "q": "Do you know the amount of substance of component 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 amount of substance of component 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 amount of substance of component given molality = -0.318734, mass of component = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative molality: no example had one" + }, + { + "q": "What is a typical amount of substance of component?", + "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-amount-of-substance-of-compon/program.py b/root/organs10/physics/specialists/solved-amount-of-substance-of-compon/program.py new file mode 100644 index 0000000000000000000000000000000000000000..2fefdb689b17a224348d7d06d8d5e41f518c86db --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-of-compon/program.py @@ -0,0 +1,7 @@ +"""amount of substance of component from molality, mass of component (molality (Wikidata Q172623)) +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(molality, mass_of_component): + return 1.0*mass_of_component*molality diff --git a/root/organs10/physics/specialists/solved-amount-of-substance-of-compon/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-amount-of-substance-of-compon/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amount-of-substance-of-compon/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-amp-re-number/organ_formula_v2.py b/root/organs10/physics/specialists/solved-amp-re-number/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amp-re-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-amp-re-number/panel.json b/root/organs10/physics/specialists/solved-amp-re-number/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..5c5e034a456540604f9af502884f429b9c8ff0a0 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amp-re-number/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the Ampère number for electric surface current 107.961, characteristic length 59.4105 and magnetic field strength 5.77717?", + "a": "0.31454848897160476", + "valid": true, + "kind": "compute", + "expect": 0.31454848897160476, + "dtype": "quantity", + "unit": "1", + "why": "Ampère number for [107.960666, 59.4105, 5.777165] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Tell me the Ampère number with electric surface current of 107.961, characteristic length of 59.4105 and magnetic field strength of 5.77717.", + "a": "0.31454848897160476", + "valid": true, + "kind": "compute", + "expect": 0.31454848897160476, + "dtype": "quantity", + "unit": "1", + "why": "Ampère number for [107.960666, 59.4105, 5.777165] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "What is the Ampère number for electric surface current 85.2416, characteristic length 160.878 and magnetic field strength 2.69428?", + "a": "0.19665778745152376", + "valid": true, + "kind": "compute", + "expect": 0.19665778745152376, + "dtype": "quantity", + "unit": "1", + "why": "Ampère number for [85.241623, 160.878442, 2.69428] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Tell me the Ampère number with electric surface current of 85.2416, characteristic length of 160.878 and magnetic field strength of 2.69428.", + "a": "0.19665778745152376", + "valid": true, + "kind": "compute", + "expect": 0.19665778745152376, + "dtype": "quantity", + "unit": "1", + "why": "Ampère number for [85.241623, 160.878442, 2.69428] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "What is the Ampère number for electric surface current 45.6157, characteristic length 4.97131 and magnetic field strength 94.305?", + "a": "0.09729902784967777", + "valid": true, + "kind": "compute", + "expect": 0.09729902784967777, + "dtype": "quantity", + "unit": "1", + "why": "Ampère number for [45.61567, 4.971307, 94.305046] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Tell me the Ampère number with electric surface current of 45.6157, characteristic length of 4.97131 and magnetic field strength of 94.305.", + "a": "0.09729902784967777", + "valid": true, + "kind": "compute", + "expect": 0.09729902784967777, + "dtype": "quantity", + "unit": "1", + "why": "Ampère number for [45.61567, 4.971307, 94.305046] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "What is the Ampère number for electric surface current 10.5638, characteristic length 198.839 and magnetic field strength 0.671697?", + "a": "0.07909461512056756", + "valid": true, + "kind": "compute", + "expect": 0.07909461512056756, + "dtype": "quantity", + "unit": "1", + "why": "Ampère number for [10.563843, 198.838955, 0.671697] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Tell me the Ampère number with electric surface current of 10.5638, characteristic length of 198.839 and magnetic field strength of 0.671697.", + "a": "0.07909461512056756", + "valid": true, + "kind": "compute", + "expect": 0.07909461512056756, + "dtype": "quantity", + "unit": "1", + "why": "Ampère number for [10.563843, 198.838955, 0.671697] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "What is the Ampère number for electric surface current 21.7931, characteristic length 29.5106 and magnetic field strength 8.50341?", + "a": "0.08684571530973768", + "valid": true, + "kind": "compute", + "expect": 0.08684571530973768, + "dtype": "quantity", + "unit": "1", + "why": "Ampère number for [21.793126, 29.510609, 8.503407] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Tell me the Ampère number with electric surface current of 21.7931, characteristic length of 29.5106 and magnetic field strength of 8.50341.", + "a": "0.08684571530973768", + "valid": true, + "kind": "compute", + "expect": 0.08684571530973768, + "dtype": "quantity", + "unit": "1", + "why": "Ampère number for [21.793126, 29.510609, 8.503407] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "What is the Ampère number for electric surface current 223.509, characteristic length 17.3414 and magnetic field strength 7.10705?", + "a": "1.813511306470856", + "valid": true, + "kind": "compute", + "expect": 1.813511306470856, + "dtype": "quantity", + "unit": "1", + "why": "Ampère number for [223.508796, 17.341435, 7.107049] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Tell me the Ampère number with electric surface current of 223.509, characteristic length of 17.3414 and magnetic field strength of 7.10705.", + "a": "1.813511306470856", + "valid": true, + "kind": "compute", + "expect": 1.813511306470856, + "dtype": "quantity", + "unit": "1", + "why": "Ampère number for [223.508796, 17.341435, 7.107049] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "What is the Ampère number for electric surface current 107.961 and characteristic length 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic field strength is not given" + }, + { + "q": "Tell me the Ampère number with electric surface current of 85.2416 and characteristic length of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic field strength is not given" + }, + { + "q": "What is the Ampère 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 Ampère 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 Ampère number with electric surface current of -45.6157, characteristic length of 4.97131 and magnetic field strength 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 Ampère 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 surface current is 17.7888, characteristic length is 167.625 and the magnetic field strength is 10.3291, what is the Ampère number?", + "a": "0.010274173498684694", + "valid": true, + "kind": "compute", + "expect": 0.010274173498684694, + "dtype": "quantity", + "unit": "1", + "why": "Ampère number for [17.788778, 167.624948, 10.329054] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Could you work out the Ampère number given electric surface current = 17.7888, characteristic length = 167.625, magnetic field strength = 10.3291?", + "a": "0.010274173498684694", + "valid": true, + "kind": "compute", + "expect": 0.010274173498684694, + "dtype": "quantity", + "unit": "1", + "why": "Ampère number for [17.788778, 167.624948, 10.329054] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "I need the Ampère number for an electric surface current of 21.511, a characteristic length of 173.394 and a magnetic field strength of 251.084, please.", + "a": "0.000494092950151496", + "valid": true, + "kind": "compute", + "expect": 0.000494092950151496, + "dtype": "quantity", + "unit": "1", + "why": "Ampère number for [21.511016, 173.393638, 251.084037] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "If the electric surface current is 21.511, characteristic length is 173.394 and the magnetic field strength is 251.084, what is the Ampère number?", + "a": "0.000494092950151496", + "valid": true, + "kind": "compute", + "expect": 0.000494092950151496, + "dtype": "quantity", + "unit": "1", + "why": "Ampère number for [21.511016, 173.393638, 251.084037] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Could you work out the Ampère number given electric surface current = 129.245, characteristic length = 1.18359, magnetic field strength = 15.9416?", + "a": "6.849836414751194", + "valid": true, + "kind": "compute", + "expect": 6.849836414751194, + "dtype": "quantity", + "unit": "1", + "why": "Ampère number for [129.244984, 1.18359, 15.941612] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "I need the Ampère number for an electric surface current of 129.245, a characteristic length of 1.18359 and a magnetic field strength of 15.9416, please.", + "a": "6.849836414751194", + "valid": true, + "kind": "compute", + "expect": 6.849836414751194, + "dtype": "quantity", + "unit": "1", + "why": "Ampère number for [129.244984, 1.18359, 15.941612] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "If the electric surface current is 20.3966, characteristic length is 18.3169 and the magnetic field strength is 6.8386, what is the Ampère number?", + "a": "0.16283157083473473", + "valid": true, + "kind": "compute", + "expect": 0.16283157083473473, + "dtype": "quantity", + "unit": "1", + "why": "Ampère number for [20.396558, 18.316867, 6.838598] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Could you work out the Ampère number given electric surface current = 20.3966, characteristic length = 18.3169, magnetic field strength = 6.8386?", + "a": "0.16283157083473473", + "valid": true, + "kind": "compute", + "expect": 0.16283157083473473, + "dtype": "quantity", + "unit": "1", + "why": "Ampère number for [20.396558, 18.316867, 6.838598] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "I need the Ampère number for an electric surface current of 185.886, a characteristic length of 13.2373 and a magnetic field strength of 4.71941, please.", + "a": "2.975496158535072", + "valid": true, + "kind": "compute", + "expect": 2.975496158535072, + "dtype": "quantity", + "unit": "1", + "why": "Ampère number for [185.886273, 13.237311, 4.719415] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "If the electric surface current is 185.886, characteristic length is 13.2373 and the magnetic field strength is 4.71941, what is the Ampère number?", + "a": "2.975496158535072", + "valid": true, + "kind": "compute", + "expect": 2.975496158535072, + "dtype": "quantity", + "unit": "1", + "why": "Ampère number for [185.886273, 13.237311, 4.719415] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Could you work out the Ampère number given electric surface current = 9.1061, characteristic length = 24.5721, magnetic field strength = 108.451?", + "a": "0.003417101331511669", + "valid": true, + "kind": "compute", + "expect": 0.003417101331511669, + "dtype": "quantity", + "unit": "1", + "why": "Ampère number for [9.106104, 24.572086, 108.450792] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "I need the Ampère number for an electric surface current of 9.1061, a characteristic length of 24.5721 and a magnetic field strength of 108.451, please.", + "a": "0.003417101331511669", + "valid": true, + "kind": "compute", + "expect": 0.003417101331511669, + "dtype": "quantity", + "unit": "1", + "why": "Ampère number 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 characteristic length is 167.625, what is the Ampère number?", + "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 Ampère number given electric surface current = 21.511, characteristic length = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic field strength is not given" + }, + { + "q": "Do you know the Ampère 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 Ampère 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 Ampère number given electric surface current = -129.245, characteristic length = 1.18359, magnetic field strength = 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 Ampère 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-amp-re-number/program.py b/root/organs10/physics/specialists/solved-amp-re-number/program.py new file mode 100644 index 0000000000000000000000000000000000000000..89f9e9c2096557daaf3580f6c5f1ba782942f7e0 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amp-re-number/program.py @@ -0,0 +1,7 @@ +"""Ampère number from electric surface current, characteristic length, magnetic field strength (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, characteristic_length, magnetic_field_strength): + return 1.0*electric_surface_current/(characteristic_length*magnetic_field_strength) diff --git a/root/organs10/physics/specialists/solved-amp-re-number/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-amp-re-number/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-amp-re-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-angle-of-optical-rotation-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-angle-of-optical-rotation-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-angle-of-optical-rotation-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-angle-of-optical-rotation-2/panel.json b/root/organs10/physics/specialists/solved-angle-of-optical-rotation-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..5173a03746a27d0485a16177cd18b9c1aaed6e01 --- /dev/null +++ b/root/organs10/physics/specialists/solved-angle-of-optical-rotation-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the angle of optical rotation for specific rotation 107.961, cross-sectional area 59.4105 and mass 5.77717?", + "a": "10.498254293702432", + "valid": true, + "kind": "compute", + "expect": 10.498254293702432, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [107.960666, 59.4105, 5.777165] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Tell me the angle of optical rotation with specific rotation of 107.961, cross-sectional area of 59.4105 and mass of 5.77717.", + "a": "10.498254293702432", + "valid": true, + "kind": "compute", + "expect": 10.498254293702432, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [107.960666, 59.4105, 5.777165] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "What is the angle of optical rotation for specific rotation 85.2416, cross-sectional area 160.878 and mass 2.69428?", + "a": "1.4275672119863798", + "valid": true, + "kind": "compute", + "expect": 1.4275672119863798, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [85.241623, 160.878442, 2.69428] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Tell me the angle of optical rotation with specific rotation of 85.2416, cross-sectional area of 160.878 and mass of 2.69428.", + "a": "1.4275672119863798", + "valid": true, + "kind": "compute", + "expect": 1.4275672119863798, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [85.241623, 160.878442, 2.69428] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "What is the angle of optical rotation for specific rotation 45.6157, cross-sectional area 4.97131 and mass 94.305?", + "a": "865.3232325447225", + "valid": true, + "kind": "compute", + "expect": 865.3232325447225, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [45.61567, 4.971307, 94.305046] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Tell me the angle of optical rotation with specific rotation of 45.6157, cross-sectional area of 4.97131 and mass of 94.305.", + "a": "865.3232325447225", + "valid": true, + "kind": "compute", + "expect": 865.3232325447225, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [45.61567, 4.971307, 94.305046] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "What is the angle of optical rotation for specific rotation 10.5638, cross-sectional area 198.839 and mass 0.671697?", + "a": "0.03568568130708054", + "valid": true, + "kind": "compute", + "expect": 0.03568568130708054, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [10.563843, 198.838955, 0.671697] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Tell me the angle of optical rotation with specific rotation of 10.5638, cross-sectional area of 198.839 and mass of 0.671697.", + "a": "0.03568568130708054", + "valid": true, + "kind": "compute", + "expect": 0.03568568130708054, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [10.563843, 198.838955, 0.671697] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "What is the angle of optical rotation for specific rotation 21.7931, cross-sectional area 29.5106 and mass 8.50341?", + "a": "6.279634237980215", + "valid": true, + "kind": "compute", + "expect": 6.279634237980215, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [21.793126, 29.510609, 8.503407] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Tell me the angle of optical rotation with specific rotation of 21.7931, cross-sectional area of 29.5106 and mass of 8.50341.", + "a": "6.279634237980215", + "valid": true, + "kind": "compute", + "expect": 6.279634237980215, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [21.793126, 29.510609, 8.503407] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "What is the angle of optical rotation for specific rotation 223.509, cross-sectional area 17.3414 and mass 7.10705?", + "a": "91.60072422473009", + "valid": true, + "kind": "compute", + "expect": 91.60072422473009, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [223.508796, 17.341435, 7.107049] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Tell me the angle of optical rotation with specific rotation of 223.509, cross-sectional area of 17.3414 and mass of 7.10705.", + "a": "91.60072422473009", + "valid": true, + "kind": "compute", + "expect": 91.60072422473009, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [223.508796, 17.341435, 7.107049] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "What is the angle of optical rotation for specific rotation 107.961 and cross-sectional area 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Tell me the angle of optical rotation with specific rotation of 85.2416 and cross-sectional area of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "What is the angle of optical rotation 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 angle of optical rotation 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 angle of optical rotation with specific rotation of -45.6157, cross-sectional area of 4.97131 and mass of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative specific rotation: no example had one" + }, + { + "q": "Which angle of optical rotation is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the specific rotation is 17.7888, cross-sectional area is 167.625 and the mass is 10.3291, what is the angle of optical rotation?", + "a": "1.0961450581531387", + "valid": true, + "kind": "compute", + "expect": 1.0961450581531387, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [17.788778, 167.624948, 10.329054] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Could you work out the angle of optical rotation given specific rotation = 17.7888, cross-sectional area = 167.625, mass = 10.3291?", + "a": "1.0961450581531387", + "valid": true, + "kind": "compute", + "expect": 1.0961450581531387, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [17.788778, 167.624948, 10.329054] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "I need the angle of optical rotation for a specific rotation of 21.511, a cross-sectional area of 173.394 and a mass of 251.084, please.", + "a": "31.149197612202414", + "valid": true, + "kind": "compute", + "expect": 31.149197612202414, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [21.511016, 173.393638, 251.084037] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "If the specific rotation is 21.511, cross-sectional area is 173.394 and the mass is 251.084, what is the angle of optical rotation?", + "a": "31.149197612202414", + "valid": true, + "kind": "compute", + "expect": 31.149197612202414, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [21.511016, 173.393638, 251.084037] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Could you work out the angle of optical rotation given specific rotation = 129.245, cross-sectional area = 1.18359, mass = 15.9416?", + "a": "1740.7830274187056", + "valid": true, + "kind": "compute", + "expect": 1740.7830274187056, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [129.244984, 1.18359, 15.941612] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "I need the angle of optical rotation for a specific rotation of 129.245, a cross-sectional area of 1.18359 and a mass of 15.9416, please.", + "a": "1740.7830274187056", + "valid": true, + "kind": "compute", + "expect": 1740.7830274187056, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [129.244984, 1.18359, 15.941612] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "If the specific rotation is 20.3966, cross-sectional area is 18.3169 and the mass is 6.8386, what is the angle of optical rotation?", + "a": "7.615050191987504", + "valid": true, + "kind": "compute", + "expect": 7.615050191987504, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [20.396558, 18.316867, 6.838598] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Could you work out the angle of optical rotation given specific rotation = 20.3966, cross-sectional area = 18.3169, mass = 6.8386?", + "a": "7.615050191987504", + "valid": true, + "kind": "compute", + "expect": 7.615050191987504, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [20.396558, 18.316867, 6.838598] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "I need the angle of optical rotation for a specific rotation of 185.886, a cross-sectional area of 13.2373 and a mass of 4.71941, please.", + "a": "66.27285731134774", + "valid": true, + "kind": "compute", + "expect": 66.27285731134774, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [185.886273, 13.237311, 4.719415] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "If the specific rotation is 185.886, cross-sectional area is 13.2373 and the mass is 4.71941, what is the angle of optical rotation?", + "a": "66.27285731134774", + "valid": true, + "kind": "compute", + "expect": 66.27285731134774, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [185.886273, 13.237311, 4.719415] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Could you work out the angle of optical rotation given specific rotation = 9.1061, cross-sectional area = 24.5721, mass = 108.451?", + "a": "40.19049097830896", + "valid": true, + "kind": "compute", + "expect": 40.19049097830896, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [9.106104, 24.572086, 108.450792] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "I need the angle of optical rotation for a specific rotation of 9.1061, a cross-sectional area of 24.5721 and a mass of 108.451, please.", + "a": "40.19049097830896", + "valid": true, + "kind": "compute", + "expect": 40.19049097830896, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [9.106104, 24.572086, 108.450792] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "If the specific rotation is 17.7888 and the cross-sectional area is 167.625, what is the angle of optical rotation?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Could you work out the angle of optical rotation given specific rotation = 21.511, cross-sectional area = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Do you know the angle of optical rotation 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 angle of optical rotation 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 angle of optical rotation given specific rotation = -129.245, cross-sectional area = 1.18359, mass = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative specific rotation: no example had one" + }, + { + "q": "What is a typical angle of optical rotation?", + "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-angle-of-optical-rotation-2/program.py b/root/organs10/physics/specialists/solved-angle-of-optical-rotation-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..30a5e1f255e62ca2681a592a15af5d52f700e85b --- /dev/null +++ b/root/organs10/physics/specialists/solved-angle-of-optical-rotation-2/program.py @@ -0,0 +1,7 @@ +"""angle of optical rotation from specific rotation, cross-sectional area, mass (specific rotation (Wikidata Q2191631)) +The maths organ (systems.py): solved from the system q0 = q1*((q2)/(q3)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(specific_rotation, cross_sectional_area, mass): + return 1.0*mass*specific_rotation/cross_sectional_area diff --git a/root/organs10/physics/specialists/solved-angle-of-optical-rotation-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-angle-of-optical-rotation-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-angle-of-optical-rotation-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-angle-of-optical-rotation/organ_formula_v2.py b/root/organs10/physics/specialists/solved-angle-of-optical-rotation/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-angle-of-optical-rotation/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-angle-of-optical-rotation/panel.json b/root/organs10/physics/specialists/solved-angle-of-optical-rotation/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..a56f4c5c7b411946bbc261ed31d6de7a575f5566 --- /dev/null +++ b/root/organs10/physics/specialists/solved-angle-of-optical-rotation/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the angle of optical rotation for molar optical rotatory power 107.961, cross-sectional area 59.4105 and amount of substance 5.77717?", + "a": "10.498254293702432", + "valid": true, + "kind": "compute", + "expect": 10.498254293702432, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [107.960666, 59.4105, 5.777165] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Tell me the angle of optical rotation with molar optical rotatory power of 107.961, cross-sectional area of 59.4105 and amount of substance of 5.77717.", + "a": "10.498254293702432", + "valid": true, + "kind": "compute", + "expect": 10.498254293702432, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [107.960666, 59.4105, 5.777165] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "What is the angle of optical rotation for molar optical rotatory power 85.2416, cross-sectional area 160.878 and amount of substance 2.69428?", + "a": "1.4275672119863798", + "valid": true, + "kind": "compute", + "expect": 1.4275672119863798, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [85.241623, 160.878442, 2.69428] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Tell me the angle of optical rotation with molar optical rotatory power of 85.2416, cross-sectional area of 160.878 and amount of substance of 2.69428.", + "a": "1.4275672119863798", + "valid": true, + "kind": "compute", + "expect": 1.4275672119863798, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [85.241623, 160.878442, 2.69428] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "What is the angle of optical rotation for molar optical rotatory power 45.6157, cross-sectional area 4.97131 and amount of substance 94.305?", + "a": "865.3232325447225", + "valid": true, + "kind": "compute", + "expect": 865.3232325447225, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [45.61567, 4.971307, 94.305046] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Tell me the angle of optical rotation with molar optical rotatory power of 45.6157, cross-sectional area of 4.97131 and amount of substance of 94.305.", + "a": "865.3232325447225", + "valid": true, + "kind": "compute", + "expect": 865.3232325447225, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [45.61567, 4.971307, 94.305046] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "What is the angle of optical rotation for molar optical rotatory power 10.5638, cross-sectional area 198.839 and amount of substance 0.671697?", + "a": "0.03568568130708054", + "valid": true, + "kind": "compute", + "expect": 0.03568568130708054, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [10.563843, 198.838955, 0.671697] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Tell me the angle of optical rotation with molar optical rotatory power of 10.5638, cross-sectional area of 198.839 and amount of substance of 0.671697.", + "a": "0.03568568130708054", + "valid": true, + "kind": "compute", + "expect": 0.03568568130708054, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [10.563843, 198.838955, 0.671697] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "What is the angle of optical rotation for molar optical rotatory power 21.7931, cross-sectional area 29.5106 and amount of substance 8.50341?", + "a": "6.279634237980215", + "valid": true, + "kind": "compute", + "expect": 6.279634237980215, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [21.793126, 29.510609, 8.503407] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Tell me the angle of optical rotation with molar optical rotatory power of 21.7931, cross-sectional area of 29.5106 and amount of substance of 8.50341.", + "a": "6.279634237980215", + "valid": true, + "kind": "compute", + "expect": 6.279634237980215, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [21.793126, 29.510609, 8.503407] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "What is the angle of optical rotation for molar optical rotatory power 223.509, cross-sectional area 17.3414 and amount of substance 7.10705?", + "a": "91.60072422473009", + "valid": true, + "kind": "compute", + "expect": 91.60072422473009, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [223.508796, 17.341435, 7.107049] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Tell me the angle of optical rotation with molar optical rotatory power of 223.509, cross-sectional area of 17.3414 and amount of substance of 7.10705.", + "a": "91.60072422473009", + "valid": true, + "kind": "compute", + "expect": 91.60072422473009, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [223.508796, 17.341435, 7.107049] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "What is the angle of optical rotation for molar optical rotatory power 107.961 and cross-sectional area 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the amount of substance is not given" + }, + { + "q": "Tell me the angle of optical rotation with molar optical rotatory power of 85.2416 and cross-sectional area of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the amount of substance is not given" + }, + { + "q": "What is the angle of optical rotation 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 angle of optical rotation 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 angle of optical rotation with molar optical rotatory power of -45.6157, cross-sectional area of 4.97131 and amount of substance of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative molar optical rotatory power: no example had one" + }, + { + "q": "Which angle of optical rotation is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the molar optical rotatory power is 17.7888, cross-sectional area is 167.625 and the amount of substance is 10.3291, what is the angle of optical rotation?", + "a": "1.0961450581531387", + "valid": true, + "kind": "compute", + "expect": 1.0961450581531387, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [17.788778, 167.624948, 10.329054] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Could you work out the angle of optical rotation given molar optical rotatory power = 17.7888, cross-sectional area = 167.625, amount of substance = 10.3291?", + "a": "1.0961450581531387", + "valid": true, + "kind": "compute", + "expect": 1.0961450581531387, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [17.788778, 167.624948, 10.329054] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "I need the angle of optical rotation for a molar optical rotatory power of 21.511, a cross-sectional area of 173.394 and an amount of substance of 251.084, please.", + "a": "31.149197612202414", + "valid": true, + "kind": "compute", + "expect": 31.149197612202414, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [21.511016, 173.393638, 251.084037] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "If the molar optical rotatory power is 21.511, cross-sectional area is 173.394 and the amount of substance is 251.084, what is the angle of optical rotation?", + "a": "31.149197612202414", + "valid": true, + "kind": "compute", + "expect": 31.149197612202414, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [21.511016, 173.393638, 251.084037] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Could you work out the angle of optical rotation given molar optical rotatory power = 129.245, cross-sectional area = 1.18359, amount of substance = 15.9416?", + "a": "1740.7830274187056", + "valid": true, + "kind": "compute", + "expect": 1740.7830274187056, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [129.244984, 1.18359, 15.941612] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "I need the angle of optical rotation for a molar optical rotatory power of 129.245, a cross-sectional area of 1.18359 and an amount of substance of 15.9416, please.", + "a": "1740.7830274187056", + "valid": true, + "kind": "compute", + "expect": 1740.7830274187056, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [129.244984, 1.18359, 15.941612] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "If the molar optical rotatory power is 20.3966, cross-sectional area is 18.3169 and the amount of substance is 6.8386, what is the angle of optical rotation?", + "a": "7.615050191987504", + "valid": true, + "kind": "compute", + "expect": 7.615050191987504, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [20.396558, 18.316867, 6.838598] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Could you work out the angle of optical rotation given molar optical rotatory power = 20.3966, cross-sectional area = 18.3169, amount of substance = 6.8386?", + "a": "7.615050191987504", + "valid": true, + "kind": "compute", + "expect": 7.615050191987504, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [20.396558, 18.316867, 6.838598] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "I need the angle of optical rotation for a molar optical rotatory power of 185.886, a cross-sectional area of 13.2373 and an amount of substance of 4.71941, please.", + "a": "66.27285731134774", + "valid": true, + "kind": "compute", + "expect": 66.27285731134774, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [185.886273, 13.237311, 4.719415] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "If the molar optical rotatory power is 185.886, cross-sectional area is 13.2373 and the amount of substance is 4.71941, what is the angle of optical rotation?", + "a": "66.27285731134774", + "valid": true, + "kind": "compute", + "expect": 66.27285731134774, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [185.886273, 13.237311, 4.719415] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "Could you work out the angle of optical rotation given molar optical rotatory power = 9.1061, cross-sectional area = 24.5721, amount of substance = 108.451?", + "a": "40.19049097830896", + "valid": true, + "kind": "compute", + "expect": 40.19049097830896, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [9.106104, 24.572086, 108.450792] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "I need the angle of optical rotation for a molar optical rotatory power of 9.1061, a cross-sectional area of 24.5721 and an amount of substance of 108.451, please.", + "a": "40.19049097830896", + "valid": true, + "kind": "compute", + "expect": 40.19049097830896, + "dtype": "quantity", + "unit": "1", + "why": "angle of optical rotation for [9.106104, 24.572086, 108.450792] (the system molar optical rotatory power (Wikidata Q96346994))" + }, + { + "q": "If the molar optical rotatory power is 17.7888 and the cross-sectional area is 167.625, what is the angle of optical rotation?", + "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 angle of optical rotation given molar optical rotatory power = 21.511, cross-sectional area = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the amount of substance is not given" + }, + { + "q": "Do you know the angle of optical rotation 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 angle of optical rotation 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 angle of optical rotation given molar optical rotatory power = -129.245, cross-sectional area = 1.18359, amount of substance = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative molar optical rotatory power: no example had one" + }, + { + "q": "What is a typical angle of optical rotation?", + "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-angle-of-optical-rotation/program.py b/root/organs10/physics/specialists/solved-angle-of-optical-rotation/program.py new file mode 100644 index 0000000000000000000000000000000000000000..017e6d744121ad90bd637f556c4a7f750b20cdc0 --- /dev/null +++ b/root/organs10/physics/specialists/solved-angle-of-optical-rotation/program.py @@ -0,0 +1,7 @@ +"""angle of optical rotation from molar optical rotatory power, cross-sectional area, amount of substance (molar optical rotatory power (Wikidata Q96346994)) +The maths organ (systems.py): solved from the system q0 = q1*((q2)/(q3)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(molar_optical_rotatory_power, cross_sectional_area, amount_of_substance): + return 1.0*amount_of_substance*molar_optical_rotatory_power/cross_sectional_area diff --git a/root/organs10/physics/specialists/solved-angle-of-optical-rotation/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-angle-of-optical-rotation/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-angle-of-optical-rotation/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-angular-acceleration/organ_formula_v2.py b/root/organs10/physics/specialists/solved-angular-acceleration/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-angular-acceleration/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-angular-acceleration/panel.json b/root/organs10/physics/specialists/solved-angular-acceleration/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..cda46072feff2470a12a0e301725f906c6460d02 --- /dev/null +++ b/root/organs10/physics/specialists/solved-angular-acceleration/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the angular acceleration for moment of force 107.961 and moment of inertia 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1/s^2", + "why": "angular acceleration for [107.960666, 59.4105] (the system torque from angular acceleration (Wikidata Q104223516))" + }, + { + "q": "Tell me the angular acceleration with moment of force of 107.961 and moment of inertia of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1/s^2", + "why": "angular acceleration for [107.960666, 59.4105] (the system torque from angular acceleration (Wikidata Q104223516))" + }, + { + "q": "What is the angular acceleration for moment of force 169.527 and moment of inertia 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1/s^2", + "why": "angular acceleration for [169.526958, 280.773702] (the system torque from angular acceleration (Wikidata Q104223516))" + }, + { + "q": "Tell me the angular acceleration with moment of force of 169.527 and moment of inertia of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1/s^2", + "why": "angular acceleration for [169.526958, 280.773702] (the system torque from angular acceleration (Wikidata Q104223516))" + }, + { + "q": "What is the angular acceleration for moment of force 1.91195 and moment of inertia 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1/s^2", + "why": "angular acceleration for [1.911952, 82.240072] (the system torque from angular acceleration (Wikidata Q104223516))" + }, + { + "q": "Tell me the angular acceleration with moment of force of 1.91195 and moment of inertia of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1/s^2", + "why": "angular acceleration for [1.911952, 82.240072] (the system torque from angular acceleration (Wikidata Q104223516))" + }, + { + "q": "What is the angular acceleration for moment of force 1.64379 and moment of inertia 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1/s^2", + "why": "angular acceleration for [1.643795, 252.709024] (the system torque from angular acceleration (Wikidata Q104223516))" + }, + { + "q": "Tell me the angular acceleration with moment of force of 1.64379 and moment of inertia of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1/s^2", + "why": "angular acceleration for [1.643795, 252.709024] (the system torque from angular acceleration (Wikidata Q104223516))" + }, + { + "q": "What is the angular acceleration for moment of force 18.3169 and moment of inertia 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1/s^2", + "why": "angular acceleration for [18.316867, 6.838598] (the system torque from angular acceleration (Wikidata Q104223516))" + }, + { + "q": "Tell me the angular acceleration with moment of force of 18.3169 and moment of inertia of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1/s^2", + "why": "angular acceleration for [18.316867, 6.838598] (the system torque from angular acceleration (Wikidata Q104223516))" + }, + { + "q": "What is the angular acceleration for moment of force 156.502 and moment of inertia 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1/s^2", + "why": "angular acceleration for [156.50181, 185.886273] (the system torque from angular acceleration (Wikidata Q104223516))" + }, + { + "q": "Tell me the angular acceleration with moment of force of 156.502 and moment of inertia of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1/s^2", + "why": "angular acceleration for [156.50181, 185.886273] (the system torque from angular acceleration (Wikidata Q104223516))" + }, + { + "q": "What is the angular acceleration for moment of force 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the moment of inertia is not given" + }, + { + "q": "Tell me the angular acceleration with moment of force of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the moment of inertia is not given" + }, + { + "q": "What is the angular acceleration 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 angular acceleration 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 angular acceleration with moment of force of -1.91195 and moment of inertia of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative moment of force: no example had one" + }, + { + "q": "Which angular acceleration 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 moment of force is 8.50728 and the moment of inertia is 17.7888, what is the angular acceleration?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1/s^2", + "why": "angular acceleration for [8.507283, 17.788778] (the system torque from angular acceleration (Wikidata Q104223516))" + }, + { + "q": "Could you work out the angular acceleration given moment of force = 8.50728, moment of inertia = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1/s^2", + "why": "angular acceleration for [8.507283, 17.788778] (the system torque from angular acceleration (Wikidata Q104223516))" + }, + { + "q": "I need the angular acceleration for a moment of force of 8.24951 and a moment of inertia of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1/s^2", + "why": "angular acceleration for [8.24951, 0.634021] (the system torque from angular acceleration (Wikidata Q104223516))" + }, + { + "q": "If the moment of force is 8.24951 and the moment of inertia is 0.634021, what is the angular acceleration?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1/s^2", + "why": "angular acceleration for [8.24951, 0.634021] (the system torque from angular acceleration (Wikidata Q104223516))" + }, + { + "q": "Could you work out the angular acceleration given moment of force = 0.318734, moment of inertia = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1/s^2", + "why": "angular acceleration for [0.318734, 9.566071] (the system torque from angular acceleration (Wikidata Q104223516))" + }, + { + "q": "I need the angular acceleration for a moment of force of 0.318734 and a moment of inertia of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1/s^2", + "why": "angular acceleration for [0.318734, 9.566071] (the system torque from angular acceleration (Wikidata Q104223516))" + }, + { + "q": "If the moment of force is 0.671697 and the moment of inertia is 14.2496, what is the angular acceleration?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1/s^2", + "why": "angular acceleration for [0.671697, 14.249569] (the system torque from angular acceleration (Wikidata Q104223516))" + }, + { + "q": "Could you work out the angular acceleration given moment of force = 0.671697, moment of inertia = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1/s^2", + "why": "angular acceleration for [0.671697, 14.249569] (the system torque from angular acceleration (Wikidata Q104223516))" + }, + { + "q": "I need the angular acceleration for a moment of force of 21.7931 and a moment of inertia of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1/s^2", + "why": "angular acceleration for [21.793126, 29.510609] (the system torque from angular acceleration (Wikidata Q104223516))" + }, + { + "q": "If the moment of force is 21.7931 and the moment of inertia is 29.5106, what is the angular acceleration?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1/s^2", + "why": "angular acceleration for [21.793126, 29.510609] (the system torque from angular acceleration (Wikidata Q104223516))" + }, + { + "q": "Could you work out the angular acceleration given moment of force = 153.15, moment of inertia = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1/s^2", + "why": "angular acceleration for [153.149965, 18.595315] (the system torque from angular acceleration (Wikidata Q104223516))" + }, + { + "q": "I need the angular acceleration for a moment of force of 153.15 and a moment of inertia of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1/s^2", + "why": "angular acceleration for [153.149965, 18.595315] (the system torque from angular acceleration (Wikidata Q104223516))" + }, + { + "q": "If the moment of force is 8.50728, what is the angular acceleration?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the moment of inertia is not given" + }, + { + "q": "Could you work out the angular acceleration given moment of force = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the moment of inertia is not given" + }, + { + "q": "Do you know the angular acceleration 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 angular acceleration 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 angular acceleration given moment of force = -0.318734, moment of inertia = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative moment of force: no example had one" + }, + { + "q": "What is a typical angular acceleration?", + "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-angular-acceleration/program.py b/root/organs10/physics/specialists/solved-angular-acceleration/program.py new file mode 100644 index 0000000000000000000000000000000000000000..86d72900dc0ee10821184c22600e2ba61fa0b604 --- /dev/null +++ b/root/organs10/physics/specialists/solved-angular-acceleration/program.py @@ -0,0 +1,7 @@ +"""angular acceleration from moment of force, moment of inertia (torque from angular acceleration (Wikidata Q104223516)) +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(moment_of_force, moment_of_inertia): + return 1.0*moment_of_force/moment_of_inertia diff --git a/root/organs10/physics/specialists/solved-angular-acceleration/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-angular-acceleration/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-angular-acceleration/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-angular-displacement/organ_formula_v2.py b/root/organs10/physics/specialists/solved-angular-displacement/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-angular-displacement/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-angular-displacement/panel.json b/root/organs10/physics/specialists/solved-angular-displacement/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..d7e1451d6c0c3e11abd618c20d498d0362e311ef --- /dev/null +++ b/root/organs10/physics/specialists/solved-angular-displacement/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the angular displacement for arc length 107.961 and radius 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "angular displacement for [107.960666, 59.4105] (the system angular displacement (Wikidata Q3305038))" + }, + { + "q": "Tell me the angular displacement with arc length of 107.961 and radius of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "angular displacement for [107.960666, 59.4105] (the system angular displacement (Wikidata Q3305038))" + }, + { + "q": "What is the angular displacement for arc length 169.527 and radius 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "angular displacement for [169.526958, 280.773702] (the system angular displacement (Wikidata Q3305038))" + }, + { + "q": "Tell me the angular displacement with arc length of 169.527 and radius of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "angular displacement for [169.526958, 280.773702] (the system angular displacement (Wikidata Q3305038))" + }, + { + "q": "What is the angular displacement for arc length 1.91195 and radius 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "angular displacement for [1.911952, 82.240072] (the system angular displacement (Wikidata Q3305038))" + }, + { + "q": "Tell me the angular displacement with arc length of 1.91195 and radius of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "angular displacement for [1.911952, 82.240072] (the system angular displacement (Wikidata Q3305038))" + }, + { + "q": "What is the angular displacement for arc length 1.64379 and radius 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "angular displacement for [1.643795, 252.709024] (the system angular displacement (Wikidata Q3305038))" + }, + { + "q": "Tell me the angular displacement with arc length of 1.64379 and radius of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "angular displacement for [1.643795, 252.709024] (the system angular displacement (Wikidata Q3305038))" + }, + { + "q": "What is the angular displacement for arc length 18.3169 and radius 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "angular displacement for [18.316867, 6.838598] (the system angular displacement (Wikidata Q3305038))" + }, + { + "q": "Tell me the angular displacement with arc length of 18.3169 and radius of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "angular displacement for [18.316867, 6.838598] (the system angular displacement (Wikidata Q3305038))" + }, + { + "q": "What is the angular displacement for arc length 156.502 and radius 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "angular displacement for [156.50181, 185.886273] (the system angular displacement (Wikidata Q3305038))" + }, + { + "q": "Tell me the angular displacement with arc length of 156.502 and radius of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "angular displacement for [156.50181, 185.886273] (the system angular displacement (Wikidata Q3305038))" + }, + { + "q": "What is the angular displacement for arc length 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radius is not given" + }, + { + "q": "Tell me the angular displacement with arc length of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radius is not given" + }, + { + "q": "What is the angular displacement 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 angular displacement 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 angular displacement with arc length of -1.91195 and radius of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative arc length: no example had one" + }, + { + "q": "Which angular displacement 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 arc length is 8.50728 and the radius is 17.7888, what is the angular displacement?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "angular displacement for [8.507283, 17.788778] (the system angular displacement (Wikidata Q3305038))" + }, + { + "q": "Could you work out the angular displacement given arc length = 8.50728, radius = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "angular displacement for [8.507283, 17.788778] (the system angular displacement (Wikidata Q3305038))" + }, + { + "q": "I need the angular displacement for an arc length of 8.24951 and a radius of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "angular displacement for [8.24951, 0.634021] (the system angular displacement (Wikidata Q3305038))" + }, + { + "q": "If the arc length is 8.24951 and the radius is 0.634021, what is the angular displacement?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "angular displacement for [8.24951, 0.634021] (the system angular displacement (Wikidata Q3305038))" + }, + { + "q": "Could you work out the angular displacement given arc length = 0.318734, radius = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "angular displacement for [0.318734, 9.566071] (the system angular displacement (Wikidata Q3305038))" + }, + { + "q": "I need the angular displacement for an arc length of 0.318734 and a radius of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "angular displacement for [0.318734, 9.566071] (the system angular displacement (Wikidata Q3305038))" + }, + { + "q": "If the arc length is 0.671697 and the radius is 14.2496, what is the angular displacement?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "angular displacement for [0.671697, 14.249569] (the system angular displacement (Wikidata Q3305038))" + }, + { + "q": "Could you work out the angular displacement given arc length = 0.671697, radius = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "angular displacement for [0.671697, 14.249569] (the system angular displacement (Wikidata Q3305038))" + }, + { + "q": "I need the angular displacement for an arc length of 21.7931 and a radius of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "angular displacement for [21.793126, 29.510609] (the system angular displacement (Wikidata Q3305038))" + }, + { + "q": "If the arc length is 21.7931 and the radius is 29.5106, what is the angular displacement?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "angular displacement for [21.793126, 29.510609] (the system angular displacement (Wikidata Q3305038))" + }, + { + "q": "Could you work out the angular displacement given arc length = 153.15, radius = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "angular displacement for [153.149965, 18.595315] (the system angular displacement (Wikidata Q3305038))" + }, + { + "q": "I need the angular displacement for an arc length of 153.15 and a radius of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "angular displacement for [153.149965, 18.595315] (the system angular displacement (Wikidata Q3305038))" + }, + { + "q": "If the arc length is 8.50728, what is the angular displacement?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radius is not given" + }, + { + "q": "Could you work out the angular displacement given arc length = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radius is not given" + }, + { + "q": "Do you know the angular displacement 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 angular displacement 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 angular displacement given arc length = -0.318734, radius = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative arc length: no example had one" + }, + { + "q": "What is a typical angular displacement?", + "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-angular-displacement/program.py b/root/organs10/physics/specialists/solved-angular-displacement/program.py new file mode 100644 index 0000000000000000000000000000000000000000..2d56986b414f0fe216a9853b764283fca67670aa --- /dev/null +++ b/root/organs10/physics/specialists/solved-angular-displacement/program.py @@ -0,0 +1,7 @@ +"""angular displacement from arc length, radius (angular displacement (Wikidata Q3305038)) +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(arc_length, radius): + return 1.0*arc_length/radius diff --git a/root/organs10/physics/specialists/solved-angular-displacement/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-angular-displacement/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-angular-displacement/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-angular-frequency-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-angular-frequency-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-angular-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-angular-frequency/exam_log.jsonl b/root/organs10/physics/specialists/solved-angular-frequency/exam_log.jsonl new file mode 100644 index 0000000000000000000000000000000000000000..4d1173f4dc0642112292c88612d595ecf834c3f5 --- /dev/null +++ b/root/organs10/physics/specialists/solved-angular-frequency/exam_log.jsonl @@ -0,0 +1 @@ +{"t": "2026-10-25T01:08:57Z", "heldout_sha": "17a88ee793e34ba107d3811510ebcb629fe8bab3775649276258ef9818cbb1b7", "files_sha": "1726c5643af4e74bbc1c42c65ff313bc262221948cb03be78e07224ab5b37366"} diff --git a/root/organs10/physics/specialists/solved-angular-frequency/manifest.json b/root/organs10/physics/specialists/solved-angular-frequency/manifest.json new file mode 100644 index 0000000000000000000000000000000000000000..630335dd605a3019c296d29eba6e58f914fb5193 --- /dev/null +++ b/root/organs10/physics/specialists/solved-angular-frequency/manifest.json @@ -0,0 +1,41 @@ +{ + "id": "solved-angular-frequency", + "name": "Solved: angular frequency from Womersley number, pipe radius, kinematic viscosity (Womersley number (Wikidata Q2066584))", + "sources": [ + { + "name": "the maths organ: solved from the system q0 = q1*sqrt(((q2)/(q3))); checked by substitution into every equation and by a numeric solution at 12 points", + "licence": "yours" + } + ], + "organ": { + "module": "organ_formula_v3", + "class": "FormulaOrgan", + "init": {} + }, + "accept": { + "kinds": [ + "compute" + ], + "declines_in_domain": [ + "missing_input", + "out_of_domain" + ], + "context_regex": "\\bangular\\ frequency\\b" + }, + "panel": { + "path": "panel.json", + "scorer_module": "scorer_formula_v1", + "scorer_fn": "ok" + }, + "budgets": { + "latency_p50_ms": 200 + }, + "notes": "Solved by the maths organ from the system Womersley number (Wikidata Q2066584): 6 checked points used to write the panel's dev half, 6 its held-out half.", + "built_from": { + "questions": [ + "What is the angular frequency for Womersley number and pipe radius and kinematic viscosity?" + ], + "gap": "", + "node": "hod" + } +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-angular-frequency/narrow_v1.py b/root/organs10/physics/specialists/solved-angular-frequency/narrow_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..647d4d5e961acd006c9de30f0ce8b0b8f5139cda --- /dev/null +++ b/root/organs10/physics/specialists/solved-angular-frequency/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-angular-frequency/organ_formula_v2.py b/root/organs10/physics/specialists/solved-angular-frequency/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-angular-frequency/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-angular-frequency/panel.json b/root/organs10/physics/specialists/solved-angular-frequency/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..39bfba7535500fd8dff026e187e9a0ece571d628 --- /dev/null +++ b/root/organs10/physics/specialists/solved-angular-frequency/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the angular frequency for Womersley number 107.961, pipe radius 59.4105 and kinematic viscosity 5.77717?", + "a": "19.077410905197116", + "valid": true, + "kind": "compute", + "expect": 19.077410905197116, + "dtype": "quantity", + "unit": "Hz", + "why": "angular frequency for [107.960666, 59.4105, 5.777165] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "Tell me the angular frequency with Womersley number of 107.961, pipe radius of 59.4105 and kinematic viscosity of 5.77717.", + "a": "19.077410905197116", + "valid": true, + "kind": "compute", + "expect": 19.077410905197116, + "dtype": "quantity", + "unit": "Hz", + "why": "angular frequency for [107.960666, 59.4105, 5.777165] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "What is the angular frequency for Womersley number 85.2416, pipe radius 160.878 and kinematic viscosity 2.69428?", + "a": "0.7563980861224532", + "valid": true, + "kind": "compute", + "expect": 0.7563980861224532, + "dtype": "quantity", + "unit": "Hz", + "why": "angular frequency for [85.241623, 160.878442, 2.69428] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "Tell me the angular frequency with Womersley number of 85.2416, pipe radius of 160.878 and kinematic viscosity of 2.69428.", + "a": "0.7563980861224532", + "valid": true, + "kind": "compute", + "expect": 0.7563980861224532, + "dtype": "quantity", + "unit": "Hz", + "why": "angular frequency for [85.241623, 160.878442, 2.69428] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "What is the angular frequency for Womersley number 45.6157, pipe radius 4.97131 and kinematic viscosity 94.305?", + "a": "7940.023659373331", + "valid": true, + "kind": "compute", + "expect": 7940.023659373331, + "dtype": "quantity", + "unit": "Hz", + "why": "angular frequency for [45.61567, 4.971307, 94.305046] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "Tell me the angular frequency with Womersley number of 45.6157, pipe radius of 4.97131 and kinematic viscosity of 94.305.", + "a": "7940.023659373331", + "valid": true, + "kind": "compute", + "expect": 7940.023659373331, + "dtype": "quantity", + "unit": "Hz", + "why": "angular frequency for [45.61567, 4.971307, 94.305046] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "What is the angular frequency for Womersley number 10.5638, pipe radius 198.839 and kinematic viscosity 0.671697?", + "a": "0.0018958957261407446", + "valid": true, + "kind": "compute", + "expect": 0.0018958957261407446, + "dtype": "quantity", + "unit": "Hz", + "why": "angular frequency for [10.563843, 198.838955, 0.671697] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "Tell me the angular frequency with Womersley number of 10.5638, pipe radius of 198.839 and kinematic viscosity of 0.671697.", + "a": "0.0018958957261407446", + "valid": true, + "kind": "compute", + "expect": 0.0018958957261407446, + "dtype": "quantity", + "unit": "Hz", + "why": "angular frequency for [10.563843, 198.838955, 0.671697] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "What is the angular frequency for Womersley number 21.7931, pipe radius 29.5106 and kinematic viscosity 8.50341?", + "a": "4.6374124251653255", + "valid": true, + "kind": "compute", + "expect": 4.6374124251653255, + "dtype": "quantity", + "unit": "Hz", + "why": "angular frequency for [21.793126, 29.510609, 8.503407] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "Tell me the angular frequency with Womersley number of 21.7931, pipe radius of 29.5106 and kinematic viscosity of 8.50341.", + "a": "4.6374124251653255", + "valid": true, + "kind": "compute", + "expect": 4.6374124251653255, + "dtype": "quantity", + "unit": "Hz", + "why": "angular frequency for [21.793126, 29.510609, 8.503407] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "What is the angular frequency for Womersley number 223.509, pipe radius 17.3414 and kinematic viscosity 7.10705?", + "a": "1180.6155384619217", + "valid": true, + "kind": "compute", + "expect": 1180.6155384619217, + "dtype": "quantity", + "unit": "Hz", + "why": "angular frequency for [223.508796, 17.341435, 7.107049] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "Tell me the angular frequency with Womersley number of 223.509, pipe radius of 17.3414 and kinematic viscosity of 7.10705.", + "a": "1180.6155384619217", + "valid": true, + "kind": "compute", + "expect": 1180.6155384619217, + "dtype": "quantity", + "unit": "Hz", + "why": "angular frequency for [223.508796, 17.341435, 7.107049] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "What is the angular frequency for Womersley number 107.961 and pipe radius 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the kinematic viscosity is not given" + }, + { + "q": "Tell me the angular frequency 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 kinematic viscosity is not given" + }, + { + "q": "What is the angular frequency of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the angular frequency of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the angular frequency with Womersley number of -45.6157, pipe radius of 4.97131 and kinematic viscosity 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 angular frequency is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the Womersley number is 17.7888, pipe radius is 167.625 and the kinematic viscosity is 10.3291, what is the angular frequency?", + "a": "0.1163256513564536", + "valid": true, + "kind": "compute", + "expect": 0.1163256513564536, + "dtype": "quantity", + "unit": "Hz", + "why": "angular frequency for [17.788778, 167.624948, 10.329054] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "Could you work out the angular frequency given Womersley number = 17.7888, pipe radius = 167.625, kinematic viscosity = 10.3291?", + "a": "0.1163256513564536", + "valid": true, + "kind": "compute", + "expect": 0.1163256513564536, + "dtype": "quantity", + "unit": "Hz", + "why": "angular frequency for [17.788778, 167.624948, 10.329054] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "I need the angular frequency for a Womersley number of 21.511, a pipe radius of 173.394 and a kinematic viscosity of 251.084, please.", + "a": "3.8643337195288274", + "valid": true, + "kind": "compute", + "expect": 3.8643337195288274, + "dtype": "quantity", + "unit": "Hz", + "why": "angular frequency 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 kinematic viscosity is 251.084, what is the angular frequency?", + "a": "3.8643337195288274", + "valid": true, + "kind": "compute", + "expect": 3.8643337195288274, + "dtype": "quantity", + "unit": "Hz", + "why": "angular frequency for [21.511016, 173.393638, 251.084037] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "Could you work out the angular frequency given Womersley number = 129.245, pipe radius = 1.18359, kinematic viscosity = 15.9416?", + "a": "190089.03479667084", + "valid": true, + "kind": "compute", + "expect": 190089.03479667084, + "dtype": "quantity", + "unit": "Hz", + "why": "angular frequency for [129.244984, 1.18359, 15.941612] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "I need the angular frequency for a Womersley number of 129.245, a pipe radius of 1.18359 and a kinematic viscosity of 15.9416, please.", + "a": "190089.03479667084", + "valid": true, + "kind": "compute", + "expect": 190089.03479667084, + "dtype": "quantity", + "unit": "Hz", + "why": "angular frequency 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 kinematic viscosity is 6.8386, what is the angular frequency?", + "a": "8.479660437071294", + "valid": true, + "kind": "compute", + "expect": 8.479660437071294, + "dtype": "quantity", + "unit": "Hz", + "why": "angular frequency for [20.396558, 18.316867, 6.838598] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "Could you work out the angular frequency given Womersley number = 20.3966, pipe radius = 18.3169, kinematic viscosity = 6.8386?", + "a": "8.479660437071294", + "valid": true, + "kind": "compute", + "expect": 8.479660437071294, + "dtype": "quantity", + "unit": "Hz", + "why": "angular frequency for [20.396558, 18.316867, 6.838598] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "I need the angular frequency for a Womersley number of 185.886, a pipe radius of 13.2373 and a kinematic viscosity of 4.71941, please.", + "a": "930.6432675759245", + "valid": true, + "kind": "compute", + "expect": 930.6432675759245, + "dtype": "quantity", + "unit": "Hz", + "why": "angular frequency 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 kinematic viscosity is 4.71941, what is the angular frequency?", + "a": "930.6432675759245", + "valid": true, + "kind": "compute", + "expect": 930.6432675759245, + "dtype": "quantity", + "unit": "Hz", + "why": "angular frequency for [185.886273, 13.237311, 4.719415] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "Could you work out the angular frequency given Womersley number = 9.1061, pipe radius = 24.5721, kinematic viscosity = 108.451?", + "a": "14.894087343834052", + "valid": true, + "kind": "compute", + "expect": 14.894087343834052, + "dtype": "quantity", + "unit": "Hz", + "why": "angular frequency for [9.106104, 24.572086, 108.450792] (the system Womersley number (Wikidata Q2066584))" + }, + { + "q": "I need the angular frequency for a Womersley number of 9.1061, a pipe radius of 24.5721 and a kinematic viscosity of 108.451, please.", + "a": "14.894087343834052", + "valid": true, + "kind": "compute", + "expect": 14.894087343834052, + "dtype": "quantity", + "unit": "Hz", + "why": "angular frequency 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 angular frequency?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the kinematic viscosity is not given" + }, + { + "q": "Could you work out the angular frequency given Womersley number = 21.511, pipe radius = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the kinematic viscosity is not given" + }, + { + "q": "Do you know the angular frequency of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the angular frequency of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the angular frequency given Womersley number = -129.245, pipe radius = 1.18359, kinematic viscosity = 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 angular frequency?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-angular-frequency/program.py b/root/organs10/physics/specialists/solved-angular-frequency/program.py new file mode 100644 index 0000000000000000000000000000000000000000..ba339b35a7333ec757b9cd718f6f0914cb1128b0 --- /dev/null +++ b/root/organs10/physics/specialists/solved-angular-frequency/program.py @@ -0,0 +1,7 @@ +"""angular frequency from Womersley number, pipe radius, kinematic viscosity (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, kinematic_viscosity): + return 1.0*kinematic_viscosity*womersley_number**2/pipe_radius**2 diff --git a/root/organs10/physics/specialists/solved-angular-frequency/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-angular-frequency/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-angular-frequency/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?