diff --git a/root/organs10/physics/specialists/solved-resistivity-tensor/organ_formula_v2.py b/root/organs10/physics/specialists/solved-resistivity-tensor/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-resistivity-tensor/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-resistivity-tensor/panel.json b/root/organs10/physics/specialists/solved-resistivity-tensor/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..3d479466a2c9a459fecb4371b792b5ff10fe6bf6 --- /dev/null +++ b/root/organs10/physics/specialists/solved-resistivity-tensor/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the resistivity tensor for electric current density 107.961 and electric field strength 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A^2", + "why": "resistivity tensor for [107.960666, 59.4105] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Tell me the resistivity tensor with electric current density of 107.961 and electric field strength of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A^2", + "why": "resistivity tensor for [107.960666, 59.4105] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "What is the resistivity tensor for electric current density 169.527 and electric field strength 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A^2", + "why": "resistivity tensor for [169.526958, 280.773702] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Tell me the resistivity tensor with electric current density of 169.527 and electric field strength of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A^2", + "why": "resistivity tensor for [169.526958, 280.773702] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "What is the resistivity tensor for electric current density 1.91195 and electric field strength 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A^2", + "why": "resistivity tensor for [1.911952, 82.240072] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Tell me the resistivity tensor with electric current density of 1.91195 and electric field strength of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A^2", + "why": "resistivity tensor for [1.911952, 82.240072] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "What is the resistivity tensor for electric current density 1.64379 and electric field strength 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A^2", + "why": "resistivity tensor for [1.643795, 252.709024] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Tell me the resistivity tensor with electric current density of 1.64379 and electric field strength of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A^2", + "why": "resistivity tensor for [1.643795, 252.709024] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "What is the resistivity tensor for electric current density 18.3169 and electric field strength 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A^2", + "why": "resistivity tensor for [18.316867, 6.838598] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Tell me the resistivity tensor with electric current density of 18.3169 and electric field strength of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A^2", + "why": "resistivity tensor for [18.316867, 6.838598] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "What is the resistivity tensor for electric current density 156.502 and electric field strength 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A^2", + "why": "resistivity tensor for [156.50181, 185.886273] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Tell me the resistivity tensor with electric current density of 156.502 and electric field strength of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A^2", + "why": "resistivity tensor for [156.50181, 185.886273] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "What is the resistivity tensor for electric current density 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric field strength is not given" + }, + { + "q": "Tell me the resistivity tensor with electric current density of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric field strength is not given" + }, + { + "q": "What is the resistivity tensor of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the resistivity tensor of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the resistivity tensor with electric current density of -1.91195 and electric field strength of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric current density: no example had one" + }, + { + "q": "Which resistivity tensor is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the electric current density is 8.50728 and the electric field strength is 17.7888, what is the resistivity tensor?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A^2", + "why": "resistivity tensor for [8.507283, 17.788778] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Could you work out the resistivity tensor given electric current density = 8.50728, electric field strength = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A^2", + "why": "resistivity tensor for [8.507283, 17.788778] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "I need the resistivity tensor for an electric current density of 8.24951 and an electric field strength of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A^2", + "why": "resistivity tensor for [8.24951, 0.634021] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "If the electric current density is 8.24951 and the electric field strength is 0.634021, what is the resistivity tensor?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A^2", + "why": "resistivity tensor for [8.24951, 0.634021] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Could you work out the resistivity tensor given electric current density = 0.318734, electric field strength = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A^2", + "why": "resistivity tensor for [0.318734, 9.566071] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "I need the resistivity tensor for an electric current density of 0.318734 and an electric field strength of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A^2", + "why": "resistivity tensor for [0.318734, 9.566071] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "If the electric current density is 0.671697 and the electric field strength is 14.2496, what is the resistivity tensor?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A^2", + "why": "resistivity tensor for [0.671697, 14.249569] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Could you work out the resistivity tensor given electric current density = 0.671697, electric field strength = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A^2", + "why": "resistivity tensor for [0.671697, 14.249569] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "I need the resistivity tensor for an electric current density of 21.7931 and an electric field strength of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A^2", + "why": "resistivity tensor for [21.793126, 29.510609] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "If the electric current density is 21.7931 and the electric field strength is 29.5106, what is the resistivity tensor?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A^2", + "why": "resistivity tensor for [21.793126, 29.510609] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Could you work out the resistivity tensor given electric current density = 153.15, electric field strength = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A^2", + "why": "resistivity tensor for [153.149965, 18.595315] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "I need the resistivity tensor for an electric current density of 153.15 and an electric field strength of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A^2", + "why": "resistivity tensor for [153.149965, 18.595315] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "If the electric current density is 8.50728, what is the resistivity tensor?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric field strength is not given" + }, + { + "q": "Could you work out the resistivity tensor given electric current density = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric field strength is not given" + }, + { + "q": "Do you know the resistivity tensor of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the resistivity tensor of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the resistivity tensor given electric current density = -0.318734, electric field strength = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric current density: no example had one" + }, + { + "q": "What is a typical resistivity tensor?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-resistivity-tensor/program.py b/root/organs10/physics/specialists/solved-resistivity-tensor/program.py new file mode 100644 index 0000000000000000000000000000000000000000..e1ebb8bb3cdd749d36d955e989c9c2dc69c40045 --- /dev/null +++ b/root/organs10/physics/specialists/solved-resistivity-tensor/program.py @@ -0,0 +1,7 @@ +"""resistivity tensor from electric current density, electric field strength (resistivity tensor (Wikidata Q112762959)) +The maths organ (systems.py): solved from the system q3 = q1 * q2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(electric_current_density, electric_field_strength): + return 1.0*electric_field_strength/electric_current_density diff --git a/root/organs10/physics/specialists/solved-resistivity-tensor/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-resistivity-tensor/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-resistivity-tensor/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-rest-energy/organ_formula_v2.py b/root/organs10/physics/specialists/solved-rest-energy/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-rest-energy/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-rest-energy/panel.json b/root/organs10/physics/specialists/solved-rest-energy/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..6bb4d74982460803ac5abb6afc164676fbb94e9e --- /dev/null +++ b/root/organs10/physics/specialists/solved-rest-energy/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the rest energy for rest mass 107.961?", + "a": "5.225296229074162e+18", + "valid": true, + "kind": "compute", + "expect": 5.225296229074162e+18, + "dtype": "quantity", + "unit": "J", + "why": "rest energy for [107.960666] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "Tell me the rest energy with rest mass of 107.961.", + "a": "5.225296229074162e+18", + "valid": true, + "kind": "compute", + "expect": 5.225296229074162e+18, + "dtype": "quantity", + "unit": "J", + "why": "rest energy for [107.960666] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "What is the rest energy for rest mass 22.652?", + "a": "1.0963579033678204e+18", + "valid": true, + "kind": "compute", + "expect": 1.0963579033678204e+18, + "dtype": "quantity", + "unit": "J", + "why": "rest energy for [22.652023] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "Tell me the rest energy with rest mass of 22.652.", + "a": "1.0963579033678204e+18", + "valid": true, + "kind": "compute", + "expect": 1.0963579033678204e+18, + "dtype": "quantity", + "unit": "J", + "why": "rest energy for [22.652023] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "What is the rest energy for rest mass 173.394?", + "a": "8.392252098816641e+18", + "valid": true, + "kind": "compute", + "expect": 8.392252098816641e+18, + "dtype": "quantity", + "unit": "J", + "why": "rest energy for [173.393638] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "Tell me the rest energy with rest mass of 173.394.", + "a": "8.392252098816641e+18", + "valid": true, + "kind": "compute", + "expect": 8.392252098816641e+18, + "dtype": "quantity", + "unit": "J", + "why": "rest energy for [173.393638] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "What is the rest energy for rest mass 126.709?", + "a": "6.132727787128827e+18", + "valid": true, + "kind": "compute", + "expect": 6.132727787128827e+18, + "dtype": "quantity", + "unit": "J", + "why": "rest energy for [126.709252] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "Tell me the rest energy with rest mass of 126.709.", + "a": "6.132727787128827e+18", + "valid": true, + "kind": "compute", + "expect": 6.132727787128827e+18, + "dtype": "quantity", + "unit": "J", + "why": "rest energy for [126.709252] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "What is the rest energy for rest mass 0.671697?", + "a": "3.2510144470038964e+16", + "valid": true, + "kind": "compute", + "expect": 3.2510144470038964e+16, + "dtype": "quantity", + "unit": "J", + "why": "rest energy for [0.671697] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "Tell me the rest energy with rest mass of 0.671697.", + "a": "3.2510144470038964e+16", + "valid": true, + "kind": "compute", + "expect": 3.2510144470038964e+16, + "dtype": "quantity", + "unit": "J", + "why": "rest energy for [0.671697] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "What is the rest energy for rest mass 1.16997?", + "a": "5.66264514893321e+16", + "valid": true, + "kind": "compute", + "expect": 5.66264514893321e+16, + "dtype": "quantity", + "unit": "J", + "why": "rest energy for [1.169968] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "Tell me the rest energy with rest mass of 1.16997.", + "a": "5.66264514893321e+16", + "valid": true, + "kind": "compute", + "expect": 5.66264514893321e+16, + "dtype": "quantity", + "unit": "J", + "why": "rest energy for [1.169968] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "What is the rest energy?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the rest mass is not given" + }, + { + "q": "Could you work out the rest energy, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the rest mass is not given" + }, + { + "q": "What is the rest energy of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the rest energy of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the rest energy with rest mass of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative rest mass: no example had one" + }, + { + "q": "Which rest energy is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the rest mass is 2.57003, what is the rest energy?", + "a": "1.2438944780389635e+17", + "valid": true, + "kind": "compute", + "expect": 1.2438944780389635e+17, + "dtype": "quantity", + "unit": "J", + "why": "rest energy for [2.57003] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "Could you work out the rest energy given rest mass = 2.57003?", + "a": "1.2438944780389635e+17", + "valid": true, + "kind": "compute", + "expect": 1.2438944780389635e+17, + "dtype": "quantity", + "unit": "J", + "why": "rest energy for [2.57003] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "I need the rest energy for a rest mass of 48.921, please.", + "a": "2.367775957624573e+18", + "valid": true, + "kind": "compute", + "expect": 2.367775957624573e+18, + "dtype": "quantity", + "unit": "J", + "why": "rest energy for [48.920991] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "If the rest mass is 48.921, what is the rest energy?", + "a": "2.367775957624573e+18", + "valid": true, + "kind": "compute", + "expect": 2.367775957624573e+18, + "dtype": "quantity", + "unit": "J", + "why": "rest energy for [48.920991] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "Could you work out the rest energy given rest mass = 0.348438?", + "a": "1.6864383210093476e+16", + "valid": true, + "kind": "compute", + "expect": 1.6864383210093476e+16, + "dtype": "quantity", + "unit": "J", + "why": "rest energy for [0.348438] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "I need the rest energy for a rest mass of 0.348438, please.", + "a": "1.6864383210093476e+16", + "valid": true, + "kind": "compute", + "expect": 1.6864383210093476e+16, + "dtype": "quantity", + "unit": "J", + "why": "rest energy for [0.348438] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "If the rest mass is 252.709, what is the rest energy?", + "a": "1.2231116773677132e+19", + "valid": true, + "kind": "compute", + "expect": 1.2231116773677132e+19, + "dtype": "quantity", + "unit": "J", + "why": "rest energy for [252.709024] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "Could you work out the rest energy given rest mass = 252.709?", + "a": "1.2231116773677132e+19", + "valid": true, + "kind": "compute", + "expect": 1.2231116773677132e+19, + "dtype": "quantity", + "unit": "J", + "why": "rest energy for [252.709024] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "I need the rest energy for a rest mass of 20.3966, please.", + "a": "9.871934053590304e+17", + "valid": true, + "kind": "compute", + "expect": 9.871934053590304e+17, + "dtype": "quantity", + "unit": "J", + "why": "rest energy for [20.396558] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "If the rest mass is 20.3966, what is the rest energy?", + "a": "9.871934053590304e+17", + "valid": true, + "kind": "compute", + "expect": 9.871934053590304e+17, + "dtype": "quantity", + "unit": "J", + "why": "rest energy for [20.396558] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "Could you work out the rest energy given rest mass = 134.993?", + "a": "6.533659146907719e+18", + "valid": true, + "kind": "compute", + "expect": 6.533659146907719e+18, + "dtype": "quantity", + "unit": "J", + "why": "rest energy for [134.992958] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "I need the rest energy for a rest mass of 134.993, please.", + "a": "6.533659146907719e+18", + "valid": true, + "kind": "compute", + "expect": 6.533659146907719e+18, + "dtype": "quantity", + "unit": "J", + "why": "rest energy for [134.992958] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "I need the rest energy, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the rest mass is not given" + }, + { + "q": "Can you tell me the rest energy?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the rest mass is not given" + }, + { + "q": "Do you know the rest energy of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the rest energy of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the rest energy given rest mass = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative rest mass: no example had one" + }, + { + "q": "What is a typical rest energy?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-rest-energy/program.py b/root/organs10/physics/specialists/solved-rest-energy/program.py new file mode 100644 index 0000000000000000000000000000000000000000..9c31cf0a5ddcf420fad7d09f790187c97f5500a5 --- /dev/null +++ b/root/organs10/physics/specialists/solved-rest-energy/program.py @@ -0,0 +1,7 @@ +"""rest energy from rest mass (rest energy (Wikidata Q11663629)) +The maths organ (systems.py): solved from the system q0 = q1*q2 **(2); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(rest_mass): + return 4.84e+16*rest_mass diff --git a/root/organs10/physics/specialists/solved-rest-energy/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-rest-energy/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-rest-energy/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-rest-mass-of-atom-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-rest-mass-of-atom-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-rest-mass-of-atom-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-rest-mass-of-atom-2/panel.json b/root/organs10/physics/specialists/solved-rest-mass-of-atom-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..c818109c1190bde06b2de957d324a09866479406 --- /dev/null +++ b/root/organs10/physics/specialists/solved-rest-mass-of-atom-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the rest mass of atom for hydrogen-1 mass 107.961, mass defect 59.4105, atomic number 5.77717 and neutron number 1.89126?", + "a": "564.2960385318023", + "valid": true, + "kind": "compute", + "expect": 564.2960385318023, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [107.960666, 59.4105, 5.777165, 1.891256] (the system mass defect (Wikidata Q26897126))" + }, + { + "q": "Tell me the rest mass of atom with hydrogen-1 mass of 107.961, mass defect of 59.4105, atomic number of 5.77717 and neutron number of 1.89126.", + "a": "564.2960385318023", + "valid": true, + "kind": "compute", + "expect": 564.2960385318023, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [107.960666, 59.4105, 5.777165, 1.891256] (the system mass defect (Wikidata Q26897126))" + }, + { + "q": "What is the rest mass of atom for hydrogen-1 mass 2.69428, mass defect 48.921, atomic number 157.223 and neutron number 35.6412?", + "a": "374.68058966653797", + "valid": true, + "kind": "compute", + "expect": 374.68058966653797, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [2.69428, 48.920991, 157.22256, 35.641157] (the system mass defect (Wikidata Q26897126))" + }, + { + "q": "Tell me the rest mass of atom with hydrogen-1 mass of 2.69428, mass defect of 48.921, atomic number of 157.223 and neutron number of 35.6412.", + "a": "374.68058966653797", + "valid": true, + "kind": "compute", + "expect": 374.68058966653797, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [2.69428, 48.920991, 157.22256, 35.641157] (the system mass defect (Wikidata Q26897126))" + }, + { + "q": "What is the rest mass of atom for hydrogen-1 mass 0.551236, mass defect 2.88512, atomic number 10.5638 and neutron number 198.839?", + "a": "2.9380492976499477", + "valid": true, + "kind": "compute", + "expect": 2.9380492976499477, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [0.551236, 2.88512, 10.563843, 198.838955] (the system mass defect (Wikidata Q26897126))" + }, + { + "q": "Tell me the rest mass of atom with hydrogen-1 mass of 0.551236, mass defect of 2.88512, atomic number of 10.5638 and neutron number of 198.839.", + "a": "2.9380492976499477", + "valid": true, + "kind": "compute", + "expect": 2.9380492976499477, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [0.551236, 2.88512, 10.563843, 198.838955] (the system mass defect (Wikidata Q26897126))" + }, + { + "q": "What is the rest mass of atom for hydrogen-1 mass 21.7931, mass defect 29.5106, atomic number 8.50341 and neutron number 0.58813?", + "a": "155.80521986442068", + "valid": true, + "kind": "compute", + "expect": 155.80521986442068, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [21.793126, 29.510609, 8.503407, 0.58813] (the system mass defect (Wikidata Q26897126))" + }, + { + "q": "Tell me the rest mass of atom with hydrogen-1 mass of 21.7931, mass defect of 29.5106, atomic number of 8.50341 and neutron number of 0.58813.", + "a": "155.80521986442068", + "valid": true, + "kind": "compute", + "expect": 155.80521986442068, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [21.793126, 29.510609, 8.503407, 0.58813] (the system mass defect (Wikidata Q26897126))" + }, + { + "q": "What is the rest mass of atom for hydrogen-1 mass 7.10705, mass defect 30.2507, atomic number 308.158 and neutron number 178.166?", + "a": "2159.8436670986293", + "valid": true, + "kind": "compute", + "expect": 2159.8436670986293, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [7.107049, 30.250749, 308.158051, 178.165518] (the system mass defect (Wikidata Q26897126))" + }, + { + "q": "Tell me the rest mass of atom with hydrogen-1 mass of 7.10705, mass defect of 30.2507, atomic number of 308.158 and neutron number of 178.166.", + "a": "2159.8436670986293", + "valid": true, + "kind": "compute", + "expect": 2159.8436670986293, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [7.107049, 30.250749, 308.158051, 178.165518] (the system mass defect (Wikidata Q26897126))" + }, + { + "q": "What is the rest mass of atom for hydrogen-1 mass 34.8151, mass defect 0.380269, atomic number 25.4097 and neutron number 20.8456?", + "a": "884.260861631089", + "valid": true, + "kind": "compute", + "expect": 884.260861631089, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [34.81508, 0.380269, 25.409711, 20.845641] (the system mass defect (Wikidata Q26897126))" + }, + { + "q": "Tell me the rest mass of atom with hydrogen-1 mass of 34.8151, mass defect of 0.380269, atomic number of 25.4097 and neutron number of 20.8456.", + "a": "884.260861631089", + "valid": true, + "kind": "compute", + "expect": 884.260861631089, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [34.81508, 0.380269, 25.409711, 20.845641] (the system mass defect (Wikidata Q26897126))" + }, + { + "q": "What is the rest mass of atom for hydrogen-1 mass 107.961, mass defect 59.4105 and atomic number 5.77717?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the neutron number is not given" + }, + { + "q": "Tell me the rest mass of atom with hydrogen-1 mass of 2.69428, mass defect of 48.921 and atomic number of 157.223.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the neutron number is not given" + }, + { + "q": "What is the rest mass of atom 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 rest mass of atom 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 rest mass of atom with hydrogen-1 mass of -0.551236, mass defect of 2.88512, atomic number of 10.5638 and neutron number of 198.839.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative hydrogen-1 mass: no example had one" + }, + { + "q": "Which rest mass of atom 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 hydrogen-1 mass is 167.625, mass defect is 10.3291, atomic number is 2.21571 and the neutron number is 58.5346, what is the rest mass of atom?", + "a": "361.07974491365565", + "valid": true, + "kind": "compute", + "expect": 361.07974491365565, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [167.624948, 10.329054, 2.215713, 58.534594] (the system mass defect (Wikidata Q26897126))" + }, + { + "q": "Could you work out the rest mass of atom given hydrogen-1 mass = 167.625, mass defect = 10.3291, atomic number = 2.21571, neutron number = 58.5346?", + "a": "361.07974491365565", + "valid": true, + "kind": "compute", + "expect": 361.07974491365565, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [167.624948, 10.329054, 2.215713, 58.534594] (the system mass defect (Wikidata Q26897126))" + }, + { + "q": "I need the rest mass of atom for a hydrogen-1 mass of 13.999, a mass defect of 0.348438, an atomic number of 45.6157 and a neutron number of 4.97131, please.", + "a": "638.2266485937756", + "valid": true, + "kind": "compute", + "expect": 638.2266485937756, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [13.999029, 0.348438, 45.61567, 4.971307] (the system mass defect (Wikidata Q26897126))" + }, + { + "q": "If the hydrogen-1 mass is 13.999, mass defect is 0.348438, atomic number is 45.6157 and the neutron number is 4.97131, what is the rest mass of atom?", + "a": "638.2266485937756", + "valid": true, + "kind": "compute", + "expect": 638.2266485937756, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [13.999029, 0.348438, 45.61567, 4.971307] (the system mass defect (Wikidata Q26897126))" + }, + { + "q": "Could you work out the rest mass of atom given hydrogen-1 mass = 246.329, mass defect = 20.3966, atomic number = 18.3169, neutron number = 6.8386?", + "a": "4491.579956881549", + "valid": true, + "kind": "compute", + "expect": 4491.579956881549, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [246.329053, 20.396558, 18.316867, 6.838598] (the system mass defect (Wikidata Q26897126))" + }, + { + "q": "I need the rest mass of atom for a hydrogen-1 mass of 246.329, a mass defect of 20.3966, an atomic number of 18.3169 and a neutron number of 6.8386, please.", + "a": "4491.579956881549", + "valid": true, + "kind": "compute", + "expect": 4491.579956881549, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [246.329053, 20.396558, 18.316867, 6.838598] (the system mass defect (Wikidata Q26897126))" + }, + { + "q": "If the hydrogen-1 mass is 13.2373, mass defect is 4.71941, atomic number is 41.2905 and the neutron number is 2.12277, what is the rest mass of atom?", + "a": "541.8558506889378", + "valid": true, + "kind": "compute", + "expect": 541.8558506889378, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [13.237311, 4.719415, 41.290505, 2.122767] (the system mass defect (Wikidata Q26897126))" + }, + { + "q": "Could you work out the rest mass of atom given hydrogen-1 mass = 13.2373, mass defect = 4.71941, atomic number = 41.2905, neutron number = 2.12277?", + "a": "541.8558506889378", + "valid": true, + "kind": "compute", + "expect": 541.8558506889378, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [13.237311, 4.719415, 41.290505, 2.122767] (the system mass defect (Wikidata Q26897126))" + }, + { + "q": "I need the rest mass of atom for a hydrogen-1 mass of 76.5154, a mass defect of 3.14491, an atomic number of 88.6624 and a neutron number of 0.633611, please.", + "a": "6780.892039067489", + "valid": true, + "kind": "compute", + "expect": 6780.892039067489, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [76.515411, 3.144912, 88.662361, 0.633611] (the system mass defect (Wikidata Q26897126))" + }, + { + "q": "If the hydrogen-1 mass is 76.5154, mass defect is 3.14491, atomic number is 88.6624 and the neutron number is 0.633611, what is the rest mass of atom?", + "a": "6780.892039067489", + "valid": true, + "kind": "compute", + "expect": 6780.892039067489, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [76.515411, 3.144912, 88.662361, 0.633611] (the system mass defect (Wikidata Q26897126))" + }, + { + "q": "Could you work out the rest mass of atom given hydrogen-1 mass = 241.598, mass defect = 1.13479, atomic number = 0.744193, neutron number = 1.35435?", + "a": "178.66092284472543", + "valid": true, + "kind": "compute", + "expect": 178.66092284472543, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [241.598281, 1.134791, 0.744193, 1.354346] (the system mass defect (Wikidata Q26897126))" + }, + { + "q": "I need the rest mass of atom for a hydrogen-1 mass of 241.598, a mass defect of 1.13479, an atomic number of 0.744193 and a neutron number of 1.35435, please.", + "a": "178.66092284472543", + "valid": true, + "kind": "compute", + "expect": 178.66092284472543, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [241.598281, 1.134791, 0.744193, 1.354346] (the system mass defect (Wikidata Q26897126))" + }, + { + "q": "If the hydrogen-1 mass is 167.625, mass defect is 10.3291 and the atomic number is 2.21571, what is the rest mass of atom?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the neutron number is not given" + }, + { + "q": "Could you work out the rest mass of atom given hydrogen-1 mass = 13.999, mass defect = 0.348438, atomic number = 45.6157?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the neutron number is not given" + }, + { + "q": "Do you know the rest mass of atom 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 rest mass of atom 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 rest mass of atom given hydrogen-1 mass = -246.329, mass defect = 20.3966, atomic number = 18.3169, neutron number = 6.8386?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative hydrogen-1 mass: no example had one" + }, + { + "q": "What is a typical rest mass of atom?", + "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-rest-mass-of-atom-2/program.py b/root/organs10/physics/specialists/solved-rest-mass-of-atom-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..4c23f45a323d19bd80e4bacbcb28d8d982ecbe40 --- /dev/null +++ b/root/organs10/physics/specialists/solved-rest-mass-of-atom-2/program.py @@ -0,0 +1,7 @@ +"""rest mass of atom from hydrogen-1 mass, mass defect, atomic number, neutron number (mass defect (Wikidata Q26897126)) +The maths organ (systems.py): solved from the system q3 = q4*q0 + q5*q1 - q2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(hydrogen_1_mass, mass_defect, atomic_number, neutron_number): + return 1.0*atomic_number*hydrogen_1_mass - 1.0*mass_defect + 1.67492749804e-27*neutron_number diff --git a/root/organs10/physics/specialists/solved-rest-mass-of-atom-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-rest-mass-of-atom-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-rest-mass-of-atom-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-rest-mass-of-atom/organ_formula_v2.py b/root/organs10/physics/specialists/solved-rest-mass-of-atom/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-rest-mass-of-atom/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-rest-mass-of-atom/panel.json b/root/organs10/physics/specialists/solved-rest-mass-of-atom/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..a3a359c15384817396e414cedc4a6f18a0cacf55 --- /dev/null +++ b/root/organs10/physics/specialists/solved-rest-mass-of-atom/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the rest mass of atom for mass excess 107.961 and mass number 59.4105?", + "a": "107.96066588996202", + "valid": true, + "kind": "compute", + "expect": 107.96066588996202, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [107.960666, 59.4105] (the system mass excess (Wikidata Q1571163))" + }, + { + "q": "Tell me the rest mass of atom with mass excess of 107.961 and mass number of 59.4105.", + "a": "107.96066588996202", + "valid": true, + "kind": "compute", + "expect": 107.96066588996202, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [107.960666, 59.4105] (the system mass excess (Wikidata Q1571163))" + }, + { + "q": "What is the rest mass of atom for mass excess 169.527 and mass number 280.774?", + "a": "169.5269575388401", + "valid": true, + "kind": "compute", + "expect": 169.5269575388401, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [169.526958, 280.773702] (the system mass excess (Wikidata Q1571163))" + }, + { + "q": "Tell me the rest mass of atom with mass excess of 169.527 and mass number of 280.774.", + "a": "169.5269575388401", + "valid": true, + "kind": "compute", + "expect": 169.5269575388401, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [169.526958, 280.773702] (the system mass excess (Wikidata Q1571163))" + }, + { + "q": "What is the rest mass of atom for mass excess 1.91195 and mass number 82.2401?", + "a": "1.911951762986252", + "valid": true, + "kind": "compute", + "expect": 1.911951762986252, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [1.911952, 82.240072] (the system mass excess (Wikidata Q1571163))" + }, + { + "q": "Tell me the rest mass of atom with mass excess of 1.91195 and mass number of 82.2401.", + "a": "1.911951762986252", + "valid": true, + "kind": "compute", + "expect": 1.911951762986252, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [1.911952, 82.240072] (the system mass excess (Wikidata Q1571163))" + }, + { + "q": "What is the rest mass of atom for mass excess 1.64379 and mass number 252.709?", + "a": "1.6437954948627898", + "valid": true, + "kind": "compute", + "expect": 1.6437954948627898, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [1.643795, 252.709024] (the system mass excess (Wikidata Q1571163))" + }, + { + "q": "Tell me the rest mass of atom with mass excess of 1.64379 and mass number of 252.709.", + "a": "1.6437954948627898", + "valid": true, + "kind": "compute", + "expect": 1.6437954948627898, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [1.643795, 252.709024] (the system mass excess (Wikidata Q1571163))" + }, + { + "q": "What is the rest mass of atom for mass excess 18.3169 and mass number 6.8386?", + "a": "18.31686702277652", + "valid": true, + "kind": "compute", + "expect": 18.31686702277652, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [18.316867, 6.838598] (the system mass excess (Wikidata Q1571163))" + }, + { + "q": "Tell me the rest mass of atom with mass excess of 18.3169 and mass number of 6.8386.", + "a": "18.31686702277652", + "valid": true, + "kind": "compute", + "expect": 18.31686702277652, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [18.316867, 6.838598] (the system mass excess (Wikidata Q1571163))" + }, + { + "q": "What is the rest mass of atom for mass excess 156.502 and mass number 185.886?", + "a": "156.50180977998866", + "valid": true, + "kind": "compute", + "expect": 156.50180977998866, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [156.50181, 185.886273] (the system mass excess (Wikidata Q1571163))" + }, + { + "q": "Tell me the rest mass of atom with mass excess of 156.502 and mass number of 185.886.", + "a": "156.50180977998866", + "valid": true, + "kind": "compute", + "expect": 156.50180977998866, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [156.50181, 185.886273] (the system mass excess (Wikidata Q1571163))" + }, + { + "q": "What is the rest mass of atom for mass excess 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass number is not given" + }, + { + "q": "Tell me the rest mass of atom with mass excess of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass number is not given" + }, + { + "q": "What is the rest mass of atom 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 rest mass of atom 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 rest mass of atom with mass excess of -1.91195 and mass number of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mass excess: no example had one" + }, + { + "q": "Which rest mass of atom is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the mass excess is 8.50728 and the mass number is 17.7888, what is the rest mass of atom?", + "a": "8.507282654040921", + "valid": true, + "kind": "compute", + "expect": 8.507282654040921, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [8.507283, 17.788778] (the system mass excess (Wikidata Q1571163))" + }, + { + "q": "Could you work out the rest mass of atom given mass excess = 8.50728, mass number = 17.7888?", + "a": "8.507282654040921", + "valid": true, + "kind": "compute", + "expect": 8.507282654040921, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [8.507283, 17.788778] (the system mass excess (Wikidata Q1571163))" + }, + { + "q": "I need the rest mass of atom for a mass excess of 8.24951 and a mass number of 0.634021, please.", + "a": "8.249509867687042", + "valid": true, + "kind": "compute", + "expect": 8.249509867687042, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [8.24951, 0.634021] (the system mass excess (Wikidata Q1571163))" + }, + { + "q": "If the mass excess is 8.24951 and the mass number is 0.634021, what is the rest mass of atom?", + "a": "8.249509867687042", + "valid": true, + "kind": "compute", + "expect": 8.249509867687042, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [8.24951, 0.634021] (the system mass excess (Wikidata Q1571163))" + }, + { + "q": "Could you work out the rest mass of atom given mass excess = 0.318734, mass number = 9.56607?", + "a": "0.3187340476516735", + "valid": true, + "kind": "compute", + "expect": 0.3187340476516735, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [0.318734, 9.566071] (the system mass excess (Wikidata Q1571163))" + }, + { + "q": "I need the rest mass of atom for a mass excess of 0.318734 and a mass number of 9.56607, please.", + "a": "0.3187340476516735", + "valid": true, + "kind": "compute", + "expect": 0.3187340476516735, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [0.318734, 9.566071] (the system mass excess (Wikidata Q1571163))" + }, + { + "q": "If the mass excess is 0.671697 and the mass number is 14.2496, what is the rest mass of atom?", + "a": "0.6716971997941935", + "valid": true, + "kind": "compute", + "expect": 0.6716971997941935, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [0.671697, 14.249569] (the system mass excess (Wikidata Q1571163))" + }, + { + "q": "Could you work out the rest mass of atom given mass excess = 0.671697, mass number = 14.2496?", + "a": "0.6716971997941935", + "valid": true, + "kind": "compute", + "expect": 0.6716971997941935, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [0.671697, 14.249569] (the system mass excess (Wikidata Q1571163))" + }, + { + "q": "I need the rest mass of atom for a mass excess of 21.7931 and a mass number of 29.5106, please.", + "a": "21.793126492285822", + "valid": true, + "kind": "compute", + "expect": 21.793126492285822, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [21.793126, 29.510609] (the system mass excess (Wikidata Q1571163))" + }, + { + "q": "If the mass excess is 21.7931 and the mass number is 29.5106, what is the rest mass of atom?", + "a": "21.793126492285822", + "valid": true, + "kind": "compute", + "expect": 21.793126492285822, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [21.793126, 29.510609] (the system mass excess (Wikidata Q1571163))" + }, + { + "q": "Could you work out the rest mass of atom given mass excess = 153.15, mass number = 18.5953?", + "a": "153.14996513701908", + "valid": true, + "kind": "compute", + "expect": 153.14996513701908, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [153.149965, 18.595315] (the system mass excess (Wikidata Q1571163))" + }, + { + "q": "I need the rest mass of atom for a mass excess of 153.15 and a mass number of 18.5953, please.", + "a": "153.14996513701908", + "valid": true, + "kind": "compute", + "expect": 153.14996513701908, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass of atom for [153.149965, 18.595315] (the system mass excess (Wikidata Q1571163))" + }, + { + "q": "If the mass excess is 8.50728, what is the rest mass of atom?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass number is not given" + }, + { + "q": "Could you work out the rest mass of atom given mass excess = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass number is not given" + }, + { + "q": "Do you know the rest mass of atom 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 rest mass of atom 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 rest mass of atom given mass excess = -0.318734, mass number = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mass excess: no example had one" + }, + { + "q": "What is a typical rest mass of atom?", + "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-rest-mass-of-atom/program.py b/root/organs10/physics/specialists/solved-rest-mass-of-atom/program.py new file mode 100644 index 0000000000000000000000000000000000000000..c69cad144f3f5bba6bda9b9cc0b9e0b3a0a12007 --- /dev/null +++ b/root/organs10/physics/specialists/solved-rest-mass-of-atom/program.py @@ -0,0 +1,7 @@ +"""rest mass of atom from mass excess, mass number (mass excess (Wikidata Q1571163)) +The maths organ (systems.py): solved from the system q2 = q0 - q3*q1; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(mass_excess, mass_number): + return 1.0*mass_excess + 1.6605390666e-27*mass_number diff --git a/root/organs10/physics/specialists/solved-rest-mass-of-atom/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-rest-mass-of-atom/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-rest-mass-of-atom/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-rest-mass/organ_formula_v2.py b/root/organs10/physics/specialists/solved-rest-mass/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-rest-mass/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-rest-mass/panel.json b/root/organs10/physics/specialists/solved-rest-mass/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..59e424e8be992277c5480df46712e23c80cfc7e2 --- /dev/null +++ b/root/organs10/physics/specialists/solved-rest-mass/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the rest mass for rest energy 107.961?", + "a": "2.2305922704537617e-15", + "valid": true, + "kind": "compute", + "expect": 2.2305922704537617e-15, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass for [107.960666] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "Tell me the rest mass with rest energy of 107.961.", + "a": "2.2305922704537617e-15", + "valid": true, + "kind": "compute", + "expect": 2.2305922704537617e-15, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass for [107.960666] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "What is the rest mass for rest energy 22.652?", + "a": "4.680169999350373e-16", + "valid": true, + "kind": "compute", + "expect": 4.680169999350373e-16, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass for [22.652023] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "Tell me the rest mass with rest energy of 22.652.", + "a": "4.680169999350373e-16", + "valid": true, + "kind": "compute", + "expect": 4.680169999350373e-16, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass for [22.652023] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "What is the rest mass for rest energy 173.394?", + "a": "3.582513190192202e-15", + "valid": true, + "kind": "compute", + "expect": 3.582513190192202e-15, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass for [173.393638] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "Tell me the rest mass with rest energy of 173.394.", + "a": "3.582513190192202e-15", + "valid": true, + "kind": "compute", + "expect": 3.582513190192202e-15, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass for [173.393638] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "What is the rest mass for rest energy 126.709?", + "a": "2.617959747937654e-15", + "valid": true, + "kind": "compute", + "expect": 2.617959747937654e-15, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass for [126.709252] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "Tell me the rest mass with rest energy of 126.709.", + "a": "2.617959747937654e-15", + "valid": true, + "kind": "compute", + "expect": 2.617959747937654e-15, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass for [126.709252] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "What is the rest mass for rest energy 0.671697?", + "a": "1.3878041318061857e-17", + "valid": true, + "kind": "compute", + "expect": 1.3878041318061857e-17, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass for [0.671697] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "Tell me the rest mass with rest energy of 0.671697.", + "a": "1.3878041318061857e-17", + "valid": true, + "kind": "compute", + "expect": 1.3878041318061857e-17, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass for [0.671697] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "What is the rest mass for rest energy 1.16997?", + "a": "2.4172892685494553e-17", + "valid": true, + "kind": "compute", + "expect": 2.4172892685494553e-17, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass for [1.169968] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "Tell me the rest mass with rest energy of 1.16997.", + "a": "2.4172892685494553e-17", + "valid": true, + "kind": "compute", + "expect": 2.4172892685494553e-17, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass for [1.169968] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "What is the rest mass?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the rest energy is not given" + }, + { + "q": "Could you work out the rest mass, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the rest energy is not given" + }, + { + "q": "What is the rest mass 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 rest mass 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 rest mass with rest energy of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative rest energy: no example had one" + }, + { + "q": "Which rest mass 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 rest energy is 2.57003, what is the rest mass?", + "a": "5.309979159718275e-17", + "valid": true, + "kind": "compute", + "expect": 5.309979159718275e-17, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass for [2.57003] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "Could you work out the rest mass given rest energy = 2.57003?", + "a": "5.309979159718275e-17", + "valid": true, + "kind": "compute", + "expect": 5.309979159718275e-17, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass for [2.57003] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "I need the rest mass for a rest energy of 48.921, please.", + "a": "1.0107642739671871e-15", + "valid": true, + "kind": "compute", + "expect": 1.0107642739671871e-15, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass for [48.920991] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "If the rest energy is 48.921, what is the rest mass?", + "a": "1.0107642739671871e-15", + "valid": true, + "kind": "compute", + "expect": 1.0107642739671871e-15, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass for [48.920991] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "Could you work out the rest mass given rest energy = 0.348438?", + "a": "7.199125405579145e-18", + "valid": true, + "kind": "compute", + "expect": 7.199125405579145e-18, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass for [0.348438] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "I need the rest mass for a rest energy of 0.348438, please.", + "a": "7.199125405579145e-18", + "valid": true, + "kind": "compute", + "expect": 7.199125405579145e-18, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass for [0.348438] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "If the rest energy is 252.709, what is the rest mass?", + "a": "5.221260831601811e-15", + "valid": true, + "kind": "compute", + "expect": 5.221260831601811e-15, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass for [252.709024] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "Could you work out the rest mass given rest energy = 252.709?", + "a": "5.221260831601811e-15", + "valid": true, + "kind": "compute", + "expect": 5.221260831601811e-15, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass for [252.709024] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "I need the rest mass for a rest energy of 20.3966, please.", + "a": "4.214164868174266e-16", + "valid": true, + "kind": "compute", + "expect": 4.214164868174266e-16, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass for [20.396558] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "If the rest energy is 20.3966, what is the rest mass?", + "a": "4.214164868174266e-16", + "valid": true, + "kind": "compute", + "expect": 4.214164868174266e-16, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass for [20.396558] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "Could you work out the rest mass given rest energy = 134.993?", + "a": "2.7891106938169023e-15", + "valid": true, + "kind": "compute", + "expect": 2.7891106938169023e-15, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass for [134.992958] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "I need the rest mass for a rest energy of 134.993, please.", + "a": "2.7891106938169023e-15", + "valid": true, + "kind": "compute", + "expect": 2.7891106938169023e-15, + "dtype": "quantity", + "unit": "kg", + "why": "rest mass for [134.992958] (the system rest energy (Wikidata Q11663629))" + }, + { + "q": "I need the rest mass, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the rest energy is not given" + }, + { + "q": "Can you tell me the rest mass?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the rest energy is not given" + }, + { + "q": "Do you know the rest mass 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 rest mass 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 rest mass given rest energy = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative rest energy: no example had one" + }, + { + "q": "What is a typical rest mass?", + "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-rest-mass/program.py b/root/organs10/physics/specialists/solved-rest-mass/program.py new file mode 100644 index 0000000000000000000000000000000000000000..1c945444d2ceb0bcfe39fde36a6185ebb7038128 --- /dev/null +++ b/root/organs10/physics/specialists/solved-rest-mass/program.py @@ -0,0 +1,7 @@ +"""rest mass from rest energy (rest energy (Wikidata Q11663629)) +The maths organ (systems.py): solved from the system q0 = q1*q2 **(2); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(rest_energy): + return 2.06611570247934e-17*rest_energy diff --git a/root/organs10/physics/specialists/solved-rest-mass/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-rest-mass/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-rest-mass/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-revenue/organ_formula_v2.py b/root/organs10/physics/specialists/solved-revenue/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-revenue/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-revenue/panel.json b/root/organs10/physics/specialists/solved-revenue/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..f1b277da7f669c1fd4f5a2bdd8ebccbc5e64551b --- /dev/null +++ b/root/organs10/physics/specialists/solved-revenue/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the revenue for unit price 107.961 and quantity 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "$", + "why": "revenue for [107.960666, 59.4105] (the system Revenue)" + }, + { + "q": "Tell me the revenue with unit price of 107.961 and quantity of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "$", + "why": "revenue for [107.960666, 59.4105] (the system Revenue)" + }, + { + "q": "What is the revenue for unit price 169.527 and quantity 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "$", + "why": "revenue for [169.526958, 280.773702] (the system Revenue)" + }, + { + "q": "Tell me the revenue with unit price of 169.527 and quantity of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "$", + "why": "revenue for [169.526958, 280.773702] (the system Revenue)" + }, + { + "q": "What is the revenue for unit price 1.91195 and quantity 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "$", + "why": "revenue for [1.911952, 82.240072] (the system Revenue)" + }, + { + "q": "Tell me the revenue with unit price of 1.91195 and quantity of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "$", + "why": "revenue for [1.911952, 82.240072] (the system Revenue)" + }, + { + "q": "What is the revenue for unit price 1.64379 and quantity 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "$", + "why": "revenue for [1.643795, 252.709024] (the system Revenue)" + }, + { + "q": "Tell me the revenue with unit price of 1.64379 and quantity of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "$", + "why": "revenue for [1.643795, 252.709024] (the system Revenue)" + }, + { + "q": "What is the revenue for unit price 18.3169 and quantity 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "$", + "why": "revenue for [18.316867, 6.838598] (the system Revenue)" + }, + { + "q": "Tell me the revenue with unit price of 18.3169 and quantity of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "$", + "why": "revenue for [18.316867, 6.838598] (the system Revenue)" + }, + { + "q": "What is the revenue for unit price 156.502 and quantity 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "$", + "why": "revenue for [156.50181, 185.886273] (the system Revenue)" + }, + { + "q": "Tell me the revenue with unit price of 156.502 and quantity of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "$", + "why": "revenue for [156.50181, 185.886273] (the system Revenue)" + }, + { + "q": "What is the revenue for unit price 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the quantity is not given" + }, + { + "q": "Tell me the revenue with unit price of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the quantity is not given" + }, + { + "q": "What is the revenue 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 revenue 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 revenue with unit price of -1.91195 and quantity of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative unit price: no example had one" + }, + { + "q": "Which revenue 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 unit price is 8.50728 and the quantity is 17.7888, what is the revenue?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "$", + "why": "revenue for [8.507283, 17.788778] (the system Revenue)" + }, + { + "q": "Could you work out the revenue given unit price = 8.50728, quantity = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "$", + "why": "revenue for [8.507283, 17.788778] (the system Revenue)" + }, + { + "q": "I need the revenue for an unit price of 8.24951 and a quantity of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "$", + "why": "revenue for [8.24951, 0.634021] (the system Revenue)" + }, + { + "q": "If the unit price is 8.24951 and the quantity is 0.634021, what is the revenue?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "$", + "why": "revenue for [8.24951, 0.634021] (the system Revenue)" + }, + { + "q": "Could you work out the revenue given unit price = 0.318734, quantity = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "$", + "why": "revenue for [0.318734, 9.566071] (the system Revenue)" + }, + { + "q": "I need the revenue for an unit price of 0.318734 and a quantity of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "$", + "why": "revenue for [0.318734, 9.566071] (the system Revenue)" + }, + { + "q": "If the unit price is 0.671697 and the quantity is 14.2496, what is the revenue?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "$", + "why": "revenue for [0.671697, 14.249569] (the system Revenue)" + }, + { + "q": "Could you work out the revenue given unit price = 0.671697, quantity = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "$", + "why": "revenue for [0.671697, 14.249569] (the system Revenue)" + }, + { + "q": "I need the revenue for an unit price of 21.7931 and a quantity of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "$", + "why": "revenue for [21.793126, 29.510609] (the system Revenue)" + }, + { + "q": "If the unit price is 21.7931 and the quantity is 29.5106, what is the revenue?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "$", + "why": "revenue for [21.793126, 29.510609] (the system Revenue)" + }, + { + "q": "Could you work out the revenue given unit price = 153.15, quantity = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "$", + "why": "revenue for [153.149965, 18.595315] (the system Revenue)" + }, + { + "q": "I need the revenue for an unit price of 153.15 and a quantity of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "$", + "why": "revenue for [153.149965, 18.595315] (the system Revenue)" + }, + { + "q": "If the unit price is 8.50728, what is the revenue?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the quantity is not given" + }, + { + "q": "Could you work out the revenue given unit price = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the quantity is not given" + }, + { + "q": "Do you know the revenue 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 revenue 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 revenue given unit price = -0.318734, quantity = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative unit price: no example had one" + }, + { + "q": "What is a typical revenue?", + "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-revenue/program.py b/root/organs10/physics/specialists/solved-revenue/program.py new file mode 100644 index 0000000000000000000000000000000000000000..16a71c2d2dae37f97be5a68d922179a86df58bf5 --- /dev/null +++ b/root/organs10/physics/specialists/solved-revenue/program.py @@ -0,0 +1,7 @@ +"""revenue from unit price, quantity (Revenue) +The maths organ (systems.py): solved from the system R = p*q; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(unit_price, quantity): + return 1.0*quantity*unit_price diff --git a/root/organs10/physics/specialists/solved-revenue/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-revenue/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-revenue/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-richardson-number/organ_formula_v2.py b/root/organs10/physics/specialists/solved-richardson-number/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-richardson-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-richardson-number/panel.json b/root/organs10/physics/specialists/solved-richardson-number/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..d447facf829a5ebdd8bb0ae23d900d9ccdb69c57 --- /dev/null +++ b/root/organs10/physics/specialists/solved-richardson-number/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the Richardson number for acceleration of free fall 107.961, characteristic height 59.4105 and characteristic speed 5.77717?", + "a": "192.17605698754332", + "valid": true, + "kind": "compute", + "expect": 192.17605698754332, + "dtype": "quantity", + "unit": "1", + "why": "Richardson number for [107.960666, 59.4105, 5.777165] (the system Richardson number (Wikidata Q961847))" + }, + { + "q": "Tell me the Richardson number with acceleration of free fall of 107.961, characteristic height of 59.4105 and characteristic speed of 5.77717.", + "a": "192.17605698754332", + "valid": true, + "kind": "compute", + "expect": 192.17605698754332, + "dtype": "quantity", + "unit": "1", + "why": "Richardson number for [107.960666, 59.4105, 5.777165] (the system Richardson number (Wikidata Q961847))" + }, + { + "q": "What is the Richardson number for acceleration of free fall 85.2416, characteristic height 160.878 and characteristic speed 2.69428?", + "a": "1889.139998571463", + "valid": true, + "kind": "compute", + "expect": 1889.139998571463, + "dtype": "quantity", + "unit": "1", + "why": "Richardson number for [85.241623, 160.878442, 2.69428] (the system Richardson number (Wikidata Q961847))" + }, + { + "q": "Tell me the Richardson number with acceleration of free fall of 85.2416, characteristic height of 160.878 and characteristic speed of 2.69428.", + "a": "1889.139998571463", + "valid": true, + "kind": "compute", + "expect": 1889.139998571463, + "dtype": "quantity", + "unit": "1", + "why": "Richardson number for [85.241623, 160.878442, 2.69428] (the system Richardson number (Wikidata Q961847))" + }, + { + "q": "What is the Richardson number for acceleration of free fall 45.6157, characteristic height 4.97131 and characteristic speed 94.305?", + "a": "0.02549851115812748", + "valid": true, + "kind": "compute", + "expect": 0.02549851115812748, + "dtype": "quantity", + "unit": "1", + "why": "Richardson number for [45.61567, 4.971307, 94.305046] (the system Richardson number (Wikidata Q961847))" + }, + { + "q": "Tell me the Richardson number with acceleration of free fall of 45.6157, characteristic height of 4.97131 and characteristic speed of 94.305.", + "a": "0.02549851115812748", + "valid": true, + "kind": "compute", + "expect": 0.02549851115812748, + "dtype": "quantity", + "unit": "1", + "why": "Richardson number for [45.61567, 4.971307, 94.305046] (the system Richardson number (Wikidata Q961847))" + }, + { + "q": "What is the Richardson number for acceleration of free fall 10.5638, characteristic height 198.839 and characteristic speed 0.671697?", + "a": "4655.607130241956", + "valid": true, + "kind": "compute", + "expect": 4655.607130241956, + "dtype": "quantity", + "unit": "1", + "why": "Richardson number for [10.563843, 198.838955, 0.671697] (the system Richardson number (Wikidata Q961847))" + }, + { + "q": "Tell me the Richardson number with acceleration of free fall of 10.5638, characteristic height of 198.839 and characteristic speed of 0.671697.", + "a": "4655.607130241956", + "valid": true, + "kind": "compute", + "expect": 4655.607130241956, + "dtype": "quantity", + "unit": "1", + "why": "Richardson number for [10.563843, 198.838955, 0.671697] (the system Richardson number (Wikidata Q961847))" + }, + { + "q": "What is the Richardson number for acceleration of free fall 21.7931, characteristic height 29.5106 and characteristic speed 8.50341?", + "a": "8.894299603447644", + "valid": true, + "kind": "compute", + "expect": 8.894299603447644, + "dtype": "quantity", + "unit": "1", + "why": "Richardson number for [21.793126, 29.510609, 8.503407] (the system Richardson number (Wikidata Q961847))" + }, + { + "q": "Tell me the Richardson number with acceleration of free fall of 21.7931, characteristic height of 29.5106 and characteristic speed of 8.50341.", + "a": "8.894299603447644", + "valid": true, + "kind": "compute", + "expect": 8.894299603447644, + "dtype": "quantity", + "unit": "1", + "why": "Richardson number for [21.793126, 29.510609, 8.503407] (the system Richardson number (Wikidata Q961847))" + }, + { + "q": "What is the Richardson number for acceleration of free fall 223.509, characteristic height 17.3414 and characteristic speed 7.10705?", + "a": "76.73632777404201", + "valid": true, + "kind": "compute", + "expect": 76.73632777404201, + "dtype": "quantity", + "unit": "1", + "why": "Richardson number for [223.508796, 17.341435, 7.107049] (the system Richardson number (Wikidata Q961847))" + }, + { + "q": "Tell me the Richardson number with acceleration of free fall of 223.509, characteristic height of 17.3414 and characteristic speed of 7.10705.", + "a": "76.73632777404201", + "valid": true, + "kind": "compute", + "expect": 76.73632777404201, + "dtype": "quantity", + "unit": "1", + "why": "Richardson number for [223.508796, 17.341435, 7.107049] (the system Richardson number (Wikidata Q961847))" + }, + { + "q": "What is the Richardson number for acceleration of free fall 107.961 and characteristic height 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the characteristic speed is not given" + }, + { + "q": "Tell me the Richardson number with acceleration of free fall of 85.2416 and characteristic height of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the characteristic speed is not given" + }, + { + "q": "What is the Richardson 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 Richardson 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 Richardson number with acceleration of free fall of -45.6157, characteristic height of 4.97131 and characteristic speed of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative acceleration of free fall: no example had one" + }, + { + "q": "Which Richardson 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 acceleration of free fall is 17.7888, characteristic height is 167.625 and the characteristic speed is 10.3291, what is the Richardson number?", + "a": "27.948830448798585", + "valid": true, + "kind": "compute", + "expect": 27.948830448798585, + "dtype": "quantity", + "unit": "1", + "why": "Richardson number for [17.788778, 167.624948, 10.329054] (the system Richardson number (Wikidata Q961847))" + }, + { + "q": "Could you work out the Richardson number given acceleration of free fall = 17.7888, characteristic height = 167.625, characteristic speed = 10.3291?", + "a": "27.948830448798585", + "valid": true, + "kind": "compute", + "expect": 27.948830448798585, + "dtype": "quantity", + "unit": "1", + "why": "Richardson number for [17.788778, 167.624948, 10.329054] (the system Richardson number (Wikidata Q961847))" + }, + { + "q": "I need the Richardson number for an acceleration of free fall of 21.511, a characteristic height of 173.394 and a characteristic speed of 251.084, please.", + "a": "0.059163774543084434", + "valid": true, + "kind": "compute", + "expect": 0.059163774543084434, + "dtype": "quantity", + "unit": "1", + "why": "Richardson number for [21.511016, 173.393638, 251.084037] (the system Richardson number (Wikidata Q961847))" + }, + { + "q": "If the acceleration of free fall is 21.511, characteristic height is 173.394 and the characteristic speed is 251.084, what is the Richardson number?", + "a": "0.059163774543084434", + "valid": true, + "kind": "compute", + "expect": 0.059163774543084434, + "dtype": "quantity", + "unit": "1", + "why": "Richardson number for [21.511016, 173.393638, 251.084037] (the system Richardson number (Wikidata Q961847))" + }, + { + "q": "Could you work out the Richardson number given acceleration of free fall = 129.245, characteristic height = 1.18359, characteristic speed = 15.9416?", + "a": "0.6019362898805625", + "valid": true, + "kind": "compute", + "expect": 0.6019362898805625, + "dtype": "quantity", + "unit": "1", + "why": "Richardson number for [129.244984, 1.18359, 15.941612] (the system Richardson number (Wikidata Q961847))" + }, + { + "q": "I need the Richardson number for an acceleration of free fall of 129.245, a characteristic height of 1.18359 and a characteristic speed of 15.9416, please.", + "a": "0.6019362898805625", + "valid": true, + "kind": "compute", + "expect": 0.6019362898805625, + "dtype": "quantity", + "unit": "1", + "why": "Richardson number for [129.244984, 1.18359, 15.941612] (the system Richardson number (Wikidata Q961847))" + }, + { + "q": "If the acceleration of free fall is 20.3966, characteristic height is 18.3169 and the characteristic speed is 6.8386, what is the Richardson number?", + "a": "7.988659649060692", + "valid": true, + "kind": "compute", + "expect": 7.988659649060692, + "dtype": "quantity", + "unit": "1", + "why": "Richardson number for [20.396558, 18.316867, 6.838598] (the system Richardson number (Wikidata Q961847))" + }, + { + "q": "Could you work out the Richardson number given acceleration of free fall = 20.3966, characteristic height = 18.3169, characteristic speed = 6.8386?", + "a": "7.988659649060692", + "valid": true, + "kind": "compute", + "expect": 7.988659649060692, + "dtype": "quantity", + "unit": "1", + "why": "Richardson number for [20.396558, 18.316867, 6.838598] (the system Richardson number (Wikidata Q961847))" + }, + { + "q": "I need the Richardson number for an acceleration of free fall of 185.886, a characteristic height of 13.2373 and a characteristic speed of 4.71941, please.", + "a": "110.47672537422135", + "valid": true, + "kind": "compute", + "expect": 110.47672537422135, + "dtype": "quantity", + "unit": "1", + "why": "Richardson number for [185.886273, 13.237311, 4.719415] (the system Richardson number (Wikidata Q961847))" + }, + { + "q": "If the acceleration of free fall is 185.886, characteristic height is 13.2373 and the characteristic speed is 4.71941, what is the Richardson number?", + "a": "110.47672537422135", + "valid": true, + "kind": "compute", + "expect": 110.47672537422135, + "dtype": "quantity", + "unit": "1", + "why": "Richardson number for [185.886273, 13.237311, 4.719415] (the system Richardson number (Wikidata Q961847))" + }, + { + "q": "Could you work out the Richardson number given acceleration of free fall = 9.1061, characteristic height = 24.5721, characteristic speed = 108.451?", + "a": "0.019024321344549868", + "valid": true, + "kind": "compute", + "expect": 0.019024321344549868, + "dtype": "quantity", + "unit": "1", + "why": "Richardson number for [9.106104, 24.572086, 108.450792] (the system Richardson number (Wikidata Q961847))" + }, + { + "q": "I need the Richardson number for an acceleration of free fall of 9.1061, a characteristic height of 24.5721 and a characteristic speed of 108.451, please.", + "a": "0.019024321344549868", + "valid": true, + "kind": "compute", + "expect": 0.019024321344549868, + "dtype": "quantity", + "unit": "1", + "why": "Richardson number for [9.106104, 24.572086, 108.450792] (the system Richardson number (Wikidata Q961847))" + }, + { + "q": "If the acceleration of free fall is 17.7888 and the characteristic height is 167.625, what is the Richardson number?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the characteristic speed is not given" + }, + { + "q": "Could you work out the Richardson number given acceleration of free fall = 21.511, characteristic height = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the characteristic speed is not given" + }, + { + "q": "Do you know the Richardson 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 Richardson 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 Richardson number given acceleration of free fall = -129.245, characteristic height = 1.18359, characteristic speed = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative acceleration of free fall: no example had one" + }, + { + "q": "What is a typical Richardson 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-richardson-number/program.py b/root/organs10/physics/specialists/solved-richardson-number/program.py new file mode 100644 index 0000000000000000000000000000000000000000..6373819ab8a4dfeaa6b478ae6d7f33933b00807d --- /dev/null +++ b/root/organs10/physics/specialists/solved-richardson-number/program.py @@ -0,0 +1,7 @@ +"""Richardson number from acceleration of free fall, characteristic height, characteristic speed (Richardson number (Wikidata Q961847)) +The maths organ (systems.py): solved from the system q0 = ((q1*q2)/(q3 **(2))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(acceleration_of_free_fall, characteristic_height, characteristic_speed): + return 1.0*acceleration_of_free_fall*characteristic_height/characteristic_speed**2 diff --git a/root/organs10/physics/specialists/solved-richardson-number/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-richardson-number/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-richardson-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-rms-electric-current/organ_formula_v2.py b/root/organs10/physics/specialists/solved-rms-electric-current/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-rms-electric-current/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-rms-electric-current/panel.json b/root/organs10/physics/specialists/solved-rms-electric-current/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..644049c9c065e5699c42fcfa0e4f8fbec13b4d9e --- /dev/null +++ b/root/organs10/physics/specialists/solved-rms-electric-current/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the RMS electric current for apparent power 107.961 and RMS voltage 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "A", + "why": "RMS electric current for [107.960666, 59.4105] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "Tell me the RMS electric current with apparent power of 107.961 and RMS voltage of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "A", + "why": "RMS electric current for [107.960666, 59.4105] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "What is the RMS electric current for apparent power 169.527 and RMS voltage 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "A", + "why": "RMS electric current for [169.526958, 280.773702] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "Tell me the RMS electric current with apparent power of 169.527 and RMS voltage of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "A", + "why": "RMS electric current for [169.526958, 280.773702] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "What is the RMS electric current for apparent power 1.91195 and RMS voltage 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "A", + "why": "RMS electric current for [1.911952, 82.240072] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "Tell me the RMS electric current with apparent power of 1.91195 and RMS voltage of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "A", + "why": "RMS electric current for [1.911952, 82.240072] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "What is the RMS electric current for apparent power 1.64379 and RMS voltage 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "A", + "why": "RMS electric current for [1.643795, 252.709024] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "Tell me the RMS electric current with apparent power of 1.64379 and RMS voltage of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "A", + "why": "RMS electric current for [1.643795, 252.709024] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "What is the RMS electric current for apparent power 18.3169 and RMS voltage 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "A", + "why": "RMS electric current for [18.316867, 6.838598] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "Tell me the RMS electric current with apparent power of 18.3169 and RMS voltage of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "A", + "why": "RMS electric current for [18.316867, 6.838598] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "What is the RMS electric current for apparent power 156.502 and RMS voltage 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "A", + "why": "RMS electric current for [156.50181, 185.886273] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "Tell me the RMS electric current with apparent power of 156.502 and RMS voltage of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "A", + "why": "RMS electric current for [156.50181, 185.886273] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "What is the RMS electric current for apparent power 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the RMS voltage is not given" + }, + { + "q": "Tell me the RMS electric current with apparent power of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the RMS voltage is not given" + }, + { + "q": "What is the RMS electric current of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the RMS electric current of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the RMS electric current with apparent power of -1.91195 and RMS voltage of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative apparent power: no example had one" + }, + { + "q": "Which RMS electric current is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the apparent power is 8.50728 and the RMS voltage is 17.7888, what is the RMS electric current?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "A", + "why": "RMS electric current for [8.507283, 17.788778] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "Could you work out the RMS electric current given apparent power = 8.50728, RMS voltage = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "A", + "why": "RMS electric current for [8.507283, 17.788778] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "I need the RMS electric current for an apparent power of 8.24951 and a RMS voltage of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "A", + "why": "RMS electric current for [8.24951, 0.634021] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "If the apparent power is 8.24951 and the RMS voltage is 0.634021, what is the RMS electric current?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "A", + "why": "RMS electric current for [8.24951, 0.634021] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "Could you work out the RMS electric current given apparent power = 0.318734, RMS voltage = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "A", + "why": "RMS electric current for [0.318734, 9.566071] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "I need the RMS electric current for an apparent power of 0.318734 and a RMS voltage of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "A", + "why": "RMS electric current for [0.318734, 9.566071] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "If the apparent power is 0.671697 and the RMS voltage is 14.2496, what is the RMS electric current?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "A", + "why": "RMS electric current for [0.671697, 14.249569] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "Could you work out the RMS electric current given apparent power = 0.671697, RMS voltage = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "A", + "why": "RMS electric current for [0.671697, 14.249569] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "I need the RMS electric current for an apparent power of 21.7931 and a RMS voltage of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "A", + "why": "RMS electric current for [21.793126, 29.510609] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "If the apparent power is 21.7931 and the RMS voltage is 29.5106, what is the RMS electric current?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "A", + "why": "RMS electric current for [21.793126, 29.510609] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "Could you work out the RMS electric current given apparent power = 153.15, RMS voltage = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "A", + "why": "RMS electric current for [153.149965, 18.595315] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "I need the RMS electric current for an apparent power of 153.15 and a RMS voltage of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "A", + "why": "RMS electric current for [153.149965, 18.595315] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "If the apparent power is 8.50728, what is the RMS electric current?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the RMS voltage is not given" + }, + { + "q": "Could you work out the RMS electric current given apparent power = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the RMS voltage is not given" + }, + { + "q": "Do you know the RMS electric current of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the RMS electric current of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the RMS electric current given apparent power = -0.318734, RMS voltage = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative apparent power: no example had one" + }, + { + "q": "What is a typical RMS electric current?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-rms-electric-current/program.py b/root/organs10/physics/specialists/solved-rms-electric-current/program.py new file mode 100644 index 0000000000000000000000000000000000000000..7dcc7de49b3cf26308339ae1e9d3abb65639d266 --- /dev/null +++ b/root/organs10/physics/specialists/solved-rms-electric-current/program.py @@ -0,0 +1,7 @@ +"""RMS electric current from apparent power, RMS voltage (apparent power (Wikidata Q1930258)) +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(apparent_power, rms_voltage): + return 1.0*apparent_power/rms_voltage diff --git a/root/organs10/physics/specialists/solved-rms-electric-current/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-rms-electric-current/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-rms-electric-current/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-rms-voltage/organ_formula_v2.py b/root/organs10/physics/specialists/solved-rms-voltage/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-rms-voltage/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-rms-voltage/panel.json b/root/organs10/physics/specialists/solved-rms-voltage/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..5c8621329820da3677642ff17f47d044527267ba --- /dev/null +++ b/root/organs10/physics/specialists/solved-rms-voltage/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the RMS voltage for apparent power 107.961 and RMS electric current 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "V", + "why": "RMS voltage for [107.960666, 59.4105] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "Tell me the RMS voltage with apparent power of 107.961 and RMS electric current of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "V", + "why": "RMS voltage for [107.960666, 59.4105] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "What is the RMS voltage for apparent power 169.527 and RMS electric current 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "V", + "why": "RMS voltage for [169.526958, 280.773702] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "Tell me the RMS voltage with apparent power of 169.527 and RMS electric current of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "V", + "why": "RMS voltage for [169.526958, 280.773702] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "What is the RMS voltage for apparent power 1.91195 and RMS electric current 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "V", + "why": "RMS voltage for [1.911952, 82.240072] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "Tell me the RMS voltage with apparent power of 1.91195 and RMS electric current of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "V", + "why": "RMS voltage for [1.911952, 82.240072] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "What is the RMS voltage for apparent power 1.64379 and RMS electric current 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "V", + "why": "RMS voltage for [1.643795, 252.709024] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "Tell me the RMS voltage with apparent power of 1.64379 and RMS electric current of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "V", + "why": "RMS voltage for [1.643795, 252.709024] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "What is the RMS voltage for apparent power 18.3169 and RMS electric current 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "V", + "why": "RMS voltage for [18.316867, 6.838598] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "Tell me the RMS voltage with apparent power of 18.3169 and RMS electric current of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "V", + "why": "RMS voltage for [18.316867, 6.838598] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "What is the RMS voltage for apparent power 156.502 and RMS electric current 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "V", + "why": "RMS voltage for [156.50181, 185.886273] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "Tell me the RMS voltage with apparent power of 156.502 and RMS electric current of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "V", + "why": "RMS voltage for [156.50181, 185.886273] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "What is the RMS voltage for apparent power 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the RMS electric current is not given" + }, + { + "q": "Tell me the RMS voltage with apparent power of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the RMS electric current is not given" + }, + { + "q": "What is the RMS voltage of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the RMS voltage of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the RMS voltage with apparent power of -1.91195 and RMS electric current of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative apparent power: no example had one" + }, + { + "q": "Which RMS voltage is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the apparent power is 8.50728 and the RMS electric current is 17.7888, what is the RMS voltage?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "V", + "why": "RMS voltage for [8.507283, 17.788778] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "Could you work out the RMS voltage given apparent power = 8.50728, RMS electric current = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "V", + "why": "RMS voltage for [8.507283, 17.788778] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "I need the RMS voltage for an apparent power of 8.24951 and a RMS electric current of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "V", + "why": "RMS voltage for [8.24951, 0.634021] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "If the apparent power is 8.24951 and the RMS electric current is 0.634021, what is the RMS voltage?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "V", + "why": "RMS voltage for [8.24951, 0.634021] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "Could you work out the RMS voltage given apparent power = 0.318734, RMS electric current = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "V", + "why": "RMS voltage for [0.318734, 9.566071] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "I need the RMS voltage for an apparent power of 0.318734 and a RMS electric current of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "V", + "why": "RMS voltage for [0.318734, 9.566071] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "If the apparent power is 0.671697 and the RMS electric current is 14.2496, what is the RMS voltage?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "V", + "why": "RMS voltage for [0.671697, 14.249569] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "Could you work out the RMS voltage given apparent power = 0.671697, RMS electric current = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "V", + "why": "RMS voltage for [0.671697, 14.249569] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "I need the RMS voltage for an apparent power of 21.7931 and a RMS electric current of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "V", + "why": "RMS voltage for [21.793126, 29.510609] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "If the apparent power is 21.7931 and the RMS electric current is 29.5106, what is the RMS voltage?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "V", + "why": "RMS voltage for [21.793126, 29.510609] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "Could you work out the RMS voltage given apparent power = 153.15, RMS electric current = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "V", + "why": "RMS voltage for [153.149965, 18.595315] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "I need the RMS voltage for an apparent power of 153.15 and a RMS electric current of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "V", + "why": "RMS voltage for [153.149965, 18.595315] (the system apparent power (Wikidata Q1930258))" + }, + { + "q": "If the apparent power is 8.50728, what is the RMS voltage?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the RMS electric current is not given" + }, + { + "q": "Could you work out the RMS voltage given apparent power = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the RMS electric current is not given" + }, + { + "q": "Do you know the RMS voltage of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the RMS voltage of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the RMS voltage given apparent power = -0.318734, RMS electric current = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative apparent power: no example had one" + }, + { + "q": "What is a typical RMS voltage?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-rms-voltage/program.py b/root/organs10/physics/specialists/solved-rms-voltage/program.py new file mode 100644 index 0000000000000000000000000000000000000000..9727cd45e27eb0b95fc8f673efe46ba24f320ba5 --- /dev/null +++ b/root/organs10/physics/specialists/solved-rms-voltage/program.py @@ -0,0 +1,7 @@ +"""RMS voltage from apparent power, RMS electric current (apparent power (Wikidata Q1930258)) +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(apparent_power, rms_electric_current): + return 1.0*apparent_power/rms_electric_current diff --git a/root/organs10/physics/specialists/solved-rms-voltage/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-rms-voltage/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-rms-voltage/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-roberts-number/organ_formula_v2.py b/root/organs10/physics/specialists/solved-roberts-number/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-roberts-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-roberts-number/panel.json b/root/organs10/physics/specialists/solved-roberts-number/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..a48a415a9de0ed221dac8fd3fb73bceabf300ef6 --- /dev/null +++ b/root/organs10/physics/specialists/solved-roberts-number/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the Roberts number for thermal diffusivity 107.961, electrical conductivity 59.4105 and magnetic permeability 5.77717?", + "a": "37054.71743803604", + "valid": true, + "kind": "compute", + "expect": 37054.71743803604, + "dtype": "quantity", + "unit": "1", + "why": "Roberts number for [107.960666, 59.4105, 5.777165] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Tell me the Roberts number with thermal diffusivity of 107.961, electrical conductivity of 59.4105 and magnetic permeability of 5.77717.", + "a": "37054.71743803604", + "valid": true, + "kind": "compute", + "expect": 37054.71743803604, + "dtype": "quantity", + "unit": "1", + "why": "Roberts number for [107.960666, 59.4105, 5.777165] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "What is the Roberts number for thermal diffusivity 85.2416, electrical conductivity 160.878 and magnetic permeability 2.69428?", + "a": "36948.1133481491", + "valid": true, + "kind": "compute", + "expect": 36948.1133481491, + "dtype": "quantity", + "unit": "1", + "why": "Roberts number for [85.241623, 160.878442, 2.69428] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Tell me the Roberts number with thermal diffusivity of 85.2416, electrical conductivity of 160.878 and magnetic permeability of 2.69428.", + "a": "36948.1133481491", + "valid": true, + "kind": "compute", + "expect": 36948.1133481491, + "dtype": "quantity", + "unit": "1", + "why": "Roberts number for [85.241623, 160.878442, 2.69428] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "What is the Roberts number for thermal diffusivity 45.6157, electrical conductivity 4.97131 and magnetic permeability 94.305?", + "a": "21385.51027534082", + "valid": true, + "kind": "compute", + "expect": 21385.51027534082, + "dtype": "quantity", + "unit": "1", + "why": "Roberts number for [45.61567, 4.971307, 94.305046] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Tell me the Roberts number with thermal diffusivity of 45.6157, electrical conductivity of 4.97131 and magnetic permeability of 94.305.", + "a": "21385.51027534082", + "valid": true, + "kind": "compute", + "expect": 21385.51027534082, + "dtype": "quantity", + "unit": "1", + "why": "Roberts number for [45.61567, 4.971307, 94.305046] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "What is the Roberts number for thermal diffusivity 10.5638, electrical conductivity 198.839 and magnetic permeability 0.671697?", + "a": "1410.9022889506464", + "valid": true, + "kind": "compute", + "expect": 1410.9022889506464, + "dtype": "quantity", + "unit": "1", + "why": "Roberts number for [10.563843, 198.838955, 0.671697] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Tell me the Roberts number with thermal diffusivity of 10.5638, electrical conductivity of 198.839 and magnetic permeability of 0.671697.", + "a": "1410.9022889506464", + "valid": true, + "kind": "compute", + "expect": 1410.9022889506464, + "dtype": "quantity", + "unit": "1", + "why": "Roberts number for [10.563843, 198.838955, 0.671697] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "What is the Roberts number for thermal diffusivity 21.7931, electrical conductivity 29.5106 and magnetic permeability 8.50341?", + "a": "5468.782894065435", + "valid": true, + "kind": "compute", + "expect": 5468.782894065435, + "dtype": "quantity", + "unit": "1", + "why": "Roberts number for [21.793126, 29.510609, 8.503407] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Tell me the Roberts number with thermal diffusivity of 21.7931, electrical conductivity of 29.5106 and magnetic permeability of 8.50341.", + "a": "5468.782894065435", + "valid": true, + "kind": "compute", + "expect": 5468.782894065435, + "dtype": "quantity", + "unit": "1", + "why": "Roberts number for [21.793126, 29.510609, 8.503407] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "What is the Roberts number for thermal diffusivity 223.509, electrical conductivity 17.3414 and magnetic permeability 7.10705?", + "a": "27546.66146773027", + "valid": true, + "kind": "compute", + "expect": 27546.66146773027, + "dtype": "quantity", + "unit": "1", + "why": "Roberts number for [223.508796, 17.341435, 7.107049] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Tell me the Roberts number with thermal diffusivity of 223.509, electrical conductivity of 17.3414 and magnetic permeability of 7.10705.", + "a": "27546.66146773027", + "valid": true, + "kind": "compute", + "expect": 27546.66146773027, + "dtype": "quantity", + "unit": "1", + "why": "Roberts number for [223.508796, 17.341435, 7.107049] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "What is the Roberts number for thermal diffusivity 107.961 and electrical conductivity 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "Tell me the Roberts number with thermal diffusivity of 85.2416 and electrical conductivity of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "What is the Roberts 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 Roberts 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 Roberts number with thermal diffusivity of -45.6157, electrical conductivity of 4.97131 and magnetic permeability of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative thermal diffusivity: no example had one" + }, + { + "q": "Which Roberts 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 thermal diffusivity is 17.7888, electrical conductivity is 167.625 and the magnetic permeability is 10.3291, what is the Roberts number?", + "a": "30799.618802505258", + "valid": true, + "kind": "compute", + "expect": 30799.618802505258, + "dtype": "quantity", + "unit": "1", + "why": "Roberts number for [17.788778, 167.624948, 10.329054] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Could you work out the Roberts number given thermal diffusivity = 17.7888, electrical conductivity = 167.625, magnetic permeability = 10.3291?", + "a": "30799.618802505258", + "valid": true, + "kind": "compute", + "expect": 30799.618802505258, + "dtype": "quantity", + "unit": "1", + "why": "Roberts number for [17.788778, 167.624948, 10.329054] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "I need the Roberts number for a thermal diffusivity of 21.511, an electrical conductivity of 173.394 and a magnetic permeability of 251.084, please.", + "a": "936511.6480251562", + "valid": true, + "kind": "compute", + "expect": 936511.6480251562, + "dtype": "quantity", + "unit": "1", + "why": "Roberts number for [21.511016, 173.393638, 251.084037] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "If the thermal diffusivity is 21.511, electrical conductivity is 173.394 and the magnetic permeability is 251.084, what is the Roberts number?", + "a": "936511.6480251562", + "valid": true, + "kind": "compute", + "expect": 936511.6480251562, + "dtype": "quantity", + "unit": "1", + "why": "Roberts number for [21.511016, 173.393638, 251.084037] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Could you work out the Roberts number given thermal diffusivity = 129.245, electrical conductivity = 1.18359, magnetic permeability = 15.9416?", + "a": "2438.6371902755973", + "valid": true, + "kind": "compute", + "expect": 2438.6371902755973, + "dtype": "quantity", + "unit": "1", + "why": "Roberts number for [129.244984, 1.18359, 15.941612] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "I need the Roberts number for a thermal diffusivity of 129.245, an electrical conductivity of 1.18359 and a magnetic permeability of 15.9416, please.", + "a": "2438.6371902755973", + "valid": true, + "kind": "compute", + "expect": 2438.6371902755973, + "dtype": "quantity", + "unit": "1", + "why": "Roberts number for [129.244984, 1.18359, 15.941612] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "If the thermal diffusivity is 20.3966, electrical conductivity is 18.3169 and the magnetic permeability is 6.8386, what is the Roberts number?", + "a": "2554.907347285691", + "valid": true, + "kind": "compute", + "expect": 2554.907347285691, + "dtype": "quantity", + "unit": "1", + "why": "Roberts number for [20.396558, 18.316867, 6.838598] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Could you work out the Roberts number given thermal diffusivity = 20.3966, electrical conductivity = 18.3169, magnetic permeability = 6.8386?", + "a": "2554.907347285691", + "valid": true, + "kind": "compute", + "expect": 2554.907347285691, + "dtype": "quantity", + "unit": "1", + "why": "Roberts number for [20.396558, 18.316867, 6.838598] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "I need the Roberts number for a thermal diffusivity of 185.886, an electrical conductivity of 13.2373 and a magnetic permeability of 4.71941, please.", + "a": "11612.754502579606", + "valid": true, + "kind": "compute", + "expect": 11612.754502579606, + "dtype": "quantity", + "unit": "1", + "why": "Roberts number for [185.886273, 13.237311, 4.719415] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "If the thermal diffusivity is 185.886, electrical conductivity is 13.2373 and the magnetic permeability is 4.71941, what is the Roberts number?", + "a": "11612.754502579606", + "valid": true, + "kind": "compute", + "expect": 11612.754502579606, + "dtype": "quantity", + "unit": "1", + "why": "Roberts number for [185.886273, 13.237311, 4.719415] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Could you work out the Roberts number given thermal diffusivity = 9.1061, electrical conductivity = 24.5721, magnetic permeability = 108.451?", + "a": "24266.511866047756", + "valid": true, + "kind": "compute", + "expect": 24266.511866047756, + "dtype": "quantity", + "unit": "1", + "why": "Roberts number for [9.106104, 24.572086, 108.450792] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "I need the Roberts number for a thermal diffusivity of 9.1061, an electrical conductivity of 24.5721 and a magnetic permeability of 108.451, please.", + "a": "24266.511866047756", + "valid": true, + "kind": "compute", + "expect": 24266.511866047756, + "dtype": "quantity", + "unit": "1", + "why": "Roberts number for [9.106104, 24.572086, 108.450792] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "If the thermal diffusivity is 17.7888 and the electrical conductivity is 167.625, what is the Roberts number?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "Could you work out the Roberts number given thermal diffusivity = 21.511, electrical conductivity = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "Do you know the Roberts 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 Roberts 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 Roberts number given thermal diffusivity = -129.245, electrical conductivity = 1.18359, magnetic permeability = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative thermal diffusivity: no example had one" + }, + { + "q": "What is a typical Roberts 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-roberts-number/program.py b/root/organs10/physics/specialists/solved-roberts-number/program.py new file mode 100644 index 0000000000000000000000000000000000000000..efea1dcbe18fc5a26ff9b71749b17d85ee262656 --- /dev/null +++ b/root/organs10/physics/specialists/solved-roberts-number/program.py @@ -0,0 +1,7 @@ +"""Roberts number from thermal diffusivity, electrical conductivity, magnetic permeability (Roberts number (Wikidata Q105190387)) +The maths organ (systems.py): solved from the system q0 = q1*q2*q3; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(thermal_diffusivity, electrical_conductivity, magnetic_permeability): + return 1.0*electrical_conductivity*magnetic_permeability*thermal_diffusivity diff --git a/root/organs10/physics/specialists/solved-roberts-number/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-roberts-number/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-roberts-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-rolling-resistance-factor/organ_formula_v2.py b/root/organs10/physics/specialists/solved-rolling-resistance-factor/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-rolling-resistance-factor/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-rolling-resistance-factor/panel.json b/root/organs10/physics/specialists/solved-rolling-resistance-factor/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..eed191e94f63fe5a0c0a3c83b8aa6b70db196e03 --- /dev/null +++ b/root/organs10/physics/specialists/solved-rolling-resistance-factor/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the rolling resistance factor for tangential force 107.961 and normal force 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "rolling resistance factor for [107.960666, 59.4105] (the system rolling resistance factor (Wikidata Q91738044))" + }, + { + "q": "Tell me the rolling resistance factor with tangential force of 107.961 and normal force of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "rolling resistance factor for [107.960666, 59.4105] (the system rolling resistance factor (Wikidata Q91738044))" + }, + { + "q": "What is the rolling resistance factor for tangential force 169.527 and normal force 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "rolling resistance factor for [169.526958, 280.773702] (the system rolling resistance factor (Wikidata Q91738044))" + }, + { + "q": "Tell me the rolling resistance factor with tangential force of 169.527 and normal force of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "rolling resistance factor for [169.526958, 280.773702] (the system rolling resistance factor (Wikidata Q91738044))" + }, + { + "q": "What is the rolling resistance factor for tangential force 1.91195 and normal force 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "rolling resistance factor for [1.911952, 82.240072] (the system rolling resistance factor (Wikidata Q91738044))" + }, + { + "q": "Tell me the rolling resistance factor with tangential force of 1.91195 and normal force of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "rolling resistance factor for [1.911952, 82.240072] (the system rolling resistance factor (Wikidata Q91738044))" + }, + { + "q": "What is the rolling resistance factor for tangential force 1.64379 and normal force 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "rolling resistance factor for [1.643795, 252.709024] (the system rolling resistance factor (Wikidata Q91738044))" + }, + { + "q": "Tell me the rolling resistance factor with tangential force of 1.64379 and normal force of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "rolling resistance factor for [1.643795, 252.709024] (the system rolling resistance factor (Wikidata Q91738044))" + }, + { + "q": "What is the rolling resistance factor for tangential force 18.3169 and normal force 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "rolling resistance factor for [18.316867, 6.838598] (the system rolling resistance factor (Wikidata Q91738044))" + }, + { + "q": "Tell me the rolling resistance factor with tangential force of 18.3169 and normal force of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "rolling resistance factor for [18.316867, 6.838598] (the system rolling resistance factor (Wikidata Q91738044))" + }, + { + "q": "What is the rolling resistance factor for tangential force 156.502 and normal force 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "rolling resistance factor for [156.50181, 185.886273] (the system rolling resistance factor (Wikidata Q91738044))" + }, + { + "q": "Tell me the rolling resistance factor with tangential force of 156.502 and normal force of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "rolling resistance factor for [156.50181, 185.886273] (the system rolling resistance factor (Wikidata Q91738044))" + }, + { + "q": "What is the rolling resistance factor for tangential force 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the normal force is not given" + }, + { + "q": "Tell me the rolling resistance factor with tangential force of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the normal force is not given" + }, + { + "q": "What is the rolling resistance factor of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the rolling resistance factor of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the rolling resistance factor with tangential force of -1.91195 and normal force of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative tangential force: no example had one" + }, + { + "q": "Which rolling resistance factor is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the tangential force is 8.50728 and the normal force is 17.7888, what is the rolling resistance factor?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "rolling resistance factor for [8.507283, 17.788778] (the system rolling resistance factor (Wikidata Q91738044))" + }, + { + "q": "Could you work out the rolling resistance factor given tangential force = 8.50728, normal force = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "rolling resistance factor for [8.507283, 17.788778] (the system rolling resistance factor (Wikidata Q91738044))" + }, + { + "q": "I need the rolling resistance factor for a tangential force of 8.24951 and a normal force of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "rolling resistance factor for [8.24951, 0.634021] (the system rolling resistance factor (Wikidata Q91738044))" + }, + { + "q": "If the tangential force is 8.24951 and the normal force is 0.634021, what is the rolling resistance factor?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "rolling resistance factor for [8.24951, 0.634021] (the system rolling resistance factor (Wikidata Q91738044))" + }, + { + "q": "Could you work out the rolling resistance factor given tangential force = 0.318734, normal force = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "rolling resistance factor for [0.318734, 9.566071] (the system rolling resistance factor (Wikidata Q91738044))" + }, + { + "q": "I need the rolling resistance factor for a tangential force of 0.318734 and a normal force of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "rolling resistance factor for [0.318734, 9.566071] (the system rolling resistance factor (Wikidata Q91738044))" + }, + { + "q": "If the tangential force is 0.671697 and the normal force is 14.2496, what is the rolling resistance factor?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "rolling resistance factor for [0.671697, 14.249569] (the system rolling resistance factor (Wikidata Q91738044))" + }, + { + "q": "Could you work out the rolling resistance factor given tangential force = 0.671697, normal force = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "rolling resistance factor for [0.671697, 14.249569] (the system rolling resistance factor (Wikidata Q91738044))" + }, + { + "q": "I need the rolling resistance factor for a tangential force of 21.7931 and a normal force of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "rolling resistance factor for [21.793126, 29.510609] (the system rolling resistance factor (Wikidata Q91738044))" + }, + { + "q": "If the tangential force is 21.7931 and the normal force is 29.5106, what is the rolling resistance factor?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "rolling resistance factor for [21.793126, 29.510609] (the system rolling resistance factor (Wikidata Q91738044))" + }, + { + "q": "Could you work out the rolling resistance factor given tangential force = 153.15, normal force = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "rolling resistance factor for [153.149965, 18.595315] (the system rolling resistance factor (Wikidata Q91738044))" + }, + { + "q": "I need the rolling resistance factor for a tangential force of 153.15 and a normal force of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "rolling resistance factor for [153.149965, 18.595315] (the system rolling resistance factor (Wikidata Q91738044))" + }, + { + "q": "If the tangential force is 8.50728, what is the rolling resistance factor?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the normal force is not given" + }, + { + "q": "Could you work out the rolling resistance factor given tangential force = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the normal force is not given" + }, + { + "q": "Do you know the rolling resistance factor of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the rolling resistance factor of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the rolling resistance factor given tangential force = -0.318734, normal force = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative tangential force: no example had one" + }, + { + "q": "What is a typical rolling resistance factor?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-rolling-resistance-factor/program.py b/root/organs10/physics/specialists/solved-rolling-resistance-factor/program.py new file mode 100644 index 0000000000000000000000000000000000000000..934013d9d31221fd8d3e41981ecf67fc8e992e41 --- /dev/null +++ b/root/organs10/physics/specialists/solved-rolling-resistance-factor/program.py @@ -0,0 +1,7 @@ +"""rolling resistance factor from tangential force, normal force (rolling resistance factor (Wikidata Q91738044)) +The maths organ (systems.py): solved from the system q1 = q0*q2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(tangential_force, normal_force): + return 1.0*tangential_force/normal_force diff --git a/root/organs10/physics/specialists/solved-rolling-resistance-factor/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-rolling-resistance-factor/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-rolling-resistance-factor/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-root-mean-square-sound-pressu/organ_formula_v2.py b/root/organs10/physics/specialists/solved-root-mean-square-sound-pressu/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-root-mean-square-sound-pressu/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-root-mean-square-sound-pressu/panel.json b/root/organs10/physics/specialists/solved-root-mean-square-sound-pressu/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..51f32e412e690e77a5458229277c18ea4aa2d8ab --- /dev/null +++ b/root/organs10/physics/specialists/solved-root-mean-square-sound-pressu/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the root-mean-square sound pressure for acoustic impedance 107.961 and sound volume velocity 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "Pa", + "why": "root-mean-square sound pressure for [107.960666, 59.4105] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "Tell me the root-mean-square sound pressure with acoustic impedance of 107.961 and sound volume velocity of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "Pa", + "why": "root-mean-square sound pressure for [107.960666, 59.4105] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "What is the root-mean-square sound pressure for acoustic impedance 169.527 and sound volume velocity 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "Pa", + "why": "root-mean-square sound pressure for [169.526958, 280.773702] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "Tell me the root-mean-square sound pressure with acoustic impedance of 169.527 and sound volume velocity of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "Pa", + "why": "root-mean-square sound pressure for [169.526958, 280.773702] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "What is the root-mean-square sound pressure for acoustic impedance 1.91195 and sound volume velocity 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "Pa", + "why": "root-mean-square sound pressure for [1.911952, 82.240072] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "Tell me the root-mean-square sound pressure with acoustic impedance of 1.91195 and sound volume velocity of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "Pa", + "why": "root-mean-square sound pressure for [1.911952, 82.240072] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "What is the root-mean-square sound pressure for acoustic impedance 1.64379 and sound volume velocity 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "Pa", + "why": "root-mean-square sound pressure for [1.643795, 252.709024] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "Tell me the root-mean-square sound pressure with acoustic impedance of 1.64379 and sound volume velocity of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "Pa", + "why": "root-mean-square sound pressure for [1.643795, 252.709024] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "What is the root-mean-square sound pressure for acoustic impedance 18.3169 and sound volume velocity 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "Pa", + "why": "root-mean-square sound pressure for [18.316867, 6.838598] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "Tell me the root-mean-square sound pressure with acoustic impedance of 18.3169 and sound volume velocity of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "Pa", + "why": "root-mean-square sound pressure for [18.316867, 6.838598] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "What is the root-mean-square sound pressure for acoustic impedance 156.502 and sound volume velocity 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "Pa", + "why": "root-mean-square sound pressure for [156.50181, 185.886273] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "Tell me the root-mean-square sound pressure with acoustic impedance of 156.502 and sound volume velocity of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "Pa", + "why": "root-mean-square sound pressure for [156.50181, 185.886273] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "What is the root-mean-square sound pressure for acoustic impedance 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the sound volume velocity is not given" + }, + { + "q": "Tell me the root-mean-square sound pressure with acoustic impedance of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the sound volume velocity is not given" + }, + { + "q": "What is the root-mean-square sound pressure of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the root-mean-square sound pressure of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the root-mean-square sound pressure with acoustic impedance of -1.91195 and sound volume velocity of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative acoustic impedance: no example had one" + }, + { + "q": "Which root-mean-square sound pressure is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the acoustic impedance is 8.50728 and the sound volume velocity is 17.7888, what is the root-mean-square sound pressure?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "Pa", + "why": "root-mean-square sound pressure for [8.507283, 17.788778] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "Could you work out the root-mean-square sound pressure given acoustic impedance = 8.50728, sound volume velocity = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "Pa", + "why": "root-mean-square sound pressure for [8.507283, 17.788778] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "I need the root-mean-square sound pressure for an acoustic impedance of 8.24951 and a sound volume velocity of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "Pa", + "why": "root-mean-square sound pressure for [8.24951, 0.634021] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "If the acoustic impedance is 8.24951 and the sound volume velocity is 0.634021, what is the root-mean-square sound pressure?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "Pa", + "why": "root-mean-square sound pressure for [8.24951, 0.634021] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "Could you work out the root-mean-square sound pressure given acoustic impedance = 0.318734, sound volume velocity = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "Pa", + "why": "root-mean-square sound pressure for [0.318734, 9.566071] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "I need the root-mean-square sound pressure for an acoustic impedance of 0.318734 and a sound volume velocity of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "Pa", + "why": "root-mean-square sound pressure for [0.318734, 9.566071] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "If the acoustic impedance is 0.671697 and the sound volume velocity is 14.2496, what is the root-mean-square sound pressure?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "Pa", + "why": "root-mean-square sound pressure for [0.671697, 14.249569] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "Could you work out the root-mean-square sound pressure given acoustic impedance = 0.671697, sound volume velocity = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "Pa", + "why": "root-mean-square sound pressure for [0.671697, 14.249569] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "I need the root-mean-square sound pressure for an acoustic impedance of 21.7931 and a sound volume velocity of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "Pa", + "why": "root-mean-square sound pressure for [21.793126, 29.510609] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "If the acoustic impedance is 21.7931 and the sound volume velocity is 29.5106, what is the root-mean-square sound pressure?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "Pa", + "why": "root-mean-square sound pressure for [21.793126, 29.510609] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "Could you work out the root-mean-square sound pressure given acoustic impedance = 153.15, sound volume velocity = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "Pa", + "why": "root-mean-square sound pressure for [153.149965, 18.595315] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "I need the root-mean-square sound pressure for an acoustic impedance of 153.15 and a sound volume velocity of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "Pa", + "why": "root-mean-square sound pressure for [153.149965, 18.595315] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "If the acoustic impedance is 8.50728, what is the root-mean-square sound pressure?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the sound volume velocity is not given" + }, + { + "q": "Could you work out the root-mean-square sound pressure given acoustic impedance = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the sound volume velocity is not given" + }, + { + "q": "Do you know the root-mean-square sound pressure of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the root-mean-square sound pressure of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the root-mean-square sound pressure given acoustic impedance = -0.318734, sound volume velocity = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative acoustic impedance: no example had one" + }, + { + "q": "What is a typical root-mean-square sound pressure?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-root-mean-square-sound-pressu/program.py b/root/organs10/physics/specialists/solved-root-mean-square-sound-pressu/program.py new file mode 100644 index 0000000000000000000000000000000000000000..64454455ea935cbde7210b9cff3b7556b4b296fd --- /dev/null +++ b/root/organs10/physics/specialists/solved-root-mean-square-sound-pressu/program.py @@ -0,0 +1,7 @@ +"""root-mean-square sound pressure from acoustic impedance, sound volume velocity (acoustic impedance (Wikidata Q975684)) +The maths organ (systems.py): solved from the system q1 = ((q0)/(q2)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(acoustic_impedance, sound_volume_velocity): + return 1.0*acoustic_impedance*sound_volume_velocity diff --git a/root/organs10/physics/specialists/solved-root-mean-square-sound-pressu/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-root-mean-square-sound-pressu/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-root-mean-square-sound-pressu/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-schmidt-number/organ_formula_v2.py b/root/organs10/physics/specialists/solved-schmidt-number/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-schmidt-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-schmidt-number/panel.json b/root/organs10/physics/specialists/solved-schmidt-number/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..34066ff175fb4d7ecd92d32a4d8f3eac34a64ea7 --- /dev/null +++ b/root/organs10/physics/specialists/solved-schmidt-number/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the Schmidt number for kinematic viscosity 107.961 and mass diffusivity 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "Schmidt number for [107.960666, 59.4105] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "Tell me the Schmidt number with kinematic viscosity of 107.961 and mass diffusivity of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "Schmidt number for [107.960666, 59.4105] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "What is the Schmidt number for kinematic viscosity 169.527 and mass diffusivity 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "Schmidt number for [169.526958, 280.773702] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "Tell me the Schmidt number with kinematic viscosity of 169.527 and mass diffusivity of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "Schmidt number for [169.526958, 280.773702] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "What is the Schmidt number for kinematic viscosity 1.91195 and mass diffusivity 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "Schmidt number for [1.911952, 82.240072] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "Tell me the Schmidt number with kinematic viscosity of 1.91195 and mass diffusivity of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "Schmidt number for [1.911952, 82.240072] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "What is the Schmidt number for kinematic viscosity 1.64379 and mass diffusivity 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "Schmidt number for [1.643795, 252.709024] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "Tell me the Schmidt number with kinematic viscosity of 1.64379 and mass diffusivity of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "Schmidt number for [1.643795, 252.709024] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "What is the Schmidt number for kinematic viscosity 18.3169 and mass diffusivity 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "Schmidt number for [18.316867, 6.838598] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "Tell me the Schmidt number with kinematic viscosity of 18.3169 and mass diffusivity of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "Schmidt number for [18.316867, 6.838598] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "What is the Schmidt number for kinematic viscosity 156.502 and mass diffusivity 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "Schmidt number for [156.50181, 185.886273] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "Tell me the Schmidt number with kinematic viscosity of 156.502 and mass diffusivity of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "Schmidt number for [156.50181, 185.886273] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "What is the Schmidt number for kinematic viscosity 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass diffusivity is not given" + }, + { + "q": "Tell me the Schmidt number with kinematic viscosity of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass diffusivity is not given" + }, + { + "q": "What is the Schmidt 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 Schmidt 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 Schmidt number with kinematic viscosity of -1.91195 and mass diffusivity of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative kinematic viscosity: no example had one" + }, + { + "q": "Which Schmidt 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 kinematic viscosity is 8.50728 and the mass diffusivity is 17.7888, what is the Schmidt number?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "Schmidt number for [8.507283, 17.788778] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "Could you work out the Schmidt number given kinematic viscosity = 8.50728, mass diffusivity = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "Schmidt number for [8.507283, 17.788778] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "I need the Schmidt number for a kinematic viscosity of 8.24951 and a mass diffusivity of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "Schmidt number for [8.24951, 0.634021] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "If the kinematic viscosity is 8.24951 and the mass diffusivity is 0.634021, what is the Schmidt number?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "Schmidt number for [8.24951, 0.634021] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "Could you work out the Schmidt number given kinematic viscosity = 0.318734, mass diffusivity = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "Schmidt number for [0.318734, 9.566071] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "I need the Schmidt number for a kinematic viscosity of 0.318734 and a mass diffusivity of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "Schmidt number for [0.318734, 9.566071] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "If the kinematic viscosity is 0.671697 and the mass diffusivity is 14.2496, what is the Schmidt number?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "Schmidt number for [0.671697, 14.249569] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "Could you work out the Schmidt number given kinematic viscosity = 0.671697, mass diffusivity = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "Schmidt number for [0.671697, 14.249569] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "I need the Schmidt number for a kinematic viscosity of 21.7931 and a mass diffusivity of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "Schmidt number for [21.793126, 29.510609] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "If the kinematic viscosity is 21.7931 and the mass diffusivity is 29.5106, what is the Schmidt number?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "Schmidt number for [21.793126, 29.510609] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "Could you work out the Schmidt number given kinematic viscosity = 153.15, mass diffusivity = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "Schmidt number for [153.149965, 18.595315] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "I need the Schmidt number for a kinematic viscosity of 153.15 and a mass diffusivity of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "Schmidt number for [153.149965, 18.595315] (the system Schmidt number (Wikidata Q581997))" + }, + { + "q": "If the kinematic viscosity is 8.50728, what is the Schmidt number?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass diffusivity is not given" + }, + { + "q": "Could you work out the Schmidt number given kinematic viscosity = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass diffusivity is not given" + }, + { + "q": "Do you know the Schmidt 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 Schmidt 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 Schmidt number given kinematic viscosity = -0.318734, mass diffusivity = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative kinematic viscosity: no example had one" + }, + { + "q": "What is a typical Schmidt 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-schmidt-number/program.py b/root/organs10/physics/specialists/solved-schmidt-number/program.py new file mode 100644 index 0000000000000000000000000000000000000000..4caa6e9e39ea6a80e688b80770b7277e2e4f83c3 --- /dev/null +++ b/root/organs10/physics/specialists/solved-schmidt-number/program.py @@ -0,0 +1,7 @@ +"""Schmidt number from kinematic viscosity, mass diffusivity (Schmidt number (Wikidata Q581997)) +The maths organ (systems.py): solved from the system q0 = ((q1)/(q2)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(kinematic_viscosity, mass_diffusivity): + return 1.0*kinematic_viscosity/mass_diffusivity diff --git a/root/organs10/physics/specialists/solved-schmidt-number/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-schmidt-number/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-schmidt-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-second-axial-moment-of-area/organ_formula_v2.py b/root/organs10/physics/specialists/solved-second-axial-moment-of-area/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-second-axial-moment-of-area/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-second-axial-moment-of-area/panel.json b/root/organs10/physics/specialists/solved-second-axial-moment-of-area/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..dcdac1c95821df3d4aae93903d90cab2599a6005 --- /dev/null +++ b/root/organs10/physics/specialists/solved-second-axial-moment-of-area/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the second axial moment of area for maximum radial distance 107.961 and section modulus 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "m^4", + "why": "second axial moment of area for [107.960666, 59.4105] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "Tell me the second axial moment of area with maximum radial distance of 107.961 and section modulus of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "m^4", + "why": "second axial moment of area for [107.960666, 59.4105] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "What is the second axial moment of area for maximum radial distance 169.527 and section modulus 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "m^4", + "why": "second axial moment of area for [169.526958, 280.773702] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "Tell me the second axial moment of area with maximum radial distance of 169.527 and section modulus of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "m^4", + "why": "second axial moment of area for [169.526958, 280.773702] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "What is the second axial moment of area for maximum radial distance 1.91195 and section modulus 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "m^4", + "why": "second axial moment of area for [1.911952, 82.240072] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "Tell me the second axial moment of area with maximum radial distance of 1.91195 and section modulus of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "m^4", + "why": "second axial moment of area for [1.911952, 82.240072] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "What is the second axial moment of area for maximum radial distance 1.64379 and section modulus 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "m^4", + "why": "second axial moment of area for [1.643795, 252.709024] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "Tell me the second axial moment of area with maximum radial distance of 1.64379 and section modulus of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "m^4", + "why": "second axial moment of area for [1.643795, 252.709024] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "What is the second axial moment of area for maximum radial distance 18.3169 and section modulus 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "m^4", + "why": "second axial moment of area for [18.316867, 6.838598] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "Tell me the second axial moment of area with maximum radial distance of 18.3169 and section modulus of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "m^4", + "why": "second axial moment of area for [18.316867, 6.838598] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "What is the second axial moment of area for maximum radial distance 156.502 and section modulus 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "m^4", + "why": "second axial moment of area for [156.50181, 185.886273] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "Tell me the second axial moment of area with maximum radial distance of 156.502 and section modulus of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "m^4", + "why": "second axial moment of area for [156.50181, 185.886273] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "What is the second axial moment of area for maximum radial distance 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the section modulus is not given" + }, + { + "q": "Tell me the second axial moment of area with maximum radial distance of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the section modulus is not given" + }, + { + "q": "What is the second axial moment of area of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the second axial moment of area of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the second axial moment of area with maximum radial distance of -1.91195 and section modulus of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative maximum radial distance: no example had one" + }, + { + "q": "Which second axial moment of area is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the maximum radial distance is 8.50728 and the section modulus is 17.7888, what is the second axial moment of area?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "m^4", + "why": "second axial moment of area for [8.507283, 17.788778] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "Could you work out the second axial moment of area given maximum radial distance = 8.50728, section modulus = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "m^4", + "why": "second axial moment of area for [8.507283, 17.788778] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "I need the second axial moment of area for a maximum radial distance of 8.24951 and a section modulus of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "m^4", + "why": "second axial moment of area for [8.24951, 0.634021] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "If the maximum radial distance is 8.24951 and the section modulus is 0.634021, what is the second axial moment of area?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "m^4", + "why": "second axial moment of area for [8.24951, 0.634021] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "Could you work out the second axial moment of area given maximum radial distance = 0.318734, section modulus = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "m^4", + "why": "second axial moment of area for [0.318734, 9.566071] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "I need the second axial moment of area for a maximum radial distance of 0.318734 and a section modulus of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "m^4", + "why": "second axial moment of area for [0.318734, 9.566071] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "If the maximum radial distance is 0.671697 and the section modulus is 14.2496, what is the second axial moment of area?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "m^4", + "why": "second axial moment of area for [0.671697, 14.249569] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "Could you work out the second axial moment of area given maximum radial distance = 0.671697, section modulus = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "m^4", + "why": "second axial moment of area for [0.671697, 14.249569] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "I need the second axial moment of area for a maximum radial distance of 21.7931 and a section modulus of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "m^4", + "why": "second axial moment of area for [21.793126, 29.510609] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "If the maximum radial distance is 21.7931 and the section modulus is 29.5106, what is the second axial moment of area?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "m^4", + "why": "second axial moment of area for [21.793126, 29.510609] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "Could you work out the second axial moment of area given maximum radial distance = 153.15, section modulus = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "m^4", + "why": "second axial moment of area for [153.149965, 18.595315] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "I need the second axial moment of area for a maximum radial distance of 153.15 and a section modulus of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "m^4", + "why": "second axial moment of area for [153.149965, 18.595315] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "If the maximum radial distance is 8.50728, what is the second axial moment of area?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the section modulus is not given" + }, + { + "q": "Could you work out the second axial moment of area given maximum radial distance = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the section modulus is not given" + }, + { + "q": "Do you know the second axial moment of area of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the second axial moment of area of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the second axial moment of area given maximum radial distance = -0.318734, section modulus = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative maximum radial distance: no example had one" + }, + { + "q": "What is a typical second axial moment of area?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-second-axial-moment-of-area/program.py b/root/organs10/physics/specialists/solved-second-axial-moment-of-area/program.py new file mode 100644 index 0000000000000000000000000000000000000000..2f53ea102eb0e47539911efaac13d90b125f4e0b --- /dev/null +++ b/root/organs10/physics/specialists/solved-second-axial-moment-of-area/program.py @@ -0,0 +1,7 @@ +"""second axial moment of area from maximum radial distance, section modulus (section modulus (Wikidata Q1930808)) +The maths organ (systems.py): solved from the system q2 = q1 / q0; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(maximum_radial_distance, section_modulus): + return 1.0*maximum_radial_distance*section_modulus diff --git a/root/organs10/physics/specialists/solved-second-axial-moment-of-area/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-second-axial-moment-of-area/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-second-axial-moment-of-area/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-section-modulus/organ_formula_v2.py b/root/organs10/physics/specialists/solved-section-modulus/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-section-modulus/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-section-modulus/panel.json b/root/organs10/physics/specialists/solved-section-modulus/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..fd39397d0eecb6b9f222dcdb60cf52fb3769a67a --- /dev/null +++ b/root/organs10/physics/specialists/solved-section-modulus/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the section modulus for maximum radial distance 107.961 and second axial moment of area 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "m^3", + "why": "section modulus for [107.960666, 59.4105] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "Tell me the section modulus with maximum radial distance of 107.961 and second axial moment of area of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "m^3", + "why": "section modulus for [107.960666, 59.4105] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "What is the section modulus for maximum radial distance 169.527 and second axial moment of area 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "m^3", + "why": "section modulus for [169.526958, 280.773702] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "Tell me the section modulus with maximum radial distance of 169.527 and second axial moment of area of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "m^3", + "why": "section modulus for [169.526958, 280.773702] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "What is the section modulus for maximum radial distance 1.91195 and second axial moment of area 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "m^3", + "why": "section modulus for [1.911952, 82.240072] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "Tell me the section modulus with maximum radial distance of 1.91195 and second axial moment of area of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "m^3", + "why": "section modulus for [1.911952, 82.240072] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "What is the section modulus for maximum radial distance 1.64379 and second axial moment of area 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "m^3", + "why": "section modulus for [1.643795, 252.709024] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "Tell me the section modulus with maximum radial distance of 1.64379 and second axial moment of area of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "m^3", + "why": "section modulus for [1.643795, 252.709024] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "What is the section modulus for maximum radial distance 18.3169 and second axial moment of area 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "m^3", + "why": "section modulus for [18.316867, 6.838598] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "Tell me the section modulus with maximum radial distance of 18.3169 and second axial moment of area of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "m^3", + "why": "section modulus for [18.316867, 6.838598] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "What is the section modulus for maximum radial distance 156.502 and second axial moment of area 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "m^3", + "why": "section modulus for [156.50181, 185.886273] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "Tell me the section modulus with maximum radial distance of 156.502 and second axial moment of area of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "m^3", + "why": "section modulus for [156.50181, 185.886273] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "What is the section modulus for maximum radial distance 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the second axial moment of area is not given" + }, + { + "q": "Tell me the section modulus with maximum radial distance of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the second axial moment of area is not given" + }, + { + "q": "What is the section modulus 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 section modulus 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 section modulus with maximum radial distance of -1.91195 and second axial moment of area of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative maximum radial distance: no example had one" + }, + { + "q": "Which section modulus 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 maximum radial distance is 8.50728 and the second axial moment of area is 17.7888, what is the section modulus?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "m^3", + "why": "section modulus for [8.507283, 17.788778] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "Could you work out the section modulus given maximum radial distance = 8.50728, second axial moment of area = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "m^3", + "why": "section modulus for [8.507283, 17.788778] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "I need the section modulus for a maximum radial distance of 8.24951 and a second axial moment of area of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "m^3", + "why": "section modulus for [8.24951, 0.634021] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "If the maximum radial distance is 8.24951 and the second axial moment of area is 0.634021, what is the section modulus?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "m^3", + "why": "section modulus for [8.24951, 0.634021] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "Could you work out the section modulus given maximum radial distance = 0.318734, second axial moment of area = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "m^3", + "why": "section modulus for [0.318734, 9.566071] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "I need the section modulus for a maximum radial distance of 0.318734 and a second axial moment of area of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "m^3", + "why": "section modulus for [0.318734, 9.566071] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "If the maximum radial distance is 0.671697 and the second axial moment of area is 14.2496, what is the section modulus?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "m^3", + "why": "section modulus for [0.671697, 14.249569] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "Could you work out the section modulus given maximum radial distance = 0.671697, second axial moment of area = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "m^3", + "why": "section modulus for [0.671697, 14.249569] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "I need the section modulus for a maximum radial distance of 21.7931 and a second axial moment of area of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "m^3", + "why": "section modulus for [21.793126, 29.510609] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "If the maximum radial distance is 21.7931 and the second axial moment of area is 29.5106, what is the section modulus?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "m^3", + "why": "section modulus for [21.793126, 29.510609] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "Could you work out the section modulus given maximum radial distance = 153.15, second axial moment of area = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "m^3", + "why": "section modulus for [153.149965, 18.595315] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "I need the section modulus for a maximum radial distance of 153.15 and a second axial moment of area of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "m^3", + "why": "section modulus for [153.149965, 18.595315] (the system section modulus (Wikidata Q1930808))" + }, + { + "q": "If the maximum radial distance is 8.50728, what is the section modulus?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the second axial moment of area is not given" + }, + { + "q": "Could you work out the section modulus given maximum radial distance = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the second axial moment of area is not given" + }, + { + "q": "Do you know the section modulus 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 section modulus 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 section modulus given maximum radial distance = -0.318734, second axial moment of area = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative maximum radial distance: no example had one" + }, + { + "q": "What is a typical section modulus?", + "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-section-modulus/program.py b/root/organs10/physics/specialists/solved-section-modulus/program.py new file mode 100644 index 0000000000000000000000000000000000000000..660b259386911e7114635214ed74ecfae455c50d --- /dev/null +++ b/root/organs10/physics/specialists/solved-section-modulus/program.py @@ -0,0 +1,7 @@ +"""section modulus from maximum radial distance, second axial moment of area (section modulus (Wikidata Q1930808)) +The maths organ (systems.py): solved from the system q2 = q1 / q0; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(maximum_radial_distance, second_axial_moment_of_area): + return 1.0*second_axial_moment_of_area/maximum_radial_distance diff --git a/root/organs10/physics/specialists/solved-section-modulus/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-section-modulus/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-section-modulus/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-shear-modulus/organ_formula_v2.py b/root/organs10/physics/specialists/solved-shear-modulus/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-modulus/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-shear-modulus/panel.json b/root/organs10/physics/specialists/solved-shear-modulus/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..65945cf64b95cf8a4c48a030542e072b9f9fc5ce --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-modulus/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the shear modulus for shear stress 107.961 and shear strain 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "Pa", + "why": "shear modulus for [107.960666, 59.4105] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Tell me the shear modulus with shear stress of 107.961 and shear strain of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "Pa", + "why": "shear modulus for [107.960666, 59.4105] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "What is the shear modulus for shear stress 169.527 and shear strain 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "Pa", + "why": "shear modulus for [169.526958, 280.773702] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Tell me the shear modulus with shear stress of 169.527 and shear strain of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "Pa", + "why": "shear modulus for [169.526958, 280.773702] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "What is the shear modulus for shear stress 1.91195 and shear strain 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "Pa", + "why": "shear modulus for [1.911952, 82.240072] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Tell me the shear modulus with shear stress of 1.91195 and shear strain of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "Pa", + "why": "shear modulus for [1.911952, 82.240072] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "What is the shear modulus for shear stress 1.64379 and shear strain 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "Pa", + "why": "shear modulus for [1.643795, 252.709024] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Tell me the shear modulus with shear stress of 1.64379 and shear strain of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "Pa", + "why": "shear modulus for [1.643795, 252.709024] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "What is the shear modulus for shear stress 18.3169 and shear strain 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "Pa", + "why": "shear modulus for [18.316867, 6.838598] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Tell me the shear modulus with shear stress of 18.3169 and shear strain of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "Pa", + "why": "shear modulus for [18.316867, 6.838598] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "What is the shear modulus for shear stress 156.502 and shear strain 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "Pa", + "why": "shear modulus for [156.50181, 185.886273] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Tell me the shear modulus with shear stress of 156.502 and shear strain of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "Pa", + "why": "shear modulus for [156.50181, 185.886273] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "What is the shear modulus for shear stress 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the shear strain is not given" + }, + { + "q": "Tell me the shear modulus with shear stress of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the shear strain is not given" + }, + { + "q": "What is the shear modulus 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 shear modulus 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 shear modulus with shear stress of -1.91195 and shear strain of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative shear stress: no example had one" + }, + { + "q": "Which shear modulus is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the shear stress is 8.50728 and the shear strain is 17.7888, what is the shear modulus?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "Pa", + "why": "shear modulus for [8.507283, 17.788778] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Could you work out the shear modulus given shear stress = 8.50728, shear strain = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "Pa", + "why": "shear modulus for [8.507283, 17.788778] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "I need the shear modulus for a shear stress of 8.24951 and a shear strain of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "Pa", + "why": "shear modulus for [8.24951, 0.634021] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "If the shear stress is 8.24951 and the shear strain is 0.634021, what is the shear modulus?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "Pa", + "why": "shear modulus for [8.24951, 0.634021] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Could you work out the shear modulus given shear stress = 0.318734, shear strain = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "Pa", + "why": "shear modulus for [0.318734, 9.566071] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "I need the shear modulus for a shear stress of 0.318734 and a shear strain of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "Pa", + "why": "shear modulus for [0.318734, 9.566071] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "If the shear stress is 0.671697 and the shear strain is 14.2496, what is the shear modulus?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "Pa", + "why": "shear modulus for [0.671697, 14.249569] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Could you work out the shear modulus given shear stress = 0.671697, shear strain = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "Pa", + "why": "shear modulus for [0.671697, 14.249569] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "I need the shear modulus for a shear stress of 21.7931 and a shear strain of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "Pa", + "why": "shear modulus for [21.793126, 29.510609] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "If the shear stress is 21.7931 and the shear strain is 29.5106, what is the shear modulus?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "Pa", + "why": "shear modulus for [21.793126, 29.510609] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Could you work out the shear modulus given shear stress = 153.15, shear strain = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "Pa", + "why": "shear modulus for [153.149965, 18.595315] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "I need the shear modulus for a shear stress of 153.15 and a shear strain of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "Pa", + "why": "shear modulus for [153.149965, 18.595315] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "If the shear stress is 8.50728, what is the shear modulus?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the shear strain is not given" + }, + { + "q": "Could you work out the shear modulus given shear stress = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the shear strain is not given" + }, + { + "q": "Do you know the shear modulus 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 shear modulus 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 shear modulus given shear stress = -0.318734, shear strain = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative shear stress: no example had one" + }, + { + "q": "What is a typical shear modulus?", + "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-shear-modulus/program.py b/root/organs10/physics/specialists/solved-shear-modulus/program.py new file mode 100644 index 0000000000000000000000000000000000000000..ab058ebecddaa8270a4b3512a8d1153ad8255faf --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-modulus/program.py @@ -0,0 +1,7 @@ +"""shear modulus from shear stress, shear strain (shear modulus (Wikidata Q461466)) +The maths organ (systems.py): solved from the system q0 = q1 / q2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(shear_stress, shear_strain): + return 1.0*shear_stress/shear_strain diff --git a/root/organs10/physics/specialists/solved-shear-modulus/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-shear-modulus/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-modulus/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-shear-rate/organ_formula_v2.py b/root/organs10/physics/specialists/solved-shear-rate/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-rate/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-shear-rate/panel.json b/root/organs10/physics/specialists/solved-shear-rate/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..4d18dc3210b7a7dcb5485b8c01314c0427a500c7 --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-rate/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the shear rate for speed 107.961 and distance 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "Hz", + "why": "shear rate for [107.960666, 59.4105] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Tell me the shear rate with speed of 107.961 and distance of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "Hz", + "why": "shear rate for [107.960666, 59.4105] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "What is the shear rate for speed 169.527 and distance 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "Hz", + "why": "shear rate for [169.526958, 280.773702] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Tell me the shear rate with speed of 169.527 and distance of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "Hz", + "why": "shear rate for [169.526958, 280.773702] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "What is the shear rate for speed 1.91195 and distance 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "Hz", + "why": "shear rate for [1.911952, 82.240072] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Tell me the shear rate with speed of 1.91195 and distance of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "Hz", + "why": "shear rate for [1.911952, 82.240072] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "What is the shear rate for speed 1.64379 and distance 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "Hz", + "why": "shear rate for [1.643795, 252.709024] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Tell me the shear rate with speed of 1.64379 and distance of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "Hz", + "why": "shear rate for [1.643795, 252.709024] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "What is the shear rate for speed 18.3169 and distance 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "Hz", + "why": "shear rate for [18.316867, 6.838598] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Tell me the shear rate with speed of 18.3169 and distance of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "Hz", + "why": "shear rate for [18.316867, 6.838598] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "What is the shear rate for speed 156.502 and distance 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "Hz", + "why": "shear rate for [156.50181, 185.886273] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Tell me the shear rate with speed of 156.502 and distance of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "Hz", + "why": "shear rate for [156.50181, 185.886273] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "What is the shear rate for speed 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the distance is not given" + }, + { + "q": "Tell me the shear rate with speed of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the distance is not given" + }, + { + "q": "What is the shear rate of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the shear rate of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the shear rate with speed of -1.91195 and distance of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative speed: no example had one" + }, + { + "q": "Which shear rate is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the speed is 8.50728 and the distance is 17.7888, what is the shear rate?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "Hz", + "why": "shear rate for [8.507283, 17.788778] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Could you work out the shear rate given speed = 8.50728, distance = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "Hz", + "why": "shear rate for [8.507283, 17.788778] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "I need the shear rate for a speed of 8.24951 and a distance of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "Hz", + "why": "shear rate for [8.24951, 0.634021] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "If the speed is 8.24951 and the distance is 0.634021, what is the shear rate?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "Hz", + "why": "shear rate for [8.24951, 0.634021] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Could you work out the shear rate given speed = 0.318734, distance = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "Hz", + "why": "shear rate for [0.318734, 9.566071] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "I need the shear rate for a speed of 0.318734 and a distance of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "Hz", + "why": "shear rate for [0.318734, 9.566071] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "If the speed is 0.671697 and the distance is 14.2496, what is the shear rate?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "Hz", + "why": "shear rate for [0.671697, 14.249569] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Could you work out the shear rate given speed = 0.671697, distance = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "Hz", + "why": "shear rate for [0.671697, 14.249569] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "I need the shear rate for a speed of 21.7931 and a distance of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "Hz", + "why": "shear rate for [21.793126, 29.510609] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "If the speed is 21.7931 and the distance is 29.5106, what is the shear rate?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "Hz", + "why": "shear rate for [21.793126, 29.510609] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Could you work out the shear rate given speed = 153.15, distance = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "Hz", + "why": "shear rate for [153.149965, 18.595315] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "I need the shear rate for a speed of 153.15 and a distance of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "Hz", + "why": "shear rate for [153.149965, 18.595315] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "If the speed is 8.50728, what is the shear rate?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the distance is not given" + }, + { + "q": "Could you work out the shear rate given speed = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the distance is not given" + }, + { + "q": "Do you know the shear rate of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the shear rate of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the shear rate given speed = -0.318734, distance = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative speed: no example had one" + }, + { + "q": "What is a typical shear rate?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-shear-rate/program.py b/root/organs10/physics/specialists/solved-shear-rate/program.py new file mode 100644 index 0000000000000000000000000000000000000000..b82e7e5cacbe0aefe64adedb44271fb384c148b3 --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-rate/program.py @@ -0,0 +1,7 @@ +"""shear rate from speed, distance (shear rate (Wikidata Q461293)) +The maths organ (systems.py): solved from the system q0 = ((q1)/(q2)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(speed, distance): + return 1.0*speed/distance diff --git a/root/organs10/physics/specialists/solved-shear-rate/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-shear-rate/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-rate/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-shear-strain-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-shear-strain-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-strain-2/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-shear-strain-2/panel.json b/root/organs10/physics/specialists/solved-shear-strain-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..b732a97fd8e96a11b15f410029a1098a6bcad5d3 --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-strain-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the shear strain for shear modulus 107.961 and shear stress 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [107.960666, 59.4105] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Tell me the shear strain with shear modulus of 107.961 and shear stress of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [107.960666, 59.4105] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "What is the shear strain for shear modulus 169.527 and shear stress 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [169.526958, 280.773702] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Tell me the shear strain with shear modulus of 169.527 and shear stress of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [169.526958, 280.773702] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "What is the shear strain for shear modulus 1.91195 and shear stress 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [1.911952, 82.240072] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Tell me the shear strain with shear modulus of 1.91195 and shear stress of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [1.911952, 82.240072] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "What is the shear strain for shear modulus 1.64379 and shear stress 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [1.643795, 252.709024] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Tell me the shear strain with shear modulus of 1.64379 and shear stress of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [1.643795, 252.709024] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "What is the shear strain for shear modulus 18.3169 and shear stress 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [18.316867, 6.838598] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Tell me the shear strain with shear modulus of 18.3169 and shear stress of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [18.316867, 6.838598] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "What is the shear strain for shear modulus 156.502 and shear stress 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [156.50181, 185.886273] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Tell me the shear strain with shear modulus of 156.502 and shear stress of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [156.50181, 185.886273] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "What is the shear strain for shear modulus 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the shear stress is not given" + }, + { + "q": "Tell me the shear strain with shear modulus of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the shear stress is not given" + }, + { + "q": "What is the shear strain of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the shear strain of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the shear strain with shear modulus of -1.91195 and shear stress of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative shear modulus: no example had one" + }, + { + "q": "Which shear strain is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the shear modulus is 8.50728 and the shear stress is 17.7888, what is the shear strain?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [8.507283, 17.788778] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Could you work out the shear strain given shear modulus = 8.50728, shear stress = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [8.507283, 17.788778] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "I need the shear strain for a shear modulus of 8.24951 and a shear stress of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [8.24951, 0.634021] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "If the shear modulus is 8.24951 and the shear stress is 0.634021, what is the shear strain?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [8.24951, 0.634021] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Could you work out the shear strain given shear modulus = 0.318734, shear stress = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [0.318734, 9.566071] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "I need the shear strain for a shear modulus of 0.318734 and a shear stress of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [0.318734, 9.566071] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "If the shear modulus is 0.671697 and the shear stress is 14.2496, what is the shear strain?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [0.671697, 14.249569] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Could you work out the shear strain given shear modulus = 0.671697, shear stress = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [0.671697, 14.249569] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "I need the shear strain for a shear modulus of 21.7931 and a shear stress of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [21.793126, 29.510609] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "If the shear modulus is 21.7931 and the shear stress is 29.5106, what is the shear strain?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [21.793126, 29.510609] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Could you work out the shear strain given shear modulus = 153.15, shear stress = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [153.149965, 18.595315] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "I need the shear strain for a shear modulus of 153.15 and a shear stress of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [153.149965, 18.595315] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "If the shear modulus is 8.50728, what is the shear strain?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the shear stress is not given" + }, + { + "q": "Could you work out the shear strain given shear modulus = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the shear stress is not given" + }, + { + "q": "Do you know the shear strain of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the shear strain of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the shear strain given shear modulus = -0.318734, shear stress = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative shear modulus: no example had one" + }, + { + "q": "What is a typical shear strain?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-shear-strain-2/program.py b/root/organs10/physics/specialists/solved-shear-strain-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..f76fb50a86ad8f179ea975d2821c4f072816ce70 --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-strain-2/program.py @@ -0,0 +1,7 @@ +"""shear strain from shear modulus, shear stress (shear modulus (Wikidata Q461466)) +The maths organ (systems.py): solved from the system q0 = q1 / q2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(shear_modulus, shear_stress): + return 1.0*shear_stress/shear_modulus diff --git a/root/organs10/physics/specialists/solved-shear-strain-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-shear-strain-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-strain-2/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-shear-strain-rate/organ_formula_v2.py b/root/organs10/physics/specialists/solved-shear-strain-rate/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-strain-rate/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-shear-strain-rate/panel.json b/root/organs10/physics/specialists/solved-shear-strain-rate/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..864f1988ce96a706160882c38e7f93716284320b --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-strain-rate/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the shear strain rate for relaxation time 107.961 and Weissenberg number 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "Hz", + "why": "shear strain rate for [107.960666, 59.4105] (the system Weissenberg number (Wikidata Q1753014))" + }, + { + "q": "Tell me the shear strain rate with relaxation time of 107.961 and Weissenberg number of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "Hz", + "why": "shear strain rate for [107.960666, 59.4105] (the system Weissenberg number (Wikidata Q1753014))" + }, + { + "q": "What is the shear strain rate for relaxation time 169.527 and Weissenberg number 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "Hz", + "why": "shear strain rate for [169.526958, 280.773702] (the system Weissenberg number (Wikidata Q1753014))" + }, + { + "q": "Tell me the shear strain rate with relaxation time of 169.527 and Weissenberg number of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "Hz", + "why": "shear strain rate for [169.526958, 280.773702] (the system Weissenberg number (Wikidata Q1753014))" + }, + { + "q": "What is the shear strain rate for relaxation time 1.91195 and Weissenberg number 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "Hz", + "why": "shear strain rate for [1.911952, 82.240072] (the system Weissenberg number (Wikidata Q1753014))" + }, + { + "q": "Tell me the shear strain rate with relaxation time of 1.91195 and Weissenberg number of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "Hz", + "why": "shear strain rate for [1.911952, 82.240072] (the system Weissenberg number (Wikidata Q1753014))" + }, + { + "q": "What is the shear strain rate for relaxation time 1.64379 and Weissenberg number 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "Hz", + "why": "shear strain rate for [1.643795, 252.709024] (the system Weissenberg number (Wikidata Q1753014))" + }, + { + "q": "Tell me the shear strain rate with relaxation time of 1.64379 and Weissenberg number of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "Hz", + "why": "shear strain rate for [1.643795, 252.709024] (the system Weissenberg number (Wikidata Q1753014))" + }, + { + "q": "What is the shear strain rate for relaxation time 18.3169 and Weissenberg number 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "Hz", + "why": "shear strain rate for [18.316867, 6.838598] (the system Weissenberg number (Wikidata Q1753014))" + }, + { + "q": "Tell me the shear strain rate with relaxation time of 18.3169 and Weissenberg number of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "Hz", + "why": "shear strain rate for [18.316867, 6.838598] (the system Weissenberg number (Wikidata Q1753014))" + }, + { + "q": "What is the shear strain rate for relaxation time 156.502 and Weissenberg number 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "Hz", + "why": "shear strain rate for [156.50181, 185.886273] (the system Weissenberg number (Wikidata Q1753014))" + }, + { + "q": "Tell me the shear strain rate with relaxation time of 156.502 and Weissenberg number of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "Hz", + "why": "shear strain rate for [156.50181, 185.886273] (the system Weissenberg number (Wikidata Q1753014))" + }, + { + "q": "What is the shear strain rate for relaxation time 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Weissenberg number is not given" + }, + { + "q": "Tell me the shear strain rate with relaxation time of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Weissenberg number is not given" + }, + { + "q": "What is the shear strain rate of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the shear strain rate of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the shear strain rate with relaxation time of -1.91195 and Weissenberg number of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative relaxation time: no example had one" + }, + { + "q": "Which shear strain rate is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the relaxation time is 8.50728 and the Weissenberg number is 17.7888, what is the shear strain rate?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "Hz", + "why": "shear strain rate for [8.507283, 17.788778] (the system Weissenberg number (Wikidata Q1753014))" + }, + { + "q": "Could you work out the shear strain rate given relaxation time = 8.50728, Weissenberg number = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "Hz", + "why": "shear strain rate for [8.507283, 17.788778] (the system Weissenberg number (Wikidata Q1753014))" + }, + { + "q": "I need the shear strain rate for a relaxation time of 8.24951 and a Weissenberg number of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "Hz", + "why": "shear strain rate for [8.24951, 0.634021] (the system Weissenberg number (Wikidata Q1753014))" + }, + { + "q": "If the relaxation time is 8.24951 and the Weissenberg number is 0.634021, what is the shear strain rate?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "Hz", + "why": "shear strain rate for [8.24951, 0.634021] (the system Weissenberg number (Wikidata Q1753014))" + }, + { + "q": "Could you work out the shear strain rate given relaxation time = 0.318734, Weissenberg number = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "Hz", + "why": "shear strain rate for [0.318734, 9.566071] (the system Weissenberg number (Wikidata Q1753014))" + }, + { + "q": "I need the shear strain rate for a relaxation time of 0.318734 and a Weissenberg number of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "Hz", + "why": "shear strain rate for [0.318734, 9.566071] (the system Weissenberg number (Wikidata Q1753014))" + }, + { + "q": "If the relaxation time is 0.671697 and the Weissenberg number is 14.2496, what is the shear strain rate?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "Hz", + "why": "shear strain rate for [0.671697, 14.249569] (the system Weissenberg number (Wikidata Q1753014))" + }, + { + "q": "Could you work out the shear strain rate given relaxation time = 0.671697, Weissenberg number = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "Hz", + "why": "shear strain rate for [0.671697, 14.249569] (the system Weissenberg number (Wikidata Q1753014))" + }, + { + "q": "I need the shear strain rate for a relaxation time of 21.7931 and a Weissenberg number of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "Hz", + "why": "shear strain rate for [21.793126, 29.510609] (the system Weissenberg number (Wikidata Q1753014))" + }, + { + "q": "If the relaxation time is 21.7931 and the Weissenberg number is 29.5106, what is the shear strain rate?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "Hz", + "why": "shear strain rate for [21.793126, 29.510609] (the system Weissenberg number (Wikidata Q1753014))" + }, + { + "q": "Could you work out the shear strain rate given relaxation time = 153.15, Weissenberg number = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "Hz", + "why": "shear strain rate for [153.149965, 18.595315] (the system Weissenberg number (Wikidata Q1753014))" + }, + { + "q": "I need the shear strain rate for a relaxation time of 153.15 and a Weissenberg number of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "Hz", + "why": "shear strain rate for [153.149965, 18.595315] (the system Weissenberg number (Wikidata Q1753014))" + }, + { + "q": "If the relaxation time is 8.50728, what is the shear strain rate?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Weissenberg number is not given" + }, + { + "q": "Could you work out the shear strain rate given relaxation time = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Weissenberg number is not given" + }, + { + "q": "Do you know the shear strain rate of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the shear strain rate of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the shear strain rate given relaxation time = -0.318734, Weissenberg number = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative relaxation time: no example had one" + }, + { + "q": "What is a typical shear strain rate?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-shear-strain-rate/program.py b/root/organs10/physics/specialists/solved-shear-strain-rate/program.py new file mode 100644 index 0000000000000000000000000000000000000000..acbe4d8c6353ce8eb6386181521a95d4e8e065f2 --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-strain-rate/program.py @@ -0,0 +1,7 @@ +"""shear strain rate from relaxation time, Weissenberg number (Weissenberg number (Wikidata Q1753014)) +The maths organ (systems.py): solved from the system q2 = q0*q1; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(relaxation_time, weissenberg_number): + return 1.0*weissenberg_number/relaxation_time diff --git a/root/organs10/physics/specialists/solved-shear-strain-rate/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-shear-strain-rate/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-strain-rate/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-shear-strain/organ_formula_v2.py b/root/organs10/physics/specialists/solved-shear-strain/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-strain/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-shear-strain/panel.json b/root/organs10/physics/specialists/solved-shear-strain/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..00e83268be66108f66d923504f2a6527feb48bed --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-strain/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the shear strain for parallel displacement 107.961 and thickness 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [107.960666, 59.4105] (the system shear strain (Wikidata Q7561704))" + }, + { + "q": "Tell me the shear strain with parallel displacement of 107.961 and thickness of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [107.960666, 59.4105] (the system shear strain (Wikidata Q7561704))" + }, + { + "q": "What is the shear strain for parallel displacement 169.527 and thickness 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [169.526958, 280.773702] (the system shear strain (Wikidata Q7561704))" + }, + { + "q": "Tell me the shear strain with parallel displacement of 169.527 and thickness of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [169.526958, 280.773702] (the system shear strain (Wikidata Q7561704))" + }, + { + "q": "What is the shear strain for parallel displacement 1.91195 and thickness 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [1.911952, 82.240072] (the system shear strain (Wikidata Q7561704))" + }, + { + "q": "Tell me the shear strain with parallel displacement of 1.91195 and thickness of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [1.911952, 82.240072] (the system shear strain (Wikidata Q7561704))" + }, + { + "q": "What is the shear strain for parallel displacement 1.64379 and thickness 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [1.643795, 252.709024] (the system shear strain (Wikidata Q7561704))" + }, + { + "q": "Tell me the shear strain with parallel displacement of 1.64379 and thickness of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [1.643795, 252.709024] (the system shear strain (Wikidata Q7561704))" + }, + { + "q": "What is the shear strain for parallel displacement 18.3169 and thickness 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [18.316867, 6.838598] (the system shear strain (Wikidata Q7561704))" + }, + { + "q": "Tell me the shear strain with parallel displacement of 18.3169 and thickness of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [18.316867, 6.838598] (the system shear strain (Wikidata Q7561704))" + }, + { + "q": "What is the shear strain for parallel displacement 156.502 and thickness 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [156.50181, 185.886273] (the system shear strain (Wikidata Q7561704))" + }, + { + "q": "Tell me the shear strain with parallel displacement of 156.502 and thickness of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [156.50181, 185.886273] (the system shear strain (Wikidata Q7561704))" + }, + { + "q": "What is the shear strain for parallel displacement 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the thickness is not given" + }, + { + "q": "Tell me the shear strain with parallel displacement of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the thickness is not given" + }, + { + "q": "What is the shear strain of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the shear strain of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the shear strain with parallel displacement of -1.91195 and thickness of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative parallel displacement: no example had one" + }, + { + "q": "Which shear strain is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the parallel displacement is 8.50728 and the thickness is 17.7888, what is the shear strain?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [8.507283, 17.788778] (the system shear strain (Wikidata Q7561704))" + }, + { + "q": "Could you work out the shear strain given parallel displacement = 8.50728, thickness = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [8.507283, 17.788778] (the system shear strain (Wikidata Q7561704))" + }, + { + "q": "I need the shear strain for a parallel displacement of 8.24951 and a thickness of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [8.24951, 0.634021] (the system shear strain (Wikidata Q7561704))" + }, + { + "q": "If the parallel displacement is 8.24951 and the thickness is 0.634021, what is the shear strain?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [8.24951, 0.634021] (the system shear strain (Wikidata Q7561704))" + }, + { + "q": "Could you work out the shear strain given parallel displacement = 0.318734, thickness = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [0.318734, 9.566071] (the system shear strain (Wikidata Q7561704))" + }, + { + "q": "I need the shear strain for a parallel displacement of 0.318734 and a thickness of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [0.318734, 9.566071] (the system shear strain (Wikidata Q7561704))" + }, + { + "q": "If the parallel displacement is 0.671697 and the thickness is 14.2496, what is the shear strain?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [0.671697, 14.249569] (the system shear strain (Wikidata Q7561704))" + }, + { + "q": "Could you work out the shear strain given parallel displacement = 0.671697, thickness = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [0.671697, 14.249569] (the system shear strain (Wikidata Q7561704))" + }, + { + "q": "I need the shear strain for a parallel displacement of 21.7931 and a thickness of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [21.793126, 29.510609] (the system shear strain (Wikidata Q7561704))" + }, + { + "q": "If the parallel displacement is 21.7931 and the thickness is 29.5106, what is the shear strain?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [21.793126, 29.510609] (the system shear strain (Wikidata Q7561704))" + }, + { + "q": "Could you work out the shear strain given parallel displacement = 153.15, thickness = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [153.149965, 18.595315] (the system shear strain (Wikidata Q7561704))" + }, + { + "q": "I need the shear strain for a parallel displacement of 153.15 and a thickness of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "shear strain for [153.149965, 18.595315] (the system shear strain (Wikidata Q7561704))" + }, + { + "q": "If the parallel displacement is 8.50728, what is the shear strain?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the thickness is not given" + }, + { + "q": "Could you work out the shear strain given parallel displacement = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the thickness is not given" + }, + { + "q": "Do you know the shear strain of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the shear strain of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the shear strain given parallel displacement = -0.318734, thickness = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative parallel displacement: no example had one" + }, + { + "q": "What is a typical shear strain?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-shear-strain/program.py b/root/organs10/physics/specialists/solved-shear-strain/program.py new file mode 100644 index 0000000000000000000000000000000000000000..1c3a6fa4f731cac6754e4068edc327fd19bdb675 --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-strain/program.py @@ -0,0 +1,7 @@ +"""shear strain from parallel displacement, thickness (shear strain (Wikidata Q7561704)) +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(parallel_displacement, thickness): + return 1.0*parallel_displacement/thickness diff --git a/root/organs10/physics/specialists/solved-shear-strain/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-shear-strain/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-strain/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-shear-stress-at-flow-limit/organ_formula_v2.py b/root/organs10/physics/specialists/solved-shear-stress-at-flow-limit/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-stress-at-flow-limit/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-shear-stress-at-flow-limit/panel.json b/root/organs10/physics/specialists/solved-shear-stress-at-flow-limit/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..3df8bbb99bd5b5db8a482843bdbfb98014235703 --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-stress-at-flow-limit/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the shear stress at flow limit for Hedström number 107.961, characteristic diameter 59.4105, mass density 5.77717 and dynamic viscosity 1.89126?", + "a": "0.018937598127323402", + "valid": true, + "kind": "compute", + "expect": 0.018937598127323402, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress at flow limit for [107.960666, 59.4105, 5.777165, 1.891256] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "Tell me the shear stress at flow limit with Hedström number of 107.961, characteristic diameter of 59.4105, mass density of 5.77717 and dynamic viscosity of 1.89126.", + "a": "0.018937598127323402", + "valid": true, + "kind": "compute", + "expect": 0.018937598127323402, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress at flow limit for [107.960666, 59.4105, 5.777165, 1.891256] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "What is the shear stress at flow limit for Hedström number 2.69428, characteristic diameter 48.921, mass density 157.223 and dynamic viscosity 35.6412?", + "a": "0.009095800911943307", + "valid": true, + "kind": "compute", + "expect": 0.009095800911943307, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress at flow limit for [2.69428, 48.920991, 157.22256, 35.641157] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "Tell me the shear stress at flow limit with Hedström number of 2.69428, characteristic diameter of 48.921, mass density of 157.223 and dynamic viscosity of 35.6412.", + "a": "0.009095800911943307", + "valid": true, + "kind": "compute", + "expect": 0.009095800911943307, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress at flow limit for [2.69428, 48.920991, 157.22256, 35.641157] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "What is the shear stress at flow limit for Hedström number 0.318734, characteristic diameter 9.56607, mass density 126.709 and dynamic viscosity 1.70503?", + "a": "7.991313970700597e-05", + "valid": true, + "kind": "compute", + "expect": 7.991313970700597e-05, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress at flow limit for [0.318734, 9.566071, 126.709252, 1.705032] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "Tell me the shear stress at flow limit with Hedström number of 0.318734, characteristic diameter of 9.56607, mass density of 126.709 and dynamic viscosity of 1.70503.", + "a": "7.991313970700597e-05", + "valid": true, + "kind": "compute", + "expect": 7.991313970700597e-05, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress at flow limit for [0.318734, 9.566071, 126.709252, 1.705032] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "What is the shear stress at flow limit for Hedström number 87.781, characteristic diameter 13.2084, mass density 246.329 and dynamic viscosity 20.3966?", + "a": "0.8497615532327472", + "valid": true, + "kind": "compute", + "expect": 0.8497615532327472, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress at flow limit for [87.781013, 13.208419, 246.329053, 20.396558] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "Tell me the shear stress at flow limit with Hedström number of 87.781, characteristic diameter of 13.2084, mass density of 246.329 and dynamic viscosity of 20.3966.", + "a": "0.8497615532327472", + "valid": true, + "kind": "compute", + "expect": 0.8497615532327472, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress at flow limit for [87.781013, 13.208419, 246.329053, 20.396558] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "What is the shear stress at flow limit for Hedström number 156.502, characteristic diameter 185.886, mass density 13.2373 and dynamic viscosity 4.71941?", + "a": "0.007620812064797249", + "valid": true, + "kind": "compute", + "expect": 0.007620812064797249, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress at flow limit for [156.50181, 185.886273, 13.237311, 4.719415] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "Tell me the shear stress at flow limit with Hedström number of 156.502, characteristic diameter of 185.886, mass density of 13.2373 and dynamic viscosity of 4.71941.", + "a": "0.007620812064797249", + "valid": true, + "kind": "compute", + "expect": 0.007620812064797249, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress at flow limit for [156.50181, 185.886273, 13.237311, 4.719415] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "What is the shear stress at flow limit for Hedström number 24.5721, characteristic diameter 108.451, mass density 1.69476 and dynamic viscosity 49.4843?", + "a": "3.018596233171603", + "valid": true, + "kind": "compute", + "expect": 3.018596233171603, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress at flow limit for [24.572086, 108.450792, 1.694755, 49.484326] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "Tell me the shear stress at flow limit with Hedström number of 24.5721, characteristic diameter of 108.451, mass density of 1.69476 and dynamic viscosity of 49.4843.", + "a": "3.018596233171603", + "valid": true, + "kind": "compute", + "expect": 3.018596233171603, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress at flow limit for [24.572086, 108.450792, 1.694755, 49.484326] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "What is the shear stress at flow limit for Hedström number 107.961, characteristic diameter 59.4105 and mass density 5.77717?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the dynamic viscosity is not given" + }, + { + "q": "Tell me the shear stress at flow limit with Hedström number of 2.69428, characteristic diameter of 48.921 and mass density of 157.223.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the dynamic viscosity is not given" + }, + { + "q": "What is the shear stress at flow limit 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 shear stress at flow limit 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 shear stress at flow limit with Hedström number of -0.318734, characteristic diameter of 9.56607, mass density of 126.709 and dynamic viscosity of 1.70503.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Hedström number: no example had one" + }, + { + "q": "Which shear stress at flow limit 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 Hedström number is 167.625, characteristic diameter is 10.3291, mass density is 2.21571 and the dynamic viscosity is 58.5346, what is the shear stress at flow limit?", + "a": "2429.568859278261", + "valid": true, + "kind": "compute", + "expect": 2429.568859278261, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress at flow limit for [167.624948, 10.329054, 2.215713, 58.534594] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "Could you work out the shear stress at flow limit given Hedström number = 167.625, characteristic diameter = 10.3291, mass density = 2.21571, dynamic viscosity = 58.5346?", + "a": "2429.568859278261", + "valid": true, + "kind": "compute", + "expect": 2429.568859278261, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress at flow limit for [167.624948, 10.329054, 2.215713, 58.534594] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "I need the shear stress at flow limit for a Hedström number of 8.53158, a characteristic diameter of 124.718, a mass density of 1.91195 and a dynamic viscosity of 82.2401, please.", + "a": "1.940259390104639", + "valid": true, + "kind": "compute", + "expect": 1.940259390104639, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress at flow limit for [8.531577, 124.718185, 1.911952, 82.240072] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "If the Hedström number is 8.53158, characteristic diameter is 124.718, mass density is 1.91195 and the dynamic viscosity is 82.2401, what is the shear stress at flow limit?", + "a": "1.940259390104639", + "valid": true, + "kind": "compute", + "expect": 1.940259390104639, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress at flow limit for [8.531577, 124.718185, 1.911952, 82.240072] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "Could you work out the shear stress at flow limit given Hedström number = 81.1967, characteristic diameter = 6.98086, mass density = 0.551236, dynamic viscosity = 2.88512?", + "a": "25.160029825721363", + "valid": true, + "kind": "compute", + "expect": 25.160029825721363, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress at flow limit for [81.196705, 6.980857, 0.551236, 2.88512] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "I need the shear stress at flow limit for a Hedström number of 81.1967, a characteristic diameter of 6.98086, a mass density of 0.551236 and a dynamic viscosity of 2.88512, please.", + "a": "25.160029825721363", + "valid": true, + "kind": "compute", + "expect": 25.160029825721363, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress at flow limit for [81.196705, 6.980857, 0.551236, 2.88512] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "If the Hedström number is 1.16997, characteristic diameter is 1.14865, mass density is 21.7931 and the dynamic viscosity is 29.5106, what is the shear stress at flow limit?", + "a": "35.435095737378084", + "valid": true, + "kind": "compute", + "expect": 35.435095737378084, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress at flow limit for [1.169968, 1.148652, 21.793126, 29.510609] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "Could you work out the shear stress at flow limit given Hedström number = 1.16997, characteristic diameter = 1.14865, mass density = 21.7931, dynamic viscosity = 29.5106?", + "a": "35.435095737378084", + "valid": true, + "kind": "compute", + "expect": 35.435095737378084, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress at flow limit for [1.169968, 1.148652, 21.793126, 29.510609] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "I need the shear stress at flow limit for a Hedström number of 223.509, a characteristic diameter of 17.3414, a mass density of 7.10705 and a dynamic viscosity of 30.2507, please.", + "a": "95.69902366455786", + "valid": true, + "kind": "compute", + "expect": 95.69902366455786, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress at flow limit for [223.508796, 17.341435, 7.107049, 30.250749] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "If the Hedström number is 223.509, characteristic diameter is 17.3414, mass density is 7.10705 and the dynamic viscosity is 30.2507, what is the shear stress at flow limit?", + "a": "95.69902366455786", + "valid": true, + "kind": "compute", + "expect": 95.69902366455786, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress at flow limit for [223.508796, 17.341435, 7.107049, 30.250749] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "Could you work out the shear stress at flow limit given Hedström number = 0.869111, characteristic diameter = 39.1753, mass density = 0.432217, dynamic viscosity = 16.6571?", + "a": "0.36353450865728587", + "valid": true, + "kind": "compute", + "expect": 0.36353450865728587, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress at flow limit for [0.869111, 39.175263, 0.432217, 16.657051] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "I need the shear stress at flow limit for a Hedström number of 0.869111, a characteristic diameter of 39.1753, a mass density of 0.432217 and a dynamic viscosity of 16.6571, please.", + "a": "0.36353450865728587", + "valid": true, + "kind": "compute", + "expect": 0.36353450865728587, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress at flow limit for [0.869111, 39.175263, 0.432217, 16.657051] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "If the Hedström number is 167.625, characteristic diameter is 10.3291 and the mass density is 2.21571, what is the shear stress at flow limit?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the dynamic viscosity is not given" + }, + { + "q": "Could you work out the shear stress at flow limit given Hedström number = 8.53158, characteristic diameter = 124.718, mass density = 1.91195?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the dynamic viscosity is not given" + }, + { + "q": "Do you know the shear stress at flow limit 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 shear stress at flow limit 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 shear stress at flow limit given Hedström number = -81.1967, characteristic diameter = 6.98086, mass density = 0.551236, dynamic viscosity = 2.88512?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Hedström number: no example had one" + }, + { + "q": "What is a typical shear stress at flow limit?", + "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-shear-stress-at-flow-limit/program.py b/root/organs10/physics/specialists/solved-shear-stress-at-flow-limit/program.py new file mode 100644 index 0000000000000000000000000000000000000000..7d7259adaecb7c1a1cafc490727d7d0f01380182 --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-stress-at-flow-limit/program.py @@ -0,0 +1,7 @@ +"""shear stress at flow limit from Hedström number, characteristic diameter, mass density, dynamic viscosity (Hedström number (Wikidata Q3343027)) +The maths organ (systems.py): solved from the system q1 = ((q0*q2 **(2)*q3)/(q4 **(2))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(hedstr_m_number, characteristic_diameter, mass_density, dynamic_viscosity): + return 1.0*dynamic_viscosity**2*hedstr_m_number/(characteristic_diameter**2*mass_density) diff --git a/root/organs10/physics/specialists/solved-shear-stress-at-flow-limit/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-shear-stress-at-flow-limit/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-stress-at-flow-limit/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-shear-stress/organ_formula_v2.py b/root/organs10/physics/specialists/solved-shear-stress/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-stress/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-shear-stress/panel.json b/root/organs10/physics/specialists/solved-shear-stress/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..4e017f1f37059a12c86bd34ed1eddf0366dd46a9 --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-stress/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the shear stress for shear modulus 107.961 and shear strain 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress for [107.960666, 59.4105] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Tell me the shear stress with shear modulus of 107.961 and shear strain of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress for [107.960666, 59.4105] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "What is the shear stress for shear modulus 169.527 and shear strain 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress for [169.526958, 280.773702] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Tell me the shear stress with shear modulus of 169.527 and shear strain of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress for [169.526958, 280.773702] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "What is the shear stress for shear modulus 1.91195 and shear strain 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress for [1.911952, 82.240072] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Tell me the shear stress with shear modulus of 1.91195 and shear strain of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress for [1.911952, 82.240072] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "What is the shear stress for shear modulus 1.64379 and shear strain 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress for [1.643795, 252.709024] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Tell me the shear stress with shear modulus of 1.64379 and shear strain of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress for [1.643795, 252.709024] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "What is the shear stress for shear modulus 18.3169 and shear strain 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress for [18.316867, 6.838598] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Tell me the shear stress with shear modulus of 18.3169 and shear strain of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress for [18.316867, 6.838598] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "What is the shear stress for shear modulus 156.502 and shear strain 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress for [156.50181, 185.886273] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Tell me the shear stress with shear modulus of 156.502 and shear strain of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress for [156.50181, 185.886273] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "What is the shear stress for shear modulus 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the shear strain is not given" + }, + { + "q": "Tell me the shear stress with shear modulus of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the shear strain is not given" + }, + { + "q": "What is the shear stress 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 shear stress 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 shear stress with shear modulus of -1.91195 and shear strain of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative shear modulus: no example had one" + }, + { + "q": "Which shear stress is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the shear modulus is 8.50728 and the shear strain is 17.7888, what is the shear stress?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress for [8.507283, 17.788778] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Could you work out the shear stress given shear modulus = 8.50728, shear strain = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress for [8.507283, 17.788778] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "I need the shear stress for a shear modulus of 8.24951 and a shear strain of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress for [8.24951, 0.634021] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "If the shear modulus is 8.24951 and the shear strain is 0.634021, what is the shear stress?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress for [8.24951, 0.634021] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Could you work out the shear stress given shear modulus = 0.318734, shear strain = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress for [0.318734, 9.566071] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "I need the shear stress for a shear modulus of 0.318734 and a shear strain of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress for [0.318734, 9.566071] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "If the shear modulus is 0.671697 and the shear strain is 14.2496, what is the shear stress?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress for [0.671697, 14.249569] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Could you work out the shear stress given shear modulus = 0.671697, shear strain = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress for [0.671697, 14.249569] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "I need the shear stress for a shear modulus of 21.7931 and a shear strain of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress for [21.793126, 29.510609] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "If the shear modulus is 21.7931 and the shear strain is 29.5106, what is the shear stress?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress for [21.793126, 29.510609] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "Could you work out the shear stress given shear modulus = 153.15, shear strain = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress for [153.149965, 18.595315] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "I need the shear stress for a shear modulus of 153.15 and a shear strain of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "Pa", + "why": "shear stress for [153.149965, 18.595315] (the system shear modulus (Wikidata Q461466))" + }, + { + "q": "If the shear modulus is 8.50728, what is the shear stress?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the shear strain is not given" + }, + { + "q": "Could you work out the shear stress given shear modulus = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the shear strain is not given" + }, + { + "q": "Do you know the shear stress 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 shear stress 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 shear stress given shear modulus = -0.318734, shear strain = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative shear modulus: no example had one" + }, + { + "q": "What is a typical shear stress?", + "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-shear-stress/program.py b/root/organs10/physics/specialists/solved-shear-stress/program.py new file mode 100644 index 0000000000000000000000000000000000000000..1aa7d25bc8629a3a0482ad8fc9f595417839b77f --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-stress/program.py @@ -0,0 +1,7 @@ +"""shear stress from shear modulus, shear strain (shear modulus (Wikidata Q461466)) +The maths organ (systems.py): solved from the system q0 = q1 / q2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(shear_modulus, shear_strain): + return 1.0*shear_modulus*shear_strain diff --git a/root/organs10/physics/specialists/solved-shear-stress/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-shear-stress/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-shear-stress/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-signal-bandwidth/organ_formula_v2.py b/root/organs10/physics/specialists/solved-signal-bandwidth/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-signal-bandwidth/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-signal-bandwidth/panel.json b/root/organs10/physics/specialists/solved-signal-bandwidth/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..18e71aa06b00b6bc1cee1fe5dcb01d7720e62403 --- /dev/null +++ b/root/organs10/physics/specialists/solved-signal-bandwidth/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the signal bandwidth for Q10470221 107.961?", + "a": "215.92133177992403", + "valid": true, + "kind": "compute", + "expect": 215.92133177992403, + "dtype": "quantity", + "unit": "Hz", + "why": "signal bandwidth for [107.960666] (the system None (Wikidata Q10470221))" + }, + { + "q": "Tell me the signal bandwidth with Q10470221 of 107.961.", + "a": "215.92133177992403", + "valid": true, + "kind": "compute", + "expect": 215.92133177992403, + "dtype": "quantity", + "unit": "Hz", + "why": "signal bandwidth for [107.960666] (the system None (Wikidata Q10470221))" + }, + { + "q": "What is the signal bandwidth for Q10470221 22.652?", + "a": "45.304045593711585", + "valid": true, + "kind": "compute", + "expect": 45.304045593711585, + "dtype": "quantity", + "unit": "Hz", + "why": "signal bandwidth for [22.652023] (the system None (Wikidata Q10470221))" + }, + { + "q": "Tell me the signal bandwidth with Q10470221 of 22.652.", + "a": "45.304045593711585", + "valid": true, + "kind": "compute", + "expect": 45.304045593711585, + "dtype": "quantity", + "unit": "Hz", + "why": "signal bandwidth for [22.652023] (the system None (Wikidata Q10470221))" + }, + { + "q": "What is the signal bandwidth for Q10470221 173.394?", + "a": "346.787276810605", + "valid": true, + "kind": "compute", + "expect": 346.787276810605, + "dtype": "quantity", + "unit": "Hz", + "why": "signal bandwidth for [173.393638] (the system None (Wikidata Q10470221))" + }, + { + "q": "Tell me the signal bandwidth with Q10470221 of 173.394.", + "a": "346.787276810605", + "valid": true, + "kind": "compute", + "expect": 346.787276810605, + "dtype": "quantity", + "unit": "Hz", + "why": "signal bandwidth for [173.393638] (the system None (Wikidata Q10470221))" + }, + { + "q": "What is the signal bandwidth for Q10470221 126.709?", + "a": "253.41850360036474", + "valid": true, + "kind": "compute", + "expect": 253.41850360036474, + "dtype": "quantity", + "unit": "Hz", + "why": "signal bandwidth for [126.709252] (the system None (Wikidata Q10470221))" + }, + { + "q": "Tell me the signal bandwidth with Q10470221 of 126.709.", + "a": "253.41850360036474", + "valid": true, + "kind": "compute", + "expect": 253.41850360036474, + "dtype": "quantity", + "unit": "Hz", + "why": "signal bandwidth for [126.709252] (the system None (Wikidata Q10470221))" + }, + { + "q": "What is the signal bandwidth for Q10470221 0.671697?", + "a": "1.343394399588387", + "valid": true, + "kind": "compute", + "expect": 1.343394399588387, + "dtype": "quantity", + "unit": "Hz", + "why": "signal bandwidth for [0.671697] (the system None (Wikidata Q10470221))" + }, + { + "q": "Tell me the signal bandwidth with Q10470221 of 0.671697.", + "a": "1.343394399588387", + "valid": true, + "kind": "compute", + "expect": 1.343394399588387, + "dtype": "quantity", + "unit": "Hz", + "why": "signal bandwidth for [0.671697] (the system None (Wikidata Q10470221))" + }, + { + "q": "What is the signal bandwidth for Q10470221 1.16997?", + "a": "2.3399360119558716", + "valid": true, + "kind": "compute", + "expect": 2.3399360119558716, + "dtype": "quantity", + "unit": "Hz", + "why": "signal bandwidth for [1.169968] (the system None (Wikidata Q10470221))" + }, + { + "q": "Tell me the signal bandwidth with Q10470221 of 1.16997.", + "a": "2.3399360119558716", + "valid": true, + "kind": "compute", + "expect": 2.3399360119558716, + "dtype": "quantity", + "unit": "Hz", + "why": "signal bandwidth for [1.169968] (the system None (Wikidata Q10470221))" + }, + { + "q": "What is the signal bandwidth?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Q10470221 is not given" + }, + { + "q": "Could you work out the signal bandwidth, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Q10470221 is not given" + }, + { + "q": "What is the signal bandwidth 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 signal bandwidth 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 signal bandwidth with Q10470221 of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Q10470221: no example had one" + }, + { + "q": "Which signal bandwidth 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 Q10470221 is 2.57003, what is the signal bandwidth?", + "a": "5.140059826607287", + "valid": true, + "kind": "compute", + "expect": 5.140059826607287, + "dtype": "quantity", + "unit": "Hz", + "why": "signal bandwidth for [2.57003] (the system None (Wikidata Q10470221))" + }, + { + "q": "Could you work out the signal bandwidth given Q10470221 = 2.57003?", + "a": "5.140059826607287", + "valid": true, + "kind": "compute", + "expect": 5.140059826607287, + "dtype": "quantity", + "unit": "Hz", + "why": "signal bandwidth for [2.57003] (the system None (Wikidata Q10470221))" + }, + { + "q": "I need the signal bandwidth for a Q10470221 of 48.921, please.", + "a": "97.84198172002367", + "valid": true, + "kind": "compute", + "expect": 97.84198172002367, + "dtype": "quantity", + "unit": "Hz", + "why": "signal bandwidth for [48.920991] (the system None (Wikidata Q10470221))" + }, + { + "q": "If the Q10470221 is 48.921, what is the signal bandwidth?", + "a": "97.84198172002367", + "valid": true, + "kind": "compute", + "expect": 97.84198172002367, + "dtype": "quantity", + "unit": "Hz", + "why": "signal bandwidth for [48.920991] (the system None (Wikidata Q10470221))" + }, + { + "q": "Could you work out the signal bandwidth given Q10470221 = 0.348438?", + "a": "0.696875339260061", + "valid": true, + "kind": "compute", + "expect": 0.696875339260061, + "dtype": "quantity", + "unit": "Hz", + "why": "signal bandwidth for [0.348438] (the system None (Wikidata Q10470221))" + }, + { + "q": "I need the signal bandwidth for a Q10470221 of 0.348438, please.", + "a": "0.696875339260061", + "valid": true, + "kind": "compute", + "expect": 0.696875339260061, + "dtype": "quantity", + "unit": "Hz", + "why": "signal bandwidth for [0.348438] (the system None (Wikidata Q10470221))" + }, + { + "q": "If the Q10470221 is 252.709, what is the signal bandwidth?", + "a": "505.41804849905503", + "valid": true, + "kind": "compute", + "expect": 505.41804849905503, + "dtype": "quantity", + "unit": "Hz", + "why": "signal bandwidth for [252.709024] (the system None (Wikidata Q10470221))" + }, + { + "q": "Could you work out the signal bandwidth given Q10470221 = 252.709?", + "a": "505.41804849905503", + "valid": true, + "kind": "compute", + "expect": 505.41804849905503, + "dtype": "quantity", + "unit": "Hz", + "why": "signal bandwidth for [252.709024] (the system None (Wikidata Q10470221))" + }, + { + "q": "I need the signal bandwidth for a Q10470221 of 20.3966, please.", + "a": "40.79311592392688", + "valid": true, + "kind": "compute", + "expect": 40.79311592392688, + "dtype": "quantity", + "unit": "Hz", + "why": "signal bandwidth for [20.396558] (the system None (Wikidata Q10470221))" + }, + { + "q": "If the Q10470221 is 20.3966, what is the signal bandwidth?", + "a": "40.79311592392688", + "valid": true, + "kind": "compute", + "expect": 40.79311592392688, + "dtype": "quantity", + "unit": "Hz", + "why": "signal bandwidth for [20.396558] (the system None (Wikidata Q10470221))" + }, + { + "q": "Could you work out the signal bandwidth given Q10470221 = 134.993?", + "a": "269.985915161476", + "valid": true, + "kind": "compute", + "expect": 269.985915161476, + "dtype": "quantity", + "unit": "Hz", + "why": "signal bandwidth for [134.992958] (the system None (Wikidata Q10470221))" + }, + { + "q": "I need the signal bandwidth for a Q10470221 of 134.993, please.", + "a": "269.985915161476", + "valid": true, + "kind": "compute", + "expect": 269.985915161476, + "dtype": "quantity", + "unit": "Hz", + "why": "signal bandwidth for [134.992958] (the system None (Wikidata Q10470221))" + }, + { + "q": "I need the signal bandwidth, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Q10470221 is not given" + }, + { + "q": "Can you tell me the signal bandwidth?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Q10470221 is not given" + }, + { + "q": "Do you know the signal bandwidth 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 signal bandwidth 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 signal bandwidth given Q10470221 = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Q10470221: no example had one" + }, + { + "q": "What is a typical signal bandwidth?", + "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-signal-bandwidth/program.py b/root/organs10/physics/specialists/solved-signal-bandwidth/program.py new file mode 100644 index 0000000000000000000000000000000000000000..50238423a3fd2b4d0ebaf971d76a0a7743f17e71 --- /dev/null +++ b/root/organs10/physics/specialists/solved-signal-bandwidth/program.py @@ -0,0 +1,7 @@ +"""signal bandwidth from Q10470221 (None (Wikidata Q10470221)) +The maths organ (systems.py): solved from the system q0 = ((q1)/(2)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(q10470221): + return 2.0*q10470221 diff --git a/root/organs10/physics/specialists/solved-signal-bandwidth/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-signal-bandwidth/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-signal-bandwidth/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-signal-energy-per-binary-digi/organ_formula_v2.py b/root/organs10/physics/specialists/solved-signal-energy-per-binary-digi/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-signal-energy-per-binary-digi/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-signal-energy-per-binary-digi/panel.json b/root/organs10/physics/specialists/solved-signal-energy-per-binary-digi/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..0078c1203d133989ebc89f0c6188928bbface037 --- /dev/null +++ b/root/organs10/physics/specialists/solved-signal-energy-per-binary-digi/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the signal energy per binary digit for carrier power 107.961 and period of binary digits 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "J", + "why": "signal energy per binary digit for [107.960666, 59.4105] (the system signal energy per binary digit (Wikidata Q106344792))" + }, + { + "q": "Tell me the signal energy per binary digit with carrier power of 107.961 and period of binary digits of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "J", + "why": "signal energy per binary digit for [107.960666, 59.4105] (the system signal energy per binary digit (Wikidata Q106344792))" + }, + { + "q": "What is the signal energy per binary digit for carrier power 169.527 and period of binary digits 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "J", + "why": "signal energy per binary digit for [169.526958, 280.773702] (the system signal energy per binary digit (Wikidata Q106344792))" + }, + { + "q": "Tell me the signal energy per binary digit with carrier power of 169.527 and period of binary digits of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "J", + "why": "signal energy per binary digit for [169.526958, 280.773702] (the system signal energy per binary digit (Wikidata Q106344792))" + }, + { + "q": "What is the signal energy per binary digit for carrier power 1.91195 and period of binary digits 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "J", + "why": "signal energy per binary digit for [1.911952, 82.240072] (the system signal energy per binary digit (Wikidata Q106344792))" + }, + { + "q": "Tell me the signal energy per binary digit with carrier power of 1.91195 and period of binary digits of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "J", + "why": "signal energy per binary digit for [1.911952, 82.240072] (the system signal energy per binary digit (Wikidata Q106344792))" + }, + { + "q": "What is the signal energy per binary digit for carrier power 1.64379 and period of binary digits 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "J", + "why": "signal energy per binary digit for [1.643795, 252.709024] (the system signal energy per binary digit (Wikidata Q106344792))" + }, + { + "q": "Tell me the signal energy per binary digit with carrier power of 1.64379 and period of binary digits of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "J", + "why": "signal energy per binary digit for [1.643795, 252.709024] (the system signal energy per binary digit (Wikidata Q106344792))" + }, + { + "q": "What is the signal energy per binary digit for carrier power 18.3169 and period of binary digits 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "J", + "why": "signal energy per binary digit for [18.316867, 6.838598] (the system signal energy per binary digit (Wikidata Q106344792))" + }, + { + "q": "Tell me the signal energy per binary digit with carrier power of 18.3169 and period of binary digits of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "J", + "why": "signal energy per binary digit for [18.316867, 6.838598] (the system signal energy per binary digit (Wikidata Q106344792))" + }, + { + "q": "What is the signal energy per binary digit for carrier power 156.502 and period of binary digits 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "J", + "why": "signal energy per binary digit for [156.50181, 185.886273] (the system signal energy per binary digit (Wikidata Q106344792))" + }, + { + "q": "Tell me the signal energy per binary digit with carrier power of 156.502 and period of binary digits of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "J", + "why": "signal energy per binary digit for [156.50181, 185.886273] (the system signal energy per binary digit (Wikidata Q106344792))" + }, + { + "q": "What is the signal energy per binary digit for carrier power 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the period of binary digits is not given" + }, + { + "q": "Tell me the signal energy per binary digit with carrier power of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the period of binary digits is not given" + }, + { + "q": "What is the signal energy per binary digit 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 signal energy per binary digit 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 signal energy per binary digit with carrier power of -1.91195 and period of binary digits of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative carrier power: no example had one" + }, + { + "q": "Which signal energy per binary digit 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 carrier power is 8.50728 and the period of binary digits is 17.7888, what is the signal energy per binary digit?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "J", + "why": "signal energy per binary digit for [8.507283, 17.788778] (the system signal energy per binary digit (Wikidata Q106344792))" + }, + { + "q": "Could you work out the signal energy per binary digit given carrier power = 8.50728, period of binary digits = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "J", + "why": "signal energy per binary digit for [8.507283, 17.788778] (the system signal energy per binary digit (Wikidata Q106344792))" + }, + { + "q": "I need the signal energy per binary digit for a carrier power of 8.24951 and a period of binary digits of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "J", + "why": "signal energy per binary digit for [8.24951, 0.634021] (the system signal energy per binary digit (Wikidata Q106344792))" + }, + { + "q": "If the carrier power is 8.24951 and the period of binary digits is 0.634021, what is the signal energy per binary digit?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "J", + "why": "signal energy per binary digit for [8.24951, 0.634021] (the system signal energy per binary digit (Wikidata Q106344792))" + }, + { + "q": "Could you work out the signal energy per binary digit given carrier power = 0.318734, period of binary digits = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "J", + "why": "signal energy per binary digit for [0.318734, 9.566071] (the system signal energy per binary digit (Wikidata Q106344792))" + }, + { + "q": "I need the signal energy per binary digit for a carrier power of 0.318734 and a period of binary digits of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "J", + "why": "signal energy per binary digit for [0.318734, 9.566071] (the system signal energy per binary digit (Wikidata Q106344792))" + }, + { + "q": "If the carrier power is 0.671697 and the period of binary digits is 14.2496, what is the signal energy per binary digit?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "J", + "why": "signal energy per binary digit for [0.671697, 14.249569] (the system signal energy per binary digit (Wikidata Q106344792))" + }, + { + "q": "Could you work out the signal energy per binary digit given carrier power = 0.671697, period of binary digits = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "J", + "why": "signal energy per binary digit for [0.671697, 14.249569] (the system signal energy per binary digit (Wikidata Q106344792))" + }, + { + "q": "I need the signal energy per binary digit for a carrier power of 21.7931 and a period of binary digits of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "J", + "why": "signal energy per binary digit for [21.793126, 29.510609] (the system signal energy per binary digit (Wikidata Q106344792))" + }, + { + "q": "If the carrier power is 21.7931 and the period of binary digits is 29.5106, what is the signal energy per binary digit?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "J", + "why": "signal energy per binary digit for [21.793126, 29.510609] (the system signal energy per binary digit (Wikidata Q106344792))" + }, + { + "q": "Could you work out the signal energy per binary digit given carrier power = 153.15, period of binary digits = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "J", + "why": "signal energy per binary digit for [153.149965, 18.595315] (the system signal energy per binary digit (Wikidata Q106344792))" + }, + { + "q": "I need the signal energy per binary digit for a carrier power of 153.15 and a period of binary digits of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "J", + "why": "signal energy per binary digit for [153.149965, 18.595315] (the system signal energy per binary digit (Wikidata Q106344792))" + }, + { + "q": "If the carrier power is 8.50728, what is the signal energy per binary digit?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the period of binary digits is not given" + }, + { + "q": "Could you work out the signal energy per binary digit given carrier power = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the period of binary digits is not given" + }, + { + "q": "Do you know the signal energy per binary digit 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 signal energy per binary digit 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 signal energy per binary digit given carrier power = -0.318734, period of binary digits = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative carrier power: no example had one" + }, + { + "q": "What is a typical signal energy per binary digit?", + "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-signal-energy-per-binary-digi/program.py b/root/organs10/physics/specialists/solved-signal-energy-per-binary-digi/program.py new file mode 100644 index 0000000000000000000000000000000000000000..fe0f57bce2ec9bdb17d72e96dacfed58ed5029ca --- /dev/null +++ b/root/organs10/physics/specialists/solved-signal-energy-per-binary-digi/program.py @@ -0,0 +1,7 @@ +"""signal energy per binary digit from carrier power, period of binary digits (signal energy per binary digit (Wikidata Q106344792)) +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(carrier_power, period_of_binary_digits): + return 1.0*carrier_power*period_of_binary_digits diff --git a/root/organs10/physics/specialists/solved-signal-energy-per-binary-digi/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-signal-energy-per-binary-digi/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-signal-energy-per-binary-digi/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-slowing-down-area/organ_formula_v2.py b/root/organs10/physics/specialists/solved-slowing-down-area/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-slowing-down-area/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-slowing-down-area/panel.json b/root/organs10/physics/specialists/solved-slowing-down-area/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..ae4f17daa784292031877d63c1c32846ce10b48a --- /dev/null +++ b/root/organs10/physics/specialists/solved-slowing-down-area/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the slowing-down area for migration area 107.961 and diffusion area 59.4105?", + "a": "48.55016569879068", + "valid": true, + "kind": "compute", + "expect": 48.55016569879068, + "dtype": "quantity", + "unit": "m^2", + "why": "slowing-down area for [107.960666, 59.4105] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "Tell me the slowing-down area with migration area of 107.961 and diffusion area of 59.4105.", + "a": "48.55016569879068", + "valid": true, + "kind": "compute", + "expect": 48.55016569879068, + "dtype": "quantity", + "unit": "m^2", + "why": "slowing-down area for [107.960666, 59.4105] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "What is the slowing-down area for migration area 157.223 and diffusion area 35.6412?", + "a": "121.5814030054458", + "valid": true, + "kind": "compute", + "expect": 121.5814030054458, + "dtype": "quantity", + "unit": "m^2", + "why": "slowing-down area for [157.22256, 35.641157] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "Tell me the slowing-down area with migration area of 157.223 and diffusion area of 35.6412.", + "a": "121.5814030054458", + "valid": true, + "kind": "compute", + "expect": 121.5814030054458, + "dtype": "quantity", + "unit": "m^2", + "why": "slowing-down area for [157.22256, 35.641157] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "What is the slowing-down area for migration area 126.709 and diffusion area 1.70503?", + "a": "125.00421942160824", + "valid": true, + "kind": "compute", + "expect": 125.00421942160824, + "dtype": "quantity", + "unit": "m^2", + "why": "slowing-down area for [126.709252, 1.705032] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "Tell me the slowing-down area with migration area of 126.709 and diffusion area of 1.70503.", + "a": "125.00421942160824", + "valid": true, + "kind": "compute", + "expect": 125.00421942160824, + "dtype": "quantity", + "unit": "m^2", + "why": "slowing-down area for [126.709252, 1.705032] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "What is the slowing-down area for migration area 41.6566 and diffusion area 13.8779?", + "a": "27.77877186905885", + "valid": true, + "kind": "compute", + "expect": 27.77877186905885, + "dtype": "quantity", + "unit": "m^2", + "why": "slowing-down area for [41.656639, 13.877867] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "Tell me the slowing-down area with migration area of 41.6566 and diffusion area of 13.8779.", + "a": "27.77877186905885", + "valid": true, + "kind": "compute", + "expect": 27.77877186905885, + "dtype": "quantity", + "unit": "m^2", + "why": "slowing-down area for [41.656639, 13.877867] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "What is the slowing-down area for migration area 8.50341 and diffusion area 0.58813?", + "a": "7.91527752848457", + "valid": true, + "kind": "compute", + "expect": 7.91527752848457, + "dtype": "quantity", + "unit": "m^2", + "why": "slowing-down area for [8.503407, 0.58813] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "Tell me the slowing-down area with migration area of 8.50341 and diffusion area of 0.58813.", + "a": "7.91527752848457", + "valid": true, + "kind": "compute", + "expect": 7.91527752848457, + "dtype": "quantity", + "unit": "m^2", + "why": "slowing-down area for [8.503407, 0.58813] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "What is the slowing-down area for migration area 223.509 and diffusion area 17.3414?", + "a": "206.1673613150397", + "valid": true, + "kind": "compute", + "expect": 206.1673613150397, + "dtype": "quantity", + "unit": "m^2", + "why": "slowing-down area for [223.508796, 17.341435] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "Tell me the slowing-down area with migration area of 223.509 and diffusion area of 17.3414.", + "a": "206.1673613150397", + "valid": true, + "kind": "compute", + "expect": 206.1673613150397, + "dtype": "quantity", + "unit": "m^2", + "why": "slowing-down area for [223.508796, 17.341435] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "What is the slowing-down area for migration area 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the diffusion area is not given" + }, + { + "q": "Tell me the slowing-down area with migration area of 157.223.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the diffusion area is not given" + }, + { + "q": "What is the slowing-down area of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the slowing-down area of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the slowing-down area with migration area of -126.709 and diffusion area of 1.70503.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative migration area: no example had one" + }, + { + "q": "Which slowing-down area is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the migration area is 167.625 and the diffusion area is 10.3291, what is the slowing-down area?", + "a": "157.29589332353083", + "valid": true, + "kind": "compute", + "expect": 157.29589332353083, + "dtype": "quantity", + "unit": "m^2", + "why": "slowing-down area for [167.624948, 10.329054] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "Could you work out the slowing-down area given migration area = 167.625, diffusion area = 10.3291?", + "a": "157.29589332353083", + "valid": true, + "kind": "compute", + "expect": 157.29589332353083, + "dtype": "quantity", + "unit": "m^2", + "why": "slowing-down area for [167.624948, 10.329054] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "I need the slowing-down area for a migration area of 13.999 and a diffusion area of 0.348438, please.", + "a": "13.650591275493905", + "valid": true, + "kind": "compute", + "expect": 13.650591275493905, + "dtype": "quantity", + "unit": "m^2", + "why": "slowing-down area for [13.999029, 0.348438] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "If the migration area is 13.999 and the diffusion area is 0.348438, what is the slowing-down area?", + "a": "13.650591275493905", + "valid": true, + "kind": "compute", + "expect": 13.650591275493905, + "dtype": "quantity", + "unit": "m^2", + "why": "slowing-down area for [13.999029, 0.348438] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "Could you work out the slowing-down area given migration area = 81.1967, diffusion area = 6.98086?", + "a": "74.21584883614992", + "valid": true, + "kind": "compute", + "expect": 74.21584883614992, + "dtype": "quantity", + "unit": "m^2", + "why": "slowing-down area for [81.196705, 6.980857] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "I need the slowing-down area for a migration area of 81.1967 and a diffusion area of 6.98086, please.", + "a": "74.21584883614992", + "valid": true, + "kind": "compute", + "expect": 74.21584883614992, + "dtype": "quantity", + "unit": "m^2", + "why": "slowing-down area for [81.196705, 6.980857] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "If the migration area is 19.4474 and the diffusion area is 4.51548, what is the slowing-down area?", + "a": "14.931881955429075", + "valid": true, + "kind": "compute", + "expect": 14.931881955429075, + "dtype": "quantity", + "unit": "m^2", + "why": "slowing-down area for [19.447357, 4.515475] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "Could you work out the slowing-down area given migration area = 19.4474, diffusion area = 4.51548?", + "a": "14.931881955429075", + "valid": true, + "kind": "compute", + "expect": 14.931881955429075, + "dtype": "quantity", + "unit": "m^2", + "why": "slowing-down area for [19.447357, 4.515475] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "I need the slowing-down area for a migration area of 13.2373 and a diffusion area of 4.71941, please.", + "a": "8.517896269006052", + "valid": true, + "kind": "compute", + "expect": 8.517896269006052, + "dtype": "quantity", + "unit": "m^2", + "why": "slowing-down area for [13.237311, 4.719415] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "If the migration area is 13.2373 and the diffusion area is 4.71941, what is the slowing-down area?", + "a": "8.517896269006052", + "valid": true, + "kind": "compute", + "expect": 8.517896269006052, + "dtype": "quantity", + "unit": "m^2", + "why": "slowing-down area for [13.237311, 4.719415] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "Could you work out the slowing-down area given migration area = 21.7946, diffusion area = 9.1061?", + "a": "12.68851669739933", + "valid": true, + "kind": "compute", + "expect": 12.68851669739933, + "dtype": "quantity", + "unit": "m^2", + "why": "slowing-down area for [21.794621, 9.106104] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "I need the slowing-down area for a migration area of 21.7946 and a diffusion area of 9.1061, please.", + "a": "12.68851669739933", + "valid": true, + "kind": "compute", + "expect": 12.68851669739933, + "dtype": "quantity", + "unit": "m^2", + "why": "slowing-down area for [21.794621, 9.106104] (the system migration area (Wikidata Q98966325))" + }, + { + "q": "If the migration area is 167.625, what is the slowing-down area?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the diffusion area is not given" + }, + { + "q": "Could you work out the slowing-down area given migration area = 13.999?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the diffusion area is not given" + }, + { + "q": "Do you know the slowing-down area of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the slowing-down area of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the slowing-down area given migration area = -81.1967, diffusion area = 6.98086?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative migration area: no example had one" + }, + { + "q": "What is a typical slowing-down area?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-slowing-down-area/program.py b/root/organs10/physics/specialists/solved-slowing-down-area/program.py new file mode 100644 index 0000000000000000000000000000000000000000..a5f46775e3ea3a4abd18f9824e6516f30625b07c --- /dev/null +++ b/root/organs10/physics/specialists/solved-slowing-down-area/program.py @@ -0,0 +1,7 @@ +"""slowing-down area from migration area, diffusion area (migration area (Wikidata Q98966325)) +The maths organ (systems.py): solved from the system q1 = q0 + q2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(migration_area, diffusion_area): + return -1.0*diffusion_area + 1.0*migration_area diff --git a/root/organs10/physics/specialists/solved-slowing-down-area/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-slowing-down-area/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-slowing-down-area/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-solid-angle/organ_formula_v2.py b/root/organs10/physics/specialists/solved-solid-angle/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-solid-angle/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-solid-angle/panel.json b/root/organs10/physics/specialists/solved-solid-angle/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..9b0fef29928e5f3cecff3ef16ed01d785e5816dd --- /dev/null +++ b/root/organs10/physics/specialists/solved-solid-angle/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the solid angle for area 107.961 and radius 59.4105?", + "a": "0.030587158736956204", + "valid": true, + "kind": "compute", + "expect": 0.030587158736956204, + "dtype": "quantity", + "unit": "1", + "why": "solid angle for [107.960666, 59.4105] (the system solid angle (Wikidata Q208476))" + }, + { + "q": "Tell me the solid angle with area of 107.961 and radius of 59.4105.", + "a": "0.030587158736956204", + "valid": true, + "kind": "compute", + "expect": 0.030587158736956204, + "dtype": "quantity", + "unit": "1", + "why": "solid angle for [107.960666, 59.4105] (the system solid angle (Wikidata Q208476))" + }, + { + "q": "What is the solid angle for area 169.527 and radius 280.774?", + "a": "0.0021504329786134614", + "valid": true, + "kind": "compute", + "expect": 0.0021504329786134614, + "dtype": "quantity", + "unit": "1", + "why": "solid angle for [169.526958, 280.773702] (the system solid angle (Wikidata Q208476))" + }, + { + "q": "Tell me the solid angle with area of 169.527 and radius of 280.774.", + "a": "0.0021504329786134614", + "valid": true, + "kind": "compute", + "expect": 0.0021504329786134614, + "dtype": "quantity", + "unit": "1", + "why": "solid angle for [169.526958, 280.773702] (the system solid angle (Wikidata Q208476))" + }, + { + "q": "What is the solid angle for area 1.91195 and radius 82.2401?", + "a": "0.0002826896896815724", + "valid": true, + "kind": "compute", + "expect": 0.0002826896896815724, + "dtype": "quantity", + "unit": "1", + "why": "solid angle for [1.911952, 82.240072] (the system solid angle (Wikidata Q208476))" + }, + { + "q": "Tell me the solid angle with area of 1.91195 and radius of 82.2401.", + "a": "0.0002826896896815724", + "valid": true, + "kind": "compute", + "expect": 0.0002826896896815724, + "dtype": "quantity", + "unit": "1", + "why": "solid angle for [1.911952, 82.240072] (the system solid angle (Wikidata Q208476))" + }, + { + "q": "What is the solid angle for area 1.64379 and radius 252.709?", + "a": "2.5739866144509097e-05", + "valid": true, + "kind": "compute", + "expect": 2.5739866144509097e-05, + "dtype": "quantity", + "unit": "1", + "why": "solid angle for [1.643795, 252.709024] (the system solid angle (Wikidata Q208476))" + }, + { + "q": "Tell me the solid angle with area of 1.64379 and radius of 252.709.", + "a": "2.5739866144509097e-05", + "valid": true, + "kind": "compute", + "expect": 2.5739866144509097e-05, + "dtype": "quantity", + "unit": "1", + "why": "solid angle for [1.643795, 252.709024] (the system solid angle (Wikidata Q208476))" + }, + { + "q": "What is the solid angle for area 18.3169 and radius 6.8386?", + "a": "0.3916670481342175", + "valid": true, + "kind": "compute", + "expect": 0.3916670481342175, + "dtype": "quantity", + "unit": "1", + "why": "solid angle for [18.316867, 6.838598] (the system solid angle (Wikidata Q208476))" + }, + { + "q": "Tell me the solid angle with area of 18.3169 and radius of 6.8386.", + "a": "0.3916670481342175", + "valid": true, + "kind": "compute", + "expect": 0.3916670481342175, + "dtype": "quantity", + "unit": "1", + "why": "solid angle for [18.316867, 6.838598] (the system solid angle (Wikidata Q208476))" + }, + { + "q": "What is the solid angle for area 156.502 and radius 185.886?", + "a": "0.004529233648974367", + "valid": true, + "kind": "compute", + "expect": 0.004529233648974367, + "dtype": "quantity", + "unit": "1", + "why": "solid angle for [156.50181, 185.886273] (the system solid angle (Wikidata Q208476))" + }, + { + "q": "Tell me the solid angle with area of 156.502 and radius of 185.886.", + "a": "0.004529233648974367", + "valid": true, + "kind": "compute", + "expect": 0.004529233648974367, + "dtype": "quantity", + "unit": "1", + "why": "solid angle for [156.50181, 185.886273] (the system solid angle (Wikidata Q208476))" + }, + { + "q": "What is the solid angle for area 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radius is not given" + }, + { + "q": "Tell me the solid angle with area of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radius is not given" + }, + { + "q": "What is the solid angle 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 solid angle 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 solid angle with area of -1.91195 and radius of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative area: no example had one" + }, + { + "q": "Which solid angle is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the area is 8.50728 and the radius is 17.7888, what is the solid angle?", + "a": "0.026884293358369424", + "valid": true, + "kind": "compute", + "expect": 0.026884293358369424, + "dtype": "quantity", + "unit": "1", + "why": "solid angle for [8.507283, 17.788778] (the system solid angle (Wikidata Q208476))" + }, + { + "q": "Could you work out the solid angle given area = 8.50728, radius = 17.7888?", + "a": "0.026884293358369424", + "valid": true, + "kind": "compute", + "expect": 0.026884293358369424, + "dtype": "quantity", + "unit": "1", + "why": "solid angle for [8.507283, 17.788778] (the system solid angle (Wikidata Q208476))" + }, + { + "q": "I need the solid angle for an area of 8.24951 and a radius of 0.634021, please.", + "a": "20.52205739786059", + "valid": true, + "kind": "compute", + "expect": 20.52205739786059, + "dtype": "quantity", + "unit": "1", + "why": "solid angle for [8.24951, 0.634021] (the system solid angle (Wikidata Q208476))" + }, + { + "q": "If the area is 8.24951 and the radius is 0.634021, what is the solid angle?", + "a": "20.52205739786059", + "valid": true, + "kind": "compute", + "expect": 20.52205739786059, + "dtype": "quantity", + "unit": "1", + "why": "solid angle for [8.24951, 0.634021] (the system solid angle (Wikidata Q208476))" + }, + { + "q": "Could you work out the solid angle given area = 0.318734, radius = 9.56607?", + "a": "0.003483062433049944", + "valid": true, + "kind": "compute", + "expect": 0.003483062433049944, + "dtype": "quantity", + "unit": "1", + "why": "solid angle for [0.318734, 9.566071] (the system solid angle (Wikidata Q208476))" + }, + { + "q": "I need the solid angle for an area of 0.318734 and a radius of 9.56607, please.", + "a": "0.003483062433049944", + "valid": true, + "kind": "compute", + "expect": 0.003483062433049944, + "dtype": "quantity", + "unit": "1", + "why": "solid angle for [0.318734, 9.566071] (the system solid angle (Wikidata Q208476))" + }, + { + "q": "If the area is 0.671697 and the radius is 14.2496, what is the solid angle?", + "a": "0.0033080347362955827", + "valid": true, + "kind": "compute", + "expect": 0.0033080347362955827, + "dtype": "quantity", + "unit": "1", + "why": "solid angle for [0.671697, 14.249569] (the system solid angle (Wikidata Q208476))" + }, + { + "q": "Could you work out the solid angle given area = 0.671697, radius = 14.2496?", + "a": "0.0033080347362955827", + "valid": true, + "kind": "compute", + "expect": 0.0033080347362955827, + "dtype": "quantity", + "unit": "1", + "why": "solid angle for [0.671697, 14.249569] (the system solid angle (Wikidata Q208476))" + }, + { + "q": "I need the solid angle for an area of 21.7931 and a radius of 29.5106, please.", + "a": "0.025024373050555816", + "valid": true, + "kind": "compute", + "expect": 0.025024373050555816, + "dtype": "quantity", + "unit": "1", + "why": "solid angle for [21.793126, 29.510609] (the system solid angle (Wikidata Q208476))" + }, + { + "q": "If the area is 21.7931 and the radius is 29.5106, what is the solid angle?", + "a": "0.025024373050555816", + "valid": true, + "kind": "compute", + "expect": 0.025024373050555816, + "dtype": "quantity", + "unit": "1", + "why": "solid angle for [21.793126, 29.510609] (the system solid angle (Wikidata Q208476))" + }, + { + "q": "Could you work out the solid angle given area = 153.15, radius = 18.5953?", + "a": "0.44290420248180523", + "valid": true, + "kind": "compute", + "expect": 0.44290420248180523, + "dtype": "quantity", + "unit": "1", + "why": "solid angle for [153.149965, 18.595315] (the system solid angle (Wikidata Q208476))" + }, + { + "q": "I need the solid angle for an area of 153.15 and a radius of 18.5953, please.", + "a": "0.44290420248180523", + "valid": true, + "kind": "compute", + "expect": 0.44290420248180523, + "dtype": "quantity", + "unit": "1", + "why": "solid angle for [153.149965, 18.595315] (the system solid angle (Wikidata Q208476))" + }, + { + "q": "If the area is 8.50728, what is the solid angle?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radius is not given" + }, + { + "q": "Could you work out the solid angle given area = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radius is not given" + }, + { + "q": "Do you know the solid angle 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 solid angle 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 solid angle given area = -0.318734, radius = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative area: no example had one" + }, + { + "q": "What is a typical solid angle?", + "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-solid-angle/program.py b/root/organs10/physics/specialists/solved-solid-angle/program.py new file mode 100644 index 0000000000000000000000000000000000000000..cfa5a92b9c09dbfa1521ec470b3ea054e17f3683 --- /dev/null +++ b/root/organs10/physics/specialists/solved-solid-angle/program.py @@ -0,0 +1,7 @@ +"""solid angle from area, radius (solid angle (Wikidata Q208476)) +The maths organ (systems.py): solved from the system q0 = q1 / q2 **(2); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(area, radius): + return 1.0*area/radius**2 diff --git a/root/organs10/physics/specialists/solved-solid-angle/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-solid-angle/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-solid-angle/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-sound-angular-frequency/organ_formula_v2.py b/root/organs10/physics/specialists/solved-sound-angular-frequency/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-sound-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-sound-angular-frequency/panel.json b/root/organs10/physics/specialists/solved-sound-angular-frequency/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..67819fcc2d28e3521fdd8d739ac7b6af5e4ccbac --- /dev/null +++ b/root/organs10/physics/specialists/solved-sound-angular-frequency/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the sound angular frequency for sound phase speed 107.961 and sound angular wavenumber 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "Hz", + "why": "sound angular frequency for [107.960666, 59.4105] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Tell me the sound angular frequency with sound phase speed of 107.961 and sound angular wavenumber of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "Hz", + "why": "sound angular frequency for [107.960666, 59.4105] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "What is the sound angular frequency for sound phase speed 169.527 and sound angular wavenumber 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "Hz", + "why": "sound angular frequency for [169.526958, 280.773702] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Tell me the sound angular frequency with sound phase speed of 169.527 and sound angular wavenumber of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "Hz", + "why": "sound angular frequency for [169.526958, 280.773702] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "What is the sound angular frequency for sound phase speed 1.91195 and sound angular wavenumber 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "Hz", + "why": "sound angular frequency for [1.911952, 82.240072] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Tell me the sound angular frequency with sound phase speed of 1.91195 and sound angular wavenumber of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "Hz", + "why": "sound angular frequency for [1.911952, 82.240072] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "What is the sound angular frequency for sound phase speed 1.64379 and sound angular wavenumber 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "Hz", + "why": "sound angular frequency for [1.643795, 252.709024] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Tell me the sound angular frequency with sound phase speed of 1.64379 and sound angular wavenumber of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "Hz", + "why": "sound angular frequency for [1.643795, 252.709024] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "What is the sound angular frequency for sound phase speed 18.3169 and sound angular wavenumber 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "Hz", + "why": "sound angular frequency for [18.316867, 6.838598] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Tell me the sound angular frequency with sound phase speed of 18.3169 and sound angular wavenumber of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "Hz", + "why": "sound angular frequency for [18.316867, 6.838598] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "What is the sound angular frequency for sound phase speed 156.502 and sound angular wavenumber 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "Hz", + "why": "sound angular frequency for [156.50181, 185.886273] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Tell me the sound angular frequency with sound phase speed of 156.502 and sound angular wavenumber of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "Hz", + "why": "sound angular frequency for [156.50181, 185.886273] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "What is the sound angular frequency for sound phase speed 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the sound angular wavenumber is not given" + }, + { + "q": "Tell me the sound angular frequency with sound phase speed of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the sound angular wavenumber is not given" + }, + { + "q": "What is the sound 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 sound 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 sound angular frequency with sound phase speed of -1.91195 and sound angular wavenumber of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative sound phase speed: no example had one" + }, + { + "q": "Which sound 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 sound phase speed is 8.50728 and the sound angular wavenumber is 17.7888, what is the sound angular frequency?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "Hz", + "why": "sound angular frequency for [8.507283, 17.788778] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Could you work out the sound angular frequency given sound phase speed = 8.50728, sound angular wavenumber = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "Hz", + "why": "sound angular frequency for [8.507283, 17.788778] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "I need the sound angular frequency for a sound phase speed of 8.24951 and a sound angular wavenumber of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "Hz", + "why": "sound angular frequency for [8.24951, 0.634021] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "If the sound phase speed is 8.24951 and the sound angular wavenumber is 0.634021, what is the sound angular frequency?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "Hz", + "why": "sound angular frequency for [8.24951, 0.634021] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Could you work out the sound angular frequency given sound phase speed = 0.318734, sound angular wavenumber = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "Hz", + "why": "sound angular frequency for [0.318734, 9.566071] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "I need the sound angular frequency for a sound phase speed of 0.318734 and a sound angular wavenumber of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "Hz", + "why": "sound angular frequency for [0.318734, 9.566071] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "If the sound phase speed is 0.671697 and the sound angular wavenumber is 14.2496, what is the sound angular frequency?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "Hz", + "why": "sound angular frequency for [0.671697, 14.249569] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Could you work out the sound angular frequency given sound phase speed = 0.671697, sound angular wavenumber = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "Hz", + "why": "sound angular frequency for [0.671697, 14.249569] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "I need the sound angular frequency for a sound phase speed of 21.7931 and a sound angular wavenumber of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "Hz", + "why": "sound angular frequency for [21.793126, 29.510609] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "If the sound phase speed is 21.7931 and the sound angular wavenumber is 29.5106, what is the sound angular frequency?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "Hz", + "why": "sound angular frequency for [21.793126, 29.510609] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Could you work out the sound angular frequency given sound phase speed = 153.15, sound angular wavenumber = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "Hz", + "why": "sound angular frequency for [153.149965, 18.595315] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "I need the sound angular frequency for a sound phase speed of 153.15 and a sound angular wavenumber of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "Hz", + "why": "sound angular frequency for [153.149965, 18.595315] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "If the sound phase speed is 8.50728, what is the sound angular frequency?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the sound angular wavenumber is not given" + }, + { + "q": "Could you work out the sound angular frequency given sound phase speed = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the sound angular wavenumber is not given" + }, + { + "q": "Do you know the sound 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 sound 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 sound angular frequency given sound phase speed = -0.318734, sound angular wavenumber = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative sound phase speed: no example had one" + }, + { + "q": "What is a typical sound 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-sound-angular-frequency/program.py b/root/organs10/physics/specialists/solved-sound-angular-frequency/program.py new file mode 100644 index 0000000000000000000000000000000000000000..7757e33c9582af7e6194b8b458d013bf5c416b4a --- /dev/null +++ b/root/organs10/physics/specialists/solved-sound-angular-frequency/program.py @@ -0,0 +1,7 @@ +"""sound angular frequency from sound phase speed, sound angular wavenumber (sound phase speed (Wikidata Q86501878)) +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(sound_phase_speed, sound_angular_wavenumber): + return 1.0*sound_angular_wavenumber*sound_phase_speed diff --git a/root/organs10/physics/specialists/solved-sound-angular-frequency/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-sound-angular-frequency/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-sound-angular-frequency/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-sound-angular-wavenumber/organ_formula_v2.py b/root/organs10/physics/specialists/solved-sound-angular-wavenumber/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-sound-angular-wavenumber/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-sound-angular-wavenumber/panel.json b/root/organs10/physics/specialists/solved-sound-angular-wavenumber/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..c33fa8b9c01ca14266717e77bc8200ec77e22419 --- /dev/null +++ b/root/organs10/physics/specialists/solved-sound-angular-wavenumber/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the sound angular wavenumber for sound phase speed 107.961 and sound angular frequency 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "1/m", + "why": "sound angular wavenumber for [107.960666, 59.4105] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Tell me the sound angular wavenumber with sound phase speed of 107.961 and sound angular frequency of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "1/m", + "why": "sound angular wavenumber for [107.960666, 59.4105] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "What is the sound angular wavenumber for sound phase speed 169.527 and sound angular frequency 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "1/m", + "why": "sound angular wavenumber for [169.526958, 280.773702] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Tell me the sound angular wavenumber with sound phase speed of 169.527 and sound angular frequency of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "1/m", + "why": "sound angular wavenumber for [169.526958, 280.773702] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "What is the sound angular wavenumber for sound phase speed 1.91195 and sound angular frequency 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "1/m", + "why": "sound angular wavenumber for [1.911952, 82.240072] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Tell me the sound angular wavenumber with sound phase speed of 1.91195 and sound angular frequency of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "1/m", + "why": "sound angular wavenumber for [1.911952, 82.240072] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "What is the sound angular wavenumber for sound phase speed 1.64379 and sound angular frequency 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "1/m", + "why": "sound angular wavenumber for [1.643795, 252.709024] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Tell me the sound angular wavenumber with sound phase speed of 1.64379 and sound angular frequency of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "1/m", + "why": "sound angular wavenumber for [1.643795, 252.709024] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "What is the sound angular wavenumber for sound phase speed 18.3169 and sound angular frequency 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "1/m", + "why": "sound angular wavenumber for [18.316867, 6.838598] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Tell me the sound angular wavenumber with sound phase speed of 18.3169 and sound angular frequency of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "1/m", + "why": "sound angular wavenumber for [18.316867, 6.838598] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "What is the sound angular wavenumber for sound phase speed 156.502 and sound angular frequency 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "1/m", + "why": "sound angular wavenumber for [156.50181, 185.886273] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Tell me the sound angular wavenumber with sound phase speed of 156.502 and sound angular frequency of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "1/m", + "why": "sound angular wavenumber for [156.50181, 185.886273] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "What is the sound angular wavenumber for sound phase speed 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the sound angular frequency is not given" + }, + { + "q": "Tell me the sound angular wavenumber with sound phase speed of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the sound angular frequency is not given" + }, + { + "q": "What is the sound angular wavenumber of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the sound angular wavenumber of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the sound angular wavenumber with sound phase speed of -1.91195 and sound angular frequency of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative sound phase speed: no example had one" + }, + { + "q": "Which sound angular wavenumber is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the sound phase speed is 8.50728 and the sound angular frequency is 17.7888, what is the sound angular wavenumber?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "1/m", + "why": "sound angular wavenumber for [8.507283, 17.788778] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Could you work out the sound angular wavenumber given sound phase speed = 8.50728, sound angular frequency = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "1/m", + "why": "sound angular wavenumber for [8.507283, 17.788778] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "I need the sound angular wavenumber for a sound phase speed of 8.24951 and a sound angular frequency of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "1/m", + "why": "sound angular wavenumber for [8.24951, 0.634021] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "If the sound phase speed is 8.24951 and the sound angular frequency is 0.634021, what is the sound angular wavenumber?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "1/m", + "why": "sound angular wavenumber for [8.24951, 0.634021] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Could you work out the sound angular wavenumber given sound phase speed = 0.318734, sound angular frequency = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "1/m", + "why": "sound angular wavenumber for [0.318734, 9.566071] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "I need the sound angular wavenumber for a sound phase speed of 0.318734 and a sound angular frequency of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "1/m", + "why": "sound angular wavenumber for [0.318734, 9.566071] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "If the sound phase speed is 0.671697 and the sound angular frequency is 14.2496, what is the sound angular wavenumber?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "1/m", + "why": "sound angular wavenumber for [0.671697, 14.249569] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Could you work out the sound angular wavenumber given sound phase speed = 0.671697, sound angular frequency = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "1/m", + "why": "sound angular wavenumber for [0.671697, 14.249569] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "I need the sound angular wavenumber for a sound phase speed of 21.7931 and a sound angular frequency of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "1/m", + "why": "sound angular wavenumber for [21.793126, 29.510609] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "If the sound phase speed is 21.7931 and the sound angular frequency is 29.5106, what is the sound angular wavenumber?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "1/m", + "why": "sound angular wavenumber for [21.793126, 29.510609] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Could you work out the sound angular wavenumber given sound phase speed = 153.15, sound angular frequency = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "1/m", + "why": "sound angular wavenumber for [153.149965, 18.595315] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "I need the sound angular wavenumber for a sound phase speed of 153.15 and a sound angular frequency of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "1/m", + "why": "sound angular wavenumber for [153.149965, 18.595315] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "If the sound phase speed is 8.50728, what is the sound angular wavenumber?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the sound angular frequency is not given" + }, + { + "q": "Could you work out the sound angular wavenumber given sound phase speed = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the sound angular frequency is not given" + }, + { + "q": "Do you know the sound angular wavenumber of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the sound angular wavenumber of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the sound angular wavenumber given sound phase speed = -0.318734, sound angular frequency = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative sound phase speed: no example had one" + }, + { + "q": "What is a typical sound angular wavenumber?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-sound-angular-wavenumber/program.py b/root/organs10/physics/specialists/solved-sound-angular-wavenumber/program.py new file mode 100644 index 0000000000000000000000000000000000000000..f8449da1c751b495f807b55b48c9c784c19c29f3 --- /dev/null +++ b/root/organs10/physics/specialists/solved-sound-angular-wavenumber/program.py @@ -0,0 +1,7 @@ +"""sound angular wavenumber from sound phase speed, sound angular frequency (sound phase speed (Wikidata Q86501878)) +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(sound_phase_speed, sound_angular_frequency): + return 1.0*sound_angular_frequency/sound_phase_speed diff --git a/root/organs10/physics/specialists/solved-sound-angular-wavenumber/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-sound-angular-wavenumber/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-sound-angular-wavenumber/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-sound-phase-speed/organ_formula_v2.py b/root/organs10/physics/specialists/solved-sound-phase-speed/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-sound-phase-speed/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-sound-phase-speed/panel.json b/root/organs10/physics/specialists/solved-sound-phase-speed/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..d883320256f5e55e1cfac6d5fb35bab52a6c7872 --- /dev/null +++ b/root/organs10/physics/specialists/solved-sound-phase-speed/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the sound phase speed for sound angular frequency 107.961 and sound angular wavenumber 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m/s", + "why": "sound phase speed for [107.960666, 59.4105] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Tell me the sound phase speed with sound angular frequency of 107.961 and sound angular wavenumber of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m/s", + "why": "sound phase speed for [107.960666, 59.4105] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "What is the sound phase speed for sound angular frequency 169.527 and sound angular wavenumber 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m/s", + "why": "sound phase speed for [169.526958, 280.773702] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Tell me the sound phase speed with sound angular frequency of 169.527 and sound angular wavenumber of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m/s", + "why": "sound phase speed for [169.526958, 280.773702] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "What is the sound phase speed for sound angular frequency 1.91195 and sound angular wavenumber 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m/s", + "why": "sound phase speed for [1.911952, 82.240072] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Tell me the sound phase speed with sound angular frequency of 1.91195 and sound angular wavenumber of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m/s", + "why": "sound phase speed for [1.911952, 82.240072] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "What is the sound phase speed for sound angular frequency 1.64379 and sound angular wavenumber 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m/s", + "why": "sound phase speed for [1.643795, 252.709024] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Tell me the sound phase speed with sound angular frequency of 1.64379 and sound angular wavenumber of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m/s", + "why": "sound phase speed for [1.643795, 252.709024] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "What is the sound phase speed for sound angular frequency 18.3169 and sound angular wavenumber 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m/s", + "why": "sound phase speed for [18.316867, 6.838598] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Tell me the sound phase speed with sound angular frequency of 18.3169 and sound angular wavenumber of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m/s", + "why": "sound phase speed for [18.316867, 6.838598] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "What is the sound phase speed for sound angular frequency 156.502 and sound angular wavenumber 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m/s", + "why": "sound phase speed for [156.50181, 185.886273] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Tell me the sound phase speed with sound angular frequency of 156.502 and sound angular wavenumber of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m/s", + "why": "sound phase speed for [156.50181, 185.886273] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "What is the sound phase speed for sound angular frequency 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the sound angular wavenumber is not given" + }, + { + "q": "Tell me the sound phase speed with sound angular frequency of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the sound angular wavenumber is not given" + }, + { + "q": "What is the sound phase speed of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the sound phase speed of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the sound phase speed with sound angular frequency of -1.91195 and sound angular wavenumber of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative sound angular frequency: no example had one" + }, + { + "q": "Which sound phase speed is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the sound angular frequency is 8.50728 and the sound angular wavenumber is 17.7888, what is the sound phase speed?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m/s", + "why": "sound phase speed for [8.507283, 17.788778] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Could you work out the sound phase speed given sound angular frequency = 8.50728, sound angular wavenumber = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m/s", + "why": "sound phase speed for [8.507283, 17.788778] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "I need the sound phase speed for a sound angular frequency of 8.24951 and a sound angular wavenumber of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m/s", + "why": "sound phase speed for [8.24951, 0.634021] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "If the sound angular frequency is 8.24951 and the sound angular wavenumber is 0.634021, what is the sound phase speed?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m/s", + "why": "sound phase speed for [8.24951, 0.634021] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Could you work out the sound phase speed given sound angular frequency = 0.318734, sound angular wavenumber = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m/s", + "why": "sound phase speed for [0.318734, 9.566071] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "I need the sound phase speed for a sound angular frequency of 0.318734 and a sound angular wavenumber of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m/s", + "why": "sound phase speed for [0.318734, 9.566071] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "If the sound angular frequency is 0.671697 and the sound angular wavenumber is 14.2496, what is the sound phase speed?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m/s", + "why": "sound phase speed for [0.671697, 14.249569] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Could you work out the sound phase speed given sound angular frequency = 0.671697, sound angular wavenumber = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m/s", + "why": "sound phase speed for [0.671697, 14.249569] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "I need the sound phase speed for a sound angular frequency of 21.7931 and a sound angular wavenumber of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m/s", + "why": "sound phase speed for [21.793126, 29.510609] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "If the sound angular frequency is 21.7931 and the sound angular wavenumber is 29.5106, what is the sound phase speed?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m/s", + "why": "sound phase speed for [21.793126, 29.510609] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "Could you work out the sound phase speed given sound angular frequency = 153.15, sound angular wavenumber = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m/s", + "why": "sound phase speed for [153.149965, 18.595315] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "I need the sound phase speed for a sound angular frequency of 153.15 and a sound angular wavenumber of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m/s", + "why": "sound phase speed for [153.149965, 18.595315] (the system sound phase speed (Wikidata Q86501878))" + }, + { + "q": "If the sound angular frequency is 8.50728, what is the sound phase speed?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the sound angular wavenumber is not given" + }, + { + "q": "Could you work out the sound phase speed given sound angular frequency = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the sound angular wavenumber is not given" + }, + { + "q": "Do you know the sound phase speed of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the sound phase speed of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the sound phase speed given sound angular frequency = -0.318734, sound angular wavenumber = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative sound angular frequency: no example had one" + }, + { + "q": "What is a typical sound phase speed?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-sound-phase-speed/program.py b/root/organs10/physics/specialists/solved-sound-phase-speed/program.py new file mode 100644 index 0000000000000000000000000000000000000000..e90c3faae5b29b12ee93bb2297eea32c5a1828ec --- /dev/null +++ b/root/organs10/physics/specialists/solved-sound-phase-speed/program.py @@ -0,0 +1,7 @@ +"""sound phase speed from sound angular frequency, sound angular wavenumber (sound phase speed (Wikidata Q86501878)) +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(sound_angular_frequency, sound_angular_wavenumber): + return 1.0*sound_angular_frequency/sound_angular_wavenumber diff --git a/root/organs10/physics/specialists/solved-sound-phase-speed/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-sound-phase-speed/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-sound-phase-speed/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-sound-volume-velocity/organ_formula_v2.py b/root/organs10/physics/specialists/solved-sound-volume-velocity/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-sound-volume-velocity/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-sound-volume-velocity/panel.json b/root/organs10/physics/specialists/solved-sound-volume-velocity/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..23abae5cdcafa2ec9401d4338df09b7d4cb474e8 --- /dev/null +++ b/root/organs10/physics/specialists/solved-sound-volume-velocity/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the sound volume velocity for root-mean-square sound pressure 107.961 and acoustic impedance 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^3/s", + "why": "sound volume velocity for [107.960666, 59.4105] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "Tell me the sound volume velocity with root-mean-square sound pressure of 107.961 and acoustic impedance of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^3/s", + "why": "sound volume velocity for [107.960666, 59.4105] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "What is the sound volume velocity for root-mean-square sound pressure 169.527 and acoustic impedance 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^3/s", + "why": "sound volume velocity for [169.526958, 280.773702] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "Tell me the sound volume velocity with root-mean-square sound pressure of 169.527 and acoustic impedance of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^3/s", + "why": "sound volume velocity for [169.526958, 280.773702] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "What is the sound volume velocity for root-mean-square sound pressure 1.91195 and acoustic impedance 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^3/s", + "why": "sound volume velocity for [1.911952, 82.240072] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "Tell me the sound volume velocity with root-mean-square sound pressure of 1.91195 and acoustic impedance of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^3/s", + "why": "sound volume velocity for [1.911952, 82.240072] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "What is the sound volume velocity for root-mean-square sound pressure 1.64379 and acoustic impedance 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^3/s", + "why": "sound volume velocity for [1.643795, 252.709024] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "Tell me the sound volume velocity with root-mean-square sound pressure of 1.64379 and acoustic impedance of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^3/s", + "why": "sound volume velocity for [1.643795, 252.709024] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "What is the sound volume velocity for root-mean-square sound pressure 18.3169 and acoustic impedance 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^3/s", + "why": "sound volume velocity for [18.316867, 6.838598] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "Tell me the sound volume velocity with root-mean-square sound pressure of 18.3169 and acoustic impedance of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^3/s", + "why": "sound volume velocity for [18.316867, 6.838598] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "What is the sound volume velocity for root-mean-square sound pressure 156.502 and acoustic impedance 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^3/s", + "why": "sound volume velocity for [156.50181, 185.886273] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "Tell me the sound volume velocity with root-mean-square sound pressure of 156.502 and acoustic impedance of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^3/s", + "why": "sound volume velocity for [156.50181, 185.886273] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "What is the sound volume velocity for root-mean-square sound pressure 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the acoustic impedance is not given" + }, + { + "q": "Tell me the sound volume velocity with root-mean-square sound pressure of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the acoustic impedance is not given" + }, + { + "q": "What is the sound volume velocity 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 sound volume velocity 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 sound volume velocity with root-mean-square sound pressure of -1.91195 and acoustic impedance of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative root-mean-square sound pressure: no example had one" + }, + { + "q": "Which sound volume velocity 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 root-mean-square sound pressure is 8.50728 and the acoustic impedance is 17.7888, what is the sound volume velocity?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^3/s", + "why": "sound volume velocity for [8.507283, 17.788778] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "Could you work out the sound volume velocity given root-mean-square sound pressure = 8.50728, acoustic impedance = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^3/s", + "why": "sound volume velocity for [8.507283, 17.788778] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "I need the sound volume velocity for a root-mean-square sound pressure of 8.24951 and an acoustic impedance of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^3/s", + "why": "sound volume velocity for [8.24951, 0.634021] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "If the root-mean-square sound pressure is 8.24951 and the acoustic impedance is 0.634021, what is the sound volume velocity?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^3/s", + "why": "sound volume velocity for [8.24951, 0.634021] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "Could you work out the sound volume velocity given root-mean-square sound pressure = 0.318734, acoustic impedance = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^3/s", + "why": "sound volume velocity for [0.318734, 9.566071] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "I need the sound volume velocity for a root-mean-square sound pressure of 0.318734 and an acoustic impedance of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^3/s", + "why": "sound volume velocity for [0.318734, 9.566071] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "If the root-mean-square sound pressure is 0.671697 and the acoustic impedance is 14.2496, what is the sound volume velocity?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^3/s", + "why": "sound volume velocity for [0.671697, 14.249569] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "Could you work out the sound volume velocity given root-mean-square sound pressure = 0.671697, acoustic impedance = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^3/s", + "why": "sound volume velocity for [0.671697, 14.249569] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "I need the sound volume velocity for a root-mean-square sound pressure of 21.7931 and an acoustic impedance of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^3/s", + "why": "sound volume velocity for [21.793126, 29.510609] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "If the root-mean-square sound pressure is 21.7931 and the acoustic impedance is 29.5106, what is the sound volume velocity?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^3/s", + "why": "sound volume velocity for [21.793126, 29.510609] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "Could you work out the sound volume velocity given root-mean-square sound pressure = 153.15, acoustic impedance = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^3/s", + "why": "sound volume velocity for [153.149965, 18.595315] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "I need the sound volume velocity for a root-mean-square sound pressure of 153.15 and an acoustic impedance of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^3/s", + "why": "sound volume velocity for [153.149965, 18.595315] (the system acoustic impedance (Wikidata Q975684))" + }, + { + "q": "If the root-mean-square sound pressure is 8.50728, what is the sound volume velocity?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the acoustic impedance is not given" + }, + { + "q": "Could you work out the sound volume velocity given root-mean-square sound pressure = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the acoustic impedance is not given" + }, + { + "q": "Do you know the sound volume velocity 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 sound volume velocity 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 sound volume velocity given root-mean-square sound pressure = -0.318734, acoustic impedance = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative root-mean-square sound pressure: no example had one" + }, + { + "q": "What is a typical sound volume velocity?", + "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-sound-volume-velocity/program.py b/root/organs10/physics/specialists/solved-sound-volume-velocity/program.py new file mode 100644 index 0000000000000000000000000000000000000000..d4023a75bb1619e1008d97271280fb07464ba887 --- /dev/null +++ b/root/organs10/physics/specialists/solved-sound-volume-velocity/program.py @@ -0,0 +1,7 @@ +"""sound volume velocity from root-mean-square sound pressure, acoustic impedance (acoustic impedance (Wikidata Q975684)) +The maths organ (systems.py): solved from the system q1 = ((q0)/(q2)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(root_mean_square_sound_pressure, acoustic_impedance): + return 1.0*root_mean_square_sound_pressure/acoustic_impedance diff --git a/root/organs10/physics/specialists/solved-sound-volume-velocity/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-sound-volume-velocity/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-sound-volume-velocity/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-sound-wavelength/organ_formula_v2.py b/root/organs10/physics/specialists/solved-sound-wavelength/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-sound-wavelength/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-sound-wavelength/panel.json b/root/organs10/physics/specialists/solved-sound-wavelength/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..ad2c2241374bb69330a332b52e5906d5a5439829 --- /dev/null +++ b/root/organs10/physics/specialists/solved-sound-wavelength/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the sound wavelength for sound wavenumber 107.961?", + "a": "0.00926263275385168", + "valid": true, + "kind": "compute", + "expect": 0.00926263275385168, + "dtype": "quantity", + "unit": "m", + "why": "sound wavelength for [107.960666] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "Tell me the sound wavelength with sound wavenumber of 107.961.", + "a": "0.00926263275385168", + "valid": true, + "kind": "compute", + "expect": 0.00926263275385168, + "dtype": "quantity", + "unit": "m", + "why": "sound wavelength for [107.960666] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "What is the sound wavelength for sound wavenumber 22.652?", + "a": "0.044146167826513254", + "valid": true, + "kind": "compute", + "expect": 0.044146167826513254, + "dtype": "quantity", + "unit": "m", + "why": "sound wavelength for [22.652023] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "Tell me the sound wavelength with sound wavenumber of 22.652.", + "a": "0.044146167826513254", + "valid": true, + "kind": "compute", + "expect": 0.044146167826513254, + "dtype": "quantity", + "unit": "m", + "why": "sound wavelength for [22.652023] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "What is the sound wavelength for sound wavenumber 173.394?", + "a": "0.0057672242718762815", + "valid": true, + "kind": "compute", + "expect": 0.0057672242718762815, + "dtype": "quantity", + "unit": "m", + "why": "sound wavelength for [173.393638] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "Tell me the sound wavelength with sound wavenumber of 173.394.", + "a": "0.0057672242718762815", + "valid": true, + "kind": "compute", + "expect": 0.0057672242718762815, + "dtype": "quantity", + "unit": "m", + "why": "sound wavelength for [173.393638] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "What is the sound wavelength for sound wavenumber 126.709?", + "a": "0.007892083536070259", + "valid": true, + "kind": "compute", + "expect": 0.007892083536070259, + "dtype": "quantity", + "unit": "m", + "why": "sound wavelength for [126.709252] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "Tell me the sound wavelength with sound wavenumber of 126.709.", + "a": "0.007892083536070259", + "valid": true, + "kind": "compute", + "expect": 0.007892083536070259, + "dtype": "quantity", + "unit": "m", + "why": "sound wavelength for [126.709252] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "What is the sound wavelength for sound wavenumber 0.671697?", + "a": "1.4887660694527203", + "valid": true, + "kind": "compute", + "expect": 1.4887660694527203, + "dtype": "quantity", + "unit": "m", + "why": "sound wavelength for [0.671697] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "Tell me the sound wavelength with sound wavenumber of 0.671697.", + "a": "1.4887660694527203", + "valid": true, + "kind": "compute", + "expect": 1.4887660694527203, + "dtype": "quantity", + "unit": "m", + "why": "sound wavelength for [0.671697] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "What is the sound wavelength for sound wavenumber 1.16997?", + "a": "0.8547242274066585", + "valid": true, + "kind": "compute", + "expect": 0.8547242274066585, + "dtype": "quantity", + "unit": "m", + "why": "sound wavelength for [1.169968] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "Tell me the sound wavelength with sound wavenumber of 1.16997.", + "a": "0.8547242274066585", + "valid": true, + "kind": "compute", + "expect": 0.8547242274066585, + "dtype": "quantity", + "unit": "m", + "why": "sound wavelength for [1.169968] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "What is the sound wavelength?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the sound wavenumber is not given" + }, + { + "q": "Could you work out the sound wavelength, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the sound wavenumber is not given" + }, + { + "q": "What is the sound wavelength 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 sound wavelength 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 sound wavelength with sound wavenumber of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative sound wavenumber: no example had one" + }, + { + "q": "Which sound wavelength 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 sound wavenumber is 2.57003, what is the sound wavelength?", + "a": "0.38910052946214563", + "valid": true, + "kind": "compute", + "expect": 0.38910052946214563, + "dtype": "quantity", + "unit": "m", + "why": "sound wavelength for [2.57003] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "Could you work out the sound wavelength given sound wavenumber = 2.57003?", + "a": "0.38910052946214563", + "valid": true, + "kind": "compute", + "expect": 0.38910052946214563, + "dtype": "quantity", + "unit": "m", + "why": "sound wavelength for [2.57003] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "I need the sound wavelength for a sound wavenumber of 48.921, please.", + "a": "0.02044112317474344", + "valid": true, + "kind": "compute", + "expect": 0.02044112317474344, + "dtype": "quantity", + "unit": "m", + "why": "sound wavelength for [48.920991] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "If the sound wavenumber is 48.921, what is the sound wavelength?", + "a": "0.02044112317474344", + "valid": true, + "kind": "compute", + "expect": 0.02044112317474344, + "dtype": "quantity", + "unit": "m", + "why": "sound wavelength for [48.920991] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "Could you work out the sound wavelength given sound wavenumber = 0.348438?", + "a": "2.869953759769417", + "valid": true, + "kind": "compute", + "expect": 2.869953759769417, + "dtype": "quantity", + "unit": "m", + "why": "sound wavelength for [0.348438] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "I need the sound wavelength for a sound wavenumber of 0.348438, please.", + "a": "2.869953759769417", + "valid": true, + "kind": "compute", + "expect": 2.869953759769417, + "dtype": "quantity", + "unit": "m", + "why": "sound wavelength for [0.348438] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "If the sound wavenumber is 252.709, what is the sound wavelength?", + "a": "0.0039571202610184176", + "valid": true, + "kind": "compute", + "expect": 0.0039571202610184176, + "dtype": "quantity", + "unit": "m", + "why": "sound wavelength for [252.709024] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "Could you work out the sound wavelength given sound wavenumber = 252.709?", + "a": "0.0039571202610184176", + "valid": true, + "kind": "compute", + "expect": 0.0039571202610184176, + "dtype": "quantity", + "unit": "m", + "why": "sound wavelength for [252.709024] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "I need the sound wavelength for a sound wavenumber of 20.3966, please.", + "a": "0.04902788018766951", + "valid": true, + "kind": "compute", + "expect": 0.04902788018766951, + "dtype": "quantity", + "unit": "m", + "why": "sound wavelength for [20.396558] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "If the sound wavenumber is 20.3966, what is the sound wavelength?", + "a": "0.04902788018766951", + "valid": true, + "kind": "compute", + "expect": 0.04902788018766951, + "dtype": "quantity", + "unit": "m", + "why": "sound wavelength for [20.396558] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "Could you work out the sound wavelength given sound wavenumber = 134.993?", + "a": "0.007407793842889246", + "valid": true, + "kind": "compute", + "expect": 0.007407793842889246, + "dtype": "quantity", + "unit": "m", + "why": "sound wavelength for [134.992958] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "I need the sound wavelength for a sound wavenumber of 134.993, please.", + "a": "0.007407793842889246", + "valid": true, + "kind": "compute", + "expect": 0.007407793842889246, + "dtype": "quantity", + "unit": "m", + "why": "sound wavelength for [134.992958] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "I need the sound wavelength, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the sound wavenumber is not given" + }, + { + "q": "Can you tell me the sound wavelength?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the sound wavenumber is not given" + }, + { + "q": "Do you know the sound wavelength 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 sound wavelength 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 sound wavelength given sound wavenumber = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative sound wavenumber: no example had one" + }, + { + "q": "What is a typical sound wavelength?", + "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-sound-wavelength/program.py b/root/organs10/physics/specialists/solved-sound-wavelength/program.py new file mode 100644 index 0000000000000000000000000000000000000000..ab8168ed0cdf21ea894d70cea5ec3d3bb19f6a54 --- /dev/null +++ b/root/organs10/physics/specialists/solved-sound-wavelength/program.py @@ -0,0 +1,7 @@ +"""sound wavelength from sound wavenumber (sound wavenumber (Wikidata Q106007899)) +The maths organ (systems.py): solved from the system q0 = ((1)/(q1)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(sound_wavenumber): + return 1.0/sound_wavenumber diff --git a/root/organs10/physics/specialists/solved-sound-wavelength/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-sound-wavelength/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-sound-wavelength/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-sound-wavenumber/organ_formula_v2.py b/root/organs10/physics/specialists/solved-sound-wavenumber/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-sound-wavenumber/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-sound-wavenumber/panel.json b/root/organs10/physics/specialists/solved-sound-wavenumber/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..1ded087bf1da7a6021f21ab3950c7b6adfa48a33 --- /dev/null +++ b/root/organs10/physics/specialists/solved-sound-wavenumber/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the sound wavenumber for sound wavelength 107.961?", + "a": "0.00926263275385168", + "valid": true, + "kind": "compute", + "expect": 0.00926263275385168, + "dtype": "quantity", + "unit": "1/m", + "why": "sound wavenumber for [107.960666] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "Tell me the sound wavenumber with sound wavelength of 107.961.", + "a": "0.00926263275385168", + "valid": true, + "kind": "compute", + "expect": 0.00926263275385168, + "dtype": "quantity", + "unit": "1/m", + "why": "sound wavenumber for [107.960666] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "What is the sound wavenumber for sound wavelength 22.652?", + "a": "0.044146167826513254", + "valid": true, + "kind": "compute", + "expect": 0.044146167826513254, + "dtype": "quantity", + "unit": "1/m", + "why": "sound wavenumber for [22.652023] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "Tell me the sound wavenumber with sound wavelength of 22.652.", + "a": "0.044146167826513254", + "valid": true, + "kind": "compute", + "expect": 0.044146167826513254, + "dtype": "quantity", + "unit": "1/m", + "why": "sound wavenumber for [22.652023] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "What is the sound wavenumber for sound wavelength 173.394?", + "a": "0.0057672242718762815", + "valid": true, + "kind": "compute", + "expect": 0.0057672242718762815, + "dtype": "quantity", + "unit": "1/m", + "why": "sound wavenumber for [173.393638] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "Tell me the sound wavenumber with sound wavelength of 173.394.", + "a": "0.0057672242718762815", + "valid": true, + "kind": "compute", + "expect": 0.0057672242718762815, + "dtype": "quantity", + "unit": "1/m", + "why": "sound wavenumber for [173.393638] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "What is the sound wavenumber for sound wavelength 126.709?", + "a": "0.007892083536070259", + "valid": true, + "kind": "compute", + "expect": 0.007892083536070259, + "dtype": "quantity", + "unit": "1/m", + "why": "sound wavenumber for [126.709252] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "Tell me the sound wavenumber with sound wavelength of 126.709.", + "a": "0.007892083536070259", + "valid": true, + "kind": "compute", + "expect": 0.007892083536070259, + "dtype": "quantity", + "unit": "1/m", + "why": "sound wavenumber for [126.709252] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "What is the sound wavenumber for sound wavelength 0.671697?", + "a": "1.4887660694527203", + "valid": true, + "kind": "compute", + "expect": 1.4887660694527203, + "dtype": "quantity", + "unit": "1/m", + "why": "sound wavenumber for [0.671697] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "Tell me the sound wavenumber with sound wavelength of 0.671697.", + "a": "1.4887660694527203", + "valid": true, + "kind": "compute", + "expect": 1.4887660694527203, + "dtype": "quantity", + "unit": "1/m", + "why": "sound wavenumber for [0.671697] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "What is the sound wavenumber for sound wavelength 1.16997?", + "a": "0.8547242274066585", + "valid": true, + "kind": "compute", + "expect": 0.8547242274066585, + "dtype": "quantity", + "unit": "1/m", + "why": "sound wavenumber for [1.169968] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "Tell me the sound wavenumber with sound wavelength of 1.16997.", + "a": "0.8547242274066585", + "valid": true, + "kind": "compute", + "expect": 0.8547242274066585, + "dtype": "quantity", + "unit": "1/m", + "why": "sound wavenumber for [1.169968] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "What is the sound wavenumber?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the sound wavelength is not given" + }, + { + "q": "Could you work out the sound wavenumber, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the sound wavelength is not given" + }, + { + "q": "What is the sound wavenumber of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the sound wavenumber of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the sound wavenumber with sound wavelength of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative sound wavelength: no example had one" + }, + { + "q": "Which sound wavenumber is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the sound wavelength is 2.57003, what is the sound wavenumber?", + "a": "0.38910052946214563", + "valid": true, + "kind": "compute", + "expect": 0.38910052946214563, + "dtype": "quantity", + "unit": "1/m", + "why": "sound wavenumber for [2.57003] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "Could you work out the sound wavenumber given sound wavelength = 2.57003?", + "a": "0.38910052946214563", + "valid": true, + "kind": "compute", + "expect": 0.38910052946214563, + "dtype": "quantity", + "unit": "1/m", + "why": "sound wavenumber for [2.57003] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "I need the sound wavenumber for a sound wavelength of 48.921, please.", + "a": "0.02044112317474344", + "valid": true, + "kind": "compute", + "expect": 0.02044112317474344, + "dtype": "quantity", + "unit": "1/m", + "why": "sound wavenumber for [48.920991] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "If the sound wavelength is 48.921, what is the sound wavenumber?", + "a": "0.02044112317474344", + "valid": true, + "kind": "compute", + "expect": 0.02044112317474344, + "dtype": "quantity", + "unit": "1/m", + "why": "sound wavenumber for [48.920991] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "Could you work out the sound wavenumber given sound wavelength = 0.348438?", + "a": "2.869953759769417", + "valid": true, + "kind": "compute", + "expect": 2.869953759769417, + "dtype": "quantity", + "unit": "1/m", + "why": "sound wavenumber for [0.348438] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "I need the sound wavenumber for a sound wavelength of 0.348438, please.", + "a": "2.869953759769417", + "valid": true, + "kind": "compute", + "expect": 2.869953759769417, + "dtype": "quantity", + "unit": "1/m", + "why": "sound wavenumber for [0.348438] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "If the sound wavelength is 252.709, what is the sound wavenumber?", + "a": "0.0039571202610184176", + "valid": true, + "kind": "compute", + "expect": 0.0039571202610184176, + "dtype": "quantity", + "unit": "1/m", + "why": "sound wavenumber for [252.709024] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "Could you work out the sound wavenumber given sound wavelength = 252.709?", + "a": "0.0039571202610184176", + "valid": true, + "kind": "compute", + "expect": 0.0039571202610184176, + "dtype": "quantity", + "unit": "1/m", + "why": "sound wavenumber for [252.709024] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "I need the sound wavenumber for a sound wavelength of 20.3966, please.", + "a": "0.04902788018766951", + "valid": true, + "kind": "compute", + "expect": 0.04902788018766951, + "dtype": "quantity", + "unit": "1/m", + "why": "sound wavenumber for [20.396558] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "If the sound wavelength is 20.3966, what is the sound wavenumber?", + "a": "0.04902788018766951", + "valid": true, + "kind": "compute", + "expect": 0.04902788018766951, + "dtype": "quantity", + "unit": "1/m", + "why": "sound wavenumber for [20.396558] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "Could you work out the sound wavenumber given sound wavelength = 134.993?", + "a": "0.007407793842889246", + "valid": true, + "kind": "compute", + "expect": 0.007407793842889246, + "dtype": "quantity", + "unit": "1/m", + "why": "sound wavenumber for [134.992958] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "I need the sound wavenumber for a sound wavelength of 134.993, please.", + "a": "0.007407793842889246", + "valid": true, + "kind": "compute", + "expect": 0.007407793842889246, + "dtype": "quantity", + "unit": "1/m", + "why": "sound wavenumber for [134.992958] (the system sound wavenumber (Wikidata Q106007899))" + }, + { + "q": "I need the sound wavenumber, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the sound wavelength is not given" + }, + { + "q": "Can you tell me the sound wavenumber?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the sound wavelength is not given" + }, + { + "q": "Do you know the sound wavenumber of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the sound wavenumber of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the sound wavenumber given sound wavelength = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative sound wavelength: no example had one" + }, + { + "q": "What is a typical sound wavenumber?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-sound-wavenumber/program.py b/root/organs10/physics/specialists/solved-sound-wavenumber/program.py new file mode 100644 index 0000000000000000000000000000000000000000..42f1d6741bfa9331a489384b2f804919d5ab17e3 --- /dev/null +++ b/root/organs10/physics/specialists/solved-sound-wavenumber/program.py @@ -0,0 +1,7 @@ +"""sound wavenumber from sound wavelength (sound wavenumber (Wikidata Q106007899)) +The maths organ (systems.py): solved from the system q0 = ((1)/(q1)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(sound_wavelength): + return 1.0/sound_wavelength diff --git a/root/organs10/physics/specialists/solved-sound-wavenumber/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-sound-wavenumber/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-sound-wavenumber/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-specific-activity/organ_formula_v2.py b/root/organs10/physics/specialists/solved-specific-activity/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-activity/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-specific-activity/panel.json b/root/organs10/physics/specialists/solved-specific-activity/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..4181408c6f96f268a44fc5cc4930d7e77667a68c --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-activity/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the specific activity for activity 107.961 and mass 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1/kg/s", + "why": "specific activity for [107.960666, 59.4105] (the system specific activity (Wikidata Q2823748))" + }, + { + "q": "Tell me the specific activity with activity of 107.961 and mass of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1/kg/s", + "why": "specific activity for [107.960666, 59.4105] (the system specific activity (Wikidata Q2823748))" + }, + { + "q": "What is the specific activity for activity 169.527 and mass 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1/kg/s", + "why": "specific activity for [169.526958, 280.773702] (the system specific activity (Wikidata Q2823748))" + }, + { + "q": "Tell me the specific activity with activity of 169.527 and mass of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1/kg/s", + "why": "specific activity for [169.526958, 280.773702] (the system specific activity (Wikidata Q2823748))" + }, + { + "q": "What is the specific activity for activity 1.91195 and mass 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1/kg/s", + "why": "specific activity for [1.911952, 82.240072] (the system specific activity (Wikidata Q2823748))" + }, + { + "q": "Tell me the specific activity with activity of 1.91195 and mass of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1/kg/s", + "why": "specific activity for [1.911952, 82.240072] (the system specific activity (Wikidata Q2823748))" + }, + { + "q": "What is the specific activity for activity 1.64379 and mass 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1/kg/s", + "why": "specific activity for [1.643795, 252.709024] (the system specific activity (Wikidata Q2823748))" + }, + { + "q": "Tell me the specific activity with activity of 1.64379 and mass of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1/kg/s", + "why": "specific activity for [1.643795, 252.709024] (the system specific activity (Wikidata Q2823748))" + }, + { + "q": "What is the specific activity for activity 18.3169 and mass 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1/kg/s", + "why": "specific activity for [18.316867, 6.838598] (the system specific activity (Wikidata Q2823748))" + }, + { + "q": "Tell me the specific activity with activity of 18.3169 and mass of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1/kg/s", + "why": "specific activity for [18.316867, 6.838598] (the system specific activity (Wikidata Q2823748))" + }, + { + "q": "What is the specific activity for activity 156.502 and mass 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1/kg/s", + "why": "specific activity for [156.50181, 185.886273] (the system specific activity (Wikidata Q2823748))" + }, + { + "q": "Tell me the specific activity with activity of 156.502 and mass of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1/kg/s", + "why": "specific activity for [156.50181, 185.886273] (the system specific activity (Wikidata Q2823748))" + }, + { + "q": "What is the specific activity for activity 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Tell me the specific activity with activity of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "What is the specific activity 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 specific activity 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 specific activity with activity of -1.91195 and mass of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative activity: no example had one" + }, + { + "q": "Which specific activity 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 activity is 8.50728 and the mass is 17.7888, what is the specific activity?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1/kg/s", + "why": "specific activity for [8.507283, 17.788778] (the system specific activity (Wikidata Q2823748))" + }, + { + "q": "Could you work out the specific activity given activity = 8.50728, mass = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1/kg/s", + "why": "specific activity for [8.507283, 17.788778] (the system specific activity (Wikidata Q2823748))" + }, + { + "q": "I need the specific activity for an activity of 8.24951 and a mass of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1/kg/s", + "why": "specific activity for [8.24951, 0.634021] (the system specific activity (Wikidata Q2823748))" + }, + { + "q": "If the activity is 8.24951 and the mass is 0.634021, what is the specific activity?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1/kg/s", + "why": "specific activity for [8.24951, 0.634021] (the system specific activity (Wikidata Q2823748))" + }, + { + "q": "Could you work out the specific activity given activity = 0.318734, mass = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1/kg/s", + "why": "specific activity for [0.318734, 9.566071] (the system specific activity (Wikidata Q2823748))" + }, + { + "q": "I need the specific activity for an activity of 0.318734 and a mass of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1/kg/s", + "why": "specific activity for [0.318734, 9.566071] (the system specific activity (Wikidata Q2823748))" + }, + { + "q": "If the activity is 0.671697 and the mass is 14.2496, what is the specific activity?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1/kg/s", + "why": "specific activity for [0.671697, 14.249569] (the system specific activity (Wikidata Q2823748))" + }, + { + "q": "Could you work out the specific activity given activity = 0.671697, mass = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1/kg/s", + "why": "specific activity for [0.671697, 14.249569] (the system specific activity (Wikidata Q2823748))" + }, + { + "q": "I need the specific activity for an activity of 21.7931 and a mass of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1/kg/s", + "why": "specific activity for [21.793126, 29.510609] (the system specific activity (Wikidata Q2823748))" + }, + { + "q": "If the activity is 21.7931 and the mass is 29.5106, what is the specific activity?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1/kg/s", + "why": "specific activity for [21.793126, 29.510609] (the system specific activity (Wikidata Q2823748))" + }, + { + "q": "Could you work out the specific activity given activity = 153.15, mass = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1/kg/s", + "why": "specific activity for [153.149965, 18.595315] (the system specific activity (Wikidata Q2823748))" + }, + { + "q": "I need the specific activity for an activity of 153.15 and a mass of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1/kg/s", + "why": "specific activity for [153.149965, 18.595315] (the system specific activity (Wikidata Q2823748))" + }, + { + "q": "If the activity is 8.50728, what is the specific activity?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Could you work out the specific activity given activity = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Do you know the specific activity 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 specific activity 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 specific activity given activity = -0.318734, mass = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative activity: no example had one" + }, + { + "q": "What is a typical specific activity?", + "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-specific-activity/program.py b/root/organs10/physics/specialists/solved-specific-activity/program.py new file mode 100644 index 0000000000000000000000000000000000000000..1359e35cd41e5726a230afe7638ce3a44f238f21 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-activity/program.py @@ -0,0 +1,7 @@ +"""specific activity from activity, mass (specific activity (Wikidata Q2823748)) +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(activity, mass): + return 1.0*activity/mass diff --git a/root/organs10/physics/specialists/solved-specific-activity/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-specific-activity/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-activity/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-specific-energy-imparted/organ_formula_v2.py b/root/organs10/physics/specialists/solved-specific-energy-imparted/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-energy-imparted/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-specific-energy-imparted/panel.json b/root/organs10/physics/specialists/solved-specific-energy-imparted/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..476e231a989797969b0de849e827b7a7b4dc1705 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-energy-imparted/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the specific energy imparted for energy imparted 107.961 and mass 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific energy imparted for [107.960666, 59.4105] (the system specific energy imparted (Wikidata Q99566195))" + }, + { + "q": "Tell me the specific energy imparted with energy imparted of 107.961 and mass of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific energy imparted for [107.960666, 59.4105] (the system specific energy imparted (Wikidata Q99566195))" + }, + { + "q": "What is the specific energy imparted for energy imparted 169.527 and mass 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific energy imparted for [169.526958, 280.773702] (the system specific energy imparted (Wikidata Q99566195))" + }, + { + "q": "Tell me the specific energy imparted with energy imparted of 169.527 and mass of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific energy imparted for [169.526958, 280.773702] (the system specific energy imparted (Wikidata Q99566195))" + }, + { + "q": "What is the specific energy imparted for energy imparted 1.91195 and mass 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific energy imparted for [1.911952, 82.240072] (the system specific energy imparted (Wikidata Q99566195))" + }, + { + "q": "Tell me the specific energy imparted with energy imparted of 1.91195 and mass of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific energy imparted for [1.911952, 82.240072] (the system specific energy imparted (Wikidata Q99566195))" + }, + { + "q": "What is the specific energy imparted for energy imparted 1.64379 and mass 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific energy imparted for [1.643795, 252.709024] (the system specific energy imparted (Wikidata Q99566195))" + }, + { + "q": "Tell me the specific energy imparted with energy imparted of 1.64379 and mass of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific energy imparted for [1.643795, 252.709024] (the system specific energy imparted (Wikidata Q99566195))" + }, + { + "q": "What is the specific energy imparted for energy imparted 18.3169 and mass 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific energy imparted for [18.316867, 6.838598] (the system specific energy imparted (Wikidata Q99566195))" + }, + { + "q": "Tell me the specific energy imparted with energy imparted of 18.3169 and mass of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific energy imparted for [18.316867, 6.838598] (the system specific energy imparted (Wikidata Q99566195))" + }, + { + "q": "What is the specific energy imparted for energy imparted 156.502 and mass 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific energy imparted for [156.50181, 185.886273] (the system specific energy imparted (Wikidata Q99566195))" + }, + { + "q": "Tell me the specific energy imparted with energy imparted of 156.502 and mass of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific energy imparted for [156.50181, 185.886273] (the system specific energy imparted (Wikidata Q99566195))" + }, + { + "q": "What is the specific energy imparted for energy imparted 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Tell me the specific energy imparted with energy imparted of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "What is the specific energy imparted 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 specific energy imparted 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 specific energy imparted with energy imparted of -1.91195 and mass of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative energy imparted: no example had one" + }, + { + "q": "Which specific energy imparted 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 energy imparted is 8.50728 and the mass is 17.7888, what is the specific energy imparted?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific energy imparted for [8.507283, 17.788778] (the system specific energy imparted (Wikidata Q99566195))" + }, + { + "q": "Could you work out the specific energy imparted given energy imparted = 8.50728, mass = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific energy imparted for [8.507283, 17.788778] (the system specific energy imparted (Wikidata Q99566195))" + }, + { + "q": "I need the specific energy imparted for an energy imparted of 8.24951 and a mass of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific energy imparted for [8.24951, 0.634021] (the system specific energy imparted (Wikidata Q99566195))" + }, + { + "q": "If the energy imparted is 8.24951 and the mass is 0.634021, what is the specific energy imparted?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific energy imparted for [8.24951, 0.634021] (the system specific energy imparted (Wikidata Q99566195))" + }, + { + "q": "Could you work out the specific energy imparted given energy imparted = 0.318734, mass = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific energy imparted for [0.318734, 9.566071] (the system specific energy imparted (Wikidata Q99566195))" + }, + { + "q": "I need the specific energy imparted for an energy imparted of 0.318734 and a mass of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific energy imparted for [0.318734, 9.566071] (the system specific energy imparted (Wikidata Q99566195))" + }, + { + "q": "If the energy imparted is 0.671697 and the mass is 14.2496, what is the specific energy imparted?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific energy imparted for [0.671697, 14.249569] (the system specific energy imparted (Wikidata Q99566195))" + }, + { + "q": "Could you work out the specific energy imparted given energy imparted = 0.671697, mass = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific energy imparted for [0.671697, 14.249569] (the system specific energy imparted (Wikidata Q99566195))" + }, + { + "q": "I need the specific energy imparted for an energy imparted of 21.7931 and a mass of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific energy imparted for [21.793126, 29.510609] (the system specific energy imparted (Wikidata Q99566195))" + }, + { + "q": "If the energy imparted is 21.7931 and the mass is 29.5106, what is the specific energy imparted?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific energy imparted for [21.793126, 29.510609] (the system specific energy imparted (Wikidata Q99566195))" + }, + { + "q": "Could you work out the specific energy imparted given energy imparted = 153.15, mass = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific energy imparted for [153.149965, 18.595315] (the system specific energy imparted (Wikidata Q99566195))" + }, + { + "q": "I need the specific energy imparted for an energy imparted of 153.15 and a mass of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific energy imparted for [153.149965, 18.595315] (the system specific energy imparted (Wikidata Q99566195))" + }, + { + "q": "If the energy imparted is 8.50728, what is the specific energy imparted?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Could you work out the specific energy imparted given energy imparted = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Do you know the specific energy imparted 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 specific energy imparted 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 specific energy imparted given energy imparted = -0.318734, mass = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative energy imparted: no example had one" + }, + { + "q": "What is a typical specific energy imparted?", + "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-specific-energy-imparted/program.py b/root/organs10/physics/specialists/solved-specific-energy-imparted/program.py new file mode 100644 index 0000000000000000000000000000000000000000..776aeeaaa5af64027e7efe5c992ac1311834f456 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-energy-imparted/program.py @@ -0,0 +1,7 @@ +"""specific energy imparted from energy imparted, mass (specific energy imparted (Wikidata Q99566195)) +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(energy_imparted, mass): + return 1.0*energy_imparted/mass diff --git a/root/organs10/physics/specialists/solved-specific-energy-imparted/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-specific-energy-imparted/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-energy-imparted/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-specific-enthalpy-of-fusion/organ_formula_v2.py b/root/organs10/physics/specialists/solved-specific-enthalpy-of-fusion/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-enthalpy-of-fusion/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-specific-enthalpy-of-fusion/panel.json b/root/organs10/physics/specialists/solved-specific-enthalpy-of-fusion/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..6f1cf534c67a71d2d4a688a1fd0e0f20ea233156 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-enthalpy-of-fusion/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the specific enthalpy of fusion for enthalpy of fusion 107.961 and mass 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy of fusion for [107.960666, 59.4105] (the system specific enthalpy of fusion (Wikidata Q108858257))" + }, + { + "q": "Tell me the specific enthalpy of fusion with enthalpy of fusion of 107.961 and mass of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy of fusion for [107.960666, 59.4105] (the system specific enthalpy of fusion (Wikidata Q108858257))" + }, + { + "q": "What is the specific enthalpy of fusion for enthalpy of fusion 169.527 and mass 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy of fusion for [169.526958, 280.773702] (the system specific enthalpy of fusion (Wikidata Q108858257))" + }, + { + "q": "Tell me the specific enthalpy of fusion with enthalpy of fusion of 169.527 and mass of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy of fusion for [169.526958, 280.773702] (the system specific enthalpy of fusion (Wikidata Q108858257))" + }, + { + "q": "What is the specific enthalpy of fusion for enthalpy of fusion 1.91195 and mass 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy of fusion for [1.911952, 82.240072] (the system specific enthalpy of fusion (Wikidata Q108858257))" + }, + { + "q": "Tell me the specific enthalpy of fusion with enthalpy of fusion of 1.91195 and mass of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy of fusion for [1.911952, 82.240072] (the system specific enthalpy of fusion (Wikidata Q108858257))" + }, + { + "q": "What is the specific enthalpy of fusion for enthalpy of fusion 1.64379 and mass 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy of fusion for [1.643795, 252.709024] (the system specific enthalpy of fusion (Wikidata Q108858257))" + }, + { + "q": "Tell me the specific enthalpy of fusion with enthalpy of fusion of 1.64379 and mass of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy of fusion for [1.643795, 252.709024] (the system specific enthalpy of fusion (Wikidata Q108858257))" + }, + { + "q": "What is the specific enthalpy of fusion for enthalpy of fusion 18.3169 and mass 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy of fusion for [18.316867, 6.838598] (the system specific enthalpy of fusion (Wikidata Q108858257))" + }, + { + "q": "Tell me the specific enthalpy of fusion with enthalpy of fusion of 18.3169 and mass of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy of fusion for [18.316867, 6.838598] (the system specific enthalpy of fusion (Wikidata Q108858257))" + }, + { + "q": "What is the specific enthalpy of fusion for enthalpy of fusion 156.502 and mass 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy of fusion for [156.50181, 185.886273] (the system specific enthalpy of fusion (Wikidata Q108858257))" + }, + { + "q": "Tell me the specific enthalpy of fusion with enthalpy of fusion of 156.502 and mass of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy of fusion for [156.50181, 185.886273] (the system specific enthalpy of fusion (Wikidata Q108858257))" + }, + { + "q": "What is the specific enthalpy of fusion for enthalpy of fusion 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Tell me the specific enthalpy of fusion with enthalpy of fusion of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "What is the specific enthalpy of fusion 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 specific enthalpy of fusion 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 specific enthalpy of fusion with enthalpy of fusion of -1.91195 and mass of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative enthalpy of fusion: no example had one" + }, + { + "q": "Which specific enthalpy of fusion is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the enthalpy of fusion is 8.50728 and the mass is 17.7888, what is the specific enthalpy of fusion?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy of fusion for [8.507283, 17.788778] (the system specific enthalpy of fusion (Wikidata Q108858257))" + }, + { + "q": "Could you work out the specific enthalpy of fusion given enthalpy of fusion = 8.50728, mass = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy of fusion for [8.507283, 17.788778] (the system specific enthalpy of fusion (Wikidata Q108858257))" + }, + { + "q": "I need the specific enthalpy of fusion for an enthalpy of fusion of 8.24951 and a mass of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy of fusion for [8.24951, 0.634021] (the system specific enthalpy of fusion (Wikidata Q108858257))" + }, + { + "q": "If the enthalpy of fusion is 8.24951 and the mass is 0.634021, what is the specific enthalpy of fusion?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy of fusion for [8.24951, 0.634021] (the system specific enthalpy of fusion (Wikidata Q108858257))" + }, + { + "q": "Could you work out the specific enthalpy of fusion given enthalpy of fusion = 0.318734, mass = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy of fusion for [0.318734, 9.566071] (the system specific enthalpy of fusion (Wikidata Q108858257))" + }, + { + "q": "I need the specific enthalpy of fusion for an enthalpy of fusion of 0.318734 and a mass of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy of fusion for [0.318734, 9.566071] (the system specific enthalpy of fusion (Wikidata Q108858257))" + }, + { + "q": "If the enthalpy of fusion is 0.671697 and the mass is 14.2496, what is the specific enthalpy of fusion?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy of fusion for [0.671697, 14.249569] (the system specific enthalpy of fusion (Wikidata Q108858257))" + }, + { + "q": "Could you work out the specific enthalpy of fusion given enthalpy of fusion = 0.671697, mass = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy of fusion for [0.671697, 14.249569] (the system specific enthalpy of fusion (Wikidata Q108858257))" + }, + { + "q": "I need the specific enthalpy of fusion for an enthalpy of fusion of 21.7931 and a mass of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy of fusion for [21.793126, 29.510609] (the system specific enthalpy of fusion (Wikidata Q108858257))" + }, + { + "q": "If the enthalpy of fusion is 21.7931 and the mass is 29.5106, what is the specific enthalpy of fusion?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy of fusion for [21.793126, 29.510609] (the system specific enthalpy of fusion (Wikidata Q108858257))" + }, + { + "q": "Could you work out the specific enthalpy of fusion given enthalpy of fusion = 153.15, mass = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy of fusion for [153.149965, 18.595315] (the system specific enthalpy of fusion (Wikidata Q108858257))" + }, + { + "q": "I need the specific enthalpy of fusion for an enthalpy of fusion of 153.15 and a mass of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy of fusion for [153.149965, 18.595315] (the system specific enthalpy of fusion (Wikidata Q108858257))" + }, + { + "q": "If the enthalpy of fusion is 8.50728, what is the specific enthalpy of fusion?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Could you work out the specific enthalpy of fusion given enthalpy of fusion = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Do you know the specific enthalpy of fusion 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 specific enthalpy of fusion 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 specific enthalpy of fusion given enthalpy of fusion = -0.318734, mass = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative enthalpy of fusion: no example had one" + }, + { + "q": "What is a typical specific enthalpy of fusion?", + "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-specific-enthalpy-of-fusion/program.py b/root/organs10/physics/specialists/solved-specific-enthalpy-of-fusion/program.py new file mode 100644 index 0000000000000000000000000000000000000000..b6044f765d29c0a7935a3ff1e8a900a6a7e5d340 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-enthalpy-of-fusion/program.py @@ -0,0 +1,7 @@ +"""specific enthalpy of fusion from enthalpy of fusion, mass (specific enthalpy of fusion (Wikidata Q108858257)) +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(enthalpy_of_fusion, mass): + return 1.0*enthalpy_of_fusion/mass diff --git a/root/organs10/physics/specialists/solved-specific-enthalpy-of-fusion/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-specific-enthalpy-of-fusion/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-enthalpy-of-fusion/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-specific-enthalpy/organ_formula_v2.py b/root/organs10/physics/specialists/solved-specific-enthalpy/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-enthalpy/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-specific-enthalpy/panel.json b/root/organs10/physics/specialists/solved-specific-enthalpy/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..faa051f8f204f8ced3caff58a3789e37162fab97 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-enthalpy/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the specific enthalpy for enthalpy 107.961 and mass 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy for [107.960666, 59.4105] (the system specific enthalpy (Wikidata Q21572993))" + }, + { + "q": "Tell me the specific enthalpy with enthalpy of 107.961 and mass of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy for [107.960666, 59.4105] (the system specific enthalpy (Wikidata Q21572993))" + }, + { + "q": "What is the specific enthalpy for enthalpy 169.527 and mass 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy for [169.526958, 280.773702] (the system specific enthalpy (Wikidata Q21572993))" + }, + { + "q": "Tell me the specific enthalpy with enthalpy of 169.527 and mass of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy for [169.526958, 280.773702] (the system specific enthalpy (Wikidata Q21572993))" + }, + { + "q": "What is the specific enthalpy for enthalpy 1.91195 and mass 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy for [1.911952, 82.240072] (the system specific enthalpy (Wikidata Q21572993))" + }, + { + "q": "Tell me the specific enthalpy with enthalpy of 1.91195 and mass of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy for [1.911952, 82.240072] (the system specific enthalpy (Wikidata Q21572993))" + }, + { + "q": "What is the specific enthalpy for enthalpy 1.64379 and mass 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy for [1.643795, 252.709024] (the system specific enthalpy (Wikidata Q21572993))" + }, + { + "q": "Tell me the specific enthalpy with enthalpy of 1.64379 and mass of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy for [1.643795, 252.709024] (the system specific enthalpy (Wikidata Q21572993))" + }, + { + "q": "What is the specific enthalpy for enthalpy 18.3169 and mass 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy for [18.316867, 6.838598] (the system specific enthalpy (Wikidata Q21572993))" + }, + { + "q": "Tell me the specific enthalpy with enthalpy of 18.3169 and mass of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy for [18.316867, 6.838598] (the system specific enthalpy (Wikidata Q21572993))" + }, + { + "q": "What is the specific enthalpy for enthalpy 156.502 and mass 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy for [156.50181, 185.886273] (the system specific enthalpy (Wikidata Q21572993))" + }, + { + "q": "Tell me the specific enthalpy with enthalpy of 156.502 and mass of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy for [156.50181, 185.886273] (the system specific enthalpy (Wikidata Q21572993))" + }, + { + "q": "What is the specific enthalpy for enthalpy 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Tell me the specific enthalpy with enthalpy of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "What is the specific enthalpy 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 specific enthalpy 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 specific enthalpy with enthalpy of -1.91195 and mass of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative enthalpy: no example had one" + }, + { + "q": "Which specific enthalpy is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the enthalpy is 8.50728 and the mass is 17.7888, what is the specific enthalpy?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy for [8.507283, 17.788778] (the system specific enthalpy (Wikidata Q21572993))" + }, + { + "q": "Could you work out the specific enthalpy given enthalpy = 8.50728, mass = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy for [8.507283, 17.788778] (the system specific enthalpy (Wikidata Q21572993))" + }, + { + "q": "I need the specific enthalpy for an enthalpy of 8.24951 and a mass of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy for [8.24951, 0.634021] (the system specific enthalpy (Wikidata Q21572993))" + }, + { + "q": "If the enthalpy is 8.24951 and the mass is 0.634021, what is the specific enthalpy?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy for [8.24951, 0.634021] (the system specific enthalpy (Wikidata Q21572993))" + }, + { + "q": "Could you work out the specific enthalpy given enthalpy = 0.318734, mass = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy for [0.318734, 9.566071] (the system specific enthalpy (Wikidata Q21572993))" + }, + { + "q": "I need the specific enthalpy for an enthalpy of 0.318734 and a mass of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy for [0.318734, 9.566071] (the system specific enthalpy (Wikidata Q21572993))" + }, + { + "q": "If the enthalpy is 0.671697 and the mass is 14.2496, what is the specific enthalpy?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy for [0.671697, 14.249569] (the system specific enthalpy (Wikidata Q21572993))" + }, + { + "q": "Could you work out the specific enthalpy given enthalpy = 0.671697, mass = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy for [0.671697, 14.249569] (the system specific enthalpy (Wikidata Q21572993))" + }, + { + "q": "I need the specific enthalpy for an enthalpy of 21.7931 and a mass of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy for [21.793126, 29.510609] (the system specific enthalpy (Wikidata Q21572993))" + }, + { + "q": "If the enthalpy is 21.7931 and the mass is 29.5106, what is the specific enthalpy?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy for [21.793126, 29.510609] (the system specific enthalpy (Wikidata Q21572993))" + }, + { + "q": "Could you work out the specific enthalpy given enthalpy = 153.15, mass = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy for [153.149965, 18.595315] (the system specific enthalpy (Wikidata Q21572993))" + }, + { + "q": "I need the specific enthalpy for an enthalpy of 153.15 and a mass of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific enthalpy for [153.149965, 18.595315] (the system specific enthalpy (Wikidata Q21572993))" + }, + { + "q": "If the enthalpy is 8.50728, what is the specific enthalpy?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Could you work out the specific enthalpy given enthalpy = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Do you know the specific enthalpy 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 specific enthalpy 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 specific enthalpy given enthalpy = -0.318734, mass = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative enthalpy: no example had one" + }, + { + "q": "What is a typical specific enthalpy?", + "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-specific-enthalpy/program.py b/root/organs10/physics/specialists/solved-specific-enthalpy/program.py new file mode 100644 index 0000000000000000000000000000000000000000..7f141dea5504324e2c941cc081388fb5849e13a4 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-enthalpy/program.py @@ -0,0 +1,7 @@ +"""specific enthalpy from enthalpy, mass (specific enthalpy (Wikidata Q21572993)) +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(enthalpy, mass): + return 1.0*enthalpy/mass diff --git a/root/organs10/physics/specialists/solved-specific-enthalpy/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-specific-enthalpy/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-enthalpy/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-specific-entropy/organ_formula_v2.py b/root/organs10/physics/specialists/solved-specific-entropy/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-entropy/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-specific-entropy/panel.json b/root/organs10/physics/specialists/solved-specific-entropy/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..38ea9ce7660bb389fbd294742a0cd31efc31c6b5 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-entropy/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the specific entropy for entropy 107.961 and mass 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific entropy for [107.960666, 59.4105] (the system specific entropy (Wikidata Q69423705))" + }, + { + "q": "Tell me the specific entropy with entropy of 107.961 and mass of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific entropy for [107.960666, 59.4105] (the system specific entropy (Wikidata Q69423705))" + }, + { + "q": "What is the specific entropy for entropy 169.527 and mass 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific entropy for [169.526958, 280.773702] (the system specific entropy (Wikidata Q69423705))" + }, + { + "q": "Tell me the specific entropy with entropy of 169.527 and mass of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific entropy for [169.526958, 280.773702] (the system specific entropy (Wikidata Q69423705))" + }, + { + "q": "What is the specific entropy for entropy 1.91195 and mass 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific entropy for [1.911952, 82.240072] (the system specific entropy (Wikidata Q69423705))" + }, + { + "q": "Tell me the specific entropy with entropy of 1.91195 and mass of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific entropy for [1.911952, 82.240072] (the system specific entropy (Wikidata Q69423705))" + }, + { + "q": "What is the specific entropy for entropy 1.64379 and mass 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific entropy for [1.643795, 252.709024] (the system specific entropy (Wikidata Q69423705))" + }, + { + "q": "Tell me the specific entropy with entropy of 1.64379 and mass of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific entropy for [1.643795, 252.709024] (the system specific entropy (Wikidata Q69423705))" + }, + { + "q": "What is the specific entropy for entropy 18.3169 and mass 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific entropy for [18.316867, 6.838598] (the system specific entropy (Wikidata Q69423705))" + }, + { + "q": "Tell me the specific entropy with entropy of 18.3169 and mass of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific entropy for [18.316867, 6.838598] (the system specific entropy (Wikidata Q69423705))" + }, + { + "q": "What is the specific entropy for entropy 156.502 and mass 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific entropy for [156.50181, 185.886273] (the system specific entropy (Wikidata Q69423705))" + }, + { + "q": "Tell me the specific entropy with entropy of 156.502 and mass of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific entropy for [156.50181, 185.886273] (the system specific entropy (Wikidata Q69423705))" + }, + { + "q": "What is the specific entropy for entropy 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Tell me the specific entropy with entropy of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "What is the specific entropy 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 specific entropy 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 specific entropy with entropy of -1.91195 and mass of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative entropy: no example had one" + }, + { + "q": "Which specific entropy 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 entropy is 8.50728 and the mass is 17.7888, what is the specific entropy?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific entropy for [8.507283, 17.788778] (the system specific entropy (Wikidata Q69423705))" + }, + { + "q": "Could you work out the specific entropy given entropy = 8.50728, mass = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific entropy for [8.507283, 17.788778] (the system specific entropy (Wikidata Q69423705))" + }, + { + "q": "I need the specific entropy for an entropy of 8.24951 and a mass of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific entropy for [8.24951, 0.634021] (the system specific entropy (Wikidata Q69423705))" + }, + { + "q": "If the entropy is 8.24951 and the mass is 0.634021, what is the specific entropy?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific entropy for [8.24951, 0.634021] (the system specific entropy (Wikidata Q69423705))" + }, + { + "q": "Could you work out the specific entropy given entropy = 0.318734, mass = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific entropy for [0.318734, 9.566071] (the system specific entropy (Wikidata Q69423705))" + }, + { + "q": "I need the specific entropy for an entropy of 0.318734 and a mass of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific entropy for [0.318734, 9.566071] (the system specific entropy (Wikidata Q69423705))" + }, + { + "q": "If the entropy is 0.671697 and the mass is 14.2496, what is the specific entropy?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific entropy for [0.671697, 14.249569] (the system specific entropy (Wikidata Q69423705))" + }, + { + "q": "Could you work out the specific entropy given entropy = 0.671697, mass = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific entropy for [0.671697, 14.249569] (the system specific entropy (Wikidata Q69423705))" + }, + { + "q": "I need the specific entropy for an entropy of 21.7931 and a mass of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific entropy for [21.793126, 29.510609] (the system specific entropy (Wikidata Q69423705))" + }, + { + "q": "If the entropy is 21.7931 and the mass is 29.5106, what is the specific entropy?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific entropy for [21.793126, 29.510609] (the system specific entropy (Wikidata Q69423705))" + }, + { + "q": "Could you work out the specific entropy given entropy = 153.15, mass = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific entropy for [153.149965, 18.595315] (the system specific entropy (Wikidata Q69423705))" + }, + { + "q": "I need the specific entropy for an entropy of 153.15 and a mass of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific entropy for [153.149965, 18.595315] (the system specific entropy (Wikidata Q69423705))" + }, + { + "q": "If the entropy is 8.50728, what is the specific entropy?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Could you work out the specific entropy given entropy = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Do you know the specific entropy 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 specific entropy 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 specific entropy given entropy = -0.318734, mass = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative entropy: no example had one" + }, + { + "q": "What is a typical specific entropy?", + "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-specific-entropy/program.py b/root/organs10/physics/specialists/solved-specific-entropy/program.py new file mode 100644 index 0000000000000000000000000000000000000000..72c77adc9b26baa4a4092e8f6db553380c6aace5 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-entropy/program.py @@ -0,0 +1,7 @@ +"""specific entropy from entropy, mass (specific entropy (Wikidata Q69423705)) +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(entropy, mass): + return 1.0*entropy/mass diff --git a/root/organs10/physics/specialists/solved-specific-entropy/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-specific-entropy/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-entropy/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-specific-gibbs-energy/organ_formula_v2.py b/root/organs10/physics/specialists/solved-specific-gibbs-energy/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-gibbs-energy/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-specific-gibbs-energy/panel.json b/root/organs10/physics/specialists/solved-specific-gibbs-energy/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..7ed32bb9f19653d01a6106a92beaf5f081f4bfb5 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-gibbs-energy/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the specific Gibbs energy for Gibbs free energy 107.961 and mass 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Gibbs energy for [107.960666, 59.4105] (the system specific Gibbs energy (Wikidata Q76360636))" + }, + { + "q": "Tell me the specific Gibbs energy with Gibbs free energy of 107.961 and mass of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Gibbs energy for [107.960666, 59.4105] (the system specific Gibbs energy (Wikidata Q76360636))" + }, + { + "q": "What is the specific Gibbs energy for Gibbs free energy 169.527 and mass 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Gibbs energy for [169.526958, 280.773702] (the system specific Gibbs energy (Wikidata Q76360636))" + }, + { + "q": "Tell me the specific Gibbs energy with Gibbs free energy of 169.527 and mass of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Gibbs energy for [169.526958, 280.773702] (the system specific Gibbs energy (Wikidata Q76360636))" + }, + { + "q": "What is the specific Gibbs energy for Gibbs free energy 1.91195 and mass 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Gibbs energy for [1.911952, 82.240072] (the system specific Gibbs energy (Wikidata Q76360636))" + }, + { + "q": "Tell me the specific Gibbs energy with Gibbs free energy of 1.91195 and mass of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Gibbs energy for [1.911952, 82.240072] (the system specific Gibbs energy (Wikidata Q76360636))" + }, + { + "q": "What is the specific Gibbs energy for Gibbs free energy 1.64379 and mass 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Gibbs energy for [1.643795, 252.709024] (the system specific Gibbs energy (Wikidata Q76360636))" + }, + { + "q": "Tell me the specific Gibbs energy with Gibbs free energy of 1.64379 and mass of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Gibbs energy for [1.643795, 252.709024] (the system specific Gibbs energy (Wikidata Q76360636))" + }, + { + "q": "What is the specific Gibbs energy for Gibbs free energy 18.3169 and mass 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Gibbs energy for [18.316867, 6.838598] (the system specific Gibbs energy (Wikidata Q76360636))" + }, + { + "q": "Tell me the specific Gibbs energy with Gibbs free energy of 18.3169 and mass of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Gibbs energy for [18.316867, 6.838598] (the system specific Gibbs energy (Wikidata Q76360636))" + }, + { + "q": "What is the specific Gibbs energy for Gibbs free energy 156.502 and mass 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Gibbs energy for [156.50181, 185.886273] (the system specific Gibbs energy (Wikidata Q76360636))" + }, + { + "q": "Tell me the specific Gibbs energy with Gibbs free energy of 156.502 and mass of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Gibbs energy for [156.50181, 185.886273] (the system specific Gibbs energy (Wikidata Q76360636))" + }, + { + "q": "What is the specific Gibbs energy for Gibbs free energy 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Tell me the specific Gibbs energy with Gibbs free energy of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "What is the specific Gibbs energy of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the specific Gibbs energy of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the specific Gibbs energy with Gibbs free energy of -1.91195 and mass of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Gibbs free energy: no example had one" + }, + { + "q": "Which specific Gibbs energy is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the Gibbs free energy is 8.50728 and the mass is 17.7888, what is the specific Gibbs energy?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Gibbs energy for [8.507283, 17.788778] (the system specific Gibbs energy (Wikidata Q76360636))" + }, + { + "q": "Could you work out the specific Gibbs energy given Gibbs free energy = 8.50728, mass = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Gibbs energy for [8.507283, 17.788778] (the system specific Gibbs energy (Wikidata Q76360636))" + }, + { + "q": "I need the specific Gibbs energy for a Gibbs free energy of 8.24951 and a mass of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Gibbs energy for [8.24951, 0.634021] (the system specific Gibbs energy (Wikidata Q76360636))" + }, + { + "q": "If the Gibbs free energy is 8.24951 and the mass is 0.634021, what is the specific Gibbs energy?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Gibbs energy for [8.24951, 0.634021] (the system specific Gibbs energy (Wikidata Q76360636))" + }, + { + "q": "Could you work out the specific Gibbs energy given Gibbs free energy = 0.318734, mass = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Gibbs energy for [0.318734, 9.566071] (the system specific Gibbs energy (Wikidata Q76360636))" + }, + { + "q": "I need the specific Gibbs energy for a Gibbs free energy of 0.318734 and a mass of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Gibbs energy for [0.318734, 9.566071] (the system specific Gibbs energy (Wikidata Q76360636))" + }, + { + "q": "If the Gibbs free energy is 0.671697 and the mass is 14.2496, what is the specific Gibbs energy?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Gibbs energy for [0.671697, 14.249569] (the system specific Gibbs energy (Wikidata Q76360636))" + }, + { + "q": "Could you work out the specific Gibbs energy given Gibbs free energy = 0.671697, mass = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Gibbs energy for [0.671697, 14.249569] (the system specific Gibbs energy (Wikidata Q76360636))" + }, + { + "q": "I need the specific Gibbs energy for a Gibbs free energy of 21.7931 and a mass of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Gibbs energy for [21.793126, 29.510609] (the system specific Gibbs energy (Wikidata Q76360636))" + }, + { + "q": "If the Gibbs free energy is 21.7931 and the mass is 29.5106, what is the specific Gibbs energy?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Gibbs energy for [21.793126, 29.510609] (the system specific Gibbs energy (Wikidata Q76360636))" + }, + { + "q": "Could you work out the specific Gibbs energy given Gibbs free energy = 153.15, mass = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Gibbs energy for [153.149965, 18.595315] (the system specific Gibbs energy (Wikidata Q76360636))" + }, + { + "q": "I need the specific Gibbs energy for a Gibbs free energy of 153.15 and a mass of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Gibbs energy for [153.149965, 18.595315] (the system specific Gibbs energy (Wikidata Q76360636))" + }, + { + "q": "If the Gibbs free energy is 8.50728, what is the specific Gibbs energy?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Could you work out the specific Gibbs energy given Gibbs free energy = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Do you know the specific Gibbs energy of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the specific Gibbs energy of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the specific Gibbs energy given Gibbs free energy = -0.318734, mass = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Gibbs free energy: no example had one" + }, + { + "q": "What is a typical specific Gibbs energy?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-specific-gibbs-energy/program.py b/root/organs10/physics/specialists/solved-specific-gibbs-energy/program.py new file mode 100644 index 0000000000000000000000000000000000000000..23403dbac0f724e2dd9b4dfaa7124ba476964180 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-gibbs-energy/program.py @@ -0,0 +1,7 @@ +"""specific Gibbs energy from Gibbs free energy, mass (specific Gibbs energy (Wikidata Q76360636)) +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(gibbs_free_energy, mass): + return 1.0*gibbs_free_energy/mass diff --git a/root/organs10/physics/specialists/solved-specific-gibbs-energy/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-specific-gibbs-energy/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-gibbs-energy/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-specific-heat-capacity-at-con-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-specific-heat-capacity-at-con-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-heat-capacity-at-con-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-specific-heat-capacity-at-con-2/panel.json b/root/organs10/physics/specialists/solved-specific-heat-capacity-at-con-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..d98b18944048e0ca695ee6143c1acd73fb78fb06 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-heat-capacity-at-con-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the specific heat capacity at constant pressure for Eckert number 107.961, characteristic speed 59.4105 and temperature difference 5.77717?", + "a": "5.659083741837898", + "valid": true, + "kind": "compute", + "expect": 5.659083741837898, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [107.960666, 59.4105, 5.777165] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "Tell me the specific heat capacity at constant pressure with Eckert number of 107.961, characteristic speed of 59.4105 and temperature difference of 5.77717.", + "a": "5.659083741837898", + "valid": true, + "kind": "compute", + "expect": 5.659083741837898, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [107.960666, 59.4105, 5.777165] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "What is the specific heat capacity at constant pressure for Eckert number 85.2416, characteristic speed 160.878 and temperature difference 2.69428?", + "a": "112.69412780468696", + "valid": true, + "kind": "compute", + "expect": 112.69412780468696, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [85.241623, 160.878442, 2.69428] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "Tell me the specific heat capacity at constant pressure with Eckert number of 85.2416, characteristic speed of 160.878 and temperature difference of 2.69428.", + "a": "112.69412780468696", + "valid": true, + "kind": "compute", + "expect": 112.69412780468696, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [85.241623, 160.878442, 2.69428] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "What is the specific heat capacity at constant pressure for Eckert number 45.6157, characteristic speed 4.97131 and temperature difference 94.305?", + "a": "0.005745029494864405", + "valid": true, + "kind": "compute", + "expect": 0.005745029494864405, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [45.61567, 4.971307, 94.305046] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "Tell me the specific heat capacity at constant pressure with Eckert number of 45.6157, characteristic speed of 4.97131 and temperature difference of 94.305.", + "a": "0.005745029494864405", + "valid": true, + "kind": "compute", + "expect": 0.005745029494864405, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [45.61567, 4.971307, 94.305046] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "What is the specific heat capacity at constant pressure for Eckert number 10.5638, characteristic speed 198.839 and temperature difference 0.671697?", + "a": "5571.9534562043855", + "valid": true, + "kind": "compute", + "expect": 5571.9534562043855, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [10.563843, 198.838955, 0.671697] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "Tell me the specific heat capacity at constant pressure with Eckert number of 10.5638, characteristic speed of 198.839 and temperature difference of 0.671697.", + "a": "5571.9534562043855", + "valid": true, + "kind": "compute", + "expect": 5571.9534562043855, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [10.563843, 198.838955, 0.671697] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "What is the specific heat capacity at constant pressure for Eckert number 21.7931, characteristic speed 29.5106 and temperature difference 8.50341?", + "a": "4.699415211384588", + "valid": true, + "kind": "compute", + "expect": 4.699415211384588, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [21.793126, 29.510609, 8.503407] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "Tell me the specific heat capacity at constant pressure with Eckert number of 21.7931, characteristic speed of 29.5106 and temperature difference of 8.50341.", + "a": "4.699415211384588", + "valid": true, + "kind": "compute", + "expect": 4.699415211384588, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [21.793126, 29.510609, 8.503407] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "What is the specific heat capacity at constant pressure for Eckert number 223.509, characteristic speed 17.3414 and temperature difference 7.10705?", + "a": "0.18931547910985136", + "valid": true, + "kind": "compute", + "expect": 0.18931547910985136, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [223.508796, 17.341435, 7.107049] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "Tell me the specific heat capacity at constant pressure with Eckert number of 223.509, characteristic speed of 17.3414 and temperature difference of 7.10705.", + "a": "0.18931547910985136", + "valid": true, + "kind": "compute", + "expect": 0.18931547910985136, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [223.508796, 17.341435, 7.107049] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "What is the specific heat capacity at constant pressure for Eckert number 107.961 and characteristic speed 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the temperature difference is not given" + }, + { + "q": "Tell me the specific heat capacity at constant pressure with Eckert number of 85.2416 and characteristic speed of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the temperature difference is not given" + }, + { + "q": "What is the specific heat capacity at constant pressure of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the specific heat capacity at constant pressure of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the specific heat capacity at constant pressure with Eckert number of -45.6157, characteristic speed of 4.97131 and temperature difference of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Eckert number: no example had one" + }, + { + "q": "Which specific heat capacity at constant pressure is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the Eckert number is 17.7888, characteristic speed is 167.625 and the temperature difference is 10.3291, what is the specific heat capacity at constant pressure?", + "a": "152.92223099628058", + "valid": true, + "kind": "compute", + "expect": 152.92223099628058, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [17.788778, 167.624948, 10.329054] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "Could you work out the specific heat capacity at constant pressure given Eckert number = 17.7888, characteristic speed = 167.625, temperature difference = 10.3291?", + "a": "152.92223099628058", + "valid": true, + "kind": "compute", + "expect": 152.92223099628058, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [17.788778, 167.624948, 10.329054] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "I need the specific heat capacity at constant pressure for an Eckert number of 21.511, a characteristic speed of 173.394 and a temperature difference of 251.084, please.", + "a": "5.566552325488382", + "valid": true, + "kind": "compute", + "expect": 5.566552325488382, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [21.511016, 173.393638, 251.084037] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "If the Eckert number is 21.511, characteristic speed is 173.394 and the temperature difference is 251.084, what is the specific heat capacity at constant pressure?", + "a": "5.566552325488382", + "valid": true, + "kind": "compute", + "expect": 5.566552325488382, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [21.511016, 173.393638, 251.084037] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "Could you work out the specific heat capacity at constant pressure given Eckert number = 129.245, characteristic speed = 1.18359, temperature difference = 15.9416?", + "a": "0.0006799181441626288", + "valid": true, + "kind": "compute", + "expect": 0.0006799181441626288, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [129.244984, 1.18359, 15.941612] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "I need the specific heat capacity at constant pressure for an Eckert number of 129.245, a characteristic speed of 1.18359 and a temperature difference of 15.9416, please.", + "a": "0.0006799181441626288", + "valid": true, + "kind": "compute", + "expect": 0.0006799181441626288, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [129.244984, 1.18359, 15.941612] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "If the Eckert number is 20.3966, characteristic speed is 18.3169 and the temperature difference is 6.8386, what is the specific heat capacity at constant pressure?", + "a": "2.4053507936230525", + "valid": true, + "kind": "compute", + "expect": 2.4053507936230525, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [20.396558, 18.316867, 6.838598] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "Could you work out the specific heat capacity at constant pressure given Eckert number = 20.3966, characteristic speed = 18.3169, temperature difference = 6.8386?", + "a": "2.4053507936230525", + "valid": true, + "kind": "compute", + "expect": 2.4053507936230525, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [20.396558, 18.316867, 6.838598] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "I need the specific heat capacity at constant pressure for an Eckert number of 185.886, a characteristic speed of 13.2373 and a temperature difference of 4.71941, please.", + "a": "0.19973955571183355", + "valid": true, + "kind": "compute", + "expect": 0.19973955571183355, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [185.886273, 13.237311, 4.719415] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "If the Eckert number is 185.886, characteristic speed is 13.2373 and the temperature difference is 4.71941, what is the specific heat capacity at constant pressure?", + "a": "0.19973955571183355", + "valid": true, + "kind": "compute", + "expect": 0.19973955571183355, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [185.886273, 13.237311, 4.719415] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "Could you work out the specific heat capacity at constant pressure given Eckert number = 9.1061, characteristic speed = 24.5721, temperature difference = 108.451?", + "a": "0.6113905328353927", + "valid": true, + "kind": "compute", + "expect": 0.6113905328353927, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [9.106104, 24.572086, 108.450792] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "I need the specific heat capacity at constant pressure for an Eckert number of 9.1061, a characteristic speed of 24.5721 and a temperature difference of 108.451, please.", + "a": "0.6113905328353927", + "valid": true, + "kind": "compute", + "expect": 0.6113905328353927, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [9.106104, 24.572086, 108.450792] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "If the Eckert number is 17.7888 and the characteristic speed is 167.625, what is the specific heat capacity at constant pressure?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the temperature difference is not given" + }, + { + "q": "Could you work out the specific heat capacity at constant pressure given Eckert number = 21.511, characteristic speed = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the temperature difference is not given" + }, + { + "q": "Do you know the specific heat capacity at constant pressure of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the specific heat capacity at constant pressure of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the specific heat capacity at constant pressure given Eckert number = -129.245, characteristic speed = 1.18359, temperature difference = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Eckert number: no example had one" + }, + { + "q": "What is a typical specific heat capacity at constant pressure?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-specific-heat-capacity-at-con-2/program.py b/root/organs10/physics/specialists/solved-specific-heat-capacity-at-con-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..eb98faef959cd9926d347978d1748dac6701e7d5 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-heat-capacity-at-con-2/program.py @@ -0,0 +1,7 @@ +"""specific heat capacity at constant pressure from Eckert number, characteristic speed, temperature difference (Eckert number (Wikidata Q905744)) +The maths organ (systems.py): solved from the system q1 = ((q2 **(2))/(q0*q3)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(eckert_number, characteristic_speed, temperature_difference): + return 1.0*characteristic_speed**2/(eckert_number*temperature_difference) diff --git a/root/organs10/physics/specialists/solved-specific-heat-capacity-at-con-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-specific-heat-capacity-at-con-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-heat-capacity-at-con-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-specific-heat-capacity-at-con/organ_formula_v2.py b/root/organs10/physics/specialists/solved-specific-heat-capacity-at-con/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-heat-capacity-at-con/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-specific-heat-capacity-at-con/panel.json b/root/organs10/physics/specialists/solved-specific-heat-capacity-at-con/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..5ecf65882a719411667993c718fcc6a18c74592c --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-heat-capacity-at-con/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the specific heat capacity at constant pressure for Stefan number 107.961, temperature difference 59.4105 and specific latent heat of phase transition 5.77717?", + "a": "10.498254293702432", + "valid": true, + "kind": "compute", + "expect": 10.498254293702432, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [107.960666, 59.4105, 5.777165] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Tell me the specific heat capacity at constant pressure with Stefan number of 107.961, temperature difference of 59.4105 and specific latent heat of phase transition of 5.77717.", + "a": "10.498254293702432", + "valid": true, + "kind": "compute", + "expect": 10.498254293702432, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [107.960666, 59.4105, 5.777165] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "What is the specific heat capacity at constant pressure for Stefan number 85.2416, temperature difference 160.878 and specific latent heat of phase transition 2.69428?", + "a": "1.4275672119863798", + "valid": true, + "kind": "compute", + "expect": 1.4275672119863798, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [85.241623, 160.878442, 2.69428] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Tell me the specific heat capacity at constant pressure with Stefan number of 85.2416, temperature difference of 160.878 and specific latent heat of phase transition of 2.69428.", + "a": "1.4275672119863798", + "valid": true, + "kind": "compute", + "expect": 1.4275672119863798, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [85.241623, 160.878442, 2.69428] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "What is the specific heat capacity at constant pressure for Stefan number 45.6157, temperature difference 4.97131 and specific latent heat of phase transition 94.305?", + "a": "865.3232325447225", + "valid": true, + "kind": "compute", + "expect": 865.3232325447225, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [45.61567, 4.971307, 94.305046] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Tell me the specific heat capacity at constant pressure with Stefan number of 45.6157, temperature difference of 4.97131 and specific latent heat of phase transition of 94.305.", + "a": "865.3232325447225", + "valid": true, + "kind": "compute", + "expect": 865.3232325447225, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [45.61567, 4.971307, 94.305046] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "What is the specific heat capacity at constant pressure for Stefan number 10.5638, temperature difference 198.839 and specific latent heat of phase transition 0.671697?", + "a": "0.03568568130708054", + "valid": true, + "kind": "compute", + "expect": 0.03568568130708054, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [10.563843, 198.838955, 0.671697] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Tell me the specific heat capacity at constant pressure with Stefan number of 10.5638, temperature difference of 198.839 and specific latent heat of phase transition of 0.671697.", + "a": "0.03568568130708054", + "valid": true, + "kind": "compute", + "expect": 0.03568568130708054, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [10.563843, 198.838955, 0.671697] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "What is the specific heat capacity at constant pressure for Stefan number 21.7931, temperature difference 29.5106 and specific latent heat of phase transition 8.50341?", + "a": "6.279634237980215", + "valid": true, + "kind": "compute", + "expect": 6.279634237980215, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [21.793126, 29.510609, 8.503407] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Tell me the specific heat capacity at constant pressure with Stefan number of 21.7931, temperature difference of 29.5106 and specific latent heat of phase transition of 8.50341.", + "a": "6.279634237980215", + "valid": true, + "kind": "compute", + "expect": 6.279634237980215, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [21.793126, 29.510609, 8.503407] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "What is the specific heat capacity at constant pressure for Stefan number 223.509, temperature difference 17.3414 and specific latent heat of phase transition 7.10705?", + "a": "91.60072422473009", + "valid": true, + "kind": "compute", + "expect": 91.60072422473009, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [223.508796, 17.341435, 7.107049] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Tell me the specific heat capacity at constant pressure with Stefan number of 223.509, temperature difference of 17.3414 and specific latent heat of phase transition of 7.10705.", + "a": "91.60072422473009", + "valid": true, + "kind": "compute", + "expect": 91.60072422473009, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [223.508796, 17.341435, 7.107049] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "What is the specific heat capacity at constant pressure for Stefan number 107.961 and temperature difference 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the specific latent heat of phase transition is not given" + }, + { + "q": "Tell me the specific heat capacity at constant pressure with Stefan number of 85.2416 and temperature difference of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the specific latent heat of phase transition is not given" + }, + { + "q": "What is the specific heat capacity at constant pressure of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the specific heat capacity at constant pressure of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the specific heat capacity at constant pressure with Stefan number of -45.6157, temperature difference of 4.97131 and specific latent heat of phase transition of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Stefan number: no example had one" + }, + { + "q": "Which specific heat capacity at constant pressure is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the Stefan number is 17.7888, temperature difference is 167.625 and the specific latent heat of phase transition is 10.3291, what is the specific heat capacity at constant pressure?", + "a": "1.0961450581531387", + "valid": true, + "kind": "compute", + "expect": 1.0961450581531387, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [17.788778, 167.624948, 10.329054] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Could you work out the specific heat capacity at constant pressure given Stefan number = 17.7888, temperature difference = 167.625, specific latent heat of phase transition = 10.3291?", + "a": "1.0961450581531387", + "valid": true, + "kind": "compute", + "expect": 1.0961450581531387, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [17.788778, 167.624948, 10.329054] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "I need the specific heat capacity at constant pressure for a Stefan number of 21.511, a temperature difference of 173.394 and a specific latent heat of phase transition of 251.084, please.", + "a": "31.149197612202414", + "valid": true, + "kind": "compute", + "expect": 31.149197612202414, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [21.511016, 173.393638, 251.084037] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "If the Stefan number is 21.511, temperature difference is 173.394 and the specific latent heat of phase transition is 251.084, what is the specific heat capacity at constant pressure?", + "a": "31.149197612202414", + "valid": true, + "kind": "compute", + "expect": 31.149197612202414, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [21.511016, 173.393638, 251.084037] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Could you work out the specific heat capacity at constant pressure given Stefan number = 129.245, temperature difference = 1.18359, specific latent heat of phase transition = 15.9416?", + "a": "1740.7830274187056", + "valid": true, + "kind": "compute", + "expect": 1740.7830274187056, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [129.244984, 1.18359, 15.941612] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "I need the specific heat capacity at constant pressure for a Stefan number of 129.245, a temperature difference of 1.18359 and a specific latent heat of phase transition of 15.9416, please.", + "a": "1740.7830274187056", + "valid": true, + "kind": "compute", + "expect": 1740.7830274187056, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [129.244984, 1.18359, 15.941612] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "If the Stefan number is 20.3966, temperature difference is 18.3169 and the specific latent heat of phase transition is 6.8386, what is the specific heat capacity at constant pressure?", + "a": "7.615050191987504", + "valid": true, + "kind": "compute", + "expect": 7.615050191987504, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [20.396558, 18.316867, 6.838598] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Could you work out the specific heat capacity at constant pressure given Stefan number = 20.3966, temperature difference = 18.3169, specific latent heat of phase transition = 6.8386?", + "a": "7.615050191987504", + "valid": true, + "kind": "compute", + "expect": 7.615050191987504, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [20.396558, 18.316867, 6.838598] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "I need the specific heat capacity at constant pressure for a Stefan number of 185.886, a temperature difference of 13.2373 and a specific latent heat of phase transition of 4.71941, please.", + "a": "66.27285731134774", + "valid": true, + "kind": "compute", + "expect": 66.27285731134774, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [185.886273, 13.237311, 4.719415] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "If the Stefan number is 185.886, temperature difference is 13.2373 and the specific latent heat of phase transition is 4.71941, what is the specific heat capacity at constant pressure?", + "a": "66.27285731134774", + "valid": true, + "kind": "compute", + "expect": 66.27285731134774, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [185.886273, 13.237311, 4.719415] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Could you work out the specific heat capacity at constant pressure given Stefan number = 9.1061, temperature difference = 24.5721, specific latent heat of phase transition = 108.451?", + "a": "40.19049097830896", + "valid": true, + "kind": "compute", + "expect": 40.19049097830896, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [9.106104, 24.572086, 108.450792] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "I need the specific heat capacity at constant pressure for a Stefan number of 9.1061, a temperature difference of 24.5721 and a specific latent heat of phase transition of 108.451, please.", + "a": "40.19049097830896", + "valid": true, + "kind": "compute", + "expect": 40.19049097830896, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity at constant pressure for [9.106104, 24.572086, 108.450792] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "If the Stefan number is 17.7888 and the temperature difference is 167.625, what is the specific heat capacity at constant pressure?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the specific latent heat of phase transition is not given" + }, + { + "q": "Could you work out the specific heat capacity at constant pressure given Stefan number = 21.511, temperature difference = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the specific latent heat of phase transition is not given" + }, + { + "q": "Do you know the specific heat capacity at constant pressure of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the specific heat capacity at constant pressure of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the specific heat capacity at constant pressure given Stefan number = -129.245, temperature difference = 1.18359, specific latent heat of phase transition = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Stefan number: no example had one" + }, + { + "q": "What is a typical specific heat capacity at constant pressure?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-specific-heat-capacity-at-con/program.py b/root/organs10/physics/specialists/solved-specific-heat-capacity-at-con/program.py new file mode 100644 index 0000000000000000000000000000000000000000..01c8029e79367053af2c669f206034284c6658f5 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-heat-capacity-at-con/program.py @@ -0,0 +1,7 @@ +"""specific heat capacity at constant pressure from Stefan number, temperature difference, specific latent heat of phase transition (Stefan number (Wikidata Q909876)) +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(stefan_number, temperature_difference, specific_latent_heat_of_phase_transition): + return 1.0*specific_latent_heat_of_phase_transition*stefan_number/temperature_difference diff --git a/root/organs10/physics/specialists/solved-specific-heat-capacity-at-con/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-specific-heat-capacity-at-con/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-heat-capacity-at-con/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-specific-heat-capacity/organ_formula_v2.py b/root/organs10/physics/specialists/solved-specific-heat-capacity/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-heat-capacity/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-specific-heat-capacity/panel.json b/root/organs10/physics/specialists/solved-specific-heat-capacity/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..b19c145a802f631ffec41ae7a664cff7a12001e7 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-heat-capacity/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the specific heat capacity for heat capacity 107.961 and mass 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity for [107.960666, 59.4105] (the system specific heat capacity (Wikidata Q487756))" + }, + { + "q": "Tell me the specific heat capacity with heat capacity of 107.961 and mass of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity for [107.960666, 59.4105] (the system specific heat capacity (Wikidata Q487756))" + }, + { + "q": "What is the specific heat capacity for heat capacity 169.527 and mass 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity for [169.526958, 280.773702] (the system specific heat capacity (Wikidata Q487756))" + }, + { + "q": "Tell me the specific heat capacity with heat capacity of 169.527 and mass of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity for [169.526958, 280.773702] (the system specific heat capacity (Wikidata Q487756))" + }, + { + "q": "What is the specific heat capacity for heat capacity 1.91195 and mass 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity for [1.911952, 82.240072] (the system specific heat capacity (Wikidata Q487756))" + }, + { + "q": "Tell me the specific heat capacity with heat capacity of 1.91195 and mass of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity for [1.911952, 82.240072] (the system specific heat capacity (Wikidata Q487756))" + }, + { + "q": "What is the specific heat capacity for heat capacity 1.64379 and mass 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity for [1.643795, 252.709024] (the system specific heat capacity (Wikidata Q487756))" + }, + { + "q": "Tell me the specific heat capacity with heat capacity of 1.64379 and mass of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity for [1.643795, 252.709024] (the system specific heat capacity (Wikidata Q487756))" + }, + { + "q": "What is the specific heat capacity for heat capacity 18.3169 and mass 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity for [18.316867, 6.838598] (the system specific heat capacity (Wikidata Q487756))" + }, + { + "q": "Tell me the specific heat capacity with heat capacity of 18.3169 and mass of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity for [18.316867, 6.838598] (the system specific heat capacity (Wikidata Q487756))" + }, + { + "q": "What is the specific heat capacity for heat capacity 156.502 and mass 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity for [156.50181, 185.886273] (the system specific heat capacity (Wikidata Q487756))" + }, + { + "q": "Tell me the specific heat capacity with heat capacity of 156.502 and mass of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity for [156.50181, 185.886273] (the system specific heat capacity (Wikidata Q487756))" + }, + { + "q": "What is the specific heat capacity for heat capacity 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Tell me the specific heat capacity with heat capacity of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "What is the specific heat capacity 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 specific heat capacity 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 specific heat capacity with heat capacity of -1.91195 and mass of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative heat capacity: no example had one" + }, + { + "q": "Which specific heat capacity 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 heat capacity is 8.50728 and the mass is 17.7888, what is the specific heat capacity?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity for [8.507283, 17.788778] (the system specific heat capacity (Wikidata Q487756))" + }, + { + "q": "Could you work out the specific heat capacity given heat capacity = 8.50728, mass = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity for [8.507283, 17.788778] (the system specific heat capacity (Wikidata Q487756))" + }, + { + "q": "I need the specific heat capacity for a heat capacity of 8.24951 and a mass of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity for [8.24951, 0.634021] (the system specific heat capacity (Wikidata Q487756))" + }, + { + "q": "If the heat capacity is 8.24951 and the mass is 0.634021, what is the specific heat capacity?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity for [8.24951, 0.634021] (the system specific heat capacity (Wikidata Q487756))" + }, + { + "q": "Could you work out the specific heat capacity given heat capacity = 0.318734, mass = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity for [0.318734, 9.566071] (the system specific heat capacity (Wikidata Q487756))" + }, + { + "q": "I need the specific heat capacity for a heat capacity of 0.318734 and a mass of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity for [0.318734, 9.566071] (the system specific heat capacity (Wikidata Q487756))" + }, + { + "q": "If the heat capacity is 0.671697 and the mass is 14.2496, what is the specific heat capacity?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity for [0.671697, 14.249569] (the system specific heat capacity (Wikidata Q487756))" + }, + { + "q": "Could you work out the specific heat capacity given heat capacity = 0.671697, mass = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity for [0.671697, 14.249569] (the system specific heat capacity (Wikidata Q487756))" + }, + { + "q": "I need the specific heat capacity for a heat capacity of 21.7931 and a mass of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity for [21.793126, 29.510609] (the system specific heat capacity (Wikidata Q487756))" + }, + { + "q": "If the heat capacity is 21.7931 and the mass is 29.5106, what is the specific heat capacity?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity for [21.793126, 29.510609] (the system specific heat capacity (Wikidata Q487756))" + }, + { + "q": "Could you work out the specific heat capacity given heat capacity = 153.15, mass = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity for [153.149965, 18.595315] (the system specific heat capacity (Wikidata Q487756))" + }, + { + "q": "I need the specific heat capacity for a heat capacity of 153.15 and a mass of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2/s^2/K", + "why": "specific heat capacity for [153.149965, 18.595315] (the system specific heat capacity (Wikidata Q487756))" + }, + { + "q": "If the heat capacity is 8.50728, what is the specific heat capacity?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Could you work out the specific heat capacity given heat capacity = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Do you know the specific heat capacity 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 specific heat capacity 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 specific heat capacity given heat capacity = -0.318734, mass = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative heat capacity: no example had one" + }, + { + "q": "What is a typical specific heat capacity?", + "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-specific-heat-capacity/program.py b/root/organs10/physics/specialists/solved-specific-heat-capacity/program.py new file mode 100644 index 0000000000000000000000000000000000000000..2136d5bc4ad9c1248cca3282ee2484ed042ebfae --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-heat-capacity/program.py @@ -0,0 +1,7 @@ +"""specific heat capacity from heat capacity, mass (specific heat capacity (Wikidata Q487756)) +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(heat_capacity, mass): + return 1.0*heat_capacity/mass diff --git a/root/organs10/physics/specialists/solved-specific-heat-capacity/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-specific-heat-capacity/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-heat-capacity/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-specific-heat/organ_formula_v2.py b/root/organs10/physics/specialists/solved-specific-heat/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-heat/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-specific-heat/panel.json b/root/organs10/physics/specialists/solved-specific-heat/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..72b0274fbbf89af6e5089b5264930be561fa07e0 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-heat/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the specific heat for heat 107.961, mass 59.4105 and temperature rise 5.77717?", + "a": "0.31454848897160476", + "valid": true, + "kind": "compute", + "expect": 0.31454848897160476, + "dtype": "quantity", + "unit": "J/kg/K", + "why": "specific heat for [107.960666, 59.4105, 5.777165] (the system Heating)" + }, + { + "q": "Tell me the specific heat with heat of 107.961, mass of 59.4105 and temperature rise of 5.77717.", + "a": "0.31454848897160476", + "valid": true, + "kind": "compute", + "expect": 0.31454848897160476, + "dtype": "quantity", + "unit": "J/kg/K", + "why": "specific heat for [107.960666, 59.4105, 5.777165] (the system Heating)" + }, + { + "q": "What is the specific heat for heat 85.2416, mass 160.878 and temperature rise 2.69428?", + "a": "0.19665778745152376", + "valid": true, + "kind": "compute", + "expect": 0.19665778745152376, + "dtype": "quantity", + "unit": "J/kg/K", + "why": "specific heat for [85.241623, 160.878442, 2.69428] (the system Heating)" + }, + { + "q": "Tell me the specific heat with heat of 85.2416, mass of 160.878 and temperature rise of 2.69428.", + "a": "0.19665778745152376", + "valid": true, + "kind": "compute", + "expect": 0.19665778745152376, + "dtype": "quantity", + "unit": "J/kg/K", + "why": "specific heat for [85.241623, 160.878442, 2.69428] (the system Heating)" + }, + { + "q": "What is the specific heat for heat 45.6157, mass 4.97131 and temperature rise 94.305?", + "a": "0.09729902784967777", + "valid": true, + "kind": "compute", + "expect": 0.09729902784967777, + "dtype": "quantity", + "unit": "J/kg/K", + "why": "specific heat for [45.61567, 4.971307, 94.305046] (the system Heating)" + }, + { + "q": "Tell me the specific heat with heat of 45.6157, mass of 4.97131 and temperature rise of 94.305.", + "a": "0.09729902784967777", + "valid": true, + "kind": "compute", + "expect": 0.09729902784967777, + "dtype": "quantity", + "unit": "J/kg/K", + "why": "specific heat for [45.61567, 4.971307, 94.305046] (the system Heating)" + }, + { + "q": "What is the specific heat for heat 10.5638, mass 198.839 and temperature rise 0.671697?", + "a": "0.07909461512056756", + "valid": true, + "kind": "compute", + "expect": 0.07909461512056756, + "dtype": "quantity", + "unit": "J/kg/K", + "why": "specific heat for [10.563843, 198.838955, 0.671697] (the system Heating)" + }, + { + "q": "Tell me the specific heat with heat of 10.5638, mass of 198.839 and temperature rise of 0.671697.", + "a": "0.07909461512056756", + "valid": true, + "kind": "compute", + "expect": 0.07909461512056756, + "dtype": "quantity", + "unit": "J/kg/K", + "why": "specific heat for [10.563843, 198.838955, 0.671697] (the system Heating)" + }, + { + "q": "What is the specific heat for heat 21.7931, mass 29.5106 and temperature rise 8.50341?", + "a": "0.08684571530973768", + "valid": true, + "kind": "compute", + "expect": 0.08684571530973768, + "dtype": "quantity", + "unit": "J/kg/K", + "why": "specific heat for [21.793126, 29.510609, 8.503407] (the system Heating)" + }, + { + "q": "Tell me the specific heat with heat of 21.7931, mass of 29.5106 and temperature rise of 8.50341.", + "a": "0.08684571530973768", + "valid": true, + "kind": "compute", + "expect": 0.08684571530973768, + "dtype": "quantity", + "unit": "J/kg/K", + "why": "specific heat for [21.793126, 29.510609, 8.503407] (the system Heating)" + }, + { + "q": "What is the specific heat for heat 223.509, mass 17.3414 and temperature rise 7.10705?", + "a": "1.813511306470856", + "valid": true, + "kind": "compute", + "expect": 1.813511306470856, + "dtype": "quantity", + "unit": "J/kg/K", + "why": "specific heat for [223.508796, 17.341435, 7.107049] (the system Heating)" + }, + { + "q": "Tell me the specific heat with heat of 223.509, mass of 17.3414 and temperature rise of 7.10705.", + "a": "1.813511306470856", + "valid": true, + "kind": "compute", + "expect": 1.813511306470856, + "dtype": "quantity", + "unit": "J/kg/K", + "why": "specific heat for [223.508796, 17.341435, 7.107049] (the system Heating)" + }, + { + "q": "What is the specific heat for heat 107.961 and mass 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the temperature rise is not given" + }, + { + "q": "Tell me the specific heat with heat of 85.2416 and mass of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the temperature rise is not given" + }, + { + "q": "What is the specific heat 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 specific heat 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 specific heat with heat of -45.6157, mass of 4.97131 and temperature rise of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative heat: no example had one" + }, + { + "q": "Which specific heat 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 heat is 17.7888, mass is 167.625 and the temperature rise is 10.3291, what is the specific heat?", + "a": "0.010274173498684694", + "valid": true, + "kind": "compute", + "expect": 0.010274173498684694, + "dtype": "quantity", + "unit": "J/kg/K", + "why": "specific heat for [17.788778, 167.624948, 10.329054] (the system Heating)" + }, + { + "q": "Could you work out the specific heat given heat = 17.7888, mass = 167.625, temperature rise = 10.3291?", + "a": "0.010274173498684694", + "valid": true, + "kind": "compute", + "expect": 0.010274173498684694, + "dtype": "quantity", + "unit": "J/kg/K", + "why": "specific heat for [17.788778, 167.624948, 10.329054] (the system Heating)" + }, + { + "q": "I need the specific heat for a heat of 21.511, a mass of 173.394 and a temperature rise of 251.084, please.", + "a": "0.000494092950151496", + "valid": true, + "kind": "compute", + "expect": 0.000494092950151496, + "dtype": "quantity", + "unit": "J/kg/K", + "why": "specific heat for [21.511016, 173.393638, 251.084037] (the system Heating)" + }, + { + "q": "If the heat is 21.511, mass is 173.394 and the temperature rise is 251.084, what is the specific heat?", + "a": "0.000494092950151496", + "valid": true, + "kind": "compute", + "expect": 0.000494092950151496, + "dtype": "quantity", + "unit": "J/kg/K", + "why": "specific heat for [21.511016, 173.393638, 251.084037] (the system Heating)" + }, + { + "q": "Could you work out the specific heat given heat = 129.245, mass = 1.18359, temperature rise = 15.9416?", + "a": "6.849836414751194", + "valid": true, + "kind": "compute", + "expect": 6.849836414751194, + "dtype": "quantity", + "unit": "J/kg/K", + "why": "specific heat for [129.244984, 1.18359, 15.941612] (the system Heating)" + }, + { + "q": "I need the specific heat for a heat of 129.245, a mass of 1.18359 and a temperature rise of 15.9416, please.", + "a": "6.849836414751194", + "valid": true, + "kind": "compute", + "expect": 6.849836414751194, + "dtype": "quantity", + "unit": "J/kg/K", + "why": "specific heat for [129.244984, 1.18359, 15.941612] (the system Heating)" + }, + { + "q": "If the heat is 20.3966, mass is 18.3169 and the temperature rise is 6.8386, what is the specific heat?", + "a": "0.16283157083473473", + "valid": true, + "kind": "compute", + "expect": 0.16283157083473473, + "dtype": "quantity", + "unit": "J/kg/K", + "why": "specific heat for [20.396558, 18.316867, 6.838598] (the system Heating)" + }, + { + "q": "Could you work out the specific heat given heat = 20.3966, mass = 18.3169, temperature rise = 6.8386?", + "a": "0.16283157083473473", + "valid": true, + "kind": "compute", + "expect": 0.16283157083473473, + "dtype": "quantity", + "unit": "J/kg/K", + "why": "specific heat for [20.396558, 18.316867, 6.838598] (the system Heating)" + }, + { + "q": "I need the specific heat for a heat of 185.886, a mass of 13.2373 and a temperature rise of 4.71941, please.", + "a": "2.975496158535072", + "valid": true, + "kind": "compute", + "expect": 2.975496158535072, + "dtype": "quantity", + "unit": "J/kg/K", + "why": "specific heat for [185.886273, 13.237311, 4.719415] (the system Heating)" + }, + { + "q": "If the heat is 185.886, mass is 13.2373 and the temperature rise is 4.71941, what is the specific heat?", + "a": "2.975496158535072", + "valid": true, + "kind": "compute", + "expect": 2.975496158535072, + "dtype": "quantity", + "unit": "J/kg/K", + "why": "specific heat for [185.886273, 13.237311, 4.719415] (the system Heating)" + }, + { + "q": "Could you work out the specific heat given heat = 9.1061, mass = 24.5721, temperature rise = 108.451?", + "a": "0.003417101331511669", + "valid": true, + "kind": "compute", + "expect": 0.003417101331511669, + "dtype": "quantity", + "unit": "J/kg/K", + "why": "specific heat for [9.106104, 24.572086, 108.450792] (the system Heating)" + }, + { + "q": "I need the specific heat for a heat of 9.1061, a mass of 24.5721 and a temperature rise of 108.451, please.", + "a": "0.003417101331511669", + "valid": true, + "kind": "compute", + "expect": 0.003417101331511669, + "dtype": "quantity", + "unit": "J/kg/K", + "why": "specific heat for [9.106104, 24.572086, 108.450792] (the system Heating)" + }, + { + "q": "If the heat is 17.7888 and the mass is 167.625, what is the specific heat?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the temperature rise is not given" + }, + { + "q": "Could you work out the specific heat given heat = 21.511, mass = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the temperature rise is not given" + }, + { + "q": "Do you know the specific heat 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 specific heat 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 specific heat given heat = -129.245, mass = 1.18359, temperature rise = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative heat: no example had one" + }, + { + "q": "What is a typical specific heat?", + "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-specific-heat/program.py b/root/organs10/physics/specialists/solved-specific-heat/program.py new file mode 100644 index 0000000000000000000000000000000000000000..9d7212a32f446c29d5649a486f50bfd0cb024d70 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-heat/program.py @@ -0,0 +1,7 @@ +"""specific heat from heat, mass, temperature rise (Heating) +The maths organ (systems.py): solved from the system Q = m*c*T; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(heat, mass, temperature_rise): + return 1.0*heat/(mass*temperature_rise) diff --git a/root/organs10/physics/specialists/solved-specific-heat/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-specific-heat/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-heat/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-specific-helmholtz-energy/organ_formula_v2.py b/root/organs10/physics/specialists/solved-specific-helmholtz-energy/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-helmholtz-energy/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-specific-helmholtz-energy/panel.json b/root/organs10/physics/specialists/solved-specific-helmholtz-energy/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..02870dc45fb1676ea58a0f63d685469c29b583d5 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-helmholtz-energy/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the specific Helmholtz energy for Helmholtz free energy 107.961 and mass 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Helmholtz energy for [107.960666, 59.4105] (the system specific Helmholtz energy (Wikidata Q76359554))" + }, + { + "q": "Tell me the specific Helmholtz energy with Helmholtz free energy of 107.961 and mass of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Helmholtz energy for [107.960666, 59.4105] (the system specific Helmholtz energy (Wikidata Q76359554))" + }, + { + "q": "What is the specific Helmholtz energy for Helmholtz free energy 169.527 and mass 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Helmholtz energy for [169.526958, 280.773702] (the system specific Helmholtz energy (Wikidata Q76359554))" + }, + { + "q": "Tell me the specific Helmholtz energy with Helmholtz free energy of 169.527 and mass of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Helmholtz energy for [169.526958, 280.773702] (the system specific Helmholtz energy (Wikidata Q76359554))" + }, + { + "q": "What is the specific Helmholtz energy for Helmholtz free energy 1.91195 and mass 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Helmholtz energy for [1.911952, 82.240072] (the system specific Helmholtz energy (Wikidata Q76359554))" + }, + { + "q": "Tell me the specific Helmholtz energy with Helmholtz free energy of 1.91195 and mass of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Helmholtz energy for [1.911952, 82.240072] (the system specific Helmholtz energy (Wikidata Q76359554))" + }, + { + "q": "What is the specific Helmholtz energy for Helmholtz free energy 1.64379 and mass 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Helmholtz energy for [1.643795, 252.709024] (the system specific Helmholtz energy (Wikidata Q76359554))" + }, + { + "q": "Tell me the specific Helmholtz energy with Helmholtz free energy of 1.64379 and mass of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Helmholtz energy for [1.643795, 252.709024] (the system specific Helmholtz energy (Wikidata Q76359554))" + }, + { + "q": "What is the specific Helmholtz energy for Helmholtz free energy 18.3169 and mass 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Helmholtz energy for [18.316867, 6.838598] (the system specific Helmholtz energy (Wikidata Q76359554))" + }, + { + "q": "Tell me the specific Helmholtz energy with Helmholtz free energy of 18.3169 and mass of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Helmholtz energy for [18.316867, 6.838598] (the system specific Helmholtz energy (Wikidata Q76359554))" + }, + { + "q": "What is the specific Helmholtz energy for Helmholtz free energy 156.502 and mass 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Helmholtz energy for [156.50181, 185.886273] (the system specific Helmholtz energy (Wikidata Q76359554))" + }, + { + "q": "Tell me the specific Helmholtz energy with Helmholtz free energy of 156.502 and mass of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Helmholtz energy for [156.50181, 185.886273] (the system specific Helmholtz energy (Wikidata Q76359554))" + }, + { + "q": "What is the specific Helmholtz energy for Helmholtz free energy 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Tell me the specific Helmholtz energy with Helmholtz free energy of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "What is the specific Helmholtz energy of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the specific Helmholtz energy of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the specific Helmholtz energy with Helmholtz free energy of -1.91195 and mass of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Helmholtz free energy: no example had one" + }, + { + "q": "Which specific Helmholtz energy is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the Helmholtz free energy is 8.50728 and the mass is 17.7888, what is the specific Helmholtz energy?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Helmholtz energy for [8.507283, 17.788778] (the system specific Helmholtz energy (Wikidata Q76359554))" + }, + { + "q": "Could you work out the specific Helmholtz energy given Helmholtz free energy = 8.50728, mass = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Helmholtz energy for [8.507283, 17.788778] (the system specific Helmholtz energy (Wikidata Q76359554))" + }, + { + "q": "I need the specific Helmholtz energy for a Helmholtz free energy of 8.24951 and a mass of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Helmholtz energy for [8.24951, 0.634021] (the system specific Helmholtz energy (Wikidata Q76359554))" + }, + { + "q": "If the Helmholtz free energy is 8.24951 and the mass is 0.634021, what is the specific Helmholtz energy?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Helmholtz energy for [8.24951, 0.634021] (the system specific Helmholtz energy (Wikidata Q76359554))" + }, + { + "q": "Could you work out the specific Helmholtz energy given Helmholtz free energy = 0.318734, mass = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Helmholtz energy for [0.318734, 9.566071] (the system specific Helmholtz energy (Wikidata Q76359554))" + }, + { + "q": "I need the specific Helmholtz energy for a Helmholtz free energy of 0.318734 and a mass of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Helmholtz energy for [0.318734, 9.566071] (the system specific Helmholtz energy (Wikidata Q76359554))" + }, + { + "q": "If the Helmholtz free energy is 0.671697 and the mass is 14.2496, what is the specific Helmholtz energy?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Helmholtz energy for [0.671697, 14.249569] (the system specific Helmholtz energy (Wikidata Q76359554))" + }, + { + "q": "Could you work out the specific Helmholtz energy given Helmholtz free energy = 0.671697, mass = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Helmholtz energy for [0.671697, 14.249569] (the system specific Helmholtz energy (Wikidata Q76359554))" + }, + { + "q": "I need the specific Helmholtz energy for a Helmholtz free energy of 21.7931 and a mass of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Helmholtz energy for [21.793126, 29.510609] (the system specific Helmholtz energy (Wikidata Q76359554))" + }, + { + "q": "If the Helmholtz free energy is 21.7931 and the mass is 29.5106, what is the specific Helmholtz energy?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Helmholtz energy for [21.793126, 29.510609] (the system specific Helmholtz energy (Wikidata Q76359554))" + }, + { + "q": "Could you work out the specific Helmholtz energy given Helmholtz free energy = 153.15, mass = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Helmholtz energy for [153.149965, 18.595315] (the system specific Helmholtz energy (Wikidata Q76359554))" + }, + { + "q": "I need the specific Helmholtz energy for a Helmholtz free energy of 153.15 and a mass of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific Helmholtz energy for [153.149965, 18.595315] (the system specific Helmholtz energy (Wikidata Q76359554))" + }, + { + "q": "If the Helmholtz free energy is 8.50728, what is the specific Helmholtz energy?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Could you work out the specific Helmholtz energy given Helmholtz free energy = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Do you know the specific Helmholtz energy of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the specific Helmholtz energy of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the specific Helmholtz energy given Helmholtz free energy = -0.318734, mass = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Helmholtz free energy: no example had one" + }, + { + "q": "What is a typical specific Helmholtz energy?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-specific-helmholtz-energy/program.py b/root/organs10/physics/specialists/solved-specific-helmholtz-energy/program.py new file mode 100644 index 0000000000000000000000000000000000000000..bb2e4b1bb9fbb1d5697d2d8640cc7e941d0da917 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-helmholtz-energy/program.py @@ -0,0 +1,7 @@ +"""specific Helmholtz energy from Helmholtz free energy, mass (specific Helmholtz energy (Wikidata Q76359554)) +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(helmholtz_free_energy, mass): + return 1.0*helmholtz_free_energy/mass diff --git a/root/organs10/physics/specialists/solved-specific-helmholtz-energy/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-specific-helmholtz-energy/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-helmholtz-energy/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-specific-impulse-by-mass/organ_formula_v2.py b/root/organs10/physics/specialists/solved-specific-impulse-by-mass/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-impulse-by-mass/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-specific-impulse-by-mass/panel.json b/root/organs10/physics/specialists/solved-specific-impulse-by-mass/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..89acea652c9bb5c73e2ae1378cf00810176025b3 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-impulse-by-mass/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the specific impulse by mass for mass flow rate 107.961 and thrust 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "m/s", + "why": "specific impulse by mass for [107.960666, 59.4105] (the system specific impulse by mass (Wikidata Q100793317))" + }, + { + "q": "Tell me the specific impulse by mass with mass flow rate of 107.961 and thrust of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "m/s", + "why": "specific impulse by mass for [107.960666, 59.4105] (the system specific impulse by mass (Wikidata Q100793317))" + }, + { + "q": "What is the specific impulse by mass for mass flow rate 169.527 and thrust 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "m/s", + "why": "specific impulse by mass for [169.526958, 280.773702] (the system specific impulse by mass (Wikidata Q100793317))" + }, + { + "q": "Tell me the specific impulse by mass with mass flow rate of 169.527 and thrust of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "m/s", + "why": "specific impulse by mass for [169.526958, 280.773702] (the system specific impulse by mass (Wikidata Q100793317))" + }, + { + "q": "What is the specific impulse by mass for mass flow rate 1.91195 and thrust 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "m/s", + "why": "specific impulse by mass for [1.911952, 82.240072] (the system specific impulse by mass (Wikidata Q100793317))" + }, + { + "q": "Tell me the specific impulse by mass with mass flow rate of 1.91195 and thrust of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "m/s", + "why": "specific impulse by mass for [1.911952, 82.240072] (the system specific impulse by mass (Wikidata Q100793317))" + }, + { + "q": "What is the specific impulse by mass for mass flow rate 1.64379 and thrust 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "m/s", + "why": "specific impulse by mass for [1.643795, 252.709024] (the system specific impulse by mass (Wikidata Q100793317))" + }, + { + "q": "Tell me the specific impulse by mass with mass flow rate of 1.64379 and thrust of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "m/s", + "why": "specific impulse by mass for [1.643795, 252.709024] (the system specific impulse by mass (Wikidata Q100793317))" + }, + { + "q": "What is the specific impulse by mass for mass flow rate 18.3169 and thrust 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "m/s", + "why": "specific impulse by mass for [18.316867, 6.838598] (the system specific impulse by mass (Wikidata Q100793317))" + }, + { + "q": "Tell me the specific impulse by mass with mass flow rate of 18.3169 and thrust of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "m/s", + "why": "specific impulse by mass for [18.316867, 6.838598] (the system specific impulse by mass (Wikidata Q100793317))" + }, + { + "q": "What is the specific impulse by mass for mass flow rate 156.502 and thrust 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "m/s", + "why": "specific impulse by mass for [156.50181, 185.886273] (the system specific impulse by mass (Wikidata Q100793317))" + }, + { + "q": "Tell me the specific impulse by mass with mass flow rate of 156.502 and thrust of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "m/s", + "why": "specific impulse by mass for [156.50181, 185.886273] (the system specific impulse by mass (Wikidata Q100793317))" + }, + { + "q": "What is the specific impulse by mass for mass flow rate 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the thrust is not given" + }, + { + "q": "Tell me the specific impulse by mass with mass flow rate of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the thrust is not given" + }, + { + "q": "What is the specific impulse by mass 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 specific impulse by mass 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 specific impulse by mass with mass flow rate of -1.91195 and thrust of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mass flow rate: no example had one" + }, + { + "q": "Which specific impulse by mass is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the mass flow rate is 8.50728 and the thrust is 17.7888, what is the specific impulse by mass?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "m/s", + "why": "specific impulse by mass for [8.507283, 17.788778] (the system specific impulse by mass (Wikidata Q100793317))" + }, + { + "q": "Could you work out the specific impulse by mass given mass flow rate = 8.50728, thrust = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "m/s", + "why": "specific impulse by mass for [8.507283, 17.788778] (the system specific impulse by mass (Wikidata Q100793317))" + }, + { + "q": "I need the specific impulse by mass for a mass flow rate of 8.24951 and a thrust of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "m/s", + "why": "specific impulse by mass for [8.24951, 0.634021] (the system specific impulse by mass (Wikidata Q100793317))" + }, + { + "q": "If the mass flow rate is 8.24951 and the thrust is 0.634021, what is the specific impulse by mass?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "m/s", + "why": "specific impulse by mass for [8.24951, 0.634021] (the system specific impulse by mass (Wikidata Q100793317))" + }, + { + "q": "Could you work out the specific impulse by mass given mass flow rate = 0.318734, thrust = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "m/s", + "why": "specific impulse by mass for [0.318734, 9.566071] (the system specific impulse by mass (Wikidata Q100793317))" + }, + { + "q": "I need the specific impulse by mass for a mass flow rate of 0.318734 and a thrust of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "m/s", + "why": "specific impulse by mass for [0.318734, 9.566071] (the system specific impulse by mass (Wikidata Q100793317))" + }, + { + "q": "If the mass flow rate is 0.671697 and the thrust is 14.2496, what is the specific impulse by mass?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "m/s", + "why": "specific impulse by mass for [0.671697, 14.249569] (the system specific impulse by mass (Wikidata Q100793317))" + }, + { + "q": "Could you work out the specific impulse by mass given mass flow rate = 0.671697, thrust = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "m/s", + "why": "specific impulse by mass for [0.671697, 14.249569] (the system specific impulse by mass (Wikidata Q100793317))" + }, + { + "q": "I need the specific impulse by mass for a mass flow rate of 21.7931 and a thrust of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "m/s", + "why": "specific impulse by mass for [21.793126, 29.510609] (the system specific impulse by mass (Wikidata Q100793317))" + }, + { + "q": "If the mass flow rate is 21.7931 and the thrust is 29.5106, what is the specific impulse by mass?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "m/s", + "why": "specific impulse by mass for [21.793126, 29.510609] (the system specific impulse by mass (Wikidata Q100793317))" + }, + { + "q": "Could you work out the specific impulse by mass given mass flow rate = 153.15, thrust = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "m/s", + "why": "specific impulse by mass for [153.149965, 18.595315] (the system specific impulse by mass (Wikidata Q100793317))" + }, + { + "q": "I need the specific impulse by mass for a mass flow rate of 153.15 and a thrust of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "m/s", + "why": "specific impulse by mass for [153.149965, 18.595315] (the system specific impulse by mass (Wikidata Q100793317))" + }, + { + "q": "If the mass flow rate is 8.50728, what is the specific impulse by mass?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the thrust is not given" + }, + { + "q": "Could you work out the specific impulse by mass given mass flow rate = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the thrust is not given" + }, + { + "q": "Do you know the specific impulse by mass 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 specific impulse by mass 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 specific impulse by mass given mass flow rate = -0.318734, thrust = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mass flow rate: no example had one" + }, + { + "q": "What is a typical specific impulse by mass?", + "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-specific-impulse-by-mass/program.py b/root/organs10/physics/specialists/solved-specific-impulse-by-mass/program.py new file mode 100644 index 0000000000000000000000000000000000000000..50437ae83c182ce7b3ce8d756142707f101c9c1d --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-impulse-by-mass/program.py @@ -0,0 +1,7 @@ +"""specific impulse by mass from mass flow rate, thrust (specific impulse by mass (Wikidata Q100793317)) +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(mass_flow_rate, thrust): + return 1.0*thrust/mass_flow_rate diff --git a/root/organs10/physics/specialists/solved-specific-impulse-by-mass/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-specific-impulse-by-mass/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-impulse-by-mass/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-specific-internal-energy/organ_formula_v2.py b/root/organs10/physics/specialists/solved-specific-internal-energy/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-internal-energy/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-specific-internal-energy/panel.json b/root/organs10/physics/specialists/solved-specific-internal-energy/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..20217224e7c89ae7be2a2e7f9dffd67de31b35ae --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-internal-energy/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the specific internal energy for internal energy 107.961 and mass 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific internal energy for [107.960666, 59.4105] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "Tell me the specific internal energy with internal energy of 107.961 and mass of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific internal energy for [107.960666, 59.4105] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "What is the specific internal energy for internal energy 169.527 and mass 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific internal energy for [169.526958, 280.773702] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "Tell me the specific internal energy with internal energy of 169.527 and mass of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific internal energy for [169.526958, 280.773702] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "What is the specific internal energy for internal energy 1.91195 and mass 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific internal energy for [1.911952, 82.240072] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "Tell me the specific internal energy with internal energy of 1.91195 and mass of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific internal energy for [1.911952, 82.240072] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "What is the specific internal energy for internal energy 1.64379 and mass 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific internal energy for [1.643795, 252.709024] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "Tell me the specific internal energy with internal energy of 1.64379 and mass of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific internal energy for [1.643795, 252.709024] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "What is the specific internal energy for internal energy 18.3169 and mass 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific internal energy for [18.316867, 6.838598] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "Tell me the specific internal energy with internal energy of 18.3169 and mass of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific internal energy for [18.316867, 6.838598] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "What is the specific internal energy for internal energy 156.502 and mass 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific internal energy for [156.50181, 185.886273] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "Tell me the specific internal energy with internal energy of 156.502 and mass of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific internal energy for [156.50181, 185.886273] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "What is the specific internal energy for internal energy 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Tell me the specific internal energy with internal energy of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "What is the specific internal energy of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the specific internal energy of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the specific internal energy with internal energy of -1.91195 and mass of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative internal energy: no example had one" + }, + { + "q": "Which specific internal energy is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the internal energy is 8.50728 and the mass is 17.7888, what is the specific internal energy?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific internal energy for [8.507283, 17.788778] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "Could you work out the specific internal energy given internal energy = 8.50728, mass = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific internal energy for [8.507283, 17.788778] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "I need the specific internal energy for an internal energy of 8.24951 and a mass of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific internal energy for [8.24951, 0.634021] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "If the internal energy is 8.24951 and the mass is 0.634021, what is the specific internal energy?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific internal energy for [8.24951, 0.634021] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "Could you work out the specific internal energy given internal energy = 0.318734, mass = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific internal energy for [0.318734, 9.566071] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "I need the specific internal energy for an internal energy of 0.318734 and a mass of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific internal energy for [0.318734, 9.566071] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "If the internal energy is 0.671697 and the mass is 14.2496, what is the specific internal energy?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific internal energy for [0.671697, 14.249569] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "Could you work out the specific internal energy given internal energy = 0.671697, mass = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific internal energy for [0.671697, 14.249569] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "I need the specific internal energy for an internal energy of 21.7931 and a mass of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific internal energy for [21.793126, 29.510609] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "If the internal energy is 21.7931 and the mass is 29.5106, what is the specific internal energy?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific internal energy for [21.793126, 29.510609] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "Could you work out the specific internal energy given internal energy = 153.15, mass = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific internal energy for [153.149965, 18.595315] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "I need the specific internal energy for an internal energy of 153.15 and a mass of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific internal energy for [153.149965, 18.595315] (the system specific internal energy (Wikidata Q76357367))" + }, + { + "q": "If the internal energy is 8.50728, what is the specific internal energy?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Could you work out the specific internal energy given internal energy = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Do you know the specific internal energy of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the specific internal energy of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the specific internal energy given internal energy = -0.318734, mass = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative internal energy: no example had one" + }, + { + "q": "What is a typical specific internal energy?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-specific-internal-energy/program.py b/root/organs10/physics/specialists/solved-specific-internal-energy/program.py new file mode 100644 index 0000000000000000000000000000000000000000..787ae2b9d86e6dd7e4a58147a1e6ed4518d088ae --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-internal-energy/program.py @@ -0,0 +1,7 @@ +"""specific internal energy from internal energy, mass (specific internal energy (Wikidata Q76357367)) +The maths organ (systems.py): solved from the system q0 = q1 / q2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(internal_energy, mass): + return 1.0*internal_energy/mass diff --git a/root/organs10/physics/specialists/solved-specific-internal-energy/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-specific-internal-energy/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-internal-energy/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-specific-latent-heat-of-phase-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-specific-latent-heat-of-phase-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-latent-heat-of-phase-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-specific-latent-heat-of-phase-2/panel.json b/root/organs10/physics/specialists/solved-specific-latent-heat-of-phase-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..8a7960521d083f16b45866d477d168154389b019 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-latent-heat-of-phase-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the specific latent heat of phase transition for latent heat of phase transition 107.961 and mass 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [107.960666, 59.4105] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "Tell me the specific latent heat of phase transition with latent heat of phase transition of 107.961 and mass of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [107.960666, 59.4105] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "What is the specific latent heat of phase transition for latent heat of phase transition 169.527 and mass 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [169.526958, 280.773702] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "Tell me the specific latent heat of phase transition with latent heat of phase transition of 169.527 and mass of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [169.526958, 280.773702] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "What is the specific latent heat of phase transition for latent heat of phase transition 1.91195 and mass 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [1.911952, 82.240072] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "Tell me the specific latent heat of phase transition with latent heat of phase transition of 1.91195 and mass of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [1.911952, 82.240072] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "What is the specific latent heat of phase transition for latent heat of phase transition 1.64379 and mass 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [1.643795, 252.709024] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "Tell me the specific latent heat of phase transition with latent heat of phase transition of 1.64379 and mass of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [1.643795, 252.709024] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "What is the specific latent heat of phase transition for latent heat of phase transition 18.3169 and mass 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [18.316867, 6.838598] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "Tell me the specific latent heat of phase transition with latent heat of phase transition of 18.3169 and mass of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [18.316867, 6.838598] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "What is the specific latent heat of phase transition for latent heat of phase transition 156.502 and mass 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [156.50181, 185.886273] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "Tell me the specific latent heat of phase transition with latent heat of phase transition of 156.502 and mass of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [156.50181, 185.886273] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "What is the specific latent heat of phase transition for latent heat of phase transition 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Tell me the specific latent heat of phase transition with latent heat of phase transition of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "What is the specific latent heat of phase transition of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the specific latent heat of phase transition of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the specific latent heat of phase transition with latent heat of phase transition of -1.91195 and mass of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative latent heat of phase transition: no example had one" + }, + { + "q": "Which specific latent heat of phase transition is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the latent heat of phase transition is 8.50728 and the mass is 17.7888, what is the specific latent heat of phase transition?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [8.507283, 17.788778] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "Could you work out the specific latent heat of phase transition given latent heat of phase transition = 8.50728, mass = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [8.507283, 17.788778] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "I need the specific latent heat of phase transition for a latent heat of phase transition of 8.24951 and a mass of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [8.24951, 0.634021] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "If the latent heat of phase transition is 8.24951 and the mass is 0.634021, what is the specific latent heat of phase transition?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [8.24951, 0.634021] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "Could you work out the specific latent heat of phase transition given latent heat of phase transition = 0.318734, mass = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [0.318734, 9.566071] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "I need the specific latent heat of phase transition for a latent heat of phase transition of 0.318734 and a mass of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [0.318734, 9.566071] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "If the latent heat of phase transition is 0.671697 and the mass is 14.2496, what is the specific latent heat of phase transition?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [0.671697, 14.249569] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "Could you work out the specific latent heat of phase transition given latent heat of phase transition = 0.671697, mass = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [0.671697, 14.249569] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "I need the specific latent heat of phase transition for a latent heat of phase transition of 21.7931 and a mass of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [21.793126, 29.510609] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "If the latent heat of phase transition is 21.7931 and the mass is 29.5106, what is the specific latent heat of phase transition?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [21.793126, 29.510609] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "Could you work out the specific latent heat of phase transition given latent heat of phase transition = 153.15, mass = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [153.149965, 18.595315] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "I need the specific latent heat of phase transition for a latent heat of phase transition of 153.15 and a mass of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [153.149965, 18.595315] (the system specific latent heat of phase transition (Wikidata Q104217205))" + }, + { + "q": "If the latent heat of phase transition is 8.50728, what is the specific latent heat of phase transition?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Could you work out the specific latent heat of phase transition given latent heat of phase transition = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Do you know the specific latent heat of phase transition of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the specific latent heat of phase transition of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the specific latent heat of phase transition given latent heat of phase transition = -0.318734, mass = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative latent heat of phase transition: no example had one" + }, + { + "q": "What is a typical specific latent heat of phase transition?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-specific-latent-heat-of-phase-2/program.py b/root/organs10/physics/specialists/solved-specific-latent-heat-of-phase-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..0e325e8c1b621c3f6a0b5ab8713115499bc1f9bb --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-latent-heat-of-phase-2/program.py @@ -0,0 +1,7 @@ +"""specific latent heat of phase transition from latent heat of phase transition, mass (specific latent heat of phase transition (Wikidata Q104217205)) +The maths organ (systems.py): solved from the system q1 = ((q0)/(q2)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(latent_heat_of_phase_transition, mass): + return 1.0*latent_heat_of_phase_transition/mass diff --git a/root/organs10/physics/specialists/solved-specific-latent-heat-of-phase-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-specific-latent-heat-of-phase-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-latent-heat-of-phase-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-specific-latent-heat-of-phase/organ_formula_v2.py b/root/organs10/physics/specialists/solved-specific-latent-heat-of-phase/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-latent-heat-of-phase/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-specific-latent-heat-of-phase/panel.json b/root/organs10/physics/specialists/solved-specific-latent-heat-of-phase/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..d459761be12ae21a108164c07da827f2f164a56f --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-latent-heat-of-phase/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the specific latent heat of phase transition for Stefan number 107.961, specific heat capacity at constant pressure 59.4105 and temperature difference 5.77717?", + "a": "3.1791600820256143", + "valid": true, + "kind": "compute", + "expect": 3.1791600820256143, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [107.960666, 59.4105, 5.777165] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Tell me the specific latent heat of phase transition with Stefan number of 107.961, specific heat capacity at constant pressure of 59.4105 and temperature difference of 5.77717.", + "a": "3.1791600820256143", + "valid": true, + "kind": "compute", + "expect": 3.1791600820256143, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [107.960666, 59.4105, 5.777165] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "What is the specific latent heat of phase transition for Stefan number 85.2416, specific heat capacity at constant pressure 160.878 and temperature difference 2.69428?", + "a": "5.084975341983345", + "valid": true, + "kind": "compute", + "expect": 5.084975341983345, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [85.241623, 160.878442, 2.69428] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Tell me the specific latent heat of phase transition with Stefan number of 85.2416, specific heat capacity at constant pressure of 160.878 and temperature difference of 2.69428.", + "a": "5.084975341983345", + "valid": true, + "kind": "compute", + "expect": 5.084975341983345, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [85.241623, 160.878442, 2.69428] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "What is the specific latent heat of phase transition for Stefan number 45.6157, specific heat capacity at constant pressure 4.97131 and temperature difference 94.305?", + "a": "10.27759497808088", + "valid": true, + "kind": "compute", + "expect": 10.27759497808088, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [45.61567, 4.971307, 94.305046] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Tell me the specific latent heat of phase transition with Stefan number of 45.6157, specific heat capacity at constant pressure of 4.97131 and temperature difference of 94.305.", + "a": "10.27759497808088", + "valid": true, + "kind": "compute", + "expect": 10.27759497808088, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [45.61567, 4.971307, 94.305046] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "What is the specific latent heat of phase transition for Stefan number 10.5638, specific heat capacity at constant pressure 198.839 and temperature difference 0.671697?", + "a": "12.643085733152049", + "valid": true, + "kind": "compute", + "expect": 12.643085733152049, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [10.563843, 198.838955, 0.671697] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Tell me the specific latent heat of phase transition with Stefan number of 10.5638, specific heat capacity at constant pressure of 198.839 and temperature difference of 0.671697.", + "a": "12.643085733152049", + "valid": true, + "kind": "compute", + "expect": 12.643085733152049, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [10.563843, 198.838955, 0.671697] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "What is the specific latent heat of phase transition for Stefan number 21.7931, specific heat capacity at constant pressure 29.5106 and temperature difference 8.50341?", + "a": "11.514672847514374", + "valid": true, + "kind": "compute", + "expect": 11.514672847514374, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [21.793126, 29.510609, 8.503407] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Tell me the specific latent heat of phase transition with Stefan number of 21.7931, specific heat capacity at constant pressure of 29.5106 and temperature difference of 8.50341.", + "a": "11.514672847514374", + "valid": true, + "kind": "compute", + "expect": 11.514672847514374, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [21.793126, 29.510609, 8.503407] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "What is the specific latent heat of phase transition for Stefan number 223.509, specific heat capacity at constant pressure 17.3414 and temperature difference 7.10705?", + "a": "0.5514164683902788", + "valid": true, + "kind": "compute", + "expect": 0.5514164683902788, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [223.508796, 17.341435, 7.107049] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Tell me the specific latent heat of phase transition with Stefan number of 223.509, specific heat capacity at constant pressure of 17.3414 and temperature difference of 7.10705.", + "a": "0.5514164683902788", + "valid": true, + "kind": "compute", + "expect": 0.5514164683902788, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [223.508796, 17.341435, 7.107049] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "What is the specific latent heat of phase transition for Stefan number 107.961 and specific heat capacity at constant pressure 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the temperature difference is not given" + }, + { + "q": "Tell me the specific latent heat of phase transition with Stefan number of 85.2416 and specific heat capacity at constant pressure of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the temperature difference is not given" + }, + { + "q": "What is the specific latent heat of phase transition of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the specific latent heat of phase transition of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the specific latent heat of phase transition with Stefan number of -45.6157, specific heat capacity at constant pressure of 4.97131 and temperature difference of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Stefan number: no example had one" + }, + { + "q": "Which specific latent heat of phase transition is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the Stefan number is 17.7888, specific heat capacity at constant pressure is 167.625 and the temperature difference is 10.3291, what is the specific latent heat of phase transition?", + "a": "97.3314301270093", + "valid": true, + "kind": "compute", + "expect": 97.3314301270093, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [17.788778, 167.624948, 10.329054] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Could you work out the specific latent heat of phase transition given Stefan number = 17.7888, specific heat capacity at constant pressure = 167.625, temperature difference = 10.3291?", + "a": "97.3314301270093", + "valid": true, + "kind": "compute", + "expect": 97.3314301270093, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [17.788778, 167.624948, 10.329054] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "I need the specific latent heat of phase transition for a Stefan number of 21.511, a specific heat capacity at constant pressure of 173.394 and a temperature difference of 251.084, please.", + "a": "2023.9106825818615", + "valid": true, + "kind": "compute", + "expect": 2023.9106825818615, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [21.511016, 173.393638, 251.084037] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "If the Stefan number is 21.511, specific heat capacity at constant pressure is 173.394 and the temperature difference is 251.084, what is the specific latent heat of phase transition?", + "a": "2023.9106825818615", + "valid": true, + "kind": "compute", + "expect": 2023.9106825818615, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [21.511016, 173.393638, 251.084037] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Could you work out the specific latent heat of phase transition given Stefan number = 129.245, specific heat capacity at constant pressure = 1.18359, temperature difference = 15.9416?", + "a": "0.1459888878289837", + "valid": true, + "kind": "compute", + "expect": 0.1459888878289837, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [129.244984, 1.18359, 15.941612] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "I need the specific latent heat of phase transition for a Stefan number of 129.245, a specific heat capacity at constant pressure of 1.18359 and a temperature difference of 15.9416, please.", + "a": "0.1459888878289837", + "valid": true, + "kind": "compute", + "expect": 0.1459888878289837, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [129.244984, 1.18359, 15.941612] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "If the Stefan number is 20.3966, specific heat capacity at constant pressure is 18.3169 and the temperature difference is 6.8386, what is the specific latent heat of phase transition?", + "a": "6.141315193814264", + "valid": true, + "kind": "compute", + "expect": 6.141315193814264, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [20.396558, 18.316867, 6.838598] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Could you work out the specific latent heat of phase transition given Stefan number = 20.3966, specific heat capacity at constant pressure = 18.3169, temperature difference = 6.8386?", + "a": "6.141315193814264", + "valid": true, + "kind": "compute", + "expect": 6.141315193814264, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [20.396558, 18.316867, 6.838598] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "I need the specific latent heat of phase transition for a Stefan number of 185.886, a specific heat capacity at constant pressure of 13.2373 and a temperature difference of 4.71941, please.", + "a": "0.3360784039769457", + "valid": true, + "kind": "compute", + "expect": 0.3360784039769457, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [185.886273, 13.237311, 4.719415] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "If the Stefan number is 185.886, specific heat capacity at constant pressure is 13.2373 and the temperature difference is 4.71941, what is the specific latent heat of phase transition?", + "a": "0.3360784039769457", + "valid": true, + "kind": "compute", + "expect": 0.3360784039769457, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [185.886273, 13.237311, 4.719415] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Could you work out the specific latent heat of phase transition given Stefan number = 9.1061, specific heat capacity at constant pressure = 24.5721, temperature difference = 108.451?", + "a": "292.64569674251265", + "valid": true, + "kind": "compute", + "expect": 292.64569674251265, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [9.106104, 24.572086, 108.450792] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "I need the specific latent heat of phase transition for a Stefan number of 9.1061, a specific heat capacity at constant pressure of 24.5721 and a temperature difference of 108.451, please.", + "a": "292.64569674251265", + "valid": true, + "kind": "compute", + "expect": 292.64569674251265, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific latent heat of phase transition for [9.106104, 24.572086, 108.450792] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "If the Stefan number is 17.7888 and the specific heat capacity at constant pressure is 167.625, what is the specific latent heat of phase transition?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the temperature difference is not given" + }, + { + "q": "Could you work out the specific latent heat of phase transition given Stefan number = 21.511, specific heat capacity at constant pressure = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the temperature difference is not given" + }, + { + "q": "Do you know the specific latent heat of phase transition of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the specific latent heat of phase transition of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the specific latent heat of phase transition given Stefan number = -129.245, specific heat capacity at constant pressure = 1.18359, temperature difference = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Stefan number: no example had one" + }, + { + "q": "What is a typical specific latent heat of phase transition?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-specific-latent-heat-of-phase/program.py b/root/organs10/physics/specialists/solved-specific-latent-heat-of-phase/program.py new file mode 100644 index 0000000000000000000000000000000000000000..2cb7b7b392e34366a3d7e76b226c9f50c8e43611 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-latent-heat-of-phase/program.py @@ -0,0 +1,7 @@ +"""specific latent heat of phase transition from Stefan number, specific heat capacity at constant pressure, temperature difference (Stefan number (Wikidata Q909876)) +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(stefan_number, specific_heat_capacity_at_constant_pressure, temperature_difference): + return 1.0*specific_heat_capacity_at_constant_pressure*temperature_difference/stefan_number diff --git a/root/organs10/physics/specialists/solved-specific-latent-heat-of-phase/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-specific-latent-heat-of-phase/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-latent-heat-of-phase/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-specific-modulus/organ_formula_v2.py b/root/organs10/physics/specialists/solved-specific-modulus/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-modulus/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-specific-modulus/panel.json b/root/organs10/physics/specialists/solved-specific-modulus/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..eb1a726b1fbe43fd6be0ca7a9cce8290c009168e --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-modulus/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the specific modulus for Young's modulus 107.961 and mass density 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific modulus for [107.960666, 59.4105] (the system specific modulus (Wikidata Q2309560))" + }, + { + "q": "Tell me the specific modulus with Young's modulus of 107.961 and mass density of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific modulus for [107.960666, 59.4105] (the system specific modulus (Wikidata Q2309560))" + }, + { + "q": "What is the specific modulus for Young's modulus 169.527 and mass density 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific modulus for [169.526958, 280.773702] (the system specific modulus (Wikidata Q2309560))" + }, + { + "q": "Tell me the specific modulus with Young's modulus of 169.527 and mass density of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific modulus for [169.526958, 280.773702] (the system specific modulus (Wikidata Q2309560))" + }, + { + "q": "What is the specific modulus for Young's modulus 1.91195 and mass density 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific modulus for [1.911952, 82.240072] (the system specific modulus (Wikidata Q2309560))" + }, + { + "q": "Tell me the specific modulus with Young's modulus of 1.91195 and mass density of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific modulus for [1.911952, 82.240072] (the system specific modulus (Wikidata Q2309560))" + }, + { + "q": "What is the specific modulus for Young's modulus 1.64379 and mass density 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific modulus for [1.643795, 252.709024] (the system specific modulus (Wikidata Q2309560))" + }, + { + "q": "Tell me the specific modulus with Young's modulus of 1.64379 and mass density of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific modulus for [1.643795, 252.709024] (the system specific modulus (Wikidata Q2309560))" + }, + { + "q": "What is the specific modulus for Young's modulus 18.3169 and mass density 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific modulus for [18.316867, 6.838598] (the system specific modulus (Wikidata Q2309560))" + }, + { + "q": "Tell me the specific modulus with Young's modulus of 18.3169 and mass density of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific modulus for [18.316867, 6.838598] (the system specific modulus (Wikidata Q2309560))" + }, + { + "q": "What is the specific modulus for Young's modulus 156.502 and mass density 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific modulus for [156.50181, 185.886273] (the system specific modulus (Wikidata Q2309560))" + }, + { + "q": "Tell me the specific modulus with Young's modulus of 156.502 and mass density of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific modulus for [156.50181, 185.886273] (the system specific modulus (Wikidata Q2309560))" + }, + { + "q": "What is the specific modulus for Young's modulus 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Tell me the specific modulus with Young's modulus of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "What is the specific modulus 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 specific modulus 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 specific modulus with Young's modulus of -1.91195 and mass density of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Young's modulus: no example had one" + }, + { + "q": "Which specific modulus is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the Young's modulus is 8.50728 and the mass density is 17.7888, what is the specific modulus?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific modulus for [8.507283, 17.788778] (the system specific modulus (Wikidata Q2309560))" + }, + { + "q": "Could you work out the specific modulus given Young's modulus = 8.50728, mass density = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific modulus for [8.507283, 17.788778] (the system specific modulus (Wikidata Q2309560))" + }, + { + "q": "I need the specific modulus for a Young's modulus of 8.24951 and a mass density of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific modulus for [8.24951, 0.634021] (the system specific modulus (Wikidata Q2309560))" + }, + { + "q": "If the Young's modulus is 8.24951 and the mass density is 0.634021, what is the specific modulus?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific modulus for [8.24951, 0.634021] (the system specific modulus (Wikidata Q2309560))" + }, + { + "q": "Could you work out the specific modulus given Young's modulus = 0.318734, mass density = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific modulus for [0.318734, 9.566071] (the system specific modulus (Wikidata Q2309560))" + }, + { + "q": "I need the specific modulus for a Young's modulus of 0.318734 and a mass density of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific modulus for [0.318734, 9.566071] (the system specific modulus (Wikidata Q2309560))" + }, + { + "q": "If the Young's modulus is 0.671697 and the mass density is 14.2496, what is the specific modulus?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific modulus for [0.671697, 14.249569] (the system specific modulus (Wikidata Q2309560))" + }, + { + "q": "Could you work out the specific modulus given Young's modulus = 0.671697, mass density = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific modulus for [0.671697, 14.249569] (the system specific modulus (Wikidata Q2309560))" + }, + { + "q": "I need the specific modulus for a Young's modulus of 21.7931 and a mass density of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific modulus for [21.793126, 29.510609] (the system specific modulus (Wikidata Q2309560))" + }, + { + "q": "If the Young's modulus is 21.7931 and the mass density is 29.5106, what is the specific modulus?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific modulus for [21.793126, 29.510609] (the system specific modulus (Wikidata Q2309560))" + }, + { + "q": "Could you work out the specific modulus given Young's modulus = 153.15, mass density = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific modulus for [153.149965, 18.595315] (the system specific modulus (Wikidata Q2309560))" + }, + { + "q": "I need the specific modulus for a Young's modulus of 153.15 and a mass density of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "specific modulus for [153.149965, 18.595315] (the system specific modulus (Wikidata Q2309560))" + }, + { + "q": "If the Young's modulus is 8.50728, what is the specific modulus?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Could you work out the specific modulus given Young's modulus = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Do you know the specific modulus 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 specific modulus 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 specific modulus given Young's modulus = -0.318734, mass density = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Young's modulus: no example had one" + }, + { + "q": "What is a typical specific modulus?", + "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-specific-modulus/program.py b/root/organs10/physics/specialists/solved-specific-modulus/program.py new file mode 100644 index 0000000000000000000000000000000000000000..90167b7f3fe6f1255cae1a67306e7d4ef126fe07 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-modulus/program.py @@ -0,0 +1,7 @@ +"""specific modulus from Young's modulus, mass density (specific modulus (Wikidata Q2309560)) +The maths organ (systems.py): solved from the system q0 = ((q1)/(q2)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(young_s_modulus, mass_density): + return 1.0*young_s_modulus/mass_density diff --git a/root/organs10/physics/specialists/solved-specific-modulus/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-specific-modulus/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-modulus/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-specific-rotation/organ_formula_v2.py b/root/organs10/physics/specialists/solved-specific-rotation/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-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-specific-rotation/panel.json b/root/organs10/physics/specialists/solved-specific-rotation/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..c4fef7c93e3b5cbb84a29b54b82ae45e9faf05ac --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-rotation/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the specific rotation for angle of optical rotation 107.961, cross-sectional area 59.4105 and mass 5.77717?", + "a": "1110.2327161569876", + "valid": true, + "kind": "compute", + "expect": 1110.2327161569876, + "dtype": "quantity", + "unit": "m^2/kg", + "why": "specific rotation for [107.960666, 59.4105, 5.777165] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Tell me the specific rotation with angle of optical rotation of 107.961, cross-sectional area of 59.4105 and mass of 5.77717.", + "a": "1110.2327161569876", + "valid": true, + "kind": "compute", + "expect": 1110.2327161569876, + "dtype": "quantity", + "unit": "m^2/kg", + "why": "specific rotation for [107.960666, 59.4105, 5.777165] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "What is the specific rotation for angle of optical rotation 85.2416, cross-sectional area 160.878 and mass 2.69428?", + "a": "5089.871888725082", + "valid": true, + "kind": "compute", + "expect": 5089.871888725082, + "dtype": "quantity", + "unit": "m^2/kg", + "why": "specific rotation for [85.241623, 160.878442, 2.69428] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Tell me the specific rotation with angle of optical rotation of 85.2416, cross-sectional area of 160.878 and mass of 2.69428.", + "a": "5089.871888725082", + "valid": true, + "kind": "compute", + "expect": 5089.871888725082, + "dtype": "quantity", + "unit": "m^2/kg", + "why": "specific rotation for [85.241623, 160.878442, 2.69428] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "What is the specific rotation for angle of optical rotation 45.6157, cross-sectional area 4.97131 and mass 94.305?", + "a": "2.404638268801381", + "valid": true, + "kind": "compute", + "expect": 2.404638268801381, + "dtype": "quantity", + "unit": "m^2/kg", + "why": "specific rotation for [45.61567, 4.971307, 94.305046] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Tell me the specific rotation with angle of optical rotation of 45.6157, cross-sectional area of 4.97131 and mass of 94.305.", + "a": "2.404638268801381", + "valid": true, + "kind": "compute", + "expect": 2.404638268801381, + "dtype": "quantity", + "unit": "m^2/kg", + "why": "specific rotation for [45.61567, 4.971307, 94.305046] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "What is the specific rotation for angle of optical rotation 10.5638, cross-sectional area 198.839 and mass 0.671697?", + "a": "3127.1582727254026", + "valid": true, + "kind": "compute", + "expect": 3127.1582727254026, + "dtype": "quantity", + "unit": "m^2/kg", + "why": "specific rotation for [10.563843, 198.838955, 0.671697] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Tell me the specific rotation with angle of optical rotation of 10.5638, cross-sectional area of 198.839 and mass of 0.671697.", + "a": "3127.1582727254026", + "valid": true, + "kind": "compute", + "expect": 3127.1582727254026, + "dtype": "quantity", + "unit": "m^2/kg", + "why": "specific rotation for [10.563843, 198.838955, 0.671697] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "What is the specific rotation for angle of optical rotation 21.7931, cross-sectional area 29.5106 and mass 8.50341?", + "a": "75.6318512050043", + "valid": true, + "kind": "compute", + "expect": 75.6318512050043, + "dtype": "quantity", + "unit": "m^2/kg", + "why": "specific rotation for [21.793126, 29.510609, 8.503407] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Tell me the specific rotation with angle of optical rotation of 21.7931, cross-sectional area of 29.5106 and mass of 8.50341.", + "a": "75.6318512050043", + "valid": true, + "kind": "compute", + "expect": 75.6318512050043, + "dtype": "quantity", + "unit": "m^2/kg", + "why": "specific rotation for [21.793126, 29.510609, 8.503407] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "What is the specific rotation for angle of optical rotation 223.509, cross-sectional area 17.3414 and mass 7.10705?", + "a": "545.3688543410761", + "valid": true, + "kind": "compute", + "expect": 545.3688543410761, + "dtype": "quantity", + "unit": "m^2/kg", + "why": "specific rotation for [223.508796, 17.341435, 7.107049] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Tell me the specific rotation with angle of optical rotation of 223.509, cross-sectional area of 17.3414 and mass of 7.10705.", + "a": "545.3688543410761", + "valid": true, + "kind": "compute", + "expect": 545.3688543410761, + "dtype": "quantity", + "unit": "m^2/kg", + "why": "specific rotation for [223.508796, 17.341435, 7.107049] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "What is the specific rotation for angle of optical 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 specific rotation with angle of optical 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 specific 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 specific 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 specific rotation with angle of optical 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 angle of optical rotation: no example had one" + }, + { + "q": "Which specific 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 angle of optical rotation is 17.7888, cross-sectional area is 167.625 and the mass is 10.3291, what is the specific rotation?", + "a": "288.6849919329577", + "valid": true, + "kind": "compute", + "expect": 288.6849919329577, + "dtype": "quantity", + "unit": "m^2/kg", + "why": "specific rotation for [17.788778, 167.624948, 10.329054] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Could you work out the specific rotation given angle of optical rotation = 17.7888, cross-sectional area = 167.625, mass = 10.3291?", + "a": "288.6849919329577", + "valid": true, + "kind": "compute", + "expect": 288.6849919329577, + "dtype": "quantity", + "unit": "m^2/kg", + "why": "specific rotation for [17.788778, 167.624948, 10.329054] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "I need the specific rotation for an angle of optical rotation of 21.511, a cross-sectional area of 173.394 and a mass of 251.084, please.", + "a": "14.855079375871984", + "valid": true, + "kind": "compute", + "expect": 14.855079375871984, + "dtype": "quantity", + "unit": "m^2/kg", + "why": "specific rotation for [21.511016, 173.393638, 251.084037] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "If the angle of optical rotation is 21.511, cross-sectional area is 173.394 and the mass is 251.084, what is the specific rotation?", + "a": "14.855079375871984", + "valid": true, + "kind": "compute", + "expect": 14.855079375871984, + "dtype": "quantity", + "unit": "m^2/kg", + "why": "specific rotation for [21.511016, 173.393638, 251.084037] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Could you work out the specific rotation given angle of optical rotation = 129.245, cross-sectional area = 1.18359, mass = 15.9416?", + "a": "9.59583449816029", + "valid": true, + "kind": "compute", + "expect": 9.59583449816029, + "dtype": "quantity", + "unit": "m^2/kg", + "why": "specific rotation for [129.244984, 1.18359, 15.941612] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "I need the specific rotation for an angle of optical rotation of 129.245, a cross-sectional area of 1.18359 and a mass of 15.9416, please.", + "a": "9.59583449816029", + "valid": true, + "kind": "compute", + "expect": 9.59583449816029, + "dtype": "quantity", + "unit": "m^2/kg", + "why": "specific rotation for [129.244984, 1.18359, 15.941612] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "If the angle of optical rotation is 20.3966, cross-sectional area is 18.3169 and the mass is 6.8386, what is the specific rotation?", + "a": "54.63123238944198", + "valid": true, + "kind": "compute", + "expect": 54.63123238944198, + "dtype": "quantity", + "unit": "m^2/kg", + "why": "specific rotation for [20.396558, 18.316867, 6.838598] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Could you work out the specific rotation given angle of optical rotation = 20.3966, cross-sectional area = 18.3169, mass = 6.8386?", + "a": "54.63123238944198", + "valid": true, + "kind": "compute", + "expect": 54.63123238944198, + "dtype": "quantity", + "unit": "m^2/kg", + "why": "specific rotation for [20.396558, 18.316867, 6.838598] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "I need the specific rotation for an angle of optical rotation of 185.886, a cross-sectional area of 13.2373 and a mass of 4.71941, please.", + "a": "521.3854934623429", + "valid": true, + "kind": "compute", + "expect": 521.3854934623429, + "dtype": "quantity", + "unit": "m^2/kg", + "why": "specific rotation for [185.886273, 13.237311, 4.719415] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "If the angle of optical rotation is 185.886, cross-sectional area is 13.2373 and the mass is 4.71941, what is the specific rotation?", + "a": "521.3854934623429", + "valid": true, + "kind": "compute", + "expect": 521.3854934623429, + "dtype": "quantity", + "unit": "m^2/kg", + "why": "specific rotation for [185.886273, 13.237311, 4.719415] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "Could you work out the specific rotation given angle of optical rotation = 9.1061, cross-sectional area = 24.5721, mass = 108.451?", + "a": "2.0632027126358947", + "valid": true, + "kind": "compute", + "expect": 2.0632027126358947, + "dtype": "quantity", + "unit": "m^2/kg", + "why": "specific rotation for [9.106104, 24.572086, 108.450792] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "I need the specific rotation for an angle of optical rotation of 9.1061, a cross-sectional area of 24.5721 and a mass of 108.451, please.", + "a": "2.0632027126358947", + "valid": true, + "kind": "compute", + "expect": 2.0632027126358947, + "dtype": "quantity", + "unit": "m^2/kg", + "why": "specific rotation for [9.106104, 24.572086, 108.450792] (the system specific rotation (Wikidata Q2191631))" + }, + { + "q": "If the angle of optical rotation is 17.7888 and the cross-sectional area is 167.625, what is the specific rotation?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Could you work out the specific rotation given angle of optical 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 specific 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 specific 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 specific rotation given angle of optical 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 angle of optical rotation: no example had one" + }, + { + "q": "What is a typical specific 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-specific-rotation/program.py b/root/organs10/physics/specialists/solved-specific-rotation/program.py new file mode 100644 index 0000000000000000000000000000000000000000..5fdf394348ae25e079f52e22e09943747863905e --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-rotation/program.py @@ -0,0 +1,7 @@ +"""specific rotation from angle of optical 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(angle_of_optical_rotation, cross_sectional_area, mass): + return 1.0*angle_of_optical_rotation*cross_sectional_area/mass diff --git a/root/organs10/physics/specialists/solved-specific-rotation/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-specific-rotation/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-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-specific-volume/organ_formula_v2.py b/root/organs10/physics/specialists/solved-specific-volume/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-volume/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-specific-volume/panel.json b/root/organs10/physics/specialists/solved-specific-volume/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..918c540895130ba215816642e918d73eeacd8b23 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-volume/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the specific volume for mass density 107.961?", + "a": "0.00926263275385168", + "valid": true, + "kind": "compute", + "expect": 0.00926263275385168, + "dtype": "quantity", + "unit": "m^3/kg", + "why": "specific volume for [107.960666] (the system specific volume (Wikidata Q683556))" + }, + { + "q": "Tell me the specific volume with mass density of 107.961.", + "a": "0.00926263275385168", + "valid": true, + "kind": "compute", + "expect": 0.00926263275385168, + "dtype": "quantity", + "unit": "m^3/kg", + "why": "specific volume for [107.960666] (the system specific volume (Wikidata Q683556))" + }, + { + "q": "What is the specific volume for mass density 22.652?", + "a": "0.044146167826513254", + "valid": true, + "kind": "compute", + "expect": 0.044146167826513254, + "dtype": "quantity", + "unit": "m^3/kg", + "why": "specific volume for [22.652023] (the system specific volume (Wikidata Q683556))" + }, + { + "q": "Tell me the specific volume with mass density of 22.652.", + "a": "0.044146167826513254", + "valid": true, + "kind": "compute", + "expect": 0.044146167826513254, + "dtype": "quantity", + "unit": "m^3/kg", + "why": "specific volume for [22.652023] (the system specific volume (Wikidata Q683556))" + }, + { + "q": "What is the specific volume for mass density 173.394?", + "a": "0.0057672242718762815", + "valid": true, + "kind": "compute", + "expect": 0.0057672242718762815, + "dtype": "quantity", + "unit": "m^3/kg", + "why": "specific volume for [173.393638] (the system specific volume (Wikidata Q683556))" + }, + { + "q": "Tell me the specific volume with mass density of 173.394.", + "a": "0.0057672242718762815", + "valid": true, + "kind": "compute", + "expect": 0.0057672242718762815, + "dtype": "quantity", + "unit": "m^3/kg", + "why": "specific volume for [173.393638] (the system specific volume (Wikidata Q683556))" + }, + { + "q": "What is the specific volume for mass density 126.709?", + "a": "0.007892083536070259", + "valid": true, + "kind": "compute", + "expect": 0.007892083536070259, + "dtype": "quantity", + "unit": "m^3/kg", + "why": "specific volume for [126.709252] (the system specific volume (Wikidata Q683556))" + }, + { + "q": "Tell me the specific volume with mass density of 126.709.", + "a": "0.007892083536070259", + "valid": true, + "kind": "compute", + "expect": 0.007892083536070259, + "dtype": "quantity", + "unit": "m^3/kg", + "why": "specific volume for [126.709252] (the system specific volume (Wikidata Q683556))" + }, + { + "q": "What is the specific volume for mass density 0.671697?", + "a": "1.4887660694527203", + "valid": true, + "kind": "compute", + "expect": 1.4887660694527203, + "dtype": "quantity", + "unit": "m^3/kg", + "why": "specific volume for [0.671697] (the system specific volume (Wikidata Q683556))" + }, + { + "q": "Tell me the specific volume with mass density of 0.671697.", + "a": "1.4887660694527203", + "valid": true, + "kind": "compute", + "expect": 1.4887660694527203, + "dtype": "quantity", + "unit": "m^3/kg", + "why": "specific volume for [0.671697] (the system specific volume (Wikidata Q683556))" + }, + { + "q": "What is the specific volume for mass density 1.16997?", + "a": "0.8547242274066585", + "valid": true, + "kind": "compute", + "expect": 0.8547242274066585, + "dtype": "quantity", + "unit": "m^3/kg", + "why": "specific volume for [1.169968] (the system specific volume (Wikidata Q683556))" + }, + { + "q": "Tell me the specific volume with mass density of 1.16997.", + "a": "0.8547242274066585", + "valid": true, + "kind": "compute", + "expect": 0.8547242274066585, + "dtype": "quantity", + "unit": "m^3/kg", + "why": "specific volume for [1.169968] (the system specific volume (Wikidata Q683556))" + }, + { + "q": "What is the specific volume?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Could you work out the specific volume, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "What is the specific volume 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 specific volume 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 specific volume with mass density of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mass density: no example had one" + }, + { + "q": "Which specific volume is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the mass density is 2.57003, what is the specific volume?", + "a": "0.38910052946214563", + "valid": true, + "kind": "compute", + "expect": 0.38910052946214563, + "dtype": "quantity", + "unit": "m^3/kg", + "why": "specific volume for [2.57003] (the system specific volume (Wikidata Q683556))" + }, + { + "q": "Could you work out the specific volume given mass density = 2.57003?", + "a": "0.38910052946214563", + "valid": true, + "kind": "compute", + "expect": 0.38910052946214563, + "dtype": "quantity", + "unit": "m^3/kg", + "why": "specific volume for [2.57003] (the system specific volume (Wikidata Q683556))" + }, + { + "q": "I need the specific volume for a mass density of 48.921, please.", + "a": "0.02044112317474344", + "valid": true, + "kind": "compute", + "expect": 0.02044112317474344, + "dtype": "quantity", + "unit": "m^3/kg", + "why": "specific volume for [48.920991] (the system specific volume (Wikidata Q683556))" + }, + { + "q": "If the mass density is 48.921, what is the specific volume?", + "a": "0.02044112317474344", + "valid": true, + "kind": "compute", + "expect": 0.02044112317474344, + "dtype": "quantity", + "unit": "m^3/kg", + "why": "specific volume for [48.920991] (the system specific volume (Wikidata Q683556))" + }, + { + "q": "Could you work out the specific volume given mass density = 0.348438?", + "a": "2.869953759769417", + "valid": true, + "kind": "compute", + "expect": 2.869953759769417, + "dtype": "quantity", + "unit": "m^3/kg", + "why": "specific volume for [0.348438] (the system specific volume (Wikidata Q683556))" + }, + { + "q": "I need the specific volume for a mass density of 0.348438, please.", + "a": "2.869953759769417", + "valid": true, + "kind": "compute", + "expect": 2.869953759769417, + "dtype": "quantity", + "unit": "m^3/kg", + "why": "specific volume for [0.348438] (the system specific volume (Wikidata Q683556))" + }, + { + "q": "If the mass density is 252.709, what is the specific volume?", + "a": "0.0039571202610184176", + "valid": true, + "kind": "compute", + "expect": 0.0039571202610184176, + "dtype": "quantity", + "unit": "m^3/kg", + "why": "specific volume for [252.709024] (the system specific volume (Wikidata Q683556))" + }, + { + "q": "Could you work out the specific volume given mass density = 252.709?", + "a": "0.0039571202610184176", + "valid": true, + "kind": "compute", + "expect": 0.0039571202610184176, + "dtype": "quantity", + "unit": "m^3/kg", + "why": "specific volume for [252.709024] (the system specific volume (Wikidata Q683556))" + }, + { + "q": "I need the specific volume for a mass density of 20.3966, please.", + "a": "0.04902788018766951", + "valid": true, + "kind": "compute", + "expect": 0.04902788018766951, + "dtype": "quantity", + "unit": "m^3/kg", + "why": "specific volume for [20.396558] (the system specific volume (Wikidata Q683556))" + }, + { + "q": "If the mass density is 20.3966, what is the specific volume?", + "a": "0.04902788018766951", + "valid": true, + "kind": "compute", + "expect": 0.04902788018766951, + "dtype": "quantity", + "unit": "m^3/kg", + "why": "specific volume for [20.396558] (the system specific volume (Wikidata Q683556))" + }, + { + "q": "Could you work out the specific volume given mass density = 134.993?", + "a": "0.007407793842889246", + "valid": true, + "kind": "compute", + "expect": 0.007407793842889246, + "dtype": "quantity", + "unit": "m^3/kg", + "why": "specific volume for [134.992958] (the system specific volume (Wikidata Q683556))" + }, + { + "q": "I need the specific volume for a mass density of 134.993, please.", + "a": "0.007407793842889246", + "valid": true, + "kind": "compute", + "expect": 0.007407793842889246, + "dtype": "quantity", + "unit": "m^3/kg", + "why": "specific volume for [134.992958] (the system specific volume (Wikidata Q683556))" + }, + { + "q": "I need the specific volume, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Can you tell me the specific volume?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Do you know the specific volume 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 specific volume 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 specific volume given mass density = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mass density: no example had one" + }, + { + "q": "What is a typical specific volume?", + "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-specific-volume/program.py b/root/organs10/physics/specialists/solved-specific-volume/program.py new file mode 100644 index 0000000000000000000000000000000000000000..48e61de6e36325bb10af2134a3da7dbd2153fbb0 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-volume/program.py @@ -0,0 +1,7 @@ +"""specific volume from mass density (specific volume (Wikidata Q683556)) +The maths organ (systems.py): solved from the system q0 = 1 / q1; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(mass_density): + return 1.0/mass_density diff --git a/root/organs10/physics/specialists/solved-specific-volume/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-specific-volume/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-specific-volume/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-spectral-emissivity/organ_formula_v2.py b/root/organs10/physics/specialists/solved-spectral-emissivity/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-spectral-emissivity/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-spectral-emissivity/panel.json b/root/organs10/physics/specialists/solved-spectral-emissivity/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..dfaea61e62d37f5308e24b6146b5e22726167a9c --- /dev/null +++ b/root/organs10/physics/specialists/solved-spectral-emissivity/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the spectral emissivity for radiant exitance of a Planckian radiator 107.961 and radiant exitance 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "1", + "why": "spectral emissivity for [107.960666, 59.4105] (the system spectral emissivity (Wikidata Q84710157))" + }, + { + "q": "Tell me the spectral emissivity with radiant exitance of a Planckian radiator of 107.961 and radiant exitance of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "1", + "why": "spectral emissivity for [107.960666, 59.4105] (the system spectral emissivity (Wikidata Q84710157))" + }, + { + "q": "What is the spectral emissivity for radiant exitance of a Planckian radiator 169.527 and radiant exitance 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "1", + "why": "spectral emissivity for [169.526958, 280.773702] (the system spectral emissivity (Wikidata Q84710157))" + }, + { + "q": "Tell me the spectral emissivity with radiant exitance of a Planckian radiator of 169.527 and radiant exitance of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "1", + "why": "spectral emissivity for [169.526958, 280.773702] (the system spectral emissivity (Wikidata Q84710157))" + }, + { + "q": "What is the spectral emissivity for radiant exitance of a Planckian radiator 1.91195 and radiant exitance 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "1", + "why": "spectral emissivity for [1.911952, 82.240072] (the system spectral emissivity (Wikidata Q84710157))" + }, + { + "q": "Tell me the spectral emissivity with radiant exitance of a Planckian radiator of 1.91195 and radiant exitance of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "1", + "why": "spectral emissivity for [1.911952, 82.240072] (the system spectral emissivity (Wikidata Q84710157))" + }, + { + "q": "What is the spectral emissivity for radiant exitance of a Planckian radiator 1.64379 and radiant exitance 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "1", + "why": "spectral emissivity for [1.643795, 252.709024] (the system spectral emissivity (Wikidata Q84710157))" + }, + { + "q": "Tell me the spectral emissivity with radiant exitance of a Planckian radiator of 1.64379 and radiant exitance of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "1", + "why": "spectral emissivity for [1.643795, 252.709024] (the system spectral emissivity (Wikidata Q84710157))" + }, + { + "q": "What is the spectral emissivity for radiant exitance of a Planckian radiator 18.3169 and radiant exitance 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "1", + "why": "spectral emissivity for [18.316867, 6.838598] (the system spectral emissivity (Wikidata Q84710157))" + }, + { + "q": "Tell me the spectral emissivity with radiant exitance of a Planckian radiator of 18.3169 and radiant exitance of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "1", + "why": "spectral emissivity for [18.316867, 6.838598] (the system spectral emissivity (Wikidata Q84710157))" + }, + { + "q": "What is the spectral emissivity for radiant exitance of a Planckian radiator 156.502 and radiant exitance 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "1", + "why": "spectral emissivity for [156.50181, 185.886273] (the system spectral emissivity (Wikidata Q84710157))" + }, + { + "q": "Tell me the spectral emissivity with radiant exitance of a Planckian radiator of 156.502 and radiant exitance of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "1", + "why": "spectral emissivity for [156.50181, 185.886273] (the system spectral emissivity (Wikidata Q84710157))" + }, + { + "q": "What is the spectral emissivity for radiant exitance of a Planckian radiator 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radiant exitance is not given" + }, + { + "q": "Tell me the spectral emissivity with radiant exitance of a Planckian radiator of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radiant exitance is not given" + }, + { + "q": "What is the spectral emissivity 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 spectral emissivity 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 spectral emissivity with radiant exitance of a Planckian radiator of -1.91195 and radiant exitance of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative radiant exitance of a Planckian radiator: no example had one" + }, + { + "q": "Which spectral emissivity 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 radiant exitance of a Planckian radiator is 8.50728 and the radiant exitance is 17.7888, what is the spectral emissivity?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "1", + "why": "spectral emissivity for [8.507283, 17.788778] (the system spectral emissivity (Wikidata Q84710157))" + }, + { + "q": "Could you work out the spectral emissivity given radiant exitance of a Planckian radiator = 8.50728, radiant exitance = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "1", + "why": "spectral emissivity for [8.507283, 17.788778] (the system spectral emissivity (Wikidata Q84710157))" + }, + { + "q": "I need the spectral emissivity for a radiant exitance of a Planckian radiator of 8.24951 and a radiant exitance of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "1", + "why": "spectral emissivity for [8.24951, 0.634021] (the system spectral emissivity (Wikidata Q84710157))" + }, + { + "q": "If the radiant exitance of a Planckian radiator is 8.24951 and the radiant exitance is 0.634021, what is the spectral emissivity?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "1", + "why": "spectral emissivity for [8.24951, 0.634021] (the system spectral emissivity (Wikidata Q84710157))" + }, + { + "q": "Could you work out the spectral emissivity given radiant exitance of a Planckian radiator = 0.318734, radiant exitance = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "1", + "why": "spectral emissivity for [0.318734, 9.566071] (the system spectral emissivity (Wikidata Q84710157))" + }, + { + "q": "I need the spectral emissivity for a radiant exitance of a Planckian radiator of 0.318734 and a radiant exitance of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "1", + "why": "spectral emissivity for [0.318734, 9.566071] (the system spectral emissivity (Wikidata Q84710157))" + }, + { + "q": "If the radiant exitance of a Planckian radiator is 0.671697 and the radiant exitance is 14.2496, what is the spectral emissivity?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "1", + "why": "spectral emissivity for [0.671697, 14.249569] (the system spectral emissivity (Wikidata Q84710157))" + }, + { + "q": "Could you work out the spectral emissivity given radiant exitance of a Planckian radiator = 0.671697, radiant exitance = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "1", + "why": "spectral emissivity for [0.671697, 14.249569] (the system spectral emissivity (Wikidata Q84710157))" + }, + { + "q": "I need the spectral emissivity for a radiant exitance of a Planckian radiator of 21.7931 and a radiant exitance of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "1", + "why": "spectral emissivity for [21.793126, 29.510609] (the system spectral emissivity (Wikidata Q84710157))" + }, + { + "q": "If the radiant exitance of a Planckian radiator is 21.7931 and the radiant exitance is 29.5106, what is the spectral emissivity?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "1", + "why": "spectral emissivity for [21.793126, 29.510609] (the system spectral emissivity (Wikidata Q84710157))" + }, + { + "q": "Could you work out the spectral emissivity given radiant exitance of a Planckian radiator = 153.15, radiant exitance = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "1", + "why": "spectral emissivity for [153.149965, 18.595315] (the system spectral emissivity (Wikidata Q84710157))" + }, + { + "q": "I need the spectral emissivity for a radiant exitance of a Planckian radiator of 153.15 and a radiant exitance of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "1", + "why": "spectral emissivity for [153.149965, 18.595315] (the system spectral emissivity (Wikidata Q84710157))" + }, + { + "q": "If the radiant exitance of a Planckian radiator is 8.50728, what is the spectral emissivity?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radiant exitance is not given" + }, + { + "q": "Could you work out the spectral emissivity given radiant exitance of a Planckian radiator = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radiant exitance is not given" + }, + { + "q": "Do you know the spectral emissivity 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 spectral emissivity 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 spectral emissivity given radiant exitance of a Planckian radiator = -0.318734, radiant exitance = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative radiant exitance of a Planckian radiator: no example had one" + }, + { + "q": "What is a typical spectral emissivity?", + "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-spectral-emissivity/program.py b/root/organs10/physics/specialists/solved-spectral-emissivity/program.py new file mode 100644 index 0000000000000000000000000000000000000000..2439ad42ea3ac30a4bb0e18768726377f98067ef --- /dev/null +++ b/root/organs10/physics/specialists/solved-spectral-emissivity/program.py @@ -0,0 +1,7 @@ +"""spectral emissivity from radiant exitance of a Planckian radiator, radiant exitance (spectral emissivity (Wikidata Q84710157)) +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(radiant_exitance_of_a_planckian_radiator, radiant_exitance): + return 1.0*radiant_exitance/radiant_exitance_of_a_planckian_radiator diff --git a/root/organs10/physics/specialists/solved-spectral-emissivity/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-spectral-emissivity/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-spectral-emissivity/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-spectral-luminous-efficiency--2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-spectral-luminous-efficiency--2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-spectral-luminous-efficiency--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-spectral-luminous-efficiency--2/panel.json b/root/organs10/physics/specialists/solved-spectral-luminous-efficiency--2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..be35313a435be4d42399cdcbeaf9e2e4abf90c0f --- /dev/null +++ b/root/organs10/physics/specialists/solved-spectral-luminous-efficiency--2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the spectral luminous efficiency for scotopic vision for spectral luminous efficiency for mesopic vision 2.21571, spectral luminous efficiency for photopic vision 58.5346, normalizing function for mesopic vision 22.652 and adaptation coefficient for mesopic vision 1.78451?", + "a": "69.17079784369166", + "valid": true, + "kind": "compute", + "expect": 69.17079784369166, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for scotopic vision for [2.215713, 58.534594, 22.652023, 1.78451] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Tell me the spectral luminous efficiency for scotopic vision with spectral luminous efficiency for mesopic vision of 2.21571, spectral luminous efficiency for photopic vision of 58.5346, normalizing function for mesopic vision of 22.652 and adaptation coefficient for mesopic vision of 1.78451.", + "a": "69.17079784369166", + "valid": true, + "kind": "compute", + "expect": 69.17079784369166, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for scotopic vision for [2.215713, 58.534594, 22.652023, 1.78451] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "What is the spectral luminous efficiency for scotopic vision for spectral luminous efficiency for mesopic vision 2.9896, spectral luminous efficiency for photopic vision 129.245, normalizing function for mesopic vision 1.18359 and adaptation coefficient for mesopic vision 15.9416?", + "a": "137.65816750566935", + "valid": true, + "kind": "compute", + "expect": 137.65816750566935, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for scotopic vision for [2.9896, 129.244984, 1.18359, 15.941612] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Tell me the spectral luminous efficiency for scotopic vision with spectral luminous efficiency for mesopic vision of 2.9896, spectral luminous efficiency for photopic vision of 129.245, normalizing function for mesopic vision of 1.18359 and adaptation coefficient for mesopic vision of 15.9416.", + "a": "137.65816750566935", + "valid": true, + "kind": "compute", + "expect": 137.65816750566935, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for scotopic vision for [2.9896, 129.244984, 1.18359, 15.941612] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "What is the spectral luminous efficiency for scotopic vision for spectral luminous efficiency for mesopic vision 21.7931, spectral luminous efficiency for photopic vision 29.5106, normalizing function for mesopic vision 8.50341 and adaptation coefficient for mesopic vision 0.58813?", + "a": "407.7976704008104", + "valid": true, + "kind": "compute", + "expect": 407.7976704008104, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for scotopic vision for [21.793126, 29.510609, 8.503407, 0.58813] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Tell me the spectral luminous efficiency for scotopic vision with spectral luminous efficiency for mesopic vision of 21.7931, spectral luminous efficiency for photopic vision of 29.5106, normalizing function for mesopic vision of 8.50341 and adaptation coefficient for mesopic vision of 0.58813.", + "a": "407.7976704008104", + "valid": true, + "kind": "compute", + "expect": 407.7976704008104, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for scotopic vision for [21.793126, 29.510609, 8.503407, 0.58813] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "What is the spectral luminous efficiency for scotopic vision for spectral luminous efficiency for mesopic vision 0.432217, spectral luminous efficiency for photopic vision 16.6571, normalizing function for mesopic vision 169.843 and adaptation coefficient for mesopic vision 12.6647?", + "a": "11.791752067430172", + "valid": true, + "kind": "compute", + "expect": 11.791752067430172, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for scotopic vision for [0.432217, 16.657051, 169.843153, 12.664671] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Tell me the spectral luminous efficiency for scotopic vision with spectral luminous efficiency for mesopic vision of 0.432217, spectral luminous efficiency for photopic vision of 16.6571, normalizing function for mesopic vision of 169.843 and adaptation coefficient for mesopic vision of 12.6647.", + "a": "11.791752067430172", + "valid": true, + "kind": "compute", + "expect": 11.791752067430172, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for scotopic vision for [0.432217, 16.657051, 169.843153, 12.664671] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "What is the spectral luminous efficiency for scotopic vision for spectral luminous efficiency for mesopic vision 10.2544, spectral luminous efficiency for photopic vision 0.415084, normalizing function for mesopic vision 0.634985 and adaptation coefficient for mesopic vision 291.545?", + "a": "0.39410182711289377", + "valid": true, + "kind": "compute", + "expect": 0.39410182711289377, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for scotopic vision for [10.254383, 0.415084, 0.634985, 291.544898] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Tell me the spectral luminous efficiency for scotopic vision with spectral luminous efficiency for mesopic vision of 10.2544, spectral luminous efficiency for photopic vision of 0.415084, normalizing function for mesopic vision of 0.634985 and adaptation coefficient for mesopic vision of 291.545.", + "a": "0.39410182711289377", + "valid": true, + "kind": "compute", + "expect": 0.39410182711289377, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for scotopic vision for [10.254383, 0.415084, 0.634985, 291.544898] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "What is the spectral luminous efficiency for scotopic vision for spectral luminous efficiency for mesopic vision 0.4336, spectral luminous efficiency for photopic vision 2.20115, normalizing function for mesopic vision 1.66108 and adaptation coefficient for mesopic vision 228.764?", + "a": "2.207651173679303", + "valid": true, + "kind": "compute", + "expect": 2.207651173679303, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for scotopic vision for [0.4336, 2.201149, 1.661083, 228.764178] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Tell me the spectral luminous efficiency for scotopic vision with spectral luminous efficiency for mesopic vision of 0.4336, spectral luminous efficiency for photopic vision of 2.20115, normalizing function for mesopic vision of 1.66108 and adaptation coefficient for mesopic vision of 228.764.", + "a": "2.207651173679303", + "valid": true, + "kind": "compute", + "expect": 2.207651173679303, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for scotopic vision for [0.4336, 2.201149, 1.661083, 228.764178] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "What is the spectral luminous efficiency for scotopic vision for spectral luminous efficiency for mesopic vision 2.21571, spectral luminous efficiency for photopic vision 58.5346 and normalizing function for mesopic vision 22.652?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the adaptation coefficient for mesopic vision is not given" + }, + { + "q": "Tell me the spectral luminous efficiency for scotopic vision with spectral luminous efficiency for mesopic vision of 2.9896, spectral luminous efficiency for photopic vision of 129.245 and normalizing function for mesopic vision of 1.18359.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the adaptation coefficient for mesopic vision is not given" + }, + { + "q": "What is the spectral luminous efficiency for scotopic vision 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 spectral luminous efficiency for scotopic vision 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 spectral luminous efficiency for scotopic vision with spectral luminous efficiency for mesopic vision of -21.7931, spectral luminous efficiency for photopic vision of 29.5106, normalizing function for mesopic vision of 8.50341 and adaptation coefficient for mesopic vision of 0.58813.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative spectral luminous efficiency for mesopic vision: no example had one" + }, + { + "q": "Which spectral luminous efficiency for scotopic vision 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 spectral luminous efficiency for mesopic vision is 157.223, spectral luminous efficiency for photopic vision is 35.6412, normalizing function for mesopic vision is 8.24951 and the adaptation coefficient for mesopic vision is 0.634021, what is the spectral luminous efficiency for scotopic vision?", + "a": "3482.199049234607", + "valid": true, + "kind": "compute", + "expect": 3482.199049234607, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for scotopic vision for [157.22256, 35.641157, 8.24951, 0.634021] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Could you work out the spectral luminous efficiency for scotopic vision given spectral luminous efficiency for mesopic vision = 157.223, spectral luminous efficiency for photopic vision = 35.6412, normalizing function for mesopic vision = 8.24951, adaptation coefficient for mesopic vision = 0.634021?", + "a": "3482.199049234607", + "valid": true, + "kind": "compute", + "expect": 3482.199049234607, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for scotopic vision for [157.22256, 35.641157, 8.24951, 0.634021] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "I need the spectral luminous efficiency for scotopic vision for a spectral luminous efficiency for mesopic vision of 10.5638, a spectral luminous efficiency for photopic vision of 198.839, a normalizing function for mesopic vision of 0.671697 and an adaptation coefficient for mesopic vision of 14.2496, please.", + "a": "213.31061449741813", + "valid": true, + "kind": "compute", + "expect": 213.31061449741813, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for scotopic vision for [10.563843, 198.838955, 0.671697, 14.249569] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "If the spectral luminous efficiency for mesopic vision is 10.5638, spectral luminous efficiency for photopic vision is 198.839, normalizing function for mesopic vision is 0.671697 and the adaptation coefficient for mesopic vision is 14.2496, what is the spectral luminous efficiency for scotopic vision?", + "a": "213.31061449741813", + "valid": true, + "kind": "compute", + "expect": 213.31061449741813, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for scotopic vision for [10.563843, 198.838955, 0.671697, 14.249569] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Could you work out the spectral luminous efficiency for scotopic vision given spectral luminous efficiency for mesopic vision = 308.158, spectral luminous efficiency for photopic vision = 178.166, normalizing function for mesopic vision = 75.8534, adaptation coefficient for mesopic vision = 0.558623?", + "a": "52733.44455964305", + "valid": true, + "kind": "compute", + "expect": 52733.44455964305, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for scotopic vision for [308.158051, 178.165518, 75.853435, 0.558623] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "I need the spectral luminous efficiency for scotopic vision for a spectral luminous efficiency for mesopic vision of 308.158, a spectral luminous efficiency for photopic vision of 178.166, a normalizing function for mesopic vision of 75.8534 and an adaptation coefficient for mesopic vision of 0.558623, please.", + "a": "52733.44455964305", + "valid": true, + "kind": "compute", + "expect": 52733.44455964305, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for scotopic vision for [308.158051, 178.165518, 75.853435, 0.558623] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "If the spectral luminous efficiency for mesopic vision is 79.8435, spectral luminous efficiency for photopic vision is 204.601, normalizing function for mesopic vision is 0.370124 and the adaptation coefficient for mesopic vision is 5.98214, what is the spectral luminous efficiency for scotopic vision?", + "a": "239.73614611171163", + "valid": true, + "kind": "compute", + "expect": 239.73614611171163, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for scotopic vision for [79.843538, 204.60087, 0.370124, 5.982139] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Could you work out the spectral luminous efficiency for scotopic vision given spectral luminous efficiency for mesopic vision = 79.8435, spectral luminous efficiency for photopic vision = 204.601, normalizing function for mesopic vision = 0.370124, adaptation coefficient for mesopic vision = 5.98214?", + "a": "239.73614611171163", + "valid": true, + "kind": "compute", + "expect": 239.73614611171163, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for scotopic vision for [79.843538, 204.60087, 0.370124, 5.982139] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "I need the spectral luminous efficiency for scotopic vision for a spectral luminous efficiency for mesopic vision of 32.955, a spectral luminous efficiency for photopic vision of 250.984, a normalizing function for mesopic vision of 0.472229 and an adaptation coefficient for mesopic vision of 33.7758, please.", + "a": "258.1671950986114", + "valid": true, + "kind": "compute", + "expect": 258.1671950986114, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for scotopic vision for [32.955028, 250.984389, 0.472229, 33.775777] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "If the spectral luminous efficiency for mesopic vision is 32.955, spectral luminous efficiency for photopic vision is 250.984, normalizing function for mesopic vision is 0.472229 and the adaptation coefficient for mesopic vision is 33.7758, what is the spectral luminous efficiency for scotopic vision?", + "a": "258.1671950986114", + "valid": true, + "kind": "compute", + "expect": 258.1671950986114, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for scotopic vision for [32.955028, 250.984389, 0.472229, 33.775777] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Could you work out the spectral luminous efficiency for scotopic vision given spectral luminous efficiency for mesopic vision = 0.319575, spectral luminous efficiency for photopic vision = 1.19379, normalizing function for mesopic vision = 3.1857, adaptation coefficient for mesopic vision = 1.65291?", + "a": "1.462930083900879", + "valid": true, + "kind": "compute", + "expect": 1.462930083900879, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for scotopic vision for [0.319575, 1.193792, 3.185701, 1.652905] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "I need the spectral luminous efficiency for scotopic vision for a spectral luminous efficiency for mesopic vision of 0.319575, a spectral luminous efficiency for photopic vision of 1.19379, a normalizing function for mesopic vision of 3.1857 and an adaptation coefficient for mesopic vision of 1.65291, please.", + "a": "1.462930083900879", + "valid": true, + "kind": "compute", + "expect": 1.462930083900879, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for scotopic vision for [0.319575, 1.193792, 3.185701, 1.652905] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "If the spectral luminous efficiency for mesopic vision is 157.223, spectral luminous efficiency for photopic vision is 35.6412 and the normalizing function for mesopic vision is 8.24951, what is the spectral luminous efficiency for scotopic vision?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the adaptation coefficient for mesopic vision is not given" + }, + { + "q": "Could you work out the spectral luminous efficiency for scotopic vision given spectral luminous efficiency for mesopic vision = 10.5638, spectral luminous efficiency for photopic vision = 198.839, normalizing function for mesopic vision = 0.671697?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the adaptation coefficient for mesopic vision is not given" + }, + { + "q": "Do you know the spectral luminous efficiency for scotopic vision 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 spectral luminous efficiency for scotopic vision 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 spectral luminous efficiency for scotopic vision given spectral luminous efficiency for mesopic vision = -308.158, spectral luminous efficiency for photopic vision = 178.166, normalizing function for mesopic vision = 75.8534, adaptation coefficient for mesopic vision = 0.558623?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative spectral luminous efficiency for mesopic vision: no example had one" + }, + { + "q": "What is a typical spectral luminous efficiency for scotopic vision?", + "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-spectral-luminous-efficiency--2/program.py b/root/organs10/physics/specialists/solved-spectral-luminous-efficiency--2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..1e502670d0f3d40b80ecc3b0ac13be94c4f3b98b --- /dev/null +++ b/root/organs10/physics/specialists/solved-spectral-luminous-efficiency--2/program.py @@ -0,0 +1,7 @@ +"""spectral luminous efficiency for scotopic vision from spectral luminous efficiency for mesopic vision, spectral luminous efficiency for photopic vision, normalizing function for mesopic vision, adaptation coefficient for mesopic vision (spectral luminous efficiency for mesopic vision (Wikidata Q107018545)) +The maths organ (systems.py): solved from the system q0 = ((1)/(q3))*( q4*q2 + ( 1 - q4 )*q1 ); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(spectral_luminous_efficiency_for_mesopic_vision, spectral_luminous_efficiency_for_photopic_vision, normalizing_function_for_mesopic_vision, adaptation_coefficient_for_mesopic_vision): + return (1.0*adaptation_coefficient_for_mesopic_vision*spectral_luminous_efficiency_for_photopic_vision - 1.0*normalizing_function_for_mesopic_vision*spectral_luminous_efficiency_for_mesopic_vision)/(adaptation_coefficient_for_mesopic_vision - 1.0) diff --git a/root/organs10/physics/specialists/solved-spectral-luminous-efficiency--2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-spectral-luminous-efficiency--2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-spectral-luminous-efficiency--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-spectral-luminous-efficiency--3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-spectral-luminous-efficiency--3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-spectral-luminous-efficiency--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-spectral-luminous-efficiency--3/panel.json b/root/organs10/physics/specialists/solved-spectral-luminous-efficiency--3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..341d920b5183d6b2f83a5b13d4991700be39bd9a --- /dev/null +++ b/root/organs10/physics/specialists/solved-spectral-luminous-efficiency--3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the spectral luminous efficiency for photopic vision for spectral luminous efficiency for mesopic vision 107.961, spectral luminous efficiency for scotopic vision 59.4105, normalizing function for mesopic vision 5.77717 and adaptation coefficient for mesopic vision 1.89126?", + "a": "357.78159687188014", + "valid": true, + "kind": "compute", + "expect": 357.78159687188014, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for photopic vision for [107.960666, 59.4105, 5.777165, 1.891256] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Tell me the spectral luminous efficiency for photopic vision with spectral luminous efficiency for mesopic vision of 107.961, spectral luminous efficiency for scotopic vision of 59.4105, normalizing function for mesopic vision of 5.77717 and adaptation coefficient for mesopic vision of 1.89126.", + "a": "357.78159687188014", + "valid": true, + "kind": "compute", + "expect": 357.78159687188014, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for photopic vision for [107.960666, 59.4105, 5.777165, 1.891256] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "What is the spectral luminous efficiency for photopic vision for spectral luminous efficiency for mesopic vision 2.69428, spectral luminous efficiency for scotopic vision 48.921, normalizing function for mesopic vision 157.223 and adaptation coefficient for mesopic vision 35.6412?", + "a": "59.43357293033549", + "valid": true, + "kind": "compute", + "expect": 59.43357293033549, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for photopic vision for [2.69428, 48.920991, 157.22256, 35.641157] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Tell me the spectral luminous efficiency for photopic vision with spectral luminous efficiency for mesopic vision of 2.69428, spectral luminous efficiency for scotopic vision of 48.921, normalizing function for mesopic vision of 157.223 and adaptation coefficient for mesopic vision of 35.6412.", + "a": "59.43357293033549", + "valid": true, + "kind": "compute", + "expect": 59.43357293033549, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for photopic vision for [2.69428, 48.920991, 157.22256, 35.641157] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "What is the spectral luminous efficiency for photopic vision for spectral luminous efficiency for mesopic vision 0.318734, spectral luminous efficiency for scotopic vision 9.56607, normalizing function for mesopic vision 126.709 and adaptation coefficient for mesopic vision 1.70503?", + "a": "27.642256578940636", + "valid": true, + "kind": "compute", + "expect": 27.642256578940636, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for photopic vision for [0.318734, 9.566071, 126.709252, 1.705032] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Tell me the spectral luminous efficiency for photopic vision with spectral luminous efficiency for mesopic vision of 0.318734, spectral luminous efficiency for scotopic vision of 9.56607, normalizing function for mesopic vision of 126.709 and adaptation coefficient for mesopic vision of 1.70503.", + "a": "27.642256578940636", + "valid": true, + "kind": "compute", + "expect": 27.642256578940636, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for photopic vision for [0.318734, 9.566071, 126.709252, 1.705032] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "What is the spectral luminous efficiency for photopic vision for spectral luminous efficiency for mesopic vision 87.781, spectral luminous efficiency for scotopic vision 13.2084, normalizing function for mesopic vision 246.329 and adaptation coefficient for mesopic vision 20.3966?", + "a": "1072.6913732624564", + "valid": true, + "kind": "compute", + "expect": 1072.6913732624564, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for photopic vision for [87.781013, 13.208419, 246.329053, 20.396558] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Tell me the spectral luminous efficiency for photopic vision with spectral luminous efficiency for mesopic vision of 87.781, spectral luminous efficiency for scotopic vision of 13.2084, normalizing function for mesopic vision of 246.329 and adaptation coefficient for mesopic vision of 20.3966.", + "a": "1072.6913732624564", + "valid": true, + "kind": "compute", + "expect": 1072.6913732624564, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for photopic vision for [87.781013, 13.208419, 246.329053, 20.396558] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "What is the spectral luminous efficiency for photopic vision for spectral luminous efficiency for mesopic vision 156.502, spectral luminous efficiency for scotopic vision 185.886, normalizing function for mesopic vision 13.2373 and adaptation coefficient for mesopic vision 4.71941?", + "a": "585.464810504079", + "valid": true, + "kind": "compute", + "expect": 585.464810504079, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for photopic vision for [156.50181, 185.886273, 13.237311, 4.719415] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Tell me the spectral luminous efficiency for photopic vision with spectral luminous efficiency for mesopic vision of 156.502, spectral luminous efficiency for scotopic vision of 185.886, normalizing function for mesopic vision of 13.2373 and adaptation coefficient for mesopic vision of 4.71941.", + "a": "585.464810504079", + "valid": true, + "kind": "compute", + "expect": 585.464810504079, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for photopic vision for [156.50181, 185.886273, 13.237311, 4.719415] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "What is the spectral luminous efficiency for photopic vision for spectral luminous efficiency for mesopic vision 24.5721, spectral luminous efficiency for scotopic vision 108.451, normalizing function for mesopic vision 1.69476 and adaptation coefficient for mesopic vision 49.4843?", + "a": "107.10072524758118", + "valid": true, + "kind": "compute", + "expect": 107.10072524758118, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for photopic vision for [24.572086, 108.450792, 1.694755, 49.484326] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Tell me the spectral luminous efficiency for photopic vision with spectral luminous efficiency for mesopic vision of 24.5721, spectral luminous efficiency for scotopic vision of 108.451, normalizing function for mesopic vision of 1.69476 and adaptation coefficient for mesopic vision of 49.4843.", + "a": "107.10072524758118", + "valid": true, + "kind": "compute", + "expect": 107.10072524758118, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for photopic vision for [24.572086, 108.450792, 1.694755, 49.484326] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "What is the spectral luminous efficiency for photopic vision for spectral luminous efficiency for mesopic vision 107.961, spectral luminous efficiency for scotopic vision 59.4105 and normalizing function for mesopic vision 5.77717?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the adaptation coefficient for mesopic vision is not given" + }, + { + "q": "Tell me the spectral luminous efficiency for photopic vision with spectral luminous efficiency for mesopic vision of 2.69428, spectral luminous efficiency for scotopic vision of 48.921 and normalizing function for mesopic vision of 157.223.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the adaptation coefficient for mesopic vision is not given" + }, + { + "q": "What is the spectral luminous efficiency for photopic vision 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 spectral luminous efficiency for photopic vision 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 spectral luminous efficiency for photopic vision with spectral luminous efficiency for mesopic vision of -0.318734, spectral luminous efficiency for scotopic vision of 9.56607, normalizing function for mesopic vision of 126.709 and adaptation coefficient for mesopic vision of 1.70503.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative spectral luminous efficiency for mesopic vision: no example had one" + }, + { + "q": "Which spectral luminous efficiency for photopic vision 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 spectral luminous efficiency for mesopic vision is 167.625, spectral luminous efficiency for scotopic vision is 10.3291, normalizing function for mesopic vision is 2.21571 and the adaptation coefficient for mesopic vision is 58.5346, what is the spectral luminous efficiency for photopic vision?", + "a": "16.4977099302231", + "valid": true, + "kind": "compute", + "expect": 16.4977099302231, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for photopic vision for [167.624948, 10.329054, 2.215713, 58.534594] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Could you work out the spectral luminous efficiency for photopic vision given spectral luminous efficiency for mesopic vision = 167.625, spectral luminous efficiency for scotopic vision = 10.3291, normalizing function for mesopic vision = 2.21571, adaptation coefficient for mesopic vision = 58.5346?", + "a": "16.4977099302231", + "valid": true, + "kind": "compute", + "expect": 16.4977099302231, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for photopic vision for [167.624948, 10.329054, 2.215713, 58.534594] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "I need the spectral luminous efficiency for photopic vision for a spectral luminous efficiency for mesopic vision of 8.53158, a spectral luminous efficiency for scotopic vision of 124.718, a normalizing function for mesopic vision of 1.91195 and an adaptation coefficient for mesopic vision of 82.2401, please.", + "a": "123.40001749164882", + "valid": true, + "kind": "compute", + "expect": 123.40001749164882, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for photopic vision for [8.531577, 124.718185, 1.911952, 82.240072] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "If the spectral luminous efficiency for mesopic vision is 8.53158, spectral luminous efficiency for scotopic vision is 124.718, normalizing function for mesopic vision is 1.91195 and the adaptation coefficient for mesopic vision is 82.2401, what is the spectral luminous efficiency for photopic vision?", + "a": "123.40001749164882", + "valid": true, + "kind": "compute", + "expect": 123.40001749164882, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for photopic vision for [8.531577, 124.718185, 1.911952, 82.240072] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Could you work out the spectral luminous efficiency for photopic vision given spectral luminous efficiency for mesopic vision = 81.1967, spectral luminous efficiency for scotopic vision = 6.98086, normalizing function for mesopic vision = 0.551236, adaptation coefficient for mesopic vision = 2.88512?", + "a": "20.07482989036208", + "valid": true, + "kind": "compute", + "expect": 20.07482989036208, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for photopic vision for [81.196705, 6.980857, 0.551236, 2.88512] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "I need the spectral luminous efficiency for photopic vision for a spectral luminous efficiency for mesopic vision of 81.1967, a spectral luminous efficiency for scotopic vision of 6.98086, a normalizing function for mesopic vision of 0.551236 and an adaptation coefficient for mesopic vision of 2.88512, please.", + "a": "20.07482989036208", + "valid": true, + "kind": "compute", + "expect": 20.07482989036208, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for photopic vision for [81.196705, 6.980857, 0.551236, 2.88512] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "If the spectral luminous efficiency for mesopic vision is 1.16997, spectral luminous efficiency for scotopic vision is 1.14865, normalizing function for mesopic vision is 21.7931 and the adaptation coefficient for mesopic vision is 29.5106, what is the spectral luminous efficiency for photopic vision?", + "a": "1.9737321414259648", + "valid": true, + "kind": "compute", + "expect": 1.9737321414259648, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for photopic vision for [1.169968, 1.148652, 21.793126, 29.510609] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Could you work out the spectral luminous efficiency for photopic vision given spectral luminous efficiency for mesopic vision = 1.16997, spectral luminous efficiency for scotopic vision = 1.14865, normalizing function for mesopic vision = 21.7931, adaptation coefficient for mesopic vision = 29.5106?", + "a": "1.9737321414259648", + "valid": true, + "kind": "compute", + "expect": 1.9737321414259648, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for photopic vision for [1.169968, 1.148652, 21.793126, 29.510609] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "I need the spectral luminous efficiency for photopic vision for a spectral luminous efficiency for mesopic vision of 223.509, a spectral luminous efficiency for scotopic vision of 17.3414, a normalizing function for mesopic vision of 7.10705 and an adaptation coefficient for mesopic vision of 30.2507, please.", + "a": "69.27887812847672", + "valid": true, + "kind": "compute", + "expect": 69.27887812847672, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for photopic vision for [223.508796, 17.341435, 7.107049, 30.250749] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "If the spectral luminous efficiency for mesopic vision is 223.509, spectral luminous efficiency for scotopic vision is 17.3414, normalizing function for mesopic vision is 7.10705 and the adaptation coefficient for mesopic vision is 30.2507, what is the spectral luminous efficiency for photopic vision?", + "a": "69.27887812847672", + "valid": true, + "kind": "compute", + "expect": 69.27887812847672, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for photopic vision for [223.508796, 17.341435, 7.107049, 30.250749] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Could you work out the spectral luminous efficiency for photopic vision given spectral luminous efficiency for mesopic vision = 0.869111, spectral luminous efficiency for scotopic vision = 39.1753, normalizing function for mesopic vision = 0.432217, adaptation coefficient for mesopic vision = 16.6571?", + "a": "36.845942002208744", + "valid": true, + "kind": "compute", + "expect": 36.845942002208744, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for photopic vision for [0.869111, 39.175263, 0.432217, 16.657051] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "I need the spectral luminous efficiency for photopic vision for a spectral luminous efficiency for mesopic vision of 0.869111, a spectral luminous efficiency for scotopic vision of 39.1753, a normalizing function for mesopic vision of 0.432217 and an adaptation coefficient for mesopic vision of 16.6571, please.", + "a": "36.845942002208744", + "valid": true, + "kind": "compute", + "expect": 36.845942002208744, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for photopic vision for [0.869111, 39.175263, 0.432217, 16.657051] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "If the spectral luminous efficiency for mesopic vision is 167.625, spectral luminous efficiency for scotopic vision is 10.3291 and the normalizing function for mesopic vision is 2.21571, what is the spectral luminous efficiency for photopic vision?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the adaptation coefficient for mesopic vision is not given" + }, + { + "q": "Could you work out the spectral luminous efficiency for photopic vision given spectral luminous efficiency for mesopic vision = 8.53158, spectral luminous efficiency for scotopic vision = 124.718, normalizing function for mesopic vision = 1.91195?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the adaptation coefficient for mesopic vision is not given" + }, + { + "q": "Do you know the spectral luminous efficiency for photopic vision 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 spectral luminous efficiency for photopic vision 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 spectral luminous efficiency for photopic vision given spectral luminous efficiency for mesopic vision = -81.1967, spectral luminous efficiency for scotopic vision = 6.98086, normalizing function for mesopic vision = 0.551236, adaptation coefficient for mesopic vision = 2.88512?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative spectral luminous efficiency for mesopic vision: no example had one" + }, + { + "q": "What is a typical spectral luminous efficiency for photopic vision?", + "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-spectral-luminous-efficiency--3/program.py b/root/organs10/physics/specialists/solved-spectral-luminous-efficiency--3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..cc2f302e9446b47c726c91c24ac31f186d837a76 --- /dev/null +++ b/root/organs10/physics/specialists/solved-spectral-luminous-efficiency--3/program.py @@ -0,0 +1,7 @@ +"""spectral luminous efficiency for photopic vision from spectral luminous efficiency for mesopic vision, spectral luminous efficiency for scotopic vision, normalizing function for mesopic vision, adaptation coefficient for mesopic vision (spectral luminous efficiency for mesopic vision (Wikidata Q107018545)) +The maths organ (systems.py): solved from the system q0 = ((1)/(q3))*( q4*q2 + ( 1 - q4 )*q1 ); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(spectral_luminous_efficiency_for_mesopic_vision, spectral_luminous_efficiency_for_scotopic_vision, normalizing_function_for_mesopic_vision, adaptation_coefficient_for_mesopic_vision): + return (1.0*adaptation_coefficient_for_mesopic_vision*spectral_luminous_efficiency_for_scotopic_vision + 1.0*normalizing_function_for_mesopic_vision*spectral_luminous_efficiency_for_mesopic_vision - 1.0*spectral_luminous_efficiency_for_scotopic_vision)/adaptation_coefficient_for_mesopic_vision diff --git a/root/organs10/physics/specialists/solved-spectral-luminous-efficiency--3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-spectral-luminous-efficiency--3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-spectral-luminous-efficiency--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-spectral-luminous-efficiency-/organ_formula_v2.py b/root/organs10/physics/specialists/solved-spectral-luminous-efficiency-/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-spectral-luminous-efficiency-/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-spectral-luminous-efficiency-/panel.json b/root/organs10/physics/specialists/solved-spectral-luminous-efficiency-/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..10d6d83c647209fab15bc58195200270088f22cb --- /dev/null +++ b/root/organs10/physics/specialists/solved-spectral-luminous-efficiency-/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the spectral luminous efficiency for mesopic vision for spectral luminous efficiency for scotopic vision 107.961, spectral luminous efficiency for photopic vision 59.4105, normalizing function for mesopic vision 5.77717 and adaptation coefficient for mesopic vision 1.89126?", + "a": "2.7937386512518154", + "valid": true, + "kind": "compute", + "expect": 2.7937386512518154, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for mesopic vision for [107.960666, 59.4105, 5.777165, 1.891256] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Tell me the spectral luminous efficiency for mesopic vision with spectral luminous efficiency for scotopic vision of 107.961, spectral luminous efficiency for photopic vision of 59.4105, normalizing function for mesopic vision of 5.77717 and adaptation coefficient for mesopic vision of 1.89126.", + "a": "2.7937386512518154", + "valid": true, + "kind": "compute", + "expect": 2.7937386512518154, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for mesopic vision for [107.960666, 59.4105, 5.777165, 1.891256] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "What is the spectral luminous efficiency for mesopic vision for spectral luminous efficiency for scotopic vision 173.394, spectral luminous efficiency for photopic vision 251.084, normalizing function for mesopic vision 8.53158 and adaptation coefficient for mesopic vision 124.718?", + "a": "1156.0346785462461", + "valid": true, + "kind": "compute", + "expect": 1156.0346785462461, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for mesopic vision for [173.393638, 251.084037, 8.531577, 124.718185] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Tell me the spectral luminous efficiency for mesopic vision with spectral luminous efficiency for scotopic vision of 173.394, spectral luminous efficiency for photopic vision of 251.084, normalizing function for mesopic vision of 8.53158 and adaptation coefficient for mesopic vision of 124.718.", + "a": "1156.0346785462461", + "valid": true, + "kind": "compute", + "expect": 1156.0346785462461, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for mesopic vision for [173.393638, 251.084037, 8.531577, 124.718185] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "What is the spectral luminous efficiency for mesopic vision for spectral luminous efficiency for scotopic vision 0.671697, spectral luminous efficiency for photopic vision 14.2496, normalizing function for mesopic vision 41.6566 and adaptation coefficient for mesopic vision 13.8779?", + "a": "4.539579030751915", + "valid": true, + "kind": "compute", + "expect": 4.539579030751915, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for mesopic vision for [0.671697, 14.249569, 41.656639, 13.877867] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Tell me the spectral luminous efficiency for mesopic vision with spectral luminous efficiency for scotopic vision of 0.671697, spectral luminous efficiency for photopic vision of 14.2496, normalizing function for mesopic vision of 41.6566 and adaptation coefficient for mesopic vision of 13.8779.", + "a": "4.539579030751915", + "valid": true, + "kind": "compute", + "expect": 4.539579030751915, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for mesopic vision for [0.671697, 14.249569, 41.656639, 13.877867] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "What is the spectral luminous efficiency for mesopic vision for spectral luminous efficiency for scotopic vision 156.502, spectral luminous efficiency for photopic vision 185.886, normalizing function for mesopic vision 13.2373 and adaptation coefficient for mesopic vision 4.71941?", + "a": "22.299036255374364", + "valid": true, + "kind": "compute", + "expect": 22.299036255374364, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for mesopic vision for [156.50181, 185.886273, 13.237311, 4.719415] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Tell me the spectral luminous efficiency for mesopic vision with spectral luminous efficiency for scotopic vision of 156.502, spectral luminous efficiency for photopic vision of 185.886, normalizing function for mesopic vision of 13.2373 and adaptation coefficient for mesopic vision of 4.71941.", + "a": "22.299036255374364", + "valid": true, + "kind": "compute", + "expect": 22.299036255374364, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for mesopic vision for [156.50181, 185.886273, 13.237311, 4.719415] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "What is the spectral luminous efficiency for mesopic vision for spectral luminous efficiency for scotopic vision 0.710236, spectral luminous efficiency for photopic vision 1.45005, normalizing function for mesopic vision 76.5154 and adaptation coefficient for mesopic vision 3.14491?", + "a": "0.03968965953539708", + "valid": true, + "kind": "compute", + "expect": 0.03968965953539708, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for mesopic vision for [0.710236, 1.450045, 76.515411, 3.144912] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Tell me the spectral luminous efficiency for mesopic vision with spectral luminous efficiency for scotopic vision of 0.710236, spectral luminous efficiency for photopic vision of 1.45005, normalizing function for mesopic vision of 76.5154 and adaptation coefficient for mesopic vision of 3.14491.", + "a": "0.03968965953539708", + "valid": true, + "kind": "compute", + "expect": 0.03968965953539708, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for mesopic vision for [0.710236, 1.450045, 76.515411, 3.144912] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "What is the spectral luminous efficiency for mesopic vision for spectral luminous efficiency for scotopic vision 0.406608, spectral luminous efficiency for photopic vision 0.367206, normalizing function for mesopic vision 241.598 and adaptation coefficient for mesopic vision 1.13479?", + "a": "0.0014979192521294808", + "valid": true, + "kind": "compute", + "expect": 0.0014979192521294808, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for mesopic vision for [0.406608, 0.367206, 241.598281, 1.134791] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Tell me the spectral luminous efficiency for mesopic vision with spectral luminous efficiency for scotopic vision of 0.406608, spectral luminous efficiency for photopic vision of 0.367206, normalizing function for mesopic vision of 241.598 and adaptation coefficient for mesopic vision of 1.13479.", + "a": "0.0014979192521294808", + "valid": true, + "kind": "compute", + "expect": 0.0014979192521294808, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for mesopic vision for [0.406608, 0.367206, 241.598281, 1.134791] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "What is the spectral luminous efficiency for mesopic vision for spectral luminous efficiency for scotopic vision 107.961, spectral luminous efficiency for photopic vision 59.4105 and normalizing function for mesopic vision 5.77717?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the adaptation coefficient for mesopic vision is not given" + }, + { + "q": "Tell me the spectral luminous efficiency for mesopic vision with spectral luminous efficiency for scotopic vision of 173.394, spectral luminous efficiency for photopic vision of 251.084 and normalizing function for mesopic vision of 8.53158.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the adaptation coefficient for mesopic vision is not given" + }, + { + "q": "What is the spectral luminous efficiency for mesopic vision 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 spectral luminous efficiency for mesopic vision 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 spectral luminous efficiency for mesopic vision with spectral luminous efficiency for scotopic vision of -0.671697, spectral luminous efficiency for photopic vision of 14.2496, normalizing function for mesopic vision of 41.6566 and adaptation coefficient for mesopic vision of 13.8779.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative spectral luminous efficiency for scotopic vision: no example had one" + }, + { + "q": "Which spectral luminous efficiency for mesopic vision 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 spectral luminous efficiency for scotopic vision is 85.2416, spectral luminous efficiency for photopic vision is 160.878, normalizing function for mesopic vision is 2.69428 and the adaptation coefficient for mesopic vision is 48.921, what is the spectral luminous efficiency for mesopic vision?", + "a": "1405.0024252419532", + "valid": true, + "kind": "compute", + "expect": 1405.0024252419532, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for mesopic vision for [85.241623, 160.878442, 2.69428, 48.920991] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Could you work out the spectral luminous efficiency for mesopic vision given spectral luminous efficiency for scotopic vision = 85.2416, spectral luminous efficiency for photopic vision = 160.878, normalizing function for mesopic vision = 2.69428, adaptation coefficient for mesopic vision = 48.921?", + "a": "1405.0024252419532", + "valid": true, + "kind": "compute", + "expect": 1405.0024252419532, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for mesopic vision for [85.241623, 160.878442, 2.69428, 48.920991] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "I need the spectral luminous efficiency for mesopic vision for a spectral luminous efficiency for scotopic vision of 1.18359, a spectral luminous efficiency for photopic vision of 15.9416, a normalizing function for mesopic vision of 1.64379 and an adaptation coefficient for mesopic vision of 252.709, please.", + "a": "2269.5455917002023", + "valid": true, + "kind": "compute", + "expect": 2269.5455917002023, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for mesopic vision for [1.18359, 15.941612, 1.643795, 252.709024] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "If the spectral luminous efficiency for scotopic vision is 1.18359, spectral luminous efficiency for photopic vision is 15.9416, normalizing function for mesopic vision is 1.64379 and the adaptation coefficient for mesopic vision is 252.709, what is the spectral luminous efficiency for mesopic vision?", + "a": "2269.5455917002023", + "valid": true, + "kind": "compute", + "expect": 2269.5455917002023, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for mesopic vision for [1.18359, 15.941612, 1.643795, 252.709024] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Could you work out the spectral luminous efficiency for mesopic vision given spectral luminous efficiency for scotopic vision = 1.16997, spectral luminous efficiency for photopic vision = 1.14865, normalizing function for mesopic vision = 21.7931, adaptation coefficient for mesopic vision = 29.5106?", + "a": "0.024821055913023974", + "valid": true, + "kind": "compute", + "expect": 0.024821055913023974, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for mesopic vision for [1.169968, 1.148652, 21.793126, 29.510609] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "I need the spectral luminous efficiency for mesopic vision for a spectral luminous efficiency for scotopic vision of 1.16997, a spectral luminous efficiency for photopic vision of 1.14865, a normalizing function for mesopic vision of 21.7931 and an adaptation coefficient for mesopic vision of 29.5106, please.", + "a": "0.024821055913023974", + "valid": true, + "kind": "compute", + "expect": 0.024821055913023974, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for mesopic vision for [1.169968, 1.148652, 21.793126, 29.510609] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "If the spectral luminous efficiency for scotopic vision is 308.158, spectral luminous efficiency for photopic vision is 178.166, normalizing function for mesopic vision is 75.8534 and the adaptation coefficient for mesopic vision is 0.558623, what is the spectral luminous efficiency for mesopic vision?", + "a": "3.1052147667382877", + "valid": true, + "kind": "compute", + "expect": 3.1052147667382877, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for mesopic vision for [308.158051, 178.165518, 75.853435, 0.558623] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Could you work out the spectral luminous efficiency for mesopic vision given spectral luminous efficiency for scotopic vision = 308.158, spectral luminous efficiency for photopic vision = 178.166, normalizing function for mesopic vision = 75.8534, adaptation coefficient for mesopic vision = 0.558623?", + "a": "3.1052147667382877", + "valid": true, + "kind": "compute", + "expect": 3.1052147667382877, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for mesopic vision for [308.158051, 178.165518, 75.853435, 0.558623] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "I need the spectral luminous efficiency for mesopic vision for a spectral luminous efficiency for scotopic vision of 169.843, a spectral luminous efficiency for photopic vision of 12.6647, a normalizing function for mesopic vision of 34.8151 and an adaptation coefficient for mesopic vision of 0.380269, please.", + "a": "3.1616507257677173", + "valid": true, + "kind": "compute", + "expect": 3.1616507257677173, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for mesopic vision for [169.843153, 12.664671, 34.81508, 0.380269] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "If the spectral luminous efficiency for scotopic vision is 169.843, spectral luminous efficiency for photopic vision is 12.6647, normalizing function for mesopic vision is 34.8151 and the adaptation coefficient for mesopic vision is 0.380269, what is the spectral luminous efficiency for mesopic vision?", + "a": "3.1616507257677173", + "valid": true, + "kind": "compute", + "expect": 3.1616507257677173, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for mesopic vision for [169.843153, 12.664671, 34.81508, 0.380269] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "Could you work out the spectral luminous efficiency for mesopic vision given spectral luminous efficiency for scotopic vision = 0.63753, spectral luminous efficiency for photopic vision = 1.90441, normalizing function for mesopic vision = 1.45374, adaptation coefficient for mesopic vision = 27.5916?", + "a": "24.483479979471287", + "valid": true, + "kind": "compute", + "expect": 24.483479979471287, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for mesopic vision for [0.63753, 1.904407, 1.453744, 27.591617] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "I need the spectral luminous efficiency for mesopic vision for a spectral luminous efficiency for scotopic vision of 0.63753, a spectral luminous efficiency for photopic vision of 1.90441, a normalizing function for mesopic vision of 1.45374 and an adaptation coefficient for mesopic vision of 27.5916, please.", + "a": "24.483479979471287", + "valid": true, + "kind": "compute", + "expect": 24.483479979471287, + "dtype": "quantity", + "unit": "1", + "why": "spectral luminous efficiency for mesopic vision for [0.63753, 1.904407, 1.453744, 27.591617] (the system spectral luminous efficiency for mesopic vision (Wikidata Q107018545))" + }, + { + "q": "If the spectral luminous efficiency for scotopic vision is 85.2416, spectral luminous efficiency for photopic vision is 160.878 and the normalizing function for mesopic vision is 2.69428, what is the spectral luminous efficiency for mesopic vision?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the adaptation coefficient for mesopic vision is not given" + }, + { + "q": "Could you work out the spectral luminous efficiency for mesopic vision given spectral luminous efficiency for scotopic vision = 1.18359, spectral luminous efficiency for photopic vision = 15.9416, normalizing function for mesopic vision = 1.64379?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the adaptation coefficient for mesopic vision is not given" + }, + { + "q": "Do you know the spectral luminous efficiency for mesopic vision 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 spectral luminous efficiency for mesopic vision 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 spectral luminous efficiency for mesopic vision given spectral luminous efficiency for scotopic vision = -1.16997, spectral luminous efficiency for photopic vision = 1.14865, normalizing function for mesopic vision = 21.7931, adaptation coefficient for mesopic vision = 29.5106?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative spectral luminous efficiency for scotopic vision: no example had one" + }, + { + "q": "What is a typical spectral luminous efficiency for mesopic vision?", + "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-spectral-luminous-efficiency-/program.py b/root/organs10/physics/specialists/solved-spectral-luminous-efficiency-/program.py new file mode 100644 index 0000000000000000000000000000000000000000..da510b34c10d2414c1ec5b6a01e4608066e2f5d6 --- /dev/null +++ b/root/organs10/physics/specialists/solved-spectral-luminous-efficiency-/program.py @@ -0,0 +1,7 @@ +"""spectral luminous efficiency for mesopic vision from spectral luminous efficiency for scotopic vision, spectral luminous efficiency for photopic vision, normalizing function for mesopic vision, adaptation coefficient for mesopic vision (spectral luminous efficiency for mesopic vision (Wikidata Q107018545)) +The maths organ (systems.py): solved from the system q0 = ((1)/(q3))*( q4*q2 + ( 1 - q4 )*q1 ); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(spectral_luminous_efficiency_for_scotopic_vision, spectral_luminous_efficiency_for_photopic_vision, normalizing_function_for_mesopic_vision, adaptation_coefficient_for_mesopic_vision): + return (1.0*adaptation_coefficient_for_mesopic_vision*spectral_luminous_efficiency_for_photopic_vision - 1.0*adaptation_coefficient_for_mesopic_vision*spectral_luminous_efficiency_for_scotopic_vision + 1.0*spectral_luminous_efficiency_for_scotopic_vision)/normalizing_function_for_mesopic_vision diff --git a/root/organs10/physics/specialists/solved-spectral-luminous-efficiency-/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-spectral-luminous-efficiency-/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-spectral-luminous-efficiency-/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-speed-10/organ_formula_v2.py b/root/organs10/physics/specialists/solved-speed-10/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-10/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-speed-10/panel.json b/root/organs10/physics/specialists/solved-speed-10/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..632a6d2e1583ada1bc0b08e9dc7621897a826354 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-10/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the speed for distance 107.961 and duration 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Tell me the speed with distance of 107.961 and duration of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105] (the system speed (Wikidata Q3711325))" + }, + { + "q": "What is the speed for distance 169.527 and duration 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [169.526958, 280.773702] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Tell me the speed with distance of 169.527 and duration of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [169.526958, 280.773702] (the system speed (Wikidata Q3711325))" + }, + { + "q": "What is the speed for distance 1.91195 and duration 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.911952, 82.240072] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Tell me the speed with distance of 1.91195 and duration of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.911952, 82.240072] (the system speed (Wikidata Q3711325))" + }, + { + "q": "What is the speed for distance 1.64379 and duration 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.643795, 252.709024] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Tell me the speed with distance of 1.64379 and duration of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.643795, 252.709024] (the system speed (Wikidata Q3711325))" + }, + { + "q": "What is the speed for distance 18.3169 and duration 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [18.316867, 6.838598] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Tell me the speed with distance of 18.3169 and duration of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [18.316867, 6.838598] (the system speed (Wikidata Q3711325))" + }, + { + "q": "What is the speed for distance 156.502 and duration 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [156.50181, 185.886273] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Tell me the speed with distance of 156.502 and duration of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [156.50181, 185.886273] (the system speed (Wikidata Q3711325))" + }, + { + "q": "What is the speed for distance 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the duration is not given" + }, + { + "q": "Tell me the speed with distance of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the duration is not given" + }, + { + "q": "What is the speed of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the speed of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the speed with distance of -1.91195 and duration of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative distance: no example had one" + }, + { + "q": "Which speed is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the distance is 8.50728 and the duration is 17.7888, what is the speed?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.507283, 17.788778] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Could you work out the speed given distance = 8.50728, duration = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.507283, 17.788778] (the system speed (Wikidata Q3711325))" + }, + { + "q": "I need the speed for a distance of 8.24951 and a duration of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.24951, 0.634021] (the system speed (Wikidata Q3711325))" + }, + { + "q": "If the distance is 8.24951 and the duration is 0.634021, what is the speed?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.24951, 0.634021] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Could you work out the speed given distance = 0.318734, duration = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.318734, 9.566071] (the system speed (Wikidata Q3711325))" + }, + { + "q": "I need the speed for a distance of 0.318734 and a duration of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.318734, 9.566071] (the system speed (Wikidata Q3711325))" + }, + { + "q": "If the distance is 0.671697 and the duration is 14.2496, what is the speed?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.671697, 14.249569] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Could you work out the speed given distance = 0.671697, duration = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.671697, 14.249569] (the system speed (Wikidata Q3711325))" + }, + { + "q": "I need the speed for a distance of 21.7931 and a duration of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609] (the system speed (Wikidata Q3711325))" + }, + { + "q": "If the distance is 21.7931 and the duration is 29.5106, what is the speed?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Could you work out the speed given distance = 153.15, duration = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [153.149965, 18.595315] (the system speed (Wikidata Q3711325))" + }, + { + "q": "I need the speed for a distance of 153.15 and a duration of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [153.149965, 18.595315] (the system speed (Wikidata Q3711325))" + }, + { + "q": "If the distance is 8.50728, what is the speed?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the duration is not given" + }, + { + "q": "Could you work out the speed given distance = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the duration is not given" + }, + { + "q": "Do you know the speed of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the speed of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the speed given distance = -0.318734, duration = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative distance: no example had one" + }, + { + "q": "What is a typical speed?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-speed-10/program.py b/root/organs10/physics/specialists/solved-speed-10/program.py new file mode 100644 index 0000000000000000000000000000000000000000..5cd13c1bb893b0fb4eb25fd525a55504dd9dd323 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-10/program.py @@ -0,0 +1,7 @@ +"""speed from distance, duration (speed (Wikidata Q3711325)) +The maths organ (systems.py): solved from the system q0 = q1 / q2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(distance, duration): + return 1.0*distance/duration diff --git a/root/organs10/physics/specialists/solved-speed-10/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-speed-10/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-10/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-speed-11/organ_formula_v2.py b/root/organs10/physics/specialists/solved-speed-11/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-11/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-speed-11/panel.json b/root/organs10/physics/specialists/solved-speed-11/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..d8583801c520a271464d0362f9e4c8c0b58cc89b --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-11/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the speed for shear rate 107.961 and distance 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Tell me the speed with shear rate of 107.961 and distance of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "What is the speed for shear rate 169.527 and distance 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [169.526958, 280.773702] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Tell me the speed with shear rate of 169.527 and distance of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [169.526958, 280.773702] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "What is the speed for shear rate 1.91195 and distance 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.911952, 82.240072] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Tell me the speed with shear rate of 1.91195 and distance of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.911952, 82.240072] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "What is the speed for shear rate 1.64379 and distance 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.643795, 252.709024] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Tell me the speed with shear rate of 1.64379 and distance of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.643795, 252.709024] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "What is the speed for shear rate 18.3169 and distance 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [18.316867, 6.838598] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Tell me the speed with shear rate of 18.3169 and distance of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [18.316867, 6.838598] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "What is the speed for shear rate 156.502 and distance 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [156.50181, 185.886273] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Tell me the speed with shear rate of 156.502 and distance of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [156.50181, 185.886273] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "What is the speed for shear rate 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the distance is not given" + }, + { + "q": "Tell me the speed with shear rate of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the distance is not given" + }, + { + "q": "What is the speed of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the speed of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the speed with shear rate of -1.91195 and distance of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative shear rate: no example had one" + }, + { + "q": "Which speed is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the shear rate is 8.50728 and the distance is 17.7888, what is the speed?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.507283, 17.788778] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Could you work out the speed given shear rate = 8.50728, distance = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.507283, 17.788778] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "I need the speed for a shear rate of 8.24951 and a distance of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.24951, 0.634021] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "If the shear rate is 8.24951 and the distance is 0.634021, what is the speed?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.24951, 0.634021] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Could you work out the speed given shear rate = 0.318734, distance = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.318734, 9.566071] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "I need the speed for a shear rate of 0.318734 and a distance of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.318734, 9.566071] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "If the shear rate is 0.671697 and the distance is 14.2496, what is the speed?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.671697, 14.249569] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Could you work out the speed given shear rate = 0.671697, distance = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.671697, 14.249569] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "I need the speed for a shear rate of 21.7931 and a distance of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "If the shear rate is 21.7931 and the distance is 29.5106, what is the speed?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "Could you work out the speed given shear rate = 153.15, distance = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [153.149965, 18.595315] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "I need the speed for a shear rate of 153.15 and a distance of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [153.149965, 18.595315] (the system shear rate (Wikidata Q461293))" + }, + { + "q": "If the shear rate is 8.50728, what is the speed?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the distance is not given" + }, + { + "q": "Could you work out the speed given shear rate = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the distance is not given" + }, + { + "q": "Do you know the speed of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the speed of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the speed given shear rate = -0.318734, distance = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative shear rate: no example had one" + }, + { + "q": "What is a typical speed?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-speed-11/program.py b/root/organs10/physics/specialists/solved-speed-11/program.py new file mode 100644 index 0000000000000000000000000000000000000000..6b8656163b68cab668078a2ed234c7222f24d308 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-11/program.py @@ -0,0 +1,7 @@ +"""speed from shear rate, distance (shear rate (Wikidata Q461293)) +The maths organ (systems.py): solved from the system q0 = ((q1)/(q2)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(shear_rate, distance): + return 1.0*distance*shear_rate diff --git a/root/organs10/physics/specialists/solved-speed-11/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-speed-11/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-11/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-speed-12/organ_formula_v2.py b/root/organs10/physics/specialists/solved-speed-12/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-12/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-speed-12/panel.json b/root/organs10/physics/specialists/solved-speed-12/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..d85731ac8d787327dc4c2c816ba0110ae510cc0e --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-12/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the speed for kinetic energy 107.961 and mass 59.4105?", + "a": "1.9064093998872984", + "valid": true, + "kind": "compute", + "expect": 1.9064093998872984, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "Tell me the speed with kinetic energy of 107.961 and mass of 59.4105.", + "a": "1.9064093998872984", + "valid": true, + "kind": "compute", + "expect": 1.9064093998872984, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "What is the speed for kinetic energy 169.527 and mass 280.774?", + "a": "1.0988949250779128", + "valid": true, + "kind": "compute", + "expect": 1.0988949250779128, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [169.526958, 280.773702] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "Tell me the speed with kinetic energy of 169.527 and mass of 280.774.", + "a": "1.0988949250779128", + "valid": true, + "kind": "compute", + "expect": 1.0988949250779128, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [169.526958, 280.773702] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "What is the speed for kinetic energy 1.91195 and mass 82.2401?", + "a": "0.21563126113155487", + "valid": true, + "kind": "compute", + "expect": 0.21563126113155487, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.911952, 82.240072] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "Tell me the speed with kinetic energy of 1.91195 and mass of 82.2401.", + "a": "0.21563126113155487", + "valid": true, + "kind": "compute", + "expect": 0.21563126113155487, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.911952, 82.240072] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "What is the speed for kinetic energy 1.64379 and mass 252.709?", + "a": "0.11405872573102283", + "valid": true, + "kind": "compute", + "expect": 0.11405872573102283, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.643795, 252.709024] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "Tell me the speed with kinetic energy of 1.64379 and mass of 252.709.", + "a": "0.11405872573102283", + "valid": true, + "kind": "compute", + "expect": 0.11405872573102283, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.643795, 252.709024] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "What is the speed for kinetic energy 18.3169 and mass 6.8386?", + "a": "2.3144993048580904", + "valid": true, + "kind": "compute", + "expect": 2.3144993048580904, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [18.316867, 6.838598] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "Tell me the speed with kinetic energy of 18.3169 and mass of 6.8386.", + "a": "2.3144993048580904", + "valid": true, + "kind": "compute", + "expect": 2.3144993048580904, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [18.316867, 6.838598] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "What is the speed for kinetic energy 156.502 and mass 185.886?", + "a": "1.2976304262856653", + "valid": true, + "kind": "compute", + "expect": 1.2976304262856653, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [156.50181, 185.886273] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "Tell me the speed with kinetic energy of 156.502 and mass of 185.886.", + "a": "1.2976304262856653", + "valid": true, + "kind": "compute", + "expect": 1.2976304262856653, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [156.50181, 185.886273] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "What is the speed for kinetic energy 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Tell me the speed with kinetic energy of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "What is the speed of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the speed of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the speed with kinetic energy of -1.91195 and mass of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative kinetic energy: no example had one" + }, + { + "q": "Which speed is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the kinetic energy is 8.50728 and the mass is 17.7888, what is the speed?", + "a": "0.9779966560958291", + "valid": true, + "kind": "compute", + "expect": 0.9779966560958291, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.507283, 17.788778] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "Could you work out the speed given kinetic energy = 8.50728, mass = 17.7888?", + "a": "0.9779966560958291", + "valid": true, + "kind": "compute", + "expect": 0.9779966560958291, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.507283, 17.788778] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "I need the speed for a kinetic energy of 8.24951 and a mass of 0.634021, please.", + "a": "5.101257648186148", + "valid": true, + "kind": "compute", + "expect": 5.101257648186148, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.24951, 0.634021] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "If the kinetic energy is 8.24951 and the mass is 0.634021, what is the speed?", + "a": "5.101257648186148", + "valid": true, + "kind": "compute", + "expect": 5.101257648186148, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.24951, 0.634021] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "Could you work out the speed given kinetic energy = 0.318734, mass = 9.56607?", + "a": "0.25814423291087363", + "valid": true, + "kind": "compute", + "expect": 0.25814423291087363, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.318734, 9.566071] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "I need the speed for a kinetic energy of 0.318734 and a mass of 9.56607, please.", + "a": "0.25814423291087363", + "valid": true, + "kind": "compute", + "expect": 0.25814423291087363, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.318734, 9.566071] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "If the kinetic energy is 0.671697 and the mass is 14.2496, what is the speed?", + "a": "0.30704419966634233", + "valid": true, + "kind": "compute", + "expect": 0.30704419966634233, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.671697, 14.249569] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "Could you work out the speed given kinetic energy = 0.671697, mass = 14.2496?", + "a": "0.30704419966634233", + "valid": true, + "kind": "compute", + "expect": 0.30704419966634233, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.671697, 14.249569] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "I need the speed for a kinetic energy of 21.7931 and a mass of 29.5106, please.", + "a": "1.2153061178601818", + "valid": true, + "kind": "compute", + "expect": 1.2153061178601818, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "If the kinetic energy is 21.7931 and the mass is 29.5106, what is the speed?", + "a": "1.2153061178601818", + "valid": true, + "kind": "compute", + "expect": 1.2153061178601818, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "Could you work out the speed given kinetic energy = 153.15, mass = 18.5953?", + "a": "4.058557223570786", + "valid": true, + "kind": "compute", + "expect": 4.058557223570786, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [153.149965, 18.595315] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "I need the speed for a kinetic energy of 153.15 and a mass of 18.5953, please.", + "a": "4.058557223570786", + "valid": true, + "kind": "compute", + "expect": 4.058557223570786, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [153.149965, 18.595315] (the system kinetic energy (Wikidata Q46276))" + }, + { + "q": "If the kinetic energy is 8.50728, what is the speed?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Could you work out the speed given kinetic energy = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Do you know the speed of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the speed of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the speed given kinetic energy = -0.318734, mass = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative kinetic energy: no example had one" + }, + { + "q": "What is a typical speed?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-speed-12/program.py b/root/organs10/physics/specialists/solved-speed-12/program.py new file mode 100644 index 0000000000000000000000000000000000000000..9477db0e7171afc2ed6f2a5090afebb9b765af1e --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-12/program.py @@ -0,0 +1,7 @@ +"""speed from kinetic energy, mass (kinetic energy (Wikidata Q46276)) +The maths organ (systems.py): solved from the system q0 = ((1)/(2))*q1*q2 **(2); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(kinetic_energy, mass): + return 1.4142135623731*kinetic_energy**0.5*mass**(-0.5) diff --git a/root/organs10/physics/specialists/solved-speed-12/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-speed-12/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-12/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-speed-13/organ_formula_v2.py b/root/organs10/physics/specialists/solved-speed-13/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-13/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-speed-13/panel.json b/root/organs10/physics/specialists/solved-speed-13/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..b6ea40cee7361a5cf46ebd8ebb0daa976efdd89a --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-13/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the speed for Strouhal number 107.961, frequency 59.4105 and characteristic length 5.77717?", + "a": "3.1791600820256143", + "valid": true, + "kind": "compute", + "expect": 3.1791600820256143, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105, 5.777165] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "Tell me the speed with Strouhal number of 107.961, frequency of 59.4105 and characteristic length of 5.77717.", + "a": "3.1791600820256143", + "valid": true, + "kind": "compute", + "expect": 3.1791600820256143, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105, 5.777165] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "What is the speed for Strouhal number 85.2416, frequency 160.878 and characteristic length 2.69428?", + "a": "5.084975341983345", + "valid": true, + "kind": "compute", + "expect": 5.084975341983345, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [85.241623, 160.878442, 2.69428] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "Tell me the speed with Strouhal number of 85.2416, frequency of 160.878 and characteristic length of 2.69428.", + "a": "5.084975341983345", + "valid": true, + "kind": "compute", + "expect": 5.084975341983345, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [85.241623, 160.878442, 2.69428] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "What is the speed for Strouhal number 45.6157, frequency 4.97131 and characteristic length 94.305?", + "a": "10.27759497808088", + "valid": true, + "kind": "compute", + "expect": 10.27759497808088, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [45.61567, 4.971307, 94.305046] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "Tell me the speed with Strouhal number of 45.6157, frequency of 4.97131 and characteristic length of 94.305.", + "a": "10.27759497808088", + "valid": true, + "kind": "compute", + "expect": 10.27759497808088, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [45.61567, 4.971307, 94.305046] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "What is the speed for Strouhal number 10.5638, frequency 198.839 and characteristic length 0.671697?", + "a": "12.643085733152049", + "valid": true, + "kind": "compute", + "expect": 12.643085733152049, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [10.563843, 198.838955, 0.671697] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "Tell me the speed with Strouhal number of 10.5638, frequency of 198.839 and characteristic length of 0.671697.", + "a": "12.643085733152049", + "valid": true, + "kind": "compute", + "expect": 12.643085733152049, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [10.563843, 198.838955, 0.671697] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "What is the speed for Strouhal number 21.7931, frequency 29.5106 and characteristic length 8.50341?", + "a": "11.514672847514374", + "valid": true, + "kind": "compute", + "expect": 11.514672847514374, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609, 8.503407] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "Tell me the speed with Strouhal number of 21.7931, frequency of 29.5106 and characteristic length of 8.50341.", + "a": "11.514672847514374", + "valid": true, + "kind": "compute", + "expect": 11.514672847514374, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609, 8.503407] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "What is the speed for Strouhal number 223.509, frequency 17.3414 and characteristic length 7.10705?", + "a": "0.5514164683902788", + "valid": true, + "kind": "compute", + "expect": 0.5514164683902788, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [223.508796, 17.341435, 7.107049] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "Tell me the speed with Strouhal number of 223.509, frequency of 17.3414 and characteristic length of 7.10705.", + "a": "0.5514164683902788", + "valid": true, + "kind": "compute", + "expect": 0.5514164683902788, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [223.508796, 17.341435, 7.107049] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "What is the speed for Strouhal number 107.961 and frequency 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the characteristic length is not given" + }, + { + "q": "Tell me the speed with Strouhal number of 85.2416 and frequency of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the characteristic length is not given" + }, + { + "q": "What is the speed of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the speed of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the speed with Strouhal number of -45.6157, frequency of 4.97131 and characteristic length of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Strouhal number: no example had one" + }, + { + "q": "Which speed is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the Strouhal number is 17.7888, frequency is 167.625 and the characteristic length is 10.3291, what is the speed?", + "a": "97.3314301270093", + "valid": true, + "kind": "compute", + "expect": 97.3314301270093, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [17.788778, 167.624948, 10.329054] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "Could you work out the speed given Strouhal number = 17.7888, frequency = 167.625, characteristic length = 10.3291?", + "a": "97.3314301270093", + "valid": true, + "kind": "compute", + "expect": 97.3314301270093, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [17.788778, 167.624948, 10.329054] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "I need the speed for a Strouhal number of 21.511, a frequency of 173.394 and a characteristic length of 251.084, please.", + "a": "2023.9106825818615", + "valid": true, + "kind": "compute", + "expect": 2023.9106825818615, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.511016, 173.393638, 251.084037] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "If the Strouhal number is 21.511, frequency is 173.394 and the characteristic length is 251.084, what is the speed?", + "a": "2023.9106825818615", + "valid": true, + "kind": "compute", + "expect": 2023.9106825818615, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.511016, 173.393638, 251.084037] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "Could you work out the speed given Strouhal number = 129.245, frequency = 1.18359, characteristic length = 15.9416?", + "a": "0.1459888878289837", + "valid": true, + "kind": "compute", + "expect": 0.1459888878289837, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [129.244984, 1.18359, 15.941612] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "I need the speed for a Strouhal number of 129.245, a frequency of 1.18359 and a characteristic length of 15.9416, please.", + "a": "0.1459888878289837", + "valid": true, + "kind": "compute", + "expect": 0.1459888878289837, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [129.244984, 1.18359, 15.941612] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "If the Strouhal number is 20.3966, frequency is 18.3169 and the characteristic length is 6.8386, what is the speed?", + "a": "6.141315193814264", + "valid": true, + "kind": "compute", + "expect": 6.141315193814264, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [20.396558, 18.316867, 6.838598] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "Could you work out the speed given Strouhal number = 20.3966, frequency = 18.3169, characteristic length = 6.8386?", + "a": "6.141315193814264", + "valid": true, + "kind": "compute", + "expect": 6.141315193814264, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [20.396558, 18.316867, 6.838598] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "I need the speed for a Strouhal number of 185.886, a frequency of 13.2373 and a characteristic length of 4.71941, please.", + "a": "0.3360784039769457", + "valid": true, + "kind": "compute", + "expect": 0.3360784039769457, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [185.886273, 13.237311, 4.719415] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "If the Strouhal number is 185.886, frequency is 13.2373 and the characteristic length is 4.71941, what is the speed?", + "a": "0.3360784039769457", + "valid": true, + "kind": "compute", + "expect": 0.3360784039769457, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [185.886273, 13.237311, 4.719415] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "Could you work out the speed given Strouhal number = 9.1061, frequency = 24.5721, characteristic length = 108.451?", + "a": "292.64569674251265", + "valid": true, + "kind": "compute", + "expect": 292.64569674251265, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [9.106104, 24.572086, 108.450792] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "I need the speed for a Strouhal number of 9.1061, a frequency of 24.5721 and a characteristic length of 108.451, please.", + "a": "292.64569674251265", + "valid": true, + "kind": "compute", + "expect": 292.64569674251265, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [9.106104, 24.572086, 108.450792] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "If the Strouhal number is 17.7888 and the frequency is 167.625, what is the speed?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the characteristic length is not given" + }, + { + "q": "Could you work out the speed given Strouhal number = 21.511, frequency = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the characteristic length is not given" + }, + { + "q": "Do you know the speed of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the speed of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the speed given Strouhal number = -129.245, frequency = 1.18359, characteristic length = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Strouhal number: no example had one" + }, + { + "q": "What is a typical speed?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-speed-13/program.py b/root/organs10/physics/specialists/solved-speed-13/program.py new file mode 100644 index 0000000000000000000000000000000000000000..bed751233deefc0a6405606041f5022390e10c1c --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-13/program.py @@ -0,0 +1,7 @@ +"""speed from Strouhal number, frequency, characteristic length (Strouhal number (Wikidata Q646627)) +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(strouhal_number, frequency, characteristic_length): + return 1.0*characteristic_length*frequency/strouhal_number diff --git a/root/organs10/physics/specialists/solved-speed-13/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-speed-13/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-13/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-speed-14/organ_formula_v2.py b/root/organs10/physics/specialists/solved-speed-14/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-14/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-speed-14/panel.json b/root/organs10/physics/specialists/solved-speed-14/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..864820fa0fbb37580e4e1139aa8d9b6b3b5a467d --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-14/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the speed for Péclet number for heat transfer 107.961, length 59.4105 and thermal diffusivity 5.77717?", + "a": "10.498254293702432", + "valid": true, + "kind": "compute", + "expect": 10.498254293702432, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105, 5.777165] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "Tell me the speed with Péclet number for heat transfer of 107.961, length of 59.4105 and thermal diffusivity of 5.77717.", + "a": "10.498254293702432", + "valid": true, + "kind": "compute", + "expect": 10.498254293702432, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105, 5.777165] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "What is the speed for Péclet number for heat transfer 85.2416, length 160.878 and thermal diffusivity 2.69428?", + "a": "1.4275672119863798", + "valid": true, + "kind": "compute", + "expect": 1.4275672119863798, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [85.241623, 160.878442, 2.69428] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "Tell me the speed with Péclet number for heat transfer of 85.2416, length of 160.878 and thermal diffusivity of 2.69428.", + "a": "1.4275672119863798", + "valid": true, + "kind": "compute", + "expect": 1.4275672119863798, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [85.241623, 160.878442, 2.69428] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "What is the speed for Péclet number for heat transfer 45.6157, length 4.97131 and thermal diffusivity 94.305?", + "a": "865.3232325447225", + "valid": true, + "kind": "compute", + "expect": 865.3232325447225, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [45.61567, 4.971307, 94.305046] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "Tell me the speed with Péclet number for heat transfer of 45.6157, length of 4.97131 and thermal diffusivity of 94.305.", + "a": "865.3232325447225", + "valid": true, + "kind": "compute", + "expect": 865.3232325447225, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [45.61567, 4.971307, 94.305046] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "What is the speed for Péclet number for heat transfer 10.5638, length 198.839 and thermal diffusivity 0.671697?", + "a": "0.03568568130708054", + "valid": true, + "kind": "compute", + "expect": 0.03568568130708054, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [10.563843, 198.838955, 0.671697] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "Tell me the speed with Péclet number for heat transfer of 10.5638, length of 198.839 and thermal diffusivity of 0.671697.", + "a": "0.03568568130708054", + "valid": true, + "kind": "compute", + "expect": 0.03568568130708054, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [10.563843, 198.838955, 0.671697] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "What is the speed for Péclet number for heat transfer 21.7931, length 29.5106 and thermal diffusivity 8.50341?", + "a": "6.279634237980215", + "valid": true, + "kind": "compute", + "expect": 6.279634237980215, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609, 8.503407] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "Tell me the speed with Péclet number for heat transfer of 21.7931, length of 29.5106 and thermal diffusivity of 8.50341.", + "a": "6.279634237980215", + "valid": true, + "kind": "compute", + "expect": 6.279634237980215, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609, 8.503407] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "What is the speed for Péclet number for heat transfer 223.509, length 17.3414 and thermal diffusivity 7.10705?", + "a": "91.60072422473009", + "valid": true, + "kind": "compute", + "expect": 91.60072422473009, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [223.508796, 17.341435, 7.107049] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "Tell me the speed with Péclet number for heat transfer of 223.509, length of 17.3414 and thermal diffusivity of 7.10705.", + "a": "91.60072422473009", + "valid": true, + "kind": "compute", + "expect": 91.60072422473009, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [223.508796, 17.341435, 7.107049] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "What is the speed for Péclet number for heat transfer 107.961 and length 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the thermal diffusivity is not given" + }, + { + "q": "Tell me the speed with Péclet number for heat transfer of 85.2416 and length of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the thermal diffusivity is not given" + }, + { + "q": "What is the speed of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the speed of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the speed with Péclet number for heat transfer of -45.6157, length of 4.97131 and thermal diffusivity of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Péclet number for heat transfer: no example had one" + }, + { + "q": "Which speed is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the Péclet number for heat transfer is 17.7888, length is 167.625 and the thermal diffusivity is 10.3291, what is the speed?", + "a": "1.0961450581531387", + "valid": true, + "kind": "compute", + "expect": 1.0961450581531387, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [17.788778, 167.624948, 10.329054] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "Could you work out the speed given Péclet number for heat transfer = 17.7888, length = 167.625, thermal diffusivity = 10.3291?", + "a": "1.0961450581531387", + "valid": true, + "kind": "compute", + "expect": 1.0961450581531387, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [17.788778, 167.624948, 10.329054] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "I need the speed for a Péclet number for heat transfer of 21.511, a length of 173.394 and a thermal diffusivity of 251.084, please.", + "a": "31.149197612202414", + "valid": true, + "kind": "compute", + "expect": 31.149197612202414, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.511016, 173.393638, 251.084037] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "If the Péclet number for heat transfer is 21.511, length is 173.394 and the thermal diffusivity is 251.084, what is the speed?", + "a": "31.149197612202414", + "valid": true, + "kind": "compute", + "expect": 31.149197612202414, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.511016, 173.393638, 251.084037] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "Could you work out the speed given Péclet number for heat transfer = 129.245, length = 1.18359, thermal diffusivity = 15.9416?", + "a": "1740.7830274187056", + "valid": true, + "kind": "compute", + "expect": 1740.7830274187056, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [129.244984, 1.18359, 15.941612] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "I need the speed for a Péclet number for heat transfer of 129.245, a length of 1.18359 and a thermal diffusivity of 15.9416, please.", + "a": "1740.7830274187056", + "valid": true, + "kind": "compute", + "expect": 1740.7830274187056, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [129.244984, 1.18359, 15.941612] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "If the Péclet number for heat transfer is 20.3966, length is 18.3169 and the thermal diffusivity is 6.8386, what is the speed?", + "a": "7.615050191987504", + "valid": true, + "kind": "compute", + "expect": 7.615050191987504, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [20.396558, 18.316867, 6.838598] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "Could you work out the speed given Péclet number for heat transfer = 20.3966, length = 18.3169, thermal diffusivity = 6.8386?", + "a": "7.615050191987504", + "valid": true, + "kind": "compute", + "expect": 7.615050191987504, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [20.396558, 18.316867, 6.838598] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "I need the speed for a Péclet number for heat transfer of 185.886, a length of 13.2373 and a thermal diffusivity of 4.71941, please.", + "a": "66.27285731134774", + "valid": true, + "kind": "compute", + "expect": 66.27285731134774, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [185.886273, 13.237311, 4.719415] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "If the Péclet number for heat transfer is 185.886, length is 13.2373 and the thermal diffusivity is 4.71941, what is the speed?", + "a": "66.27285731134774", + "valid": true, + "kind": "compute", + "expect": 66.27285731134774, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [185.886273, 13.237311, 4.719415] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "Could you work out the speed given Péclet number for heat transfer = 9.1061, length = 24.5721, thermal diffusivity = 108.451?", + "a": "40.19049097830896", + "valid": true, + "kind": "compute", + "expect": 40.19049097830896, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [9.106104, 24.572086, 108.450792] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "I need the speed for a Péclet number for heat transfer of 9.1061, a length of 24.5721 and a thermal diffusivity of 108.451, please.", + "a": "40.19049097830896", + "valid": true, + "kind": "compute", + "expect": 40.19049097830896, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [9.106104, 24.572086, 108.450792] (the system Péclet number for heat transfer (Wikidata Q899769))" + }, + { + "q": "If the Péclet number for heat transfer is 17.7888 and the length is 167.625, what is the speed?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the thermal diffusivity is not given" + }, + { + "q": "Could you work out the speed given Péclet number for heat transfer = 21.511, length = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the thermal diffusivity is not given" + }, + { + "q": "Do you know the speed of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the speed of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the speed given Péclet number for heat transfer = -129.245, length = 1.18359, thermal diffusivity = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Péclet number for heat transfer: no example had one" + }, + { + "q": "What is a typical speed?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-speed-14/program.py b/root/organs10/physics/specialists/solved-speed-14/program.py new file mode 100644 index 0000000000000000000000000000000000000000..8cd18b5555907d268942d833232dd9d42e842dfc --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-14/program.py @@ -0,0 +1,7 @@ +"""speed from Péclet number for heat transfer, length, thermal diffusivity (Péclet number for heat transfer (Wikidata Q899769)) +The maths organ (systems.py): solved from the system q0 = ((q1*q2)/(q3)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(p_clet_number_for_heat_transfer, length, thermal_diffusivity): + return 1.0*p_clet_number_for_heat_transfer*thermal_diffusivity/length diff --git a/root/organs10/physics/specialists/solved-speed-14/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-speed-14/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-14/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-speed-15/organ_formula_v2.py b/root/organs10/physics/specialists/solved-speed-15/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-15/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-speed-15/panel.json b/root/organs10/physics/specialists/solved-speed-15/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..351def2617796ecbf9711b2ce10bae5fccd643a5 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-15/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the speed for boundary layer thickness 107.961, radius of curvature 59.4105, Görtler number 5.77717 and kinematic viscosity 1.89126?", + "a": "0.07507550564475969", + "valid": true, + "kind": "compute", + "expect": 0.07507550564475969, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105, 5.777165, 1.891256] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "Tell me the speed with boundary layer thickness of 107.961, radius of curvature of 59.4105, Görtler number of 5.77717 and kinematic viscosity of 1.89126.", + "a": "0.07507550564475969", + "valid": true, + "kind": "compute", + "expect": 0.07507550564475969, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105, 5.777165, 1.891256] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "What is the speed for boundary layer thickness 2.69428, radius of curvature 48.921, Görtler number 157.223 and kinematic viscosity 35.6412?", + "a": "8862.376987716827", + "valid": true, + "kind": "compute", + "expect": 8862.376987716827, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [2.69428, 48.920991, 157.22256, 35.641157] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "Tell me the speed with boundary layer thickness of 2.69428, radius of curvature of 48.921, Görtler number of 157.223 and kinematic viscosity of 35.6412.", + "a": "8862.376987716827", + "valid": true, + "kind": "compute", + "expect": 8862.376987716827, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [2.69428, 48.920991, 157.22256, 35.641157] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "What is the speed for boundary layer thickness 0.318734, radius of curvature 9.56607, Görtler number 126.709 and kinematic viscosity 1.70503?", + "a": "3713.343027678174", + "valid": true, + "kind": "compute", + "expect": 3713.343027678174, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.318734, 9.566071, 126.709252, 1.705032] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "Tell me the speed with boundary layer thickness of 0.318734, radius of curvature of 9.56607, Görtler number of 126.709 and kinematic viscosity of 1.70503.", + "a": "3713.343027678174", + "valid": true, + "kind": "compute", + "expect": 3713.343027678174, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.318734, 9.566071, 126.709252, 1.705032] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "What is the speed for boundary layer thickness 87.781, radius of curvature 13.2084, Görtler number 246.329 and kinematic viscosity 20.3966?", + "a": "22.202253010974832", + "valid": true, + "kind": "compute", + "expect": 22.202253010974832, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [87.781013, 13.208419, 246.329053, 20.396558] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "Tell me the speed with boundary layer thickness of 87.781, radius of curvature of 13.2084, Görtler number of 246.329 and kinematic viscosity of 20.3966.", + "a": "22.202253010974832", + "valid": true, + "kind": "compute", + "expect": 22.202253010974832, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [87.781013, 13.208419, 246.329053, 20.396558] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "What is the speed for boundary layer thickness 156.502, radius of curvature 185.886, Görtler number 13.2373 and kinematic viscosity 4.71941?", + "a": "0.43504335468618416", + "valid": true, + "kind": "compute", + "expect": 0.43504335468618416, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [156.50181, 185.886273, 13.237311, 4.719415] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "Tell me the speed with boundary layer thickness of 156.502, radius of curvature of 185.886, Görtler number of 13.2373 and kinematic viscosity of 4.71941.", + "a": "0.43504335468618416", + "valid": true, + "kind": "compute", + "expect": 0.43504335468618416, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [156.50181, 185.886273, 13.237311, 4.719415] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "What is the speed for boundary layer thickness 24.5721, radius of curvature 108.451, Görtler number 1.69476 and kinematic viscosity 49.4843?", + "a": "7.170143709447331", + "valid": true, + "kind": "compute", + "expect": 7.170143709447331, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [24.572086, 108.450792, 1.694755, 49.484326] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "Tell me the speed with boundary layer thickness of 24.5721, radius of curvature of 108.451, Görtler number of 1.69476 and kinematic viscosity of 49.4843.", + "a": "7.170143709447331", + "valid": true, + "kind": "compute", + "expect": 7.170143709447331, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [24.572086, 108.450792, 1.694755, 49.484326] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "What is the speed for boundary layer thickness 107.961, radius of curvature 59.4105 and Görtler number 5.77717?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the kinematic viscosity is not given" + }, + { + "q": "Tell me the speed with boundary layer thickness of 2.69428, radius of curvature of 48.921 and Görtler number of 157.223.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the kinematic viscosity is not given" + }, + { + "q": "What is the speed of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the speed of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the speed with boundary layer thickness of -0.318734, radius of curvature of 9.56607, Görtler number of 126.709 and kinematic viscosity of 1.70503.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative boundary layer thickness: no example had one" + }, + { + "q": "Which speed is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the boundary layer thickness is 167.625, radius of curvature is 10.3291, Görtler number is 2.21571 and the kinematic viscosity is 58.5346, what is the speed?", + "a": "0.19206509526791216", + "valid": true, + "kind": "compute", + "expect": 0.19206509526791216, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [167.624948, 10.329054, 2.215713, 58.534594] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "Could you work out the speed given boundary layer thickness = 167.625, radius of curvature = 10.3291, Görtler number = 2.21571, kinematic viscosity = 58.5346?", + "a": "0.19206509526791216", + "valid": true, + "kind": "compute", + "expect": 0.19206509526791216, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [167.624948, 10.329054, 2.215713, 58.534594] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "I need the speed for a boundary layer thickness of 8.53158, a radius of curvature of 124.718, a Görtler number of 1.91195 and a kinematic viscosity of 82.2401, please.", + "a": "70.46627419336463", + "valid": true, + "kind": "compute", + "expect": 70.46627419336463, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.531577, 124.718185, 1.911952, 82.240072] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "If the boundary layer thickness is 8.53158, radius of curvature is 124.718, Görtler number is 1.91195 and the kinematic viscosity is 82.2401, what is the speed?", + "a": "70.46627419336463", + "valid": true, + "kind": "compute", + "expect": 70.46627419336463, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.531577, 124.718185, 1.911952, 82.240072] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "Could you work out the speed given boundary layer thickness = 81.1967, radius of curvature = 6.98086, Görtler number = 0.551236, kinematic viscosity = 2.88512?", + "a": "0.005743121643726253", + "valid": true, + "kind": "compute", + "expect": 0.005743121643726253, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [81.196705, 6.980857, 0.551236, 2.88512] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "I need the speed for a boundary layer thickness of 81.1967, a radius of curvature of 6.98086, a Görtler number of 0.551236 and a kinematic viscosity of 2.88512, please.", + "a": "0.005743121643726253", + "valid": true, + "kind": "compute", + "expect": 0.005743121643726253, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [81.196705, 6.980857, 0.551236, 2.88512] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "If the boundary layer thickness is 1.16997, radius of curvature is 1.14865, Görtler number is 21.7931 and the kinematic viscosity is 29.5106, what is the speed?", + "a": "544.6669463235817", + "valid": true, + "kind": "compute", + "expect": 544.6669463235817, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.169968, 1.148652, 21.793126, 29.510609] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "Could you work out the speed given boundary layer thickness = 1.16997, radius of curvature = 1.14865, Görtler number = 21.7931, kinematic viscosity = 29.5106?", + "a": "544.6669463235817", + "valid": true, + "kind": "compute", + "expect": 544.6669463235817, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.169968, 1.148652, 21.793126, 29.510609] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "I need the speed for a boundary layer thickness of 223.509, a radius of curvature of 17.3414, a Görtler number of 7.10705 and a kinematic viscosity of 30.2507, please.", + "a": "0.2679328985137236", + "valid": true, + "kind": "compute", + "expect": 0.2679328985137236, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [223.508796, 17.341435, 7.107049, 30.250749] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "If the boundary layer thickness is 223.509, radius of curvature is 17.3414, Görtler number is 7.10705 and the kinematic viscosity is 30.2507, what is the speed?", + "a": "0.2679328985137236", + "valid": true, + "kind": "compute", + "expect": 0.2679328985137236, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [223.508796, 17.341435, 7.107049, 30.250749] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "Could you work out the speed given boundary layer thickness = 0.869111, radius of curvature = 39.1753, Görtler number = 0.432217, kinematic viscosity = 16.6571?", + "a": "55.61514503747118", + "valid": true, + "kind": "compute", + "expect": 55.61514503747118, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.869111, 39.175263, 0.432217, 16.657051] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "I need the speed for a boundary layer thickness of 0.869111, a radius of curvature of 39.1753, a Görtler number of 0.432217 and a kinematic viscosity of 16.6571, please.", + "a": "55.61514503747118", + "valid": true, + "kind": "compute", + "expect": 55.61514503747118, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.869111, 39.175263, 0.432217, 16.657051] (the system Görtler number (Wikidata Q102723670))" + }, + { + "q": "If the boundary layer thickness is 167.625, radius of curvature is 10.3291 and the Görtler number is 2.21571, what is the speed?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the kinematic viscosity is not given" + }, + { + "q": "Could you work out the speed given boundary layer thickness = 8.53158, radius of curvature = 124.718, Görtler number = 1.91195?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the kinematic viscosity is not given" + }, + { + "q": "Do you know the speed of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the speed of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the speed given boundary layer thickness = -81.1967, radius of curvature = 6.98086, Görtler number = 0.551236, kinematic viscosity = 2.88512?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative boundary layer thickness: no example had one" + }, + { + "q": "What is a typical speed?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-speed-15/program.py b/root/organs10/physics/specialists/solved-speed-15/program.py new file mode 100644 index 0000000000000000000000000000000000000000..c3d31ca3c5475d219f66e7945d6b2cad82a6daa9 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-15/program.py @@ -0,0 +1,7 @@ +"""speed from boundary layer thickness, radius of curvature, Görtler number, kinematic viscosity (Görtler number (Wikidata Q102723670)) +The maths organ (systems.py): solved from the system q2 = ((q3*q0)/(q4))*sqrt(((q0)/(q1))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(boundary_layer_thickness, radius_of_curvature, g_rtler_number, kinematic_viscosity): + return 1.0*boundary_layer_thickness**(-1.5)*g_rtler_number*kinematic_viscosity*radius_of_curvature**0.5 diff --git a/root/organs10/physics/specialists/solved-speed-15/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-speed-15/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-15/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-speed-16/organ_formula_v2.py b/root/organs10/physics/specialists/solved-speed-16/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-16/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-speed-16/panel.json b/root/organs10/physics/specialists/solved-speed-16/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..13c2a9f694c7582bf5c9e1a6e5d8f0f60dea77da --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-16/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the speed for electric field parameter 107.961, electric field strength 59.4105 and magnetic flux density 5.77717?", + "a": "0.09525393194179609", + "valid": true, + "kind": "compute", + "expect": 0.09525393194179609, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105, 5.777165] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Tell me the speed with electric field parameter of 107.961, electric field strength of 59.4105 and magnetic flux density of 5.77717.", + "a": "0.09525393194179609", + "valid": true, + "kind": "compute", + "expect": 0.09525393194179609, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105, 5.777165] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "What is the speed for electric field parameter 85.2416, electric field strength 160.878 and magnetic flux density 2.69428?", + "a": "0.7004924122686708", + "valid": true, + "kind": "compute", + "expect": 0.7004924122686708, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [85.241623, 160.878442, 2.69428] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Tell me the speed with electric field parameter of 85.2416, electric field strength of 160.878 and magnetic flux density of 2.69428.", + "a": "0.7004924122686708", + "valid": true, + "kind": "compute", + "expect": 0.7004924122686708, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [85.241623, 160.878442, 2.69428] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "What is the speed for electric field parameter 45.6157, electric field strength 4.97131 and magnetic flux density 94.305?", + "a": "0.001155637526406431", + "valid": true, + "kind": "compute", + "expect": 0.001155637526406431, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [45.61567, 4.971307, 94.305046] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Tell me the speed with electric field parameter of 45.6157, electric field strength of 4.97131 and magnetic flux density of 94.305.", + "a": "0.001155637526406431", + "valid": true, + "kind": "compute", + "expect": 0.001155637526406431, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [45.61567, 4.971307, 94.305046] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "What is the speed for electric field parameter 10.5638, electric field strength 198.839 and magnetic flux density 0.671697?", + "a": "28.022443831038363", + "valid": true, + "kind": "compute", + "expect": 28.022443831038363, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [10.563843, 198.838955, 0.671697] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Tell me the speed with electric field parameter of 10.5638, electric field strength of 198.839 and magnetic flux density of 0.671697.", + "a": "28.022443831038363", + "valid": true, + "kind": "compute", + "expect": 28.022443831038363, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [10.563843, 198.838955, 0.671697] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "What is the speed for electric field parameter 21.7931, electric field strength 29.5106 and magnetic flux density 8.50341?", + "a": "0.15924494359111602", + "valid": true, + "kind": "compute", + "expect": 0.15924494359111602, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609, 8.503407] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Tell me the speed with electric field parameter of 21.7931, electric field strength of 29.5106 and magnetic flux density of 8.50341.", + "a": "0.15924494359111602", + "valid": true, + "kind": "compute", + "expect": 0.15924494359111602, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609, 8.503407] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "What is the speed for electric field parameter 223.509, electric field strength 17.3414 and magnetic flux density 7.10705?", + "a": "0.010916944254137492", + "valid": true, + "kind": "compute", + "expect": 0.010916944254137492, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [223.508796, 17.341435, 7.107049] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Tell me the speed with electric field parameter of 223.509, electric field strength of 17.3414 and magnetic flux density of 7.10705.", + "a": "0.010916944254137492", + "valid": true, + "kind": "compute", + "expect": 0.010916944254137492, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [223.508796, 17.341435, 7.107049] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "What is the speed for electric field parameter 107.961 and electric field strength 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic flux density is not given" + }, + { + "q": "Tell me the speed with electric field parameter of 85.2416 and electric field strength of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic flux density is not given" + }, + { + "q": "What is the speed of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the speed of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the speed with electric field parameter of -45.6157, electric field strength of 4.97131 and magnetic flux density of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric field parameter: no example had one" + }, + { + "q": "Which speed is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the electric field parameter is 17.7888, electric field strength is 167.625 and the magnetic flux density is 10.3291, what is the speed?", + "a": "0.9122880156800318", + "valid": true, + "kind": "compute", + "expect": 0.9122880156800318, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [17.788778, 167.624948, 10.329054] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Could you work out the speed given electric field parameter = 17.7888, electric field strength = 167.625, magnetic flux density = 10.3291?", + "a": "0.9122880156800318", + "valid": true, + "kind": "compute", + "expect": 0.9122880156800318, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [17.788778, 167.624948, 10.329054] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "I need the speed for an electric field parameter of 21.511, an electric field strength of 173.394 and a magnetic flux density of 251.084, please.", + "a": "0.032103555682225955", + "valid": true, + "kind": "compute", + "expect": 0.032103555682225955, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.511016, 173.393638, 251.084037] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "If the electric field parameter is 21.511, electric field strength is 173.394 and the magnetic flux density is 251.084, what is the speed?", + "a": "0.032103555682225955", + "valid": true, + "kind": "compute", + "expect": 0.032103555682225955, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.511016, 173.393638, 251.084037] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Could you work out the speed given electric field parameter = 129.245, electric field strength = 1.18359, magnetic flux density = 15.9416?", + "a": "0.0005744541302673633", + "valid": true, + "kind": "compute", + "expect": 0.0005744541302673633, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [129.244984, 1.18359, 15.941612] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "I need the speed for an electric field parameter of 129.245, an electric field strength of 1.18359 and a magnetic flux density of 15.9416, please.", + "a": "0.0005744541302673633", + "valid": true, + "kind": "compute", + "expect": 0.0005744541302673633, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [129.244984, 1.18359, 15.941612] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "If the electric field parameter is 20.3966, electric field strength is 18.3169 and the magnetic flux density is 6.8386, what is the speed?", + "a": "0.13131889807531302", + "valid": true, + "kind": "compute", + "expect": 0.13131889807531302, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [20.396558, 18.316867, 6.838598] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Could you work out the speed given electric field parameter = 20.3966, electric field strength = 18.3169, magnetic flux density = 6.8386?", + "a": "0.13131889807531302", + "valid": true, + "kind": "compute", + "expect": 0.13131889807531302, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [20.396558, 18.316867, 6.838598] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "I need the speed for an electric field parameter of 185.886, an electric field strength of 13.2373 and a magnetic flux density of 4.71941, please.", + "a": "0.015089133629806127", + "valid": true, + "kind": "compute", + "expect": 0.015089133629806127, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [185.886273, 13.237311, 4.719415] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "If the electric field parameter is 185.886, electric field strength is 13.2373 and the magnetic flux density is 4.71941, what is the speed?", + "a": "0.015089133629806127", + "valid": true, + "kind": "compute", + "expect": 0.015089133629806127, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [185.886273, 13.237311, 4.719415] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Could you work out the speed given electric field parameter = 9.1061, electric field strength = 24.5721, magnetic flux density = 108.451?", + "a": "0.024881507432683664", + "valid": true, + "kind": "compute", + "expect": 0.024881507432683664, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [9.106104, 24.572086, 108.450792] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "I need the speed for an electric field parameter of 9.1061, an electric field strength of 24.5721 and a magnetic flux density of 108.451, please.", + "a": "0.024881507432683664", + "valid": true, + "kind": "compute", + "expect": 0.024881507432683664, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [9.106104, 24.572086, 108.450792] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "If the electric field parameter is 17.7888 and the electric field strength is 167.625, what is the speed?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic flux density is not given" + }, + { + "q": "Could you work out the speed given electric field parameter = 21.511, electric field strength = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic flux density is not given" + }, + { + "q": "Do you know the speed of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the speed of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the speed given electric field parameter = -129.245, electric field strength = 1.18359, magnetic flux density = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric field parameter: no example had one" + }, + { + "q": "What is a typical speed?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-speed-16/program.py b/root/organs10/physics/specialists/solved-speed-16/program.py new file mode 100644 index 0000000000000000000000000000000000000000..91018f0ff20b0576367423525df2388b7f2967d9 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-16/program.py @@ -0,0 +1,7 @@ +"""speed from electric field parameter, electric field strength, magnetic flux density (electric field parameter (Wikidata Q105221927)) +The maths organ (systems.py): solved from the system q0 = ((q1)/(q2*q3)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(electric_field_parameter, electric_field_strength, magnetic_flux_density): + return 1.0*electric_field_strength/(electric_field_parameter*magnetic_flux_density) diff --git a/root/organs10/physics/specialists/solved-speed-16/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-speed-16/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-16/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-speed-17/organ_formula_v2.py b/root/organs10/physics/specialists/solved-speed-17/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-17/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-speed-17/panel.json b/root/organs10/physics/specialists/solved-speed-17/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..be76e8473d60b0c8911f83a54ab568d78908c338 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-17/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the speed for Péclet number for mass transfer 107.961, characteristic length 59.4105 and mass diffusivity 5.77717?", + "a": "10.498254293702432", + "valid": true, + "kind": "compute", + "expect": 10.498254293702432, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105, 5.777165] (the system Péclet number for mass transfer (Wikidata Q104542217))" + }, + { + "q": "Tell me the speed with Péclet number for mass transfer of 107.961, characteristic length of 59.4105 and mass diffusivity of 5.77717.", + "a": "10.498254293702432", + "valid": true, + "kind": "compute", + "expect": 10.498254293702432, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105, 5.777165] (the system Péclet number for mass transfer (Wikidata Q104542217))" + }, + { + "q": "What is the speed for Péclet number for mass transfer 85.2416, characteristic length 160.878 and mass diffusivity 2.69428?", + "a": "1.4275672119863798", + "valid": true, + "kind": "compute", + "expect": 1.4275672119863798, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [85.241623, 160.878442, 2.69428] (the system Péclet number for mass transfer (Wikidata Q104542217))" + }, + { + "q": "Tell me the speed with Péclet number for mass transfer of 85.2416, characteristic length of 160.878 and mass diffusivity of 2.69428.", + "a": "1.4275672119863798", + "valid": true, + "kind": "compute", + "expect": 1.4275672119863798, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [85.241623, 160.878442, 2.69428] (the system Péclet number for mass transfer (Wikidata Q104542217))" + }, + { + "q": "What is the speed for Péclet number for mass transfer 45.6157, characteristic length 4.97131 and mass diffusivity 94.305?", + "a": "865.3232325447225", + "valid": true, + "kind": "compute", + "expect": 865.3232325447225, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [45.61567, 4.971307, 94.305046] (the system Péclet number for mass transfer (Wikidata Q104542217))" + }, + { + "q": "Tell me the speed with Péclet number for mass transfer of 45.6157, characteristic length of 4.97131 and mass diffusivity of 94.305.", + "a": "865.3232325447225", + "valid": true, + "kind": "compute", + "expect": 865.3232325447225, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [45.61567, 4.971307, 94.305046] (the system Péclet number for mass transfer (Wikidata Q104542217))" + }, + { + "q": "What is the speed for Péclet number for mass transfer 10.5638, characteristic length 198.839 and mass diffusivity 0.671697?", + "a": "0.03568568130708054", + "valid": true, + "kind": "compute", + "expect": 0.03568568130708054, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [10.563843, 198.838955, 0.671697] (the system Péclet number for mass transfer (Wikidata Q104542217))" + }, + { + "q": "Tell me the speed with Péclet number for mass transfer of 10.5638, characteristic length of 198.839 and mass diffusivity of 0.671697.", + "a": "0.03568568130708054", + "valid": true, + "kind": "compute", + "expect": 0.03568568130708054, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [10.563843, 198.838955, 0.671697] (the system Péclet number for mass transfer (Wikidata Q104542217))" + }, + { + "q": "What is the speed for Péclet number for mass transfer 21.7931, characteristic length 29.5106 and mass diffusivity 8.50341?", + "a": "6.279634237980215", + "valid": true, + "kind": "compute", + "expect": 6.279634237980215, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609, 8.503407] (the system Péclet number for mass transfer (Wikidata Q104542217))" + }, + { + "q": "Tell me the speed with Péclet number for mass transfer of 21.7931, characteristic length of 29.5106 and mass diffusivity of 8.50341.", + "a": "6.279634237980215", + "valid": true, + "kind": "compute", + "expect": 6.279634237980215, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609, 8.503407] (the system Péclet number for mass transfer (Wikidata Q104542217))" + }, + { + "q": "What is the speed for Péclet number for mass transfer 223.509, characteristic length 17.3414 and mass diffusivity 7.10705?", + "a": "91.60072422473009", + "valid": true, + "kind": "compute", + "expect": 91.60072422473009, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [223.508796, 17.341435, 7.107049] (the system Péclet number for mass transfer (Wikidata Q104542217))" + }, + { + "q": "Tell me the speed with Péclet number for mass transfer of 223.509, characteristic length of 17.3414 and mass diffusivity of 7.10705.", + "a": "91.60072422473009", + "valid": true, + "kind": "compute", + "expect": 91.60072422473009, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [223.508796, 17.341435, 7.107049] (the system Péclet number for mass transfer (Wikidata Q104542217))" + }, + { + "q": "What is the speed for Péclet number for mass transfer 107.961 and characteristic length 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass diffusivity is not given" + }, + { + "q": "Tell me the speed with Péclet number for mass transfer of 85.2416 and characteristic length of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass diffusivity is not given" + }, + { + "q": "What is the speed of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the speed of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the speed with Péclet number for mass transfer of -45.6157, characteristic length of 4.97131 and mass diffusivity of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Péclet number for mass transfer: no example had one" + }, + { + "q": "Which speed is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the Péclet number for mass transfer is 17.7888, characteristic length is 167.625 and the mass diffusivity is 10.3291, what is the speed?", + "a": "1.0961450581531387", + "valid": true, + "kind": "compute", + "expect": 1.0961450581531387, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [17.788778, 167.624948, 10.329054] (the system Péclet number for mass transfer (Wikidata Q104542217))" + }, + { + "q": "Could you work out the speed given Péclet number for mass transfer = 17.7888, characteristic length = 167.625, mass diffusivity = 10.3291?", + "a": "1.0961450581531387", + "valid": true, + "kind": "compute", + "expect": 1.0961450581531387, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [17.788778, 167.624948, 10.329054] (the system Péclet number for mass transfer (Wikidata Q104542217))" + }, + { + "q": "I need the speed for a Péclet number for mass transfer of 21.511, a characteristic length of 173.394 and a mass diffusivity of 251.084, please.", + "a": "31.149197612202414", + "valid": true, + "kind": "compute", + "expect": 31.149197612202414, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.511016, 173.393638, 251.084037] (the system Péclet number for mass transfer (Wikidata Q104542217))" + }, + { + "q": "If the Péclet number for mass transfer is 21.511, characteristic length is 173.394 and the mass diffusivity is 251.084, what is the speed?", + "a": "31.149197612202414", + "valid": true, + "kind": "compute", + "expect": 31.149197612202414, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.511016, 173.393638, 251.084037] (the system Péclet number for mass transfer (Wikidata Q104542217))" + }, + { + "q": "Could you work out the speed given Péclet number for mass transfer = 129.245, characteristic length = 1.18359, mass diffusivity = 15.9416?", + "a": "1740.7830274187056", + "valid": true, + "kind": "compute", + "expect": 1740.7830274187056, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [129.244984, 1.18359, 15.941612] (the system Péclet number for mass transfer (Wikidata Q104542217))" + }, + { + "q": "I need the speed for a Péclet number for mass transfer of 129.245, a characteristic length of 1.18359 and a mass diffusivity of 15.9416, please.", + "a": "1740.7830274187056", + "valid": true, + "kind": "compute", + "expect": 1740.7830274187056, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [129.244984, 1.18359, 15.941612] (the system Péclet number for mass transfer (Wikidata Q104542217))" + }, + { + "q": "If the Péclet number for mass transfer is 20.3966, characteristic length is 18.3169 and the mass diffusivity is 6.8386, what is the speed?", + "a": "7.615050191987504", + "valid": true, + "kind": "compute", + "expect": 7.615050191987504, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [20.396558, 18.316867, 6.838598] (the system Péclet number for mass transfer (Wikidata Q104542217))" + }, + { + "q": "Could you work out the speed given Péclet number for mass transfer = 20.3966, characteristic length = 18.3169, mass diffusivity = 6.8386?", + "a": "7.615050191987504", + "valid": true, + "kind": "compute", + "expect": 7.615050191987504, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [20.396558, 18.316867, 6.838598] (the system Péclet number for mass transfer (Wikidata Q104542217))" + }, + { + "q": "I need the speed for a Péclet number for mass transfer of 185.886, a characteristic length of 13.2373 and a mass diffusivity of 4.71941, please.", + "a": "66.27285731134774", + "valid": true, + "kind": "compute", + "expect": 66.27285731134774, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [185.886273, 13.237311, 4.719415] (the system Péclet number for mass transfer (Wikidata Q104542217))" + }, + { + "q": "If the Péclet number for mass transfer is 185.886, characteristic length is 13.2373 and the mass diffusivity is 4.71941, what is the speed?", + "a": "66.27285731134774", + "valid": true, + "kind": "compute", + "expect": 66.27285731134774, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [185.886273, 13.237311, 4.719415] (the system Péclet number for mass transfer (Wikidata Q104542217))" + }, + { + "q": "Could you work out the speed given Péclet number for mass transfer = 9.1061, characteristic length = 24.5721, mass diffusivity = 108.451?", + "a": "40.19049097830896", + "valid": true, + "kind": "compute", + "expect": 40.19049097830896, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [9.106104, 24.572086, 108.450792] (the system Péclet number for mass transfer (Wikidata Q104542217))" + }, + { + "q": "I need the speed for a Péclet number for mass transfer of 9.1061, a characteristic length of 24.5721 and a mass diffusivity of 108.451, please.", + "a": "40.19049097830896", + "valid": true, + "kind": "compute", + "expect": 40.19049097830896, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [9.106104, 24.572086, 108.450792] (the system Péclet number for mass transfer (Wikidata Q104542217))" + }, + { + "q": "If the Péclet number for mass transfer is 17.7888 and the characteristic length is 167.625, what is the speed?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass diffusivity is not given" + }, + { + "q": "Could you work out the speed given Péclet number for mass transfer = 21.511, characteristic length = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass diffusivity is not given" + }, + { + "q": "Do you know the speed of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the speed of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the speed given Péclet number for mass transfer = -129.245, characteristic length = 1.18359, mass diffusivity = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Péclet number for mass transfer: no example had one" + }, + { + "q": "What is a typical speed?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-speed-17/program.py b/root/organs10/physics/specialists/solved-speed-17/program.py new file mode 100644 index 0000000000000000000000000000000000000000..69b690f4b2d6a5ecac31abf8a5857e4bafea29bc --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-17/program.py @@ -0,0 +1,7 @@ +"""speed from Péclet number for mass transfer, characteristic length, mass diffusivity (Péclet number for mass transfer (Wikidata Q104542217)) +The maths organ (systems.py): solved from the system q0 = ((q1*q2)/(q3)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(p_clet_number_for_mass_transfer, characteristic_length, mass_diffusivity): + return 1.0*mass_diffusivity*p_clet_number_for_mass_transfer/characteristic_length diff --git a/root/organs10/physics/specialists/solved-speed-17/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-speed-17/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-17/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-speed-18/organ_formula_v2.py b/root/organs10/physics/specialists/solved-speed-18/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-18/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-speed-18/panel.json b/root/organs10/physics/specialists/solved-speed-18/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..34979f28ed9343055f3f0aa69026817b0598ec3a --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-18/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the speed for Boussinesq number 107.961, acceleration of free fall 59.4105 and characteristic length 5.77717?", + "a": "2828.5869153960098", + "valid": true, + "kind": "compute", + "expect": 2828.5869153960098, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105, 5.777165] (the system Boussinesq number (Wikidata Q2650387))" + }, + { + "q": "Tell me the speed with Boussinesq number of 107.961, acceleration of free fall of 59.4105 and characteristic length of 5.77717.", + "a": "2828.5869153960098", + "valid": true, + "kind": "compute", + "expect": 2828.5869153960098, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105, 5.777165] (the system Boussinesq number (Wikidata Q2650387))" + }, + { + "q": "What is the speed for Boussinesq number 85.2416, acceleration of free fall 160.878 and characteristic length 2.69428?", + "a": "2509.78769356597", + "valid": true, + "kind": "compute", + "expect": 2509.78769356597, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [85.241623, 160.878442, 2.69428] (the system Boussinesq number (Wikidata Q2650387))" + }, + { + "q": "Tell me the speed with Boussinesq number of 85.2416, acceleration of free fall of 160.878 and characteristic length of 2.69428.", + "a": "2509.78769356597", + "valid": true, + "kind": "compute", + "expect": 2509.78769356597, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [85.241623, 160.878442, 2.69428] (the system Boussinesq number (Wikidata Q2650387))" + }, + { + "q": "What is the speed for Boussinesq number 45.6157, acceleration of free fall 4.97131 and characteristic length 94.305?", + "a": "1396.792312348953", + "valid": true, + "kind": "compute", + "expect": 1396.792312348953, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [45.61567, 4.971307, 94.305046] (the system Boussinesq number (Wikidata Q2650387))" + }, + { + "q": "Tell me the speed with Boussinesq number of 45.6157, acceleration of free fall of 4.97131 and characteristic length of 94.305.", + "a": "1396.792312348953", + "valid": true, + "kind": "compute", + "expect": 1396.792312348953, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [45.61567, 4.971307, 94.305046] (the system Boussinesq number (Wikidata Q2650387))" + }, + { + "q": "What is the speed for Boussinesq number 10.5638, acceleration of free fall 198.839 and characteristic length 0.671697?", + "a": "172.65311991067614", + "valid": true, + "kind": "compute", + "expect": 172.65311991067614, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [10.563843, 198.838955, 0.671697] (the system Boussinesq number (Wikidata Q2650387))" + }, + { + "q": "Tell me the speed with Boussinesq number of 10.5638, acceleration of free fall of 198.839 and characteristic length of 0.671697.", + "a": "172.65311991067614", + "valid": true, + "kind": "compute", + "expect": 172.65311991067614, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [10.563843, 198.838955, 0.671697] (the system Boussinesq number (Wikidata Q2650387))" + }, + { + "q": "What is the speed for Boussinesq number 21.7931, acceleration of free fall 29.5106 and characteristic length 8.50341?", + "a": "488.2251066244279", + "valid": true, + "kind": "compute", + "expect": 488.2251066244279, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609, 8.503407] (the system Boussinesq number (Wikidata Q2650387))" + }, + { + "q": "Tell me the speed with Boussinesq number of 21.7931, acceleration of free fall of 29.5106 and characteristic length of 8.50341.", + "a": "488.2251066244279", + "valid": true, + "kind": "compute", + "expect": 488.2251066244279, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609, 8.503407] (the system Boussinesq number (Wikidata Q2650387))" + }, + { + "q": "What is the speed for Boussinesq number 223.509, acceleration of free fall 17.3414 and characteristic length 7.10705?", + "a": "3509.108475658415", + "valid": true, + "kind": "compute", + "expect": 3509.108475658415, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [223.508796, 17.341435, 7.107049] (the system Boussinesq number (Wikidata Q2650387))" + }, + { + "q": "Tell me the speed with Boussinesq number of 223.509, acceleration of free fall of 17.3414 and characteristic length of 7.10705.", + "a": "3509.108475658415", + "valid": true, + "kind": "compute", + "expect": 3509.108475658415, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [223.508796, 17.341435, 7.107049] (the system Boussinesq number (Wikidata Q2650387))" + }, + { + "q": "What is the speed for Boussinesq number 107.961 and acceleration of free fall 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the characteristic length is not given" + }, + { + "q": "Tell me the speed with Boussinesq number of 85.2416 and acceleration of free fall of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the characteristic length is not given" + }, + { + "q": "What is the speed of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the speed of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the speed with Boussinesq number of -45.6157, acceleration of free fall of 4.97131 and characteristic length of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Boussinesq number: no example had one" + }, + { + "q": "Which speed is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the Boussinesq number is 17.7888, acceleration of free fall is 167.625 and the characteristic length is 10.3291, what is the speed?", + "a": "1046.7928021247517", + "valid": true, + "kind": "compute", + "expect": 1046.7928021247517, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [17.788778, 167.624948, 10.329054] (the system Boussinesq number (Wikidata Q2650387))" + }, + { + "q": "Could you work out the speed given Boussinesq number = 17.7888, acceleration of free fall = 167.625, characteristic length = 10.3291?", + "a": "1046.7928021247517", + "valid": true, + "kind": "compute", + "expect": 1046.7928021247517, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [17.788778, 167.624948, 10.329054] (the system Boussinesq number (Wikidata Q2650387))" + }, + { + "q": "I need the speed for a Boussinesq number of 21.511, an acceleration of free fall of 173.394 and a characteristic length of 251.084, please.", + "a": "6347.490355581582", + "valid": true, + "kind": "compute", + "expect": 6347.490355581582, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.511016, 173.393638, 251.084037] (the system Boussinesq number (Wikidata Q2650387))" + }, + { + "q": "If the Boussinesq number is 21.511, acceleration of free fall is 173.394 and the characteristic length is 251.084, what is the speed?", + "a": "6347.490355581582", + "valid": true, + "kind": "compute", + "expect": 6347.490355581582, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.511016, 173.393638, 251.084037] (the system Boussinesq number (Wikidata Q2650387))" + }, + { + "q": "Could you work out the speed given Boussinesq number = 129.245, acceleration of free fall = 1.18359, characteristic length = 15.9416?", + "a": "793.9541851580137", + "valid": true, + "kind": "compute", + "expect": 793.9541851580137, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [129.244984, 1.18359, 15.941612] (the system Boussinesq number (Wikidata Q2650387))" + }, + { + "q": "I need the speed for a Boussinesq number of 129.245, an acceleration of free fall of 1.18359 and a characteristic length of 15.9416, please.", + "a": "793.9541851580137", + "valid": true, + "kind": "compute", + "expect": 793.9541851580137, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [129.244984, 1.18359, 15.941612] (the system Boussinesq number (Wikidata Q2650387))" + }, + { + "q": "If the Boussinesq number is 20.3966, acceleration of free fall is 18.3169 and the characteristic length is 6.8386, what is the speed?", + "a": "322.8353010324579", + "valid": true, + "kind": "compute", + "expect": 322.8353010324579, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [20.396558, 18.316867, 6.838598] (the system Boussinesq number (Wikidata Q2650387))" + }, + { + "q": "Could you work out the speed given Boussinesq number = 20.3966, acceleration of free fall = 18.3169, characteristic length = 6.8386?", + "a": "322.8353010324579", + "valid": true, + "kind": "compute", + "expect": 322.8353010324579, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [20.396558, 18.316867, 6.838598] (the system Boussinesq number (Wikidata Q2650387))" + }, + { + "q": "I need the speed for a Boussinesq number of 185.886, an acceleration of free fall of 13.2373 and a characteristic length of 4.71941, please.", + "a": "2077.8121432559765", + "valid": true, + "kind": "compute", + "expect": 2077.8121432559765, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [185.886273, 13.237311, 4.719415] (the system Boussinesq number (Wikidata Q2650387))" + }, + { + "q": "If the Boussinesq number is 185.886, acceleration of free fall is 13.2373 and the characteristic length is 4.71941, what is the speed?", + "a": "2077.8121432559765", + "valid": true, + "kind": "compute", + "expect": 2077.8121432559765, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [185.886273, 13.237311, 4.719415] (the system Boussinesq number (Wikidata Q2650387))" + }, + { + "q": "Could you work out the speed given Boussinesq number = 9.1061, acceleration of free fall = 24.5721, characteristic length = 108.451?", + "a": "664.7907651322727", + "valid": true, + "kind": "compute", + "expect": 664.7907651322727, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [9.106104, 24.572086, 108.450792] (the system Boussinesq number (Wikidata Q2650387))" + }, + { + "q": "I need the speed for a Boussinesq number of 9.1061, an acceleration of free fall of 24.5721 and a characteristic length of 108.451, please.", + "a": "664.7907651322727", + "valid": true, + "kind": "compute", + "expect": 664.7907651322727, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [9.106104, 24.572086, 108.450792] (the system Boussinesq number (Wikidata Q2650387))" + }, + { + "q": "If the Boussinesq number is 17.7888 and the acceleration of free fall is 167.625, what is the speed?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the characteristic length is not given" + }, + { + "q": "Could you work out the speed given Boussinesq number = 21.511, acceleration of free fall = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the characteristic length is not given" + }, + { + "q": "Do you know the speed of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the speed of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the speed given Boussinesq number = -129.245, acceleration of free fall = 1.18359, characteristic length = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Boussinesq number: no example had one" + }, + { + "q": "What is a typical speed?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-speed-18/program.py b/root/organs10/physics/specialists/solved-speed-18/program.py new file mode 100644 index 0000000000000000000000000000000000000000..d4278eace4899522482e648430f655c43c4b504d --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-18/program.py @@ -0,0 +1,7 @@ +"""speed from Boussinesq number, acceleration of free fall, characteristic length (Boussinesq number (Wikidata Q2650387)) +The maths organ (systems.py): solved from the system q0 = ((q1)/(sqrt(2*q2*q3))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(boussinesq_number, acceleration_of_free_fall, characteristic_length): + return 1.4142135623731*acceleration_of_free_fall**0.5*boussinesq_number*characteristic_length**0.5 diff --git a/root/organs10/physics/specialists/solved-speed-18/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-speed-18/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-18/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-speed-19/organ_formula_v2.py b/root/organs10/physics/specialists/solved-speed-19/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-19/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-speed-19/panel.json b/root/organs10/physics/specialists/solved-speed-19/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..274b9987195f3e393c43c638bcb8f232cb5e2bf9 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-19/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the speed for Froude number 107.961, characteristic length 59.4105 and acceleration of free fall 5.77717?", + "a": "2000.112989052058", + "valid": true, + "kind": "compute", + "expect": 2000.112989052058, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105, 5.777165] (the system Froude number (Wikidata Q273090))" + }, + { + "q": "Tell me the speed with Froude number of 107.961, characteristic length of 59.4105 and acceleration of free fall of 5.77717.", + "a": "2000.112989052058", + "valid": true, + "kind": "compute", + "expect": 2000.112989052058, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105, 5.777165] (the system Froude number (Wikidata Q273090))" + }, + { + "q": "What is the speed for Froude number 85.2416, characteristic length 160.878 and acceleration of free fall 2.69428?", + "a": "1774.6878974590418", + "valid": true, + "kind": "compute", + "expect": 1774.6878974590418, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [85.241623, 160.878442, 2.69428] (the system Froude number (Wikidata Q273090))" + }, + { + "q": "Tell me the speed with Froude number of 85.2416, characteristic length of 160.878 and acceleration of free fall of 2.69428.", + "a": "1774.6878974590418", + "valid": true, + "kind": "compute", + "expect": 1774.6878974590418, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [85.241623, 160.878442, 2.69428] (the system Froude number (Wikidata Q273090))" + }, + { + "q": "What is the speed for Froude number 45.6157, characteristic length 4.97131 and acceleration of free fall 94.305?", + "a": "987.6813159711828", + "valid": true, + "kind": "compute", + "expect": 987.6813159711828, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [45.61567, 4.971307, 94.305046] (the system Froude number (Wikidata Q273090))" + }, + { + "q": "Tell me the speed with Froude number of 45.6157, characteristic length of 4.97131 and acceleration of free fall of 94.305.", + "a": "987.6813159711828", + "valid": true, + "kind": "compute", + "expect": 987.6813159711828, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [45.61567, 4.971307, 94.305046] (the system Froude number (Wikidata Q273090))" + }, + { + "q": "What is the speed for Froude number 10.5638, characteristic length 198.839 and acceleration of free fall 0.671697?", + "a": "122.08419188185322", + "valid": true, + "kind": "compute", + "expect": 122.08419188185322, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [10.563843, 198.838955, 0.671697] (the system Froude number (Wikidata Q273090))" + }, + { + "q": "Tell me the speed with Froude number of 10.5638, characteristic length of 198.839 and acceleration of free fall of 0.671697.", + "a": "122.08419188185322", + "valid": true, + "kind": "compute", + "expect": 122.08419188185322, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [10.563843, 198.838955, 0.671697] (the system Froude number (Wikidata Q273090))" + }, + { + "q": "What is the speed for Froude number 21.7931, characteristic length 29.5106 and acceleration of free fall 8.50341?", + "a": "345.2272836396581", + "valid": true, + "kind": "compute", + "expect": 345.2272836396581, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609, 8.503407] (the system Froude number (Wikidata Q273090))" + }, + { + "q": "Tell me the speed with Froude number of 21.7931, characteristic length of 29.5106 and acceleration of free fall of 8.50341.", + "a": "345.2272836396581", + "valid": true, + "kind": "compute", + "expect": 345.2272836396581, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609, 8.503407] (the system Froude number (Wikidata Q273090))" + }, + { + "q": "What is the speed for Froude number 223.509, characteristic length 17.3414 and acceleration of free fall 7.10705?", + "a": "2481.3143990572535", + "valid": true, + "kind": "compute", + "expect": 2481.3143990572535, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [223.508796, 17.341435, 7.107049] (the system Froude number (Wikidata Q273090))" + }, + { + "q": "Tell me the speed with Froude number of 223.509, characteristic length of 17.3414 and acceleration of free fall of 7.10705.", + "a": "2481.3143990572535", + "valid": true, + "kind": "compute", + "expect": 2481.3143990572535, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [223.508796, 17.341435, 7.107049] (the system Froude number (Wikidata Q273090))" + }, + { + "q": "What is the speed for Froude number 107.961 and characteristic length 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the acceleration of free fall is not given" + }, + { + "q": "Tell me the speed with Froude number of 85.2416 and characteristic length of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the acceleration of free fall is not given" + }, + { + "q": "What is the speed of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the speed of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the speed with Froude number of -45.6157, characteristic length of 4.97131 and acceleration of free fall of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Froude number: no example had one" + }, + { + "q": "Which speed is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the Froude number is 17.7888, characteristic length is 167.625 and the acceleration of free fall is 10.3291, what is the speed?", + "a": "740.1942888796797", + "valid": true, + "kind": "compute", + "expect": 740.1942888796797, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [17.788778, 167.624948, 10.329054] (the system Froude number (Wikidata Q273090))" + }, + { + "q": "Could you work out the speed given Froude number = 17.7888, characteristic length = 167.625, acceleration of free fall = 10.3291?", + "a": "740.1942888796797", + "valid": true, + "kind": "compute", + "expect": 740.1942888796797, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [17.788778, 167.624948, 10.329054] (the system Froude number (Wikidata Q273090))" + }, + { + "q": "I need the speed for a Froude number of 21.511, a characteristic length of 173.394 and an acceleration of free fall of 251.084, please.", + "a": "4488.353473947946", + "valid": true, + "kind": "compute", + "expect": 4488.353473947946, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.511016, 173.393638, 251.084037] (the system Froude number (Wikidata Q273090))" + }, + { + "q": "If the Froude number is 21.511, characteristic length is 173.394 and the acceleration of free fall is 251.084, what is the speed?", + "a": "4488.353473947946", + "valid": true, + "kind": "compute", + "expect": 4488.353473947946, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.511016, 173.393638, 251.084037] (the system Froude number (Wikidata Q273090))" + }, + { + "q": "Could you work out the speed given Froude number = 129.245, characteristic length = 1.18359, acceleration of free fall = 15.9416?", + "a": "561.4103882766713", + "valid": true, + "kind": "compute", + "expect": 561.4103882766713, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [129.244984, 1.18359, 15.941612] (the system Froude number (Wikidata Q273090))" + }, + { + "q": "I need the speed for a Froude number of 129.245, a characteristic length of 1.18359 and an acceleration of free fall of 15.9416, please.", + "a": "561.4103882766713", + "valid": true, + "kind": "compute", + "expect": 561.4103882766713, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [129.244984, 1.18359, 15.941612] (the system Froude number (Wikidata Q273090))" + }, + { + "q": "If the Froude number is 20.3966, characteristic length is 18.3169 and the acceleration of free fall is 6.8386, what is the speed?", + "a": "228.27903056645135", + "valid": true, + "kind": "compute", + "expect": 228.27903056645135, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [20.396558, 18.316867, 6.838598] (the system Froude number (Wikidata Q273090))" + }, + { + "q": "Could you work out the speed given Froude number = 20.3966, characteristic length = 18.3169, acceleration of free fall = 6.8386?", + "a": "228.27903056645135", + "valid": true, + "kind": "compute", + "expect": 228.27903056645135, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [20.396558, 18.316867, 6.838598] (the system Froude number (Wikidata Q273090))" + }, + { + "q": "I need the speed for a Froude number of 185.886, a characteristic length of 13.2373 and an acceleration of free fall of 4.71941, please.", + "a": "1469.2350565280553", + "valid": true, + "kind": "compute", + "expect": 1469.2350565280553, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [185.886273, 13.237311, 4.719415] (the system Froude number (Wikidata Q273090))" + }, + { + "q": "If the Froude number is 185.886, characteristic length is 13.2373 and the acceleration of free fall is 4.71941, what is the speed?", + "a": "1469.2350565280553", + "valid": true, + "kind": "compute", + "expect": 1469.2350565280553, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [185.886273, 13.237311, 4.719415] (the system Froude number (Wikidata Q273090))" + }, + { + "q": "Could you work out the speed given Froude number = 9.1061, characteristic length = 24.5721, acceleration of free fall = 108.451?", + "a": "470.07805809522335", + "valid": true, + "kind": "compute", + "expect": 470.07805809522335, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [9.106104, 24.572086, 108.450792] (the system Froude number (Wikidata Q273090))" + }, + { + "q": "I need the speed for a Froude number of 9.1061, a characteristic length of 24.5721 and an acceleration of free fall of 108.451, please.", + "a": "470.07805809522335", + "valid": true, + "kind": "compute", + "expect": 470.07805809522335, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [9.106104, 24.572086, 108.450792] (the system Froude number (Wikidata Q273090))" + }, + { + "q": "If the Froude number is 17.7888 and the characteristic length is 167.625, what is the speed?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the acceleration of free fall is not given" + }, + { + "q": "Could you work out the speed given Froude number = 21.511, characteristic length = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the acceleration of free fall is not given" + }, + { + "q": "Do you know the speed of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the speed of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the speed given Froude number = -129.245, characteristic length = 1.18359, acceleration of free fall = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Froude number: no example had one" + }, + { + "q": "What is a typical speed?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-speed-19/program.py b/root/organs10/physics/specialists/solved-speed-19/program.py new file mode 100644 index 0000000000000000000000000000000000000000..40d54fc9bff37e27cd725584efc4f892c3efb8d1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-19/program.py @@ -0,0 +1,7 @@ +"""speed from Froude number, characteristic length, acceleration of free fall (Froude number (Wikidata Q273090)) +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(froude_number, characteristic_length, acceleration_of_free_fall): + return 1.0*acceleration_of_free_fall**0.5*characteristic_length**0.5*froude_number diff --git a/root/organs10/physics/specialists/solved-speed-19/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-speed-19/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-19/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-speed-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-speed-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-2/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-speed-2/panel.json b/root/organs10/physics/specialists/solved-speed-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..9068531809cb3c4fa72ee12da3a4e6a2b43dd02a --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the speed for kinetic energy 107.961 and mass 59.4105?", + "a": "1.9064093998872984", + "valid": true, + "kind": "compute", + "expect": 1.9064093998872984, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105] (the system Kinetic energy)" + }, + { + "q": "Tell me the speed with kinetic energy of 107.961 and mass of 59.4105.", + "a": "1.9064093998872984", + "valid": true, + "kind": "compute", + "expect": 1.9064093998872984, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105] (the system Kinetic energy)" + }, + { + "q": "What is the speed for kinetic energy 169.527 and mass 280.774?", + "a": "1.0988949250779128", + "valid": true, + "kind": "compute", + "expect": 1.0988949250779128, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [169.526958, 280.773702] (the system Kinetic energy)" + }, + { + "q": "Tell me the speed with kinetic energy of 169.527 and mass of 280.774.", + "a": "1.0988949250779128", + "valid": true, + "kind": "compute", + "expect": 1.0988949250779128, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [169.526958, 280.773702] (the system Kinetic energy)" + }, + { + "q": "What is the speed for kinetic energy 1.91195 and mass 82.2401?", + "a": "0.21563126113155487", + "valid": true, + "kind": "compute", + "expect": 0.21563126113155487, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.911952, 82.240072] (the system Kinetic energy)" + }, + { + "q": "Tell me the speed with kinetic energy of 1.91195 and mass of 82.2401.", + "a": "0.21563126113155487", + "valid": true, + "kind": "compute", + "expect": 0.21563126113155487, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.911952, 82.240072] (the system Kinetic energy)" + }, + { + "q": "What is the speed for kinetic energy 1.64379 and mass 252.709?", + "a": "0.11405872573102283", + "valid": true, + "kind": "compute", + "expect": 0.11405872573102283, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.643795, 252.709024] (the system Kinetic energy)" + }, + { + "q": "Tell me the speed with kinetic energy of 1.64379 and mass of 252.709.", + "a": "0.11405872573102283", + "valid": true, + "kind": "compute", + "expect": 0.11405872573102283, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.643795, 252.709024] (the system Kinetic energy)" + }, + { + "q": "What is the speed for kinetic energy 18.3169 and mass 6.8386?", + "a": "2.3144993048580904", + "valid": true, + "kind": "compute", + "expect": 2.3144993048580904, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [18.316867, 6.838598] (the system Kinetic energy)" + }, + { + "q": "Tell me the speed with kinetic energy of 18.3169 and mass of 6.8386.", + "a": "2.3144993048580904", + "valid": true, + "kind": "compute", + "expect": 2.3144993048580904, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [18.316867, 6.838598] (the system Kinetic energy)" + }, + { + "q": "What is the speed for kinetic energy 156.502 and mass 185.886?", + "a": "1.2976304262856653", + "valid": true, + "kind": "compute", + "expect": 1.2976304262856653, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [156.50181, 185.886273] (the system Kinetic energy)" + }, + { + "q": "Tell me the speed with kinetic energy of 156.502 and mass of 185.886.", + "a": "1.2976304262856653", + "valid": true, + "kind": "compute", + "expect": 1.2976304262856653, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [156.50181, 185.886273] (the system Kinetic energy)" + }, + { + "q": "What is the speed for kinetic energy 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Tell me the speed with kinetic energy of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "What is the speed of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the speed of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the speed with kinetic energy of -1.91195 and mass of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative kinetic energy: no example had one" + }, + { + "q": "Which speed is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the kinetic energy is 8.50728 and the mass is 17.7888, what is the speed?", + "a": "0.9779966560958291", + "valid": true, + "kind": "compute", + "expect": 0.9779966560958291, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.507283, 17.788778] (the system Kinetic energy)" + }, + { + "q": "Could you work out the speed given kinetic energy = 8.50728, mass = 17.7888?", + "a": "0.9779966560958291", + "valid": true, + "kind": "compute", + "expect": 0.9779966560958291, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.507283, 17.788778] (the system Kinetic energy)" + }, + { + "q": "I need the speed for a kinetic energy of 8.24951 and a mass of 0.634021, please.", + "a": "5.101257648186148", + "valid": true, + "kind": "compute", + "expect": 5.101257648186148, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.24951, 0.634021] (the system Kinetic energy)" + }, + { + "q": "If the kinetic energy is 8.24951 and the mass is 0.634021, what is the speed?", + "a": "5.101257648186148", + "valid": true, + "kind": "compute", + "expect": 5.101257648186148, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.24951, 0.634021] (the system Kinetic energy)" + }, + { + "q": "Could you work out the speed given kinetic energy = 0.318734, mass = 9.56607?", + "a": "0.25814423291087363", + "valid": true, + "kind": "compute", + "expect": 0.25814423291087363, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.318734, 9.566071] (the system Kinetic energy)" + }, + { + "q": "I need the speed for a kinetic energy of 0.318734 and a mass of 9.56607, please.", + "a": "0.25814423291087363", + "valid": true, + "kind": "compute", + "expect": 0.25814423291087363, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.318734, 9.566071] (the system Kinetic energy)" + }, + { + "q": "If the kinetic energy is 0.671697 and the mass is 14.2496, what is the speed?", + "a": "0.30704419966634233", + "valid": true, + "kind": "compute", + "expect": 0.30704419966634233, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.671697, 14.249569] (the system Kinetic energy)" + }, + { + "q": "Could you work out the speed given kinetic energy = 0.671697, mass = 14.2496?", + "a": "0.30704419966634233", + "valid": true, + "kind": "compute", + "expect": 0.30704419966634233, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.671697, 14.249569] (the system Kinetic energy)" + }, + { + "q": "I need the speed for a kinetic energy of 21.7931 and a mass of 29.5106, please.", + "a": "1.2153061178601818", + "valid": true, + "kind": "compute", + "expect": 1.2153061178601818, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609] (the system Kinetic energy)" + }, + { + "q": "If the kinetic energy is 21.7931 and the mass is 29.5106, what is the speed?", + "a": "1.2153061178601818", + "valid": true, + "kind": "compute", + "expect": 1.2153061178601818, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609] (the system Kinetic energy)" + }, + { + "q": "Could you work out the speed given kinetic energy = 153.15, mass = 18.5953?", + "a": "4.058557223570786", + "valid": true, + "kind": "compute", + "expect": 4.058557223570786, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [153.149965, 18.595315] (the system Kinetic energy)" + }, + { + "q": "I need the speed for a kinetic energy of 153.15 and a mass of 18.5953, please.", + "a": "4.058557223570786", + "valid": true, + "kind": "compute", + "expect": 4.058557223570786, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [153.149965, 18.595315] (the system Kinetic energy)" + }, + { + "q": "If the kinetic energy is 8.50728, what is the speed?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Could you work out the speed given kinetic energy = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Do you know the speed of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the speed of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the speed given kinetic energy = -0.318734, mass = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative kinetic energy: no example had one" + }, + { + "q": "What is a typical speed?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-speed-2/program.py b/root/organs10/physics/specialists/solved-speed-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..5c8f05c2c8405856e1e36bb00888343d7fc15e40 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-2/program.py @@ -0,0 +1,7 @@ +"""speed from kinetic energy, mass (Kinetic energy) +The maths organ (systems.py): solved from the system E = m*v**2/2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(kinetic_energy, mass): + return 1.4142135623731*kinetic_energy**0.5*mass**(-0.5) diff --git a/root/organs10/physics/specialists/solved-speed-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-speed-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-2/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-speed-20/organ_formula_v2.py b/root/organs10/physics/specialists/solved-speed-20/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-20/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-speed-20/panel.json b/root/organs10/physics/specialists/solved-speed-20/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..32058b1e9a937f5e6d011c118c7bb0dba46324c9 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-20/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the speed for Alfvén number 107.961, magnetic flux density 59.4105, mass density 5.77717 and magnetic permeability 1.89126?", + "a": "1940.4223431883686", + "valid": true, + "kind": "compute", + "expect": 1940.4223431883686, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105, 5.777165, 1.891256] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Tell me the speed with Alfvén number of 107.961, magnetic flux density of 59.4105, mass density of 5.77717 and magnetic permeability of 1.89126.", + "a": "1940.4223431883686", + "valid": true, + "kind": "compute", + "expect": 1940.4223431883686, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105, 5.777165, 1.891256] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "What is the speed for Alfvén number 2.69428, magnetic flux density 48.921, mass density 157.223 and magnetic permeability 35.6412?", + "a": "1.760778079531587", + "valid": true, + "kind": "compute", + "expect": 1.760778079531587, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [2.69428, 48.920991, 157.22256, 35.641157] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Tell me the speed with Alfvén number of 2.69428, magnetic flux density of 48.921, mass density of 157.223 and magnetic permeability of 35.6412.", + "a": "1.760778079531587", + "valid": true, + "kind": "compute", + "expect": 1.760778079531587, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [2.69428, 48.920991, 157.22256, 35.641157] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "What is the speed for Alfvén number 0.318734, magnetic flux density 9.56607, mass density 126.709 and magnetic permeability 1.70503?", + "a": "0.20743955817970405", + "valid": true, + "kind": "compute", + "expect": 0.20743955817970405, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.318734, 9.566071, 126.709252, 1.705032] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Tell me the speed with Alfvén number of 0.318734, magnetic flux density of 9.56607, mass density of 126.709 and magnetic permeability of 1.70503.", + "a": "0.20743955817970405", + "valid": true, + "kind": "compute", + "expect": 0.20743955817970405, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.318734, 9.566071, 126.709252, 1.705032] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "What is the speed for Alfvén number 87.781, magnetic flux density 13.2084, mass density 246.329 and magnetic permeability 20.3966?", + "a": "16.357432991606437", + "valid": true, + "kind": "compute", + "expect": 16.357432991606437, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [87.781013, 13.208419, 246.329053, 20.396558] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Tell me the speed with Alfvén number of 87.781, magnetic flux density of 13.2084, mass density of 246.329 and magnetic permeability of 20.3966.", + "a": "16.357432991606437", + "valid": true, + "kind": "compute", + "expect": 16.357432991606437, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [87.781013, 13.208419, 246.329053, 20.396558] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "What is the speed for Alfvén number 156.502, magnetic flux density 185.886, mass density 13.2373 and magnetic permeability 4.71941?", + "a": "3680.634738550051", + "valid": true, + "kind": "compute", + "expect": 3680.634738550051, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [156.50181, 185.886273, 13.237311, 4.719415] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Tell me the speed with Alfvén number of 156.502, magnetic flux density of 185.886, mass density of 13.2373 and magnetic permeability of 4.71941.", + "a": "3680.634738550051", + "valid": true, + "kind": "compute", + "expect": 3680.634738550051, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [156.50181, 185.886273, 13.237311, 4.719415] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "What is the speed for Alfvén number 24.5721, magnetic flux density 108.451, mass density 1.69476 and magnetic permeability 49.4843?", + "a": "290.9963113749551", + "valid": true, + "kind": "compute", + "expect": 290.9963113749551, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [24.572086, 108.450792, 1.694755, 49.484326] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Tell me the speed with Alfvén number of 24.5721, magnetic flux density of 108.451, mass density of 1.69476 and magnetic permeability of 49.4843.", + "a": "290.9963113749551", + "valid": true, + "kind": "compute", + "expect": 290.9963113749551, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [24.572086, 108.450792, 1.694755, 49.484326] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "What is the speed for Alfvén number 107.961, magnetic flux density 59.4105 and mass density 5.77717?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "Tell me the speed with Alfvén number of 2.69428, magnetic flux density of 48.921 and mass density of 157.223.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "What is the speed of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the speed of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the speed with Alfvén number of -0.318734, magnetic flux density of 9.56607, mass density of 126.709 and magnetic permeability of 1.70503.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Alfvén number: no example had one" + }, + { + "q": "Which speed is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the Alfvén number is 167.625, magnetic flux density is 10.3291, mass density is 2.21571 and the magnetic permeability is 58.5346, what is the speed?", + "a": "152.0323995888514", + "valid": true, + "kind": "compute", + "expect": 152.0323995888514, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [167.624948, 10.329054, 2.215713, 58.534594] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Could you work out the speed given Alfvén number = 167.625, magnetic flux density = 10.3291, mass density = 2.21571, magnetic permeability = 58.5346?", + "a": "152.0323995888514", + "valid": true, + "kind": "compute", + "expect": 152.0323995888514, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [167.624948, 10.329054, 2.215713, 58.534594] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "I need the speed for an Alfvén number of 8.53158, a magnetic flux density of 124.718, a mass density of 1.91195 and a magnetic permeability of 82.2401, please.", + "a": "84.85528703589193", + "valid": true, + "kind": "compute", + "expect": 84.85528703589193, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.531577, 124.718185, 1.911952, 82.240072] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "If the Alfvén number is 8.53158, magnetic flux density is 124.718, mass density is 1.91195 and the magnetic permeability is 82.2401, what is the speed?", + "a": "84.85528703589193", + "valid": true, + "kind": "compute", + "expect": 84.85528703589193, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.531577, 124.718185, 1.911952, 82.240072] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Could you work out the speed given Alfvén number = 81.1967, magnetic flux density = 6.98086, mass density = 0.551236, magnetic permeability = 2.88512?", + "a": "449.4656298484413", + "valid": true, + "kind": "compute", + "expect": 449.4656298484413, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [81.196705, 6.980857, 0.551236, 2.88512] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "I need the speed for an Alfvén number of 81.1967, a magnetic flux density of 6.98086, a mass density of 0.551236 and a magnetic permeability of 2.88512, please.", + "a": "449.4656298484413", + "valid": true, + "kind": "compute", + "expect": 449.4656298484413, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [81.196705, 6.980857, 0.551236, 2.88512] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "If the Alfvén number is 1.16997, magnetic flux density is 1.14865, mass density is 21.7931 and the magnetic permeability is 29.5106, what is the speed?", + "a": "0.05299241564253276", + "valid": true, + "kind": "compute", + "expect": 0.05299241564253276, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.169968, 1.148652, 21.793126, 29.510609] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Could you work out the speed given Alfvén number = 1.16997, magnetic flux density = 1.14865, mass density = 21.7931, magnetic permeability = 29.5106?", + "a": "0.05299241564253276", + "valid": true, + "kind": "compute", + "expect": 0.05299241564253276, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.169968, 1.148652, 21.793126, 29.510609] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "I need the speed for an Alfvén number of 223.509, a magnetic flux density of 17.3414, a mass density of 7.10705 and a magnetic permeability of 30.2507, please.", + "a": "264.34245803975296", + "valid": true, + "kind": "compute", + "expect": 264.34245803975296, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [223.508796, 17.341435, 7.107049, 30.250749] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "If the Alfvén number is 223.509, magnetic flux density is 17.3414, mass density is 7.10705 and the magnetic permeability is 30.2507, what is the speed?", + "a": "264.34245803975296", + "valid": true, + "kind": "compute", + "expect": 264.34245803975296, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [223.508796, 17.341435, 7.107049, 30.250749] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "Could you work out the speed given Alfvén number = 0.869111, magnetic flux density = 39.1753, mass density = 0.432217, magnetic permeability = 16.6571?", + "a": "12.689287297365192", + "valid": true, + "kind": "compute", + "expect": 12.689287297365192, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.869111, 39.175263, 0.432217, 16.657051] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "I need the speed for an Alfvén number of 0.869111, a magnetic flux density of 39.1753, a mass density of 0.432217 and a magnetic permeability of 16.6571, please.", + "a": "12.689287297365192", + "valid": true, + "kind": "compute", + "expect": 12.689287297365192, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.869111, 39.175263, 0.432217, 16.657051] (the system Alfvén number (Wikidata Q3342997))" + }, + { + "q": "If the Alfvén number is 167.625, magnetic flux density is 10.3291 and the mass density is 2.21571, what is the speed?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "Could you work out the speed given Alfvén number = 8.53158, magnetic flux density = 124.718, mass density = 1.91195?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "Do you know the speed of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the speed of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the speed given Alfvén number = -81.1967, magnetic flux density = 6.98086, mass density = 0.551236, magnetic permeability = 2.88512?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Alfvén number: no example had one" + }, + { + "q": "What is a typical speed?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-speed-20/program.py b/root/organs10/physics/specialists/solved-speed-20/program.py new file mode 100644 index 0000000000000000000000000000000000000000..d4bd62df1c0c1cd6e717534b2cf9f35d87a9c007 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-20/program.py @@ -0,0 +1,7 @@ +"""speed from Alfvén number, magnetic flux density, mass density, magnetic permeability (Alfvén number (Wikidata Q3342997)) +The maths organ (systems.py): solved from the system q0 = ((q1)/(q2 / sqrt(q3*q4))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(alfv_n_number, magnetic_flux_density, mass_density, magnetic_permeability): + return 1.0*alfv_n_number*magnetic_flux_density*magnetic_permeability**(-0.5)*mass_density**(-0.5) diff --git a/root/organs10/physics/specialists/solved-speed-20/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-speed-20/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-20/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-speed-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-speed-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-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-speed-3/panel.json b/root/organs10/physics/specialists/solved-speed-3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..fb73cb66000a93fb7a6019fbda5939c9e66eaf59 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the speed for momentum 107.961 and mass 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105] (the system Momentum)" + }, + { + "q": "Tell me the speed with momentum of 107.961 and mass of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105] (the system Momentum)" + }, + { + "q": "What is the speed for momentum 169.527 and mass 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [169.526958, 280.773702] (the system Momentum)" + }, + { + "q": "Tell me the speed with momentum of 169.527 and mass of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [169.526958, 280.773702] (the system Momentum)" + }, + { + "q": "What is the speed for momentum 1.91195 and mass 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.911952, 82.240072] (the system Momentum)" + }, + { + "q": "Tell me the speed with momentum of 1.91195 and mass of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.911952, 82.240072] (the system Momentum)" + }, + { + "q": "What is the speed for momentum 1.64379 and mass 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.643795, 252.709024] (the system Momentum)" + }, + { + "q": "Tell me the speed with momentum of 1.64379 and mass of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.643795, 252.709024] (the system Momentum)" + }, + { + "q": "What is the speed for momentum 18.3169 and mass 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [18.316867, 6.838598] (the system Momentum)" + }, + { + "q": "Tell me the speed with momentum of 18.3169 and mass of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [18.316867, 6.838598] (the system Momentum)" + }, + { + "q": "What is the speed for momentum 156.502 and mass 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [156.50181, 185.886273] (the system Momentum)" + }, + { + "q": "Tell me the speed with momentum of 156.502 and mass of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [156.50181, 185.886273] (the system Momentum)" + }, + { + "q": "What is the speed for momentum 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Tell me the speed with momentum of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "What is the speed of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the speed of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the speed with momentum of -1.91195 and mass of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative momentum: no example had one" + }, + { + "q": "Which speed is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the momentum is 8.50728 and the mass is 17.7888, what is the speed?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.507283, 17.788778] (the system Momentum)" + }, + { + "q": "Could you work out the speed given momentum = 8.50728, mass = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.507283, 17.788778] (the system Momentum)" + }, + { + "q": "I need the speed for a momentum of 8.24951 and a mass of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.24951, 0.634021] (the system Momentum)" + }, + { + "q": "If the momentum is 8.24951 and the mass is 0.634021, what is the speed?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.24951, 0.634021] (the system Momentum)" + }, + { + "q": "Could you work out the speed given momentum = 0.318734, mass = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.318734, 9.566071] (the system Momentum)" + }, + { + "q": "I need the speed for a momentum of 0.318734 and a mass of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.318734, 9.566071] (the system Momentum)" + }, + { + "q": "If the momentum is 0.671697 and the mass is 14.2496, what is the speed?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.671697, 14.249569] (the system Momentum)" + }, + { + "q": "Could you work out the speed given momentum = 0.671697, mass = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.671697, 14.249569] (the system Momentum)" + }, + { + "q": "I need the speed for a momentum of 21.7931 and a mass of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609] (the system Momentum)" + }, + { + "q": "If the momentum is 21.7931 and the mass is 29.5106, what is the speed?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609] (the system Momentum)" + }, + { + "q": "Could you work out the speed given momentum = 153.15, mass = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [153.149965, 18.595315] (the system Momentum)" + }, + { + "q": "I need the speed for a momentum of 153.15 and a mass of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [153.149965, 18.595315] (the system Momentum)" + }, + { + "q": "If the momentum is 8.50728, what is the speed?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Could you work out the speed given momentum = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass is not given" + }, + { + "q": "Do you know the speed of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the speed of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the speed given momentum = -0.318734, mass = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative momentum: no example had one" + }, + { + "q": "What is a typical speed?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-speed-3/program.py b/root/organs10/physics/specialists/solved-speed-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..c42710827c8c2a2d3f400db4e1537e35f6c0333b --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-3/program.py @@ -0,0 +1,7 @@ +"""speed from momentum, mass (Momentum) +The maths organ (systems.py): solved from the system p = m*v; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(momentum, mass): + return 1.0*momentum/mass diff --git a/root/organs10/physics/specialists/solved-speed-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-speed-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-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-speed-4/organ_formula_v2.py b/root/organs10/physics/specialists/solved-speed-4/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-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-speed-4/panel.json b/root/organs10/physics/specialists/solved-speed-4/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..9d2bd7f36591638296d7b452c0e78820eb28b6ae --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-4/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the speed for flow 107.961 and area 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105] (the system Flow from speed)" + }, + { + "q": "Tell me the speed with flow of 107.961 and area of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105] (the system Flow from speed)" + }, + { + "q": "What is the speed for flow 169.527 and area 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [169.526958, 280.773702] (the system Flow from speed)" + }, + { + "q": "Tell me the speed with flow of 169.527 and area of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [169.526958, 280.773702] (the system Flow from speed)" + }, + { + "q": "What is the speed for flow 1.91195 and area 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.911952, 82.240072] (the system Flow from speed)" + }, + { + "q": "Tell me the speed with flow of 1.91195 and area of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.911952, 82.240072] (the system Flow from speed)" + }, + { + "q": "What is the speed for flow 1.64379 and area 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.643795, 252.709024] (the system Flow from speed)" + }, + { + "q": "Tell me the speed with flow of 1.64379 and area of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.643795, 252.709024] (the system Flow from speed)" + }, + { + "q": "What is the speed for flow 18.3169 and area 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [18.316867, 6.838598] (the system Flow from speed)" + }, + { + "q": "Tell me the speed with flow of 18.3169 and area of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [18.316867, 6.838598] (the system Flow from speed)" + }, + { + "q": "What is the speed for flow 156.502 and area 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [156.50181, 185.886273] (the system Flow from speed)" + }, + { + "q": "Tell me the speed with flow of 156.502 and area of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [156.50181, 185.886273] (the system Flow from speed)" + }, + { + "q": "What is the speed for flow 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the area is not given" + }, + { + "q": "Tell me the speed with flow of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the area is not given" + }, + { + "q": "What is the speed of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the speed of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the speed with flow of -1.91195 and area of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative flow: no example had one" + }, + { + "q": "Which speed is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the flow is 8.50728 and the area is 17.7888, what is the speed?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.507283, 17.788778] (the system Flow from speed)" + }, + { + "q": "Could you work out the speed given flow = 8.50728, area = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.507283, 17.788778] (the system Flow from speed)" + }, + { + "q": "I need the speed for a flow of 8.24951 and an area of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.24951, 0.634021] (the system Flow from speed)" + }, + { + "q": "If the flow is 8.24951 and the area is 0.634021, what is the speed?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.24951, 0.634021] (the system Flow from speed)" + }, + { + "q": "Could you work out the speed given flow = 0.318734, area = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.318734, 9.566071] (the system Flow from speed)" + }, + { + "q": "I need the speed for a flow of 0.318734 and an area of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.318734, 9.566071] (the system Flow from speed)" + }, + { + "q": "If the flow is 0.671697 and the area is 14.2496, what is the speed?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.671697, 14.249569] (the system Flow from speed)" + }, + { + "q": "Could you work out the speed given flow = 0.671697, area = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.671697, 14.249569] (the system Flow from speed)" + }, + { + "q": "I need the speed for a flow of 21.7931 and an area of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609] (the system Flow from speed)" + }, + { + "q": "If the flow is 21.7931 and the area is 29.5106, what is the speed?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609] (the system Flow from speed)" + }, + { + "q": "Could you work out the speed given flow = 153.15, area = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [153.149965, 18.595315] (the system Flow from speed)" + }, + { + "q": "I need the speed for a flow of 153.15 and an area of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [153.149965, 18.595315] (the system Flow from speed)" + }, + { + "q": "If the flow is 8.50728, what is the speed?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the area is not given" + }, + { + "q": "Could you work out the speed given flow = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the area is not given" + }, + { + "q": "Do you know the speed of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the speed of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the speed given flow = -0.318734, area = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative flow: no example had one" + }, + { + "q": "What is a typical speed?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-speed-4/program.py b/root/organs10/physics/specialists/solved-speed-4/program.py new file mode 100644 index 0000000000000000000000000000000000000000..fbe1e09e970976dd27f9f8b0539bdb1c81a1c5c9 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-4/program.py @@ -0,0 +1,7 @@ +"""speed from flow, area (Flow from speed) +The maths organ (systems.py): solved from the system Q = A*v; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(flow, area): + return 1.0*flow/area diff --git a/root/organs10/physics/specialists/solved-speed-4/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-speed-4/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-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-speed-5/organ_formula_v2.py b/root/organs10/physics/specialists/solved-speed-5/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-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-speed-5/panel.json b/root/organs10/physics/specialists/solved-speed-5/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..6befb0a7319253759cbe6f724db7a98ee2d02f00 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-5/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the speed in km/h for speed 107.961?", + "a": "388.6583972038632", + "valid": true, + "kind": "compute", + "expect": 388.6583972038632, + "dtype": "quantity", + "unit": "km/h", + "why": "speed in km/h for [107.960666] (the system Speeds)" + }, + { + "q": "Tell me the speed in km/h with speed of 107.961.", + "a": "388.6583972038632", + "valid": true, + "kind": "compute", + "expect": 388.6583972038632, + "dtype": "quantity", + "unit": "km/h", + "why": "speed in km/h for [107.960666] (the system Speeds)" + }, + { + "q": "What is the speed in km/h for speed 22.652?", + "a": "81.54728206868084", + "valid": true, + "kind": "compute", + "expect": 81.54728206868084, + "dtype": "quantity", + "unit": "km/h", + "why": "speed in km/h for [22.652023] (the system Speeds)" + }, + { + "q": "Tell me the speed in km/h with speed of 22.652.", + "a": "81.54728206868084", + "valid": true, + "kind": "compute", + "expect": 81.54728206868084, + "dtype": "quantity", + "unit": "km/h", + "why": "speed in km/h for [22.652023] (the system Speeds)" + }, + { + "q": "What is the speed in km/h for speed 173.394?", + "a": "624.217098259089", + "valid": true, + "kind": "compute", + "expect": 624.217098259089, + "dtype": "quantity", + "unit": "km/h", + "why": "speed in km/h for [173.393638] (the system Speeds)" + }, + { + "q": "Tell me the speed in km/h with speed of 173.394.", + "a": "624.217098259089", + "valid": true, + "kind": "compute", + "expect": 624.217098259089, + "dtype": "quantity", + "unit": "km/h", + "why": "speed in km/h for [173.393638] (the system Speeds)" + }, + { + "q": "What is the speed in km/h for speed 126.709?", + "a": "456.1533064806565", + "valid": true, + "kind": "compute", + "expect": 456.1533064806565, + "dtype": "quantity", + "unit": "km/h", + "why": "speed in km/h for [126.709252] (the system Speeds)" + }, + { + "q": "Tell me the speed in km/h with speed of 126.709.", + "a": "456.1533064806565", + "valid": true, + "kind": "compute", + "expect": 456.1533064806565, + "dtype": "quantity", + "unit": "km/h", + "why": "speed in km/h for [126.709252] (the system Speeds)" + }, + { + "q": "What is the speed in km/h for speed 0.671697?", + "a": "2.4181099192590967", + "valid": true, + "kind": "compute", + "expect": 2.4181099192590967, + "dtype": "quantity", + "unit": "km/h", + "why": "speed in km/h for [0.671697] (the system Speeds)" + }, + { + "q": "Tell me the speed in km/h with speed of 0.671697.", + "a": "2.4181099192590967", + "valid": true, + "kind": "compute", + "expect": 2.4181099192590967, + "dtype": "quantity", + "unit": "km/h", + "why": "speed in km/h for [0.671697] (the system Speeds)" + }, + { + "q": "What is the speed in km/h for speed 1.16997?", + "a": "4.211884821520568", + "valid": true, + "kind": "compute", + "expect": 4.211884821520568, + "dtype": "quantity", + "unit": "km/h", + "why": "speed in km/h for [1.169968] (the system Speeds)" + }, + { + "q": "Tell me the speed in km/h with speed of 1.16997.", + "a": "4.211884821520568", + "valid": true, + "kind": "compute", + "expect": 4.211884821520568, + "dtype": "quantity", + "unit": "km/h", + "why": "speed in km/h for [1.169968] (the system Speeds)" + }, + { + "q": "What is the speed in km/h?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Could you work out the speed in km/h, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "What is the speed in km/h 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 speed in km/h 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 speed in km/h with speed of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative speed: no example had one" + }, + { + "q": "Which speed in km/h is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the speed is 2.57003, what is the speed in km/h?", + "a": "9.252107687893117", + "valid": true, + "kind": "compute", + "expect": 9.252107687893117, + "dtype": "quantity", + "unit": "km/h", + "why": "speed in km/h for [2.57003] (the system Speeds)" + }, + { + "q": "Could you work out the speed in km/h given speed = 2.57003?", + "a": "9.252107687893117", + "valid": true, + "kind": "compute", + "expect": 9.252107687893117, + "dtype": "quantity", + "unit": "km/h", + "why": "speed in km/h for [2.57003] (the system Speeds)" + }, + { + "q": "I need the speed in km/h for a speed of 48.921, please.", + "a": "176.11556709604258", + "valid": true, + "kind": "compute", + "expect": 176.11556709604258, + "dtype": "quantity", + "unit": "km/h", + "why": "speed in km/h for [48.920991] (the system Speeds)" + }, + { + "q": "If the speed is 48.921, what is the speed in km/h?", + "a": "176.11556709604258", + "valid": true, + "kind": "compute", + "expect": 176.11556709604258, + "dtype": "quantity", + "unit": "km/h", + "why": "speed in km/h for [48.920991] (the system Speeds)" + }, + { + "q": "Could you work out the speed in km/h given speed = 0.348438?", + "a": "1.2543756106681097", + "valid": true, + "kind": "compute", + "expect": 1.2543756106681097, + "dtype": "quantity", + "unit": "km/h", + "why": "speed in km/h for [0.348438] (the system Speeds)" + }, + { + "q": "I need the speed in km/h for a speed of 0.348438, please.", + "a": "1.2543756106681097", + "valid": true, + "kind": "compute", + "expect": 1.2543756106681097, + "dtype": "quantity", + "unit": "km/h", + "why": "speed in km/h for [0.348438] (the system Speeds)" + }, + { + "q": "If the speed is 252.709, what is the speed in km/h?", + "a": "909.752487298299", + "valid": true, + "kind": "compute", + "expect": 909.752487298299, + "dtype": "quantity", + "unit": "km/h", + "why": "speed in km/h for [252.709024] (the system Speeds)" + }, + { + "q": "Could you work out the speed in km/h given speed = 252.709?", + "a": "909.752487298299", + "valid": true, + "kind": "compute", + "expect": 909.752487298299, + "dtype": "quantity", + "unit": "km/h", + "why": "speed in km/h for [252.709024] (the system Speeds)" + }, + { + "q": "I need the speed in km/h for a speed of 20.3966, please.", + "a": "73.42760866306837", + "valid": true, + "kind": "compute", + "expect": 73.42760866306837, + "dtype": "quantity", + "unit": "km/h", + "why": "speed in km/h for [20.396558] (the system Speeds)" + }, + { + "q": "If the speed is 20.3966, what is the speed in km/h?", + "a": "73.42760866306837", + "valid": true, + "kind": "compute", + "expect": 73.42760866306837, + "dtype": "quantity", + "unit": "km/h", + "why": "speed in km/h for [20.396558] (the system Speeds)" + }, + { + "q": "Could you work out the speed in km/h given speed = 134.993?", + "a": "485.9746472906568", + "valid": true, + "kind": "compute", + "expect": 485.9746472906568, + "dtype": "quantity", + "unit": "km/h", + "why": "speed in km/h for [134.992958] (the system Speeds)" + }, + { + "q": "I need the speed in km/h for a speed of 134.993, please.", + "a": "485.9746472906568", + "valid": true, + "kind": "compute", + "expect": 485.9746472906568, + "dtype": "quantity", + "unit": "km/h", + "why": "speed in km/h for [134.992958] (the system Speeds)" + }, + { + "q": "I need the speed in km/h, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Can you tell me the speed in km/h?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Do you know the speed in km/h 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 speed in km/h 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 speed in km/h given speed = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative speed: no example had one" + }, + { + "q": "What is a typical speed in km/h?", + "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-speed-5/program.py b/root/organs10/physics/specialists/solved-speed-5/program.py new file mode 100644 index 0000000000000000000000000000000000000000..4581e61903a61ec3811f8a51e90d9af9cce0e2e2 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-5/program.py @@ -0,0 +1,7 @@ +"""speed from speed (Speeds) +The maths organ (systems.py): solved from the system K = M; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(speed): + return 3.6*speed diff --git a/root/organs10/physics/specialists/solved-speed-5/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-speed-5/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-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-speed-6/organ_formula_v2.py b/root/organs10/physics/specialists/solved-speed-6/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-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-speed-6/panel.json b/root/organs10/physics/specialists/solved-speed-6/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..250e00c7f84464d55d1ca2aeb1b01f0295c4546b --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-6/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the speed in m/s for speed 107.961?", + "a": "29.989073858322783", + "valid": true, + "kind": "compute", + "expect": 29.989073858322783, + "dtype": "quantity", + "unit": "m/s", + "why": "speed in m/s for [107.960666] (the system Speeds)" + }, + { + "q": "Tell me the speed in m/s with speed of 107.961.", + "a": "29.989073858322783", + "valid": true, + "kind": "compute", + "expect": 29.989073858322783, + "dtype": "quantity", + "unit": "m/s", + "why": "speed in m/s for [107.960666] (the system Speeds)" + }, + { + "q": "What is the speed in m/s for speed 22.652?", + "a": "6.292228554682165", + "valid": true, + "kind": "compute", + "expect": 6.292228554682165, + "dtype": "quantity", + "unit": "m/s", + "why": "speed in m/s for [22.652023] (the system Speeds)" + }, + { + "q": "Tell me the speed in m/s with speed of 22.652.", + "a": "6.292228554682165", + "valid": true, + "kind": "compute", + "expect": 6.292228554682165, + "dtype": "quantity", + "unit": "m/s", + "why": "speed in m/s for [22.652023] (the system Speeds)" + }, + { + "q": "What is the speed in m/s for speed 173.394?", + "a": "48.16489955702848", + "valid": true, + "kind": "compute", + "expect": 48.16489955702848, + "dtype": "quantity", + "unit": "m/s", + "why": "speed in m/s for [173.393638] (the system Speeds)" + }, + { + "q": "Tell me the speed in m/s with speed of 173.394.", + "a": "48.16489955702848", + "valid": true, + "kind": "compute", + "expect": 48.16489955702848, + "dtype": "quantity", + "unit": "m/s", + "why": "speed in m/s for [173.393638] (the system Speeds)" + }, + { + "q": "What is the speed in m/s for speed 126.709?", + "a": "35.19701438893955", + "valid": true, + "kind": "compute", + "expect": 35.19701438893955, + "dtype": "quantity", + "unit": "m/s", + "why": "speed in m/s for [126.709252] (the system Speeds)" + }, + { + "q": "Tell me the speed in m/s with speed of 126.709.", + "a": "35.19701438893955", + "valid": true, + "kind": "compute", + "expect": 35.19701438893955, + "dtype": "quantity", + "unit": "m/s", + "why": "speed in m/s for [126.709252] (the system Speeds)" + }, + { + "q": "What is the speed in m/s for speed 0.671697?", + "a": "0.1865825554983871", + "valid": true, + "kind": "compute", + "expect": 0.1865825554983871, + "dtype": "quantity", + "unit": "m/s", + "why": "speed in m/s for [0.671697] (the system Speeds)" + }, + { + "q": "Tell me the speed in m/s with speed of 0.671697.", + "a": "0.1865825554983871", + "valid": true, + "kind": "compute", + "expect": 0.1865825554983871, + "dtype": "quantity", + "unit": "m/s", + "why": "speed in m/s for [0.671697] (the system Speeds)" + }, + { + "q": "What is the speed in m/s for speed 1.16997?", + "a": "0.3249911127716488", + "valid": true, + "kind": "compute", + "expect": 0.3249911127716488, + "dtype": "quantity", + "unit": "m/s", + "why": "speed in m/s for [1.169968] (the system Speeds)" + }, + { + "q": "Tell me the speed in m/s with speed of 1.16997.", + "a": "0.3249911127716488", + "valid": true, + "kind": "compute", + "expect": 0.3249911127716488, + "dtype": "quantity", + "unit": "m/s", + "why": "speed in m/s for [1.169968] (the system Speeds)" + }, + { + "q": "What is the speed in m/s?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Could you work out the speed in m/s, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "What is the speed in m/s 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 speed in m/s 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 speed in m/s with speed of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative speed: no example had one" + }, + { + "q": "Which speed in m/s is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the speed is 2.57003, what is the speed in m/s?", + "a": "0.713897198139901", + "valid": true, + "kind": "compute", + "expect": 0.713897198139901, + "dtype": "quantity", + "unit": "m/s", + "why": "speed in m/s for [2.57003] (the system Speeds)" + }, + { + "q": "Could you work out the speed in m/s given speed = 2.57003?", + "a": "0.713897198139901", + "valid": true, + "kind": "compute", + "expect": 0.713897198139901, + "dtype": "quantity", + "unit": "m/s", + "why": "speed in m/s for [2.57003] (the system Speeds)" + }, + { + "q": "I need the speed in m/s for a speed of 48.921, please.", + "a": "13.589164127781066", + "valid": true, + "kind": "compute", + "expect": 13.589164127781066, + "dtype": "quantity", + "unit": "m/s", + "why": "speed in m/s for [48.920991] (the system Speeds)" + }, + { + "q": "If the speed is 48.921, what is the speed in m/s?", + "a": "13.589164127781066", + "valid": true, + "kind": "compute", + "expect": 13.589164127781066, + "dtype": "quantity", + "unit": "m/s", + "why": "speed in m/s for [48.920991] (the system Speeds)" + }, + { + "q": "Could you work out the speed in m/s given speed = 0.348438?", + "a": "0.09678824156389736", + "valid": true, + "kind": "compute", + "expect": 0.09678824156389736, + "dtype": "quantity", + "unit": "m/s", + "why": "speed in m/s for [0.348438] (the system Speeds)" + }, + { + "q": "I need the speed in m/s for a speed of 0.348438, please.", + "a": "0.09678824156389736", + "valid": true, + "kind": "compute", + "expect": 0.09678824156389736, + "dtype": "quantity", + "unit": "m/s", + "why": "speed in m/s for [0.348438] (the system Speeds)" + }, + { + "q": "If the speed is 252.709, what is the speed in m/s?", + "a": "70.1969511804243", + "valid": true, + "kind": "compute", + "expect": 70.1969511804243, + "dtype": "quantity", + "unit": "m/s", + "why": "speed in m/s for [252.709024] (the system Speeds)" + }, + { + "q": "Could you work out the speed in m/s given speed = 252.709?", + "a": "70.1969511804243", + "valid": true, + "kind": "compute", + "expect": 70.1969511804243, + "dtype": "quantity", + "unit": "m/s", + "why": "speed in m/s for [252.709024] (the system Speeds)" + }, + { + "q": "I need the speed in m/s for a speed of 20.3966, please.", + "a": "5.665710544989844", + "valid": true, + "kind": "compute", + "expect": 5.665710544989844, + "dtype": "quantity", + "unit": "m/s", + "why": "speed in m/s for [20.396558] (the system Speeds)" + }, + { + "q": "If the speed is 20.3966, what is the speed in m/s?", + "a": "5.665710544989844", + "valid": true, + "kind": "compute", + "expect": 5.665710544989844, + "dtype": "quantity", + "unit": "m/s", + "why": "speed in m/s for [20.396558] (the system Speeds)" + }, + { + "q": "Could you work out the speed in m/s given speed = 134.993?", + "a": "37.49804377242722", + "valid": true, + "kind": "compute", + "expect": 37.49804377242722, + "dtype": "quantity", + "unit": "m/s", + "why": "speed in m/s for [134.992958] (the system Speeds)" + }, + { + "q": "I need the speed in m/s for a speed of 134.993, please.", + "a": "37.49804377242722", + "valid": true, + "kind": "compute", + "expect": 37.49804377242722, + "dtype": "quantity", + "unit": "m/s", + "why": "speed in m/s for [134.992958] (the system Speeds)" + }, + { + "q": "I need the speed in m/s, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Can you tell me the speed in m/s?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Do you know the speed in m/s 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 speed in m/s 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 speed in m/s given speed = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative speed: no example had one" + }, + { + "q": "What is a typical speed in m/s?", + "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-speed-6/program.py b/root/organs10/physics/specialists/solved-speed-6/program.py new file mode 100644 index 0000000000000000000000000000000000000000..57eeeae4f3bda03f9ed694fa07d7d24d820e127a --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-6/program.py @@ -0,0 +1,7 @@ +"""speed from speed (Speeds) +The maths organ (systems.py): solved from the system K = M; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(speed): + return 0.277777777777778*speed diff --git a/root/organs10/physics/specialists/solved-speed-6/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-speed-6/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-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-speed-7/organ_formula_v2.py b/root/organs10/physics/specialists/solved-speed-7/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-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-speed-7/panel.json b/root/organs10/physics/specialists/solved-speed-7/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..14332707b2b6c692629cd7da74720d98fc56a949 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-7/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the speed for centripetal force 107.961, mass 59.4105 and radius 5.77717?", + "a": "3.2401009696770915", + "valid": true, + "kind": "compute", + "expect": 3.2401009696770915, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105, 5.777165] (the system Centripetal force)" + }, + { + "q": "Tell me the speed with centripetal force of 107.961, mass of 59.4105 and radius of 5.77717.", + "a": "3.2401009696770915", + "valid": true, + "kind": "compute", + "expect": 3.2401009696770915, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105, 5.777165] (the system Centripetal force)" + }, + { + "q": "What is the speed for centripetal force 85.2416, mass 160.878 and radius 2.69428?", + "a": "1.1948084415446603", + "valid": true, + "kind": "compute", + "expect": 1.1948084415446603, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [85.241623, 160.878442, 2.69428] (the system Centripetal force)" + }, + { + "q": "Tell me the speed with centripetal force of 85.2416, mass of 160.878 and radius of 2.69428.", + "a": "1.1948084415446603", + "valid": true, + "kind": "compute", + "expect": 1.1948084415446603, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [85.241623, 160.878442, 2.69428] (the system Centripetal force)" + }, + { + "q": "What is the speed for centripetal force 45.6157, mass 4.97131 and radius 94.305?", + "a": "29.416376944564785", + "valid": true, + "kind": "compute", + "expect": 29.416376944564785, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [45.61567, 4.971307, 94.305046] (the system Centripetal force)" + }, + { + "q": "Tell me the speed with centripetal force of 45.6157, mass of 4.97131 and radius of 94.305.", + "a": "29.416376944564785", + "valid": true, + "kind": "compute", + "expect": 29.416376944564785, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [45.61567, 4.971307, 94.305046] (the system Centripetal force)" + }, + { + "q": "What is the speed for centripetal force 10.5638, mass 198.839 and radius 0.671697?", + "a": "0.18890654119717645", + "valid": true, + "kind": "compute", + "expect": 0.18890654119717645, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [10.563843, 198.838955, 0.671697] (the system Centripetal force)" + }, + { + "q": "Tell me the speed with centripetal force of 10.5638, mass of 198.839 and radius of 0.671697.", + "a": "0.18890654119717645", + "valid": true, + "kind": "compute", + "expect": 0.18890654119717645, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [10.563843, 198.838955, 0.671697] (the system Centripetal force)" + }, + { + "q": "What is the speed for centripetal force 21.7931, mass 29.5106 and radius 8.50341?", + "a": "2.5059198386980013", + "valid": true, + "kind": "compute", + "expect": 2.5059198386980013, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609, 8.503407] (the system Centripetal force)" + }, + { + "q": "Tell me the speed with centripetal force of 21.7931, mass of 29.5106 and radius of 8.50341.", + "a": "2.5059198386980013", + "valid": true, + "kind": "compute", + "expect": 2.5059198386980013, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609, 8.503407] (the system Centripetal force)" + }, + { + "q": "What is the speed for centripetal force 223.509, mass 17.3414 and radius 7.10705?", + "a": "9.570826726293298", + "valid": true, + "kind": "compute", + "expect": 9.570826726293298, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [223.508796, 17.341435, 7.107049] (the system Centripetal force)" + }, + { + "q": "Tell me the speed with centripetal force of 223.509, mass of 17.3414 and radius of 7.10705.", + "a": "9.570826726293298", + "valid": true, + "kind": "compute", + "expect": 9.570826726293298, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [223.508796, 17.341435, 7.107049] (the system Centripetal force)" + }, + { + "q": "What is the speed for centripetal force 107.961 and mass 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radius is not given" + }, + { + "q": "Tell me the speed with centripetal force of 85.2416 and mass of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radius is not given" + }, + { + "q": "What is the speed of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the speed of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the speed with centripetal force of -45.6157, mass of 4.97131 and radius of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative centripetal force: no example had one" + }, + { + "q": "Which speed is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the centripetal force is 17.7888, mass is 167.625 and the radius is 10.3291, what is the speed?", + "a": "1.046969463811213", + "valid": true, + "kind": "compute", + "expect": 1.046969463811213, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [17.788778, 167.624948, 10.329054] (the system Centripetal force)" + }, + { + "q": "Could you work out the speed given centripetal force = 17.7888, mass = 167.625, radius = 10.3291?", + "a": "1.046969463811213", + "valid": true, + "kind": "compute", + "expect": 1.046969463811213, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [17.788778, 167.624948, 10.329054] (the system Centripetal force)" + }, + { + "q": "I need the speed for a centripetal force of 21.511, a mass of 173.394 and a radius of 251.084, please.", + "a": "5.581146621636312", + "valid": true, + "kind": "compute", + "expect": 5.581146621636312, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.511016, 173.393638, 251.084037] (the system Centripetal force)" + }, + { + "q": "If the centripetal force is 21.511, mass is 173.394 and the radius is 251.084, what is the speed?", + "a": "5.581146621636312", + "valid": true, + "kind": "compute", + "expect": 5.581146621636312, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.511016, 173.393638, 251.084037] (the system Centripetal force)" + }, + { + "q": "Could you work out the speed given centripetal force = 129.245, mass = 1.18359, radius = 15.9416?", + "a": "41.72269199630706", + "valid": true, + "kind": "compute", + "expect": 41.72269199630706, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [129.244984, 1.18359, 15.941612] (the system Centripetal force)" + }, + { + "q": "I need the speed for a centripetal force of 129.245, a mass of 1.18359 and a radius of 15.9416, please.", + "a": "41.72269199630706", + "valid": true, + "kind": "compute", + "expect": 41.72269199630706, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [129.244984, 1.18359, 15.941612] (the system Centripetal force)" + }, + { + "q": "If the centripetal force is 20.3966, mass is 18.3169 and the radius is 6.8386, what is the speed?", + "a": "2.759538039597842", + "valid": true, + "kind": "compute", + "expect": 2.759538039597842, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [20.396558, 18.316867, 6.838598] (the system Centripetal force)" + }, + { + "q": "Could you work out the speed given centripetal force = 20.3966, mass = 18.3169, radius = 6.8386?", + "a": "2.759538039597842", + "valid": true, + "kind": "compute", + "expect": 2.759538039597842, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [20.396558, 18.316867, 6.838598] (the system Centripetal force)" + }, + { + "q": "I need the speed for a centripetal force of 185.886, a mass of 13.2373 and a radius of 4.71941, please.", + "a": "8.140814290434816", + "valid": true, + "kind": "compute", + "expect": 8.140814290434816, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [185.886273, 13.237311, 4.719415] (the system Centripetal force)" + }, + { + "q": "If the centripetal force is 185.886, mass is 13.2373 and the radius is 4.71941, what is the speed?", + "a": "8.140814290434816", + "valid": true, + "kind": "compute", + "expect": 8.140814290434816, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [185.886273, 13.237311, 4.719415] (the system Centripetal force)" + }, + { + "q": "Could you work out the speed given centripetal force = 9.1061, mass = 24.5721, radius = 108.451?", + "a": "6.339597067504287", + "valid": true, + "kind": "compute", + "expect": 6.339597067504287, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [9.106104, 24.572086, 108.450792] (the system Centripetal force)" + }, + { + "q": "I need the speed for a centripetal force of 9.1061, a mass of 24.5721 and a radius of 108.451, please.", + "a": "6.339597067504287", + "valid": true, + "kind": "compute", + "expect": 6.339597067504287, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [9.106104, 24.572086, 108.450792] (the system Centripetal force)" + }, + { + "q": "If the centripetal force is 17.7888 and the mass is 167.625, what is the speed?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radius is not given" + }, + { + "q": "Could you work out the speed given centripetal force = 21.511, mass = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radius is not given" + }, + { + "q": "Do you know the speed of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the speed of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the speed given centripetal force = -129.245, mass = 1.18359, radius = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative centripetal force: no example had one" + }, + { + "q": "What is a typical speed?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-speed-7/program.py b/root/organs10/physics/specialists/solved-speed-7/program.py new file mode 100644 index 0000000000000000000000000000000000000000..1d9c2d0af47b81f1da9ba05f80bf31a43e056446 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-7/program.py @@ -0,0 +1,7 @@ +"""speed from centripetal force, mass, radius (Centripetal force) +The maths organ (systems.py): solved from the system F = m*v**2/r; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(centripetal_force, mass, radius): + return 1.0*centripetal_force**0.5*mass**(-0.5)*radius**0.5 diff --git a/root/organs10/physics/specialists/solved-speed-7/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-speed-7/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-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-speed-8/organ_formula_v2.py b/root/organs10/physics/specialists/solved-speed-8/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-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-speed-8/panel.json b/root/organs10/physics/specialists/solved-speed-8/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..f7a3b3025fd4cb4fd9f79312029d8624b5d3dcd4 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-8/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the speed for distance 107.961 and time 59.4105?", + "a": "1.8171983999893244", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893244, + "dtype": "quantity", + "unit": "km/h", + "why": "speed for [107.960666, 59.4105] (the system Average speed)" + }, + { + "q": "Tell me the speed with distance of 107.961 and time of 59.4105.", + "a": "1.8171983999893244", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893244, + "dtype": "quantity", + "unit": "km/h", + "why": "speed for [107.960666, 59.4105] (the system Average speed)" + }, + { + "q": "What is the speed for distance 169.527 and time 280.774?", + "a": "0.6037850281809956", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809956, + "dtype": "quantity", + "unit": "km/h", + "why": "speed for [169.526958, 280.773702] (the system Average speed)" + }, + { + "q": "Tell me the speed with distance of 169.527 and time of 280.774.", + "a": "0.6037850281809956", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809956, + "dtype": "quantity", + "unit": "km/h", + "why": "speed for [169.526958, 280.773702] (the system Average speed)" + }, + { + "q": "What is the speed for distance 1.91195 and time 82.2401?", + "a": "0.023248420388592397", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592397, + "dtype": "quantity", + "unit": "km/h", + "why": "speed for [1.911952, 82.240072] (the system Average speed)" + }, + { + "q": "Tell me the speed with distance of 1.91195 and time of 82.2401.", + "a": "0.023248420388592397", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592397, + "dtype": "quantity", + "unit": "km/h", + "why": "speed for [1.911952, 82.240072] (the system Average speed)" + }, + { + "q": "What is the speed for distance 1.64379 and time 252.709?", + "a": "0.006504696457692341", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692341, + "dtype": "quantity", + "unit": "km/h", + "why": "speed for [1.643795, 252.709024] (the system Average speed)" + }, + { + "q": "Tell me the speed with distance of 1.64379 and time of 252.709.", + "a": "0.006504696457692341", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692341, + "dtype": "quantity", + "unit": "km/h", + "why": "speed for [1.643795, 252.709024] (the system Average speed)" + }, + { + "q": "What is the speed for distance 18.3169 and time 6.8386?", + "a": "2.678453516094291", + "valid": true, + "kind": "compute", + "expect": 2.678453516094291, + "dtype": "quantity", + "unit": "km/h", + "why": "speed for [18.316867, 6.838598] (the system Average speed)" + }, + { + "q": "Tell me the speed with distance of 18.3169 and time of 6.8386.", + "a": "2.678453516094291", + "valid": true, + "kind": "compute", + "expect": 2.678453516094291, + "dtype": "quantity", + "unit": "km/h", + "why": "speed for [18.316867, 6.838598] (the system Average speed)" + }, + { + "q": "What is the speed for distance 156.502 and time 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "km/h", + "why": "speed for [156.50181, 185.886273] (the system Average speed)" + }, + { + "q": "Tell me the speed with distance of 156.502 and time of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "km/h", + "why": "speed for [156.50181, 185.886273] (the system Average speed)" + }, + { + "q": "What is the speed for distance 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "Tell me the speed with distance of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "What is the speed of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the speed of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the speed with distance of -1.91195 and time of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative distance: no example had one" + }, + { + "q": "Which speed is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the distance is 8.50728 and the time is 17.7888, what is the speed?", + "a": "0.4782387296673113", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673113, + "dtype": "quantity", + "unit": "km/h", + "why": "speed for [8.507283, 17.788778] (the system Average speed)" + }, + { + "q": "Could you work out the speed given distance = 8.50728, time = 17.7888?", + "a": "0.4782387296673113", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673113, + "dtype": "quantity", + "unit": "km/h", + "why": "speed for [8.507283, 17.788778] (the system Average speed)" + }, + { + "q": "I need the speed for a distance of 8.24951 and a time of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "km/h", + "why": "speed for [8.24951, 0.634021] (the system Average speed)" + }, + { + "q": "If the distance is 8.24951 and the time is 0.634021, what is the speed?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "km/h", + "why": "speed for [8.24951, 0.634021] (the system Average speed)" + }, + { + "q": "Could you work out the speed given distance = 0.318734, time = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "km/h", + "why": "speed for [0.318734, 9.566071] (the system Average speed)" + }, + { + "q": "I need the speed for a distance of 0.318734 and a time of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "km/h", + "why": "speed for [0.318734, 9.566071] (the system Average speed)" + }, + { + "q": "If the distance is 0.671697 and the time is 14.2496, what is the speed?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "km/h", + "why": "speed for [0.671697, 14.249569] (the system Average speed)" + }, + { + "q": "Could you work out the speed given distance = 0.671697, time = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "km/h", + "why": "speed for [0.671697, 14.249569] (the system Average speed)" + }, + { + "q": "I need the speed for a distance of 21.7931 and a time of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "km/h", + "why": "speed for [21.793126, 29.510609] (the system Average speed)" + }, + { + "q": "If the distance is 21.7931 and the time is 29.5106, what is the speed?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "km/h", + "why": "speed for [21.793126, 29.510609] (the system Average speed)" + }, + { + "q": "Could you work out the speed given distance = 153.15, time = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "km/h", + "why": "speed for [153.149965, 18.595315] (the system Average speed)" + }, + { + "q": "I need the speed for a distance of 153.15 and a time of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "km/h", + "why": "speed for [153.149965, 18.595315] (the system Average speed)" + }, + { + "q": "If the distance is 8.50728, what is the speed?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "Could you work out the speed given distance = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "Do you know the speed of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the speed of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the speed given distance = -0.318734, time = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative distance: no example had one" + }, + { + "q": "What is a typical speed?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-speed-8/program.py b/root/organs10/physics/specialists/solved-speed-8/program.py new file mode 100644 index 0000000000000000000000000000000000000000..ceb290b73378e7215e51e09897c208b1d83c2039 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-8/program.py @@ -0,0 +1,7 @@ +"""speed from distance, time (Average speed) +The maths organ (systems.py): solved from the system v = d/t; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(distance, time): + return 1.0*distance/time diff --git a/root/organs10/physics/specialists/solved-speed-8/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-speed-8/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-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-speed-9/organ_formula_v2.py b/root/organs10/physics/specialists/solved-speed-9/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-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-speed-9/panel.json b/root/organs10/physics/specialists/solved-speed-9/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..4fcb2372f4f29f6e3f318ed1ae1dc7fd7e266955 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-9/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the speed for Mach number 107.961 and speed of sound 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Tell me the speed with Mach number of 107.961 and speed of sound of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "What is the speed for Mach number 169.527 and speed of sound 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [169.526958, 280.773702] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Tell me the speed with Mach number of 169.527 and speed of sound of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [169.526958, 280.773702] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "What is the speed for Mach number 1.91195 and speed of sound 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.911952, 82.240072] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Tell me the speed with Mach number of 1.91195 and speed of sound of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.911952, 82.240072] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "What is the speed for Mach number 1.64379 and speed of sound 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.643795, 252.709024] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Tell me the speed with Mach number of 1.64379 and speed of sound of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.643795, 252.709024] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "What is the speed for Mach number 18.3169 and speed of sound 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [18.316867, 6.838598] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Tell me the speed with Mach number of 18.3169 and speed of sound of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [18.316867, 6.838598] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "What is the speed for Mach number 156.502 and speed of sound 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [156.50181, 185.886273] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Tell me the speed with Mach number of 156.502 and speed of sound of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [156.50181, 185.886273] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "What is the speed for Mach number 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed of sound is not given" + }, + { + "q": "Tell me the speed with Mach number of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed of sound is not given" + }, + { + "q": "What is the speed of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the speed of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the speed with Mach number of -1.91195 and speed of sound of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Mach number: no example had one" + }, + { + "q": "Which speed is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the Mach number is 8.50728 and the speed of sound is 17.7888, what is the speed?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.507283, 17.788778] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Could you work out the speed given Mach number = 8.50728, speed of sound = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.507283, 17.788778] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "I need the speed for a Mach number of 8.24951 and a speed of sound of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.24951, 0.634021] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "If the Mach number is 8.24951 and the speed of sound is 0.634021, what is the speed?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.24951, 0.634021] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Could you work out the speed given Mach number = 0.318734, speed of sound = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.318734, 9.566071] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "I need the speed for a Mach number of 0.318734 and a speed of sound of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.318734, 9.566071] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "If the Mach number is 0.671697 and the speed of sound is 14.2496, what is the speed?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.671697, 14.249569] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Could you work out the speed given Mach number = 0.671697, speed of sound = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.671697, 14.249569] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "I need the speed for a Mach number of 21.7931 and a speed of sound of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "If the Mach number is 21.7931 and the speed of sound is 29.5106, what is the speed?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Could you work out the speed given Mach number = 153.15, speed of sound = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [153.149965, 18.595315] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "I need the speed for a Mach number of 153.15 and a speed of sound of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [153.149965, 18.595315] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "If the Mach number is 8.50728, what is the speed?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed of sound is not given" + }, + { + "q": "Could you work out the speed given Mach number = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed of sound is not given" + }, + { + "q": "Do you know the speed of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the speed of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the speed given Mach number = -0.318734, speed of sound = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Mach number: no example had one" + }, + { + "q": "What is a typical speed?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-speed-9/program.py b/root/organs10/physics/specialists/solved-speed-9/program.py new file mode 100644 index 0000000000000000000000000000000000000000..effb697282efd172d36b8e0778e6bac6ad98605b --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-9/program.py @@ -0,0 +1,7 @@ +"""speed from Mach number, speed of sound (Mach number (Wikidata Q160669)) +The maths organ (systems.py): solved from the system q0 = ((q1)/(q2)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(mach_number, speed_of_sound): + return 1.0*mach_number*speed_of_sound diff --git a/root/organs10/physics/specialists/solved-speed-9/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-speed-9/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-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-speed-of-light-in-a-medium-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium-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-speed-of-light-in-a-medium-2/panel.json b/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..074a628b4f81233e934b674b9ec830297e147337 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the speed of light in a medium for light wavenumber 107.961 and light frequency 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [107.960666, 59.4105] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Tell me the speed of light in a medium with light wavenumber of 107.961 and light frequency of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [107.960666, 59.4105] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "What is the speed of light in a medium for light wavenumber 169.527 and light frequency 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [169.526958, 280.773702] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Tell me the speed of light in a medium with light wavenumber of 169.527 and light frequency of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [169.526958, 280.773702] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "What is the speed of light in a medium for light wavenumber 1.91195 and light frequency 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [1.911952, 82.240072] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Tell me the speed of light in a medium with light wavenumber of 1.91195 and light frequency of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [1.911952, 82.240072] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "What is the speed of light in a medium for light wavenumber 1.64379 and light frequency 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [1.643795, 252.709024] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Tell me the speed of light in a medium with light wavenumber of 1.64379 and light frequency of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [1.643795, 252.709024] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "What is the speed of light in a medium for light wavenumber 18.3169 and light frequency 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [18.316867, 6.838598] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Tell me the speed of light in a medium with light wavenumber of 18.3169 and light frequency of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [18.316867, 6.838598] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "What is the speed of light in a medium for light wavenumber 156.502 and light frequency 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [156.50181, 185.886273] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Tell me the speed of light in a medium with light wavenumber of 156.502 and light frequency of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [156.50181, 185.886273] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "What is the speed of light in a medium for light wavenumber 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the light frequency is not given" + }, + { + "q": "Tell me the speed of light in a medium with light wavenumber of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the light frequency is not given" + }, + { + "q": "What is the speed of light in a medium 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 speed of light in a medium 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 speed of light in a medium with light wavenumber of -1.91195 and light frequency of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative light wavenumber: no example had one" + }, + { + "q": "Which speed of light in a medium is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the light wavenumber is 8.50728 and the light frequency is 17.7888, what is the speed of light in a medium?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [8.507283, 17.788778] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Could you work out the speed of light in a medium given light wavenumber = 8.50728, light frequency = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [8.507283, 17.788778] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "I need the speed of light in a medium for a light wavenumber of 8.24951 and a light frequency of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [8.24951, 0.634021] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "If the light wavenumber is 8.24951 and the light frequency is 0.634021, what is the speed of light in a medium?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [8.24951, 0.634021] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Could you work out the speed of light in a medium given light wavenumber = 0.318734, light frequency = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [0.318734, 9.566071] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "I need the speed of light in a medium for a light wavenumber of 0.318734 and a light frequency of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [0.318734, 9.566071] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "If the light wavenumber is 0.671697 and the light frequency is 14.2496, what is the speed of light in a medium?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [0.671697, 14.249569] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Could you work out the speed of light in a medium given light wavenumber = 0.671697, light frequency = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [0.671697, 14.249569] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "I need the speed of light in a medium for a light wavenumber of 21.7931 and a light frequency of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [21.793126, 29.510609] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "If the light wavenumber is 21.7931 and the light frequency is 29.5106, what is the speed of light in a medium?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [21.793126, 29.510609] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "Could you work out the speed of light in a medium given light wavenumber = 153.15, light frequency = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [153.149965, 18.595315] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "I need the speed of light in a medium for a light wavenumber of 153.15 and a light frequency of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [153.149965, 18.595315] (the system light wavenumber (Wikidata Q106008527))" + }, + { + "q": "If the light wavenumber is 8.50728, what is the speed of light in a medium?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the light frequency is not given" + }, + { + "q": "Could you work out the speed of light in a medium given light wavenumber = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the light frequency is not given" + }, + { + "q": "Do you know the speed of light in a medium 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 speed of light in a medium 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 speed of light in a medium given light wavenumber = -0.318734, light frequency = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative light wavenumber: no example had one" + }, + { + "q": "What is a typical speed of light in a medium?", + "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-speed-of-light-in-a-medium-2/program.py b/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..004990e3bc905bbe16ff69754fed0027b9bd279d --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium-2/program.py @@ -0,0 +1,7 @@ +"""speed of light in a medium from light wavenumber, light frequency (light wavenumber (Wikidata Q106008527)) +The maths organ (systems.py): solved from the system q0 = ((q1)/(q2)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(light_wavenumber, light_frequency): + return 1.0*light_frequency/light_wavenumber diff --git a/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium-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-speed-of-light-in-a-medium-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium-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-speed-of-light-in-a-medium-3/panel.json b/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium-3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..ef80f789163d53f8086e75f42d8669b997c24f90 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium-3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the speed of light in a medium for refractive index 107.961?", + "a": "2037779.2058473695", + "valid": true, + "kind": "compute", + "expect": 2037779.2058473695, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [107.960666] (the system refractive index (Wikidata Q174102))" + }, + { + "q": "Tell me the speed of light in a medium with refractive index of 107.961.", + "a": "2037779.2058473695", + "valid": true, + "kind": "compute", + "expect": 2037779.2058473695, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [107.960666] (the system refractive index (Wikidata Q174102))" + }, + { + "q": "What is the speed of light in a medium for refractive index 22.652?", + "a": "9712156.921832915", + "valid": true, + "kind": "compute", + "expect": 9712156.921832915, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [22.652023] (the system refractive index (Wikidata Q174102))" + }, + { + "q": "Tell me the speed of light in a medium with refractive index of 22.652.", + "a": "9712156.921832915", + "valid": true, + "kind": "compute", + "expect": 9712156.921832915, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [22.652023] (the system refractive index (Wikidata Q174102))" + }, + { + "q": "What is the speed of light in a medium for refractive index 173.394?", + "a": "1268789.339812782", + "valid": true, + "kind": "compute", + "expect": 1268789.339812782, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [173.393638] (the system refractive index (Wikidata Q174102))" + }, + { + "q": "Tell me the speed of light in a medium with refractive index of 173.394.", + "a": "1268789.339812782", + "valid": true, + "kind": "compute", + "expect": 1268789.339812782, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [173.393638] (the system refractive index (Wikidata Q174102))" + }, + { + "q": "What is the speed of light in a medium for refractive index 126.709?", + "a": "1736258.3779354568", + "valid": true, + "kind": "compute", + "expect": 1736258.3779354568, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [126.709252] (the system refractive index (Wikidata Q174102))" + }, + { + "q": "Tell me the speed of light in a medium with refractive index of 126.709.", + "a": "1736258.3779354568", + "valid": true, + "kind": "compute", + "expect": 1736258.3779354568, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [126.709252] (the system refractive index (Wikidata Q174102))" + }, + { + "q": "What is the speed of light in a medium for refractive index 0.671697?", + "a": "327528535.2795985", + "valid": true, + "kind": "compute", + "expect": 327528535.2795985, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [0.671697] (the system refractive index (Wikidata Q174102))" + }, + { + "q": "Tell me the speed of light in a medium with refractive index of 0.671697.", + "a": "327528535.2795985", + "valid": true, + "kind": "compute", + "expect": 327528535.2795985, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [0.671697] (the system refractive index (Wikidata Q174102))" + }, + { + "q": "What is the speed of light in a medium for refractive index 1.16997?", + "a": "188039330.02946487", + "valid": true, + "kind": "compute", + "expect": 188039330.02946487, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [1.169968] (the system refractive index (Wikidata Q174102))" + }, + { + "q": "Tell me the speed of light in a medium with refractive index of 1.16997.", + "a": "188039330.02946487", + "valid": true, + "kind": "compute", + "expect": 188039330.02946487, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [1.169968] (the system refractive index (Wikidata Q174102))" + }, + { + "q": "What is the speed of light in a medium?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the refractive index is not given" + }, + { + "q": "Could you work out the speed of light in a medium, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the refractive index is not given" + }, + { + "q": "What is the speed of light in a medium 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 speed of light in a medium 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 speed of light in a medium with refractive index of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative refractive index: no example had one" + }, + { + "q": "Which speed of light in a medium 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 refractive index is 2.57003, what is the speed of light in a medium?", + "a": "85602116.48167203", + "valid": true, + "kind": "compute", + "expect": 85602116.48167203, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [2.57003] (the system refractive index (Wikidata Q174102))" + }, + { + "q": "Could you work out the speed of light in a medium given refractive index = 2.57003?", + "a": "85602116.48167203", + "valid": true, + "kind": "compute", + "expect": 85602116.48167203, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [2.57003] (the system refractive index (Wikidata Q174102))" + }, + { + "q": "I need the speed of light in a medium for a refractive index of 48.921, please.", + "a": "4497047.0984435575", + "valid": true, + "kind": "compute", + "expect": 4497047.0984435575, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [48.920991] (the system refractive index (Wikidata Q174102))" + }, + { + "q": "If the refractive index is 48.921, what is the speed of light in a medium?", + "a": "4497047.0984435575", + "valid": true, + "kind": "compute", + "expect": 4497047.0984435575, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [48.920991] (the system refractive index (Wikidata Q174102))" + }, + { + "q": "Could you work out the speed of light in a medium given refractive index = 0.348438?", + "a": "631389827.1492717", + "valid": true, + "kind": "compute", + "expect": 631389827.1492717, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [0.348438] (the system refractive index (Wikidata Q174102))" + }, + { + "q": "I need the speed of light in a medium for a refractive index of 0.348438, please.", + "a": "631389827.1492717", + "valid": true, + "kind": "compute", + "expect": 631389827.1492717, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [0.348438] (the system refractive index (Wikidata Q174102))" + }, + { + "q": "If the refractive index is 252.709, what is the speed of light in a medium?", + "a": "870566.457424052", + "valid": true, + "kind": "compute", + "expect": 870566.457424052, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [252.709024] (the system refractive index (Wikidata Q174102))" + }, + { + "q": "Could you work out the speed of light in a medium given refractive index = 252.709?", + "a": "870566.457424052", + "valid": true, + "kind": "compute", + "expect": 870566.457424052, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [252.709024] (the system refractive index (Wikidata Q174102))" + }, + { + "q": "I need the speed of light in a medium for a refractive index of 20.3966, please.", + "a": "10786133.641287291", + "valid": true, + "kind": "compute", + "expect": 10786133.641287291, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [20.396558] (the system refractive index (Wikidata Q174102))" + }, + { + "q": "If the refractive index is 20.3966, what is the speed of light in a medium?", + "a": "10786133.641287291", + "valid": true, + "kind": "compute", + "expect": 10786133.641287291, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [20.396558] (the system refractive index (Wikidata Q174102))" + }, + { + "q": "Could you work out the speed of light in a medium given refractive index = 134.993?", + "a": "1629714.645435634", + "valid": true, + "kind": "compute", + "expect": 1629714.645435634, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [134.992958] (the system refractive index (Wikidata Q174102))" + }, + { + "q": "I need the speed of light in a medium for a refractive index of 134.993, please.", + "a": "1629714.645435634", + "valid": true, + "kind": "compute", + "expect": 1629714.645435634, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [134.992958] (the system refractive index (Wikidata Q174102))" + }, + { + "q": "I need the speed of light in a medium, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the refractive index is not given" + }, + { + "q": "Can you tell me the speed of light in a medium?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the refractive index is not given" + }, + { + "q": "Do you know the speed of light in a medium 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 speed of light in a medium 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 speed of light in a medium given refractive index = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative refractive index: no example had one" + }, + { + "q": "What is a typical speed of light in a medium?", + "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-speed-of-light-in-a-medium-3/program.py b/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..534acfedbbc50219bfbcbb0d3115d7dadb9a51c0 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium-3/program.py @@ -0,0 +1,7 @@ +"""speed of light in a medium from refractive index (refractive index (Wikidata Q174102)) +The maths organ (systems.py): solved from the system q1 = ((q0)/(q2)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(refractive_index): + return 220000000.0/refractive_index diff --git a/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium-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-speed-of-light-in-a-medium/organ_formula_v2.py b/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium/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-speed-of-light-in-a-medium/panel.json b/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..0af4c307bdbff442b8c6cdf943c064f388cc2c87 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the speed of light in a medium for electromagnetic wavelength 107.961 and frequency 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [107.960666, 59.4105] (the system electromagnetic wavelength (Wikidata Q105581076))" + }, + { + "q": "Tell me the speed of light in a medium with electromagnetic wavelength of 107.961 and frequency of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [107.960666, 59.4105] (the system electromagnetic wavelength (Wikidata Q105581076))" + }, + { + "q": "What is the speed of light in a medium for electromagnetic wavelength 169.527 and frequency 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [169.526958, 280.773702] (the system electromagnetic wavelength (Wikidata Q105581076))" + }, + { + "q": "Tell me the speed of light in a medium with electromagnetic wavelength of 169.527 and frequency of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [169.526958, 280.773702] (the system electromagnetic wavelength (Wikidata Q105581076))" + }, + { + "q": "What is the speed of light in a medium for electromagnetic wavelength 1.91195 and frequency 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [1.911952, 82.240072] (the system electromagnetic wavelength (Wikidata Q105581076))" + }, + { + "q": "Tell me the speed of light in a medium with electromagnetic wavelength of 1.91195 and frequency of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [1.911952, 82.240072] (the system electromagnetic wavelength (Wikidata Q105581076))" + }, + { + "q": "What is the speed of light in a medium for electromagnetic wavelength 1.64379 and frequency 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [1.643795, 252.709024] (the system electromagnetic wavelength (Wikidata Q105581076))" + }, + { + "q": "Tell me the speed of light in a medium with electromagnetic wavelength of 1.64379 and frequency of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [1.643795, 252.709024] (the system electromagnetic wavelength (Wikidata Q105581076))" + }, + { + "q": "What is the speed of light in a medium for electromagnetic wavelength 18.3169 and frequency 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [18.316867, 6.838598] (the system electromagnetic wavelength (Wikidata Q105581076))" + }, + { + "q": "Tell me the speed of light in a medium with electromagnetic wavelength of 18.3169 and frequency of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [18.316867, 6.838598] (the system electromagnetic wavelength (Wikidata Q105581076))" + }, + { + "q": "What is the speed of light in a medium for electromagnetic wavelength 156.502 and frequency 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [156.50181, 185.886273] (the system electromagnetic wavelength (Wikidata Q105581076))" + }, + { + "q": "Tell me the speed of light in a medium with electromagnetic wavelength of 156.502 and frequency of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [156.50181, 185.886273] (the system electromagnetic wavelength (Wikidata Q105581076))" + }, + { + "q": "What is the speed of light in a medium for electromagnetic wavelength 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the frequency is not given" + }, + { + "q": "Tell me the speed of light in a medium with electromagnetic wavelength of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the frequency is not given" + }, + { + "q": "What is the speed of light in a medium 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 speed of light in a medium 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 speed of light in a medium with electromagnetic wavelength of -1.91195 and frequency of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electromagnetic wavelength: no example had one" + }, + { + "q": "Which speed of light in a medium 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 electromagnetic wavelength is 8.50728 and the frequency is 17.7888, what is the speed of light in a medium?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [8.507283, 17.788778] (the system electromagnetic wavelength (Wikidata Q105581076))" + }, + { + "q": "Could you work out the speed of light in a medium given electromagnetic wavelength = 8.50728, frequency = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [8.507283, 17.788778] (the system electromagnetic wavelength (Wikidata Q105581076))" + }, + { + "q": "I need the speed of light in a medium for an electromagnetic wavelength of 8.24951 and a frequency of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [8.24951, 0.634021] (the system electromagnetic wavelength (Wikidata Q105581076))" + }, + { + "q": "If the electromagnetic wavelength is 8.24951 and the frequency is 0.634021, what is the speed of light in a medium?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [8.24951, 0.634021] (the system electromagnetic wavelength (Wikidata Q105581076))" + }, + { + "q": "Could you work out the speed of light in a medium given electromagnetic wavelength = 0.318734, frequency = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [0.318734, 9.566071] (the system electromagnetic wavelength (Wikidata Q105581076))" + }, + { + "q": "I need the speed of light in a medium for an electromagnetic wavelength of 0.318734 and a frequency of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [0.318734, 9.566071] (the system electromagnetic wavelength (Wikidata Q105581076))" + }, + { + "q": "If the electromagnetic wavelength is 0.671697 and the frequency is 14.2496, what is the speed of light in a medium?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [0.671697, 14.249569] (the system electromagnetic wavelength (Wikidata Q105581076))" + }, + { + "q": "Could you work out the speed of light in a medium given electromagnetic wavelength = 0.671697, frequency = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [0.671697, 14.249569] (the system electromagnetic wavelength (Wikidata Q105581076))" + }, + { + "q": "I need the speed of light in a medium for an electromagnetic wavelength of 21.7931 and a frequency of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [21.793126, 29.510609] (the system electromagnetic wavelength (Wikidata Q105581076))" + }, + { + "q": "If the electromagnetic wavelength is 21.7931 and the frequency is 29.5106, what is the speed of light in a medium?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [21.793126, 29.510609] (the system electromagnetic wavelength (Wikidata Q105581076))" + }, + { + "q": "Could you work out the speed of light in a medium given electromagnetic wavelength = 153.15, frequency = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [153.149965, 18.595315] (the system electromagnetic wavelength (Wikidata Q105581076))" + }, + { + "q": "I need the speed of light in a medium for an electromagnetic wavelength of 153.15 and a frequency of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of light in a medium for [153.149965, 18.595315] (the system electromagnetic wavelength (Wikidata Q105581076))" + }, + { + "q": "If the electromagnetic wavelength is 8.50728, what is the speed of light in a medium?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the frequency is not given" + }, + { + "q": "Could you work out the speed of light in a medium given electromagnetic wavelength = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the frequency is not given" + }, + { + "q": "Do you know the speed of light in a medium 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 speed of light in a medium 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 speed of light in a medium given electromagnetic wavelength = -0.318734, frequency = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electromagnetic wavelength: no example had one" + }, + { + "q": "What is a typical speed of light in a medium?", + "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-speed-of-light-in-a-medium/program.py b/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium/program.py new file mode 100644 index 0000000000000000000000000000000000000000..b3178d45b852b850358d1a3b27f0a4c4e4e934f2 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium/program.py @@ -0,0 +1,7 @@ +"""speed of light in a medium from electromagnetic wavelength, frequency (electromagnetic wavelength (Wikidata Q105581076)) +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(electromagnetic_wavelength, frequency): + return 1.0*electromagnetic_wavelength*frequency diff --git a/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-of-light-in-a-medium/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-speed-of-sound/organ_formula_v2.py b/root/organs10/physics/specialists/solved-speed-of-sound/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-of-sound/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-speed-of-sound/panel.json b/root/organs10/physics/specialists/solved-speed-of-sound/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..937b2d7e7a482440f67c6b7a24dadaaccfe9a524 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-of-sound/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the speed of sound for Mach number 107.961 and speed 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of sound for [107.960666, 59.4105] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Tell me the speed of sound with Mach number of 107.961 and speed of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of sound for [107.960666, 59.4105] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "What is the speed of sound for Mach number 169.527 and speed 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of sound for [169.526958, 280.773702] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Tell me the speed of sound with Mach number of 169.527 and speed of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of sound for [169.526958, 280.773702] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "What is the speed of sound for Mach number 1.91195 and speed 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of sound for [1.911952, 82.240072] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Tell me the speed of sound with Mach number of 1.91195 and speed of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of sound for [1.911952, 82.240072] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "What is the speed of sound for Mach number 1.64379 and speed 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of sound for [1.643795, 252.709024] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Tell me the speed of sound with Mach number of 1.64379 and speed of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of sound for [1.643795, 252.709024] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "What is the speed of sound for Mach number 18.3169 and speed 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of sound for [18.316867, 6.838598] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Tell me the speed of sound with Mach number of 18.3169 and speed of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of sound for [18.316867, 6.838598] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "What is the speed of sound for Mach number 156.502 and speed 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of sound for [156.50181, 185.886273] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Tell me the speed of sound with Mach number of 156.502 and speed of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of sound for [156.50181, 185.886273] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "What is the speed of sound for Mach number 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Tell me the speed of sound with Mach number of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "What is the speed of sound 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 speed of sound 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 speed of sound with Mach number of -1.91195 and speed of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Mach number: no example had one" + }, + { + "q": "Which speed of sound 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 Mach number is 8.50728 and the speed is 17.7888, what is the speed of sound?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of sound for [8.507283, 17.788778] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Could you work out the speed of sound given Mach number = 8.50728, speed = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of sound for [8.507283, 17.788778] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "I need the speed of sound for a Mach number of 8.24951 and a speed of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of sound for [8.24951, 0.634021] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "If the Mach number is 8.24951 and the speed is 0.634021, what is the speed of sound?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of sound for [8.24951, 0.634021] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Could you work out the speed of sound given Mach number = 0.318734, speed = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of sound for [0.318734, 9.566071] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "I need the speed of sound for a Mach number of 0.318734 and a speed of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of sound for [0.318734, 9.566071] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "If the Mach number is 0.671697 and the speed is 14.2496, what is the speed of sound?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of sound for [0.671697, 14.249569] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Could you work out the speed of sound given Mach number = 0.671697, speed = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of sound for [0.671697, 14.249569] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "I need the speed of sound for a Mach number of 21.7931 and a speed of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of sound for [21.793126, 29.510609] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "If the Mach number is 21.7931 and the speed is 29.5106, what is the speed of sound?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of sound for [21.793126, 29.510609] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "Could you work out the speed of sound given Mach number = 153.15, speed = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of sound for [153.149965, 18.595315] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "I need the speed of sound for a Mach number of 153.15 and a speed of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "m/s", + "why": "speed of sound for [153.149965, 18.595315] (the system Mach number (Wikidata Q160669))" + }, + { + "q": "If the Mach number is 8.50728, what is the speed of sound?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Could you work out the speed of sound given Mach number = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Do you know the speed of sound 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 speed of sound 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 speed of sound given Mach number = -0.318734, speed = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Mach number: no example had one" + }, + { + "q": "What is a typical speed of sound?", + "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-speed-of-sound/program.py b/root/organs10/physics/specialists/solved-speed-of-sound/program.py new file mode 100644 index 0000000000000000000000000000000000000000..a3ca964effa6d3ff55b15259bbdab2ade96c9445 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-of-sound/program.py @@ -0,0 +1,7 @@ +"""speed of sound from Mach number, speed (Mach number (Wikidata Q160669)) +The maths organ (systems.py): solved from the system q0 = ((q1)/(q2)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(mach_number, speed): + return 1.0*speed/mach_number diff --git a/root/organs10/physics/specialists/solved-speed-of-sound/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-speed-of-sound/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-of-sound/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-speed-relative-to-water/organ_formula_v2.py b/root/organs10/physics/specialists/solved-speed-relative-to-water/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-relative-to-water/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-speed-relative-to-water/panel.json b/root/organs10/physics/specialists/solved-speed-relative-to-water/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..1bd5f5d04755b65629157e8aa84e9c3423a9cda9 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-relative-to-water/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the speed relative to water for Reech number 107.961, acceleration of free fall 59.4105 and characteristic length 5.77717?", + "a": "0.17160242511546347", + "valid": true, + "kind": "compute", + "expect": 0.17160242511546347, + "dtype": "quantity", + "unit": "m/s", + "why": "speed relative to water for [107.960666, 59.4105, 5.777165] (the system Reech number (Wikidata Q25401602))" + }, + { + "q": "Tell me the speed relative to water with Reech number of 107.961, acceleration of free fall of 59.4105 and characteristic length of 5.77717.", + "a": "0.17160242511546347", + "valid": true, + "kind": "compute", + "expect": 0.17160242511546347, + "dtype": "quantity", + "unit": "m/s", + "why": "speed relative to water for [107.960666, 59.4105, 5.777165] (the system Reech number (Wikidata Q25401602))" + }, + { + "q": "What is the speed relative to water for Reech number 85.2416, acceleration of free fall 160.878 and characteristic length 2.69428?", + "a": "0.2442410012458068", + "valid": true, + "kind": "compute", + "expect": 0.2442410012458068, + "dtype": "quantity", + "unit": "m/s", + "why": "speed relative to water for [85.241623, 160.878442, 2.69428] (the system Reech number (Wikidata Q25401602))" + }, + { + "q": "Tell me the speed relative to water with Reech number of 85.2416, acceleration of free fall of 160.878 and characteristic length of 2.69428.", + "a": "0.2442410012458068", + "valid": true, + "kind": "compute", + "expect": 0.2442410012458068, + "dtype": "quantity", + "unit": "m/s", + "why": "speed relative to water for [85.241623, 160.878442, 2.69428] (the system Reech number (Wikidata Q25401602))" + }, + { + "q": "What is the speed relative to water for Reech number 45.6157, acceleration of free fall 4.97131 and characteristic length 94.305?", + "a": "0.47466665056267726", + "valid": true, + "kind": "compute", + "expect": 0.47466665056267726, + "dtype": "quantity", + "unit": "m/s", + "why": "speed relative to water for [45.61567, 4.971307, 94.305046] (the system Reech number (Wikidata Q25401602))" + }, + { + "q": "Tell me the speed relative to water with Reech number of 45.6157, acceleration of free fall of 4.97131 and characteristic length of 94.305.", + "a": "0.47466665056267726", + "valid": true, + "kind": "compute", + "expect": 0.47466665056267726, + "dtype": "quantity", + "unit": "m/s", + "why": "speed relative to water for [45.61567, 4.971307, 94.305046] (the system Reech number (Wikidata Q25401602))" + }, + { + "q": "What is the speed relative to water for Reech number 10.5638, acceleration of free fall 198.839 and characteristic length 0.671697?", + "a": "1.0939956060124079", + "valid": true, + "kind": "compute", + "expect": 1.0939956060124079, + "dtype": "quantity", + "unit": "m/s", + "why": "speed relative to water for [10.563843, 198.838955, 0.671697] (the system Reech number (Wikidata Q25401602))" + }, + { + "q": "Tell me the speed relative to water with Reech number of 10.5638, acceleration of free fall of 198.839 and characteristic length of 0.671697.", + "a": "1.0939956060124079", + "valid": true, + "kind": "compute", + "expect": 1.0939956060124079, + "dtype": "quantity", + "unit": "m/s", + "why": "speed relative to water for [10.563843, 198.838955, 0.671697] (the system Reech number (Wikidata Q25401602))" + }, + { + "q": "What is the speed relative to water for Reech number 21.7931, acceleration of free fall 29.5106 and characteristic length 8.50341?", + "a": "0.726885544032195", + "valid": true, + "kind": "compute", + "expect": 0.726885544032195, + "dtype": "quantity", + "unit": "m/s", + "why": "speed relative to water for [21.793126, 29.510609, 8.503407] (the system Reech number (Wikidata Q25401602))" + }, + { + "q": "Tell me the speed relative to water with Reech number of 21.7931, acceleration of free fall of 29.5106 and characteristic length of 8.50341.", + "a": "0.726885544032195", + "valid": true, + "kind": "compute", + "expect": 0.726885544032195, + "dtype": "quantity", + "unit": "m/s", + "why": "speed relative to water for [21.793126, 29.510609, 8.503407] (the system Reech number (Wikidata Q25401602))" + }, + { + "q": "What is the speed relative to water for Reech number 223.509, acceleration of free fall 17.3414 and characteristic length 7.10705?", + "a": "0.049669816594541935", + "valid": true, + "kind": "compute", + "expect": 0.049669816594541935, + "dtype": "quantity", + "unit": "m/s", + "why": "speed relative to water for [223.508796, 17.341435, 7.107049] (the system Reech number (Wikidata Q25401602))" + }, + { + "q": "Tell me the speed relative to water with Reech number of 223.509, acceleration of free fall of 17.3414 and characteristic length of 7.10705.", + "a": "0.049669816594541935", + "valid": true, + "kind": "compute", + "expect": 0.049669816594541935, + "dtype": "quantity", + "unit": "m/s", + "why": "speed relative to water for [223.508796, 17.341435, 7.107049] (the system Reech number (Wikidata Q25401602))" + }, + { + "q": "What is the speed relative to water for Reech number 107.961 and acceleration of free fall 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the characteristic length is not given" + }, + { + "q": "Tell me the speed relative to water with Reech number of 85.2416 and acceleration of free fall of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the characteristic length is not given" + }, + { + "q": "What is the speed relative to water 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 speed relative to water 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 speed relative to water with Reech number of -45.6157, acceleration of free fall of 4.97131 and characteristic length of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Reech number: no example had one" + }, + { + "q": "Which speed relative to water 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 Reech number is 17.7888, acceleration of free fall is 167.625 and the characteristic length is 10.3291, what is the speed relative to water?", + "a": "2.3391253369227987", + "valid": true, + "kind": "compute", + "expect": 2.3391253369227987, + "dtype": "quantity", + "unit": "m/s", + "why": "speed relative to water for [17.788778, 167.624948, 10.329054] (the system Reech number (Wikidata Q25401602))" + }, + { + "q": "Could you work out the speed relative to water given Reech number = 17.7888, acceleration of free fall = 167.625, characteristic length = 10.3291?", + "a": "2.3391253369227987", + "valid": true, + "kind": "compute", + "expect": 2.3391253369227987, + "dtype": "quantity", + "unit": "m/s", + "why": "speed relative to water for [17.788778, 167.624948, 10.329054] (the system Reech number (Wikidata Q25401602))" + }, + { + "q": "I need the speed relative to water for a Reech number of 21.511, an acceleration of free fall of 173.394 and a characteristic length of 251.084, please.", + "a": "9.699854307506323", + "valid": true, + "kind": "compute", + "expect": 9.699854307506323, + "dtype": "quantity", + "unit": "m/s", + "why": "speed relative to water for [21.511016, 173.393638, 251.084037] (the system Reech number (Wikidata Q25401602))" + }, + { + "q": "If the Reech number is 21.511, acceleration of free fall is 173.394 and the characteristic length is 251.084, what is the speed relative to water?", + "a": "9.699854307506323", + "valid": true, + "kind": "compute", + "expect": 9.699854307506323, + "dtype": "quantity", + "unit": "m/s", + "why": "speed relative to water for [21.511016, 173.393638, 251.084037] (the system Reech number (Wikidata Q25401602))" + }, + { + "q": "Could you work out the speed relative to water given Reech number = 129.245, acceleration of free fall = 1.18359, characteristic length = 15.9416?", + "a": "0.03360880352640184", + "valid": true, + "kind": "compute", + "expect": 0.03360880352640184, + "dtype": "quantity", + "unit": "m/s", + "why": "speed relative to water for [129.244984, 1.18359, 15.941612] (the system Reech number (Wikidata Q25401602))" + }, + { + "q": "I need the speed relative to water for a Reech number of 129.245, an acceleration of free fall of 1.18359 and a characteristic length of 15.9416, please.", + "a": "0.03360880352640184", + "valid": true, + "kind": "compute", + "expect": 0.03360880352640184, + "dtype": "quantity", + "unit": "m/s", + "why": "speed relative to water for [129.244984, 1.18359, 15.941612] (the system Reech number (Wikidata Q25401602))" + }, + { + "q": "If the Reech number is 20.3966, acceleration of free fall is 18.3169 and the characteristic length is 6.8386, what is the speed relative to water?", + "a": "0.5487218471293449", + "valid": true, + "kind": "compute", + "expect": 0.5487218471293449, + "dtype": "quantity", + "unit": "m/s", + "why": "speed relative to water for [20.396558, 18.316867, 6.838598] (the system Reech number (Wikidata Q25401602))" + }, + { + "q": "Could you work out the speed relative to water given Reech number = 20.3966, acceleration of free fall = 18.3169, characteristic length = 6.8386?", + "a": "0.5487218471293449", + "valid": true, + "kind": "compute", + "expect": 0.5487218471293449, + "dtype": "quantity", + "unit": "m/s", + "why": "speed relative to water for [20.396558, 18.316867, 6.838598] (the system Reech number (Wikidata Q25401602))" + }, + { + "q": "I need the speed relative to water for a Reech number of 185.886, an acceleration of free fall of 13.2373 and a characteristic length of 4.71941, please.", + "a": "0.0425203316538932", + "valid": true, + "kind": "compute", + "expect": 0.0425203316538932, + "dtype": "quantity", + "unit": "m/s", + "why": "speed relative to water for [185.886273, 13.237311, 4.719415] (the system Reech number (Wikidata Q25401602))" + }, + { + "q": "If the Reech number is 185.886, acceleration of free fall is 13.2373 and the characteristic length is 4.71941, what is the speed relative to water?", + "a": "0.0425203316538932", + "valid": true, + "kind": "compute", + "expect": 0.0425203316538932, + "dtype": "quantity", + "unit": "m/s", + "why": "speed relative to water for [185.886273, 13.237311, 4.719415] (the system Reech number (Wikidata Q25401602))" + }, + { + "q": "Could you work out the speed relative to water given Reech number = 9.1061, acceleration of free fall = 24.5721, characteristic length = 108.451?", + "a": "5.668977955876654", + "valid": true, + "kind": "compute", + "expect": 5.668977955876654, + "dtype": "quantity", + "unit": "m/s", + "why": "speed relative to water for [9.106104, 24.572086, 108.450792] (the system Reech number (Wikidata Q25401602))" + }, + { + "q": "I need the speed relative to water for a Reech number of 9.1061, an acceleration of free fall of 24.5721 and a characteristic length of 108.451, please.", + "a": "5.668977955876654", + "valid": true, + "kind": "compute", + "expect": 5.668977955876654, + "dtype": "quantity", + "unit": "m/s", + "why": "speed relative to water for [9.106104, 24.572086, 108.450792] (the system Reech number (Wikidata Q25401602))" + }, + { + "q": "If the Reech number is 17.7888 and the acceleration of free fall is 167.625, what is the speed relative to water?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the characteristic length is not given" + }, + { + "q": "Could you work out the speed relative to water given Reech number = 21.511, acceleration of free fall = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the characteristic length is not given" + }, + { + "q": "Do you know the speed relative to water 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 speed relative to water 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 speed relative to water given Reech number = -129.245, acceleration of free fall = 1.18359, characteristic length = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Reech number: no example had one" + }, + { + "q": "What is a typical speed relative to water?", + "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-speed-relative-to-water/program.py b/root/organs10/physics/specialists/solved-speed-relative-to-water/program.py new file mode 100644 index 0000000000000000000000000000000000000000..acee2452f3958b9234b6046935fb938e7aab54a6 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-relative-to-water/program.py @@ -0,0 +1,7 @@ +"""speed relative to water from Reech number, acceleration of free fall, characteristic length (Reech number (Wikidata Q25401602)) +The maths organ (systems.py): solved from the system q0 = ((sqrt(q1*q2))/(q3)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(reech_number, acceleration_of_free_fall, characteristic_length): + return 1.0*acceleration_of_free_fall**0.5*characteristic_length**0.5/reech_number diff --git a/root/organs10/physics/specialists/solved-speed-relative-to-water/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-speed-relative-to-water/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed-relative-to-water/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-speed/organ_formula_v2.py b/root/organs10/physics/specialists/solved-speed/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed/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-speed/panel.json b/root/organs10/physics/specialists/solved-speed/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..99c03cc8beaa9c243cd812db6544ba5cbcc543da --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the speed for distance 107.961 and time 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105] (the system Constant speed)" + }, + { + "q": "Tell me the speed with distance of 107.961 and time of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [107.960666, 59.4105] (the system Constant speed)" + }, + { + "q": "What is the speed for distance 169.527 and time 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [169.526958, 280.773702] (the system Constant speed)" + }, + { + "q": "Tell me the speed with distance of 169.527 and time of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [169.526958, 280.773702] (the system Constant speed)" + }, + { + "q": "What is the speed for distance 1.91195 and time 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.911952, 82.240072] (the system Constant speed)" + }, + { + "q": "Tell me the speed with distance of 1.91195 and time of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.911952, 82.240072] (the system Constant speed)" + }, + { + "q": "What is the speed for distance 1.64379 and time 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.643795, 252.709024] (the system Constant speed)" + }, + { + "q": "Tell me the speed with distance of 1.64379 and time of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [1.643795, 252.709024] (the system Constant speed)" + }, + { + "q": "What is the speed for distance 18.3169 and time 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [18.316867, 6.838598] (the system Constant speed)" + }, + { + "q": "Tell me the speed with distance of 18.3169 and time of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [18.316867, 6.838598] (the system Constant speed)" + }, + { + "q": "What is the speed for distance 156.502 and time 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [156.50181, 185.886273] (the system Constant speed)" + }, + { + "q": "Tell me the speed with distance of 156.502 and time of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [156.50181, 185.886273] (the system Constant speed)" + }, + { + "q": "What is the speed for distance 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "Tell me the speed with distance of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "What is the speed of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the speed of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the speed with distance of -1.91195 and time of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative distance: no example had one" + }, + { + "q": "Which speed is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the distance is 8.50728 and the time is 17.7888, what is the speed?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.507283, 17.788778] (the system Constant speed)" + }, + { + "q": "Could you work out the speed given distance = 8.50728, time = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.507283, 17.788778] (the system Constant speed)" + }, + { + "q": "I need the speed for a distance of 8.24951 and a time of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.24951, 0.634021] (the system Constant speed)" + }, + { + "q": "If the distance is 8.24951 and the time is 0.634021, what is the speed?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [8.24951, 0.634021] (the system Constant speed)" + }, + { + "q": "Could you work out the speed given distance = 0.318734, time = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.318734, 9.566071] (the system Constant speed)" + }, + { + "q": "I need the speed for a distance of 0.318734 and a time of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.318734, 9.566071] (the system Constant speed)" + }, + { + "q": "If the distance is 0.671697 and the time is 14.2496, what is the speed?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.671697, 14.249569] (the system Constant speed)" + }, + { + "q": "Could you work out the speed given distance = 0.671697, time = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [0.671697, 14.249569] (the system Constant speed)" + }, + { + "q": "I need the speed for a distance of 21.7931 and a time of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609] (the system Constant speed)" + }, + { + "q": "If the distance is 21.7931 and the time is 29.5106, what is the speed?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [21.793126, 29.510609] (the system Constant speed)" + }, + { + "q": "Could you work out the speed given distance = 153.15, time = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [153.149965, 18.595315] (the system Constant speed)" + }, + { + "q": "I need the speed for a distance of 153.15 and a time of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m/s", + "why": "speed for [153.149965, 18.595315] (the system Constant speed)" + }, + { + "q": "If the distance is 8.50728, what is the speed?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "Could you work out the speed given distance = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "Do you know the speed of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the speed of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the speed given distance = -0.318734, time = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative distance: no example had one" + }, + { + "q": "What is a typical speed?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-speed/program.py b/root/organs10/physics/specialists/solved-speed/program.py new file mode 100644 index 0000000000000000000000000000000000000000..590d676ab4d577304985da4e19a8fd60feb16783 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed/program.py @@ -0,0 +1,7 @@ +"""speed from distance, time (Constant speed) +The maths organ (systems.py): solved from the system d = v*t; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(distance, time): + return 1.0*distance/time diff --git a/root/organs10/physics/specialists/solved-speed/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-speed/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-speed/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-spring-constant/organ_formula_v2.py b/root/organs10/physics/specialists/solved-spring-constant/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-spring-constant/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-spring-constant/panel.json b/root/organs10/physics/specialists/solved-spring-constant/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..3d0332b3a61482c425c430499a8ef7e5ef6bae35 --- /dev/null +++ b/root/organs10/physics/specialists/solved-spring-constant/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the spring constant for force 107.961 and change in length 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "kg/s^2", + "why": "spring constant for [107.960666, 59.4105] (the system Hooke's law (Wikidata Q170282))" + }, + { + "q": "Tell me the spring constant with force of 107.961 and change in length of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "kg/s^2", + "why": "spring constant for [107.960666, 59.4105] (the system Hooke's law (Wikidata Q170282))" + }, + { + "q": "What is the spring constant for force 169.527 and change in length 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "kg/s^2", + "why": "spring constant for [169.526958, 280.773702] (the system Hooke's law (Wikidata Q170282))" + }, + { + "q": "Tell me the spring constant with force of 169.527 and change in length of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "kg/s^2", + "why": "spring constant for [169.526958, 280.773702] (the system Hooke's law (Wikidata Q170282))" + }, + { + "q": "What is the spring constant for force 1.91195 and change in length 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "kg/s^2", + "why": "spring constant for [1.911952, 82.240072] (the system Hooke's law (Wikidata Q170282))" + }, + { + "q": "Tell me the spring constant with force of 1.91195 and change in length of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "kg/s^2", + "why": "spring constant for [1.911952, 82.240072] (the system Hooke's law (Wikidata Q170282))" + }, + { + "q": "What is the spring constant for force 1.64379 and change in length 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "kg/s^2", + "why": "spring constant for [1.643795, 252.709024] (the system Hooke's law (Wikidata Q170282))" + }, + { + "q": "Tell me the spring constant with force of 1.64379 and change in length of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "kg/s^2", + "why": "spring constant for [1.643795, 252.709024] (the system Hooke's law (Wikidata Q170282))" + }, + { + "q": "What is the spring constant for force 18.3169 and change in length 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "kg/s^2", + "why": "spring constant for [18.316867, 6.838598] (the system Hooke's law (Wikidata Q170282))" + }, + { + "q": "Tell me the spring constant with force of 18.3169 and change in length of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "kg/s^2", + "why": "spring constant for [18.316867, 6.838598] (the system Hooke's law (Wikidata Q170282))" + }, + { + "q": "What is the spring constant for force 156.502 and change in length 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "kg/s^2", + "why": "spring constant for [156.50181, 185.886273] (the system Hooke's law (Wikidata Q170282))" + }, + { + "q": "Tell me the spring constant with force of 156.502 and change in length of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "kg/s^2", + "why": "spring constant for [156.50181, 185.886273] (the system Hooke's law (Wikidata Q170282))" + }, + { + "q": "What is the spring constant for force 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the change in length is not given" + }, + { + "q": "Tell me the spring constant with force of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the change in length is not given" + }, + { + "q": "What is the spring constant of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the spring constant of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the spring constant with force of -1.91195 and change in length of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative force: no example had one" + }, + { + "q": "Which spring constant is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the force is 8.50728 and the change in length is 17.7888, what is the spring constant?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "kg/s^2", + "why": "spring constant for [8.507283, 17.788778] (the system Hooke's law (Wikidata Q170282))" + }, + { + "q": "Could you work out the spring constant given force = 8.50728, change in length = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "kg/s^2", + "why": "spring constant for [8.507283, 17.788778] (the system Hooke's law (Wikidata Q170282))" + }, + { + "q": "I need the spring constant for a force of 8.24951 and a change in length of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "kg/s^2", + "why": "spring constant for [8.24951, 0.634021] (the system Hooke's law (Wikidata Q170282))" + }, + { + "q": "If the force is 8.24951 and the change in length is 0.634021, what is the spring constant?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "kg/s^2", + "why": "spring constant for [8.24951, 0.634021] (the system Hooke's law (Wikidata Q170282))" + }, + { + "q": "Could you work out the spring constant given force = 0.318734, change in length = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "kg/s^2", + "why": "spring constant for [0.318734, 9.566071] (the system Hooke's law (Wikidata Q170282))" + }, + { + "q": "I need the spring constant for a force of 0.318734 and a change in length of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "kg/s^2", + "why": "spring constant for [0.318734, 9.566071] (the system Hooke's law (Wikidata Q170282))" + }, + { + "q": "If the force is 0.671697 and the change in length is 14.2496, what is the spring constant?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "kg/s^2", + "why": "spring constant for [0.671697, 14.249569] (the system Hooke's law (Wikidata Q170282))" + }, + { + "q": "Could you work out the spring constant given force = 0.671697, change in length = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "kg/s^2", + "why": "spring constant for [0.671697, 14.249569] (the system Hooke's law (Wikidata Q170282))" + }, + { + "q": "I need the spring constant for a force of 21.7931 and a change in length of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "kg/s^2", + "why": "spring constant for [21.793126, 29.510609] (the system Hooke's law (Wikidata Q170282))" + }, + { + "q": "If the force is 21.7931 and the change in length is 29.5106, what is the spring constant?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "kg/s^2", + "why": "spring constant for [21.793126, 29.510609] (the system Hooke's law (Wikidata Q170282))" + }, + { + "q": "Could you work out the spring constant given force = 153.15, change in length = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "kg/s^2", + "why": "spring constant for [153.149965, 18.595315] (the system Hooke's law (Wikidata Q170282))" + }, + { + "q": "I need the spring constant for a force of 153.15 and a change in length of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "kg/s^2", + "why": "spring constant for [153.149965, 18.595315] (the system Hooke's law (Wikidata Q170282))" + }, + { + "q": "If the force is 8.50728, what is the spring constant?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the change in length is not given" + }, + { + "q": "Could you work out the spring constant given force = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the change in length is not given" + }, + { + "q": "Do you know the spring constant of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the spring constant of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the spring constant given force = -0.318734, change in length = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative force: no example had one" + }, + { + "q": "What is a typical spring constant?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-spring-constant/program.py b/root/organs10/physics/specialists/solved-spring-constant/program.py new file mode 100644 index 0000000000000000000000000000000000000000..d39ba8320f26810a8d55be9c1ee7e4edb368ecfa --- /dev/null +++ b/root/organs10/physics/specialists/solved-spring-constant/program.py @@ -0,0 +1,7 @@ +"""spring constant from force, change in length (Hooke's law (Wikidata Q170282)) +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(force, change_in_length): + return 1.0*force/change_in_length diff --git a/root/organs10/physics/specialists/solved-spring-constant/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-spring-constant/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-spring-constant/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-spring-energy/organ_formula_v2.py b/root/organs10/physics/specialists/solved-spring-energy/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-spring-energy/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-spring-energy/panel.json b/root/organs10/physics/specialists/solved-spring-energy/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..77f2607867ed55f1d0f2a98b8ee8f35dc3ee8043 --- /dev/null +++ b/root/organs10/physics/specialists/solved-spring-energy/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the spring energy for stiffness 107.961 and extension 59.4105?", + "a": "190529.38979457287", + "valid": true, + "kind": "compute", + "expect": 190529.38979457287, + "dtype": "quantity", + "unit": "J", + "why": "spring energy for [107.960666, 59.4105] (the system Spring energy)" + }, + { + "q": "Tell me the spring energy with stiffness of 107.961 and extension of 59.4105.", + "a": "190529.38979457287", + "valid": true, + "kind": "compute", + "expect": 190529.38979457287, + "dtype": "quantity", + "unit": "J", + "why": "spring energy for [107.960666, 59.4105] (the system Spring energy)" + }, + { + "q": "What is the spring energy for stiffness 169.527 and extension 280.774?", + "a": "6682233.21028727", + "valid": true, + "kind": "compute", + "expect": 6682233.21028727, + "dtype": "quantity", + "unit": "J", + "why": "spring energy for [169.526958, 280.773702] (the system Spring energy)" + }, + { + "q": "Tell me the spring energy with stiffness of 169.527 and extension of 280.774.", + "a": "6682233.21028727", + "valid": true, + "kind": "compute", + "expect": 6682233.21028727, + "dtype": "quantity", + "unit": "J", + "why": "spring energy for [169.526958, 280.773702] (the system Spring energy)" + }, + { + "q": "What is the spring energy for stiffness 1.91195 and extension 82.2401?", + "a": "6465.675398533168", + "valid": true, + "kind": "compute", + "expect": 6465.675398533168, + "dtype": "quantity", + "unit": "J", + "why": "spring energy for [1.911952, 82.240072] (the system Spring energy)" + }, + { + "q": "Tell me the spring energy with stiffness of 1.91195 and extension of 82.2401.", + "a": "6465.675398533168", + "valid": true, + "kind": "compute", + "expect": 6465.675398533168, + "dtype": "quantity", + "unit": "J", + "why": "spring energy for [1.911952, 82.240072] (the system Spring energy)" + }, + { + "q": "What is the spring energy for stiffness 1.64379 and extension 252.709?", + "a": "52487.91143204169", + "valid": true, + "kind": "compute", + "expect": 52487.91143204169, + "dtype": "quantity", + "unit": "J", + "why": "spring energy for [1.643795, 252.709024] (the system Spring energy)" + }, + { + "q": "Tell me the spring energy with stiffness of 1.64379 and extension of 252.709.", + "a": "52487.91143204169", + "valid": true, + "kind": "compute", + "expect": 52487.91143204169, + "dtype": "quantity", + "unit": "J", + "why": "spring energy for [1.643795, 252.709024] (the system Spring energy)" + }, + { + "q": "What is the spring energy for stiffness 18.3169 and extension 6.8386?", + "a": "428.30717969297405", + "valid": true, + "kind": "compute", + "expect": 428.30717969297405, + "dtype": "quantity", + "unit": "J", + "why": "spring energy for [18.316867, 6.838598] (the system Spring energy)" + }, + { + "q": "Tell me the spring energy with stiffness of 18.3169 and extension of 6.8386.", + "a": "428.30717969297405", + "valid": true, + "kind": "compute", + "expect": 428.30717969297405, + "dtype": "quantity", + "unit": "J", + "why": "spring energy for [18.316867, 6.838598] (the system Spring energy)" + }, + { + "q": "What is the spring energy for stiffness 156.502 and extension 185.886?", + "a": "2703858.794076354", + "valid": true, + "kind": "compute", + "expect": 2703858.794076354, + "dtype": "quantity", + "unit": "J", + "why": "spring energy for [156.50181, 185.886273] (the system Spring energy)" + }, + { + "q": "Tell me the spring energy with stiffness of 156.502 and extension of 185.886.", + "a": "2703858.794076354", + "valid": true, + "kind": "compute", + "expect": 2703858.794076354, + "dtype": "quantity", + "unit": "J", + "why": "spring energy for [156.50181, 185.886273] (the system Spring energy)" + }, + { + "q": "What is the spring energy for stiffness 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the extension is not given" + }, + { + "q": "Tell me the spring energy with stiffness of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the extension is not given" + }, + { + "q": "What is the spring energy of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the spring energy of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the spring energy with stiffness of -1.91195 and extension of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative stiffness: no example had one" + }, + { + "q": "Which spring energy is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the stiffness is 8.50728 and the extension is 17.7888, what is the spring energy?", + "a": "1346.0249297051848", + "valid": true, + "kind": "compute", + "expect": 1346.0249297051848, + "dtype": "quantity", + "unit": "J", + "why": "spring energy for [8.507283, 17.788778] (the system Spring energy)" + }, + { + "q": "Could you work out the spring energy given stiffness = 8.50728, extension = 17.7888?", + "a": "1346.0249297051848", + "valid": true, + "kind": "compute", + "expect": 1346.0249297051848, + "dtype": "quantity", + "unit": "J", + "why": "spring energy for [8.507283, 17.788778] (the system Spring energy)" + }, + { + "q": "I need the spring energy for a stiffness of 8.24951 and an extension of 0.634021, please.", + "a": "1.6580796880570197", + "valid": true, + "kind": "compute", + "expect": 1.6580796880570197, + "dtype": "quantity", + "unit": "J", + "why": "spring energy for [8.24951, 0.634021] (the system Spring energy)" + }, + { + "q": "If the stiffness is 8.24951 and the extension is 0.634021, what is the spring energy?", + "a": "1.6580796880570197", + "valid": true, + "kind": "compute", + "expect": 1.6580796880570197, + "dtype": "quantity", + "unit": "J", + "why": "spring energy for [8.24951, 0.634021] (the system Spring energy)" + }, + { + "q": "Could you work out the spring energy given stiffness = 0.318734, extension = 9.56607?", + "a": "14.583630796916376", + "valid": true, + "kind": "compute", + "expect": 14.583630796916376, + "dtype": "quantity", + "unit": "J", + "why": "spring energy for [0.318734, 9.566071] (the system Spring energy)" + }, + { + "q": "I need the spring energy for a stiffness of 0.318734 and an extension of 9.56607, please.", + "a": "14.583630796916376", + "valid": true, + "kind": "compute", + "expect": 14.583630796916376, + "dtype": "quantity", + "unit": "J", + "why": "spring energy for [0.318734, 9.566071] (the system Spring energy)" + }, + { + "q": "If the stiffness is 0.671697 and the extension is 14.2496, what is the spring energy?", + "a": "68.19413400667608", + "valid": true, + "kind": "compute", + "expect": 68.19413400667608, + "dtype": "quantity", + "unit": "J", + "why": "spring energy for [0.671697, 14.249569] (the system Spring energy)" + }, + { + "q": "Could you work out the spring energy given stiffness = 0.671697, extension = 14.2496?", + "a": "68.19413400667608", + "valid": true, + "kind": "compute", + "expect": 68.19413400667608, + "dtype": "quantity", + "unit": "J", + "why": "spring energy for [0.671697, 14.249569] (the system Spring energy)" + }, + { + "q": "I need the spring energy for a stiffness of 21.7931 and an extension of 29.5106, please.", + "a": "9489.55566937213", + "valid": true, + "kind": "compute", + "expect": 9489.55566937213, + "dtype": "quantity", + "unit": "J", + "why": "spring energy for [21.793126, 29.510609] (the system Spring energy)" + }, + { + "q": "If the stiffness is 21.7931 and the extension is 29.5106, what is the spring energy?", + "a": "9489.55566937213", + "valid": true, + "kind": "compute", + "expect": 9489.55566937213, + "dtype": "quantity", + "unit": "J", + "why": "spring energy for [21.793126, 29.510609] (the system Spring energy)" + }, + { + "q": "Could you work out the spring energy given stiffness = 153.15, extension = 18.5953?", + "a": "26478.538349874543", + "valid": true, + "kind": "compute", + "expect": 26478.538349874543, + "dtype": "quantity", + "unit": "J", + "why": "spring energy for [153.149965, 18.595315] (the system Spring energy)" + }, + { + "q": "I need the spring energy for a stiffness of 153.15 and an extension of 18.5953, please.", + "a": "26478.538349874543", + "valid": true, + "kind": "compute", + "expect": 26478.538349874543, + "dtype": "quantity", + "unit": "J", + "why": "spring energy for [153.149965, 18.595315] (the system Spring energy)" + }, + { + "q": "If the stiffness is 8.50728, what is the spring energy?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the extension is not given" + }, + { + "q": "Could you work out the spring energy given stiffness = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the extension is not given" + }, + { + "q": "Do you know the spring energy of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the spring energy of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the spring energy given stiffness = -0.318734, extension = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative stiffness: no example had one" + }, + { + "q": "What is a typical spring energy?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-spring-energy/program.py b/root/organs10/physics/specialists/solved-spring-energy/program.py new file mode 100644 index 0000000000000000000000000000000000000000..7ee9a4e934c045afff78896d68b7c45110f7420f --- /dev/null +++ b/root/organs10/physics/specialists/solved-spring-energy/program.py @@ -0,0 +1,7 @@ +"""spring energy from stiffness, extension (Spring energy) +The maths organ (systems.py): solved from the system E = k*x**2/2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(stiffness, extension): + return 0.5*extension**2*stiffness diff --git a/root/organs10/physics/specialists/solved-spring-energy/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-spring-energy/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-spring-energy/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-standard-molality/organ_formula_v2.py b/root/organs10/physics/specialists/solved-standard-molality/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-standard-molality/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-standard-molality/panel.json b/root/organs10/physics/specialists/solved-standard-molality/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..6df69b2ca435a4e19decf5ee429ba83f9b9414fb --- /dev/null +++ b/root/organs10/physics/specialists/solved-standard-molality/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the standard molality for activity coefficient 107.961, activity of solute 59.4105 and molality 5.77717?", + "a": "10.498254293702432", + "valid": true, + "kind": "compute", + "expect": 10.498254293702432, + "dtype": "quantity", + "unit": "mol/kg", + "why": "standard molality for [107.960666, 59.4105, 5.777165] (the system activity coefficient (Wikidata Q745224))" + }, + { + "q": "Tell me the standard molality with activity coefficient of 107.961, activity of solute of 59.4105 and molality of 5.77717.", + "a": "10.498254293702432", + "valid": true, + "kind": "compute", + "expect": 10.498254293702432, + "dtype": "quantity", + "unit": "mol/kg", + "why": "standard molality for [107.960666, 59.4105, 5.777165] (the system activity coefficient (Wikidata Q745224))" + }, + { + "q": "What is the standard molality for activity coefficient 85.2416, activity of solute 160.878 and molality 2.69428?", + "a": "1.4275672119863798", + "valid": true, + "kind": "compute", + "expect": 1.4275672119863798, + "dtype": "quantity", + "unit": "mol/kg", + "why": "standard molality for [85.241623, 160.878442, 2.69428] (the system activity coefficient (Wikidata Q745224))" + }, + { + "q": "Tell me the standard molality with activity coefficient of 85.2416, activity of solute of 160.878 and molality of 2.69428.", + "a": "1.4275672119863798", + "valid": true, + "kind": "compute", + "expect": 1.4275672119863798, + "dtype": "quantity", + "unit": "mol/kg", + "why": "standard molality for [85.241623, 160.878442, 2.69428] (the system activity coefficient (Wikidata Q745224))" + }, + { + "q": "What is the standard molality for activity coefficient 45.6157, activity of solute 4.97131 and molality 94.305?", + "a": "865.3232325447225", + "valid": true, + "kind": "compute", + "expect": 865.3232325447225, + "dtype": "quantity", + "unit": "mol/kg", + "why": "standard molality for [45.61567, 4.971307, 94.305046] (the system activity coefficient (Wikidata Q745224))" + }, + { + "q": "Tell me the standard molality with activity coefficient of 45.6157, activity of solute of 4.97131 and molality of 94.305.", + "a": "865.3232325447225", + "valid": true, + "kind": "compute", + "expect": 865.3232325447225, + "dtype": "quantity", + "unit": "mol/kg", + "why": "standard molality for [45.61567, 4.971307, 94.305046] (the system activity coefficient (Wikidata Q745224))" + }, + { + "q": "What is the standard molality for activity coefficient 10.5638, activity of solute 198.839 and molality 0.671697?", + "a": "0.03568568130708054", + "valid": true, + "kind": "compute", + "expect": 0.03568568130708054, + "dtype": "quantity", + "unit": "mol/kg", + "why": "standard molality for [10.563843, 198.838955, 0.671697] (the system activity coefficient (Wikidata Q745224))" + }, + { + "q": "Tell me the standard molality with activity coefficient of 10.5638, activity of solute of 198.839 and molality of 0.671697.", + "a": "0.03568568130708054", + "valid": true, + "kind": "compute", + "expect": 0.03568568130708054, + "dtype": "quantity", + "unit": "mol/kg", + "why": "standard molality for [10.563843, 198.838955, 0.671697] (the system activity coefficient (Wikidata Q745224))" + }, + { + "q": "What is the standard molality for activity coefficient 21.7931, activity of solute 29.5106 and molality 8.50341?", + "a": "6.279634237980215", + "valid": true, + "kind": "compute", + "expect": 6.279634237980215, + "dtype": "quantity", + "unit": "mol/kg", + "why": "standard molality for [21.793126, 29.510609, 8.503407] (the system activity coefficient (Wikidata Q745224))" + }, + { + "q": "Tell me the standard molality with activity coefficient of 21.7931, activity of solute of 29.5106 and molality of 8.50341.", + "a": "6.279634237980215", + "valid": true, + "kind": "compute", + "expect": 6.279634237980215, + "dtype": "quantity", + "unit": "mol/kg", + "why": "standard molality for [21.793126, 29.510609, 8.503407] (the system activity coefficient (Wikidata Q745224))" + }, + { + "q": "What is the standard molality for activity coefficient 223.509, activity of solute 17.3414 and molality 7.10705?", + "a": "91.60072422473009", + "valid": true, + "kind": "compute", + "expect": 91.60072422473009, + "dtype": "quantity", + "unit": "mol/kg", + "why": "standard molality for [223.508796, 17.341435, 7.107049] (the system activity coefficient (Wikidata Q745224))" + }, + { + "q": "Tell me the standard molality with activity coefficient of 223.509, activity of solute of 17.3414 and molality of 7.10705.", + "a": "91.60072422473009", + "valid": true, + "kind": "compute", + "expect": 91.60072422473009, + "dtype": "quantity", + "unit": "mol/kg", + "why": "standard molality for [223.508796, 17.341435, 7.107049] (the system activity coefficient (Wikidata Q745224))" + }, + { + "q": "What is the standard molality for activity coefficient 107.961 and activity of solute 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the molality is not given" + }, + { + "q": "Tell me the standard molality with activity coefficient of 85.2416 and activity of solute of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the molality is not given" + }, + { + "q": "What is the standard molality 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 standard molality 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 standard molality with activity coefficient of -45.6157, activity of solute of 4.97131 and molality of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative activity coefficient: no example had one" + }, + { + "q": "Which standard molality 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 activity coefficient is 17.7888, activity of solute is 167.625 and the molality is 10.3291, what is the standard molality?", + "a": "1.0961450581531387", + "valid": true, + "kind": "compute", + "expect": 1.0961450581531387, + "dtype": "quantity", + "unit": "mol/kg", + "why": "standard molality for [17.788778, 167.624948, 10.329054] (the system activity coefficient (Wikidata Q745224))" + }, + { + "q": "Could you work out the standard molality given activity coefficient = 17.7888, activity of solute = 167.625, molality = 10.3291?", + "a": "1.0961450581531387", + "valid": true, + "kind": "compute", + "expect": 1.0961450581531387, + "dtype": "quantity", + "unit": "mol/kg", + "why": "standard molality for [17.788778, 167.624948, 10.329054] (the system activity coefficient (Wikidata Q745224))" + }, + { + "q": "I need the standard molality for an activity coefficient of 21.511, an activity of solute of 173.394 and a molality of 251.084, please.", + "a": "31.149197612202414", + "valid": true, + "kind": "compute", + "expect": 31.149197612202414, + "dtype": "quantity", + "unit": "mol/kg", + "why": "standard molality for [21.511016, 173.393638, 251.084037] (the system activity coefficient (Wikidata Q745224))" + }, + { + "q": "If the activity coefficient is 21.511, activity of solute is 173.394 and the molality is 251.084, what is the standard molality?", + "a": "31.149197612202414", + "valid": true, + "kind": "compute", + "expect": 31.149197612202414, + "dtype": "quantity", + "unit": "mol/kg", + "why": "standard molality for [21.511016, 173.393638, 251.084037] (the system activity coefficient (Wikidata Q745224))" + }, + { + "q": "Could you work out the standard molality given activity coefficient = 129.245, activity of solute = 1.18359, molality = 15.9416?", + "a": "1740.7830274187056", + "valid": true, + "kind": "compute", + "expect": 1740.7830274187056, + "dtype": "quantity", + "unit": "mol/kg", + "why": "standard molality for [129.244984, 1.18359, 15.941612] (the system activity coefficient (Wikidata Q745224))" + }, + { + "q": "I need the standard molality for an activity coefficient of 129.245, an activity of solute of 1.18359 and a molality of 15.9416, please.", + "a": "1740.7830274187056", + "valid": true, + "kind": "compute", + "expect": 1740.7830274187056, + "dtype": "quantity", + "unit": "mol/kg", + "why": "standard molality for [129.244984, 1.18359, 15.941612] (the system activity coefficient (Wikidata Q745224))" + }, + { + "q": "If the activity coefficient is 20.3966, activity of solute is 18.3169 and the molality is 6.8386, what is the standard molality?", + "a": "7.615050191987504", + "valid": true, + "kind": "compute", + "expect": 7.615050191987504, + "dtype": "quantity", + "unit": "mol/kg", + "why": "standard molality for [20.396558, 18.316867, 6.838598] (the system activity coefficient (Wikidata Q745224))" + }, + { + "q": "Could you work out the standard molality given activity coefficient = 20.3966, activity of solute = 18.3169, molality = 6.8386?", + "a": "7.615050191987504", + "valid": true, + "kind": "compute", + "expect": 7.615050191987504, + "dtype": "quantity", + "unit": "mol/kg", + "why": "standard molality for [20.396558, 18.316867, 6.838598] (the system activity coefficient (Wikidata Q745224))" + }, + { + "q": "I need the standard molality for an activity coefficient of 185.886, an activity of solute of 13.2373 and a molality of 4.71941, please.", + "a": "66.27285731134774", + "valid": true, + "kind": "compute", + "expect": 66.27285731134774, + "dtype": "quantity", + "unit": "mol/kg", + "why": "standard molality for [185.886273, 13.237311, 4.719415] (the system activity coefficient (Wikidata Q745224))" + }, + { + "q": "If the activity coefficient is 185.886, activity of solute is 13.2373 and the molality is 4.71941, what is the standard molality?", + "a": "66.27285731134774", + "valid": true, + "kind": "compute", + "expect": 66.27285731134774, + "dtype": "quantity", + "unit": "mol/kg", + "why": "standard molality for [185.886273, 13.237311, 4.719415] (the system activity coefficient (Wikidata Q745224))" + }, + { + "q": "Could you work out the standard molality given activity coefficient = 9.1061, activity of solute = 24.5721, molality = 108.451?", + "a": "40.19049097830896", + "valid": true, + "kind": "compute", + "expect": 40.19049097830896, + "dtype": "quantity", + "unit": "mol/kg", + "why": "standard molality for [9.106104, 24.572086, 108.450792] (the system activity coefficient (Wikidata Q745224))" + }, + { + "q": "I need the standard molality for an activity coefficient of 9.1061, an activity of solute of 24.5721 and a molality of 108.451, please.", + "a": "40.19049097830896", + "valid": true, + "kind": "compute", + "expect": 40.19049097830896, + "dtype": "quantity", + "unit": "mol/kg", + "why": "standard molality for [9.106104, 24.572086, 108.450792] (the system activity coefficient (Wikidata Q745224))" + }, + { + "q": "If the activity coefficient is 17.7888 and the activity of solute is 167.625, what is the standard molality?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the molality is not given" + }, + { + "q": "Could you work out the standard molality given activity coefficient = 21.511, activity of solute = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the molality is not given" + }, + { + "q": "Do you know the standard molality 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 standard molality 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 standard molality given activity coefficient = -129.245, activity of solute = 1.18359, molality = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative activity coefficient: no example had one" + }, + { + "q": "What is a typical standard molality?", + "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-standard-molality/program.py b/root/organs10/physics/specialists/solved-standard-molality/program.py new file mode 100644 index 0000000000000000000000000000000000000000..5b45b73d3f69db5e410010306c4bd8426089114b --- /dev/null +++ b/root/organs10/physics/specialists/solved-standard-molality/program.py @@ -0,0 +1,7 @@ +"""standard molality from activity coefficient, activity of solute, molality (activity coefficient (Wikidata Q745224)) +The maths organ (systems.py): solved from the system q1 = ((q2)/(q3 / q0)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(activity_coefficient, activity_of_solute, molality): + return 1.0*activity_coefficient*molality/activity_of_solute diff --git a/root/organs10/physics/specialists/solved-standard-molality/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-standard-molality/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-standard-molality/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-static-friction-coefficient/organ_formula_v2.py b/root/organs10/physics/specialists/solved-static-friction-coefficient/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-static-friction-coefficient/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-static-friction-coefficient/panel.json b/root/organs10/physics/specialists/solved-static-friction-coefficient/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..a78042fa24dbd2787fc0dce8f4a590986751a453 --- /dev/null +++ b/root/organs10/physics/specialists/solved-static-friction-coefficient/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the static friction coefficient for maximum tangential force 107.961 and normal force 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "static friction coefficient for [107.960666, 59.4105] (the system static friction coefficient (Wikidata Q73695673))" + }, + { + "q": "Tell me the static friction coefficient with maximum tangential force of 107.961 and normal force of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "static friction coefficient for [107.960666, 59.4105] (the system static friction coefficient (Wikidata Q73695673))" + }, + { + "q": "What is the static friction coefficient for maximum tangential force 169.527 and normal force 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "static friction coefficient for [169.526958, 280.773702] (the system static friction coefficient (Wikidata Q73695673))" + }, + { + "q": "Tell me the static friction coefficient with maximum tangential force of 169.527 and normal force of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "static friction coefficient for [169.526958, 280.773702] (the system static friction coefficient (Wikidata Q73695673))" + }, + { + "q": "What is the static friction coefficient for maximum tangential force 1.91195 and normal force 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "static friction coefficient for [1.911952, 82.240072] (the system static friction coefficient (Wikidata Q73695673))" + }, + { + "q": "Tell me the static friction coefficient with maximum tangential force of 1.91195 and normal force of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "static friction coefficient for [1.911952, 82.240072] (the system static friction coefficient (Wikidata Q73695673))" + }, + { + "q": "What is the static friction coefficient for maximum tangential force 1.64379 and normal force 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "static friction coefficient for [1.643795, 252.709024] (the system static friction coefficient (Wikidata Q73695673))" + }, + { + "q": "Tell me the static friction coefficient with maximum tangential force of 1.64379 and normal force of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "static friction coefficient for [1.643795, 252.709024] (the system static friction coefficient (Wikidata Q73695673))" + }, + { + "q": "What is the static friction coefficient for maximum tangential force 18.3169 and normal force 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "static friction coefficient for [18.316867, 6.838598] (the system static friction coefficient (Wikidata Q73695673))" + }, + { + "q": "Tell me the static friction coefficient with maximum tangential force of 18.3169 and normal force of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "static friction coefficient for [18.316867, 6.838598] (the system static friction coefficient (Wikidata Q73695673))" + }, + { + "q": "What is the static friction coefficient for maximum tangential force 156.502 and normal force 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "static friction coefficient for [156.50181, 185.886273] (the system static friction coefficient (Wikidata Q73695673))" + }, + { + "q": "Tell me the static friction coefficient with maximum tangential force of 156.502 and normal force of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "static friction coefficient for [156.50181, 185.886273] (the system static friction coefficient (Wikidata Q73695673))" + }, + { + "q": "What is the static friction coefficient for maximum tangential force 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the normal force is not given" + }, + { + "q": "Tell me the static friction coefficient with maximum tangential force of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the normal force is not given" + }, + { + "q": "What is the static friction coefficient of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the static friction coefficient of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the static friction coefficient with maximum tangential force of -1.91195 and normal force of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative maximum tangential force: no example had one" + }, + { + "q": "Which static friction coefficient is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the maximum tangential force is 8.50728 and the normal force is 17.7888, what is the static friction coefficient?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "static friction coefficient for [8.507283, 17.788778] (the system static friction coefficient (Wikidata Q73695673))" + }, + { + "q": "Could you work out the static friction coefficient given maximum tangential force = 8.50728, normal force = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "static friction coefficient for [8.507283, 17.788778] (the system static friction coefficient (Wikidata Q73695673))" + }, + { + "q": "I need the static friction coefficient for a maximum tangential force of 8.24951 and a normal force of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "static friction coefficient for [8.24951, 0.634021] (the system static friction coefficient (Wikidata Q73695673))" + }, + { + "q": "If the maximum tangential force is 8.24951 and the normal force is 0.634021, what is the static friction coefficient?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "static friction coefficient for [8.24951, 0.634021] (the system static friction coefficient (Wikidata Q73695673))" + }, + { + "q": "Could you work out the static friction coefficient given maximum tangential force = 0.318734, normal force = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "static friction coefficient for [0.318734, 9.566071] (the system static friction coefficient (Wikidata Q73695673))" + }, + { + "q": "I need the static friction coefficient for a maximum tangential force of 0.318734 and a normal force of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "static friction coefficient for [0.318734, 9.566071] (the system static friction coefficient (Wikidata Q73695673))" + }, + { + "q": "If the maximum tangential force is 0.671697 and the normal force is 14.2496, what is the static friction coefficient?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "static friction coefficient for [0.671697, 14.249569] (the system static friction coefficient (Wikidata Q73695673))" + }, + { + "q": "Could you work out the static friction coefficient given maximum tangential force = 0.671697, normal force = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "static friction coefficient for [0.671697, 14.249569] (the system static friction coefficient (Wikidata Q73695673))" + }, + { + "q": "I need the static friction coefficient for a maximum tangential force of 21.7931 and a normal force of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "static friction coefficient for [21.793126, 29.510609] (the system static friction coefficient (Wikidata Q73695673))" + }, + { + "q": "If the maximum tangential force is 21.7931 and the normal force is 29.5106, what is the static friction coefficient?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "static friction coefficient for [21.793126, 29.510609] (the system static friction coefficient (Wikidata Q73695673))" + }, + { + "q": "Could you work out the static friction coefficient given maximum tangential force = 153.15, normal force = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "static friction coefficient for [153.149965, 18.595315] (the system static friction coefficient (Wikidata Q73695673))" + }, + { + "q": "I need the static friction coefficient for a maximum tangential force of 153.15 and a normal force of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "static friction coefficient for [153.149965, 18.595315] (the system static friction coefficient (Wikidata Q73695673))" + }, + { + "q": "If the maximum tangential force is 8.50728, what is the static friction coefficient?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the normal force is not given" + }, + { + "q": "Could you work out the static friction coefficient given maximum tangential force = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the normal force is not given" + }, + { + "q": "Do you know the static friction coefficient of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the static friction coefficient of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the static friction coefficient given maximum tangential force = -0.318734, normal force = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative maximum tangential force: no example had one" + }, + { + "q": "What is a typical static friction coefficient?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-static-friction-coefficient/program.py b/root/organs10/physics/specialists/solved-static-friction-coefficient/program.py new file mode 100644 index 0000000000000000000000000000000000000000..b2b7a7ecfcac57004b0f8f7c5f81cb6fe2917023 --- /dev/null +++ b/root/organs10/physics/specialists/solved-static-friction-coefficient/program.py @@ -0,0 +1,7 @@ +"""static friction coefficient from maximum tangential force, normal force (static friction coefficient (Wikidata Q73695673)) +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(maximum_tangential_force, normal_force): + return 1.0*maximum_tangential_force/normal_force diff --git a/root/organs10/physics/specialists/solved-static-friction-coefficient/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-static-friction-coefficient/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-static-friction-coefficient/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-stefan-number/organ_formula_v2.py b/root/organs10/physics/specialists/solved-stefan-number/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-stefan-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-stefan-number/panel.json b/root/organs10/physics/specialists/solved-stefan-number/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..249bd857c93f3df6260f857dad555fc4014a109e --- /dev/null +++ b/root/organs10/physics/specialists/solved-stefan-number/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the Stefan number for specific heat capacity at constant pressure 107.961, temperature difference 59.4105 and specific latent heat of phase transition 5.77717?", + "a": "1110.2327161569876", + "valid": true, + "kind": "compute", + "expect": 1110.2327161569876, + "dtype": "quantity", + "unit": "1", + "why": "Stefan number for [107.960666, 59.4105, 5.777165] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Tell me the Stefan number with specific heat capacity at constant pressure of 107.961, temperature difference of 59.4105 and specific latent heat of phase transition of 5.77717.", + "a": "1110.2327161569876", + "valid": true, + "kind": "compute", + "expect": 1110.2327161569876, + "dtype": "quantity", + "unit": "1", + "why": "Stefan number for [107.960666, 59.4105, 5.777165] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "What is the Stefan number for specific heat capacity at constant pressure 85.2416, temperature difference 160.878 and specific latent heat of phase transition 2.69428?", + "a": "5089.871888725082", + "valid": true, + "kind": "compute", + "expect": 5089.871888725082, + "dtype": "quantity", + "unit": "1", + "why": "Stefan number for [85.241623, 160.878442, 2.69428] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Tell me the Stefan number with specific heat capacity at constant pressure of 85.2416, temperature difference of 160.878 and specific latent heat of phase transition of 2.69428.", + "a": "5089.871888725082", + "valid": true, + "kind": "compute", + "expect": 5089.871888725082, + "dtype": "quantity", + "unit": "1", + "why": "Stefan number for [85.241623, 160.878442, 2.69428] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "What is the Stefan number for specific heat capacity at constant pressure 45.6157, temperature difference 4.97131 and specific latent heat of phase transition 94.305?", + "a": "2.404638268801381", + "valid": true, + "kind": "compute", + "expect": 2.404638268801381, + "dtype": "quantity", + "unit": "1", + "why": "Stefan number for [45.61567, 4.971307, 94.305046] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Tell me the Stefan number with specific heat capacity at constant pressure of 45.6157, temperature difference of 4.97131 and specific latent heat of phase transition of 94.305.", + "a": "2.404638268801381", + "valid": true, + "kind": "compute", + "expect": 2.404638268801381, + "dtype": "quantity", + "unit": "1", + "why": "Stefan number for [45.61567, 4.971307, 94.305046] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "What is the Stefan number for specific heat capacity at constant pressure 10.5638, temperature difference 198.839 and specific latent heat of phase transition 0.671697?", + "a": "3127.1582727254026", + "valid": true, + "kind": "compute", + "expect": 3127.1582727254026, + "dtype": "quantity", + "unit": "1", + "why": "Stefan number for [10.563843, 198.838955, 0.671697] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Tell me the Stefan number with specific heat capacity at constant pressure of 10.5638, temperature difference of 198.839 and specific latent heat of phase transition of 0.671697.", + "a": "3127.1582727254026", + "valid": true, + "kind": "compute", + "expect": 3127.1582727254026, + "dtype": "quantity", + "unit": "1", + "why": "Stefan number for [10.563843, 198.838955, 0.671697] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "What is the Stefan number for specific heat capacity at constant pressure 21.7931, temperature difference 29.5106 and specific latent heat of phase transition 8.50341?", + "a": "75.6318512050043", + "valid": true, + "kind": "compute", + "expect": 75.6318512050043, + "dtype": "quantity", + "unit": "1", + "why": "Stefan number for [21.793126, 29.510609, 8.503407] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Tell me the Stefan number with specific heat capacity at constant pressure of 21.7931, temperature difference of 29.5106 and specific latent heat of phase transition of 8.50341.", + "a": "75.6318512050043", + "valid": true, + "kind": "compute", + "expect": 75.6318512050043, + "dtype": "quantity", + "unit": "1", + "why": "Stefan number for [21.793126, 29.510609, 8.503407] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "What is the Stefan number for specific heat capacity at constant pressure 223.509, temperature difference 17.3414 and specific latent heat of phase transition 7.10705?", + "a": "545.3688543410761", + "valid": true, + "kind": "compute", + "expect": 545.3688543410761, + "dtype": "quantity", + "unit": "1", + "why": "Stefan number for [223.508796, 17.341435, 7.107049] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Tell me the Stefan number with specific heat capacity at constant pressure of 223.509, temperature difference of 17.3414 and specific latent heat of phase transition of 7.10705.", + "a": "545.3688543410761", + "valid": true, + "kind": "compute", + "expect": 545.3688543410761, + "dtype": "quantity", + "unit": "1", + "why": "Stefan number for [223.508796, 17.341435, 7.107049] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "What is the Stefan number for specific heat capacity at constant pressure 107.961 and temperature difference 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the specific latent heat of phase transition is not given" + }, + { + "q": "Tell me the Stefan number with specific heat capacity at constant pressure of 85.2416 and temperature difference of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the specific latent heat of phase transition is not given" + }, + { + "q": "What is the Stefan 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 Stefan 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 Stefan number with specific heat capacity at constant pressure of -45.6157, temperature difference of 4.97131 and specific latent heat of phase transition of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative specific heat capacity at constant pressure: no example had one" + }, + { + "q": "Which Stefan 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 specific heat capacity at constant pressure is 17.7888, temperature difference is 167.625 and the specific latent heat of phase transition is 10.3291, what is the Stefan number?", + "a": "288.6849919329577", + "valid": true, + "kind": "compute", + "expect": 288.6849919329577, + "dtype": "quantity", + "unit": "1", + "why": "Stefan number for [17.788778, 167.624948, 10.329054] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Could you work out the Stefan number given specific heat capacity at constant pressure = 17.7888, temperature difference = 167.625, specific latent heat of phase transition = 10.3291?", + "a": "288.6849919329577", + "valid": true, + "kind": "compute", + "expect": 288.6849919329577, + "dtype": "quantity", + "unit": "1", + "why": "Stefan number for [17.788778, 167.624948, 10.329054] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "I need the Stefan number for a specific heat capacity at constant pressure of 21.511, a temperature difference of 173.394 and a specific latent heat of phase transition of 251.084, please.", + "a": "14.855079375871984", + "valid": true, + "kind": "compute", + "expect": 14.855079375871984, + "dtype": "quantity", + "unit": "1", + "why": "Stefan number for [21.511016, 173.393638, 251.084037] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "If the specific heat capacity at constant pressure is 21.511, temperature difference is 173.394 and the specific latent heat of phase transition is 251.084, what is the Stefan number?", + "a": "14.855079375871984", + "valid": true, + "kind": "compute", + "expect": 14.855079375871984, + "dtype": "quantity", + "unit": "1", + "why": "Stefan number for [21.511016, 173.393638, 251.084037] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Could you work out the Stefan number given specific heat capacity at constant pressure = 129.245, temperature difference = 1.18359, specific latent heat of phase transition = 15.9416?", + "a": "9.59583449816029", + "valid": true, + "kind": "compute", + "expect": 9.59583449816029, + "dtype": "quantity", + "unit": "1", + "why": "Stefan number for [129.244984, 1.18359, 15.941612] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "I need the Stefan number for a specific heat capacity at constant pressure of 129.245, a temperature difference of 1.18359 and a specific latent heat of phase transition of 15.9416, please.", + "a": "9.59583449816029", + "valid": true, + "kind": "compute", + "expect": 9.59583449816029, + "dtype": "quantity", + "unit": "1", + "why": "Stefan number for [129.244984, 1.18359, 15.941612] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "If the specific heat capacity at constant pressure is 20.3966, temperature difference is 18.3169 and the specific latent heat of phase transition is 6.8386, what is the Stefan number?", + "a": "54.63123238944198", + "valid": true, + "kind": "compute", + "expect": 54.63123238944198, + "dtype": "quantity", + "unit": "1", + "why": "Stefan number for [20.396558, 18.316867, 6.838598] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Could you work out the Stefan number given specific heat capacity at constant pressure = 20.3966, temperature difference = 18.3169, specific latent heat of phase transition = 6.8386?", + "a": "54.63123238944198", + "valid": true, + "kind": "compute", + "expect": 54.63123238944198, + "dtype": "quantity", + "unit": "1", + "why": "Stefan number for [20.396558, 18.316867, 6.838598] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "I need the Stefan number for a specific heat capacity at constant pressure of 185.886, a temperature difference of 13.2373 and a specific latent heat of phase transition of 4.71941, please.", + "a": "521.3854934623429", + "valid": true, + "kind": "compute", + "expect": 521.3854934623429, + "dtype": "quantity", + "unit": "1", + "why": "Stefan number for [185.886273, 13.237311, 4.719415] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "If the specific heat capacity at constant pressure is 185.886, temperature difference is 13.2373 and the specific latent heat of phase transition is 4.71941, what is the Stefan number?", + "a": "521.3854934623429", + "valid": true, + "kind": "compute", + "expect": 521.3854934623429, + "dtype": "quantity", + "unit": "1", + "why": "Stefan number for [185.886273, 13.237311, 4.719415] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "Could you work out the Stefan number given specific heat capacity at constant pressure = 9.1061, temperature difference = 24.5721, specific latent heat of phase transition = 108.451?", + "a": "2.0632027126358947", + "valid": true, + "kind": "compute", + "expect": 2.0632027126358947, + "dtype": "quantity", + "unit": "1", + "why": "Stefan number for [9.106104, 24.572086, 108.450792] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "I need the Stefan number for a specific heat capacity at constant pressure of 9.1061, a temperature difference of 24.5721 and a specific latent heat of phase transition of 108.451, please.", + "a": "2.0632027126358947", + "valid": true, + "kind": "compute", + "expect": 2.0632027126358947, + "dtype": "quantity", + "unit": "1", + "why": "Stefan number for [9.106104, 24.572086, 108.450792] (the system Stefan number (Wikidata Q909876))" + }, + { + "q": "If the specific heat capacity at constant pressure is 17.7888 and the temperature difference is 167.625, what is the Stefan number?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the specific latent heat of phase transition is not given" + }, + { + "q": "Could you work out the Stefan number given specific heat capacity at constant pressure = 21.511, temperature difference = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the specific latent heat of phase transition is not given" + }, + { + "q": "Do you know the Stefan 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 Stefan 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 Stefan number given specific heat capacity at constant pressure = -129.245, temperature difference = 1.18359, specific latent heat of phase transition = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative specific heat capacity at constant pressure: no example had one" + }, + { + "q": "What is a typical Stefan 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-stefan-number/program.py b/root/organs10/physics/specialists/solved-stefan-number/program.py new file mode 100644 index 0000000000000000000000000000000000000000..d7cec427ecb68aaa92836c74a3cec7aaa42aa5e0 --- /dev/null +++ b/root/organs10/physics/specialists/solved-stefan-number/program.py @@ -0,0 +1,7 @@ +"""Stefan number from specific heat capacity at constant pressure, temperature difference, specific latent heat of phase transition (Stefan number (Wikidata Q909876)) +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_heat_capacity_at_constant_pressure, temperature_difference, specific_latent_heat_of_phase_transition): + return 1.0*specific_heat_capacity_at_constant_pressure*temperature_difference/specific_latent_heat_of_phase_transition diff --git a/root/organs10/physics/specialists/solved-stefan-number/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-stefan-number/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-stefan-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-stiffness-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-stiffness-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-stiffness-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-stiffness-2/panel.json b/root/organs10/physics/specialists/solved-stiffness-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..28e41c0fe2fe88cc6574f573d3602106a47333f9 --- /dev/null +++ b/root/organs10/physics/specialists/solved-stiffness-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the stiffness for spring energy 107.961 and extension 59.4105?", + "a": "0.06117431747391241", + "valid": true, + "kind": "compute", + "expect": 0.06117431747391241, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [107.960666, 59.4105] (the system Spring energy)" + }, + { + "q": "Tell me the stiffness with spring energy of 107.961 and extension of 59.4105.", + "a": "0.06117431747391241", + "valid": true, + "kind": "compute", + "expect": 0.06117431747391241, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [107.960666, 59.4105] (the system Spring energy)" + }, + { + "q": "What is the stiffness for spring energy 169.527 and extension 280.774?", + "a": "0.004300865957226923", + "valid": true, + "kind": "compute", + "expect": 0.004300865957226923, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [169.526958, 280.773702] (the system Spring energy)" + }, + { + "q": "Tell me the stiffness with spring energy of 169.527 and extension of 280.774.", + "a": "0.004300865957226923", + "valid": true, + "kind": "compute", + "expect": 0.004300865957226923, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [169.526958, 280.773702] (the system Spring energy)" + }, + { + "q": "What is the stiffness for spring energy 1.91195 and extension 82.2401?", + "a": "0.0005653793793631448", + "valid": true, + "kind": "compute", + "expect": 0.0005653793793631448, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [1.911952, 82.240072] (the system Spring energy)" + }, + { + "q": "Tell me the stiffness with spring energy of 1.91195 and extension of 82.2401.", + "a": "0.0005653793793631448", + "valid": true, + "kind": "compute", + "expect": 0.0005653793793631448, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [1.911952, 82.240072] (the system Spring energy)" + }, + { + "q": "What is the stiffness for spring energy 1.64379 and extension 252.709?", + "a": "5.1479732289018194e-05", + "valid": true, + "kind": "compute", + "expect": 5.1479732289018194e-05, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [1.643795, 252.709024] (the system Spring energy)" + }, + { + "q": "Tell me the stiffness with spring energy of 1.64379 and extension of 252.709.", + "a": "5.1479732289018194e-05", + "valid": true, + "kind": "compute", + "expect": 5.1479732289018194e-05, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [1.643795, 252.709024] (the system Spring energy)" + }, + { + "q": "What is the stiffness for spring energy 18.3169 and extension 6.8386?", + "a": "0.783334096268435", + "valid": true, + "kind": "compute", + "expect": 0.783334096268435, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [18.316867, 6.838598] (the system Spring energy)" + }, + { + "q": "Tell me the stiffness with spring energy of 18.3169 and extension of 6.8386.", + "a": "0.783334096268435", + "valid": true, + "kind": "compute", + "expect": 0.783334096268435, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [18.316867, 6.838598] (the system Spring energy)" + }, + { + "q": "What is the stiffness for spring energy 156.502 and extension 185.886?", + "a": "0.009058467297948734", + "valid": true, + "kind": "compute", + "expect": 0.009058467297948734, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [156.50181, 185.886273] (the system Spring energy)" + }, + { + "q": "Tell me the stiffness with spring energy of 156.502 and extension of 185.886.", + "a": "0.009058467297948734", + "valid": true, + "kind": "compute", + "expect": 0.009058467297948734, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [156.50181, 185.886273] (the system Spring energy)" + }, + { + "q": "What is the stiffness for spring energy 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the extension is not given" + }, + { + "q": "Tell me the stiffness with spring energy of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the extension is not given" + }, + { + "q": "What is the stiffness 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 stiffness 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 stiffness with spring energy of -1.91195 and extension of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative spring energy: no example had one" + }, + { + "q": "Which stiffness 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 spring energy is 8.50728 and the extension is 17.7888, what is the stiffness?", + "a": "0.05376858671673885", + "valid": true, + "kind": "compute", + "expect": 0.05376858671673885, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [8.507283, 17.788778] (the system Spring energy)" + }, + { + "q": "Could you work out the stiffness given spring energy = 8.50728, extension = 17.7888?", + "a": "0.05376858671673885", + "valid": true, + "kind": "compute", + "expect": 0.05376858671673885, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [8.507283, 17.788778] (the system Spring energy)" + }, + { + "q": "I need the stiffness for a spring energy of 8.24951 and an extension of 0.634021, please.", + "a": "41.04411479572118", + "valid": true, + "kind": "compute", + "expect": 41.04411479572118, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [8.24951, 0.634021] (the system Spring energy)" + }, + { + "q": "If the spring energy is 8.24951 and the extension is 0.634021, what is the stiffness?", + "a": "41.04411479572118", + "valid": true, + "kind": "compute", + "expect": 41.04411479572118, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [8.24951, 0.634021] (the system Spring energy)" + }, + { + "q": "Could you work out the stiffness given spring energy = 0.318734, extension = 9.56607?", + "a": "0.006966124866099888", + "valid": true, + "kind": "compute", + "expect": 0.006966124866099888, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [0.318734, 9.566071] (the system Spring energy)" + }, + { + "q": "I need the stiffness for a spring energy of 0.318734 and an extension of 9.56607, please.", + "a": "0.006966124866099888", + "valid": true, + "kind": "compute", + "expect": 0.006966124866099888, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [0.318734, 9.566071] (the system Spring energy)" + }, + { + "q": "If the spring energy is 0.671697 and the extension is 14.2496, what is the stiffness?", + "a": "0.0066160694725911654", + "valid": true, + "kind": "compute", + "expect": 0.0066160694725911654, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [0.671697, 14.249569] (the system Spring energy)" + }, + { + "q": "Could you work out the stiffness given spring energy = 0.671697, extension = 14.2496?", + "a": "0.0066160694725911654", + "valid": true, + "kind": "compute", + "expect": 0.0066160694725911654, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [0.671697, 14.249569] (the system Spring energy)" + }, + { + "q": "I need the stiffness for a spring energy of 21.7931 and an extension of 29.5106, please.", + "a": "0.05004874610111163", + "valid": true, + "kind": "compute", + "expect": 0.05004874610111163, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [21.793126, 29.510609] (the system Spring energy)" + }, + { + "q": "If the spring energy is 21.7931 and the extension is 29.5106, what is the stiffness?", + "a": "0.05004874610111163", + "valid": true, + "kind": "compute", + "expect": 0.05004874610111163, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [21.793126, 29.510609] (the system Spring energy)" + }, + { + "q": "Could you work out the stiffness given spring energy = 153.15, extension = 18.5953?", + "a": "0.8858084049636105", + "valid": true, + "kind": "compute", + "expect": 0.8858084049636105, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [153.149965, 18.595315] (the system Spring energy)" + }, + { + "q": "I need the stiffness for a spring energy of 153.15 and an extension of 18.5953, please.", + "a": "0.8858084049636105", + "valid": true, + "kind": "compute", + "expect": 0.8858084049636105, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [153.149965, 18.595315] (the system Spring energy)" + }, + { + "q": "If the spring energy is 8.50728, what is the stiffness?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the extension is not given" + }, + { + "q": "Could you work out the stiffness given spring energy = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the extension is not given" + }, + { + "q": "Do you know the stiffness 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 stiffness 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 stiffness given spring energy = -0.318734, extension = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative spring energy: no example had one" + }, + { + "q": "What is a typical stiffness?", + "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-stiffness-2/program.py b/root/organs10/physics/specialists/solved-stiffness-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..150fe7873dedd9561f038bf305a3d923a01861fe --- /dev/null +++ b/root/organs10/physics/specialists/solved-stiffness-2/program.py @@ -0,0 +1,7 @@ +"""stiffness from spring energy, extension (Spring energy) +The maths organ (systems.py): solved from the system E = k*x**2/2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(spring_energy, extension): + return 2.0*spring_energy/extension**2 diff --git a/root/organs10/physics/specialists/solved-stiffness-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-stiffness-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-stiffness-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-stiffness/organ_formula_v2.py b/root/organs10/physics/specialists/solved-stiffness/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-stiffness/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-stiffness/panel.json b/root/organs10/physics/specialists/solved-stiffness/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..947b2b110a8dfee191cd5b7678a9fc2cd2d89f5d --- /dev/null +++ b/root/organs10/physics/specialists/solved-stiffness/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the stiffness for force 107.961 and extension 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [107.960666, 59.4105] (the system Spring force)" + }, + { + "q": "Tell me the stiffness with force of 107.961 and extension of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [107.960666, 59.4105] (the system Spring force)" + }, + { + "q": "What is the stiffness for force 169.527 and extension 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [169.526958, 280.773702] (the system Spring force)" + }, + { + "q": "Tell me the stiffness with force of 169.527 and extension of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [169.526958, 280.773702] (the system Spring force)" + }, + { + "q": "What is the stiffness for force 1.91195 and extension 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [1.911952, 82.240072] (the system Spring force)" + }, + { + "q": "Tell me the stiffness with force of 1.91195 and extension of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [1.911952, 82.240072] (the system Spring force)" + }, + { + "q": "What is the stiffness for force 1.64379 and extension 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [1.643795, 252.709024] (the system Spring force)" + }, + { + "q": "Tell me the stiffness with force of 1.64379 and extension of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [1.643795, 252.709024] (the system Spring force)" + }, + { + "q": "What is the stiffness for force 18.3169 and extension 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [18.316867, 6.838598] (the system Spring force)" + }, + { + "q": "Tell me the stiffness with force of 18.3169 and extension of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [18.316867, 6.838598] (the system Spring force)" + }, + { + "q": "What is the stiffness for force 156.502 and extension 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [156.50181, 185.886273] (the system Spring force)" + }, + { + "q": "Tell me the stiffness with force of 156.502 and extension of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [156.50181, 185.886273] (the system Spring force)" + }, + { + "q": "What is the stiffness for force 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the extension is not given" + }, + { + "q": "Tell me the stiffness with force of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the extension is not given" + }, + { + "q": "What is the stiffness 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 stiffness 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 stiffness with force of -1.91195 and extension of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative force: no example had one" + }, + { + "q": "Which stiffness is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the force is 8.50728 and the extension is 17.7888, what is the stiffness?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [8.507283, 17.788778] (the system Spring force)" + }, + { + "q": "Could you work out the stiffness given force = 8.50728, extension = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [8.507283, 17.788778] (the system Spring force)" + }, + { + "q": "I need the stiffness for a force of 8.24951 and an extension of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [8.24951, 0.634021] (the system Spring force)" + }, + { + "q": "If the force is 8.24951 and the extension is 0.634021, what is the stiffness?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [8.24951, 0.634021] (the system Spring force)" + }, + { + "q": "Could you work out the stiffness given force = 0.318734, extension = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [0.318734, 9.566071] (the system Spring force)" + }, + { + "q": "I need the stiffness for a force of 0.318734 and an extension of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [0.318734, 9.566071] (the system Spring force)" + }, + { + "q": "If the force is 0.671697 and the extension is 14.2496, what is the stiffness?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [0.671697, 14.249569] (the system Spring force)" + }, + { + "q": "Could you work out the stiffness given force = 0.671697, extension = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [0.671697, 14.249569] (the system Spring force)" + }, + { + "q": "I need the stiffness for a force of 21.7931 and an extension of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [21.793126, 29.510609] (the system Spring force)" + }, + { + "q": "If the force is 21.7931 and the extension is 29.5106, what is the stiffness?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [21.793126, 29.510609] (the system Spring force)" + }, + { + "q": "Could you work out the stiffness given force = 153.15, extension = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [153.149965, 18.595315] (the system Spring force)" + }, + { + "q": "I need the stiffness for a force of 153.15 and an extension of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "N/m", + "why": "stiffness for [153.149965, 18.595315] (the system Spring force)" + }, + { + "q": "If the force is 8.50728, what is the stiffness?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the extension is not given" + }, + { + "q": "Could you work out the stiffness given force = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the extension is not given" + }, + { + "q": "Do you know the stiffness 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 stiffness 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 stiffness given force = -0.318734, extension = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative force: no example had one" + }, + { + "q": "What is a typical stiffness?", + "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-stiffness/program.py b/root/organs10/physics/specialists/solved-stiffness/program.py new file mode 100644 index 0000000000000000000000000000000000000000..84ca105e94cb6fbe747d3034358b7f5f5ef24e72 --- /dev/null +++ b/root/organs10/physics/specialists/solved-stiffness/program.py @@ -0,0 +1,7 @@ +"""stiffness from force, extension (Spring force) +The maths organ (systems.py): solved from the system F = k*x; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(force, extension): + return 1.0*force/extension diff --git a/root/organs10/physics/specialists/solved-stiffness/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-stiffness/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-stiffness/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-stoichiometric-number/organ_formula_v2.py b/root/organs10/physics/specialists/solved-stoichiometric-number/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-stoichiometric-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-stoichiometric-number/panel.json b/root/organs10/physics/specialists/solved-stoichiometric-number/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..4786d5f5781dfdceeed57a4ad67c525d9d9395cb --- /dev/null +++ b/root/organs10/physics/specialists/solved-stoichiometric-number/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the stoichiometric number for equilibrium amount of substance 107.961, initial amount of substance 59.4105 and extent of reaction 5.77717?", + "a": "8.40380514310675", + "valid": true, + "kind": "compute", + "expect": 8.40380514310675, + "dtype": "quantity", + "unit": "1", + "why": "stoichiometric number for [107.960666, 59.4105, 5.777165] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "Tell me the stoichiometric number with equilibrium amount of substance of 107.961, initial amount of substance of 59.4105 and extent of reaction of 5.77717.", + "a": "8.40380514310675", + "valid": true, + "kind": "compute", + "expect": 8.40380514310675, + "dtype": "quantity", + "unit": "1", + "why": "stoichiometric number for [107.960666, 59.4105, 5.777165] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "What is the stoichiometric number for equilibrium amount of substance 82.2401, initial amount of substance 13.999 and extent of reaction 0.348438?", + "a": "195.84863763437602", + "valid": true, + "kind": "compute", + "expect": 195.84863763437602, + "dtype": "quantity", + "unit": "1", + "why": "stoichiometric number for [82.240072, 13.999029, 0.348438] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "Tell me the stoichiometric number with equilibrium amount of substance of 82.2401, initial amount of substance of 13.999 and extent of reaction of 0.348438.", + "a": "195.84863763437602", + "valid": true, + "kind": "compute", + "expect": 195.84863763437602, + "dtype": "quantity", + "unit": "1", + "why": "stoichiometric number for [82.240072, 13.999029, 0.348438] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "What is the stoichiometric number for equilibrium amount of substance 6.98086, initial amount of substance 0.551236 and extent of reaction 2.88512?", + "a": "2.2285456772212138", + "valid": true, + "kind": "compute", + "expect": 2.2285456772212138, + "dtype": "quantity", + "unit": "1", + "why": "stoichiometric number for [6.980857, 0.551236, 2.88512] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "Tell me the stoichiometric number with equilibrium amount of substance of 6.98086, initial amount of substance of 0.551236 and extent of reaction of 2.88512.", + "a": "2.2285456772212138", + "valid": true, + "kind": "compute", + "expect": 2.2285456772212138, + "dtype": "quantity", + "unit": "1", + "why": "stoichiometric number for [6.980857, 0.551236, 2.88512] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "What is the stoichiometric number for equilibrium amount of substance 2.34957, initial amount of substance 1.16997 and extent of reaction 1.14865?", + "a": "1.026944917824565", + "valid": true, + "kind": "compute", + "expect": 1.026944917824565, + "dtype": "quantity", + "unit": "1", + "why": "stoichiometric number for [2.349571, 1.169968, 1.148652] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "Tell me the stoichiometric number with equilibrium amount of substance of 2.34957, initial amount of substance of 1.16997 and extent of reaction of 1.14865.", + "a": "1.026944917824565", + "valid": true, + "kind": "compute", + "expect": 1.026944917824565, + "dtype": "quantity", + "unit": "1", + "why": "stoichiometric number for [2.349571, 1.169968, 1.148652] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "What is the stoichiometric number for equilibrium amount of substance 223.509, initial amount of substance 17.3414 and extent of reaction 7.10705?", + "a": "29.00885536137714", + "valid": true, + "kind": "compute", + "expect": 29.00885536137714, + "dtype": "quantity", + "unit": "1", + "why": "stoichiometric number for [223.508796, 17.341435, 7.107049] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "Tell me the stoichiometric number with equilibrium amount of substance of 223.509, initial amount of substance of 17.3414 and extent of reaction of 7.10705.", + "a": "29.00885536137714", + "valid": true, + "kind": "compute", + "expect": 29.00885536137714, + "dtype": "quantity", + "unit": "1", + "why": "stoichiometric number for [223.508796, 17.341435, 7.107049] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "What is the stoichiometric number for equilibrium amount of substance 16.8989, initial amount of substance 4.71659 and extent of reaction 4.07774?", + "a": "2.9875167729276084", + "valid": true, + "kind": "compute", + "expect": 2.9875167729276084, + "dtype": "quantity", + "unit": "1", + "why": "stoichiometric number for [16.898916, 4.716591, 4.077743] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "Tell me the stoichiometric number with equilibrium amount of substance of 16.8989, initial amount of substance of 4.71659 and extent of reaction of 4.07774.", + "a": "2.9875167729276084", + "valid": true, + "kind": "compute", + "expect": 2.9875167729276084, + "dtype": "quantity", + "unit": "1", + "why": "stoichiometric number for [16.898916, 4.716591, 4.077743] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "What is the stoichiometric number for equilibrium amount of substance 107.961 and initial amount of substance 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the extent of reaction is not given" + }, + { + "q": "Tell me the stoichiometric number with equilibrium amount of substance of 82.2401 and initial amount of substance of 13.999.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the extent of reaction is not given" + }, + { + "q": "What is the stoichiometric 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 stoichiometric 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 stoichiometric number with equilibrium amount of substance of -6.98086, initial amount of substance of 0.551236 and extent of reaction of 2.88512.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative equilibrium amount of substance: no example had one" + }, + { + "q": "Which stoichiometric 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 equilibrium amount of substance is 8.24951, initial amount of substance is 0.634021 and the extent of reaction is 6.34623, what is the stoichiometric number?", + "a": "1.2000028646440837", + "valid": true, + "kind": "compute", + "expect": 1.2000028646440837, + "dtype": "quantity", + "unit": "1", + "why": "stoichiometric number for [8.24951, 0.634021, 6.346226] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "Could you work out the stoichiometric number given equilibrium amount of substance = 8.24951, initial amount of substance = 0.634021, extent of reaction = 6.34623?", + "a": "1.2000028646440837", + "valid": true, + "kind": "compute", + "expect": 1.2000028646440837, + "dtype": "quantity", + "unit": "1", + "why": "stoichiometric number for [8.24951, 0.634021, 6.346226] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "I need the stoichiometric number for an equilibrium amount of substance of 126.709, an initial amount of substance of 1.70503 and an extent of reaction of 2.9896, please.", + "a": "41.81302463299672", + "valid": true, + "kind": "compute", + "expect": 41.81302463299672, + "dtype": "quantity", + "unit": "1", + "why": "stoichiometric number for [126.709252, 1.705032, 2.9896] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "If the equilibrium amount of substance is 126.709, initial amount of substance is 1.70503 and the extent of reaction is 2.9896, what is the stoichiometric number?", + "a": "41.81302463299672", + "valid": true, + "kind": "compute", + "expect": 41.81302463299672, + "dtype": "quantity", + "unit": "1", + "why": "stoichiometric number for [126.709252, 1.705032, 2.9896] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "Could you work out the stoichiometric number given equilibrium amount of substance = 87.781, initial amount of substance = 13.2084, extent of reaction = 246.329?", + "a": "0.30273568538290435", + "valid": true, + "kind": "compute", + "expect": 0.30273568538290435, + "dtype": "quantity", + "unit": "1", + "why": "stoichiometric number for [87.781013, 13.208419, 246.329053] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "I need the stoichiometric number for an equilibrium amount of substance of 87.781, an initial amount of substance of 13.2084 and an extent of reaction of 246.329, please.", + "a": "0.30273568538290435", + "valid": true, + "kind": "compute", + "expect": 0.30273568538290435, + "dtype": "quantity", + "unit": "1", + "why": "stoichiometric number for [87.781013, 13.208419, 246.329053] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "If the equilibrium amount of substance is 186.27, initial amount of substance is 106.508 and the extent of reaction is 156.502, what is the stoichiometric number?", + "a": "0.5096582582601115", + "valid": true, + "kind": "compute", + "expect": 0.5096582582601115, + "dtype": "quantity", + "unit": "1", + "why": "stoichiometric number for [186.270057, 106.507617, 156.50181] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "Could you work out the stoichiometric number given equilibrium amount of substance = 186.27, initial amount of substance = 106.508, extent of reaction = 156.502?", + "a": "0.5096582582601115", + "valid": true, + "kind": "compute", + "expect": 0.5096582582601115, + "dtype": "quantity", + "unit": "1", + "why": "stoichiometric number for [186.270057, 106.507617, 156.50181] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "I need the stoichiometric number for an equilibrium amount of substance of 88.6624, an initial amount of substance of 0.633611 and an extent of reaction of 0.869111, please.", + "a": "101.28591758436798", + "valid": true, + "kind": "compute", + "expect": 101.28591758436798, + "dtype": "quantity", + "unit": "1", + "why": "stoichiometric number for [88.662361, 0.633611, 0.869111] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "If the equilibrium amount of substance is 88.6624, initial amount of substance is 0.633611 and the extent of reaction is 0.869111, what is the stoichiometric number?", + "a": "101.28591758436798", + "valid": true, + "kind": "compute", + "expect": 101.28591758436798, + "dtype": "quantity", + "unit": "1", + "why": "stoichiometric number for [88.662361, 0.633611, 0.869111] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "Could you work out the stoichiometric number given equilibrium amount of substance = 1.90441, initial amount of substance = 1.45374, extent of reaction = 27.5916?", + "a": "0.01633332913840586", + "valid": true, + "kind": "compute", + "expect": 0.01633332913840586, + "dtype": "quantity", + "unit": "1", + "why": "stoichiometric number for [1.904407, 1.453744, 27.591617] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "I need the stoichiometric number for an equilibrium amount of substance of 1.90441, an initial amount of substance of 1.45374 and an extent of reaction of 27.5916, please.", + "a": "0.01633332913840586", + "valid": true, + "kind": "compute", + "expect": 0.01633332913840586, + "dtype": "quantity", + "unit": "1", + "why": "stoichiometric number for [1.904407, 1.453744, 27.591617] (the system extent of reaction (Wikidata Q899046))" + }, + { + "q": "If the equilibrium amount of substance is 8.24951 and the initial amount of substance is 0.634021, what is the stoichiometric number?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the extent of reaction is not given" + }, + { + "q": "Could you work out the stoichiometric number given equilibrium amount of substance = 126.709, initial amount of substance = 1.70503?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the extent of reaction is not given" + }, + { + "q": "Do you know the stoichiometric 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 stoichiometric 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 stoichiometric number given equilibrium amount of substance = -87.781, initial amount of substance = 13.2084, extent of reaction = 246.329?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative equilibrium amount of substance: no example had one" + }, + { + "q": "What is a typical stoichiometric 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-stoichiometric-number/program.py b/root/organs10/physics/specialists/solved-stoichiometric-number/program.py new file mode 100644 index 0000000000000000000000000000000000000000..d16ce0d476bb394125d02675b01be65797cf6036 --- /dev/null +++ b/root/organs10/physics/specialists/solved-stoichiometric-number/program.py @@ -0,0 +1,7 @@ +"""stoichiometric number from equilibrium amount of substance, initial amount of substance, extent of reaction (extent of reaction (Wikidata Q899046)) +The maths organ (systems.py): solved from the system q3 = ((q0 - q1)/(q2)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(equilibrium_amount_of_substance, initial_amount_of_substance, extent_of_reaction): + return (1.0*equilibrium_amount_of_substance - 1.0*initial_amount_of_substance)/extent_of_reaction diff --git a/root/organs10/physics/specialists/solved-stoichiometric-number/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-stoichiometric-number/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-stoichiometric-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-stokes-number-for-vibrating-p/organ_formula_v2.py b/root/organs10/physics/specialists/solved-stokes-number-for-vibrating-p/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-stokes-number-for-vibrating-p/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-stokes-number-for-vibrating-p/panel.json b/root/organs10/physics/specialists/solved-stokes-number-for-vibrating-p/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..37cdd04a5e3e30fdf29bac9307705442efbf8d36 --- /dev/null +++ b/root/organs10/physics/specialists/solved-stokes-number-for-vibrating-p/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the Stokes number for vibrating particles for kinematic viscosity 107.961, particle diameter 59.4105 and particle vibration frequency 5.77717?", + "a": "0.005294493194964685", + "valid": true, + "kind": "compute", + "expect": 0.005294493194964685, + "dtype": "quantity", + "unit": "1", + "why": "Stokes number for vibrating particles for [107.960666, 59.4105, 5.777165] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "Tell me the Stokes number for vibrating particles with kinematic viscosity of 107.961, particle diameter of 59.4105 and particle vibration frequency of 5.77717.", + "a": "0.005294493194964685", + "valid": true, + "kind": "compute", + "expect": 0.005294493194964685, + "dtype": "quantity", + "unit": "1", + "why": "Stokes number for vibrating particles for [107.960666, 59.4105, 5.777165] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "What is the Stokes number for vibrating particles for kinematic viscosity 85.2416, particle diameter 160.878 and particle vibration frequency 2.69428?", + "a": "0.001222399876611921", + "valid": true, + "kind": "compute", + "expect": 0.001222399876611921, + "dtype": "quantity", + "unit": "1", + "why": "Stokes number for vibrating particles for [85.241623, 160.878442, 2.69428] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "Tell me the Stokes number for vibrating particles with kinematic viscosity of 85.2416, particle diameter of 160.878 and particle vibration frequency of 2.69428.", + "a": "0.001222399876611921", + "valid": true, + "kind": "compute", + "expect": 0.001222399876611921, + "dtype": "quantity", + "unit": "1", + "why": "Stokes number for vibrating particles for [85.241623, 160.878442, 2.69428] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "What is the Stokes number for vibrating particles for kinematic viscosity 45.6157, particle diameter 4.97131 and particle vibration frequency 94.305?", + "a": "0.019572120206948727", + "valid": true, + "kind": "compute", + "expect": 0.019572120206948727, + "dtype": "quantity", + "unit": "1", + "why": "Stokes number for vibrating particles for [45.61567, 4.971307, 94.305046] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "Tell me the Stokes number for vibrating particles with kinematic viscosity of 45.6157, particle diameter of 4.97131 and particle vibration frequency of 94.305.", + "a": "0.019572120206948727", + "valid": true, + "kind": "compute", + "expect": 0.019572120206948727, + "dtype": "quantity", + "unit": "1", + "why": "Stokes number for vibrating particles for [45.61567, 4.971307, 94.305046] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "What is the Stokes number for vibrating particles for kinematic viscosity 10.5638, particle diameter 198.839 and particle vibration frequency 0.671697?", + "a": "0.00039778229071273146", + "valid": true, + "kind": "compute", + "expect": 0.00039778229071273146, + "dtype": "quantity", + "unit": "1", + "why": "Stokes number for vibrating particles for [10.563843, 198.838955, 0.671697] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "Tell me the Stokes number for vibrating particles with kinematic viscosity of 10.5638, particle diameter of 198.839 and particle vibration frequency of 0.671697.", + "a": "0.00039778229071273146", + "valid": true, + "kind": "compute", + "expect": 0.00039778229071273146, + "dtype": "quantity", + "unit": "1", + "why": "Stokes number for vibrating particles for [10.563843, 198.838955, 0.671697] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "What is the Stokes number for vibrating particles for kinematic viscosity 21.7931, particle diameter 29.5106 and particle vibration frequency 8.50341?", + "a": "0.0029428642530087575", + "valid": true, + "kind": "compute", + "expect": 0.0029428642530087575, + "dtype": "quantity", + "unit": "1", + "why": "Stokes number for vibrating particles for [21.793126, 29.510609, 8.503407] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "Tell me the Stokes number for vibrating particles with kinematic viscosity of 21.7931, particle diameter of 29.5106 and particle vibration frequency of 8.50341.", + "a": "0.0029428642530087575", + "valid": true, + "kind": "compute", + "expect": 0.0029428642530087575, + "dtype": "quantity", + "unit": "1", + "why": "Stokes number for vibrating particles for [21.793126, 29.510609, 8.503407] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "What is the Stokes number for vibrating particles for kinematic viscosity 223.509, particle diameter 17.3414 and particle vibration frequency 7.10705?", + "a": "0.10457677275032796", + "valid": true, + "kind": "compute", + "expect": 0.10457677275032796, + "dtype": "quantity", + "unit": "1", + "why": "Stokes number for vibrating particles for [223.508796, 17.341435, 7.107049] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "Tell me the Stokes number for vibrating particles with kinematic viscosity of 223.509, particle diameter of 17.3414 and particle vibration frequency of 7.10705.", + "a": "0.10457677275032796", + "valid": true, + "kind": "compute", + "expect": 0.10457677275032796, + "dtype": "quantity", + "unit": "1", + "why": "Stokes number for vibrating particles for [223.508796, 17.341435, 7.107049] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "What is the Stokes number for vibrating particles for kinematic viscosity 107.961 and particle diameter 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the particle vibration frequency is not given" + }, + { + "q": "Tell me the Stokes number for vibrating particles with kinematic viscosity of 85.2416 and particle diameter of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the particle vibration frequency is not given" + }, + { + "q": "What is the Stokes number for vibrating particles 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 Stokes number for vibrating particles 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 Stokes number for vibrating particles with kinematic viscosity of -45.6157, particle diameter of 4.97131 and particle vibration frequency of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative kinematic viscosity: no example had one" + }, + { + "q": "Which Stokes number for vibrating particles 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 kinematic viscosity is 17.7888, particle diameter is 167.625 and the particle vibration frequency is 10.3291, what is the Stokes number for vibrating particles?", + "a": "6.129262758464074e-05", + "valid": true, + "kind": "compute", + "expect": 6.129262758464074e-05, + "dtype": "quantity", + "unit": "1", + "why": "Stokes number for vibrating particles for [17.788778, 167.624948, 10.329054] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "Could you work out the Stokes number for vibrating particles given kinematic viscosity = 17.7888, particle diameter = 167.625, particle vibration frequency = 10.3291?", + "a": "6.129262758464074e-05", + "valid": true, + "kind": "compute", + "expect": 6.129262758464074e-05, + "dtype": "quantity", + "unit": "1", + "why": "Stokes number for vibrating particles for [17.788778, 167.624948, 10.329054] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "I need the Stokes number for vibrating particles for a kinematic viscosity of 21.511, a particle diameter of 173.394 and a particle vibration frequency of 251.084, please.", + "a": "2.8495448546766657e-06", + "valid": true, + "kind": "compute", + "expect": 2.8495448546766657e-06, + "dtype": "quantity", + "unit": "1", + "why": "Stokes number for vibrating particles for [21.511016, 173.393638, 251.084037] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "If the kinematic viscosity is 21.511, particle diameter is 173.394 and the particle vibration frequency is 251.084, what is the Stokes number for vibrating particles?", + "a": "2.8495448546766657e-06", + "valid": true, + "kind": "compute", + "expect": 2.8495448546766657e-06, + "dtype": "quantity", + "unit": "1", + "why": "Stokes number for vibrating particles for [21.511016, 173.393638, 251.084037] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "Could you work out the Stokes number for vibrating particles given kinematic viscosity = 129.245, particle diameter = 1.18359, particle vibration frequency = 15.9416?", + "a": "5.787339040577836", + "valid": true, + "kind": "compute", + "expect": 5.787339040577836, + "dtype": "quantity", + "unit": "1", + "why": "Stokes number for vibrating particles for [129.244984, 1.18359, 15.941612] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "I need the Stokes number for vibrating particles for a kinematic viscosity of 129.245, a particle diameter of 1.18359 and a particle vibration frequency of 15.9416, please.", + "a": "5.787339040577836", + "valid": true, + "kind": "compute", + "expect": 5.787339040577836, + "dtype": "quantity", + "unit": "1", + "why": "Stokes number for vibrating particles for [129.244984, 1.18359, 15.941612] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "If the kinematic viscosity is 20.3966, particle diameter is 18.3169 and the particle vibration frequency is 6.8386, what is the Stokes number for vibrating particles?", + "a": "0.008889706445554152", + "valid": true, + "kind": "compute", + "expect": 0.008889706445554152, + "dtype": "quantity", + "unit": "1", + "why": "Stokes number for vibrating particles for [20.396558, 18.316867, 6.838598] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "Could you work out the Stokes number for vibrating particles given kinematic viscosity = 20.3966, particle diameter = 18.3169, particle vibration frequency = 6.8386?", + "a": "0.008889706445554152", + "valid": true, + "kind": "compute", + "expect": 0.008889706445554152, + "dtype": "quantity", + "unit": "1", + "why": "Stokes number for vibrating particles for [20.396558, 18.316867, 6.838598] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "I need the Stokes number for vibrating particles for a kinematic viscosity of 185.886, a particle diameter of 13.2373 and a particle vibration frequency of 4.71941, please.", + "a": "0.22478101040679613", + "valid": true, + "kind": "compute", + "expect": 0.22478101040679613, + "dtype": "quantity", + "unit": "1", + "why": "Stokes number for vibrating particles for [185.886273, 13.237311, 4.719415] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "If the kinematic viscosity is 185.886, particle diameter is 13.2373 and the particle vibration frequency is 4.71941, what is the Stokes number for vibrating particles?", + "a": "0.22478101040679613", + "valid": true, + "kind": "compute", + "expect": 0.22478101040679613, + "dtype": "quantity", + "unit": "1", + "why": "Stokes number for vibrating particles for [185.886273, 13.237311, 4.719415] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "Could you work out the Stokes number for vibrating particles given kinematic viscosity = 9.1061, particle diameter = 24.5721, particle vibration frequency = 108.451?", + "a": "0.0001390643583961609", + "valid": true, + "kind": "compute", + "expect": 0.0001390643583961609, + "dtype": "quantity", + "unit": "1", + "why": "Stokes number for vibrating particles for [9.106104, 24.572086, 108.450792] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "I need the Stokes number for vibrating particles for a kinematic viscosity of 9.1061, a particle diameter of 24.5721 and a particle vibration frequency of 108.451, please.", + "a": "0.0001390643583961609", + "valid": true, + "kind": "compute", + "expect": 0.0001390643583961609, + "dtype": "quantity", + "unit": "1", + "why": "Stokes number for vibrating particles for [9.106104, 24.572086, 108.450792] (the system Stokes number for vibrating particles (Wikidata Q103820258))" + }, + { + "q": "If the kinematic viscosity is 17.7888 and the particle diameter is 167.625, what is the Stokes number for vibrating particles?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the particle vibration frequency is not given" + }, + { + "q": "Could you work out the Stokes number for vibrating particles given kinematic viscosity = 21.511, particle diameter = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the particle vibration frequency is not given" + }, + { + "q": "Do you know the Stokes number for vibrating particles 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 Stokes number for vibrating particles 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 Stokes number for vibrating particles given kinematic viscosity = -129.245, particle diameter = 1.18359, particle vibration frequency = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative kinematic viscosity: no example had one" + }, + { + "q": "What is a typical Stokes number for vibrating particles?", + "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-stokes-number-for-vibrating-p/program.py b/root/organs10/physics/specialists/solved-stokes-number-for-vibrating-p/program.py new file mode 100644 index 0000000000000000000000000000000000000000..f2310e50c55be25bb7aa549702b9a60773044ab7 --- /dev/null +++ b/root/organs10/physics/specialists/solved-stokes-number-for-vibrating-p/program.py @@ -0,0 +1,7 @@ +"""Stokes number for vibrating particles from kinematic viscosity, particle diameter, particle vibration frequency (Stokes number for vibrating particles (Wikidata Q103820258)) +The maths organ (systems.py): solved from the system q0 = ((q1)/(q2 **(2)*q3)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(kinematic_viscosity, particle_diameter, particle_vibration_frequency): + return 1.0*kinematic_viscosity/(particle_diameter**2*particle_vibration_frequency) diff --git a/root/organs10/physics/specialists/solved-stokes-number-for-vibrating-p/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-stokes-number-for-vibrating-p/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-stokes-number-for-vibrating-p/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-stress-relaxation-time/organ_formula_v2.py b/root/organs10/physics/specialists/solved-stress-relaxation-time/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-stress-relaxation-time/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-stress-relaxation-time/panel.json b/root/organs10/physics/specialists/solved-stress-relaxation-time/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..bce48f6d17264a190828ae5e985bfb2e07ac31e3 --- /dev/null +++ b/root/organs10/physics/specialists/solved-stress-relaxation-time/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the stress relaxation time for observation duration 107.961 and Deborah number 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "s", + "why": "stress relaxation time for [107.960666, 59.4105] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "Tell me the stress relaxation time with observation duration of 107.961 and Deborah number of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "s", + "why": "stress relaxation time for [107.960666, 59.4105] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "What is the stress relaxation time for observation duration 169.527 and Deborah number 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "s", + "why": "stress relaxation time for [169.526958, 280.773702] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "Tell me the stress relaxation time with observation duration of 169.527 and Deborah number of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "s", + "why": "stress relaxation time for [169.526958, 280.773702] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "What is the stress relaxation time for observation duration 1.91195 and Deborah number 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "s", + "why": "stress relaxation time for [1.911952, 82.240072] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "Tell me the stress relaxation time with observation duration of 1.91195 and Deborah number of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "s", + "why": "stress relaxation time for [1.911952, 82.240072] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "What is the stress relaxation time for observation duration 1.64379 and Deborah number 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "s", + "why": "stress relaxation time for [1.643795, 252.709024] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "Tell me the stress relaxation time with observation duration of 1.64379 and Deborah number of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "s", + "why": "stress relaxation time for [1.643795, 252.709024] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "What is the stress relaxation time for observation duration 18.3169 and Deborah number 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "s", + "why": "stress relaxation time for [18.316867, 6.838598] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "Tell me the stress relaxation time with observation duration of 18.3169 and Deborah number of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "s", + "why": "stress relaxation time for [18.316867, 6.838598] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "What is the stress relaxation time for observation duration 156.502 and Deborah number 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "s", + "why": "stress relaxation time for [156.50181, 185.886273] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "Tell me the stress relaxation time with observation duration of 156.502 and Deborah number of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "s", + "why": "stress relaxation time for [156.50181, 185.886273] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "What is the stress relaxation time for observation duration 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Deborah number is not given" + }, + { + "q": "Tell me the stress relaxation time with observation duration of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Deborah number is not given" + }, + { + "q": "What is the stress relaxation time of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the stress relaxation time of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the stress relaxation time with observation duration of -1.91195 and Deborah number of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative observation duration: no example had one" + }, + { + "q": "Which stress relaxation time is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the observation duration is 8.50728 and the Deborah number is 17.7888, what is the stress relaxation time?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "s", + "why": "stress relaxation time for [8.507283, 17.788778] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "Could you work out the stress relaxation time given observation duration = 8.50728, Deborah number = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "s", + "why": "stress relaxation time for [8.507283, 17.788778] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "I need the stress relaxation time for an observation duration of 8.24951 and a Deborah number of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "s", + "why": "stress relaxation time for [8.24951, 0.634021] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "If the observation duration is 8.24951 and the Deborah number is 0.634021, what is the stress relaxation time?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "s", + "why": "stress relaxation time for [8.24951, 0.634021] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "Could you work out the stress relaxation time given observation duration = 0.318734, Deborah number = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "s", + "why": "stress relaxation time for [0.318734, 9.566071] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "I need the stress relaxation time for an observation duration of 0.318734 and a Deborah number of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "s", + "why": "stress relaxation time for [0.318734, 9.566071] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "If the observation duration is 0.671697 and the Deborah number is 14.2496, what is the stress relaxation time?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "s", + "why": "stress relaxation time for [0.671697, 14.249569] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "Could you work out the stress relaxation time given observation duration = 0.671697, Deborah number = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "s", + "why": "stress relaxation time for [0.671697, 14.249569] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "I need the stress relaxation time for an observation duration of 21.7931 and a Deborah number of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "s", + "why": "stress relaxation time for [21.793126, 29.510609] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "If the observation duration is 21.7931 and the Deborah number is 29.5106, what is the stress relaxation time?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "s", + "why": "stress relaxation time for [21.793126, 29.510609] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "Could you work out the stress relaxation time given observation duration = 153.15, Deborah number = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "s", + "why": "stress relaxation time for [153.149965, 18.595315] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "I need the stress relaxation time for an observation duration of 153.15 and a Deborah number of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "s", + "why": "stress relaxation time for [153.149965, 18.595315] (the system Deborah number (Wikidata Q1138045))" + }, + { + "q": "If the observation duration is 8.50728, what is the stress relaxation time?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Deborah number is not given" + }, + { + "q": "Could you work out the stress relaxation time given observation duration = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Deborah number is not given" + }, + { + "q": "Do you know the stress relaxation time of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the stress relaxation time of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the stress relaxation time given observation duration = -0.318734, Deborah number = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative observation duration: no example had one" + }, + { + "q": "What is a typical stress relaxation time?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-stress-relaxation-time/program.py b/root/organs10/physics/specialists/solved-stress-relaxation-time/program.py new file mode 100644 index 0000000000000000000000000000000000000000..2c4586e846a1a2359f5a4dce01581a3ccbb89819 --- /dev/null +++ b/root/organs10/physics/specialists/solved-stress-relaxation-time/program.py @@ -0,0 +1,7 @@ +"""stress relaxation time from observation duration, Deborah number (Deborah number (Wikidata Q1138045)) +The maths organ (systems.py): solved from the system q2 = ((q0)/(q1)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(observation_duration, deborah_number): + return 1.0*deborah_number*observation_duration diff --git a/root/organs10/physics/specialists/solved-stress-relaxation-time/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-stress-relaxation-time/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-stress-relaxation-time/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-strouhal-number/organ_formula_v2.py b/root/organs10/physics/specialists/solved-strouhal-number/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-strouhal-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-strouhal-number/panel.json b/root/organs10/physics/specialists/solved-strouhal-number/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..4b04943d9c4f344b8e9ff413c0873d56e153cf6d --- /dev/null +++ b/root/organs10/physics/specialists/solved-strouhal-number/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the Strouhal number for frequency 107.961, characteristic length 59.4105 and speed 5.77717?", + "a": "1110.2327161569876", + "valid": true, + "kind": "compute", + "expect": 1110.2327161569876, + "dtype": "quantity", + "unit": "1", + "why": "Strouhal number for [107.960666, 59.4105, 5.777165] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "Tell me the Strouhal number with frequency of 107.961, characteristic length of 59.4105 and speed of 5.77717.", + "a": "1110.2327161569876", + "valid": true, + "kind": "compute", + "expect": 1110.2327161569876, + "dtype": "quantity", + "unit": "1", + "why": "Strouhal number for [107.960666, 59.4105, 5.777165] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "What is the Strouhal number for frequency 85.2416, characteristic length 160.878 and speed 2.69428?", + "a": "5089.871888725082", + "valid": true, + "kind": "compute", + "expect": 5089.871888725082, + "dtype": "quantity", + "unit": "1", + "why": "Strouhal number for [85.241623, 160.878442, 2.69428] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "Tell me the Strouhal number with frequency of 85.2416, characteristic length of 160.878 and speed of 2.69428.", + "a": "5089.871888725082", + "valid": true, + "kind": "compute", + "expect": 5089.871888725082, + "dtype": "quantity", + "unit": "1", + "why": "Strouhal number for [85.241623, 160.878442, 2.69428] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "What is the Strouhal number for frequency 45.6157, characteristic length 4.97131 and speed 94.305?", + "a": "2.404638268801381", + "valid": true, + "kind": "compute", + "expect": 2.404638268801381, + "dtype": "quantity", + "unit": "1", + "why": "Strouhal number for [45.61567, 4.971307, 94.305046] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "Tell me the Strouhal number with frequency of 45.6157, characteristic length of 4.97131 and speed of 94.305.", + "a": "2.404638268801381", + "valid": true, + "kind": "compute", + "expect": 2.404638268801381, + "dtype": "quantity", + "unit": "1", + "why": "Strouhal number for [45.61567, 4.971307, 94.305046] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "What is the Strouhal number for frequency 10.5638, characteristic length 198.839 and speed 0.671697?", + "a": "3127.1582727254026", + "valid": true, + "kind": "compute", + "expect": 3127.1582727254026, + "dtype": "quantity", + "unit": "1", + "why": "Strouhal number for [10.563843, 198.838955, 0.671697] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "Tell me the Strouhal number with frequency of 10.5638, characteristic length of 198.839 and speed of 0.671697.", + "a": "3127.1582727254026", + "valid": true, + "kind": "compute", + "expect": 3127.1582727254026, + "dtype": "quantity", + "unit": "1", + "why": "Strouhal number for [10.563843, 198.838955, 0.671697] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "What is the Strouhal number for frequency 21.7931, characteristic length 29.5106 and speed 8.50341?", + "a": "75.6318512050043", + "valid": true, + "kind": "compute", + "expect": 75.6318512050043, + "dtype": "quantity", + "unit": "1", + "why": "Strouhal number for [21.793126, 29.510609, 8.503407] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "Tell me the Strouhal number with frequency of 21.7931, characteristic length of 29.5106 and speed of 8.50341.", + "a": "75.6318512050043", + "valid": true, + "kind": "compute", + "expect": 75.6318512050043, + "dtype": "quantity", + "unit": "1", + "why": "Strouhal number for [21.793126, 29.510609, 8.503407] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "What is the Strouhal number for frequency 223.509, characteristic length 17.3414 and speed 7.10705?", + "a": "545.3688543410761", + "valid": true, + "kind": "compute", + "expect": 545.3688543410761, + "dtype": "quantity", + "unit": "1", + "why": "Strouhal number for [223.508796, 17.341435, 7.107049] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "Tell me the Strouhal number with frequency of 223.509, characteristic length of 17.3414 and speed of 7.10705.", + "a": "545.3688543410761", + "valid": true, + "kind": "compute", + "expect": 545.3688543410761, + "dtype": "quantity", + "unit": "1", + "why": "Strouhal number for [223.508796, 17.341435, 7.107049] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "What is the Strouhal number for frequency 107.961 and characteristic length 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Tell me the Strouhal number with frequency of 85.2416 and characteristic length of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "What is the Strouhal 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 Strouhal 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 Strouhal number with frequency of -45.6157, characteristic length of 4.97131 and speed of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative frequency: no example had one" + }, + { + "q": "Which Strouhal 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 frequency is 17.7888, characteristic length is 167.625 and the speed is 10.3291, what is the Strouhal number?", + "a": "288.6849919329577", + "valid": true, + "kind": "compute", + "expect": 288.6849919329577, + "dtype": "quantity", + "unit": "1", + "why": "Strouhal number for [17.788778, 167.624948, 10.329054] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "Could you work out the Strouhal number given frequency = 17.7888, characteristic length = 167.625, speed = 10.3291?", + "a": "288.6849919329577", + "valid": true, + "kind": "compute", + "expect": 288.6849919329577, + "dtype": "quantity", + "unit": "1", + "why": "Strouhal number for [17.788778, 167.624948, 10.329054] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "I need the Strouhal number for a frequency of 21.511, a characteristic length of 173.394 and a speed of 251.084, please.", + "a": "14.855079375871984", + "valid": true, + "kind": "compute", + "expect": 14.855079375871984, + "dtype": "quantity", + "unit": "1", + "why": "Strouhal number for [21.511016, 173.393638, 251.084037] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "If the frequency is 21.511, characteristic length is 173.394 and the speed is 251.084, what is the Strouhal number?", + "a": "14.855079375871984", + "valid": true, + "kind": "compute", + "expect": 14.855079375871984, + "dtype": "quantity", + "unit": "1", + "why": "Strouhal number for [21.511016, 173.393638, 251.084037] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "Could you work out the Strouhal number given frequency = 129.245, characteristic length = 1.18359, speed = 15.9416?", + "a": "9.59583449816029", + "valid": true, + "kind": "compute", + "expect": 9.59583449816029, + "dtype": "quantity", + "unit": "1", + "why": "Strouhal number for [129.244984, 1.18359, 15.941612] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "I need the Strouhal number for a frequency of 129.245, a characteristic length of 1.18359 and a speed of 15.9416, please.", + "a": "9.59583449816029", + "valid": true, + "kind": "compute", + "expect": 9.59583449816029, + "dtype": "quantity", + "unit": "1", + "why": "Strouhal number for [129.244984, 1.18359, 15.941612] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "If the frequency is 20.3966, characteristic length is 18.3169 and the speed is 6.8386, what is the Strouhal number?", + "a": "54.63123238944198", + "valid": true, + "kind": "compute", + "expect": 54.63123238944198, + "dtype": "quantity", + "unit": "1", + "why": "Strouhal number for [20.396558, 18.316867, 6.838598] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "Could you work out the Strouhal number given frequency = 20.3966, characteristic length = 18.3169, speed = 6.8386?", + "a": "54.63123238944198", + "valid": true, + "kind": "compute", + "expect": 54.63123238944198, + "dtype": "quantity", + "unit": "1", + "why": "Strouhal number for [20.396558, 18.316867, 6.838598] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "I need the Strouhal number for a frequency of 185.886, a characteristic length of 13.2373 and a speed of 4.71941, please.", + "a": "521.3854934623429", + "valid": true, + "kind": "compute", + "expect": 521.3854934623429, + "dtype": "quantity", + "unit": "1", + "why": "Strouhal number for [185.886273, 13.237311, 4.719415] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "If the frequency is 185.886, characteristic length is 13.2373 and the speed is 4.71941, what is the Strouhal number?", + "a": "521.3854934623429", + "valid": true, + "kind": "compute", + "expect": 521.3854934623429, + "dtype": "quantity", + "unit": "1", + "why": "Strouhal number for [185.886273, 13.237311, 4.719415] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "Could you work out the Strouhal number given frequency = 9.1061, characteristic length = 24.5721, speed = 108.451?", + "a": "2.0632027126358947", + "valid": true, + "kind": "compute", + "expect": 2.0632027126358947, + "dtype": "quantity", + "unit": "1", + "why": "Strouhal number for [9.106104, 24.572086, 108.450792] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "I need the Strouhal number for a frequency of 9.1061, a characteristic length of 24.5721 and a speed of 108.451, please.", + "a": "2.0632027126358947", + "valid": true, + "kind": "compute", + "expect": 2.0632027126358947, + "dtype": "quantity", + "unit": "1", + "why": "Strouhal number for [9.106104, 24.572086, 108.450792] (the system Strouhal number (Wikidata Q646627))" + }, + { + "q": "If the frequency is 17.7888 and the characteristic length is 167.625, what is the Strouhal number?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Could you work out the Strouhal number given frequency = 21.511, characteristic length = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the speed is not given" + }, + { + "q": "Do you know the Strouhal 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 Strouhal 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 Strouhal number given frequency = -129.245, characteristic length = 1.18359, speed = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative frequency: no example had one" + }, + { + "q": "What is a typical Strouhal 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-strouhal-number/program.py b/root/organs10/physics/specialists/solved-strouhal-number/program.py new file mode 100644 index 0000000000000000000000000000000000000000..92704fd1403a0a7da7ae4c3b2000de3bfde25238 --- /dev/null +++ b/root/organs10/physics/specialists/solved-strouhal-number/program.py @@ -0,0 +1,7 @@ +"""Strouhal number from frequency, characteristic length, speed (Strouhal number (Wikidata Q646627)) +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(frequency, characteristic_length, speed): + return 1.0*characteristic_length*frequency/speed diff --git a/root/organs10/physics/specialists/solved-strouhal-number/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-strouhal-number/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-strouhal-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-superconducting-coherence-len/organ_formula_v2.py b/root/organs10/physics/specialists/solved-superconducting-coherence-len/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-superconducting-coherence-len/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-superconducting-coherence-len/panel.json b/root/organs10/physics/specialists/solved-superconducting-coherence-len/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..89f46504d767d3e4067b881c425c9c7a5b479fa1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-superconducting-coherence-len/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the superconducting coherence length for London penetration depth 107.961 and Landau–Ginzburg number 59.4105?", + "a": "1.2849533113937956", + "valid": true, + "kind": "compute", + "expect": 1.2849533113937956, + "dtype": "quantity", + "unit": "m", + "why": "superconducting coherence length for [107.960666, 59.4105] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Tell me the superconducting coherence length with London penetration depth of 107.961 and Landau–Ginzburg number of 59.4105.", + "a": "1.2849533113937956", + "valid": true, + "kind": "compute", + "expect": 1.2849533113937956, + "dtype": "quantity", + "unit": "m", + "why": "superconducting coherence length for [107.960666, 59.4105] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "What is the superconducting coherence length for London penetration depth 169.527 and Landau–Ginzburg number 280.774?", + "a": "0.42694048780569277", + "valid": true, + "kind": "compute", + "expect": 0.42694048780569277, + "dtype": "quantity", + "unit": "m", + "why": "superconducting coherence length for [169.526958, 280.773702] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Tell me the superconducting coherence length with London penetration depth of 169.527 and Landau–Ginzburg number of 280.774.", + "a": "0.42694048780569277", + "valid": true, + "kind": "compute", + "expect": 0.42694048780569277, + "dtype": "quantity", + "unit": "m", + "why": "superconducting coherence length for [169.526958, 280.773702] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "What is the superconducting coherence length for London penetration depth 1.91195 and Landau–Ginzburg number 82.2401?", + "a": "0.01643911570864928", + "valid": true, + "kind": "compute", + "expect": 0.01643911570864928, + "dtype": "quantity", + "unit": "m", + "why": "superconducting coherence length for [1.911952, 82.240072] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Tell me the superconducting coherence length with London penetration depth of 1.91195 and Landau–Ginzburg number of 82.2401.", + "a": "0.01643911570864928", + "valid": true, + "kind": "compute", + "expect": 0.01643911570864928, + "dtype": "quantity", + "unit": "m", + "why": "superconducting coherence length for [1.911952, 82.240072] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "What is the superconducting coherence length for London penetration depth 1.64379 and Landau–Ginzburg number 252.709?", + "a": "0.00459951497479437", + "valid": true, + "kind": "compute", + "expect": 0.00459951497479437, + "dtype": "quantity", + "unit": "m", + "why": "superconducting coherence length for [1.643795, 252.709024] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Tell me the superconducting coherence length with London penetration depth of 1.64379 and Landau–Ginzburg number of 252.709.", + "a": "0.00459951497479437", + "valid": true, + "kind": "compute", + "expect": 0.00459951497479437, + "dtype": "quantity", + "unit": "m", + "why": "superconducting coherence length for [1.643795, 252.709024] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "What is the superconducting coherence length for London penetration depth 18.3169 and Landau–Ginzburg number 6.8386?", + "a": "1.893952644323225", + "valid": true, + "kind": "compute", + "expect": 1.893952644323225, + "dtype": "quantity", + "unit": "m", + "why": "superconducting coherence length for [18.316867, 6.838598] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Tell me the superconducting coherence length with London penetration depth of 18.3169 and Landau–Ginzburg number of 6.8386.", + "a": "1.893952644323225", + "valid": true, + "kind": "compute", + "expect": 1.893952644323225, + "dtype": "quantity", + "unit": "m", + "why": "superconducting coherence length for [18.316867, 6.838598] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "What is the superconducting coherence length for London penetration depth 156.502 and Landau–Ginzburg number 185.886?", + "a": "0.5953290111278429", + "valid": true, + "kind": "compute", + "expect": 0.5953290111278429, + "dtype": "quantity", + "unit": "m", + "why": "superconducting coherence length for [156.50181, 185.886273] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Tell me the superconducting coherence length with London penetration depth of 156.502 and Landau–Ginzburg number of 185.886.", + "a": "0.5953290111278429", + "valid": true, + "kind": "compute", + "expect": 0.5953290111278429, + "dtype": "quantity", + "unit": "m", + "why": "superconducting coherence length for [156.50181, 185.886273] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "What is the superconducting coherence length for London penetration depth 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Landau–Ginzburg number is not given" + }, + { + "q": "Tell me the superconducting coherence length with London penetration depth of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Landau–Ginzburg number is not given" + }, + { + "q": "What is the superconducting coherence length of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the superconducting coherence length of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the superconducting coherence length with London penetration depth of -1.91195 and Landau–Ginzburg number of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative London penetration depth: no example had one" + }, + { + "q": "Which superconducting coherence length is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the London penetration depth is 8.50728 and the Landau–Ginzburg number is 17.7888, what is the superconducting coherence length?", + "a": "0.338165848773796", + "valid": true, + "kind": "compute", + "expect": 0.338165848773796, + "dtype": "quantity", + "unit": "m", + "why": "superconducting coherence length for [8.507283, 17.788778] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Could you work out the superconducting coherence length given London penetration depth = 8.50728, Landau–Ginzburg number = 17.7888?", + "a": "0.338165848773796", + "valid": true, + "kind": "compute", + "expect": 0.338165848773796, + "dtype": "quantity", + "unit": "m", + "why": "superconducting coherence length for [8.507283, 17.788778] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "I need the superconducting coherence length for a London penetration depth of 8.24951 and a Landau–Ginzburg number of 0.634021, please.", + "a": "9.200459635498946", + "valid": true, + "kind": "compute", + "expect": 9.200459635498946, + "dtype": "quantity", + "unit": "m", + "why": "superconducting coherence length for [8.24951, 0.634021] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "If the London penetration depth is 8.24951 and the Landau–Ginzburg number is 0.634021, what is the superconducting coherence length?", + "a": "9.200459635498946", + "valid": true, + "kind": "compute", + "expect": 9.200459635498946, + "dtype": "quantity", + "unit": "m", + "why": "superconducting coherence length for [8.24951, 0.634021] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Could you work out the superconducting coherence length given London penetration depth = 0.318734, Landau–Ginzburg number = 9.56607?", + "a": "0.023560248168360776", + "valid": true, + "kind": "compute", + "expect": 0.023560248168360776, + "dtype": "quantity", + "unit": "m", + "why": "superconducting coherence length for [0.318734, 9.566071] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "I need the superconducting coherence length for a London penetration depth of 0.318734 and a Landau–Ginzburg number of 9.56607, please.", + "a": "0.023560248168360776", + "valid": true, + "kind": "compute", + "expect": 0.023560248168360776, + "dtype": "quantity", + "unit": "m", + "why": "superconducting coherence length for [0.318734, 9.566071] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "If the London penetration depth is 0.671697 and the Landau–Ginzburg number is 14.2496, what is the superconducting coherence length?", + "a": "0.03333164914305671", + "valid": true, + "kind": "compute", + "expect": 0.03333164914305671, + "dtype": "quantity", + "unit": "m", + "why": "superconducting coherence length for [0.671697, 14.249569] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Could you work out the superconducting coherence length given London penetration depth = 0.671697, Landau–Ginzburg number = 14.2496?", + "a": "0.03333164914305671", + "valid": true, + "kind": "compute", + "expect": 0.03333164914305671, + "dtype": "quantity", + "unit": "m", + "why": "superconducting coherence length for [0.671697, 14.249569] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "I need the superconducting coherence length for a London penetration depth of 21.7931 and a Landau–Ginzburg number of 29.5106, please.", + "a": "0.5221873836473414", + "valid": true, + "kind": "compute", + "expect": 0.5221873836473414, + "dtype": "quantity", + "unit": "m", + "why": "superconducting coherence length for [21.793126, 29.510609] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "If the London penetration depth is 21.7931 and the Landau–Ginzburg number is 29.5106, what is the superconducting coherence length?", + "a": "0.5221873836473414", + "valid": true, + "kind": "compute", + "expect": 0.5221873836473414, + "dtype": "quantity", + "unit": "m", + "why": "superconducting coherence length for [21.793126, 29.510609] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "Could you work out the superconducting coherence length given London penetration depth = 153.15, Landau–Ginzburg number = 18.5953?", + "a": "5.823691405334237", + "valid": true, + "kind": "compute", + "expect": 5.823691405334237, + "dtype": "quantity", + "unit": "m", + "why": "superconducting coherence length for [153.149965, 18.595315] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "I need the superconducting coherence length for a London penetration depth of 153.15 and a Landau–Ginzburg number of 18.5953, please.", + "a": "5.823691405334237", + "valid": true, + "kind": "compute", + "expect": 5.823691405334237, + "dtype": "quantity", + "unit": "m", + "why": "superconducting coherence length for [153.149965, 18.595315] (the system Landau–Ginzburg number (Wikidata Q105421034))" + }, + { + "q": "If the London penetration depth is 8.50728, what is the superconducting coherence length?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Landau–Ginzburg number is not given" + }, + { + "q": "Could you work out the superconducting coherence length given London penetration depth = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the Landau–Ginzburg number is not given" + }, + { + "q": "Do you know the superconducting coherence length of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the superconducting coherence length of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the superconducting coherence length given London penetration depth = -0.318734, Landau–Ginzburg number = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative London penetration depth: no example had one" + }, + { + "q": "What is a typical superconducting coherence length?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-superconducting-coherence-len/program.py b/root/organs10/physics/specialists/solved-superconducting-coherence-len/program.py new file mode 100644 index 0000000000000000000000000000000000000000..4163756e254798ecb0791e589433e4220c4afec1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-superconducting-coherence-len/program.py @@ -0,0 +1,7 @@ +"""superconducting coherence length from London penetration depth, Landau–Ginzburg number (Landau–Ginzburg number (Wikidata Q105421034)) +The maths organ (systems.py): solved from the system q1 = ((q0)/(q2*sqrt(2))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(london_penetration_depth, landau_ginzburg_number): + return 0.707106781186548*london_penetration_depth/landau_ginzburg_number diff --git a/root/organs10/physics/specialists/solved-superconducting-coherence-len/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-superconducting-coherence-len/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-superconducting-coherence-len/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-surface-activity-density/organ_formula_v2.py b/root/organs10/physics/specialists/solved-surface-activity-density/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-surface-activity-density/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-surface-activity-density/panel.json b/root/organs10/physics/specialists/solved-surface-activity-density/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..aa08151d30de0cfd3d97045fe56079df4161f57e --- /dev/null +++ b/root/organs10/physics/specialists/solved-surface-activity-density/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the surface-activity density for activity 107.961 and surface area 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1/m^2/s", + "why": "surface-activity density for [107.960666, 59.4105] (the system surface-activity density (Wikidata Q98103005))" + }, + { + "q": "Tell me the surface-activity density with activity of 107.961 and surface area of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1/m^2/s", + "why": "surface-activity density for [107.960666, 59.4105] (the system surface-activity density (Wikidata Q98103005))" + }, + { + "q": "What is the surface-activity density for activity 169.527 and surface area 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1/m^2/s", + "why": "surface-activity density for [169.526958, 280.773702] (the system surface-activity density (Wikidata Q98103005))" + }, + { + "q": "Tell me the surface-activity density with activity of 169.527 and surface area of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1/m^2/s", + "why": "surface-activity density for [169.526958, 280.773702] (the system surface-activity density (Wikidata Q98103005))" + }, + { + "q": "What is the surface-activity density for activity 1.91195 and surface area 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1/m^2/s", + "why": "surface-activity density for [1.911952, 82.240072] (the system surface-activity density (Wikidata Q98103005))" + }, + { + "q": "Tell me the surface-activity density with activity of 1.91195 and surface area of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1/m^2/s", + "why": "surface-activity density for [1.911952, 82.240072] (the system surface-activity density (Wikidata Q98103005))" + }, + { + "q": "What is the surface-activity density for activity 1.64379 and surface area 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1/m^2/s", + "why": "surface-activity density for [1.643795, 252.709024] (the system surface-activity density (Wikidata Q98103005))" + }, + { + "q": "Tell me the surface-activity density with activity of 1.64379 and surface area of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1/m^2/s", + "why": "surface-activity density for [1.643795, 252.709024] (the system surface-activity density (Wikidata Q98103005))" + }, + { + "q": "What is the surface-activity density for activity 18.3169 and surface area 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1/m^2/s", + "why": "surface-activity density for [18.316867, 6.838598] (the system surface-activity density (Wikidata Q98103005))" + }, + { + "q": "Tell me the surface-activity density with activity of 18.3169 and surface area of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1/m^2/s", + "why": "surface-activity density for [18.316867, 6.838598] (the system surface-activity density (Wikidata Q98103005))" + }, + { + "q": "What is the surface-activity density for activity 156.502 and surface area 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1/m^2/s", + "why": "surface-activity density for [156.50181, 185.886273] (the system surface-activity density (Wikidata Q98103005))" + }, + { + "q": "Tell me the surface-activity density with activity of 156.502 and surface area of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1/m^2/s", + "why": "surface-activity density for [156.50181, 185.886273] (the system surface-activity density (Wikidata Q98103005))" + }, + { + "q": "What is the surface-activity density for activity 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the surface area is not given" + }, + { + "q": "Tell me the surface-activity density with activity of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the surface area is not given" + }, + { + "q": "What is the surface-activity density of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the surface-activity density of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the surface-activity density with activity of -1.91195 and surface area of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative activity: no example had one" + }, + { + "q": "Which surface-activity density is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the activity is 8.50728 and the surface area is 17.7888, what is the surface-activity density?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1/m^2/s", + "why": "surface-activity density for [8.507283, 17.788778] (the system surface-activity density (Wikidata Q98103005))" + }, + { + "q": "Could you work out the surface-activity density given activity = 8.50728, surface area = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1/m^2/s", + "why": "surface-activity density for [8.507283, 17.788778] (the system surface-activity density (Wikidata Q98103005))" + }, + { + "q": "I need the surface-activity density for an activity of 8.24951 and a surface area of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1/m^2/s", + "why": "surface-activity density for [8.24951, 0.634021] (the system surface-activity density (Wikidata Q98103005))" + }, + { + "q": "If the activity is 8.24951 and the surface area is 0.634021, what is the surface-activity density?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1/m^2/s", + "why": "surface-activity density for [8.24951, 0.634021] (the system surface-activity density (Wikidata Q98103005))" + }, + { + "q": "Could you work out the surface-activity density given activity = 0.318734, surface area = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1/m^2/s", + "why": "surface-activity density for [0.318734, 9.566071] (the system surface-activity density (Wikidata Q98103005))" + }, + { + "q": "I need the surface-activity density for an activity of 0.318734 and a surface area of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1/m^2/s", + "why": "surface-activity density for [0.318734, 9.566071] (the system surface-activity density (Wikidata Q98103005))" + }, + { + "q": "If the activity is 0.671697 and the surface area is 14.2496, what is the surface-activity density?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1/m^2/s", + "why": "surface-activity density for [0.671697, 14.249569] (the system surface-activity density (Wikidata Q98103005))" + }, + { + "q": "Could you work out the surface-activity density given activity = 0.671697, surface area = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1/m^2/s", + "why": "surface-activity density for [0.671697, 14.249569] (the system surface-activity density (Wikidata Q98103005))" + }, + { + "q": "I need the surface-activity density for an activity of 21.7931 and a surface area of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1/m^2/s", + "why": "surface-activity density for [21.793126, 29.510609] (the system surface-activity density (Wikidata Q98103005))" + }, + { + "q": "If the activity is 21.7931 and the surface area is 29.5106, what is the surface-activity density?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1/m^2/s", + "why": "surface-activity density for [21.793126, 29.510609] (the system surface-activity density (Wikidata Q98103005))" + }, + { + "q": "Could you work out the surface-activity density given activity = 153.15, surface area = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1/m^2/s", + "why": "surface-activity density for [153.149965, 18.595315] (the system surface-activity density (Wikidata Q98103005))" + }, + { + "q": "I need the surface-activity density for an activity of 153.15 and a surface area of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1/m^2/s", + "why": "surface-activity density for [153.149965, 18.595315] (the system surface-activity density (Wikidata Q98103005))" + }, + { + "q": "If the activity is 8.50728, what is the surface-activity density?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the surface area is not given" + }, + { + "q": "Could you work out the surface-activity density given activity = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the surface area is not given" + }, + { + "q": "Do you know the surface-activity density of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the surface-activity density of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the surface-activity density given activity = -0.318734, surface area = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative activity: no example had one" + }, + { + "q": "What is a typical surface-activity density?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-surface-activity-density/program.py b/root/organs10/physics/specialists/solved-surface-activity-density/program.py new file mode 100644 index 0000000000000000000000000000000000000000..637d7068ee35630290eaf7d522554dadaca3caa1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-surface-activity-density/program.py @@ -0,0 +1,7 @@ +"""surface-activity density from activity, surface area (surface-activity density (Wikidata Q98103005)) +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(activity, surface_area): + return 1.0*activity/surface_area diff --git a/root/organs10/physics/specialists/solved-surface-activity-density/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-surface-activity-density/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-surface-activity-density/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-surface-area-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-surface-area-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-surface-area-2/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-surface-area-2/panel.json b/root/organs10/physics/specialists/solved-surface-area-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..4e1b57f81da461f2b86167609ca3082053371a05 --- /dev/null +++ b/root/organs10/physics/specialists/solved-surface-area-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the surface area for radius 107.961?", + "a": "146467.4002952481", + "valid": true, + "kind": "compute", + "expect": 146467.4002952481, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [107.960666] (the system Sphere)" + }, + { + "q": "Tell me the surface area with radius of 107.961.", + "a": "146467.4002952481", + "valid": true, + "kind": "compute", + "expect": 146467.4002952481, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [107.960666] (the system Sphere)" + }, + { + "q": "What is the surface area for radius 6.34623?", + "a": "506.1052900063624", + "valid": true, + "kind": "compute", + "expect": 506.1052900063624, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [6.346226] (the system Sphere)" + }, + { + "q": "Tell me the surface area with radius of 6.34623.", + "a": "506.1052900063624", + "valid": true, + "kind": "compute", + "expect": 506.1052900063624, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [6.346226] (the system Sphere)" + }, + { + "q": "What is the surface area for radius 0.551236?", + "a": "3.8184294889386496", + "valid": true, + "kind": "compute", + "expect": 3.8184294889386496, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [0.551236] (the system Sphere)" + }, + { + "q": "Tell me the surface area with radius of 0.551236.", + "a": "3.8184294889386496", + "valid": true, + "kind": "compute", + "expect": 3.8184294889386496, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [0.551236] (the system Sphere)" + }, + { + "q": "What is the surface area for radius 186.27?", + "a": "436009.50772619486", + "valid": true, + "kind": "compute", + "expect": 436009.50772619486, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [186.270057] (the system Sphere)" + }, + { + "q": "Tell me the surface area with radius of 186.27.", + "a": "436009.50772619486", + "valid": true, + "kind": "compute", + "expect": 436009.50772619486, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [186.270057] (the system Sphere)" + }, + { + "q": "What is the surface area for radius 0.710236?", + "a": "6.338925144428232", + "valid": true, + "kind": "compute", + "expect": 6.338925144428232, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [0.710236] (the system Sphere)" + }, + { + "q": "Tell me the surface area with radius of 0.710236.", + "a": "6.338925144428232", + "valid": true, + "kind": "compute", + "expect": 6.338925144428232, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [0.710236] (the system Sphere)" + }, + { + "q": "What is the surface area for radius 79.8435?", + "a": "80110.49332355862", + "valid": true, + "kind": "compute", + "expect": 80110.49332355862, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [79.843538] (the system Sphere)" + }, + { + "q": "Tell me the surface area with radius of 79.8435.", + "a": "80110.49332355862", + "valid": true, + "kind": "compute", + "expect": 80110.49332355862, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [79.843538] (the system Sphere)" + }, + { + "q": "What is the surface area?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radius is not given" + }, + { + "q": "Could you work out the surface area, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radius is not given" + }, + { + "q": "What is the surface area of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the surface area of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the surface area with radius of -0.551236.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative radius: no example had one" + }, + { + "q": "Which surface area is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the radius is 58.5346, what is the surface area?", + "a": "43056.13964359941", + "valid": true, + "kind": "compute", + "expect": 43056.13964359941, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [58.534594] (the system Sphere)" + }, + { + "q": "Could you work out the surface area given radius = 58.5346?", + "a": "43056.13964359941", + "valid": true, + "kind": "compute", + "expect": 43056.13964359941, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [58.534594] (the system Sphere)" + }, + { + "q": "I need the surface area for a radius of 31.9492, please.", + "a": "12827.117452652034", + "valid": true, + "kind": "compute", + "expect": 12827.117452652034, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [31.949172] (the system Sphere)" + }, + { + "q": "If the radius is 31.9492, what is the surface area?", + "a": "12827.117452652034", + "valid": true, + "kind": "compute", + "expect": 12827.117452652034, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [31.949172] (the system Sphere)" + }, + { + "q": "Could you work out the surface area given radius = 6.8386?", + "a": "587.684209326863", + "valid": true, + "kind": "compute", + "expect": 587.684209326863, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [6.838598] (the system Sphere)" + }, + { + "q": "I need the surface area for a radius of 6.8386, please.", + "a": "587.684209326863", + "valid": true, + "kind": "compute", + "expect": 587.684209326863, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [6.838598] (the system Sphere)" + }, + { + "q": "If the radius is 17.3414, what is the surface area?", + "a": "3779.0264227881607", + "valid": true, + "kind": "compute", + "expect": 3779.0264227881607, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [17.341435] (the system Sphere)" + }, + { + "q": "Could you work out the surface area given radius = 17.3414?", + "a": "3779.0264227881607", + "valid": true, + "kind": "compute", + "expect": 3779.0264227881607, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [17.341435] (the system Sphere)" + }, + { + "q": "I need the surface area for a radius of 0.380269, please.", + "a": "1.817149387308368", + "valid": true, + "kind": "compute", + "expect": 1.817149387308368, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [0.380269] (the system Sphere)" + }, + { + "q": "If the radius is 0.380269, what is the surface area?", + "a": "1.817149387308368", + "valid": true, + "kind": "compute", + "expect": 1.817149387308368, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [0.380269] (the system Sphere)" + }, + { + "q": "Could you work out the surface area given radius = 291.545?", + "a": "1068121.7444137558", + "valid": true, + "kind": "compute", + "expect": 1068121.7444137558, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [291.544898] (the system Sphere)" + }, + { + "q": "I need the surface area for a radius of 291.545, please.", + "a": "1068121.7444137558", + "valid": true, + "kind": "compute", + "expect": 1068121.7444137558, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [291.544898] (the system Sphere)" + }, + { + "q": "I need the surface area, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radius is not given" + }, + { + "q": "Can you tell me the surface area?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the radius is not given" + }, + { + "q": "Do you know the surface area of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the surface area of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the surface area given radius = -6.8386?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative radius: no example had one" + }, + { + "q": "What is a typical surface area?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-surface-area-2/program.py b/root/organs10/physics/specialists/solved-surface-area-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..7917bf018715357f8547fe381f8d9a78fe15ccf3 --- /dev/null +++ b/root/organs10/physics/specialists/solved-surface-area-2/program.py @@ -0,0 +1,7 @@ +"""surface area from radius (Sphere) +The maths organ (systems.py): solved from the system V = 4*q*r**3/3; S = 4*q*r**2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(radius): + return 12.5663706143592*radius**2 diff --git a/root/organs10/physics/specialists/solved-surface-area-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-surface-area-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-surface-area-2/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-surface-area/organ_formula_v2.py b/root/organs10/physics/specialists/solved-surface-area/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-surface-area/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-surface-area/panel.json b/root/organs10/physics/specialists/solved-surface-area/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..935a4624d19692ea3198b5b8eab3a96af8146357 --- /dev/null +++ b/root/organs10/physics/specialists/solved-surface-area/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the surface area for volume 107.961?", + "a": "109.64642955587746", + "valid": true, + "kind": "compute", + "expect": 109.64642955587746, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [107.960666] (the system Sphere)" + }, + { + "q": "Tell me the surface area with volume of 107.961.", + "a": "109.64642955587746", + "valid": true, + "kind": "compute", + "expect": 109.64642955587746, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [107.960666] (the system Sphere)" + }, + { + "q": "What is the surface area for volume 6.34623?", + "a": "16.576561989868924", + "valid": true, + "kind": "compute", + "expect": 16.576561989868924, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [6.346226] (the system Sphere)" + }, + { + "q": "Tell me the surface area with volume of 6.34623.", + "a": "16.576561989868924", + "valid": true, + "kind": "compute", + "expect": 16.576561989868924, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [6.346226] (the system Sphere)" + }, + { + "q": "What is the surface area for volume 0.551236?", + "a": "3.2511905823516716", + "valid": true, + "kind": "compute", + "expect": 3.2511905823516716, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [0.551236] (the system Sphere)" + }, + { + "q": "Tell me the surface area with volume of 0.551236.", + "a": "3.2511905823516716", + "valid": true, + "kind": "compute", + "expect": 3.2511905823516716, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [0.551236] (the system Sphere)" + }, + { + "q": "What is the surface area for volume 186.27?", + "a": "157.72946238572652", + "valid": true, + "kind": "compute", + "expect": 157.72946238572652, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [186.270057] (the system Sphere)" + }, + { + "q": "Tell me the surface area with volume of 186.27.", + "a": "157.72946238572652", + "valid": true, + "kind": "compute", + "expect": 157.72946238572652, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [186.270057] (the system Sphere)" + }, + { + "q": "What is the surface area for volume 0.710236?", + "a": "3.849633434484217", + "valid": true, + "kind": "compute", + "expect": 3.849633434484217, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [0.710236] (the system Sphere)" + }, + { + "q": "Tell me the surface area with volume of 0.710236.", + "a": "3.849633434484217", + "valid": true, + "kind": "compute", + "expect": 3.849633434484217, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [0.710236] (the system Sphere)" + }, + { + "q": "What is the surface area for volume 79.8435?", + "a": "89.66933972371315", + "valid": true, + "kind": "compute", + "expect": 89.66933972371315, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [79.843538] (the system Sphere)" + }, + { + "q": "Tell me the surface area with volume of 79.8435.", + "a": "89.66933972371315", + "valid": true, + "kind": "compute", + "expect": 89.66933972371315, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [79.843538] (the system Sphere)" + }, + { + "q": "What is the surface area?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the volume is not given" + }, + { + "q": "Could you work out the surface area, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the volume is not given" + }, + { + "q": "What is the surface area of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the surface area of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the surface area with volume of -0.551236.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative volume: no example had one" + }, + { + "q": "Which surface area is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the volume is 58.5346, what is the surface area?", + "a": "72.90531646415037", + "valid": true, + "kind": "compute", + "expect": 72.90531646415037, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [58.534594] (the system Sphere)" + }, + { + "q": "Could you work out the surface area given volume = 58.5346?", + "a": "72.90531646415037", + "valid": true, + "kind": "compute", + "expect": 72.90531646415037, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [58.534594] (the system Sphere)" + }, + { + "q": "I need the surface area for a volume of 31.9492, please.", + "a": "48.69195294124468", + "valid": true, + "kind": "compute", + "expect": 48.69195294124468, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [31.949172] (the system Sphere)" + }, + { + "q": "If the volume is 31.9492, what is the surface area?", + "a": "48.69195294124468", + "valid": true, + "kind": "compute", + "expect": 48.69195294124468, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [31.949172] (the system Sphere)" + }, + { + "q": "Could you work out the surface area given volume = 6.8386?", + "a": "17.423236981572963", + "valid": true, + "kind": "compute", + "expect": 17.423236981572963, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [6.838598] (the system Sphere)" + }, + { + "q": "I need the surface area for a volume of 6.8386, please.", + "a": "17.423236981572963", + "valid": true, + "kind": "compute", + "expect": 17.423236981572963, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [6.838598] (the system Sphere)" + }, + { + "q": "If the volume is 17.3414, what is the surface area?", + "a": "32.399686847648276", + "valid": true, + "kind": "compute", + "expect": 32.399686847648276, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [17.341435] (the system Sphere)" + }, + { + "q": "Could you work out the surface area given volume = 17.3414?", + "a": "32.399686847648276", + "valid": true, + "kind": "compute", + "expect": 32.399686847648276, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [17.341435] (the system Sphere)" + }, + { + "q": "I need the surface area for a volume of 0.380269, please.", + "a": "2.53830836200458", + "valid": true, + "kind": "compute", + "expect": 2.53830836200458, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [0.380269] (the system Sphere)" + }, + { + "q": "If the volume is 0.380269, what is the surface area?", + "a": "2.53830836200458", + "valid": true, + "kind": "compute", + "expect": 2.53830836200458, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [0.380269] (the system Sphere)" + }, + { + "q": "Could you work out the surface area given volume = 291.545?", + "a": "212.62831060343734", + "valid": true, + "kind": "compute", + "expect": 212.62831060343734, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [291.544898] (the system Sphere)" + }, + { + "q": "I need the surface area for a volume of 291.545, please.", + "a": "212.62831060343734", + "valid": true, + "kind": "compute", + "expect": 212.62831060343734, + "dtype": "quantity", + "unit": "m^2", + "why": "surface area for [291.544898] (the system Sphere)" + }, + { + "q": "I need the surface area, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the volume is not given" + }, + { + "q": "Can you tell me the surface area?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the volume is not given" + }, + { + "q": "Do you know the surface area of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the surface area of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the surface area given volume = -6.8386?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative volume: no example had one" + }, + { + "q": "What is a typical surface area?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-surface-area/program.py b/root/organs10/physics/specialists/solved-surface-area/program.py new file mode 100644 index 0000000000000000000000000000000000000000..680ac7b9c6843f7f2f01324685c55bc0a7001b90 --- /dev/null +++ b/root/organs10/physics/specialists/solved-surface-area/program.py @@ -0,0 +1,7 @@ +"""surface area from volume (Sphere) +The maths organ (systems.py): solved from the system V = 4*q*r**3/3; S = 4*q*r**2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(volume): + return 4.83597586204942*volume**0.666666666666667 diff --git a/root/organs10/physics/specialists/solved-surface-area/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-surface-area/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-surface-area/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?