diff --git a/root/organs10/physics/specialists/solved-distance-9/exam_log.jsonl b/root/organs10/physics/specialists/solved-distance-9/exam_log.jsonl new file mode 100644 index 0000000000000000000000000000000000000000..d7f79479b17d1a2fd43420945dc5ed33589d1442 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-9/exam_log.jsonl @@ -0,0 +1 @@ +{"t": "2026-10-24T00:36:30Z", "heldout_sha": "087a766fddac25f714904b405c10b2de0a93dfa17d5e4659a937bf49bfc09fe3", "files_sha": "cf7a443f588fbb4eedc24267740baf07942e8ee4b402c76ad9fb4a48907eb4a0"} diff --git a/root/organs10/physics/specialists/solved-distance-9/manifest.json b/root/organs10/physics/specialists/solved-distance-9/manifest.json new file mode 100644 index 0000000000000000000000000000000000000000..694a2c9bcf5b2ddf3ac0d4cb0bac5ddaf27b2d7d --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-9/manifest.json @@ -0,0 +1,41 @@ +{ + "id": "solved-distance-9", + "name": "Solved: distance from speed, time (Average speed)", + "sources": [ + { + "name": "the maths organ: solved from the system v = d/t; checked by substitution into every equation and by a numeric solution at 12 points", + "licence": "yours" + } + ], + "organ": { + "module": "organ_formula_v3", + "class": "FormulaOrgan", + "init": {} + }, + "accept": { + "kinds": [ + "compute" + ], + "declines_in_domain": [ + "missing_input", + "out_of_domain" + ], + "context_regex": "\\bdistance\\b" + }, + "panel": { + "path": "panel.json", + "scorer_module": "scorer_formula_v1", + "scorer_fn": "ok" + }, + "budgets": { + "latency_p50_ms": 200 + }, + "notes": "Solved by the maths organ from the system Average speed: 6 checked points used to write the panel's dev half, 6 its held-out half.", + "built_from": { + "questions": [ + "What is the distance for speed and time?" + ], + "gap": "", + "node": "hod" + } +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-distance-9/narrow_v1.py b/root/organs10/physics/specialists/solved-distance-9/narrow_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..647d4d5e961acd006c9de30f0ce8b0b8f5139cda --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-9/narrow_v1.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 +"""narrow_v1 - the shared NARROWING rule for specialist organs (2026-09-29, from the full-core sweep): + + WORD PROBLEMS AND NARRATIVE TURNS BELONG TO THE HEAD. An organ answers a direct domain question. A turn that tells a + story ("Justine wants to put a deck ...", "A new energy drink advertises 106 calories for 8 ounces ...") or relates + quantities to each other ("the length is 9 inches more than the width") is a word problem, which the core's word- + problem organ owns; organs firing on its nouns declined it or answered a different question. + An organ may DECLINE only a short direct question (it is then refusing the domain question that was asked); on + anything longer it says nothing and the head answers. + +Narrowing only ever removes turns an organ takes, so on any input where it does not fire, the organ's reply is +byte-identical to the frozen version it wraps (checked per specialist by an invariance count before re-gating). +""" +import re + +RELATION = re.compile(r"\b(?:\d+(?:\.\d+)?|one|two|three|four|five|six|seven|eight|nine|ten)\s+(?:\w+\s+){0,2}(?:more|less|fewer|longer|shorter)\s+than\b|" + r"\b(?:twice|three times|half)\s+(?:as\s+\w+\s+as\s+)?the\s+\w+\b", re.I) + + +# a RATE ("106 calories for 8 ounces", "3 apples per 2 dollars") makes the turn a proportion problem +RATE = re.compile(r"\b\d+(?:\.\d+)?\s+[a-z]+\s+(?:for|per|every)\s+(?:\d+(?:\.\d+)?|a|an|one|each)\s+[a-z]+", re.I) +NUM = re.compile(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-distance-9/organ_formula_v2.py b/root/organs10/physics/specialists/solved-distance-9/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-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-distance-9/panel.json b/root/organs10/physics/specialists/solved-distance-9/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..2eb4cb380470c3e0ae398a7c2ebb00d47661d7c6 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-9/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the distance for speed 107.961 and time 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "km", + "why": "distance for [107.960666, 59.4105] (the system Average speed)" + }, + { + "q": "Tell me the distance with speed of 107.961 and time of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "km", + "why": "distance for [107.960666, 59.4105] (the system Average speed)" + }, + { + "q": "What is the distance for speed 169.527 and time 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "km", + "why": "distance for [169.526958, 280.773702] (the system Average speed)" + }, + { + "q": "Tell me the distance with speed of 169.527 and time of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "km", + "why": "distance for [169.526958, 280.773702] (the system Average speed)" + }, + { + "q": "What is the distance for speed 1.91195 and time 82.2401?", + "a": "157.23905034769405", + "valid": true, + "kind": "compute", + "expect": 157.23905034769405, + "dtype": "quantity", + "unit": "km", + "why": "distance for [1.911952, 82.240072] (the system Average speed)" + }, + { + "q": "Tell me the distance with speed of 1.91195 and time of 82.2401.", + "a": "157.23905034769405", + "valid": true, + "kind": "compute", + "expect": 157.23905034769405, + "dtype": "quantity", + "unit": "km", + "why": "distance for [1.911952, 82.240072] (the system Average speed)" + }, + { + "q": "What is the distance for speed 1.64379 and time 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "km", + "why": "distance for [1.643795, 252.709024] (the system Average speed)" + }, + { + "q": "Tell me the distance with speed of 1.64379 and time of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "km", + "why": "distance for [1.643795, 252.709024] (the system Average speed)" + }, + { + "q": "What is the distance for speed 18.3169 and time 6.8386?", + "a": "125.26169131331939", + "valid": true, + "kind": "compute", + "expect": 125.26169131331939, + "dtype": "quantity", + "unit": "km", + "why": "distance for [18.316867, 6.838598] (the system Average speed)" + }, + { + "q": "Tell me the distance with speed of 18.3169 and time of 6.8386.", + "a": "125.26169131331939", + "valid": true, + "kind": "compute", + "expect": 125.26169131331939, + "dtype": "quantity", + "unit": "km", + "why": "distance for [18.316867, 6.838598] (the system Average speed)" + }, + { + "q": "What is the distance for speed 156.502 and time 185.886?", + "a": "29091.538105177147", + "valid": true, + "kind": "compute", + "expect": 29091.538105177147, + "dtype": "quantity", + "unit": "km", + "why": "distance for [156.50181, 185.886273] (the system Average speed)" + }, + { + "q": "Tell me the distance with speed of 156.502 and time of 185.886.", + "a": "29091.538105177147", + "valid": true, + "kind": "compute", + "expect": 29091.538105177147, + "dtype": "quantity", + "unit": "km", + "why": "distance for [156.50181, 185.886273] (the system Average speed)" + }, + { + "q": "What is the distance for speed 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "Tell me the distance with speed of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "What is the distance of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the distance of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the distance with speed of -1.91195 and time of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative speed: no example had one" + }, + { + "q": "Which distance is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the speed is 8.50728 and the time is 17.7888, what is the distance?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "km", + "why": "distance for [8.507283, 17.788778] (the system Average speed)" + }, + { + "q": "Could you work out the distance given speed = 8.50728, time = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "km", + "why": "distance for [8.507283, 17.788778] (the system Average speed)" + }, + { + "q": "I need the distance for a speed of 8.24951 and a time of 0.634021, please.", + "a": "5.230362271972725", + "valid": true, + "kind": "compute", + "expect": 5.230362271972725, + "dtype": "quantity", + "unit": "km", + "why": "distance for [8.24951, 0.634021] (the system Average speed)" + }, + { + "q": "If the speed is 8.24951 and the time is 0.634021, what is the distance?", + "a": "5.230362271972725", + "valid": true, + "kind": "compute", + "expect": 5.230362271972725, + "dtype": "quantity", + "unit": "km", + "why": "distance for [8.24951, 0.634021] (the system Average speed)" + }, + { + "q": "Could you work out the distance given speed = 0.318734, time = 9.56607?", + "a": "3.049032526346269", + "valid": true, + "kind": "compute", + "expect": 3.049032526346269, + "dtype": "quantity", + "unit": "km", + "why": "distance for [0.318734, 9.566071] (the system Average speed)" + }, + { + "q": "I need the distance for a speed of 0.318734 and a time of 9.56607, please.", + "a": "3.049032526346269", + "valid": true, + "kind": "compute", + "expect": 3.049032526346269, + "dtype": "quantity", + "unit": "km", + "why": "distance for [0.318734, 9.566071] (the system Average speed)" + }, + { + "q": "If the speed is 0.671697 and the time is 14.2496, what is the distance?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "km", + "why": "distance for [0.671697, 14.249569] (the system Average speed)" + }, + { + "q": "Could you work out the distance given speed = 0.671697, time = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "km", + "why": "distance for [0.671697, 14.249569] (the system Average speed)" + }, + { + "q": "I need the distance for a speed of 21.7931 and a time of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "km", + "why": "distance for [21.793126, 29.510609] (the system Average speed)" + }, + { + "q": "If the speed is 21.7931 and the time is 29.5106, what is the distance?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "km", + "why": "distance for [21.793126, 29.510609] (the system Average speed)" + }, + { + "q": "Could you work out the distance given speed = 153.15, time = 18.5953?", + "a": "2847.8719160673327", + "valid": true, + "kind": "compute", + "expect": 2847.8719160673327, + "dtype": "quantity", + "unit": "km", + "why": "distance for [153.149965, 18.595315] (the system Average speed)" + }, + { + "q": "I need the distance for a speed of 153.15 and a time of 18.5953, please.", + "a": "2847.8719160673327", + "valid": true, + "kind": "compute", + "expect": 2847.8719160673327, + "dtype": "quantity", + "unit": "km", + "why": "distance for [153.149965, 18.595315] (the system Average speed)" + }, + { + "q": "If the speed is 8.50728, what is the distance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "Could you work out the distance given speed = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the time is not given" + }, + { + "q": "Do you know the distance of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the distance of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the distance given speed = -0.318734, time = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative speed: no example had one" + }, + { + "q": "What is a typical distance?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-distance-9/program.py b/root/organs10/physics/specialists/solved-distance-9/program.py new file mode 100644 index 0000000000000000000000000000000000000000..a7c01017693a789c97822367b391dae1b70b629b --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-9/program.py @@ -0,0 +1,7 @@ +"""distance from speed, 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(speed, time): + return 1.0*speed*time diff --git a/root/organs10/physics/specialists/solved-distance-9/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-distance-9/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-distance-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-dose-equivalent/organ_formula_v2.py b/root/organs10/physics/specialists/solved-dose-equivalent/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-dose-equivalent/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-dose-equivalent/panel.json b/root/organs10/physics/specialists/solved-dose-equivalent/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..83c76238b8ed668373026d7523e9d7c222812e6c --- /dev/null +++ b/root/organs10/physics/specialists/solved-dose-equivalent/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the dose equivalent for quality factor 107.961 and absorbed dose 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "dose equivalent for [107.960666, 59.4105] (the system dose equivalent (Wikidata Q256106))" + }, + { + "q": "Tell me the dose equivalent with quality factor of 107.961 and absorbed dose of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "dose equivalent for [107.960666, 59.4105] (the system dose equivalent (Wikidata Q256106))" + }, + { + "q": "What is the dose equivalent for quality factor 169.527 and absorbed dose 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "dose equivalent for [169.526958, 280.773702] (the system dose equivalent (Wikidata Q256106))" + }, + { + "q": "Tell me the dose equivalent with quality factor of 169.527 and absorbed dose of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "dose equivalent for [169.526958, 280.773702] (the system dose equivalent (Wikidata Q256106))" + }, + { + "q": "What is the dose equivalent for quality factor 1.91195 and absorbed dose 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "dose equivalent for [1.911952, 82.240072] (the system dose equivalent (Wikidata Q256106))" + }, + { + "q": "Tell me the dose equivalent with quality factor of 1.91195 and absorbed dose of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "dose equivalent for [1.911952, 82.240072] (the system dose equivalent (Wikidata Q256106))" + }, + { + "q": "What is the dose equivalent for quality factor 1.64379 and absorbed dose 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "dose equivalent for [1.643795, 252.709024] (the system dose equivalent (Wikidata Q256106))" + }, + { + "q": "Tell me the dose equivalent with quality factor of 1.64379 and absorbed dose of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "dose equivalent for [1.643795, 252.709024] (the system dose equivalent (Wikidata Q256106))" + }, + { + "q": "What is the dose equivalent for quality factor 18.3169 and absorbed dose 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "dose equivalent for [18.316867, 6.838598] (the system dose equivalent (Wikidata Q256106))" + }, + { + "q": "Tell me the dose equivalent with quality factor of 18.3169 and absorbed dose of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "dose equivalent for [18.316867, 6.838598] (the system dose equivalent (Wikidata Q256106))" + }, + { + "q": "What is the dose equivalent for quality factor 156.502 and absorbed dose 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "dose equivalent for [156.50181, 185.886273] (the system dose equivalent (Wikidata Q256106))" + }, + { + "q": "Tell me the dose equivalent with quality factor of 156.502 and absorbed dose of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "dose equivalent for [156.50181, 185.886273] (the system dose equivalent (Wikidata Q256106))" + }, + { + "q": "What is the dose equivalent for quality factor 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the absorbed dose is not given" + }, + { + "q": "Tell me the dose equivalent with quality factor of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the absorbed dose is not given" + }, + { + "q": "What is the dose equivalent 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 dose equivalent 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 dose equivalent with quality factor of -1.91195 and absorbed dose of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative quality factor: no example had one" + }, + { + "q": "Which dose equivalent is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the quality factor is 8.50728 and the absorbed dose is 17.7888, what is the dose equivalent?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "dose equivalent for [8.507283, 17.788778] (the system dose equivalent (Wikidata Q256106))" + }, + { + "q": "Could you work out the dose equivalent given quality factor = 8.50728, absorbed dose = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "dose equivalent for [8.507283, 17.788778] (the system dose equivalent (Wikidata Q256106))" + }, + { + "q": "I need the dose equivalent for a quality factor of 8.24951 and an absorbed dose of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "dose equivalent for [8.24951, 0.634021] (the system dose equivalent (Wikidata Q256106))" + }, + { + "q": "If the quality factor is 8.24951 and the absorbed dose is 0.634021, what is the dose equivalent?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "dose equivalent for [8.24951, 0.634021] (the system dose equivalent (Wikidata Q256106))" + }, + { + "q": "Could you work out the dose equivalent given quality factor = 0.318734, absorbed dose = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "dose equivalent for [0.318734, 9.566071] (the system dose equivalent (Wikidata Q256106))" + }, + { + "q": "I need the dose equivalent for a quality factor of 0.318734 and an absorbed dose of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "dose equivalent for [0.318734, 9.566071] (the system dose equivalent (Wikidata Q256106))" + }, + { + "q": "If the quality factor is 0.671697 and the absorbed dose is 14.2496, what is the dose equivalent?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "dose equivalent for [0.671697, 14.249569] (the system dose equivalent (Wikidata Q256106))" + }, + { + "q": "Could you work out the dose equivalent given quality factor = 0.671697, absorbed dose = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "dose equivalent for [0.671697, 14.249569] (the system dose equivalent (Wikidata Q256106))" + }, + { + "q": "I need the dose equivalent for a quality factor of 21.7931 and an absorbed dose of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "dose equivalent for [21.793126, 29.510609] (the system dose equivalent (Wikidata Q256106))" + }, + { + "q": "If the quality factor is 21.7931 and the absorbed dose is 29.5106, what is the dose equivalent?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "dose equivalent for [21.793126, 29.510609] (the system dose equivalent (Wikidata Q256106))" + }, + { + "q": "Could you work out the dose equivalent given quality factor = 153.15, absorbed dose = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "dose equivalent for [153.149965, 18.595315] (the system dose equivalent (Wikidata Q256106))" + }, + { + "q": "I need the dose equivalent for a quality factor of 153.15 and an absorbed dose of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "m^2/s^2", + "why": "dose equivalent for [153.149965, 18.595315] (the system dose equivalent (Wikidata Q256106))" + }, + { + "q": "If the quality factor is 8.50728, what is the dose equivalent?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the absorbed dose is not given" + }, + { + "q": "Could you work out the dose equivalent given quality factor = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the absorbed dose is not given" + }, + { + "q": "Do you know the dose equivalent 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 dose equivalent 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 dose equivalent given quality factor = -0.318734, absorbed dose = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative quality factor: no example had one" + }, + { + "q": "What is a typical dose equivalent?", + "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-dose-equivalent/program.py b/root/organs10/physics/specialists/solved-dose-equivalent/program.py new file mode 100644 index 0000000000000000000000000000000000000000..fb0f43766c0b0a4439dc7918725fa934828a9676 --- /dev/null +++ b/root/organs10/physics/specialists/solved-dose-equivalent/program.py @@ -0,0 +1,7 @@ +"""dose equivalent from quality factor, absorbed dose (dose equivalent (Wikidata Q256106)) +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(quality_factor, absorbed_dose): + return 1.0*absorbed_dose*quality_factor diff --git a/root/organs10/physics/specialists/solved-dose-equivalent/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-dose-equivalent/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-dose-equivalent/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-drift-velocity/organ_formula_v2.py b/root/organs10/physics/specialists/solved-drift-velocity/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-drift-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-drift-velocity/panel.json b/root/organs10/physics/specialists/solved-drift-velocity/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..1eeef5ce15244ddffbf9b62b7bb11f35dd8280ce --- /dev/null +++ b/root/organs10/physics/specialists/solved-drift-velocity/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the drift velocity for electric field strength 107.961 and mobility 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "m/s", + "why": "drift velocity for [107.960666, 59.4105] (the system mobility (Wikidata Q900648))" + }, + { + "q": "Tell me the drift velocity with electric field strength of 107.961 and mobility of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "m/s", + "why": "drift velocity for [107.960666, 59.4105] (the system mobility (Wikidata Q900648))" + }, + { + "q": "What is the drift velocity for electric field strength 169.527 and mobility 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "m/s", + "why": "drift velocity for [169.526958, 280.773702] (the system mobility (Wikidata Q900648))" + }, + { + "q": "Tell me the drift velocity with electric field strength of 169.527 and mobility of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "m/s", + "why": "drift velocity for [169.526958, 280.773702] (the system mobility (Wikidata Q900648))" + }, + { + "q": "What is the drift velocity for electric field strength 1.91195 and mobility 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "m/s", + "why": "drift velocity for [1.911952, 82.240072] (the system mobility (Wikidata Q900648))" + }, + { + "q": "Tell me the drift velocity with electric field strength of 1.91195 and mobility of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "m/s", + "why": "drift velocity for [1.911952, 82.240072] (the system mobility (Wikidata Q900648))" + }, + { + "q": "What is the drift velocity for electric field strength 1.64379 and mobility 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "m/s", + "why": "drift velocity for [1.643795, 252.709024] (the system mobility (Wikidata Q900648))" + }, + { + "q": "Tell me the drift velocity with electric field strength of 1.64379 and mobility of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "m/s", + "why": "drift velocity for [1.643795, 252.709024] (the system mobility (Wikidata Q900648))" + }, + { + "q": "What is the drift velocity for electric field strength 18.3169 and mobility 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "m/s", + "why": "drift velocity for [18.316867, 6.838598] (the system mobility (Wikidata Q900648))" + }, + { + "q": "Tell me the drift velocity with electric field strength of 18.3169 and mobility of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "m/s", + "why": "drift velocity for [18.316867, 6.838598] (the system mobility (Wikidata Q900648))" + }, + { + "q": "What is the drift velocity for electric field strength 156.502 and mobility 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "m/s", + "why": "drift velocity for [156.50181, 185.886273] (the system mobility (Wikidata Q900648))" + }, + { + "q": "Tell me the drift velocity with electric field strength of 156.502 and mobility of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "m/s", + "why": "drift velocity for [156.50181, 185.886273] (the system mobility (Wikidata Q900648))" + }, + { + "q": "What is the drift velocity for electric field strength 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mobility is not given" + }, + { + "q": "Tell me the drift velocity with electric field strength of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mobility is not given" + }, + { + "q": "What is the drift 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 drift 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 drift velocity with electric field strength of -1.91195 and mobility of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric field strength: no example had one" + }, + { + "q": "Which drift 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 electric field strength is 8.50728 and the mobility is 17.7888, what is the drift velocity?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "m/s", + "why": "drift velocity for [8.507283, 17.788778] (the system mobility (Wikidata Q900648))" + }, + { + "q": "Could you work out the drift velocity given electric field strength = 8.50728, mobility = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "m/s", + "why": "drift velocity for [8.507283, 17.788778] (the system mobility (Wikidata Q900648))" + }, + { + "q": "I need the drift velocity for an electric field strength of 8.24951 and a mobility of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "m/s", + "why": "drift velocity for [8.24951, 0.634021] (the system mobility (Wikidata Q900648))" + }, + { + "q": "If the electric field strength is 8.24951 and the mobility is 0.634021, what is the drift velocity?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "m/s", + "why": "drift velocity for [8.24951, 0.634021] (the system mobility (Wikidata Q900648))" + }, + { + "q": "Could you work out the drift velocity given electric field strength = 0.318734, mobility = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "m/s", + "why": "drift velocity for [0.318734, 9.566071] (the system mobility (Wikidata Q900648))" + }, + { + "q": "I need the drift velocity for an electric field strength of 0.318734 and a mobility of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "m/s", + "why": "drift velocity for [0.318734, 9.566071] (the system mobility (Wikidata Q900648))" + }, + { + "q": "If the electric field strength is 0.671697 and the mobility is 14.2496, what is the drift velocity?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "m/s", + "why": "drift velocity for [0.671697, 14.249569] (the system mobility (Wikidata Q900648))" + }, + { + "q": "Could you work out the drift velocity given electric field strength = 0.671697, mobility = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "m/s", + "why": "drift velocity for [0.671697, 14.249569] (the system mobility (Wikidata Q900648))" + }, + { + "q": "I need the drift velocity for an electric field strength of 21.7931 and a mobility of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "m/s", + "why": "drift velocity for [21.793126, 29.510609] (the system mobility (Wikidata Q900648))" + }, + { + "q": "If the electric field strength is 21.7931 and the mobility is 29.5106, what is the drift velocity?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "m/s", + "why": "drift velocity for [21.793126, 29.510609] (the system mobility (Wikidata Q900648))" + }, + { + "q": "Could you work out the drift velocity given electric field strength = 153.15, mobility = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "m/s", + "why": "drift velocity for [153.149965, 18.595315] (the system mobility (Wikidata Q900648))" + }, + { + "q": "I need the drift velocity for an electric field strength of 153.15 and a mobility of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "m/s", + "why": "drift velocity for [153.149965, 18.595315] (the system mobility (Wikidata Q900648))" + }, + { + "q": "If the electric field strength is 8.50728, what is the drift velocity?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mobility is not given" + }, + { + "q": "Could you work out the drift velocity given electric field strength = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mobility is not given" + }, + { + "q": "Do you know the drift 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 drift 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 drift velocity given electric field strength = -0.318734, mobility = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric field strength: no example had one" + }, + { + "q": "What is a typical drift 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-drift-velocity/program.py b/root/organs10/physics/specialists/solved-drift-velocity/program.py new file mode 100644 index 0000000000000000000000000000000000000000..ba0fe7ba6051d39e4d45b153592913ceb0044684 --- /dev/null +++ b/root/organs10/physics/specialists/solved-drift-velocity/program.py @@ -0,0 +1,7 @@ +"""drift velocity from electric field strength, mobility (mobility (Wikidata Q900648)) +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(electric_field_strength, mobility): + return 1.0*electric_field_strength*mobility diff --git a/root/organs10/physics/specialists/solved-drift-velocity/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-drift-velocity/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-drift-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-drop-height-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-drop-height-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-drop-height-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-drop-height-2/panel.json b/root/organs10/physics/specialists/solved-drop-height-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..55b9df34651ac2257c2ba2d64133b653944c6e81 --- /dev/null +++ b/root/organs10/physics/specialists/solved-drop-height-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the drop height for impact speed 107.961?", + "a": "594.2653902914861", + "valid": true, + "kind": "compute", + "expect": 594.2653902914861, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [107.960666] (the system Free fall)" + }, + { + "q": "Tell me the drop height with impact speed of 107.961.", + "a": "594.2653902914861", + "valid": true, + "kind": "compute", + "expect": 594.2653902914861, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [107.960666] (the system Free fall)" + }, + { + "q": "What is the drop height for impact speed 6.34623?", + "a": "2.0534320749050288", + "valid": true, + "kind": "compute", + "expect": 2.0534320749050288, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [6.346226] (the system Free fall)" + }, + { + "q": "Tell me the drop height with impact speed of 6.34623.", + "a": "2.0534320749050288", + "valid": true, + "kind": "compute", + "expect": 2.0534320749050288, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [6.346226] (the system Free fall)" + }, + { + "q": "What is the drop height for impact speed 0.551236?", + "a": "0.01549259757441237", + "valid": true, + "kind": "compute", + "expect": 0.01549259757441237, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [0.551236] (the system Free fall)" + }, + { + "q": "Tell me the drop height with impact speed of 0.551236.", + "a": "0.01549259757441237", + "valid": true, + "kind": "compute", + "expect": 0.01549259757441237, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [0.551236] (the system Free fall)" + }, + { + "q": "What is the drop height for impact speed 186.27?", + "a": "1769.0309226312675", + "valid": true, + "kind": "compute", + "expect": 1769.0309226312675, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [186.270057] (the system Free fall)" + }, + { + "q": "Tell me the drop height with impact speed of 186.27.", + "a": "1769.0309226312675", + "valid": true, + "kind": "compute", + "expect": 1769.0309226312675, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [186.270057] (the system Free fall)" + }, + { + "q": "What is the drop height for impact speed 0.710236?", + "a": "0.025719059786605446", + "valid": true, + "kind": "compute", + "expect": 0.025719059786605446, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [0.710236] (the system Free fall)" + }, + { + "q": "Tell me the drop height with impact speed of 0.710236.", + "a": "0.025719059786605446", + "valid": true, + "kind": "compute", + "expect": 0.025719059786605446, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [0.710236] (the system Free fall)" + }, + { + "q": "What is the drop height for impact speed 79.8435?", + "a": "325.03405867382344", + "valid": true, + "kind": "compute", + "expect": 325.03405867382344, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [79.843538] (the system Free fall)" + }, + { + "q": "Tell me the drop height with impact speed of 79.8435.", + "a": "325.03405867382344", + "valid": true, + "kind": "compute", + "expect": 325.03405867382344, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [79.843538] (the system Free fall)" + }, + { + "q": "What is the drop height?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the impact speed is not given" + }, + { + "q": "Could you work out the drop height, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the impact speed is not given" + }, + { + "q": "What is the drop height 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 drop height 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 drop height with impact speed of -0.551236.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative impact speed: no example had one" + }, + { + "q": "Which drop height 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 impact speed is 58.5346, what is the drop height?", + "a": "174.69261814008215", + "valid": true, + "kind": "compute", + "expect": 174.69261814008215, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [58.534594] (the system Free fall)" + }, + { + "q": "Could you work out the drop height given impact speed = 58.5346?", + "a": "174.69261814008215", + "valid": true, + "kind": "compute", + "expect": 174.69261814008215, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [58.534594] (the system Free fall)" + }, + { + "q": "I need the drop height for an impact speed of 31.9492, please.", + "a": "52.043744505256306", + "valid": true, + "kind": "compute", + "expect": 52.043744505256306, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [31.949172] (the system Free fall)" + }, + { + "q": "If the impact speed is 31.9492, what is the drop height?", + "a": "52.043744505256306", + "valid": true, + "kind": "compute", + "expect": 52.043744505256306, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [31.949172] (the system Free fall)" + }, + { + "q": "Could you work out the drop height given impact speed = 6.8386?", + "a": "2.3844240105291346", + "valid": true, + "kind": "compute", + "expect": 2.3844240105291346, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [6.838598] (the system Free fall)" + }, + { + "q": "I need the drop height for an impact speed of 6.8386, please.", + "a": "2.3844240105291346", + "valid": true, + "kind": "compute", + "expect": 2.3844240105291346, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [6.838598] (the system Free fall)" + }, + { + "q": "If the impact speed is 17.3414, what is the drop height?", + "a": "15.332726651344165", + "valid": true, + "kind": "compute", + "expect": 15.332726651344165, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [17.341435] (the system Free fall)" + }, + { + "q": "Could you work out the drop height given impact speed = 17.3414?", + "a": "15.332726651344165", + "valid": true, + "kind": "compute", + "expect": 15.332726651344165, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [17.341435] (the system Free fall)" + }, + { + "q": "I need the drop height for an impact speed of 0.380269, please.", + "a": "0.007372759997719279", + "valid": true, + "kind": "compute", + "expect": 0.007372759997719279, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [0.380269] (the system Free fall)" + }, + { + "q": "If the impact speed is 0.380269, what is the drop height?", + "a": "0.007372759997719279", + "valid": true, + "kind": "compute", + "expect": 0.007372759997719279, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [0.380269] (the system Free fall)" + }, + { + "q": "Could you work out the drop height given impact speed = 291.545?", + "a": "4333.713741374141", + "valid": true, + "kind": "compute", + "expect": 4333.713741374141, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [291.544898] (the system Free fall)" + }, + { + "q": "I need the drop height for an impact speed of 291.545, please.", + "a": "4333.713741374141", + "valid": true, + "kind": "compute", + "expect": 4333.713741374141, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [291.544898] (the system Free fall)" + }, + { + "q": "I need the drop height, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the impact speed is not given" + }, + { + "q": "Can you tell me the drop height?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the impact speed is not given" + }, + { + "q": "Do you know the drop height 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 drop height 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 drop height given impact speed = -6.8386?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative impact speed: no example had one" + }, + { + "q": "What is a typical drop height?", + "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-drop-height-2/program.py b/root/organs10/physics/specialists/solved-drop-height-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..c61de8cf6ac6c387ed4553c3a44cce7cc0e499f0 --- /dev/null +++ b/root/organs10/physics/specialists/solved-drop-height-2/program.py @@ -0,0 +1,7 @@ +"""drop height from impact speed (Free fall) +The maths organ (systems.py): solved from the system h = g*t**2/2; v = g*t; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(impact_speed): + return 0.0509858106488964*impact_speed**2 diff --git a/root/organs10/physics/specialists/solved-drop-height-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-drop-height-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-drop-height-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-drop-height/organ_formula_v2.py b/root/organs10/physics/specialists/solved-drop-height/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-drop-height/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-drop-height/panel.json b/root/organs10/physics/specialists/solved-drop-height/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..ca9354ff11874b20baab1b40cee6f00e65315e4a --- /dev/null +++ b/root/organs10/physics/specialists/solved-drop-height/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the drop height for fall time 107.961?", + "a": "57150.730914466156", + "valid": true, + "kind": "compute", + "expect": 57150.730914466156, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [107.960666] (the system Free fall)" + }, + { + "q": "Tell me the drop height with fall time of 107.961.", + "a": "57150.730914466156", + "valid": true, + "kind": "compute", + "expect": 57150.730914466156, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [107.960666] (the system Free fall)" + }, + { + "q": "What is the drop height for fall time 6.34623?", + "a": "197.47935161842204", + "valid": true, + "kind": "compute", + "expect": 197.47935161842204, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [6.346226] (the system Free fall)" + }, + { + "q": "Tell me the drop height with fall time of 6.34623.", + "a": "197.47935161842204", + "valid": true, + "kind": "compute", + "expect": 197.47935161842204, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [6.346226] (the system Free fall)" + }, + { + "q": "What is the drop height for fall time 0.551236?", + "a": "1.4899290613358094", + "valid": true, + "kind": "compute", + "expect": 1.4899290613358094, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [0.551236] (the system Free fall)" + }, + { + "q": "Tell me the drop height with fall time of 0.551236.", + "a": "1.4899290613358094", + "valid": true, + "kind": "compute", + "expect": 1.4899290613358094, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [0.551236] (the system Free fall)" + }, + { + "q": "What is the drop height for fall time 186.27?", + "a": "170128.38353093268", + "valid": true, + "kind": "compute", + "expect": 170128.38353093268, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [186.270057] (the system Free fall)" + }, + { + "q": "Tell me the drop height with fall time of 186.27.", + "a": "170128.38353093268", + "valid": true, + "kind": "compute", + "expect": 170128.38353093268, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [186.270057] (the system Free fall)" + }, + { + "q": "What is the drop height for fall time 0.710236?", + "a": "2.473411861519295", + "valid": true, + "kind": "compute", + "expect": 2.473411861519295, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [0.710236] (the system Free fall)" + }, + { + "q": "Tell me the drop height with fall time of 0.710236.", + "a": "2.473411861519295", + "valid": true, + "kind": "compute", + "expect": 2.473411861519295, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [0.710236] (the system Free fall)" + }, + { + "q": "What is the drop height for fall time 79.8435?", + "a": "31258.650308060212", + "valid": true, + "kind": "compute", + "expect": 31258.650308060212, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [79.843538] (the system Free fall)" + }, + { + "q": "Tell me the drop height with fall time of 79.8435.", + "a": "31258.650308060212", + "valid": true, + "kind": "compute", + "expect": 31258.650308060212, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [79.843538] (the system Free fall)" + }, + { + "q": "What is the drop height?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the fall time is not given" + }, + { + "q": "Could you work out the drop height, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the fall time is not given" + }, + { + "q": "What is the drop height 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 drop height 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 drop height with fall time of -0.551236.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative fall time: no example had one" + }, + { + "q": "Which drop height is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the fall time is 58.5346, what is the drop height?", + "a": "16800.256207366176", + "valid": true, + "kind": "compute", + "expect": 16800.256207366176, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [58.534594] (the system Free fall)" + }, + { + "q": "Could you work out the drop height given fall time = 58.5346?", + "a": "16800.256207366176", + "valid": true, + "kind": "compute", + "expect": 16800.256207366176, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [58.534594] (the system Free fall)" + }, + { + "q": "I need the drop height for a fall time of 31.9492, please.", + "a": "5005.066905448123", + "valid": true, + "kind": "compute", + "expect": 5005.066905448123, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [31.949172] (the system Free fall)" + }, + { + "q": "If the fall time is 31.9492, what is the drop height?", + "a": "5005.066905448123", + "valid": true, + "kind": "compute", + "expect": 5005.066905448123, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [31.949172] (the system Free fall)" + }, + { + "q": "Could you work out the drop height given fall time = 6.8386?", + "a": "229.3109732419413", + "valid": true, + "kind": "compute", + "expect": 229.3109732419413, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [6.838598] (the system Free fall)" + }, + { + "q": "I need the drop height for a fall time of 6.8386, please.", + "a": "229.3109732419413", + "valid": true, + "kind": "compute", + "expect": 229.3109732419413, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [6.838598] (the system Free fall)" + }, + { + "q": "If the fall time is 17.3414, what is the drop height?", + "a": "1474.5542132383343", + "valid": true, + "kind": "compute", + "expect": 1474.5542132383343, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [17.341435] (the system Free fall)" + }, + { + "q": "Could you work out the drop height given fall time = 17.3414?", + "a": "1474.5542132383343", + "valid": true, + "kind": "compute", + "expect": 1474.5542132383343, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [17.341435] (the system Free fall)" + }, + { + "q": "I need the drop height for a fall time of 0.380269, please.", + "a": "0.7090411617609413", + "valid": true, + "kind": "compute", + "expect": 0.7090411617609413, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [0.380269] (the system Free fall)" + }, + { + "q": "If the fall time is 0.380269, what is the drop height?", + "a": "0.7090411617609413", + "valid": true, + "kind": "compute", + "expect": 0.7090411617609413, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [0.380269] (the system Free fall)" + }, + { + "q": "Could you work out the drop height given fall time = 291.545?", + "a": "416774.9156182792", + "valid": true, + "kind": "compute", + "expect": 416774.9156182792, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [291.544898] (the system Free fall)" + }, + { + "q": "I need the drop height for a fall time of 291.545, please.", + "a": "416774.9156182792", + "valid": true, + "kind": "compute", + "expect": 416774.9156182792, + "dtype": "quantity", + "unit": "m", + "why": "drop height for [291.544898] (the system Free fall)" + }, + { + "q": "I need the drop height, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the fall time is not given" + }, + { + "q": "Can you tell me the drop height?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the fall time is not given" + }, + { + "q": "Do you know the drop height 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 drop height 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 drop height given fall time = -6.8386?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative fall time: no example had one" + }, + { + "q": "What is a typical drop height?", + "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-drop-height/program.py b/root/organs10/physics/specialists/solved-drop-height/program.py new file mode 100644 index 0000000000000000000000000000000000000000..498ede529a574ad5eba504b2eae9f7bcd9946773 --- /dev/null +++ b/root/organs10/physics/specialists/solved-drop-height/program.py @@ -0,0 +1,7 @@ +"""drop height from fall time (Free fall) +The maths organ (systems.py): solved from the system h = g*t**2/2; v = g*t; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(fall_time): + return 4.903325*fall_time**2 diff --git a/root/organs10/physics/specialists/solved-drop-height/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-drop-height/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-drop-height/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-duration-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-duration-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-duration-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-duration-2/panel.json b/root/organs10/physics/specialists/solved-duration-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..f7dfa4df52e4f2957e37da3cd8fb0346262ab4f2 --- /dev/null +++ b/root/organs10/physics/specialists/solved-duration-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the duration for Fourier number for mass transfer 107.961, mass diffusivity 59.4105 and length 5.77717?", + "a": "60.65014321820282", + "valid": true, + "kind": "compute", + "expect": 60.65014321820282, + "dtype": "quantity", + "unit": "s", + "why": "duration for [107.960666, 59.4105, 5.777165] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "Tell me the duration with Fourier number for mass transfer of 107.961, mass diffusivity of 59.4105 and length of 5.77717.", + "a": "60.65014321820282", + "valid": true, + "kind": "compute", + "expect": 60.65014321820282, + "dtype": "quantity", + "unit": "s", + "why": "duration for [107.960666, 59.4105, 5.777165] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "What is the duration for Fourier number for mass transfer 85.2416, mass diffusivity 160.878 and length 2.69428?", + "a": "3.846265616656069", + "valid": true, + "kind": "compute", + "expect": 3.846265616656069, + "dtype": "quantity", + "unit": "s", + "why": "duration for [85.241623, 160.878442, 2.69428] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "Tell me the duration with Fourier number for mass transfer of 85.2416, mass diffusivity of 160.878 and length of 2.69428.", + "a": "3.846265616656069", + "valid": true, + "kind": "compute", + "expect": 3.846265616656069, + "dtype": "quantity", + "unit": "s", + "why": "duration for [85.241623, 160.878442, 2.69428] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "What is the duration for Fourier number for mass transfer 45.6157, mass diffusivity 4.97131 and length 94.305?", + "a": "81604.34728741873", + "valid": true, + "kind": "compute", + "expect": 81604.34728741873, + "dtype": "quantity", + "unit": "s", + "why": "duration for [45.61567, 4.971307, 94.305046] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "Tell me the duration with Fourier number for mass transfer of 45.6157, mass diffusivity of 4.97131 and length of 94.305.", + "a": "81604.34728741873", + "valid": true, + "kind": "compute", + "expect": 81604.34728741873, + "dtype": "quantity", + "unit": "s", + "why": "duration for [45.61567, 4.971307, 94.305046] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "What is the duration for Fourier number for mass transfer 10.5638, mass diffusivity 198.839 and length 0.671697?", + "a": "0.02396997220671399", + "valid": true, + "kind": "compute", + "expect": 0.02396997220671399, + "dtype": "quantity", + "unit": "s", + "why": "duration for [10.563843, 198.838955, 0.671697] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "Tell me the duration with Fourier number for mass transfer of 10.5638, mass diffusivity of 198.839 and length of 0.671697.", + "a": "0.02396997220671399", + "valid": true, + "kind": "compute", + "expect": 0.02396997220671399, + "dtype": "quantity", + "unit": "s", + "why": "duration for [10.563843, 198.838955, 0.671697] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "What is the duration for Fourier number for mass transfer 21.7931, mass diffusivity 29.5106 and length 8.50341?", + "a": "53.398286934776955", + "valid": true, + "kind": "compute", + "expect": 53.398286934776955, + "dtype": "quantity", + "unit": "s", + "why": "duration for [21.793126, 29.510609, 8.503407] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "Tell me the duration with Fourier number for mass transfer of 21.7931, mass diffusivity of 29.5106 and length of 8.50341.", + "a": "53.398286934776955", + "valid": true, + "kind": "compute", + "expect": 53.398286934776955, + "dtype": "quantity", + "unit": "s", + "why": "duration for [21.793126, 29.510609, 8.503407] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "What is the duration for Fourier number for mass transfer 223.509, mass diffusivity 17.3414 and length 7.10705?", + "a": "651.0108507453602", + "valid": true, + "kind": "compute", + "expect": 651.0108507453602, + "dtype": "quantity", + "unit": "s", + "why": "duration for [223.508796, 17.341435, 7.107049] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "Tell me the duration with Fourier number for mass transfer of 223.509, mass diffusivity of 17.3414 and length of 7.10705.", + "a": "651.0108507453602", + "valid": true, + "kind": "compute", + "expect": 651.0108507453602, + "dtype": "quantity", + "unit": "s", + "why": "duration for [223.508796, 17.341435, 7.107049] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "What is the duration for Fourier number for mass transfer 107.961 and mass diffusivity 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the length is not given" + }, + { + "q": "Tell me the duration with Fourier number for mass transfer of 85.2416 and mass diffusivity of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the length is not given" + }, + { + "q": "What is the duration 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 duration 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 duration with Fourier number for mass transfer of -45.6157, mass diffusivity of 4.97131 and length of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Fourier number for mass transfer: no example had one" + }, + { + "q": "Which duration is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the Fourier number for mass transfer is 17.7888, mass diffusivity is 167.625 and the length is 10.3291, what is the duration?", + "a": "11.322142006979506", + "valid": true, + "kind": "compute", + "expect": 11.322142006979506, + "dtype": "quantity", + "unit": "s", + "why": "duration for [17.788778, 167.624948, 10.329054] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "Could you work out the duration given Fourier number for mass transfer = 17.7888, mass diffusivity = 167.625, length = 10.3291?", + "a": "11.322142006979506", + "valid": true, + "kind": "compute", + "expect": 11.322142006979506, + "dtype": "quantity", + "unit": "s", + "why": "duration for [17.788778, 167.624948, 10.329054] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "I need the duration for a Fourier number for mass transfer of 21.511, a mass diffusivity of 173.394 and a length of 251.084, please.", + "a": "7821.066296015693", + "valid": true, + "kind": "compute", + "expect": 7821.066296015693, + "dtype": "quantity", + "unit": "s", + "why": "duration for [21.511016, 173.393638, 251.084037] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "If the Fourier number for mass transfer is 21.511, mass diffusivity is 173.394 and the length is 251.084, what is the duration?", + "a": "7821.066296015693", + "valid": true, + "kind": "compute", + "expect": 7821.066296015693, + "dtype": "quantity", + "unit": "s", + "why": "duration for [21.511016, 173.393638, 251.084037] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "Could you work out the duration given Fourier number for mass transfer = 129.245, mass diffusivity = 1.18359, length = 15.9416?", + "a": "27750.886778450953", + "valid": true, + "kind": "compute", + "expect": 27750.886778450953, + "dtype": "quantity", + "unit": "s", + "why": "duration for [129.244984, 1.18359, 15.941612] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "I need the duration for a Fourier number for mass transfer of 129.245, a mass diffusivity of 1.18359 and a length of 15.9416, please.", + "a": "27750.886778450953", + "valid": true, + "kind": "compute", + "expect": 27750.886778450953, + "dtype": "quantity", + "unit": "s", + "why": "duration for [129.244984, 1.18359, 15.941612] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "If the Fourier number for mass transfer is 20.3966, mass diffusivity is 18.3169 and the length is 6.8386, what is the duration?", + "a": "52.07626748057164", + "valid": true, + "kind": "compute", + "expect": 52.07626748057164, + "dtype": "quantity", + "unit": "s", + "why": "duration for [20.396558, 18.316867, 6.838598] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "Could you work out the duration given Fourier number for mass transfer = 20.3966, mass diffusivity = 18.3169, length = 6.8386?", + "a": "52.07626748057164", + "valid": true, + "kind": "compute", + "expect": 52.07626748057164, + "dtype": "quantity", + "unit": "s", + "why": "duration for [20.396558, 18.316867, 6.838598] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "I need the duration for a Fourier number for mass transfer of 185.886, a mass diffusivity of 13.2373 and a length of 4.71941, please.", + "a": "312.76910403880635", + "valid": true, + "kind": "compute", + "expect": 312.76910403880635, + "dtype": "quantity", + "unit": "s", + "why": "duration for [185.886273, 13.237311, 4.719415] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "If the Fourier number for mass transfer is 185.886, mass diffusivity is 13.2373 and the length is 4.71941, what is the duration?", + "a": "312.76910403880635", + "valid": true, + "kind": "compute", + "expect": 312.76910403880635, + "dtype": "quantity", + "unit": "s", + "why": "duration for [185.886273, 13.237311, 4.719415] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "Could you work out the duration given Fourier number for mass transfer = 9.1061, mass diffusivity = 24.5721, length = 108.451?", + "a": "4358.690568080156", + "valid": true, + "kind": "compute", + "expect": 4358.690568080156, + "dtype": "quantity", + "unit": "s", + "why": "duration for [9.106104, 24.572086, 108.450792] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "I need the duration for a Fourier number for mass transfer of 9.1061, a mass diffusivity of 24.5721 and a length of 108.451, please.", + "a": "4358.690568080156", + "valid": true, + "kind": "compute", + "expect": 4358.690568080156, + "dtype": "quantity", + "unit": "s", + "why": "duration for [9.106104, 24.572086, 108.450792] (the system Fourier number for mass transfer (Wikidata Q104542186))" + }, + { + "q": "If the Fourier number for mass transfer is 17.7888 and the mass diffusivity is 167.625, what is the duration?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the length is not given" + }, + { + "q": "Could you work out the duration given Fourier number for mass transfer = 21.511, mass diffusivity = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the length is not given" + }, + { + "q": "Do you know the duration 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 duration 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 duration given Fourier number for mass transfer = -129.245, mass diffusivity = 1.18359, length = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Fourier number for mass transfer: no example had one" + }, + { + "q": "What is a typical duration?", + "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-duration-2/program.py b/root/organs10/physics/specialists/solved-duration-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..98cd61437f798429c9934e990f152255a646ba52 --- /dev/null +++ b/root/organs10/physics/specialists/solved-duration-2/program.py @@ -0,0 +1,7 @@ +"""duration from Fourier number for mass transfer, mass diffusivity, length (Fourier number for mass transfer (Wikidata Q104542186)) +The maths organ (systems.py): solved from the system q0 = ((q1*q2)/(q3 **(2))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(fourier_number_for_mass_transfer, mass_diffusivity, length): + return 1.0*fourier_number_for_mass_transfer*length**2/mass_diffusivity diff --git a/root/organs10/physics/specialists/solved-duration-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-duration-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-duration-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-duration/organ_formula_v2.py b/root/organs10/physics/specialists/solved-duration/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-duration/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-duration/panel.json b/root/organs10/physics/specialists/solved-duration/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..c46bb3f8971449e857b6357195b61b52f790f5e1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-duration/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the duration for speed 107.961 and distance 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "s", + "why": "duration for [107.960666, 59.4105] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Tell me the duration with speed of 107.961 and distance of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "s", + "why": "duration for [107.960666, 59.4105] (the system speed (Wikidata Q3711325))" + }, + { + "q": "What is the duration for speed 169.527 and distance 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "s", + "why": "duration for [169.526958, 280.773702] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Tell me the duration with speed of 169.527 and distance of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "s", + "why": "duration for [169.526958, 280.773702] (the system speed (Wikidata Q3711325))" + }, + { + "q": "What is the duration for speed 1.91195 and distance 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "s", + "why": "duration for [1.911952, 82.240072] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Tell me the duration with speed of 1.91195 and distance of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "s", + "why": "duration for [1.911952, 82.240072] (the system speed (Wikidata Q3711325))" + }, + { + "q": "What is the duration for speed 1.64379 and distance 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "s", + "why": "duration for [1.643795, 252.709024] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Tell me the duration with speed of 1.64379 and distance of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "s", + "why": "duration for [1.643795, 252.709024] (the system speed (Wikidata Q3711325))" + }, + { + "q": "What is the duration for speed 18.3169 and distance 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "s", + "why": "duration for [18.316867, 6.838598] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Tell me the duration with speed of 18.3169 and distance of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "s", + "why": "duration for [18.316867, 6.838598] (the system speed (Wikidata Q3711325))" + }, + { + "q": "What is the duration for speed 156.502 and distance 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "s", + "why": "duration for [156.50181, 185.886273] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Tell me the duration with speed of 156.502 and distance of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "s", + "why": "duration for [156.50181, 185.886273] (the system speed (Wikidata Q3711325))" + }, + { + "q": "What is the duration 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 duration 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 duration 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 duration 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 duration 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 duration 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 duration?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "s", + "why": "duration for [8.507283, 17.788778] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Could you work out the duration given speed = 8.50728, distance = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "s", + "why": "duration for [8.507283, 17.788778] (the system speed (Wikidata Q3711325))" + }, + { + "q": "I need the duration for a speed of 8.24951 and a distance of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "s", + "why": "duration for [8.24951, 0.634021] (the system speed (Wikidata Q3711325))" + }, + { + "q": "If the speed is 8.24951 and the distance is 0.634021, what is the duration?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "s", + "why": "duration for [8.24951, 0.634021] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Could you work out the duration given speed = 0.318734, distance = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "s", + "why": "duration for [0.318734, 9.566071] (the system speed (Wikidata Q3711325))" + }, + { + "q": "I need the duration for a speed of 0.318734 and a distance of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "s", + "why": "duration for [0.318734, 9.566071] (the system speed (Wikidata Q3711325))" + }, + { + "q": "If the speed is 0.671697 and the distance is 14.2496, what is the duration?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "s", + "why": "duration for [0.671697, 14.249569] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Could you work out the duration given speed = 0.671697, distance = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "s", + "why": "duration for [0.671697, 14.249569] (the system speed (Wikidata Q3711325))" + }, + { + "q": "I need the duration for a speed of 21.7931 and a distance of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "s", + "why": "duration for [21.793126, 29.510609] (the system speed (Wikidata Q3711325))" + }, + { + "q": "If the speed is 21.7931 and the distance is 29.5106, what is the duration?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "s", + "why": "duration for [21.793126, 29.510609] (the system speed (Wikidata Q3711325))" + }, + { + "q": "Could you work out the duration given speed = 153.15, distance = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "s", + "why": "duration for [153.149965, 18.595315] (the system speed (Wikidata Q3711325))" + }, + { + "q": "I need the duration for a speed of 153.15 and a distance of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "s", + "why": "duration for [153.149965, 18.595315] (the system speed (Wikidata Q3711325))" + }, + { + "q": "If the speed is 8.50728, what is the duration?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the distance is not given" + }, + { + "q": "Could you work out the duration 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 duration 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 duration 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 duration 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 duration?", + "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-duration/program.py b/root/organs10/physics/specialists/solved-duration/program.py new file mode 100644 index 0000000000000000000000000000000000000000..1530b0f99eb8f09a8e802e147ad8378a39dd93d6 --- /dev/null +++ b/root/organs10/physics/specialists/solved-duration/program.py @@ -0,0 +1,7 @@ +"""duration from speed, distance (speed (Wikidata Q3711325)) +The maths organ (systems.py): solved from the system q0 = q1 / q2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(speed, distance): + return 1.0*distance/speed diff --git a/root/organs10/physics/specialists/solved-duration/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-duration/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-duration/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-dynamic-viscosity-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-dynamic-viscosity-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-dynamic-viscosity-2/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-dynamic-viscosity-2/panel.json b/root/organs10/physics/specialists/solved-dynamic-viscosity-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..e40e98430aecde50d8f2e02d469e33126553312d --- /dev/null +++ b/root/organs10/physics/specialists/solved-dynamic-viscosity-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the dynamic viscosity for kinematic viscosity 107.961 and mass density 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [107.960666, 59.4105] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "Tell me the dynamic viscosity with kinematic viscosity of 107.961 and mass density of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [107.960666, 59.4105] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "What is the dynamic viscosity for kinematic viscosity 169.527 and mass density 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [169.526958, 280.773702] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "Tell me the dynamic viscosity with kinematic viscosity of 169.527 and mass density of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [169.526958, 280.773702] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "What is the dynamic viscosity for kinematic viscosity 1.91195 and mass density 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [1.911952, 82.240072] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "Tell me the dynamic viscosity with kinematic viscosity of 1.91195 and mass density of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [1.911952, 82.240072] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "What is the dynamic viscosity for kinematic viscosity 1.64379 and mass density 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [1.643795, 252.709024] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "Tell me the dynamic viscosity with kinematic viscosity of 1.64379 and mass density of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [1.643795, 252.709024] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "What is the dynamic viscosity for kinematic viscosity 18.3169 and mass density 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [18.316867, 6.838598] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "Tell me the dynamic viscosity with kinematic viscosity of 18.3169 and mass density of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [18.316867, 6.838598] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "What is the dynamic viscosity for kinematic viscosity 156.502 and mass density 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [156.50181, 185.886273] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "Tell me the dynamic viscosity with kinematic viscosity of 156.502 and mass density of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [156.50181, 185.886273] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "What is the dynamic viscosity for kinematic viscosity 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Tell me the dynamic viscosity with kinematic viscosity of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "What is the dynamic viscosity of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the dynamic viscosity of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the dynamic viscosity with kinematic viscosity of -1.91195 and mass density of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative kinematic viscosity: no example had one" + }, + { + "q": "Which dynamic viscosity is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the kinematic viscosity is 8.50728 and the mass density is 17.7888, what is the dynamic viscosity?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [8.507283, 17.788778] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "Could you work out the dynamic viscosity given kinematic viscosity = 8.50728, mass density = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [8.507283, 17.788778] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "I need the dynamic viscosity for a kinematic viscosity of 8.24951 and a mass density of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [8.24951, 0.634021] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "If the kinematic viscosity is 8.24951 and the mass density is 0.634021, what is the dynamic viscosity?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [8.24951, 0.634021] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "Could you work out the dynamic viscosity given kinematic viscosity = 0.318734, mass density = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [0.318734, 9.566071] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "I need the dynamic viscosity for a kinematic viscosity of 0.318734 and a mass density of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [0.318734, 9.566071] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "If the kinematic viscosity is 0.671697 and the mass density is 14.2496, what is the dynamic viscosity?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [0.671697, 14.249569] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "Could you work out the dynamic viscosity given kinematic viscosity = 0.671697, mass density = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [0.671697, 14.249569] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "I need the dynamic viscosity for a kinematic viscosity of 21.7931 and a mass density of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [21.793126, 29.510609] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "If the kinematic viscosity is 21.7931 and the mass density is 29.5106, what is the dynamic viscosity?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [21.793126, 29.510609] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "Could you work out the dynamic viscosity given kinematic viscosity = 153.15, mass density = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [153.149965, 18.595315] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "I need the dynamic viscosity for a kinematic viscosity of 153.15 and a mass density of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [153.149965, 18.595315] (the system kinematic viscosity (Wikidata Q12793663))" + }, + { + "q": "If the kinematic viscosity is 8.50728, what is the dynamic viscosity?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Could you work out the dynamic viscosity given kinematic viscosity = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Do you know the dynamic viscosity of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the dynamic viscosity of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the dynamic viscosity given kinematic viscosity = -0.318734, mass density = 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 dynamic viscosity?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-dynamic-viscosity-2/program.py b/root/organs10/physics/specialists/solved-dynamic-viscosity-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..212340bae8481e5eeb6295101a21885cf3d19689 --- /dev/null +++ b/root/organs10/physics/specialists/solved-dynamic-viscosity-2/program.py @@ -0,0 +1,7 @@ +"""dynamic viscosity from kinematic viscosity, mass density (kinematic viscosity (Wikidata Q12793663)) +The maths organ (systems.py): solved from the system q0 = q1 / q2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(kinematic_viscosity, mass_density): + return 1.0*kinematic_viscosity*mass_density diff --git a/root/organs10/physics/specialists/solved-dynamic-viscosity-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-dynamic-viscosity-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-dynamic-viscosity-2/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-dynamic-viscosity-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-dynamic-viscosity-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-dynamic-viscosity-3/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-dynamic-viscosity-3/panel.json b/root/organs10/physics/specialists/solved-dynamic-viscosity-3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..c7e356bc55299d1d9734ac2640976f776a640011 --- /dev/null +++ b/root/organs10/physics/specialists/solved-dynamic-viscosity-3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the dynamic viscosity for Hartmann number 107.961, magnetic flux density 59.4105, characteristic length 5.77717 and electrical conductivity 1.89126?", + "a": "19.115032658371504", + "valid": true, + "kind": "compute", + "expect": 19.115032658371504, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [107.960666, 59.4105, 5.777165, 1.891256] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "Tell me the dynamic viscosity with Hartmann number of 107.961, magnetic flux density of 59.4105, characteristic length of 5.77717 and electrical conductivity of 1.89126.", + "a": "19.115032658371504", + "valid": true, + "kind": "compute", + "expect": 19.115032658371504, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [107.960666, 59.4105, 5.777165, 1.891256] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "What is the dynamic viscosity for Hartmann number 2.69428, magnetic flux density 48.921, characteristic length 157.223 and electrical conductivity 35.6412?", + "a": "290460176.6823202", + "valid": true, + "kind": "compute", + "expect": 290460176.6823202, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [2.69428, 48.920991, 157.22256, 35.641157] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "Tell me the dynamic viscosity with Hartmann number of 2.69428, magnetic flux density of 48.921, characteristic length of 157.223 and electrical conductivity of 35.6412.", + "a": "290460176.6823202", + "valid": true, + "kind": "compute", + "expect": 290460176.6823202, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [2.69428, 48.920991, 157.22256, 35.641157] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "What is the dynamic viscosity for Hartmann number 0.318734, magnetic flux density 9.56607, characteristic length 126.709 and electrical conductivity 1.70503?", + "a": "24658097.55910176", + "valid": true, + "kind": "compute", + "expect": 24658097.55910176, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [0.318734, 9.566071, 126.709252, 1.705032] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "Tell me the dynamic viscosity with Hartmann number of 0.318734, magnetic flux density of 9.56607, characteristic length of 126.709 and electrical conductivity of 1.70503.", + "a": "24658097.55910176", + "valid": true, + "kind": "compute", + "expect": 24658097.55910176, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [0.318734, 9.566071, 126.709252, 1.705032] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "What is the dynamic viscosity for Hartmann number 87.781, magnetic flux density 13.2084, characteristic length 246.329 and electrical conductivity 20.3966?", + "a": "28021.322068820944", + "valid": true, + "kind": "compute", + "expect": 28021.322068820944, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [87.781013, 13.208419, 246.329053, 20.396558] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "Tell me the dynamic viscosity with Hartmann number of 87.781, magnetic flux density of 13.2084, characteristic length of 246.329 and electrical conductivity of 20.3966.", + "a": "28021.322068820944", + "valid": true, + "kind": "compute", + "expect": 28021.322068820944, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [87.781013, 13.208419, 246.329053, 20.396558] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "What is the dynamic viscosity for Hartmann number 156.502, magnetic flux density 185.886, characteristic length 13.2373 and electrical conductivity 4.71941?", + "a": "1166.6581282317718", + "valid": true, + "kind": "compute", + "expect": 1166.6581282317718, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [156.50181, 185.886273, 13.237311, 4.719415] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "Tell me the dynamic viscosity with Hartmann number of 156.502, magnetic flux density of 185.886, characteristic length of 13.2373 and electrical conductivity of 4.71941.", + "a": "1166.6581282317718", + "valid": true, + "kind": "compute", + "expect": 1166.6581282317718, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [156.50181, 185.886273, 13.237311, 4.719415] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "What is the dynamic viscosity for Hartmann number 24.5721, magnetic flux density 108.451, characteristic length 1.69476 and electrical conductivity 49.4843?", + "a": "2768.6165222502555", + "valid": true, + "kind": "compute", + "expect": 2768.6165222502555, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [24.572086, 108.450792, 1.694755, 49.484326] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "Tell me the dynamic viscosity with Hartmann number of 24.5721, magnetic flux density of 108.451, characteristic length of 1.69476 and electrical conductivity of 49.4843.", + "a": "2768.6165222502555", + "valid": true, + "kind": "compute", + "expect": 2768.6165222502555, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [24.572086, 108.450792, 1.694755, 49.484326] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "What is the dynamic viscosity for Hartmann number 107.961, magnetic flux density 59.4105 and characteristic length 5.77717?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electrical conductivity is not given" + }, + { + "q": "Tell me the dynamic viscosity with Hartmann number of 2.69428, magnetic flux density of 48.921 and characteristic length of 157.223.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electrical conductivity is not given" + }, + { + "q": "What is the dynamic viscosity of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the dynamic viscosity of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the dynamic viscosity with Hartmann number of -0.318734, magnetic flux density of 9.56607, characteristic length of 126.709 and electrical conductivity of 1.70503.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Hartmann number: no example had one" + }, + { + "q": "Which dynamic viscosity is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the Hartmann number is 167.625, magnetic flux density is 10.3291, characteristic length is 2.21571 and the electrical conductivity is 58.5346, what is the dynamic viscosity?", + "a": "1.091147602112578", + "valid": true, + "kind": "compute", + "expect": 1.091147602112578, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [167.624948, 10.329054, 2.215713, 58.534594] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "Could you work out the dynamic viscosity given Hartmann number = 167.625, magnetic flux density = 10.3291, characteristic length = 2.21571, electrical conductivity = 58.5346?", + "a": "1.091147602112578", + "valid": true, + "kind": "compute", + "expect": 1.091147602112578, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [167.624948, 10.329054, 2.215713, 58.534594] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "I need the dynamic viscosity for a Hartmann number of 8.53158, a magnetic flux density of 124.718, a characteristic length of 1.91195 and an electrical conductivity of 82.2401, please.", + "a": "64244.83982976002", + "valid": true, + "kind": "compute", + "expect": 64244.83982976002, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [8.531577, 124.718185, 1.911952, 82.240072] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "If the Hartmann number is 8.53158, magnetic flux density is 124.718, characteristic length is 1.91195 and the electrical conductivity is 82.2401, what is the dynamic viscosity?", + "a": "64244.83982976002", + "valid": true, + "kind": "compute", + "expect": 64244.83982976002, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [8.531577, 124.718185, 1.911952, 82.240072] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "Could you work out the dynamic viscosity given Hartmann number = 81.1967, magnetic flux density = 6.98086, characteristic length = 0.551236, electrical conductivity = 2.88512?", + "a": "0.006480064642932963", + "valid": true, + "kind": "compute", + "expect": 0.006480064642932963, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [81.196705, 6.980857, 0.551236, 2.88512] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "I need the dynamic viscosity for a Hartmann number of 81.1967, a magnetic flux density of 6.98086, a characteristic length of 0.551236 and an electrical conductivity of 2.88512, please.", + "a": "0.006480064642932963", + "valid": true, + "kind": "compute", + "expect": 0.006480064642932963, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [81.196705, 6.980857, 0.551236, 2.88512] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "If the Hartmann number is 1.16997, magnetic flux density is 1.14865, characteristic length is 21.7931 and the electrical conductivity is 29.5106, what is the dynamic viscosity?", + "a": "13509.722998147015", + "valid": true, + "kind": "compute", + "expect": 13509.722998147015, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [1.169968, 1.148652, 21.793126, 29.510609] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "Could you work out the dynamic viscosity given Hartmann number = 1.16997, magnetic flux density = 1.14865, characteristic length = 21.7931, electrical conductivity = 29.5106?", + "a": "13509.722998147015", + "valid": true, + "kind": "compute", + "expect": 13509.722998147015, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [1.169968, 1.148652, 21.793126, 29.510609] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "I need the dynamic viscosity for a Hartmann number of 223.509, a magnetic flux density of 17.3414, a characteristic length of 7.10705 and an electrical conductivity of 30.2507, please.", + "a": "9.198046498938417", + "valid": true, + "kind": "compute", + "expect": 9.198046498938417, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [223.508796, 17.341435, 7.107049, 30.250749] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "If the Hartmann number is 223.509, magnetic flux density is 17.3414, characteristic length is 7.10705 and the electrical conductivity is 30.2507, what is the dynamic viscosity?", + "a": "9.198046498938417", + "valid": true, + "kind": "compute", + "expect": 9.198046498938417, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [223.508796, 17.341435, 7.107049, 30.250749] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "Could you work out the dynamic viscosity given Hartmann number = 0.869111, magnetic flux density = 39.1753, characteristic length = 0.432217, electrical conductivity = 16.6571?", + "a": "6322.304738475718", + "valid": true, + "kind": "compute", + "expect": 6322.304738475718, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [0.869111, 39.175263, 0.432217, 16.657051] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "I need the dynamic viscosity for a Hartmann number of 0.869111, a magnetic flux density of 39.1753, a characteristic length of 0.432217 and an electrical conductivity of 16.6571, please.", + "a": "6322.304738475718", + "valid": true, + "kind": "compute", + "expect": 6322.304738475718, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [0.869111, 39.175263, 0.432217, 16.657051] (the system Hartmann number (Wikidata Q1587280))" + }, + { + "q": "If the Hartmann number is 167.625, magnetic flux density is 10.3291 and the characteristic length is 2.21571, what is the dynamic viscosity?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electrical conductivity is not given" + }, + { + "q": "Could you work out the dynamic viscosity given Hartmann number = 8.53158, magnetic flux density = 124.718, characteristic length = 1.91195?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electrical conductivity is not given" + }, + { + "q": "Do you know the dynamic viscosity of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the dynamic viscosity of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the dynamic viscosity given Hartmann number = -81.1967, magnetic flux density = 6.98086, characteristic length = 0.551236, electrical conductivity = 2.88512?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Hartmann number: no example had one" + }, + { + "q": "What is a typical dynamic viscosity?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-dynamic-viscosity-3/program.py b/root/organs10/physics/specialists/solved-dynamic-viscosity-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..eceab126595fbda24d908d6f0b827ca0fdd5617d --- /dev/null +++ b/root/organs10/physics/specialists/solved-dynamic-viscosity-3/program.py @@ -0,0 +1,7 @@ +"""dynamic viscosity from Hartmann number, magnetic flux density, characteristic length, electrical conductivity (Hartmann number (Wikidata Q1587280)) +The maths organ (systems.py): solved from the system q0 = q1*q2*sqrt(((q3)/(q4))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(hartmann_number, magnetic_flux_density, characteristic_length, electrical_conductivity): + return 1.0*characteristic_length**2*electrical_conductivity*magnetic_flux_density**2/hartmann_number**2 diff --git a/root/organs10/physics/specialists/solved-dynamic-viscosity-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-dynamic-viscosity-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-dynamic-viscosity-3/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-dynamic-viscosity-4/organ_formula_v2.py b/root/organs10/physics/specialists/solved-dynamic-viscosity-4/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-dynamic-viscosity-4/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-dynamic-viscosity-4/panel.json b/root/organs10/physics/specialists/solved-dynamic-viscosity-4/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..880cbd4e78f3a8b2feba9a9a7de1699b59e1716d --- /dev/null +++ b/root/organs10/physics/specialists/solved-dynamic-viscosity-4/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the dynamic viscosity for fluidity 107.961?", + "a": "0.00926263275385168", + "valid": true, + "kind": "compute", + "expect": 0.00926263275385168, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [107.960666] (the system fluidity (Wikidata Q3074482))" + }, + { + "q": "Tell me the dynamic viscosity with fluidity of 107.961.", + "a": "0.00926263275385168", + "valid": true, + "kind": "compute", + "expect": 0.00926263275385168, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [107.960666] (the system fluidity (Wikidata Q3074482))" + }, + { + "q": "What is the dynamic viscosity for fluidity 22.652?", + "a": "0.044146167826513254", + "valid": true, + "kind": "compute", + "expect": 0.044146167826513254, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [22.652023] (the system fluidity (Wikidata Q3074482))" + }, + { + "q": "Tell me the dynamic viscosity with fluidity of 22.652.", + "a": "0.044146167826513254", + "valid": true, + "kind": "compute", + "expect": 0.044146167826513254, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [22.652023] (the system fluidity (Wikidata Q3074482))" + }, + { + "q": "What is the dynamic viscosity for fluidity 173.394?", + "a": "0.0057672242718762815", + "valid": true, + "kind": "compute", + "expect": 0.0057672242718762815, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [173.393638] (the system fluidity (Wikidata Q3074482))" + }, + { + "q": "Tell me the dynamic viscosity with fluidity of 173.394.", + "a": "0.0057672242718762815", + "valid": true, + "kind": "compute", + "expect": 0.0057672242718762815, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [173.393638] (the system fluidity (Wikidata Q3074482))" + }, + { + "q": "What is the dynamic viscosity for fluidity 126.709?", + "a": "0.007892083536070259", + "valid": true, + "kind": "compute", + "expect": 0.007892083536070259, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [126.709252] (the system fluidity (Wikidata Q3074482))" + }, + { + "q": "Tell me the dynamic viscosity with fluidity of 126.709.", + "a": "0.007892083536070259", + "valid": true, + "kind": "compute", + "expect": 0.007892083536070259, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [126.709252] (the system fluidity (Wikidata Q3074482))" + }, + { + "q": "What is the dynamic viscosity for fluidity 0.671697?", + "a": "1.4887660694527203", + "valid": true, + "kind": "compute", + "expect": 1.4887660694527203, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [0.671697] (the system fluidity (Wikidata Q3074482))" + }, + { + "q": "Tell me the dynamic viscosity with fluidity of 0.671697.", + "a": "1.4887660694527203", + "valid": true, + "kind": "compute", + "expect": 1.4887660694527203, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [0.671697] (the system fluidity (Wikidata Q3074482))" + }, + { + "q": "What is the dynamic viscosity for fluidity 1.16997?", + "a": "0.8547242274066585", + "valid": true, + "kind": "compute", + "expect": 0.8547242274066585, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [1.169968] (the system fluidity (Wikidata Q3074482))" + }, + { + "q": "Tell me the dynamic viscosity with fluidity of 1.16997.", + "a": "0.8547242274066585", + "valid": true, + "kind": "compute", + "expect": 0.8547242274066585, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [1.169968] (the system fluidity (Wikidata Q3074482))" + }, + { + "q": "What is the dynamic viscosity?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the fluidity is not given" + }, + { + "q": "Could you work out the dynamic viscosity, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the fluidity is not given" + }, + { + "q": "What is the dynamic viscosity of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the dynamic viscosity of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the dynamic viscosity with fluidity of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative fluidity: no example had one" + }, + { + "q": "Which dynamic viscosity is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the fluidity is 2.57003, what is the dynamic viscosity?", + "a": "0.38910052946214563", + "valid": true, + "kind": "compute", + "expect": 0.38910052946214563, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [2.57003] (the system fluidity (Wikidata Q3074482))" + }, + { + "q": "Could you work out the dynamic viscosity given fluidity = 2.57003?", + "a": "0.38910052946214563", + "valid": true, + "kind": "compute", + "expect": 0.38910052946214563, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [2.57003] (the system fluidity (Wikidata Q3074482))" + }, + { + "q": "I need the dynamic viscosity for a fluidity of 48.921, please.", + "a": "0.02044112317474344", + "valid": true, + "kind": "compute", + "expect": 0.02044112317474344, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [48.920991] (the system fluidity (Wikidata Q3074482))" + }, + { + "q": "If the fluidity is 48.921, what is the dynamic viscosity?", + "a": "0.02044112317474344", + "valid": true, + "kind": "compute", + "expect": 0.02044112317474344, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [48.920991] (the system fluidity (Wikidata Q3074482))" + }, + { + "q": "Could you work out the dynamic viscosity given fluidity = 0.348438?", + "a": "2.869953759769417", + "valid": true, + "kind": "compute", + "expect": 2.869953759769417, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [0.348438] (the system fluidity (Wikidata Q3074482))" + }, + { + "q": "I need the dynamic viscosity for a fluidity of 0.348438, please.", + "a": "2.869953759769417", + "valid": true, + "kind": "compute", + "expect": 2.869953759769417, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [0.348438] (the system fluidity (Wikidata Q3074482))" + }, + { + "q": "If the fluidity is 252.709, what is the dynamic viscosity?", + "a": "0.0039571202610184176", + "valid": true, + "kind": "compute", + "expect": 0.0039571202610184176, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [252.709024] (the system fluidity (Wikidata Q3074482))" + }, + { + "q": "Could you work out the dynamic viscosity given fluidity = 252.709?", + "a": "0.0039571202610184176", + "valid": true, + "kind": "compute", + "expect": 0.0039571202610184176, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [252.709024] (the system fluidity (Wikidata Q3074482))" + }, + { + "q": "I need the dynamic viscosity for a fluidity of 20.3966, please.", + "a": "0.04902788018766951", + "valid": true, + "kind": "compute", + "expect": 0.04902788018766951, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [20.396558] (the system fluidity (Wikidata Q3074482))" + }, + { + "q": "If the fluidity is 20.3966, what is the dynamic viscosity?", + "a": "0.04902788018766951", + "valid": true, + "kind": "compute", + "expect": 0.04902788018766951, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [20.396558] (the system fluidity (Wikidata Q3074482))" + }, + { + "q": "Could you work out the dynamic viscosity given fluidity = 134.993?", + "a": "0.007407793842889246", + "valid": true, + "kind": "compute", + "expect": 0.007407793842889246, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [134.992958] (the system fluidity (Wikidata Q3074482))" + }, + { + "q": "I need the dynamic viscosity for a fluidity of 134.993, please.", + "a": "0.007407793842889246", + "valid": true, + "kind": "compute", + "expect": 0.007407793842889246, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [134.992958] (the system fluidity (Wikidata Q3074482))" + }, + { + "q": "I need the dynamic viscosity, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the fluidity is not given" + }, + { + "q": "Can you tell me the dynamic viscosity?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the fluidity is not given" + }, + { + "q": "Do you know the dynamic viscosity of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the dynamic viscosity of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the dynamic viscosity given fluidity = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative fluidity: no example had one" + }, + { + "q": "What is a typical dynamic viscosity?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-dynamic-viscosity-4/program.py b/root/organs10/physics/specialists/solved-dynamic-viscosity-4/program.py new file mode 100644 index 0000000000000000000000000000000000000000..b66283d096ce53cd40153caa9547c45b968a3dc3 --- /dev/null +++ b/root/organs10/physics/specialists/solved-dynamic-viscosity-4/program.py @@ -0,0 +1,7 @@ +"""dynamic viscosity from fluidity (fluidity (Wikidata Q3074482)) +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(fluidity): + return 1.0/fluidity diff --git a/root/organs10/physics/specialists/solved-dynamic-viscosity-4/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-dynamic-viscosity-4/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-dynamic-viscosity-4/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-dynamic-viscosity/organ_formula_v2.py b/root/organs10/physics/specialists/solved-dynamic-viscosity/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-dynamic-viscosity/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-dynamic-viscosity/panel.json b/root/organs10/physics/specialists/solved-dynamic-viscosity/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..18c9e429eba77ce443a78c001c80cbaad7bc86bf --- /dev/null +++ b/root/organs10/physics/specialists/solved-dynamic-viscosity/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the dynamic viscosity for shear stress at flow limit 107.961, Hedström number 59.4105, characteristic diameter 5.77717 and mass density 1.89126?", + "a": "10.710038716786332", + "valid": true, + "kind": "compute", + "expect": 10.710038716786332, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [107.960666, 59.4105, 5.777165, 1.891256] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "Tell me the dynamic viscosity with shear stress at flow limit of 107.961, Hedström number of 59.4105, characteristic diameter of 5.77717 and mass density of 1.89126.", + "a": "10.710038716786332", + "valid": true, + "kind": "compute", + "expect": 10.710038716786332, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [107.960666, 59.4105, 5.777165, 1.891256] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "What is the dynamic viscosity for shear stress at flow limit 2.69428, Hedström number 48.921, characteristic diameter 157.223 and mass density 35.6412?", + "a": "220.27463656549023", + "valid": true, + "kind": "compute", + "expect": 220.27463656549023, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [2.69428, 48.920991, 157.22256, 35.641157] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "Tell me the dynamic viscosity with shear stress at flow limit of 2.69428, Hedström number of 48.921, characteristic diameter of 157.223 and mass density of 35.6412.", + "a": "220.27463656549023", + "valid": true, + "kind": "compute", + "expect": 220.27463656549023, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [2.69428, 48.920991, 157.22256, 35.641157] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "What is the dynamic viscosity for shear stress at flow limit 0.318734, Hedström number 9.56607, characteristic diameter 126.709 and mass density 1.70503?", + "a": "30.201051998972687", + "valid": true, + "kind": "compute", + "expect": 30.201051998972687, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [0.318734, 9.566071, 126.709252, 1.705032] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "Tell me the dynamic viscosity with shear stress at flow limit of 0.318734, Hedström number of 9.56607, characteristic diameter of 126.709 and mass density of 1.70503.", + "a": "30.201051998972687", + "valid": true, + "kind": "compute", + "expect": 30.201051998972687, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [0.318734, 9.566071, 126.709252, 1.705032] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "What is the dynamic viscosity for shear stress at flow limit 87.781, Hedström number 13.2084, characteristic diameter 246.329 and mass density 20.3966?", + "a": "2867.9327564561436", + "valid": true, + "kind": "compute", + "expect": 2867.9327564561436, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [87.781013, 13.208419, 246.329053, 20.396558] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "Tell me the dynamic viscosity with shear stress at flow limit of 87.781, Hedström number of 13.2084, characteristic diameter of 246.329 and mass density of 20.3966.", + "a": "2867.9327564561436", + "valid": true, + "kind": "compute", + "expect": 2867.9327564561436, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [87.781013, 13.208419, 246.329053, 20.396558] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "What is the dynamic viscosity for shear stress at flow limit 156.502, Hedström number 185.886, characteristic diameter 13.2373 and mass density 4.71941?", + "a": "26.386383640579513", + "valid": true, + "kind": "compute", + "expect": 26.386383640579513, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [156.50181, 185.886273, 13.237311, 4.719415] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "Tell me the dynamic viscosity with shear stress at flow limit of 156.502, Hedström number of 185.886, characteristic diameter of 13.2373 and mass density of 4.71941.", + "a": "26.386383640579513", + "valid": true, + "kind": "compute", + "expect": 26.386383640579513, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [156.50181, 185.886273, 13.237311, 4.719415] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "What is the dynamic viscosity for shear stress at flow limit 24.5721, Hedström number 108.451, characteristic diameter 1.69476 and mass density 49.4843?", + "a": "5.674731849921823", + "valid": true, + "kind": "compute", + "expect": 5.674731849921823, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [24.572086, 108.450792, 1.694755, 49.484326] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "Tell me the dynamic viscosity with shear stress at flow limit of 24.5721, Hedström number of 108.451, characteristic diameter of 1.69476 and mass density of 49.4843.", + "a": "5.674731849921823", + "valid": true, + "kind": "compute", + "expect": 5.674731849921823, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [24.572086, 108.450792, 1.694755, 49.484326] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "What is the dynamic viscosity for shear stress at flow limit 107.961, Hedström number 59.4105 and characteristic diameter 5.77717?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Tell me the dynamic viscosity with shear stress at flow limit of 2.69428, Hedström number of 48.921 and characteristic diameter of 157.223.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "What is the dynamic viscosity of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the dynamic viscosity of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the dynamic viscosity with shear stress at flow limit of -0.318734, Hedström number of 9.56607, characteristic diameter of 126.709 and mass density of 1.70503.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative shear stress at flow limit: no example had one" + }, + { + "q": "Which dynamic viscosity is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the shear stress at flow limit is 167.625, Hedström number is 10.3291, characteristic diameter is 2.21571 and the mass density is 58.5346, what is the dynamic viscosity?", + "a": "68.29027844482033", + "valid": true, + "kind": "compute", + "expect": 68.29027844482033, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [167.624948, 10.329054, 2.215713, 58.534594] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "Could you work out the dynamic viscosity given shear stress at flow limit = 167.625, Hedström number = 10.3291, characteristic diameter = 2.21571, mass density = 58.5346?", + "a": "68.29027844482033", + "valid": true, + "kind": "compute", + "expect": 68.29027844482033, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [167.624948, 10.329054, 2.215713, 58.534594] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "I need the dynamic viscosity for a shear stress at flow limit of 8.53158, a Hedström number of 124.718, a characteristic diameter of 1.91195 and a mass density of 82.2401, please.", + "a": "4.534907623507841", + "valid": true, + "kind": "compute", + "expect": 4.534907623507841, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [8.531577, 124.718185, 1.911952, 82.240072] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "If the shear stress at flow limit is 8.53158, Hedström number is 124.718, characteristic diameter is 1.91195 and the mass density is 82.2401, what is the dynamic viscosity?", + "a": "4.534907623507841", + "valid": true, + "kind": "compute", + "expect": 4.534907623507841, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [8.531577, 124.718185, 1.911952, 82.240072] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "Could you work out the dynamic viscosity given shear stress at flow limit = 81.1967, Hedström number = 6.98086, characteristic diameter = 0.551236, mass density = 2.88512?", + "a": "3.1932594746327663", + "valid": true, + "kind": "compute", + "expect": 3.1932594746327663, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [81.196705, 6.980857, 0.551236, 2.88512] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "I need the dynamic viscosity for a shear stress at flow limit of 81.1967, a Hedström number of 6.98086, a characteristic diameter of 0.551236 and a mass density of 2.88512, please.", + "a": "3.1932594746327663", + "valid": true, + "kind": "compute", + "expect": 3.1932594746327663, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [81.196705, 6.980857, 0.551236, 2.88512] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "If the shear stress at flow limit is 1.16997, Hedström number is 1.14865, characteristic diameter is 21.7931 and the mass density is 29.5106, what is the dynamic viscosity?", + "a": "119.48168084314823", + "valid": true, + "kind": "compute", + "expect": 119.48168084314823, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [1.169968, 1.148652, 21.793126, 29.510609] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "Could you work out the dynamic viscosity given shear stress at flow limit = 1.16997, Hedström number = 1.14865, characteristic diameter = 21.7931, mass density = 29.5106?", + "a": "119.48168084314823", + "valid": true, + "kind": "compute", + "expect": 119.48168084314823, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [1.169968, 1.148652, 21.793126, 29.510609] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "I need the dynamic viscosity for a shear stress at flow limit of 223.509, a Hedström number of 17.3414, a characteristic diameter of 7.10705 and a mass density of 30.2507, please.", + "a": "140.3337664698935", + "valid": true, + "kind": "compute", + "expect": 140.3337664698935, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [223.508796, 17.341435, 7.107049, 30.250749] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "If the shear stress at flow limit is 223.509, Hedström number is 17.3414, characteristic diameter is 7.10705 and the mass density is 30.2507, what is the dynamic viscosity?", + "a": "140.3337664698935", + "valid": true, + "kind": "compute", + "expect": 140.3337664698935, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [223.508796, 17.341435, 7.107049, 30.250749] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "Could you work out the dynamic viscosity given shear stress at flow limit = 0.869111, Hedström number = 39.1753, characteristic diameter = 0.432217, mass density = 16.6571?", + "a": "0.2627441358573609", + "valid": true, + "kind": "compute", + "expect": 0.2627441358573609, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [0.869111, 39.175263, 0.432217, 16.657051] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "I need the dynamic viscosity for a shear stress at flow limit of 0.869111, a Hedström number of 39.1753, a characteristic diameter of 0.432217 and a mass density of 16.6571, please.", + "a": "0.2627441358573609", + "valid": true, + "kind": "compute", + "expect": 0.2627441358573609, + "dtype": "quantity", + "unit": "kg/m/s", + "why": "dynamic viscosity for [0.869111, 39.175263, 0.432217, 16.657051] (the system Hedström number (Wikidata Q3343027))" + }, + { + "q": "If the shear stress at flow limit is 167.625, Hedström number is 10.3291 and the characteristic diameter is 2.21571, what is the dynamic viscosity?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Could you work out the dynamic viscosity given shear stress at flow limit = 8.53158, Hedström number = 124.718, characteristic diameter = 1.91195?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mass density is not given" + }, + { + "q": "Do you know the dynamic viscosity of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the dynamic viscosity of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the dynamic viscosity given shear stress at flow limit = -81.1967, Hedström number = 6.98086, characteristic diameter = 0.551236, mass density = 2.88512?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative shear stress at flow limit: no example had one" + }, + { + "q": "What is a typical dynamic viscosity?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-dynamic-viscosity/program.py b/root/organs10/physics/specialists/solved-dynamic-viscosity/program.py new file mode 100644 index 0000000000000000000000000000000000000000..32dd1bf11e55ba107a2102441b7d8df9e6ab2384 --- /dev/null +++ b/root/organs10/physics/specialists/solved-dynamic-viscosity/program.py @@ -0,0 +1,7 @@ +"""dynamic viscosity from shear stress at flow limit, Hedström number, characteristic diameter, mass density (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(shear_stress_at_flow_limit, hedstr_m_number, characteristic_diameter, mass_density): + return 1.0*characteristic_diameter*hedstr_m_number**(-0.5)*mass_density**0.5*shear_stress_at_flow_limit**0.5 diff --git a/root/organs10/physics/specialists/solved-dynamic-viscosity/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-dynamic-viscosity/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-dynamic-viscosity/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-echo-distance/organ_formula_v2.py b/root/organs10/physics/specialists/solved-echo-distance/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-echo-distance/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-echo-distance/panel.json b/root/organs10/physics/specialists/solved-echo-distance/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..393d43dc65adf722cc3c9b2bf6c5dd59906c0104 --- /dev/null +++ b/root/organs10/physics/specialists/solved-echo-distance/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the echo distance for echo time 107.961?", + "a": "18515.254200128486", + "valid": true, + "kind": "compute", + "expect": 18515.254200128486, + "dtype": "quantity", + "unit": "m", + "why": "echo distance for [107.960666] (the system Echo)" + }, + { + "q": "Tell me the echo distance with echo time of 107.961.", + "a": "18515.254200128486", + "valid": true, + "kind": "compute", + "expect": 18515.254200128486, + "dtype": "quantity", + "unit": "m", + "why": "echo distance for [107.960666] (the system Echo)" + }, + { + "q": "What is the echo distance for echo time 22.652?", + "a": "3884.8219096607686", + "valid": true, + "kind": "compute", + "expect": 3884.8219096607686, + "dtype": "quantity", + "unit": "m", + "why": "echo distance for [22.652023] (the system Echo)" + }, + { + "q": "Tell me the echo distance with echo time of 22.652.", + "a": "3884.8219096607686", + "valid": true, + "kind": "compute", + "expect": 3884.8219096607686, + "dtype": "quantity", + "unit": "m", + "why": "echo distance for [22.652023] (the system Echo)" + }, + { + "q": "What is the echo distance for echo time 173.394?", + "a": "29737.00898650938", + "valid": true, + "kind": "compute", + "expect": 29737.00898650938, + "dtype": "quantity", + "unit": "m", + "why": "echo distance for [173.393638] (the system Echo)" + }, + { + "q": "Tell me the echo distance with echo time of 173.394.", + "a": "29737.00898650938", + "valid": true, + "kind": "compute", + "expect": 29737.00898650938, + "dtype": "quantity", + "unit": "m", + "why": "echo distance for [173.393638] (the system Echo)" + }, + { + "q": "What is the echo distance for echo time 126.709?", + "a": "21730.636683731278", + "valid": true, + "kind": "compute", + "expect": 21730.636683731278, + "dtype": "quantity", + "unit": "m", + "why": "echo distance for [126.709252] (the system Echo)" + }, + { + "q": "Tell me the echo distance with echo time of 126.709.", + "a": "21730.636683731278", + "valid": true, + "kind": "compute", + "expect": 21730.636683731278, + "dtype": "quantity", + "unit": "m", + "why": "echo distance for [126.709252] (the system Echo)" + }, + { + "q": "What is the echo distance for echo time 0.671697?", + "a": "115.19606976470419", + "valid": true, + "kind": "compute", + "expect": 115.19606976470419, + "dtype": "quantity", + "unit": "m", + "why": "echo distance for [0.671697] (the system Echo)" + }, + { + "q": "Tell me the echo distance with echo time of 0.671697.", + "a": "115.19606976470419", + "valid": true, + "kind": "compute", + "expect": 115.19606976470419, + "dtype": "quantity", + "unit": "m", + "why": "echo distance for [0.671697] (the system Echo)" + }, + { + "q": "What is the echo distance for echo time 1.16997?", + "a": "200.64951302521598", + "valid": true, + "kind": "compute", + "expect": 200.64951302521598, + "dtype": "quantity", + "unit": "m", + "why": "echo distance for [1.169968] (the system Echo)" + }, + { + "q": "Tell me the echo distance with echo time of 1.16997.", + "a": "200.64951302521598", + "valid": true, + "kind": "compute", + "expect": 200.64951302521598, + "dtype": "quantity", + "unit": "m", + "why": "echo distance for [1.169968] (the system Echo)" + }, + { + "q": "What is the echo distance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the echo time is not given" + }, + { + "q": "Could you work out the echo distance, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the echo time is not given" + }, + { + "q": "What is the echo distance of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the echo distance of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the echo distance with echo time of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative echo time: no example had one" + }, + { + "q": "Which echo distance is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the echo time is 2.57003, what is the echo distance?", + "a": "440.7601301315749", + "valid": true, + "kind": "compute", + "expect": 440.7601301315749, + "dtype": "quantity", + "unit": "m", + "why": "echo distance for [2.57003] (the system Echo)" + }, + { + "q": "Could you work out the echo distance given echo time = 2.57003?", + "a": "440.7601301315749", + "valid": true, + "kind": "compute", + "expect": 440.7601301315749, + "dtype": "quantity", + "unit": "m", + "why": "echo distance for [2.57003] (the system Echo)" + }, + { + "q": "I need the echo distance for an echo time of 48.921, please.", + "a": "8389.949932492029", + "valid": true, + "kind": "compute", + "expect": 8389.949932492029, + "dtype": "quantity", + "unit": "m", + "why": "echo distance for [48.920991] (the system Echo)" + }, + { + "q": "If the echo time is 48.921, what is the echo distance?", + "a": "8389.949932492029", + "valid": true, + "kind": "compute", + "expect": 8389.949932492029, + "dtype": "quantity", + "unit": "m", + "why": "echo distance for [48.920991] (the system Echo)" + }, + { + "q": "Could you work out the echo distance given echo time = 0.348438?", + "a": "59.75706034155023", + "valid": true, + "kind": "compute", + "expect": 59.75706034155023, + "dtype": "quantity", + "unit": "m", + "why": "echo distance for [0.348438] (the system Echo)" + }, + { + "q": "I need the echo distance for an echo time of 0.348438, please.", + "a": "59.75706034155023", + "valid": true, + "kind": "compute", + "expect": 59.75706034155023, + "dtype": "quantity", + "unit": "m", + "why": "echo distance for [0.348438] (the system Echo)" + }, + { + "q": "If the echo time is 252.709, what is the echo distance?", + "a": "43339.597658793966", + "valid": true, + "kind": "compute", + "expect": 43339.597658793966, + "dtype": "quantity", + "unit": "m", + "why": "echo distance for [252.709024] (the system Echo)" + }, + { + "q": "Could you work out the echo distance given echo time = 252.709?", + "a": "43339.597658793966", + "valid": true, + "kind": "compute", + "expect": 43339.597658793966, + "dtype": "quantity", + "unit": "m", + "why": "echo distance for [252.709024] (the system Echo)" + }, + { + "q": "I need the echo distance for an echo time of 20.3966, please.", + "a": "3498.0096904767297", + "valid": true, + "kind": "compute", + "expect": 3498.0096904767297, + "dtype": "quantity", + "unit": "m", + "why": "echo distance for [20.396558] (the system Echo)" + }, + { + "q": "If the echo time is 20.3966, what is the echo distance?", + "a": "3498.0096904767297", + "valid": true, + "kind": "compute", + "expect": 3498.0096904767297, + "dtype": "quantity", + "unit": "m", + "why": "echo distance for [20.396558] (the system Echo)" + }, + { + "q": "Could you work out the echo distance given echo time = 134.993?", + "a": "23151.292225096568", + "valid": true, + "kind": "compute", + "expect": 23151.292225096568, + "dtype": "quantity", + "unit": "m", + "why": "echo distance for [134.992958] (the system Echo)" + }, + { + "q": "I need the echo distance for an echo time of 134.993, please.", + "a": "23151.292225096568", + "valid": true, + "kind": "compute", + "expect": 23151.292225096568, + "dtype": "quantity", + "unit": "m", + "why": "echo distance for [134.992958] (the system Echo)" + }, + { + "q": "I need the echo distance, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the echo time is not given" + }, + { + "q": "Can you tell me the echo distance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the echo time is not given" + }, + { + "q": "Do you know the echo distance of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the echo distance of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the echo distance given echo time = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative echo time: no example had one" + }, + { + "q": "What is a typical echo distance?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-echo-distance/program.py b/root/organs10/physics/specialists/solved-echo-distance/program.py new file mode 100644 index 0000000000000000000000000000000000000000..febbdc79a34e991a1d015421e10c69acc075b3f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-echo-distance/program.py @@ -0,0 +1,7 @@ +"""echo distance from echo time (Echo) +The maths organ (systems.py): solved from the system d = c*t/2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(echo_time): + return 171.5*echo_time diff --git a/root/organs10/physics/specialists/solved-echo-distance/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-echo-distance/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-echo-distance/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-echo-time/organ_formula_v2.py b/root/organs10/physics/specialists/solved-echo-time/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-echo-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-echo-time/panel.json b/root/organs10/physics/specialists/solved-echo-time/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..bd1c599346563491192b85b4b41f952a863b3ace --- /dev/null +++ b/root/organs10/physics/specialists/solved-echo-time/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the echo time for echo distance 107.961?", + "a": "0.6295082559181455", + "valid": true, + "kind": "compute", + "expect": 0.6295082559181455, + "dtype": "quantity", + "unit": "s", + "why": "echo time for [107.960666] (the system Echo)" + }, + { + "q": "Tell me the echo time with echo distance of 107.961.", + "a": "0.6295082559181455", + "valid": true, + "kind": "compute", + "expect": 0.6295082559181455, + "dtype": "quantity", + "unit": "s", + "why": "echo time for [107.960666] (the system Echo)" + }, + { + "q": "What is the echo time for echo distance 22.652?", + "a": "0.132081765579334", + "valid": true, + "kind": "compute", + "expect": 0.132081765579334, + "dtype": "quantity", + "unit": "s", + "why": "echo time for [22.652023] (the system Echo)" + }, + { + "q": "Tell me the echo time with echo distance of 22.652.", + "a": "0.132081765579334", + "valid": true, + "kind": "compute", + "expect": 0.132081765579334, + "dtype": "quantity", + "unit": "s", + "why": "echo time for [22.652023] (the system Echo)" + }, + { + "q": "What is the echo time for echo distance 173.394?", + "a": "1.011041623354533", + "valid": true, + "kind": "compute", + "expect": 1.011041623354533, + "dtype": "quantity", + "unit": "s", + "why": "echo time for [173.393638] (the system Echo)" + }, + { + "q": "Tell me the echo time with echo distance of 173.394.", + "a": "1.011041623354533", + "valid": true, + "kind": "compute", + "expect": 1.011041623354533, + "dtype": "quantity", + "unit": "s", + "why": "echo time for [173.393638] (the system Echo)" + }, + { + "q": "What is the echo time for echo distance 126.709?", + "a": "0.7388294565608297", + "valid": true, + "kind": "compute", + "expect": 0.7388294565608297, + "dtype": "quantity", + "unit": "s", + "why": "echo time for [126.709252] (the system Echo)" + }, + { + "q": "Tell me the echo time with echo distance of 126.709.", + "a": "0.7388294565608297", + "valid": true, + "kind": "compute", + "expect": 0.7388294565608297, + "dtype": "quantity", + "unit": "s", + "why": "echo time for [126.709252] (the system Echo)" + }, + { + "q": "What is the echo time for echo distance 0.671697?", + "a": "0.003916601748071097", + "valid": true, + "kind": "compute", + "expect": 0.003916601748071097, + "dtype": "quantity", + "unit": "s", + "why": "echo time for [0.671697] (the system Echo)" + }, + { + "q": "Tell me the echo time with echo distance of 0.671697.", + "a": "0.003916601748071097", + "valid": true, + "kind": "compute", + "expect": 0.003916601748071097, + "dtype": "quantity", + "unit": "s", + "why": "echo time for [0.671697] (the system Echo)" + }, + { + "q": "What is the echo time for echo distance 1.16997?", + "a": "0.006821970880337814", + "valid": true, + "kind": "compute", + "expect": 0.006821970880337814, + "dtype": "quantity", + "unit": "s", + "why": "echo time for [1.169968] (the system Echo)" + }, + { + "q": "Tell me the echo time with echo distance of 1.16997.", + "a": "0.006821970880337814", + "valid": true, + "kind": "compute", + "expect": 0.006821970880337814, + "dtype": "quantity", + "unit": "s", + "why": "echo time for [1.169968] (the system Echo)" + }, + { + "q": "What is the echo time?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the echo distance is not given" + }, + { + "q": "Could you work out the echo time, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the echo distance is not given" + }, + { + "q": "What is the echo 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 echo 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 echo time with echo distance of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative echo distance: no example had one" + }, + { + "q": "Which echo 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 echo distance is 2.57003, what is the echo time?", + "a": "0.014985597162120362", + "valid": true, + "kind": "compute", + "expect": 0.014985597162120362, + "dtype": "quantity", + "unit": "s", + "why": "echo time for [2.57003] (the system Echo)" + }, + { + "q": "Could you work out the echo time given echo distance = 2.57003?", + "a": "0.014985597162120362", + "valid": true, + "kind": "compute", + "expect": 0.014985597162120362, + "dtype": "quantity", + "unit": "s", + "why": "echo time for [2.57003] (the system Echo)" + }, + { + "q": "I need the echo time for an echo distance of 48.921, please.", + "a": "0.285253591020477", + "valid": true, + "kind": "compute", + "expect": 0.285253591020477, + "dtype": "quantity", + "unit": "s", + "why": "echo time for [48.920991] (the system Echo)" + }, + { + "q": "If the echo distance is 48.921, what is the echo time?", + "a": "0.285253591020477", + "valid": true, + "kind": "compute", + "expect": 0.285253591020477, + "dtype": "quantity", + "unit": "s", + "why": "echo time for [48.920991] (the system Echo)" + }, + { + "q": "Could you work out the echo time given echo distance = 0.348438?", + "a": "0.002031706528454987", + "valid": true, + "kind": "compute", + "expect": 0.002031706528454987, + "dtype": "quantity", + "unit": "s", + "why": "echo time for [0.348438] (the system Echo)" + }, + { + "q": "I need the echo time for an echo distance of 0.348438, please.", + "a": "0.002031706528454987", + "valid": true, + "kind": "compute", + "expect": 0.002031706528454987, + "dtype": "quantity", + "unit": "s", + "why": "echo time for [0.348438] (the system Echo)" + }, + { + "q": "If the echo distance is 252.709, what is the echo time?", + "a": "1.473522007285874", + "valid": true, + "kind": "compute", + "expect": 1.473522007285874, + "dtype": "quantity", + "unit": "s", + "why": "echo time for [252.709024] (the system Echo)" + }, + { + "q": "Could you work out the echo time given echo distance = 252.709?", + "a": "1.473522007285874", + "valid": true, + "kind": "compute", + "expect": 1.473522007285874, + "dtype": "quantity", + "unit": "s", + "why": "echo time for [252.709024] (the system Echo)" + }, + { + "q": "I need the echo time for an echo distance of 20.3966, please.", + "a": "0.11893036712515118", + "valid": true, + "kind": "compute", + "expect": 0.11893036712515118, + "dtype": "quantity", + "unit": "s", + "why": "echo time for [20.396558] (the system Echo)" + }, + { + "q": "If the echo distance is 20.3966, what is the echo time?", + "a": "0.11893036712515118", + "valid": true, + "kind": "compute", + "expect": 0.11893036712515118, + "dtype": "quantity", + "unit": "s", + "why": "echo time for [20.396558] (the system Echo)" + }, + { + "q": "Could you work out the echo time given echo distance = 134.993?", + "a": "0.7871309479926408", + "valid": true, + "kind": "compute", + "expect": 0.7871309479926408, + "dtype": "quantity", + "unit": "s", + "why": "echo time for [134.992958] (the system Echo)" + }, + { + "q": "I need the echo time for an echo distance of 134.993, please.", + "a": "0.7871309479926408", + "valid": true, + "kind": "compute", + "expect": 0.7871309479926408, + "dtype": "quantity", + "unit": "s", + "why": "echo time for [134.992958] (the system Echo)" + }, + { + "q": "I need the echo time, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the echo distance is not given" + }, + { + "q": "Can you tell me the echo time?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the echo distance is not given" + }, + { + "q": "Do you know the echo 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 echo 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 echo time given echo distance = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative echo distance: no example had one" + }, + { + "q": "What is a typical echo 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-echo-time/program.py b/root/organs10/physics/specialists/solved-echo-time/program.py new file mode 100644 index 0000000000000000000000000000000000000000..59441db5496e4803ab94bae305a9ce5e74f44a10 --- /dev/null +++ b/root/organs10/physics/specialists/solved-echo-time/program.py @@ -0,0 +1,7 @@ +"""echo time from echo distance (Echo) +The maths organ (systems.py): solved from the system d = c*t/2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(echo_distance): + return 0.00583090379008746*echo_distance diff --git a/root/organs10/physics/specialists/solved-echo-time/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-echo-time/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-echo-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-eckert-number/organ_formula_v2.py b/root/organs10/physics/specialists/solved-eckert-number/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-eckert-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-eckert-number/panel.json b/root/organs10/physics/specialists/solved-eckert-number/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..2841c939430ba32309d575fdc4819262dc8f5ff1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-eckert-number/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the Eckert number for specific heat capacity at constant pressure 107.961, characteristic speed 59.4105 and temperature difference 5.77717?", + "a": "5.659083741837898", + "valid": true, + "kind": "compute", + "expect": 5.659083741837898, + "dtype": "quantity", + "unit": "1", + "why": "Eckert number for [107.960666, 59.4105, 5.777165] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "Tell me the Eckert number with specific heat capacity at constant pressure 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": "1", + "why": "Eckert number for [107.960666, 59.4105, 5.777165] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "What is the Eckert number for specific heat capacity at constant pressure 85.2416, characteristic speed 160.878 and temperature difference 2.69428?", + "a": "112.69412780468696", + "valid": true, + "kind": "compute", + "expect": 112.69412780468696, + "dtype": "quantity", + "unit": "1", + "why": "Eckert number for [85.241623, 160.878442, 2.69428] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "Tell me the Eckert number with specific heat capacity at constant pressure 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": "1", + "why": "Eckert number for [85.241623, 160.878442, 2.69428] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "What is the Eckert number for specific heat capacity at constant pressure 45.6157, characteristic speed 4.97131 and temperature difference 94.305?", + "a": "0.005745029494864405", + "valid": true, + "kind": "compute", + "expect": 0.005745029494864405, + "dtype": "quantity", + "unit": "1", + "why": "Eckert number for [45.61567, 4.971307, 94.305046] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "Tell me the Eckert number with specific heat capacity at constant pressure 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": "1", + "why": "Eckert number for [45.61567, 4.971307, 94.305046] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "What is the Eckert number for specific heat capacity at constant pressure 10.5638, characteristic speed 198.839 and temperature difference 0.671697?", + "a": "5571.9534562043855", + "valid": true, + "kind": "compute", + "expect": 5571.9534562043855, + "dtype": "quantity", + "unit": "1", + "why": "Eckert number for [10.563843, 198.838955, 0.671697] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "Tell me the Eckert number with specific heat capacity at constant pressure 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": "1", + "why": "Eckert number for [10.563843, 198.838955, 0.671697] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "What is the Eckert number for specific heat capacity at constant pressure 21.7931, characteristic speed 29.5106 and temperature difference 8.50341?", + "a": "4.699415211384588", + "valid": true, + "kind": "compute", + "expect": 4.699415211384588, + "dtype": "quantity", + "unit": "1", + "why": "Eckert number for [21.793126, 29.510609, 8.503407] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "Tell me the Eckert number with specific heat capacity at constant pressure 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": "1", + "why": "Eckert number for [21.793126, 29.510609, 8.503407] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "What is the Eckert number for specific heat capacity at constant pressure 223.509, characteristic speed 17.3414 and temperature difference 7.10705?", + "a": "0.18931547910985136", + "valid": true, + "kind": "compute", + "expect": 0.18931547910985136, + "dtype": "quantity", + "unit": "1", + "why": "Eckert number for [223.508796, 17.341435, 7.107049] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "Tell me the Eckert number with specific heat capacity at constant pressure 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": "1", + "why": "Eckert number for [223.508796, 17.341435, 7.107049] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "What is the Eckert number for specific heat capacity at constant pressure 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 Eckert number with specific heat capacity at constant pressure 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 Eckert 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 Eckert 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 Eckert number with specific heat capacity at constant pressure 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 specific heat capacity at constant pressure: no example had one" + }, + { + "q": "Which Eckert 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, characteristic speed is 167.625 and the temperature difference is 10.3291, what is the Eckert number?", + "a": "152.92223099628058", + "valid": true, + "kind": "compute", + "expect": 152.92223099628058, + "dtype": "quantity", + "unit": "1", + "why": "Eckert number for [17.788778, 167.624948, 10.329054] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "Could you work out the Eckert number given specific heat capacity at constant pressure = 17.7888, characteristic speed = 167.625, temperature difference = 10.3291?", + "a": "152.92223099628058", + "valid": true, + "kind": "compute", + "expect": 152.92223099628058, + "dtype": "quantity", + "unit": "1", + "why": "Eckert number for [17.788778, 167.624948, 10.329054] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "I need the Eckert number for a specific heat capacity at constant pressure 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": "1", + "why": "Eckert number for [21.511016, 173.393638, 251.084037] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "If the specific heat capacity at constant pressure is 21.511, characteristic speed is 173.394 and the temperature difference is 251.084, what is the Eckert number?", + "a": "5.566552325488382", + "valid": true, + "kind": "compute", + "expect": 5.566552325488382, + "dtype": "quantity", + "unit": "1", + "why": "Eckert number for [21.511016, 173.393638, 251.084037] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "Could you work out the Eckert number given specific heat capacity at constant pressure = 129.245, characteristic speed = 1.18359, temperature difference = 15.9416?", + "a": "0.0006799181441626288", + "valid": true, + "kind": "compute", + "expect": 0.0006799181441626288, + "dtype": "quantity", + "unit": "1", + "why": "Eckert number for [129.244984, 1.18359, 15.941612] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "I need the Eckert number for a specific heat capacity at constant pressure 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": "1", + "why": "Eckert number for [129.244984, 1.18359, 15.941612] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "If the specific heat capacity at constant pressure is 20.3966, characteristic speed is 18.3169 and the temperature difference is 6.8386, what is the Eckert number?", + "a": "2.4053507936230525", + "valid": true, + "kind": "compute", + "expect": 2.4053507936230525, + "dtype": "quantity", + "unit": "1", + "why": "Eckert number for [20.396558, 18.316867, 6.838598] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "Could you work out the Eckert number given specific heat capacity at constant pressure = 20.3966, characteristic speed = 18.3169, temperature difference = 6.8386?", + "a": "2.4053507936230525", + "valid": true, + "kind": "compute", + "expect": 2.4053507936230525, + "dtype": "quantity", + "unit": "1", + "why": "Eckert number for [20.396558, 18.316867, 6.838598] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "I need the Eckert number for a specific heat capacity at constant pressure 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": "1", + "why": "Eckert number for [185.886273, 13.237311, 4.719415] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "If the specific heat capacity at constant pressure is 185.886, characteristic speed is 13.2373 and the temperature difference is 4.71941, what is the Eckert number?", + "a": "0.19973955571183355", + "valid": true, + "kind": "compute", + "expect": 0.19973955571183355, + "dtype": "quantity", + "unit": "1", + "why": "Eckert number for [185.886273, 13.237311, 4.719415] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "Could you work out the Eckert number given specific heat capacity at constant pressure = 9.1061, characteristic speed = 24.5721, temperature difference = 108.451?", + "a": "0.6113905328353927", + "valid": true, + "kind": "compute", + "expect": 0.6113905328353927, + "dtype": "quantity", + "unit": "1", + "why": "Eckert number for [9.106104, 24.572086, 108.450792] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "I need the Eckert number for a specific heat capacity at constant pressure 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": "1", + "why": "Eckert number for [9.106104, 24.572086, 108.450792] (the system Eckert number (Wikidata Q905744))" + }, + { + "q": "If the specific heat capacity at constant pressure is 17.7888 and the characteristic speed is 167.625, what is the Eckert number?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the temperature difference is not given" + }, + { + "q": "Could you work out the Eckert number given specific heat capacity at constant pressure = 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 Eckert 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 Eckert 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 Eckert number given specific heat capacity at constant pressure = -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 specific heat capacity at constant pressure: no example had one" + }, + { + "q": "What is a typical Eckert 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-eckert-number/program.py b/root/organs10/physics/specialists/solved-eckert-number/program.py new file mode 100644 index 0000000000000000000000000000000000000000..b62392526a350cc5cd0622fd4fe69168520a1b95 --- /dev/null +++ b/root/organs10/physics/specialists/solved-eckert-number/program.py @@ -0,0 +1,7 @@ +"""Eckert number from specific heat capacity at constant pressure, 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(specific_heat_capacity_at_constant_pressure, characteristic_speed, temperature_difference): + return 1.0*characteristic_speed**2/(specific_heat_capacity_at_constant_pressure*temperature_difference) diff --git a/root/organs10/physics/specialists/solved-eckert-number/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-eckert-number/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-eckert-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-effective-area/organ_formula_v2.py b/root/organs10/physics/specialists/solved-effective-area/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-effective-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-effective-area/panel.json b/root/organs10/physics/specialists/solved-effective-area/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..6ee2898319619532282d6f5c99688aae30dc458f --- /dev/null +++ b/root/organs10/physics/specialists/solved-effective-area/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the effective area for wing area 10.81 and angle of attack 5.18564?", + "a": "4.92699590394377", + "valid": true, + "kind": "compute", + "expect": 4.92699590394377, + "dtype": "quantity", + "unit": "m^2", + "why": "effective area for [10.809962, 5.185641] (the system effective area (Wikidata Q107645914))" + }, + { + "q": "Tell me the effective area with wing area of 10.81 and angle of attack of 5.18564.", + "a": "4.92699590394377", + "valid": true, + "kind": "compute", + "expect": 4.92699590394377, + "dtype": "quantity", + "unit": "m^2", + "why": "effective area for [10.809962, 5.185641] (the system effective area (Wikidata Q107645914))" + }, + { + "q": "What is the effective area for wing area 173.394 and angle of attack 251.084?", + "a": "168.28376393075962", + "valid": true, + "kind": "compute", + "expect": 168.28376393075962, + "dtype": "quantity", + "unit": "m^2", + "why": "effective area for [173.393638, 251.084037] (the system effective area (Wikidata Q107645914))" + }, + { + "q": "Tell me the effective area with wing area of 173.394 and angle of attack of 251.084.", + "a": "168.28376393075962", + "valid": true, + "kind": "compute", + "expect": 168.28376393075962, + "dtype": "quantity", + "unit": "m^2", + "why": "effective area for [173.393638, 251.084037] (the system effective area (Wikidata Q107645914))" + }, + { + "q": "What is the effective area for wing area 1.64379 and angle of attack 252.709?", + "a": "0.30912868717485864", + "valid": true, + "kind": "compute", + "expect": 0.30912868717485864, + "dtype": "quantity", + "unit": "m^2", + "why": "effective area for [1.643795, 252.709024] (the system effective area (Wikidata Q107645914))" + }, + { + "q": "Tell me the effective area with wing area of 1.64379 and angle of attack of 252.709.", + "a": "0.30912868717485864", + "valid": true, + "kind": "compute", + "expect": 0.30912868717485864, + "dtype": "quantity", + "unit": "m^2", + "why": "effective area for [1.643795, 252.709024] (the system effective area (Wikidata Q107645914))" + }, + { + "q": "What is the effective area for wing area 1.16997 and angle of attack 1.14865?", + "a": "0.4793560222302466", + "valid": true, + "kind": "compute", + "expect": 0.4793560222302466, + "dtype": "quantity", + "unit": "m^2", + "why": "effective area for [1.169968, 1.148652] (the system effective area (Wikidata Q107645914))" + }, + { + "q": "Tell me the effective area with wing area of 1.16997 and angle of attack of 1.14865.", + "a": "0.4793560222302466", + "valid": true, + "kind": "compute", + "expect": 0.4793560222302466, + "dtype": "quantity", + "unit": "m^2", + "why": "effective area for [1.169968, 1.148652] (the system effective area (Wikidata Q107645914))" + }, + { + "q": "What is the effective area for wing area 86.0768 and angle of attack 111.804?", + "a": "23.589486717515324", + "valid": true, + "kind": "compute", + "expect": 23.589486717515324, + "dtype": "quantity", + "unit": "m^2", + "why": "effective area for [86.076802, 111.804143] (the system effective area (Wikidata Q107645914))" + }, + { + "q": "Tell me the effective area with wing area of 86.0768 and angle of attack of 111.804.", + "a": "23.589486717515324", + "valid": true, + "kind": "compute", + "expect": 23.589486717515324, + "dtype": "quantity", + "unit": "m^2", + "why": "effective area for [86.076802, 111.804143] (the system effective area (Wikidata Q107645914))" + }, + { + "q": "What is the effective area for wing area 0.710236 and angle of attack 1.45005?", + "a": "0.0855534089237365", + "valid": true, + "kind": "compute", + "expect": 0.0855534089237365, + "dtype": "quantity", + "unit": "m^2", + "why": "effective area for [0.710236, 1.450045] (the system effective area (Wikidata Q107645914))" + }, + { + "q": "Tell me the effective area with wing area of 0.710236 and angle of attack of 1.45005.", + "a": "0.0855534089237365", + "valid": true, + "kind": "compute", + "expect": 0.0855534089237365, + "dtype": "quantity", + "unit": "m^2", + "why": "effective area for [0.710236, 1.450045] (the system effective area (Wikidata Q107645914))" + }, + { + "q": "What is the effective area for wing area 10.81?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the angle of attack is not given" + }, + { + "q": "Tell me the effective area with wing area of 173.394.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the angle of attack is not given" + }, + { + "q": "What is the effective 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 effective 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 effective area with wing area of -1.64379 and angle of attack of 252.709.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative wing area: no example had one" + }, + { + "q": "Which effective 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 wing area is 2.69428 and the angle of attack is 48.921, what is the effective area?", + "a": "0.6045371696481707", + "valid": true, + "kind": "compute", + "expect": 0.6045371696481707, + "dtype": "quantity", + "unit": "m^2", + "why": "effective area for [2.69428, 48.920991] (the system effective area (Wikidata Q107645914))" + }, + { + "q": "Could you work out the effective area given wing area = 2.69428, angle of attack = 48.921?", + "a": "0.6045371696481707", + "valid": true, + "kind": "compute", + "expect": 0.6045371696481707, + "dtype": "quantity", + "unit": "m^2", + "why": "effective area for [2.69428, 48.920991] (the system effective area (Wikidata Q107645914))" + }, + { + "q": "I need the effective area for a wing area of 45.6157 and an angle of attack of 4.97131, please.", + "a": "11.679220127397198", + "valid": true, + "kind": "compute", + "expect": 11.679220127397198, + "dtype": "quantity", + "unit": "m^2", + "why": "effective area for [45.61567, 4.971307] (the system effective area (Wikidata Q107645914))" + }, + { + "q": "If the wing area is 45.6157 and the angle of attack is 4.97131, what is the effective area?", + "a": "11.679220127397198", + "valid": true, + "kind": "compute", + "expect": 11.679220127397198, + "dtype": "quantity", + "unit": "m^2", + "why": "effective area for [45.61567, 4.971307] (the system effective area (Wikidata Q107645914))" + }, + { + "q": "Could you work out the effective area given wing area = 41.6566, angle of attack = 13.8779?", + "a": "10.680910613904786", + "valid": true, + "kind": "compute", + "expect": 10.680910613904786, + "dtype": "quantity", + "unit": "m^2", + "why": "effective area for [41.656639, 13.877867] (the system effective area (Wikidata Q107645914))" + }, + { + "q": "I need the effective area for a wing area of 41.6566 and an angle of attack of 13.8779, please.", + "a": "10.680910613904786", + "valid": true, + "kind": "compute", + "expect": 10.680910613904786, + "dtype": "quantity", + "unit": "m^2", + "why": "effective area for [41.656639, 13.877867] (the system effective area (Wikidata Q107645914))" + }, + { + "q": "If the wing area is 186.27 and the angle of attack is 106.508, what is the effective area?", + "a": "177.5871768731654", + "valid": true, + "kind": "compute", + "expect": 177.5871768731654, + "dtype": "quantity", + "unit": "m^2", + "why": "effective area for [186.270057, 106.507617] (the system effective area (Wikidata Q107645914))" + }, + { + "q": "Could you work out the effective area given wing area = 186.27, angle of attack = 106.508?", + "a": "177.5871768731654", + "valid": true, + "kind": "compute", + "expect": 177.5871768731654, + "dtype": "quantity", + "unit": "m^2", + "why": "effective area for [186.270057, 106.507617] (the system effective area (Wikidata Q107645914))" + }, + { + "q": "I need the effective area for a wing area of 75.8534 and an angle of attack of 0.558623, please.", + "a": "64.3226241782015", + "valid": true, + "kind": "compute", + "expect": 64.3226241782015, + "dtype": "quantity", + "unit": "m^2", + "why": "effective area for [75.853435, 0.558623] (the system effective area (Wikidata Q107645914))" + }, + { + "q": "If the wing area is 75.8534 and the angle of attack is 0.558623, what is the effective area?", + "a": "64.3226241782015", + "valid": true, + "kind": "compute", + "expect": 64.3226241782015, + "dtype": "quantity", + "unit": "m^2", + "why": "effective area for [75.853435, 0.558623] (the system effective area (Wikidata Q107645914))" + }, + { + "q": "Could you work out the effective area given wing area = 169.843, angle of attack = 12.6647?", + "a": "169.02322312660473", + "valid": true, + "kind": "compute", + "expect": 169.02322312660473, + "dtype": "quantity", + "unit": "m^2", + "why": "effective area for [169.843153, 12.664671] (the system effective area (Wikidata Q107645914))" + }, + { + "q": "I need the effective area for a wing area of 169.843 and an angle of attack of 12.6647, please.", + "a": "169.02322312660473", + "valid": true, + "kind": "compute", + "expect": 169.02322312660473, + "dtype": "quantity", + "unit": "m^2", + "why": "effective area for [169.843153, 12.664671] (the system effective area (Wikidata Q107645914))" + }, + { + "q": "If the wing area is 2.69428, what is the effective area?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the angle of attack is not given" + }, + { + "q": "Could you work out the effective area given wing area = 45.6157?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the angle of attack is not given" + }, + { + "q": "Do you know the effective 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 effective 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 effective area given wing area = -41.6566, angle of attack = 13.8779?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative wing area: no example had one" + }, + { + "q": "What is a typical effective 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-effective-area/program.py b/root/organs10/physics/specialists/solved-effective-area/program.py new file mode 100644 index 0000000000000000000000000000000000000000..4f1c8dbe99ba4929a234a2505a8ca47f993969c1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-effective-area/program.py @@ -0,0 +1,7 @@ +"""effective area from wing area, angle of attack (effective area (Wikidata Q107645914)) +The maths organ (systems.py): solved from the system q0 = q1*cos (q2); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(wing_area, angle_of_attack): + return 1.0*wing_area*math.cos(1.0*angle_of_attack) diff --git a/root/organs10/physics/specialists/solved-effective-area/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-effective-area/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-effective-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-efficiency-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-efficiency-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-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-efficiency-2/panel.json b/root/organs10/physics/specialists/solved-efficiency-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..abd8744fab41f7b1c9ebe6271853512be0da88bf --- /dev/null +++ b/root/organs10/physics/specialists/solved-efficiency-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the efficiency for output power 107.961 and input power 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [107.960666, 59.4105] (the system Efficiency)" + }, + { + "q": "Tell me the efficiency with output power of 107.961 and input power of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [107.960666, 59.4105] (the system Efficiency)" + }, + { + "q": "What is the efficiency for output power 169.527 and input power 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [169.526958, 280.773702] (the system Efficiency)" + }, + { + "q": "Tell me the efficiency with output power of 169.527 and input power of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [169.526958, 280.773702] (the system Efficiency)" + }, + { + "q": "What is the efficiency for output power 1.91195 and input power 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [1.911952, 82.240072] (the system Efficiency)" + }, + { + "q": "Tell me the efficiency with output power of 1.91195 and input power of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [1.911952, 82.240072] (the system Efficiency)" + }, + { + "q": "What is the efficiency for output power 1.64379 and input power 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [1.643795, 252.709024] (the system Efficiency)" + }, + { + "q": "Tell me the efficiency with output power of 1.64379 and input power of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [1.643795, 252.709024] (the system Efficiency)" + }, + { + "q": "What is the efficiency for output power 18.3169 and input power 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [18.316867, 6.838598] (the system Efficiency)" + }, + { + "q": "Tell me the efficiency with output power of 18.3169 and input power of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [18.316867, 6.838598] (the system Efficiency)" + }, + { + "q": "What is the efficiency for output power 156.502 and input power 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [156.50181, 185.886273] (the system Efficiency)" + }, + { + "q": "Tell me the efficiency with output power of 156.502 and input power of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [156.50181, 185.886273] (the system Efficiency)" + }, + { + "q": "What is the efficiency for output power 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the input power is not given" + }, + { + "q": "Tell me the efficiency with output power of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the input power is not given" + }, + { + "q": "What is the efficiency 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 efficiency 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 efficiency with output power of -1.91195 and input power of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative output power: no example had one" + }, + { + "q": "Which efficiency is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the output power is 8.50728 and the input power is 17.7888, what is the efficiency?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [8.507283, 17.788778] (the system Efficiency)" + }, + { + "q": "Could you work out the efficiency given output power = 8.50728, input power = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [8.507283, 17.788778] (the system Efficiency)" + }, + { + "q": "I need the efficiency for an output power of 8.24951 and an input power of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [8.24951, 0.634021] (the system Efficiency)" + }, + { + "q": "If the output power is 8.24951 and the input power is 0.634021, what is the efficiency?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [8.24951, 0.634021] (the system Efficiency)" + }, + { + "q": "Could you work out the efficiency given output power = 0.318734, input power = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [0.318734, 9.566071] (the system Efficiency)" + }, + { + "q": "I need the efficiency for an output power of 0.318734 and an input power of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [0.318734, 9.566071] (the system Efficiency)" + }, + { + "q": "If the output power is 0.671697 and the input power is 14.2496, what is the efficiency?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [0.671697, 14.249569] (the system Efficiency)" + }, + { + "q": "Could you work out the efficiency given output power = 0.671697, input power = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [0.671697, 14.249569] (the system Efficiency)" + }, + { + "q": "I need the efficiency for an output power of 21.7931 and an input power of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [21.793126, 29.510609] (the system Efficiency)" + }, + { + "q": "If the output power is 21.7931 and the input power is 29.5106, what is the efficiency?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [21.793126, 29.510609] (the system Efficiency)" + }, + { + "q": "Could you work out the efficiency given output power = 153.15, input power = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [153.149965, 18.595315] (the system Efficiency)" + }, + { + "q": "I need the efficiency for an output power of 153.15 and an input power of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [153.149965, 18.595315] (the system Efficiency)" + }, + { + "q": "If the output power is 8.50728, what is the efficiency?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the input power is not given" + }, + { + "q": "Could you work out the efficiency given output power = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the input power is not given" + }, + { + "q": "Do you know the efficiency 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 efficiency 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 efficiency given output power = -0.318734, input power = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative output power: no example had one" + }, + { + "q": "What is a typical efficiency?", + "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-efficiency-2/program.py b/root/organs10/physics/specialists/solved-efficiency-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..3beac3ef82676c5bb6cebd5e8a11f542c8bf03c9 --- /dev/null +++ b/root/organs10/physics/specialists/solved-efficiency-2/program.py @@ -0,0 +1,7 @@ +"""efficiency from output power, input power (Efficiency) +The maths organ (systems.py): solved from the system O = e*I; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(output_power, input_power): + return 1.0*output_power/input_power diff --git a/root/organs10/physics/specialists/solved-efficiency-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-efficiency-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-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-efficiency-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-efficiency-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-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-efficiency-3/panel.json b/root/organs10/physics/specialists/solved-efficiency-3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..97c40169b1c56147399e89db5bf3383bf11c8ab6 --- /dev/null +++ b/root/organs10/physics/specialists/solved-efficiency-3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the efficiency for power 107.961, swept area 59.4105 and wind speed 5.77717?", + "a": "0.01538693167202734", + "valid": true, + "kind": "compute", + "expect": 0.01538693167202734, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [107.960666, 59.4105, 5.777165] (the system Wind power)" + }, + { + "q": "Tell me the efficiency with power of 107.961, swept area of 59.4105 and wind speed of 5.77717.", + "a": "0.01538693167202734", + "valid": true, + "kind": "compute", + "expect": 0.01538693167202734, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [107.960666, 59.4105, 5.777165] (the system Wind power)" + }, + { + "q": "What is the efficiency for power 85.2416, swept area 160.878 and wind speed 2.69428?", + "a": "0.04423027501767519", + "valid": true, + "kind": "compute", + "expect": 0.04423027501767519, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [85.241623, 160.878442, 2.69428] (the system Wind power)" + }, + { + "q": "Tell me the efficiency with power of 85.2416, swept area of 160.878 and wind speed of 2.69428.", + "a": "0.04423027501767519", + "valid": true, + "kind": "compute", + "expect": 0.04423027501767519, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [85.241623, 160.878442, 2.69428] (the system Wind power)" + }, + { + "q": "What is the efficiency for power 45.6157, swept area 4.97131 and wind speed 94.305?", + "a": "1.7862101182749543e-05", + "valid": true, + "kind": "compute", + "expect": 1.7862101182749543e-05, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [45.61567, 4.971307, 94.305046] (the system Wind power)" + }, + { + "q": "Tell me the efficiency with power of 45.6157, swept area of 4.97131 and wind speed of 94.305.", + "a": "1.7862101182749543e-05", + "valid": true, + "kind": "compute", + "expect": 1.7862101182749543e-05, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [45.61567, 4.971307, 94.305046] (the system Wind power)" + }, + { + "q": "What is the efficiency for power 10.5638, swept area 198.839 and wind speed 0.671697?", + "a": "0.2862158948856837", + "valid": true, + "kind": "compute", + "expect": 0.2862158948856837, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [10.563843, 198.838955, 0.671697] (the system Wind power)" + }, + { + "q": "Tell me the efficiency with power of 10.5638, swept area of 198.839 and wind speed of 0.671697.", + "a": "0.2862158948856837", + "valid": true, + "kind": "compute", + "expect": 0.2862158948856837, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [10.563843, 198.838955, 0.671697] (the system Wind power)" + }, + { + "q": "What is the efficiency for power 21.7931, swept area 29.5106 and wind speed 8.50341?", + "a": "0.001960904086183933", + "valid": true, + "kind": "compute", + "expect": 0.001960904086183933, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [21.793126, 29.510609, 8.503407] (the system Wind power)" + }, + { + "q": "Tell me the efficiency with power of 21.7931, swept area of 29.5106 and wind speed of 8.50341.", + "a": "0.001960904086183933", + "valid": true, + "kind": "compute", + "expect": 0.001960904086183933, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [21.793126, 29.510609, 8.503407] (the system Wind power)" + }, + { + "q": "What is the efficiency for power 223.509, swept area 17.3414 and wind speed 7.10705?", + "a": "0.05861861253373526", + "valid": true, + "kind": "compute", + "expect": 0.05861861253373526, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [223.508796, 17.341435, 7.107049] (the system Wind power)" + }, + { + "q": "Tell me the efficiency with power of 223.509, swept area of 17.3414 and wind speed of 7.10705.", + "a": "0.05861861253373526", + "valid": true, + "kind": "compute", + "expect": 0.05861861253373526, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [223.508796, 17.341435, 7.107049] (the system Wind power)" + }, + { + "q": "What is the efficiency for power 107.961 and swept area 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the wind speed is not given" + }, + { + "q": "Tell me the efficiency with power of 85.2416 and swept area of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the wind speed is not given" + }, + { + "q": "What is the efficiency 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 efficiency 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 efficiency with power of -45.6157, swept area of 4.97131 and wind speed of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative power: no example had one" + }, + { + "q": "Which efficiency is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the power is 17.7888, swept area is 167.625 and the wind speed is 10.3291, what is the efficiency?", + "a": "0.00015722429911793392", + "valid": true, + "kind": "compute", + "expect": 0.00015722429911793392, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [17.788778, 167.624948, 10.329054] (the system Wind power)" + }, + { + "q": "Could you work out the efficiency given power = 17.7888, swept area = 167.625, wind speed = 10.3291?", + "a": "0.00015722429911793392", + "valid": true, + "kind": "compute", + "expect": 0.00015722429911793392, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [17.788778, 167.624948, 10.329054] (the system Wind power)" + }, + { + "q": "I need the efficiency for a power of 21.511, a swept area of 173.394 and a wind speed of 251.084, please.", + "a": "1.2795709083809995e-08", + "valid": true, + "kind": "compute", + "expect": 1.2795709083809995e-08, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [21.511016, 173.393638, 251.084037] (the system Wind power)" + }, + { + "q": "If the power is 21.511, swept area is 173.394 and the wind speed is 251.084, what is the efficiency?", + "a": "1.2795709083809995e-08", + "valid": true, + "kind": "compute", + "expect": 1.2795709083809995e-08, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [21.511016, 173.393638, 251.084037] (the system Wind power)" + }, + { + "q": "Could you work out the efficiency given power = 129.245, swept area = 1.18359, wind speed = 15.9416?", + "a": "0.0440057739157613", + "valid": true, + "kind": "compute", + "expect": 0.0440057739157613, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [129.244984, 1.18359, 15.941612] (the system Wind power)" + }, + { + "q": "I need the efficiency for a power of 129.245, a swept area of 1.18359 and a wind speed of 15.9416, please.", + "a": "0.0440057739157613", + "valid": true, + "kind": "compute", + "expect": 0.0440057739157613, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [129.244984, 1.18359, 15.941612] (the system Wind power)" + }, + { + "q": "If the power is 20.3966, swept area is 18.3169 and the wind speed is 6.8386, what is the efficiency?", + "a": "0.0056845797262202805", + "valid": true, + "kind": "compute", + "expect": 0.0056845797262202805, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [20.396558, 18.316867, 6.838598] (the system Wind power)" + }, + { + "q": "Could you work out the efficiency given power = 20.3966, swept area = 18.3169, wind speed = 6.8386?", + "a": "0.0056845797262202805", + "valid": true, + "kind": "compute", + "expect": 0.0056845797262202805, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [20.396558, 18.316867, 6.838598] (the system Wind power)" + }, + { + "q": "I need the efficiency for a power of 185.886, a swept area of 13.2373 and a wind speed of 4.71941, please.", + "a": "0.2181107140103613", + "valid": true, + "kind": "compute", + "expect": 0.2181107140103613, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [185.886273, 13.237311, 4.719415] (the system Wind power)" + }, + { + "q": "If the power is 185.886, swept area is 13.2373 and the wind speed is 4.71941, what is the efficiency?", + "a": "0.2181107140103613", + "valid": true, + "kind": "compute", + "expect": 0.2181107140103613, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [185.886273, 13.237311, 4.719415] (the system Wind power)" + }, + { + "q": "Could you work out the efficiency given power = 9.1061, swept area = 24.5721, wind speed = 108.451?", + "a": "4.743362442855406e-07", + "valid": true, + "kind": "compute", + "expect": 4.743362442855406e-07, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [9.106104, 24.572086, 108.450792] (the system Wind power)" + }, + { + "q": "I need the efficiency for a power of 9.1061, a swept area of 24.5721 and a wind speed of 108.451, please.", + "a": "4.743362442855406e-07", + "valid": true, + "kind": "compute", + "expect": 4.743362442855406e-07, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [9.106104, 24.572086, 108.450792] (the system Wind power)" + }, + { + "q": "If the power is 17.7888 and the swept area is 167.625, what is the efficiency?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the wind speed is not given" + }, + { + "q": "Could you work out the efficiency given power = 21.511, swept area = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the wind speed is not given" + }, + { + "q": "Do you know the efficiency 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 efficiency 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 efficiency given power = -129.245, swept area = 1.18359, wind speed = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative power: no example had one" + }, + { + "q": "What is a typical efficiency?", + "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-efficiency-3/program.py b/root/organs10/physics/specialists/solved-efficiency-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..3ab303db73b2a5c20b0406952cf02ba5f938c82a --- /dev/null +++ b/root/organs10/physics/specialists/solved-efficiency-3/program.py @@ -0,0 +1,7 @@ +"""efficiency from power, swept area, wind speed (Wind power) +The maths organ (systems.py): solved from the system P = r*A*v**3*e/2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(power, swept_area, wind_speed): + return 1.63265306122449*power/(swept_area*wind_speed**3) diff --git a/root/organs10/physics/specialists/solved-efficiency-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-efficiency-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-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-efficiency-4/organ_formula_v2.py b/root/organs10/physics/specialists/solved-efficiency-4/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-efficiency-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-efficiency-4/panel.json b/root/organs10/physics/specialists/solved-efficiency-4/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..7a198ad98fbac0b431d5fb1ea7d8a03059901eae --- /dev/null +++ b/root/organs10/physics/specialists/solved-efficiency-4/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the efficiency for power 107.961, irradiance 59.4105 and panel area 5.77717?", + "a": "0.31454848897160476", + "valid": true, + "kind": "compute", + "expect": 0.31454848897160476, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [107.960666, 59.4105, 5.777165] (the system Solar power)" + }, + { + "q": "Tell me the efficiency with power of 107.961, irradiance of 59.4105 and panel area of 5.77717.", + "a": "0.31454848897160476", + "valid": true, + "kind": "compute", + "expect": 0.31454848897160476, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [107.960666, 59.4105, 5.777165] (the system Solar power)" + }, + { + "q": "What is the efficiency for power 85.2416, irradiance 160.878 and panel area 2.69428?", + "a": "0.19665778745152376", + "valid": true, + "kind": "compute", + "expect": 0.19665778745152376, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [85.241623, 160.878442, 2.69428] (the system Solar power)" + }, + { + "q": "Tell me the efficiency with power of 85.2416, irradiance of 160.878 and panel area of 2.69428.", + "a": "0.19665778745152376", + "valid": true, + "kind": "compute", + "expect": 0.19665778745152376, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [85.241623, 160.878442, 2.69428] (the system Solar power)" + }, + { + "q": "What is the efficiency for power 45.6157, irradiance 4.97131 and panel area 94.305?", + "a": "0.09729902784967777", + "valid": true, + "kind": "compute", + "expect": 0.09729902784967777, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [45.61567, 4.971307, 94.305046] (the system Solar power)" + }, + { + "q": "Tell me the efficiency with power of 45.6157, irradiance of 4.97131 and panel area of 94.305.", + "a": "0.09729902784967777", + "valid": true, + "kind": "compute", + "expect": 0.09729902784967777, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [45.61567, 4.971307, 94.305046] (the system Solar power)" + }, + { + "q": "What is the efficiency for power 10.5638, irradiance 198.839 and panel area 0.671697?", + "a": "0.07909461512056756", + "valid": true, + "kind": "compute", + "expect": 0.07909461512056756, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [10.563843, 198.838955, 0.671697] (the system Solar power)" + }, + { + "q": "Tell me the efficiency with power of 10.5638, irradiance of 198.839 and panel area of 0.671697.", + "a": "0.07909461512056756", + "valid": true, + "kind": "compute", + "expect": 0.07909461512056756, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [10.563843, 198.838955, 0.671697] (the system Solar power)" + }, + { + "q": "What is the efficiency for power 21.7931, irradiance 29.5106 and panel area 8.50341?", + "a": "0.08684571530973768", + "valid": true, + "kind": "compute", + "expect": 0.08684571530973768, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [21.793126, 29.510609, 8.503407] (the system Solar power)" + }, + { + "q": "Tell me the efficiency with power of 21.7931, irradiance of 29.5106 and panel area of 8.50341.", + "a": "0.08684571530973768", + "valid": true, + "kind": "compute", + "expect": 0.08684571530973768, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [21.793126, 29.510609, 8.503407] (the system Solar power)" + }, + { + "q": "What is the efficiency for power 223.509, irradiance 17.3414 and panel area 7.10705?", + "a": "1.813511306470856", + "valid": true, + "kind": "compute", + "expect": 1.813511306470856, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [223.508796, 17.341435, 7.107049] (the system Solar power)" + }, + { + "q": "Tell me the efficiency with power of 223.509, irradiance of 17.3414 and panel area of 7.10705.", + "a": "1.813511306470856", + "valid": true, + "kind": "compute", + "expect": 1.813511306470856, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [223.508796, 17.341435, 7.107049] (the system Solar power)" + }, + { + "q": "What is the efficiency for power 107.961 and irradiance 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the panel area is not given" + }, + { + "q": "Tell me the efficiency with power of 85.2416 and irradiance of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the panel area is not given" + }, + { + "q": "What is the efficiency 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 efficiency 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 efficiency with power of -45.6157, irradiance of 4.97131 and panel area of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative power: no example had one" + }, + { + "q": "Which efficiency is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the power is 17.7888, irradiance is 167.625 and the panel area is 10.3291, what is the efficiency?", + "a": "0.010274173498684694", + "valid": true, + "kind": "compute", + "expect": 0.010274173498684694, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [17.788778, 167.624948, 10.329054] (the system Solar power)" + }, + { + "q": "Could you work out the efficiency given power = 17.7888, irradiance = 167.625, panel area = 10.3291?", + "a": "0.010274173498684694", + "valid": true, + "kind": "compute", + "expect": 0.010274173498684694, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [17.788778, 167.624948, 10.329054] (the system Solar power)" + }, + { + "q": "I need the efficiency for a power of 21.511, an irradiance of 173.394 and a panel area of 251.084, please.", + "a": "0.000494092950151496", + "valid": true, + "kind": "compute", + "expect": 0.000494092950151496, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [21.511016, 173.393638, 251.084037] (the system Solar power)" + }, + { + "q": "If the power is 21.511, irradiance is 173.394 and the panel area is 251.084, what is the efficiency?", + "a": "0.000494092950151496", + "valid": true, + "kind": "compute", + "expect": 0.000494092950151496, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [21.511016, 173.393638, 251.084037] (the system Solar power)" + }, + { + "q": "Could you work out the efficiency given power = 129.245, irradiance = 1.18359, panel area = 15.9416?", + "a": "6.849836414751194", + "valid": true, + "kind": "compute", + "expect": 6.849836414751194, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [129.244984, 1.18359, 15.941612] (the system Solar power)" + }, + { + "q": "I need the efficiency for a power of 129.245, an irradiance of 1.18359 and a panel area of 15.9416, please.", + "a": "6.849836414751194", + "valid": true, + "kind": "compute", + "expect": 6.849836414751194, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [129.244984, 1.18359, 15.941612] (the system Solar power)" + }, + { + "q": "If the power is 20.3966, irradiance is 18.3169 and the panel area is 6.8386, what is the efficiency?", + "a": "0.16283157083473473", + "valid": true, + "kind": "compute", + "expect": 0.16283157083473473, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [20.396558, 18.316867, 6.838598] (the system Solar power)" + }, + { + "q": "Could you work out the efficiency given power = 20.3966, irradiance = 18.3169, panel area = 6.8386?", + "a": "0.16283157083473473", + "valid": true, + "kind": "compute", + "expect": 0.16283157083473473, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [20.396558, 18.316867, 6.838598] (the system Solar power)" + }, + { + "q": "I need the efficiency for a power of 185.886, an irradiance of 13.2373 and a panel area of 4.71941, please.", + "a": "2.975496158535072", + "valid": true, + "kind": "compute", + "expect": 2.975496158535072, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [185.886273, 13.237311, 4.719415] (the system Solar power)" + }, + { + "q": "If the power is 185.886, irradiance is 13.2373 and the panel area is 4.71941, what is the efficiency?", + "a": "2.975496158535072", + "valid": true, + "kind": "compute", + "expect": 2.975496158535072, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [185.886273, 13.237311, 4.719415] (the system Solar power)" + }, + { + "q": "Could you work out the efficiency given power = 9.1061, irradiance = 24.5721, panel area = 108.451?", + "a": "0.003417101331511669", + "valid": true, + "kind": "compute", + "expect": 0.003417101331511669, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [9.106104, 24.572086, 108.450792] (the system Solar power)" + }, + { + "q": "I need the efficiency for a power of 9.1061, an irradiance of 24.5721 and a panel area of 108.451, please.", + "a": "0.003417101331511669", + "valid": true, + "kind": "compute", + "expect": 0.003417101331511669, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [9.106104, 24.572086, 108.450792] (the system Solar power)" + }, + { + "q": "If the power is 17.7888 and the irradiance is 167.625, what is the efficiency?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the panel area is not given" + }, + { + "q": "Could you work out the efficiency given power = 21.511, irradiance = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the panel area is not given" + }, + { + "q": "Do you know the efficiency 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 efficiency 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 efficiency given power = -129.245, irradiance = 1.18359, panel area = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative power: no example had one" + }, + { + "q": "What is a typical efficiency?", + "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-efficiency-4/program.py b/root/organs10/physics/specialists/solved-efficiency-4/program.py new file mode 100644 index 0000000000000000000000000000000000000000..31a1efc1cad82b64237bcc2097084e5a22f0b696 --- /dev/null +++ b/root/organs10/physics/specialists/solved-efficiency-4/program.py @@ -0,0 +1,7 @@ +"""efficiency from power, irradiance, panel area (Solar power) +The maths organ (systems.py): solved from the system P = G*A*e; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(power, irradiance, panel_area): + return 1.0*power/(irradiance*panel_area) diff --git a/root/organs10/physics/specialists/solved-efficiency-4/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-efficiency-4/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-efficiency-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-efficiency/organ_formula_v2.py b/root/organs10/physics/specialists/solved-efficiency/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-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-efficiency/panel.json b/root/organs10/physics/specialists/solved-efficiency/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..3a8325ef0f14fa3f9371fd7d87f205a889ebb91a --- /dev/null +++ b/root/organs10/physics/specialists/solved-efficiency/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the efficiency for power 107.961, flow 59.4105 and head 5.77717?", + "a": "3.2075019397205495e-05", + "valid": true, + "kind": "compute", + "expect": 3.2075019397205495e-05, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [107.960666, 59.4105, 5.777165] (the system Hydro power)" + }, + { + "q": "Tell me the efficiency with power of 107.961, flow of 59.4105 and head of 5.77717.", + "a": "3.2075019397205495e-05", + "valid": true, + "kind": "compute", + "expect": 3.2075019397205495e-05, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [107.960666, 59.4105, 5.777165] (the system Hydro power)" + }, + { + "q": "What is the efficiency for power 85.2416, flow 160.878 and head 2.69428?", + "a": "2.005351342726865e-05", + "valid": true, + "kind": "compute", + "expect": 2.005351342726865e-05, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [85.241623, 160.878442, 2.69428] (the system Hydro power)" + }, + { + "q": "Tell me the efficiency with power of 85.2416, flow of 160.878 and head of 2.69428.", + "a": "2.005351342726865e-05", + "valid": true, + "kind": "compute", + "expect": 2.005351342726865e-05, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [85.241623, 160.878442, 2.69428] (the system Hydro power)" + }, + { + "q": "What is the efficiency for power 45.6157, flow 4.97131 and head 94.305?", + "a": "9.921739620530755e-06", + "valid": true, + "kind": "compute", + "expect": 9.921739620530755e-06, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [45.61567, 4.971307, 94.305046] (the system Hydro power)" + }, + { + "q": "Tell me the efficiency with power of 45.6157, flow of 4.97131 and head of 94.305.", + "a": "9.921739620530755e-06", + "valid": true, + "kind": "compute", + "expect": 9.921739620530755e-06, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [45.61567, 4.971307, 94.305046] (the system Hydro power)" + }, + { + "q": "What is the efficiency for power 10.5638, flow 198.839 and head 0.671697?", + "a": "8.065406139769209e-06", + "valid": true, + "kind": "compute", + "expect": 8.065406139769209e-06, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [10.563843, 198.838955, 0.671697] (the system Hydro power)" + }, + { + "q": "Tell me the efficiency with power of 10.5638, flow of 198.839 and head of 0.671697.", + "a": "8.065406139769209e-06", + "valid": true, + "kind": "compute", + "expect": 8.065406139769209e-06, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [10.563843, 198.838955, 0.671697] (the system Hydro power)" + }, + { + "q": "What is the efficiency for power 21.7931, flow 29.5106 and head 8.50341?", + "a": "8.855798392900516e-06", + "valid": true, + "kind": "compute", + "expect": 8.855798392900516e-06, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [21.793126, 29.510609, 8.503407] (the system Hydro power)" + }, + { + "q": "Tell me the efficiency with power of 21.7931, flow of 29.5106 and head of 8.50341.", + "a": "8.855798392900516e-06", + "valid": true, + "kind": "compute", + "expect": 8.855798392900516e-06, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [21.793126, 29.510609, 8.503407] (the system Hydro power)" + }, + { + "q": "What is the efficiency for power 223.509, flow 17.3414 and head 7.10705?", + "a": "0.00018492668816271195", + "valid": true, + "kind": "compute", + "expect": 0.00018492668816271195, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [223.508796, 17.341435, 7.107049] (the system Hydro power)" + }, + { + "q": "Tell me the efficiency with power of 223.509, flow of 17.3414 and head of 7.10705.", + "a": "0.00018492668816271195", + "valid": true, + "kind": "compute", + "expect": 0.00018492668816271195, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [223.508796, 17.341435, 7.107049] (the system Hydro power)" + }, + { + "q": "What is the efficiency for power 107.961 and flow 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the head is not given" + }, + { + "q": "Tell me the efficiency with power of 85.2416 and flow of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the head is not given" + }, + { + "q": "What is the efficiency 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 efficiency 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 efficiency with power of -45.6157, flow of 4.97131 and head of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative power: no example had one" + }, + { + "q": "Which efficiency is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the power is 17.7888, flow is 167.625 and the head is 10.3291, what is the efficiency?", + "a": "1.0476741291556965e-06", + "valid": true, + "kind": "compute", + "expect": 1.0476741291556965e-06, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [17.788778, 167.624948, 10.329054] (the system Hydro power)" + }, + { + "q": "Could you work out the efficiency given power = 17.7888, flow = 167.625, head = 10.3291?", + "a": "1.0476741291556965e-06", + "valid": true, + "kind": "compute", + "expect": 1.0476741291556965e-06, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [17.788778, 167.624948, 10.329054] (the system Hydro power)" + }, + { + "q": "I need the efficiency for a power of 21.511, a flow of 173.394 and a head of 251.084, please.", + "a": "5.038345919875767e-08", + "valid": true, + "kind": "compute", + "expect": 5.038345919875767e-08, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [21.511016, 173.393638, 251.084037] (the system Hydro power)" + }, + { + "q": "If the power is 21.511, flow is 173.394 and the head is 251.084, what is the efficiency?", + "a": "5.038345919875767e-08", + "valid": true, + "kind": "compute", + "expect": 5.038345919875767e-08, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [21.511016, 173.393638, 251.084037] (the system Hydro power)" + }, + { + "q": "Could you work out the efficiency given power = 129.245, flow = 1.18359, head = 15.9416?", + "a": "0.0006984889248368409", + "valid": true, + "kind": "compute", + "expect": 0.0006984889248368409, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [129.244984, 1.18359, 15.941612] (the system Hydro power)" + }, + { + "q": "I need the efficiency for a power of 129.245, a flow of 1.18359 and a head of 15.9416, please.", + "a": "0.0006984889248368409", + "valid": true, + "kind": "compute", + "expect": 0.0006984889248368409, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [129.244984, 1.18359, 15.941612] (the system Hydro power)" + }, + { + "q": "If the power is 20.3966, flow is 18.3169 and the head is 6.8386, what is the efficiency?", + "a": "1.6604199276484327e-05", + "valid": true, + "kind": "compute", + "expect": 1.6604199276484327e-05, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [20.396558, 18.316867, 6.838598] (the system Hydro power)" + }, + { + "q": "Could you work out the efficiency given power = 20.3966, flow = 18.3169, head = 6.8386?", + "a": "1.6604199276484327e-05", + "valid": true, + "kind": "compute", + "expect": 1.6604199276484327e-05, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [20.396558, 18.316867, 6.838598] (the system Hydro power)" + }, + { + "q": "I need the efficiency for a power of 185.886, a flow of 13.2373 and a head of 4.71941, please.", + "a": "0.00030341616745117617", + "valid": true, + "kind": "compute", + "expect": 0.00030341616745117617, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [185.886273, 13.237311, 4.719415] (the system Hydro power)" + }, + { + "q": "If the power is 185.886, flow is 13.2373 and the head is 4.71941, what is the efficiency?", + "a": "0.00030341616745117617", + "valid": true, + "kind": "compute", + "expect": 0.00030341616745117617, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [185.886273, 13.237311, 4.719415] (the system Hydro power)" + }, + { + "q": "Could you work out the efficiency given power = 9.1061, flow = 24.5721, head = 108.451?", + "a": "3.4844736291309213e-07", + "valid": true, + "kind": "compute", + "expect": 3.4844736291309213e-07, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [9.106104, 24.572086, 108.450792] (the system Hydro power)" + }, + { + "q": "I need the efficiency for a power of 9.1061, a flow of 24.5721 and a head of 108.451, please.", + "a": "3.4844736291309213e-07", + "valid": true, + "kind": "compute", + "expect": 3.4844736291309213e-07, + "dtype": "quantity", + "unit": "1", + "why": "efficiency for [9.106104, 24.572086, 108.450792] (the system Hydro power)" + }, + { + "q": "If the power is 17.7888 and the flow is 167.625, what is the efficiency?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the head is not given" + }, + { + "q": "Could you work out the efficiency given power = 21.511, flow = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the head is not given" + }, + { + "q": "Do you know the efficiency 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 efficiency 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 efficiency given power = -129.245, flow = 1.18359, head = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative power: no example had one" + }, + { + "q": "What is a typical efficiency?", + "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-efficiency/program.py b/root/organs10/physics/specialists/solved-efficiency/program.py new file mode 100644 index 0000000000000000000000000000000000000000..aebd7c350f9c4e252f40065fbe4e3c247b217f53 --- /dev/null +++ b/root/organs10/physics/specialists/solved-efficiency/program.py @@ -0,0 +1,7 @@ +"""efficiency from power, flow, head (Hydro power) +The maths organ (systems.py): solved from the system P = e*r*g*Q*H; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(power, flow, head): + return 0.000101971621297793*power/(flow*head) diff --git a/root/organs10/physics/specialists/solved-efficiency/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-efficiency/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-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-elasticity-number/organ_formula_v2.py b/root/organs10/physics/specialists/solved-elasticity-number/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-elasticity-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-elasticity-number/panel.json b/root/organs10/physics/specialists/solved-elasticity-number/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..b99406e469569f959a77d74cc7099c7adcea5f48 --- /dev/null +++ b/root/organs10/physics/specialists/solved-elasticity-number/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the elasticity number for relaxation time 107.961, kinematic viscosity 59.4105 and pipe radius 5.77717?", + "a": "192.17605698754332", + "valid": true, + "kind": "compute", + "expect": 192.17605698754332, + "dtype": "quantity", + "unit": "1", + "why": "elasticity number for [107.960666, 59.4105, 5.777165] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "Tell me the elasticity number with relaxation time of 107.961, kinematic viscosity of 59.4105 and pipe radius of 5.77717.", + "a": "192.17605698754332", + "valid": true, + "kind": "compute", + "expect": 192.17605698754332, + "dtype": "quantity", + "unit": "1", + "why": "elasticity number for [107.960666, 59.4105, 5.777165] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "What is the elasticity number for relaxation time 85.2416, kinematic viscosity 160.878 and pipe radius 2.69428?", + "a": "1889.139998571463", + "valid": true, + "kind": "compute", + "expect": 1889.139998571463, + "dtype": "quantity", + "unit": "1", + "why": "elasticity number for [85.241623, 160.878442, 2.69428] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "Tell me the elasticity number with relaxation time of 85.2416, kinematic viscosity of 160.878 and pipe radius of 2.69428.", + "a": "1889.139998571463", + "valid": true, + "kind": "compute", + "expect": 1889.139998571463, + "dtype": "quantity", + "unit": "1", + "why": "elasticity number for [85.241623, 160.878442, 2.69428] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "What is the elasticity number for relaxation time 45.6157, kinematic viscosity 4.97131 and pipe radius 94.305?", + "a": "0.02549851115812748", + "valid": true, + "kind": "compute", + "expect": 0.02549851115812748, + "dtype": "quantity", + "unit": "1", + "why": "elasticity number for [45.61567, 4.971307, 94.305046] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "Tell me the elasticity number with relaxation time of 45.6157, kinematic viscosity of 4.97131 and pipe radius of 94.305.", + "a": "0.02549851115812748", + "valid": true, + "kind": "compute", + "expect": 0.02549851115812748, + "dtype": "quantity", + "unit": "1", + "why": "elasticity number for [45.61567, 4.971307, 94.305046] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "What is the elasticity number for relaxation time 10.5638, kinematic viscosity 198.839 and pipe radius 0.671697?", + "a": "4655.607130241956", + "valid": true, + "kind": "compute", + "expect": 4655.607130241956, + "dtype": "quantity", + "unit": "1", + "why": "elasticity number for [10.563843, 198.838955, 0.671697] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "Tell me the elasticity number with relaxation time of 10.5638, kinematic viscosity of 198.839 and pipe radius of 0.671697.", + "a": "4655.607130241956", + "valid": true, + "kind": "compute", + "expect": 4655.607130241956, + "dtype": "quantity", + "unit": "1", + "why": "elasticity number for [10.563843, 198.838955, 0.671697] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "What is the elasticity number for relaxation time 21.7931, kinematic viscosity 29.5106 and pipe radius 8.50341?", + "a": "8.894299603447644", + "valid": true, + "kind": "compute", + "expect": 8.894299603447644, + "dtype": "quantity", + "unit": "1", + "why": "elasticity number for [21.793126, 29.510609, 8.503407] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "Tell me the elasticity number with relaxation time of 21.7931, kinematic viscosity of 29.5106 and pipe radius of 8.50341.", + "a": "8.894299603447644", + "valid": true, + "kind": "compute", + "expect": 8.894299603447644, + "dtype": "quantity", + "unit": "1", + "why": "elasticity number for [21.793126, 29.510609, 8.503407] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "What is the elasticity number for relaxation time 223.509, kinematic viscosity 17.3414 and pipe radius 7.10705?", + "a": "76.73632777404201", + "valid": true, + "kind": "compute", + "expect": 76.73632777404201, + "dtype": "quantity", + "unit": "1", + "why": "elasticity number for [223.508796, 17.341435, 7.107049] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "Tell me the elasticity number with relaxation time of 223.509, kinematic viscosity of 17.3414 and pipe radius of 7.10705.", + "a": "76.73632777404201", + "valid": true, + "kind": "compute", + "expect": 76.73632777404201, + "dtype": "quantity", + "unit": "1", + "why": "elasticity number for [223.508796, 17.341435, 7.107049] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "What is the elasticity number for relaxation time 107.961 and kinematic viscosity 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the pipe radius is not given" + }, + { + "q": "Tell me the elasticity number with relaxation time of 85.2416 and kinematic viscosity of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the pipe radius is not given" + }, + { + "q": "What is the elasticity 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 elasticity 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 elasticity number with relaxation time of -45.6157, kinematic viscosity of 4.97131 and pipe radius of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative relaxation time: no example had one" + }, + { + "q": "Which elasticity 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 relaxation time is 17.7888, kinematic viscosity is 167.625 and the pipe radius is 10.3291, what is the elasticity number?", + "a": "27.948830448798585", + "valid": true, + "kind": "compute", + "expect": 27.948830448798585, + "dtype": "quantity", + "unit": "1", + "why": "elasticity number for [17.788778, 167.624948, 10.329054] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "Could you work out the elasticity number given relaxation time = 17.7888, kinematic viscosity = 167.625, pipe radius = 10.3291?", + "a": "27.948830448798585", + "valid": true, + "kind": "compute", + "expect": 27.948830448798585, + "dtype": "quantity", + "unit": "1", + "why": "elasticity number for [17.788778, 167.624948, 10.329054] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "I need the elasticity number for a relaxation time of 21.511, a kinematic viscosity of 173.394 and a pipe radius of 251.084, please.", + "a": "0.059163774543084434", + "valid": true, + "kind": "compute", + "expect": 0.059163774543084434, + "dtype": "quantity", + "unit": "1", + "why": "elasticity number for [21.511016, 173.393638, 251.084037] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "If the relaxation time is 21.511, kinematic viscosity is 173.394 and the pipe radius is 251.084, what is the elasticity number?", + "a": "0.059163774543084434", + "valid": true, + "kind": "compute", + "expect": 0.059163774543084434, + "dtype": "quantity", + "unit": "1", + "why": "elasticity number for [21.511016, 173.393638, 251.084037] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "Could you work out the elasticity number given relaxation time = 129.245, kinematic viscosity = 1.18359, pipe radius = 15.9416?", + "a": "0.6019362898805625", + "valid": true, + "kind": "compute", + "expect": 0.6019362898805625, + "dtype": "quantity", + "unit": "1", + "why": "elasticity number for [129.244984, 1.18359, 15.941612] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "I need the elasticity number for a relaxation time of 129.245, a kinematic viscosity of 1.18359 and a pipe radius of 15.9416, please.", + "a": "0.6019362898805625", + "valid": true, + "kind": "compute", + "expect": 0.6019362898805625, + "dtype": "quantity", + "unit": "1", + "why": "elasticity number for [129.244984, 1.18359, 15.941612] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "If the relaxation time is 20.3966, kinematic viscosity is 18.3169 and the pipe radius is 6.8386, what is the elasticity number?", + "a": "7.988659649060692", + "valid": true, + "kind": "compute", + "expect": 7.988659649060692, + "dtype": "quantity", + "unit": "1", + "why": "elasticity number for [20.396558, 18.316867, 6.838598] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "Could you work out the elasticity number given relaxation time = 20.3966, kinematic viscosity = 18.3169, pipe radius = 6.8386?", + "a": "7.988659649060692", + "valid": true, + "kind": "compute", + "expect": 7.988659649060692, + "dtype": "quantity", + "unit": "1", + "why": "elasticity number for [20.396558, 18.316867, 6.838598] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "I need the elasticity number for a relaxation time of 185.886, a kinematic viscosity of 13.2373 and a pipe radius of 4.71941, please.", + "a": "110.47672537422135", + "valid": true, + "kind": "compute", + "expect": 110.47672537422135, + "dtype": "quantity", + "unit": "1", + "why": "elasticity number for [185.886273, 13.237311, 4.719415] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "If the relaxation time is 185.886, kinematic viscosity is 13.2373 and the pipe radius is 4.71941, what is the elasticity number?", + "a": "110.47672537422135", + "valid": true, + "kind": "compute", + "expect": 110.47672537422135, + "dtype": "quantity", + "unit": "1", + "why": "elasticity number for [185.886273, 13.237311, 4.719415] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "Could you work out the elasticity number given relaxation time = 9.1061, kinematic viscosity = 24.5721, pipe radius = 108.451?", + "a": "0.019024321344549868", + "valid": true, + "kind": "compute", + "expect": 0.019024321344549868, + "dtype": "quantity", + "unit": "1", + "why": "elasticity number for [9.106104, 24.572086, 108.450792] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "I need the elasticity number for a relaxation time of 9.1061, a kinematic viscosity of 24.5721 and a pipe radius of 108.451, please.", + "a": "0.019024321344549868", + "valid": true, + "kind": "compute", + "expect": 0.019024321344549868, + "dtype": "quantity", + "unit": "1", + "why": "elasticity number for [9.106104, 24.572086, 108.450792] (the system elasticity number (Wikidata Q102310770))" + }, + { + "q": "If the relaxation time is 17.7888 and the kinematic viscosity is 167.625, what is the elasticity number?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the pipe radius is not given" + }, + { + "q": "Could you work out the elasticity number given relaxation time = 21.511, kinematic viscosity = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the pipe radius is not given" + }, + { + "q": "Do you know the elasticity 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 elasticity 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 elasticity number given relaxation time = -129.245, kinematic viscosity = 1.18359, pipe radius = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative relaxation time: no example had one" + }, + { + "q": "What is a typical elasticity 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-elasticity-number/program.py b/root/organs10/physics/specialists/solved-elasticity-number/program.py new file mode 100644 index 0000000000000000000000000000000000000000..cfac5d995c75c83f646233a843ea3ab4a27f39b4 --- /dev/null +++ b/root/organs10/physics/specialists/solved-elasticity-number/program.py @@ -0,0 +1,7 @@ +"""elasticity number from relaxation time, kinematic viscosity, pipe radius (elasticity number (Wikidata Q102310770)) +The maths organ (systems.py): solved from the system q1 = ((q0*q2)/(q3 **(2))); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(relaxation_time, kinematic_viscosity, pipe_radius): + return 1.0*kinematic_viscosity*relaxation_time/pipe_radius**2 diff --git a/root/organs10/physics/specialists/solved-elasticity-number/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-elasticity-number/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-elasticity-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-electric-charge-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-charge-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-charge-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-electric-charge-2/panel.json b/root/organs10/physics/specialists/solved-electric-charge-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..05d56296eda385f3505d480212be1bbadc2ecbda --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-charge-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric charge for electric flux 107.961?", + "a": "9.559040128324383e-10", + "valid": true, + "kind": "compute", + "expect": 9.559040128324383e-10, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [107.960666] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "Tell me the electric charge with electric flux of 107.961.", + "a": "9.559040128324383e-10", + "valid": true, + "kind": "compute", + "expect": 9.559040128324383e-10, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [107.960666] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "What is the electric charge for electric flux 22.652?", + "a": "2.0056526431910047e-10", + "valid": true, + "kind": "compute", + "expect": 2.0056526431910047e-10, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [22.652023] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "Tell me the electric charge with electric flux of 22.652.", + "a": "2.0056526431910047e-10", + "valid": true, + "kind": "compute", + "expect": 2.0056526431910047e-10, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [22.652023] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "What is the electric charge for electric flux 173.394?", + "a": "1.5352598410256413e-09", + "valid": true, + "kind": "compute", + "expect": 1.5352598410256413e-09, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [173.393638] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "Tell me the electric charge with electric flux of 173.394.", + "a": "1.5352598410256413e-09", + "valid": true, + "kind": "compute", + "expect": 1.5352598410256413e-09, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [173.393638] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "What is the electric charge for electric flux 126.709?", + "a": "1.1219075138184367e-09", + "valid": true, + "kind": "compute", + "expect": 1.1219075138184367e-09, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [126.709252] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "Tell me the electric charge with electric flux of 126.709.", + "a": "1.1219075138184367e-09", + "valid": true, + "kind": "compute", + "expect": 1.1219075138184367e-09, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [126.709252] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "What is the electric charge for electric flux 0.671697?", + "a": "5.9473331643398186e-12", + "valid": true, + "kind": "compute", + "expect": 5.9473331643398186e-12, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [0.671697] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "Tell me the electric charge with electric flux of 0.671697.", + "a": "5.9473331643398186e-12", + "valid": true, + "kind": "compute", + "expect": 5.9473331643398186e-12, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [0.671697] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "What is the electric charge for electric flux 1.16997?", + "a": "1.0359116466915565e-11", + "valid": true, + "kind": "compute", + "expect": 1.0359116466915565e-11, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [1.169968] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "Tell me the electric charge with electric flux of 1.16997.", + "a": "1.0359116466915565e-11", + "valid": true, + "kind": "compute", + "expect": 1.0359116466915565e-11, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [1.169968] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "What is the electric charge?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric flux is not given" + }, + { + "q": "Could you work out the electric charge, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric flux is not given" + }, + { + "q": "What is the electric charge 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 electric charge 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 electric charge with electric flux of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric flux: no example had one" + }, + { + "q": "Which electric charge 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 flux is 2.57003, what is the electric charge?", + "a": "2.2755527552324745e-11", + "valid": true, + "kind": "compute", + "expect": 2.2755527552324745e-11, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [2.57003] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "Could you work out the electric charge given electric flux = 2.57003?", + "a": "2.2755527552324745e-11", + "valid": true, + "kind": "compute", + "expect": 2.2755527552324745e-11, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [2.57003] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "I need the electric charge for an electric flux of 48.921, please.", + "a": "4.3315564135634294e-10", + "valid": true, + "kind": "compute", + "expect": 4.3315564135634294e-10, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [48.920991] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "If the electric flux is 48.921, what is the electric charge?", + "a": "4.3315564135634294e-10", + "valid": true, + "kind": "compute", + "expect": 4.3315564135634294e-10, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [48.920991] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "Could you work out the electric charge given electric flux = 0.348438?", + "a": "3.085132570049275e-12", + "valid": true, + "kind": "compute", + "expect": 3.085132570049275e-12, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [0.348438] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "I need the electric charge for an electric flux of 0.348438, please.", + "a": "3.085132570049275e-12", + "valid": true, + "kind": "compute", + "expect": 3.085132570049275e-12, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [0.348438] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "If the electric flux is 252.709, what is the electric charge?", + "a": "2.2375331642110006e-09", + "valid": true, + "kind": "compute", + "expect": 2.2375331642110006e-09, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [252.709024] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "Could you work out the electric charge given electric flux = 252.709?", + "a": "2.2375331642110006e-09", + "valid": true, + "kind": "compute", + "expect": 2.2375331642110006e-09, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [252.709024] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "I need the electric charge for an electric flux of 20.3966, please.", + "a": "1.8059495505226483e-10", + "valid": true, + "kind": "compute", + "expect": 1.8059495505226483e-10, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [20.396558] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "If the electric flux is 20.3966, what is the electric charge?", + "a": "1.8059495505226483e-10", + "valid": true, + "kind": "compute", + "expect": 1.8059495505226483e-10, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [20.396558] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "Could you work out the electric charge given electric flux = 134.993?", + "a": "1.1952530006351555e-09", + "valid": true, + "kind": "compute", + "expect": 1.1952530006351555e-09, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [134.992958] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "I need the electric charge for an electric flux of 134.993, please.", + "a": "1.1952530006351555e-09", + "valid": true, + "kind": "compute", + "expect": 1.1952530006351555e-09, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [134.992958] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "I need the electric charge, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric flux is not given" + }, + { + "q": "Can you tell me the electric charge?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric flux is not given" + }, + { + "q": "Do you know the electric charge 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 electric charge 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 electric charge given electric flux = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric flux: no example had one" + }, + { + "q": "What is a typical electric charge?", + "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-electric-charge-2/program.py b/root/organs10/physics/specialists/solved-electric-charge-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..4ebd5be62fb1293750bb440fb4cfd34039702053 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-charge-2/program.py @@ -0,0 +1,7 @@ +"""electric charge from electric flux (Gauss's law (Wikidata Q173356)) +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(electric_flux): + return 8.8541878188e-12*electric_flux diff --git a/root/organs10/physics/specialists/solved-electric-charge-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-charge-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-charge-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-electric-charge-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-charge-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-charge-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-electric-charge-3/panel.json b/root/organs10/physics/specialists/solved-electric-charge-3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..ee36132944a418b4222acd12f4b84cc659228e55 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-charge-3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric charge for position of the positively charged particle 107.961, position of the negatively charged particle 59.4105 and electric dipole moment 5.77717?", + "a": "0.11899371570035194", + "valid": true, + "kind": "compute", + "expect": 0.11899371570035194, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [107.960666, 59.4105, 5.777165] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "Tell me the electric charge with position of the positively charged particle of 107.961, position of the negatively charged particle of 59.4105 and electric dipole moment of 5.77717.", + "a": "0.11899371570035194", + "valid": true, + "kind": "compute", + "expect": 0.11899371570035194, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [107.960666, 59.4105, 5.777165] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "What is the electric charge for position of the positively charged particle 82.2401, position of the negatively charged particle 13.999 and electric dipole moment 0.348438?", + "a": "0.005105983948006165", + "valid": true, + "kind": "compute", + "expect": 0.005105983948006165, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [82.240072, 13.999029, 0.348438] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "Tell me the electric charge with position of the positively charged particle of 82.2401, position of the negatively charged particle of 13.999 and electric dipole moment of 0.348438.", + "a": "0.005105983948006165", + "valid": true, + "kind": "compute", + "expect": 0.005105983948006165, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [82.240072, 13.999029, 0.348438] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "What is the electric charge for position of the positively charged particle 6.98086, position of the negatively charged particle 0.551236 and electric dipole moment 2.88512?", + "a": "0.44872313375551076", + "valid": true, + "kind": "compute", + "expect": 0.44872313375551076, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [6.980857, 0.551236, 2.88512] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "Tell me the electric charge with position of the positively charged particle of 6.98086, position of the negatively charged particle of 0.551236 and electric dipole moment of 2.88512.", + "a": "0.44872313375551076", + "valid": true, + "kind": "compute", + "expect": 0.44872313375551076, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [6.980857, 0.551236, 2.88512] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "What is the electric charge for position of the positively charged particle 2.34957, position of the negatively charged particle 1.16997 and electric dipole moment 1.14865?", + "a": "0.9737620612781802", + "valid": true, + "kind": "compute", + "expect": 0.9737620612781802, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [2.349571, 1.169968, 1.148652] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "Tell me the electric charge with position of the positively charged particle of 2.34957, position of the negatively charged particle of 1.16997 and electric dipole moment of 1.14865.", + "a": "0.9737620612781802", + "valid": true, + "kind": "compute", + "expect": 0.9737620612781802, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [2.349571, 1.169968, 1.148652] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "What is the electric charge for position of the positively charged particle 223.509, position of the negatively charged particle 17.3414 and electric dipole moment 7.10705?", + "a": "0.03447223227330149", + "valid": true, + "kind": "compute", + "expect": 0.03447223227330149, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [223.508796, 17.341435, 7.107049] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "Tell me the electric charge with position of the positively charged particle of 223.509, position of the negatively charged particle of 17.3414 and electric dipole moment of 7.10705.", + "a": "0.03447223227330149", + "valid": true, + "kind": "compute", + "expect": 0.03447223227330149, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [223.508796, 17.341435, 7.107049] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "What is the electric charge for position of the positively charged particle 16.8989, position of the negatively charged particle 4.71659 and electric dipole moment 4.07774?", + "a": "0.334726154196635", + "valid": true, + "kind": "compute", + "expect": 0.334726154196635, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [16.898916, 4.716591, 4.077743] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "Tell me the electric charge with position of the positively charged particle of 16.8989, position of the negatively charged particle of 4.71659 and electric dipole moment of 4.07774.", + "a": "0.334726154196635", + "valid": true, + "kind": "compute", + "expect": 0.334726154196635, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [16.898916, 4.716591, 4.077743] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "What is the electric charge for position of the positively charged particle 107.961 and position of the negatively charged particle 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric dipole moment is not given" + }, + { + "q": "Tell me the electric charge with position of the positively charged particle of 82.2401 and position of the negatively charged particle of 13.999.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric dipole moment is not given" + }, + { + "q": "What is the electric charge 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 electric charge 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 electric charge with position of the positively charged particle of -6.98086, position of the negatively charged particle of 0.551236 and electric dipole moment of 2.88512.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative position of the positively charged particle: no example had one" + }, + { + "q": "Which electric charge 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 position of the positively charged particle is 8.24951, position of the negatively charged particle is 0.634021 and the electric dipole moment is 6.34623, what is the electric charge?", + "a": "0.833331344001913", + "valid": true, + "kind": "compute", + "expect": 0.833331344001913, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [8.24951, 0.634021, 6.346226] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "Could you work out the electric charge given position of the positively charged particle = 8.24951, position of the negatively charged particle = 0.634021, electric dipole moment = 6.34623?", + "a": "0.833331344001913", + "valid": true, + "kind": "compute", + "expect": 0.833331344001913, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [8.24951, 0.634021, 6.346226] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "I need the electric charge for a position of the positively charged particle of 126.709, a position of the negatively charged particle of 1.70503 and an electric dipole moment of 2.9896, please.", + "a": "0.023915992894014432", + "valid": true, + "kind": "compute", + "expect": 0.023915992894014432, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [126.709252, 1.705032, 2.9896] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "If the position of the positively charged particle is 126.709, position of the negatively charged particle is 1.70503 and the electric dipole moment is 2.9896, what is the electric charge?", + "a": "0.023915992894014432", + "valid": true, + "kind": "compute", + "expect": 0.023915992894014432, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [126.709252, 1.705032, 2.9896] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "Could you work out the electric charge given position of the positively charged particle = 87.781, position of the negatively charged particle = 13.2084, electric dipole moment = 246.329?", + "a": "3.3032115085315628", + "valid": true, + "kind": "compute", + "expect": 3.3032115085315628, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [87.781013, 13.208419, 246.329053] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "I need the electric charge for a position of the positively charged particle of 87.781, a position of the negatively charged particle of 13.2084 and an electric dipole moment of 246.329, please.", + "a": "3.3032115085315628", + "valid": true, + "kind": "compute", + "expect": 3.3032115085315628, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [87.781013, 13.208419, 246.329053] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "If the position of the positively charged particle is 186.27, position of the negatively charged particle is 106.508 and the electric dipole moment is 156.502, what is the electric charge?", + "a": "1.9620990806934702", + "valid": true, + "kind": "compute", + "expect": 1.9620990806934702, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [186.270057, 106.507617, 156.50181] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "Could you work out the electric charge given position of the positively charged particle = 186.27, position of the negatively charged particle = 106.508, electric dipole moment = 156.502?", + "a": "1.9620990806934702", + "valid": true, + "kind": "compute", + "expect": 1.9620990806934702, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [186.270057, 106.507617, 156.50181] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "I need the electric charge for a position of the positively charged particle of 88.6624, a position of the negatively charged particle of 0.633611 and an electric dipole moment of 0.869111, please.", + "a": "0.009873040831831647", + "valid": true, + "kind": "compute", + "expect": 0.009873040831831647, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [88.662361, 0.633611, 0.869111] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "If the position of the positively charged particle is 88.6624, position of the negatively charged particle is 0.633611 and the electric dipole moment is 0.869111, what is the electric charge?", + "a": "0.009873040831831647", + "valid": true, + "kind": "compute", + "expect": 0.009873040831831647, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [88.662361, 0.633611, 0.869111] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "Could you work out the electric charge given position of the positively charged particle = 1.90441, position of the negatively charged particle = 1.45374, electric dipole moment = 27.5916?", + "a": "61.224505520348586", + "valid": true, + "kind": "compute", + "expect": 61.224505520348586, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [1.904407, 1.453744, 27.591617] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "I need the electric charge for a position of the positively charged particle of 1.90441, a position of the negatively charged particle of 1.45374 and an electric dipole moment of 27.5916, please.", + "a": "61.224505520348586", + "valid": true, + "kind": "compute", + "expect": 61.224505520348586, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [1.904407, 1.453744, 27.591617] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "If the position of the positively charged particle is 8.24951 and the position of the negatively charged particle is 0.634021, what is the electric charge?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric dipole moment is not given" + }, + { + "q": "Could you work out the electric charge given position of the positively charged particle = 126.709, position of the negatively charged particle = 1.70503?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric dipole moment is not given" + }, + { + "q": "Do you know the electric charge 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 electric charge 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 electric charge given position of the positively charged particle = -87.781, position of the negatively charged particle = 13.2084, electric dipole moment = 246.329?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative position of the positively charged particle: no example had one" + }, + { + "q": "What is a typical electric charge?", + "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-electric-charge-3/program.py b/root/organs10/physics/specialists/solved-electric-charge-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..e8a26f86d87c89d2bcb9cd71c76050705a75341e --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-charge-3/program.py @@ -0,0 +1,7 @@ +"""electric charge from position of the positively charged particle, position of the negatively charged particle, electric dipole moment (electric dipole moment (Wikidata Q735135)) +The maths organ (systems.py): solved from the system q2 = q3*( q0 - q1 ); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(position_of_the_positively_charged_particle, position_of_the_negatively_charged_particle, electric_dipole_moment): + return 1.0*electric_dipole_moment/(-position_of_the_negatively_charged_particle + position_of_the_positively_charged_particle) diff --git a/root/organs10/physics/specialists/solved-electric-charge-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-charge-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-charge-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-electric-charge-4/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-charge-4/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-charge-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-electric-charge-4/panel.json b/root/organs10/physics/specialists/solved-electric-charge-4/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..5fc6a7d5b4e4fd34f072c09a6fe4b381113289c6 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-charge-4/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric charge for electric field strength 107.961 and force 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [107.960666, 59.4105] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "Tell me the electric charge with electric field strength of 107.961 and force of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [107.960666, 59.4105] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "What is the electric charge for electric field strength 169.527 and force 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [169.526958, 280.773702] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "Tell me the electric charge with electric field strength of 169.527 and force of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [169.526958, 280.773702] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "What is the electric charge for electric field strength 1.91195 and force 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [1.911952, 82.240072] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "Tell me the electric charge with electric field strength of 1.91195 and force of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [1.911952, 82.240072] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "What is the electric charge for electric field strength 1.64379 and force 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [1.643795, 252.709024] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "Tell me the electric charge with electric field strength of 1.64379 and force of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [1.643795, 252.709024] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "What is the electric charge for electric field strength 18.3169 and force 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [18.316867, 6.838598] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "Tell me the electric charge with electric field strength of 18.3169 and force of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [18.316867, 6.838598] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "What is the electric charge for electric field strength 156.502 and force 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [156.50181, 185.886273] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "Tell me the electric charge with electric field strength of 156.502 and force of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [156.50181, 185.886273] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "What is the electric charge for electric field strength 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the force is not given" + }, + { + "q": "Tell me the electric charge with electric field strength of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the force is not given" + }, + { + "q": "What is the electric charge 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 electric charge 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 electric charge with electric field strength of -1.91195 and force of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric field strength: no example had one" + }, + { + "q": "Which electric charge 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 strength is 8.50728 and the force is 17.7888, what is the electric charge?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [8.507283, 17.788778] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "Could you work out the electric charge given electric field strength = 8.50728, force = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [8.507283, 17.788778] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "I need the electric charge for an electric field strength of 8.24951 and a force of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [8.24951, 0.634021] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "If the electric field strength is 8.24951 and the force is 0.634021, what is the electric charge?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [8.24951, 0.634021] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "Could you work out the electric charge given electric field strength = 0.318734, force = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [0.318734, 9.566071] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "I need the electric charge for an electric field strength of 0.318734 and a force of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [0.318734, 9.566071] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "If the electric field strength is 0.671697 and the force is 14.2496, what is the electric charge?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [0.671697, 14.249569] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "Could you work out the electric charge given electric field strength = 0.671697, force = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [0.671697, 14.249569] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "I need the electric charge for an electric field strength of 21.7931 and a force of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [21.793126, 29.510609] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "If the electric field strength is 21.7931 and the force is 29.5106, what is the electric charge?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [21.793126, 29.510609] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "Could you work out the electric charge given electric field strength = 153.15, force = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [153.149965, 18.595315] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "I need the electric charge for an electric field strength of 153.15 and a force of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [153.149965, 18.595315] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "If the electric field strength is 8.50728, what is the electric charge?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the force is not given" + }, + { + "q": "Could you work out the electric charge given electric field strength = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the force is not given" + }, + { + "q": "Do you know the electric charge 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 electric charge 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 electric charge given electric field strength = -0.318734, force = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric field strength: no example had one" + }, + { + "q": "What is a typical electric charge?", + "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-electric-charge-4/program.py b/root/organs10/physics/specialists/solved-electric-charge-4/program.py new file mode 100644 index 0000000000000000000000000000000000000000..76481b3e287b95976e784116b8bc309b240380d5 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-charge-4/program.py @@ -0,0 +1,7 @@ +"""electric charge from electric field strength, force (electric field strength (Wikidata Q20989)) +The maths organ (systems.py): solved from the system q0 = ((q1)/(q2)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(electric_field_strength, force): + return 1.0*force/electric_field_strength diff --git a/root/organs10/physics/specialists/solved-electric-charge-4/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-charge-4/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-charge-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-electric-charge/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-charge/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-charge/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-electric-charge/panel.json b/root/organs10/physics/specialists/solved-electric-charge/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..25c648d56b9cc7356c5124882efbbd695f6cf94d --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-charge/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric charge for capacitance 107.961 and voltage 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [107.960666, 59.4105] (the system capacitance (Wikidata Q164399))" + }, + { + "q": "Tell me the electric charge with capacitance of 107.961 and voltage of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [107.960666, 59.4105] (the system capacitance (Wikidata Q164399))" + }, + { + "q": "What is the electric charge for capacitance 169.527 and voltage 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [169.526958, 280.773702] (the system capacitance (Wikidata Q164399))" + }, + { + "q": "Tell me the electric charge with capacitance of 169.527 and voltage of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [169.526958, 280.773702] (the system capacitance (Wikidata Q164399))" + }, + { + "q": "What is the electric charge for capacitance 1.91195 and voltage 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [1.911952, 82.240072] (the system capacitance (Wikidata Q164399))" + }, + { + "q": "Tell me the electric charge with capacitance of 1.91195 and voltage of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [1.911952, 82.240072] (the system capacitance (Wikidata Q164399))" + }, + { + "q": "What is the electric charge for capacitance 1.64379 and voltage 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [1.643795, 252.709024] (the system capacitance (Wikidata Q164399))" + }, + { + "q": "Tell me the electric charge with capacitance of 1.64379 and voltage of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [1.643795, 252.709024] (the system capacitance (Wikidata Q164399))" + }, + { + "q": "What is the electric charge for capacitance 18.3169 and voltage 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [18.316867, 6.838598] (the system capacitance (Wikidata Q164399))" + }, + { + "q": "Tell me the electric charge with capacitance of 18.3169 and voltage of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [18.316867, 6.838598] (the system capacitance (Wikidata Q164399))" + }, + { + "q": "What is the electric charge for capacitance 156.502 and voltage 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [156.50181, 185.886273] (the system capacitance (Wikidata Q164399))" + }, + { + "q": "Tell me the electric charge with capacitance of 156.502 and voltage of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [156.50181, 185.886273] (the system capacitance (Wikidata Q164399))" + }, + { + "q": "What is the electric charge for capacitance 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the voltage is not given" + }, + { + "q": "Tell me the electric charge with capacitance of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the voltage is not given" + }, + { + "q": "What is the electric charge 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 electric charge 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 electric charge with capacitance of -1.91195 and voltage of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative capacitance: no example had one" + }, + { + "q": "Which electric charge 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 capacitance is 8.50728 and the voltage is 17.7888, what is the electric charge?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [8.507283, 17.788778] (the system capacitance (Wikidata Q164399))" + }, + { + "q": "Could you work out the electric charge given capacitance = 8.50728, voltage = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [8.507283, 17.788778] (the system capacitance (Wikidata Q164399))" + }, + { + "q": "I need the electric charge for a capacitance of 8.24951 and a voltage of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [8.24951, 0.634021] (the system capacitance (Wikidata Q164399))" + }, + { + "q": "If the capacitance is 8.24951 and the voltage is 0.634021, what is the electric charge?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [8.24951, 0.634021] (the system capacitance (Wikidata Q164399))" + }, + { + "q": "Could you work out the electric charge given capacitance = 0.318734, voltage = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [0.318734, 9.566071] (the system capacitance (Wikidata Q164399))" + }, + { + "q": "I need the electric charge for a capacitance of 0.318734 and a voltage of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [0.318734, 9.566071] (the system capacitance (Wikidata Q164399))" + }, + { + "q": "If the capacitance is 0.671697 and the voltage is 14.2496, what is the electric charge?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [0.671697, 14.249569] (the system capacitance (Wikidata Q164399))" + }, + { + "q": "Could you work out the electric charge given capacitance = 0.671697, voltage = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [0.671697, 14.249569] (the system capacitance (Wikidata Q164399))" + }, + { + "q": "I need the electric charge for a capacitance of 21.7931 and a voltage of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [21.793126, 29.510609] (the system capacitance (Wikidata Q164399))" + }, + { + "q": "If the capacitance is 21.7931 and the voltage is 29.5106, what is the electric charge?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [21.793126, 29.510609] (the system capacitance (Wikidata Q164399))" + }, + { + "q": "Could you work out the electric charge given capacitance = 153.15, voltage = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [153.149965, 18.595315] (the system capacitance (Wikidata Q164399))" + }, + { + "q": "I need the electric charge for a capacitance of 153.15 and a voltage of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "s*A", + "why": "electric charge for [153.149965, 18.595315] (the system capacitance (Wikidata Q164399))" + }, + { + "q": "If the capacitance is 8.50728, what is the electric charge?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the voltage is not given" + }, + { + "q": "Could you work out the electric charge given capacitance = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the voltage is not given" + }, + { + "q": "Do you know the electric charge 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 electric charge 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 electric charge given capacitance = -0.318734, voltage = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative capacitance: no example had one" + }, + { + "q": "What is a typical electric charge?", + "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-electric-charge/program.py b/root/organs10/physics/specialists/solved-electric-charge/program.py new file mode 100644 index 0000000000000000000000000000000000000000..ea2cb68d0fdcb48687b42054d681de2a144f4822 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-charge/program.py @@ -0,0 +1,7 @@ +"""electric charge from capacitance, voltage (capacitance (Wikidata Q164399)) +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(capacitance, voltage): + return 1.0*capacitance*voltage diff --git a/root/organs10/physics/specialists/solved-electric-charge/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-charge/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-charge/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-electric-current-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-current-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-2/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-electric-current-2/panel.json b/root/organs10/physics/specialists/solved-electric-current-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..62aa713a23953f4ffa816cec2b6dc308955b019e --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric current for electrical resistance 107.961 and voltage 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [107.960666, 59.4105] (the system electrical resistance (Wikidata Q25358))" + }, + { + "q": "Tell me the electric current with electrical resistance of 107.961 and voltage of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [107.960666, 59.4105] (the system electrical resistance (Wikidata Q25358))" + }, + { + "q": "What is the electric current for electrical resistance 169.527 and voltage 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [169.526958, 280.773702] (the system electrical resistance (Wikidata Q25358))" + }, + { + "q": "Tell me the electric current with electrical resistance of 169.527 and voltage of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [169.526958, 280.773702] (the system electrical resistance (Wikidata Q25358))" + }, + { + "q": "What is the electric current for electrical resistance 1.91195 and voltage 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [1.911952, 82.240072] (the system electrical resistance (Wikidata Q25358))" + }, + { + "q": "Tell me the electric current with electrical resistance of 1.91195 and voltage of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [1.911952, 82.240072] (the system electrical resistance (Wikidata Q25358))" + }, + { + "q": "What is the electric current for electrical resistance 1.64379 and voltage 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [1.643795, 252.709024] (the system electrical resistance (Wikidata Q25358))" + }, + { + "q": "Tell me the electric current with electrical resistance of 1.64379 and voltage of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [1.643795, 252.709024] (the system electrical resistance (Wikidata Q25358))" + }, + { + "q": "What is the electric current for electrical resistance 18.3169 and voltage 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [18.316867, 6.838598] (the system electrical resistance (Wikidata Q25358))" + }, + { + "q": "Tell me the electric current with electrical resistance of 18.3169 and voltage of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [18.316867, 6.838598] (the system electrical resistance (Wikidata Q25358))" + }, + { + "q": "What is the electric current for electrical resistance 156.502 and voltage 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [156.50181, 185.886273] (the system electrical resistance (Wikidata Q25358))" + }, + { + "q": "Tell me the electric current with electrical resistance of 156.502 and voltage of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [156.50181, 185.886273] (the system electrical resistance (Wikidata Q25358))" + }, + { + "q": "What is the electric current for electrical resistance 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the voltage is not given" + }, + { + "q": "Tell me the electric current with electrical resistance of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the voltage is not given" + }, + { + "q": "What is the 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 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 electric current with electrical resistance of -1.91195 and voltage of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electrical resistance: no example had one" + }, + { + "q": "Which 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 electrical resistance is 8.50728 and the voltage is 17.7888, what is the electric current?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [8.507283, 17.788778] (the system electrical resistance (Wikidata Q25358))" + }, + { + "q": "Could you work out the electric current given electrical resistance = 8.50728, voltage = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [8.507283, 17.788778] (the system electrical resistance (Wikidata Q25358))" + }, + { + "q": "I need the electric current for an electrical resistance of 8.24951 and a voltage of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [8.24951, 0.634021] (the system electrical resistance (Wikidata Q25358))" + }, + { + "q": "If the electrical resistance is 8.24951 and the voltage is 0.634021, what is the electric current?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [8.24951, 0.634021] (the system electrical resistance (Wikidata Q25358))" + }, + { + "q": "Could you work out the electric current given electrical resistance = 0.318734, voltage = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [0.318734, 9.566071] (the system electrical resistance (Wikidata Q25358))" + }, + { + "q": "I need the electric current for an electrical resistance of 0.318734 and a voltage of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [0.318734, 9.566071] (the system electrical resistance (Wikidata Q25358))" + }, + { + "q": "If the electrical resistance is 0.671697 and the voltage is 14.2496, what is the electric current?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [0.671697, 14.249569] (the system electrical resistance (Wikidata Q25358))" + }, + { + "q": "Could you work out the electric current given electrical resistance = 0.671697, voltage = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [0.671697, 14.249569] (the system electrical resistance (Wikidata Q25358))" + }, + { + "q": "I need the electric current for an electrical resistance of 21.7931 and a voltage of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [21.793126, 29.510609] (the system electrical resistance (Wikidata Q25358))" + }, + { + "q": "If the electrical resistance is 21.7931 and the voltage is 29.5106, what is the electric current?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [21.793126, 29.510609] (the system electrical resistance (Wikidata Q25358))" + }, + { + "q": "Could you work out the electric current given electrical resistance = 153.15, voltage = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [153.149965, 18.595315] (the system electrical resistance (Wikidata Q25358))" + }, + { + "q": "I need the electric current for an electrical resistance of 153.15 and a voltage of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [153.149965, 18.595315] (the system electrical resistance (Wikidata Q25358))" + }, + { + "q": "If the electrical resistance is 8.50728, what is the electric current?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the voltage is not given" + }, + { + "q": "Could you work out the electric current given electrical resistance = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the voltage is not given" + }, + { + "q": "Do you know the 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 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 electric current given electrical resistance = -0.318734, voltage = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electrical resistance: no example had one" + }, + { + "q": "What is a typical 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-electric-current-2/program.py b/root/organs10/physics/specialists/solved-electric-current-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..2a884d019f06d039634e5753e85f9036249236fe --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-2/program.py @@ -0,0 +1,7 @@ +"""electric current from electrical resistance, voltage (electrical resistance (Wikidata Q25358)) +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(electrical_resistance, voltage): + return 1.0*voltage/electrical_resistance diff --git a/root/organs10/physics/specialists/solved-electric-current-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-current-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-2/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-electric-current-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-current-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-3/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-electric-current-3/panel.json b/root/organs10/physics/specialists/solved-electric-current-3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..02f9215bfb758fb7ad78a62d82df26fb5d17b99e --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric current for ion transport number 107.961 and electric current of ion 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [107.960666, 59.4105] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Tell me the electric current with ion transport number of 107.961 and electric current of ion of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [107.960666, 59.4105] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "What is the electric current for ion transport number 169.527 and electric current of ion 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [169.526958, 280.773702] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Tell me the electric current with ion transport number of 169.527 and electric current of ion of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [169.526958, 280.773702] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "What is the electric current for ion transport number 1.91195 and electric current of ion 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [1.911952, 82.240072] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Tell me the electric current with ion transport number of 1.91195 and electric current of ion of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [1.911952, 82.240072] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "What is the electric current for ion transport number 1.64379 and electric current of ion 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [1.643795, 252.709024] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Tell me the electric current with ion transport number of 1.64379 and electric current of ion of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [1.643795, 252.709024] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "What is the electric current for ion transport number 18.3169 and electric current of ion 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [18.316867, 6.838598] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Tell me the electric current with ion transport number of 18.3169 and electric current of ion of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [18.316867, 6.838598] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "What is the electric current for ion transport number 156.502 and electric current of ion 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [156.50181, 185.886273] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Tell me the electric current with ion transport number of 156.502 and electric current of ion of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [156.50181, 185.886273] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "What is the electric current for ion transport number 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current of ion is not given" + }, + { + "q": "Tell me the electric current with ion transport number of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current of ion is not given" + }, + { + "q": "What is the 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 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 electric current with ion transport number of -1.91195 and electric current of ion of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative ion transport number: no example had one" + }, + { + "q": "Which 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 ion transport number is 8.50728 and the electric current of ion is 17.7888, what is the electric current?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [8.507283, 17.788778] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Could you work out the electric current given ion transport number = 8.50728, electric current of ion = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [8.507283, 17.788778] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "I need the electric current for an ion transport number of 8.24951 and an electric current of ion of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [8.24951, 0.634021] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "If the ion transport number is 8.24951 and the electric current of ion is 0.634021, what is the electric current?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [8.24951, 0.634021] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Could you work out the electric current given ion transport number = 0.318734, electric current of ion = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [0.318734, 9.566071] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "I need the electric current for an ion transport number of 0.318734 and an electric current of ion of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [0.318734, 9.566071] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "If the ion transport number is 0.671697 and the electric current of ion is 14.2496, what is the electric current?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [0.671697, 14.249569] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Could you work out the electric current given ion transport number = 0.671697, electric current of ion = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [0.671697, 14.249569] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "I need the electric current for an ion transport number of 21.7931 and an electric current of ion of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [21.793126, 29.510609] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "If the ion transport number is 21.7931 and the electric current of ion is 29.5106, what is the electric current?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [21.793126, 29.510609] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Could you work out the electric current given ion transport number = 153.15, electric current of ion = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [153.149965, 18.595315] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "I need the electric current for an ion transport number of 153.15 and an electric current of ion of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [153.149965, 18.595315] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "If the ion transport number is 8.50728, what is the electric current?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current of ion is not given" + }, + { + "q": "Could you work out the electric current given ion transport number = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current of ion is not given" + }, + { + "q": "Do you know the 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 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 electric current given ion transport number = -0.318734, electric current of ion = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative ion transport number: no example had one" + }, + { + "q": "What is a typical 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-electric-current-3/program.py b/root/organs10/physics/specialists/solved-electric-current-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..816f748b2d026885647cc78e1269e8d06110ec5c --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-3/program.py @@ -0,0 +1,7 @@ +"""electric current from ion transport number, electric current of ion (ion transport number (Wikidata Q331854)) +The maths organ (systems.py): solved from the system q0 = ((q1)/(q2)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(ion_transport_number, electric_current_of_ion): + return 1.0*electric_current_of_ion/ion_transport_number diff --git a/root/organs10/physics/specialists/solved-electric-current-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-current-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-3/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-electric-current-4/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-current-4/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-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-electric-current-4/panel.json b/root/organs10/physics/specialists/solved-electric-current-4/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..66c5b6008fb3bee65e777012d359daabac0b3240 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-4/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric current for magnetic flux density 107.961, force 59.4105 and length 5.77717?", + "a": "0.09525393194179609", + "valid": true, + "kind": "compute", + "expect": 0.09525393194179609, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [107.960666, 59.4105, 5.777165] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Tell me the electric current with magnetic flux density of 107.961, force of 59.4105 and length of 5.77717.", + "a": "0.09525393194179609", + "valid": true, + "kind": "compute", + "expect": 0.09525393194179609, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [107.960666, 59.4105, 5.777165] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "What is the electric current for magnetic flux density 85.2416, force 160.878 and length 2.69428?", + "a": "0.7004924122686708", + "valid": true, + "kind": "compute", + "expect": 0.7004924122686708, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [85.241623, 160.878442, 2.69428] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Tell me the electric current with magnetic flux density of 85.2416, force of 160.878 and length of 2.69428.", + "a": "0.7004924122686708", + "valid": true, + "kind": "compute", + "expect": 0.7004924122686708, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [85.241623, 160.878442, 2.69428] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "What is the electric current for magnetic flux density 45.6157, force 4.97131 and length 94.305?", + "a": "0.001155637526406431", + "valid": true, + "kind": "compute", + "expect": 0.001155637526406431, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [45.61567, 4.971307, 94.305046] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Tell me the electric current with magnetic flux density of 45.6157, force of 4.97131 and length of 94.305.", + "a": "0.001155637526406431", + "valid": true, + "kind": "compute", + "expect": 0.001155637526406431, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [45.61567, 4.971307, 94.305046] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "What is the electric current for magnetic flux density 10.5638, force 198.839 and length 0.671697?", + "a": "28.022443831038363", + "valid": true, + "kind": "compute", + "expect": 28.022443831038363, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [10.563843, 198.838955, 0.671697] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Tell me the electric current with magnetic flux density of 10.5638, force of 198.839 and length of 0.671697.", + "a": "28.022443831038363", + "valid": true, + "kind": "compute", + "expect": 28.022443831038363, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [10.563843, 198.838955, 0.671697] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "What is the electric current for magnetic flux density 21.7931, force 29.5106 and length 8.50341?", + "a": "0.15924494359111602", + "valid": true, + "kind": "compute", + "expect": 0.15924494359111602, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [21.793126, 29.510609, 8.503407] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Tell me the electric current with magnetic flux density of 21.7931, force of 29.5106 and length of 8.50341.", + "a": "0.15924494359111602", + "valid": true, + "kind": "compute", + "expect": 0.15924494359111602, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [21.793126, 29.510609, 8.503407] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "What is the electric current for magnetic flux density 223.509, force 17.3414 and length 7.10705?", + "a": "0.010916944254137492", + "valid": true, + "kind": "compute", + "expect": 0.010916944254137492, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [223.508796, 17.341435, 7.107049] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Tell me the electric current with magnetic flux density of 223.509, force of 17.3414 and length of 7.10705.", + "a": "0.010916944254137492", + "valid": true, + "kind": "compute", + "expect": 0.010916944254137492, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [223.508796, 17.341435, 7.107049] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "What is the electric current for magnetic flux density 107.961 and force 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the length is not given" + }, + { + "q": "Tell me the electric current with magnetic flux density of 85.2416 and force of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the length is not given" + }, + { + "q": "What is the 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 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 electric current with magnetic flux density of -45.6157, force of 4.97131 and length of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative magnetic flux density: no example had one" + }, + { + "q": "Which 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 magnetic flux density is 17.7888, force is 167.625 and the length is 10.3291, what is the electric current?", + "a": "0.9122880156800318", + "valid": true, + "kind": "compute", + "expect": 0.9122880156800318, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [17.788778, 167.624948, 10.329054] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Could you work out the electric current given magnetic flux density = 17.7888, force = 167.625, length = 10.3291?", + "a": "0.9122880156800318", + "valid": true, + "kind": "compute", + "expect": 0.9122880156800318, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [17.788778, 167.624948, 10.329054] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "I need the electric current for a magnetic flux density of 21.511, a force of 173.394 and a length of 251.084, please.", + "a": "0.032103555682225955", + "valid": true, + "kind": "compute", + "expect": 0.032103555682225955, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [21.511016, 173.393638, 251.084037] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "If the magnetic flux density is 21.511, force is 173.394 and the length is 251.084, what is the electric current?", + "a": "0.032103555682225955", + "valid": true, + "kind": "compute", + "expect": 0.032103555682225955, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [21.511016, 173.393638, 251.084037] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Could you work out the electric current given magnetic flux density = 129.245, force = 1.18359, length = 15.9416?", + "a": "0.0005744541302673633", + "valid": true, + "kind": "compute", + "expect": 0.0005744541302673633, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [129.244984, 1.18359, 15.941612] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "I need the electric current for a magnetic flux density of 129.245, a force of 1.18359 and a length of 15.9416, please.", + "a": "0.0005744541302673633", + "valid": true, + "kind": "compute", + "expect": 0.0005744541302673633, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [129.244984, 1.18359, 15.941612] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "If the magnetic flux density is 20.3966, force is 18.3169 and the length is 6.8386, what is the electric current?", + "a": "0.13131889807531302", + "valid": true, + "kind": "compute", + "expect": 0.13131889807531302, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [20.396558, 18.316867, 6.838598] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Could you work out the electric current given magnetic flux density = 20.3966, force = 18.3169, length = 6.8386?", + "a": "0.13131889807531302", + "valid": true, + "kind": "compute", + "expect": 0.13131889807531302, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [20.396558, 18.316867, 6.838598] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "I need the electric current for a magnetic flux density of 185.886, a force of 13.2373 and a length of 4.71941, please.", + "a": "0.015089133629806127", + "valid": true, + "kind": "compute", + "expect": 0.015089133629806127, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [185.886273, 13.237311, 4.719415] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "If the magnetic flux density is 185.886, force is 13.2373 and the length is 4.71941, what is the electric current?", + "a": "0.015089133629806127", + "valid": true, + "kind": "compute", + "expect": 0.015089133629806127, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [185.886273, 13.237311, 4.719415] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "Could you work out the electric current given magnetic flux density = 9.1061, force = 24.5721, length = 108.451?", + "a": "0.024881507432683664", + "valid": true, + "kind": "compute", + "expect": 0.024881507432683664, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [9.106104, 24.572086, 108.450792] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "I need the electric current for a magnetic flux density of 9.1061, a force of 24.5721 and a length of 108.451, please.", + "a": "0.024881507432683664", + "valid": true, + "kind": "compute", + "expect": 0.024881507432683664, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [9.106104, 24.572086, 108.450792] (the system magnetic field of test current (Wikidata Q104145274))" + }, + { + "q": "If the magnetic flux density is 17.7888 and the force is 167.625, what is the electric current?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the length is not given" + }, + { + "q": "Could you work out the electric current given magnetic flux density = 21.511, force = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the length is not given" + }, + { + "q": "Do you know the 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 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 electric current given magnetic flux density = -129.245, force = 1.18359, length = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative magnetic flux density: no example had one" + }, + { + "q": "What is a typical 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-electric-current-4/program.py b/root/organs10/physics/specialists/solved-electric-current-4/program.py new file mode 100644 index 0000000000000000000000000000000000000000..23862e31b6f404e18a75a683470205df6b8595ac --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-4/program.py @@ -0,0 +1,7 @@ +"""electric current from magnetic flux density, force, length (magnetic field of test current (Wikidata Q104145274)) +The maths organ (systems.py): solved from the system q0 = ((q1)/(q2*q3)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(magnetic_flux_density, force, length): + return 1.0*force/(length*magnetic_flux_density) diff --git a/root/organs10/physics/specialists/solved-electric-current-4/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-current-4/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-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-electric-current-5/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-current-5/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-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-electric-current-5/panel.json b/root/organs10/physics/specialists/solved-electric-current-5/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..fec54d36a93ccfd271879bca3329cae295e4d34f --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-5/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric current for inductance 107.961 and total magnetic flux 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [107.960666, 59.4105] (the system inductance (Wikidata Q177897))" + }, + { + "q": "Tell me the electric current with inductance of 107.961 and total magnetic flux of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [107.960666, 59.4105] (the system inductance (Wikidata Q177897))" + }, + { + "q": "What is the electric current for inductance 169.527 and total magnetic flux 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [169.526958, 280.773702] (the system inductance (Wikidata Q177897))" + }, + { + "q": "Tell me the electric current with inductance of 169.527 and total magnetic flux of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [169.526958, 280.773702] (the system inductance (Wikidata Q177897))" + }, + { + "q": "What is the electric current for inductance 1.91195 and total magnetic flux 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [1.911952, 82.240072] (the system inductance (Wikidata Q177897))" + }, + { + "q": "Tell me the electric current with inductance of 1.91195 and total magnetic flux of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [1.911952, 82.240072] (the system inductance (Wikidata Q177897))" + }, + { + "q": "What is the electric current for inductance 1.64379 and total magnetic flux 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [1.643795, 252.709024] (the system inductance (Wikidata Q177897))" + }, + { + "q": "Tell me the electric current with inductance of 1.64379 and total magnetic flux of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [1.643795, 252.709024] (the system inductance (Wikidata Q177897))" + }, + { + "q": "What is the electric current for inductance 18.3169 and total magnetic flux 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [18.316867, 6.838598] (the system inductance (Wikidata Q177897))" + }, + { + "q": "Tell me the electric current with inductance of 18.3169 and total magnetic flux of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [18.316867, 6.838598] (the system inductance (Wikidata Q177897))" + }, + { + "q": "What is the electric current for inductance 156.502 and total magnetic flux 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [156.50181, 185.886273] (the system inductance (Wikidata Q177897))" + }, + { + "q": "Tell me the electric current with inductance of 156.502 and total magnetic flux of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [156.50181, 185.886273] (the system inductance (Wikidata Q177897))" + }, + { + "q": "What is the electric current for inductance 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total magnetic flux is not given" + }, + { + "q": "Tell me the electric current with inductance of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total magnetic flux is not given" + }, + { + "q": "What is the 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 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 electric current with inductance of -1.91195 and total magnetic flux of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative inductance: no example had one" + }, + { + "q": "Which 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 inductance is 8.50728 and the total magnetic flux is 17.7888, what is the electric current?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [8.507283, 17.788778] (the system inductance (Wikidata Q177897))" + }, + { + "q": "Could you work out the electric current given inductance = 8.50728, total magnetic flux = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [8.507283, 17.788778] (the system inductance (Wikidata Q177897))" + }, + { + "q": "I need the electric current for an inductance of 8.24951 and a total magnetic flux of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [8.24951, 0.634021] (the system inductance (Wikidata Q177897))" + }, + { + "q": "If the inductance is 8.24951 and the total magnetic flux is 0.634021, what is the electric current?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [8.24951, 0.634021] (the system inductance (Wikidata Q177897))" + }, + { + "q": "Could you work out the electric current given inductance = 0.318734, total magnetic flux = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [0.318734, 9.566071] (the system inductance (Wikidata Q177897))" + }, + { + "q": "I need the electric current for an inductance of 0.318734 and a total magnetic flux of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [0.318734, 9.566071] (the system inductance (Wikidata Q177897))" + }, + { + "q": "If the inductance is 0.671697 and the total magnetic flux is 14.2496, what is the electric current?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [0.671697, 14.249569] (the system inductance (Wikidata Q177897))" + }, + { + "q": "Could you work out the electric current given inductance = 0.671697, total magnetic flux = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [0.671697, 14.249569] (the system inductance (Wikidata Q177897))" + }, + { + "q": "I need the electric current for an inductance of 21.7931 and a total magnetic flux of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [21.793126, 29.510609] (the system inductance (Wikidata Q177897))" + }, + { + "q": "If the inductance is 21.7931 and the total magnetic flux is 29.5106, what is the electric current?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [21.793126, 29.510609] (the system inductance (Wikidata Q177897))" + }, + { + "q": "Could you work out the electric current given inductance = 153.15, total magnetic flux = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [153.149965, 18.595315] (the system inductance (Wikidata Q177897))" + }, + { + "q": "I need the electric current for an inductance of 153.15 and a total magnetic flux of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [153.149965, 18.595315] (the system inductance (Wikidata Q177897))" + }, + { + "q": "If the inductance is 8.50728, what is the electric current?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total magnetic flux is not given" + }, + { + "q": "Could you work out the electric current given inductance = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total magnetic flux is not given" + }, + { + "q": "Do you know the 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 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 electric current given inductance = -0.318734, total magnetic flux = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative inductance: no example had one" + }, + { + "q": "What is a typical 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-electric-current-5/program.py b/root/organs10/physics/specialists/solved-electric-current-5/program.py new file mode 100644 index 0000000000000000000000000000000000000000..67860e6f984a2d588fab3b8ef0c277d870320c60 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-5/program.py @@ -0,0 +1,7 @@ +"""electric current from inductance, total magnetic flux (inductance (Wikidata Q177897)) +The maths organ (systems.py): solved from the system q0 = ((q1)/(q2)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(inductance, total_magnetic_flux): + return 1.0*total_magnetic_flux/inductance diff --git a/root/organs10/physics/specialists/solved-electric-current-5/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-current-5/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-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-electric-current-6/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-current-6/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-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-electric-current-6/panel.json b/root/organs10/physics/specialists/solved-electric-current-6/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..2536b25b19dc02a205b721a276e44c412d130aab --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-6/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric current for mutual inductance 107.961 and total magnetic flux 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [107.960666, 59.4105] (the system mutual inductance (Wikidata Q78101401))" + }, + { + "q": "Tell me the electric current with mutual inductance of 107.961 and total magnetic flux of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [107.960666, 59.4105] (the system mutual inductance (Wikidata Q78101401))" + }, + { + "q": "What is the electric current for mutual inductance 169.527 and total magnetic flux 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [169.526958, 280.773702] (the system mutual inductance (Wikidata Q78101401))" + }, + { + "q": "Tell me the electric current with mutual inductance of 169.527 and total magnetic flux of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [169.526958, 280.773702] (the system mutual inductance (Wikidata Q78101401))" + }, + { + "q": "What is the electric current for mutual inductance 1.91195 and total magnetic flux 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [1.911952, 82.240072] (the system mutual inductance (Wikidata Q78101401))" + }, + { + "q": "Tell me the electric current with mutual inductance of 1.91195 and total magnetic flux of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [1.911952, 82.240072] (the system mutual inductance (Wikidata Q78101401))" + }, + { + "q": "What is the electric current for mutual inductance 1.64379 and total magnetic flux 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [1.643795, 252.709024] (the system mutual inductance (Wikidata Q78101401))" + }, + { + "q": "Tell me the electric current with mutual inductance of 1.64379 and total magnetic flux of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [1.643795, 252.709024] (the system mutual inductance (Wikidata Q78101401))" + }, + { + "q": "What is the electric current for mutual inductance 18.3169 and total magnetic flux 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [18.316867, 6.838598] (the system mutual inductance (Wikidata Q78101401))" + }, + { + "q": "Tell me the electric current with mutual inductance of 18.3169 and total magnetic flux of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [18.316867, 6.838598] (the system mutual inductance (Wikidata Q78101401))" + }, + { + "q": "What is the electric current for mutual inductance 156.502 and total magnetic flux 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [156.50181, 185.886273] (the system mutual inductance (Wikidata Q78101401))" + }, + { + "q": "Tell me the electric current with mutual inductance of 156.502 and total magnetic flux of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [156.50181, 185.886273] (the system mutual inductance (Wikidata Q78101401))" + }, + { + "q": "What is the electric current for mutual inductance 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total magnetic flux is not given" + }, + { + "q": "Tell me the electric current with mutual inductance of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total magnetic flux is not given" + }, + { + "q": "What is the 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 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 electric current with mutual inductance of -1.91195 and total magnetic flux of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mutual inductance: no example had one" + }, + { + "q": "Which 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 mutual inductance is 8.50728 and the total magnetic flux is 17.7888, what is the electric current?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [8.507283, 17.788778] (the system mutual inductance (Wikidata Q78101401))" + }, + { + "q": "Could you work out the electric current given mutual inductance = 8.50728, total magnetic flux = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [8.507283, 17.788778] (the system mutual inductance (Wikidata Q78101401))" + }, + { + "q": "I need the electric current for a mutual inductance of 8.24951 and a total magnetic flux of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [8.24951, 0.634021] (the system mutual inductance (Wikidata Q78101401))" + }, + { + "q": "If the mutual inductance is 8.24951 and the total magnetic flux is 0.634021, what is the electric current?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [8.24951, 0.634021] (the system mutual inductance (Wikidata Q78101401))" + }, + { + "q": "Could you work out the electric current given mutual inductance = 0.318734, total magnetic flux = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [0.318734, 9.566071] (the system mutual inductance (Wikidata Q78101401))" + }, + { + "q": "I need the electric current for a mutual inductance of 0.318734 and a total magnetic flux of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [0.318734, 9.566071] (the system mutual inductance (Wikidata Q78101401))" + }, + { + "q": "If the mutual inductance is 0.671697 and the total magnetic flux is 14.2496, what is the electric current?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [0.671697, 14.249569] (the system mutual inductance (Wikidata Q78101401))" + }, + { + "q": "Could you work out the electric current given mutual inductance = 0.671697, total magnetic flux = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [0.671697, 14.249569] (the system mutual inductance (Wikidata Q78101401))" + }, + { + "q": "I need the electric current for a mutual inductance of 21.7931 and a total magnetic flux of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [21.793126, 29.510609] (the system mutual inductance (Wikidata Q78101401))" + }, + { + "q": "If the mutual inductance is 21.7931 and the total magnetic flux is 29.5106, what is the electric current?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [21.793126, 29.510609] (the system mutual inductance (Wikidata Q78101401))" + }, + { + "q": "Could you work out the electric current given mutual inductance = 153.15, total magnetic flux = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [153.149965, 18.595315] (the system mutual inductance (Wikidata Q78101401))" + }, + { + "q": "I need the electric current for a mutual inductance of 153.15 and a total magnetic flux of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [153.149965, 18.595315] (the system mutual inductance (Wikidata Q78101401))" + }, + { + "q": "If the mutual inductance is 8.50728, what is the electric current?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total magnetic flux is not given" + }, + { + "q": "Could you work out the electric current given mutual inductance = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the total magnetic flux is not given" + }, + { + "q": "Do you know the 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 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 electric current given mutual inductance = -0.318734, total magnetic flux = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative mutual inductance: no example had one" + }, + { + "q": "What is a typical 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-electric-current-6/program.py b/root/organs10/physics/specialists/solved-electric-current-6/program.py new file mode 100644 index 0000000000000000000000000000000000000000..3f0b43f597fe9507f0624b7eeafcdcaaeb58f4e0 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-6/program.py @@ -0,0 +1,7 @@ +"""electric current from mutual inductance, total magnetic flux (mutual inductance (Wikidata Q78101401)) +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(mutual_inductance, total_magnetic_flux): + return 1.0*total_magnetic_flux/mutual_inductance diff --git a/root/organs10/physics/specialists/solved-electric-current-6/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-current-6/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-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-electric-current-density-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-current-density-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-density-2/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-electric-current-density-2/panel.json b/root/organs10/physics/specialists/solved-electric-current-density-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..e20a3b57bb1f6c6f1a5d4386136400c3391e25f5 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-density-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric current density for electric field strength 107.961 and electrical conductivity 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [107.960666, 59.4105] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "Tell me the electric current density with electric field strength of 107.961 and electrical conductivity of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [107.960666, 59.4105] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "What is the electric current density for electric field strength 169.527 and electrical conductivity 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [169.526958, 280.773702] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "Tell me the electric current density with electric field strength of 169.527 and electrical conductivity of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [169.526958, 280.773702] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "What is the electric current density for electric field strength 1.91195 and electrical conductivity 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [1.911952, 82.240072] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "Tell me the electric current density with electric field strength of 1.91195 and electrical conductivity of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [1.911952, 82.240072] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "What is the electric current density for electric field strength 1.64379 and electrical conductivity 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [1.643795, 252.709024] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "Tell me the electric current density with electric field strength of 1.64379 and electrical conductivity of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [1.643795, 252.709024] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "What is the electric current density for electric field strength 18.3169 and electrical conductivity 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [18.316867, 6.838598] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "Tell me the electric current density with electric field strength of 18.3169 and electrical conductivity of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [18.316867, 6.838598] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "What is the electric current density for electric field strength 156.502 and electrical conductivity 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [156.50181, 185.886273] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "Tell me the electric current density with electric field strength of 156.502 and electrical conductivity of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [156.50181, 185.886273] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "What is the electric current density for electric field strength 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electrical conductivity is not given" + }, + { + "q": "Tell me the electric current density with electric field strength of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electrical conductivity is not given" + }, + { + "q": "What is the electric current density of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the electric current density of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the electric current density with electric field strength of -1.91195 and electrical conductivity of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric field strength: no example had one" + }, + { + "q": "Which electric current density is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the electric field strength is 8.50728 and the electrical conductivity is 17.7888, what is the electric current density?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [8.507283, 17.788778] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "Could you work out the electric current density given electric field strength = 8.50728, electrical conductivity = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [8.507283, 17.788778] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "I need the electric current density for an electric field strength of 8.24951 and an electrical conductivity of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [8.24951, 0.634021] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "If the electric field strength is 8.24951 and the electrical conductivity is 0.634021, what is the electric current density?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [8.24951, 0.634021] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "Could you work out the electric current density given electric field strength = 0.318734, electrical conductivity = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [0.318734, 9.566071] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "I need the electric current density for an electric field strength of 0.318734 and an electrical conductivity of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [0.318734, 9.566071] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "If the electric field strength is 0.671697 and the electrical conductivity is 14.2496, what is the electric current density?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [0.671697, 14.249569] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "Could you work out the electric current density given electric field strength = 0.671697, electrical conductivity = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [0.671697, 14.249569] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "I need the electric current density for an electric field strength of 21.7931 and an electrical conductivity of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [21.793126, 29.510609] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "If the electric field strength is 21.7931 and the electrical conductivity is 29.5106, what is the electric current density?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [21.793126, 29.510609] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "Could you work out the electric current density given electric field strength = 153.15, electrical conductivity = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [153.149965, 18.595315] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "I need the electric current density for an electric field strength of 153.15 and an electrical conductivity of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [153.149965, 18.595315] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "If the electric field strength is 8.50728, what is the electric current density?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electrical conductivity is not given" + }, + { + "q": "Could you work out the electric current density given electric field strength = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electrical conductivity is not given" + }, + { + "q": "Do you know the electric current density of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the electric current density of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the electric current density given electric field strength = -0.318734, electrical conductivity = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric field strength: no example had one" + }, + { + "q": "What is a typical electric current density?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-electric-current-density-2/program.py b/root/organs10/physics/specialists/solved-electric-current-density-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..3bcc0f38fd53d3790ff5efcadba7cec76ccb9afc --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-density-2/program.py @@ -0,0 +1,7 @@ +"""electric current density from electric field strength, electrical conductivity (electrical conductivity (Wikidata Q4593291)) +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(electric_field_strength, electrical_conductivity): + return 1.0*electric_field_strength*electrical_conductivity diff --git a/root/organs10/physics/specialists/solved-electric-current-density-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-current-density-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-density-2/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-electric-current-density-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-current-density-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-density-3/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-electric-current-density-3/panel.json b/root/organs10/physics/specialists/solved-electric-current-density-3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..dc6d14f4f6adfbb787313f868acac66f86208227 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-density-3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric current density for total current density 107.961 and displacement current density 59.4105?", + "a": "48.55016569879068", + "valid": true, + "kind": "compute", + "expect": 48.55016569879068, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [107.960666, 59.4105] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "Tell me the electric current density with total current density of 107.961 and displacement current density of 59.4105.", + "a": "48.55016569879068", + "valid": true, + "kind": "compute", + "expect": 48.55016569879068, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [107.960666, 59.4105] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "What is the electric current density for total current density 157.223 and displacement current density 35.6412?", + "a": "121.5814030054458", + "valid": true, + "kind": "compute", + "expect": 121.5814030054458, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [157.22256, 35.641157] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "Tell me the electric current density with total current density of 157.223 and displacement current density of 35.6412.", + "a": "121.5814030054458", + "valid": true, + "kind": "compute", + "expect": 121.5814030054458, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [157.22256, 35.641157] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "What is the electric current density for total current density 126.709 and displacement current density 1.70503?", + "a": "125.00421942160824", + "valid": true, + "kind": "compute", + "expect": 125.00421942160824, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [126.709252, 1.705032] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "Tell me the electric current density with total current density of 126.709 and displacement current density of 1.70503.", + "a": "125.00421942160824", + "valid": true, + "kind": "compute", + "expect": 125.00421942160824, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [126.709252, 1.705032] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "What is the electric current density for total current density 41.6566 and displacement current density 13.8779?", + "a": "27.77877186905885", + "valid": true, + "kind": "compute", + "expect": 27.77877186905885, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [41.656639, 13.877867] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "Tell me the electric current density with total current density of 41.6566 and displacement current density of 13.8779.", + "a": "27.77877186905885", + "valid": true, + "kind": "compute", + "expect": 27.77877186905885, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [41.656639, 13.877867] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "What is the electric current density for total current density 8.50341 and displacement current density 0.58813?", + "a": "7.91527752848457", + "valid": true, + "kind": "compute", + "expect": 7.91527752848457, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [8.503407, 0.58813] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "Tell me the electric current density with total current density of 8.50341 and displacement current density of 0.58813.", + "a": "7.91527752848457", + "valid": true, + "kind": "compute", + "expect": 7.91527752848457, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [8.503407, 0.58813] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "What is the electric current density for total current density 223.509 and displacement current density 17.3414?", + "a": "206.1673613150397", + "valid": true, + "kind": "compute", + "expect": 206.1673613150397, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [223.508796, 17.341435] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "Tell me the electric current density with total current density of 223.509 and displacement current density of 17.3414.", + "a": "206.1673613150397", + "valid": true, + "kind": "compute", + "expect": 206.1673613150397, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [223.508796, 17.341435] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "What is the electric current density for total current density 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the displacement current density is not given" + }, + { + "q": "Tell me the electric current density with total current density of 157.223.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the displacement current density is not given" + }, + { + "q": "What is the electric current density of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the electric current density of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the electric current density with total current density of -126.709 and displacement current density of 1.70503.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative total current density: no example had one" + }, + { + "q": "Which electric current density is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the total current density is 167.625 and the displacement current density is 10.3291, what is the electric current density?", + "a": "157.29589332353083", + "valid": true, + "kind": "compute", + "expect": 157.29589332353083, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [167.624948, 10.329054] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "Could you work out the electric current density given total current density = 167.625, displacement current density = 10.3291?", + "a": "157.29589332353083", + "valid": true, + "kind": "compute", + "expect": 157.29589332353083, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [167.624948, 10.329054] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "I need the electric current density for a total current density of 13.999 and a displacement current density of 0.348438, please.", + "a": "13.650591275493905", + "valid": true, + "kind": "compute", + "expect": 13.650591275493905, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [13.999029, 0.348438] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "If the total current density is 13.999 and the displacement current density is 0.348438, what is the electric current density?", + "a": "13.650591275493905", + "valid": true, + "kind": "compute", + "expect": 13.650591275493905, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [13.999029, 0.348438] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "Could you work out the electric current density given total current density = 81.1967, displacement current density = 6.98086?", + "a": "74.21584883614992", + "valid": true, + "kind": "compute", + "expect": 74.21584883614992, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [81.196705, 6.980857] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "I need the electric current density for a total current density of 81.1967 and a displacement current density of 6.98086, please.", + "a": "74.21584883614992", + "valid": true, + "kind": "compute", + "expect": 74.21584883614992, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [81.196705, 6.980857] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "If the total current density is 19.4474 and the displacement current density is 4.51548, what is the electric current density?", + "a": "14.931881955429075", + "valid": true, + "kind": "compute", + "expect": 14.931881955429075, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [19.447357, 4.515475] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "Could you work out the electric current density given total current density = 19.4474, displacement current density = 4.51548?", + "a": "14.931881955429075", + "valid": true, + "kind": "compute", + "expect": 14.931881955429075, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [19.447357, 4.515475] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "I need the electric current density for a total current density of 13.2373 and a displacement current density of 4.71941, please.", + "a": "8.517896269006052", + "valid": true, + "kind": "compute", + "expect": 8.517896269006052, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [13.237311, 4.719415] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "If the total current density is 13.2373 and the displacement current density is 4.71941, what is the electric current density?", + "a": "8.517896269006052", + "valid": true, + "kind": "compute", + "expect": 8.517896269006052, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [13.237311, 4.719415] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "Could you work out the electric current density given total current density = 21.7946, displacement current density = 9.1061?", + "a": "12.68851669739933", + "valid": true, + "kind": "compute", + "expect": 12.68851669739933, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [21.794621, 9.106104] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "I need the electric current density for a total current density of 21.7946 and a displacement current density of 9.1061, please.", + "a": "12.68851669739933", + "valid": true, + "kind": "compute", + "expect": 12.68851669739933, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [21.794621, 9.106104] (the system total current density (Wikidata Q77680811))" + }, + { + "q": "If the total current density is 167.625, what is the electric current density?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the displacement current density is not given" + }, + { + "q": "Could you work out the electric current density given total current density = 13.999?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the displacement current density is not given" + }, + { + "q": "Do you know the electric current density of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the electric current density of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the electric current density given total current density = -81.1967, displacement current density = 6.98086?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative total current density: no example had one" + }, + { + "q": "What is a typical electric current density?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-electric-current-density-3/program.py b/root/organs10/physics/specialists/solved-electric-current-density-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..00f144c7dee13261b60b04d6f2d2397f02de4fd8 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-density-3/program.py @@ -0,0 +1,7 @@ +"""electric current density from total current density, displacement current density (total current density (Wikidata Q77680811)) +The maths organ (systems.py): solved from the system q0 = q2 + q1; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(total_current_density, displacement_current_density): + return -1.0*displacement_current_density + 1.0*total_current_density diff --git a/root/organs10/physics/specialists/solved-electric-current-density-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-current-density-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-density-3/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-electric-current-density-4/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-current-density-4/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-density-4/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-electric-current-density-4/panel.json b/root/organs10/physics/specialists/solved-electric-current-density-4/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..8604223dc75b821a17357d9a38120afbb189d383 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-density-4/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric current density for conductivity tensor 107.961 and electric field strength 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [107.960666, 59.4105] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "Tell me the electric current density with conductivity tensor of 107.961 and electric field strength of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [107.960666, 59.4105] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "What is the electric current density for conductivity tensor 169.527 and electric field strength 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [169.526958, 280.773702] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "Tell me the electric current density with conductivity tensor of 169.527 and electric field strength of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [169.526958, 280.773702] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "What is the electric current density for conductivity tensor 1.91195 and electric field strength 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [1.911952, 82.240072] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "Tell me the electric current density with conductivity tensor of 1.91195 and electric field strength of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [1.911952, 82.240072] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "What is the electric current density for conductivity tensor 1.64379 and electric field strength 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [1.643795, 252.709024] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "Tell me the electric current density with conductivity tensor of 1.64379 and electric field strength of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [1.643795, 252.709024] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "What is the electric current density for conductivity tensor 18.3169 and electric field strength 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [18.316867, 6.838598] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "Tell me the electric current density with conductivity tensor of 18.3169 and electric field strength of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [18.316867, 6.838598] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "What is the electric current density for conductivity tensor 156.502 and electric field strength 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [156.50181, 185.886273] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "Tell me the electric current density with conductivity tensor of 156.502 and electric field strength of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [156.50181, 185.886273] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "What is the electric current density for conductivity tensor 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 electric current density with conductivity tensor 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 electric current density of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the electric current density of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the electric current density with conductivity tensor 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 conductivity tensor: no example had one" + }, + { + "q": "Which electric current density is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the conductivity tensor is 8.50728 and the electric field strength is 17.7888, what is the electric current density?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [8.507283, 17.788778] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "Could you work out the electric current density given conductivity tensor = 8.50728, electric field strength = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [8.507283, 17.788778] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "I need the electric current density for a conductivity tensor of 8.24951 and an electric field strength of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [8.24951, 0.634021] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "If the conductivity tensor is 8.24951 and the electric field strength is 0.634021, what is the electric current density?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [8.24951, 0.634021] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "Could you work out the electric current density given conductivity tensor = 0.318734, electric field strength = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [0.318734, 9.566071] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "I need the electric current density for a conductivity tensor of 0.318734 and an electric field strength of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [0.318734, 9.566071] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "If the conductivity tensor is 0.671697 and the electric field strength is 14.2496, what is the electric current density?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [0.671697, 14.249569] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "Could you work out the electric current density given conductivity tensor = 0.671697, electric field strength = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [0.671697, 14.249569] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "I need the electric current density for a conductivity tensor of 21.7931 and an electric field strength of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [21.793126, 29.510609] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "If the conductivity tensor is 21.7931 and the electric field strength is 29.5106, what is the electric current density?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [21.793126, 29.510609] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "Could you work out the electric current density given conductivity tensor = 153.15, electric field strength = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [153.149965, 18.595315] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "I need the electric current density for a conductivity tensor of 153.15 and an electric field strength of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [153.149965, 18.595315] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "If the conductivity tensor is 8.50728, what is the electric current density?", + "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 electric current density given conductivity tensor = 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 electric current density of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the electric current density of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the electric current density given conductivity tensor = -0.318734, electric field strength = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative conductivity tensor: no example had one" + }, + { + "q": "What is a typical electric current density?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-electric-current-density-4/program.py b/root/organs10/physics/specialists/solved-electric-current-density-4/program.py new file mode 100644 index 0000000000000000000000000000000000000000..dd8663ab2b0f3ba44ce701f41f6e7ed8192add05 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-density-4/program.py @@ -0,0 +1,7 @@ +"""electric current density from conductivity tensor, electric field strength (conductivity tensor (Wikidata Q4454679)) +The maths organ (systems.py): solved from the system q2 = q1 * q3; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(conductivity_tensor, electric_field_strength): + return 1.0*conductivity_tensor*electric_field_strength diff --git a/root/organs10/physics/specialists/solved-electric-current-density-4/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-current-density-4/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-density-4/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-electric-current-density-5/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-current-density-5/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-density-5/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-electric-current-density-5/panel.json b/root/organs10/physics/specialists/solved-electric-current-density-5/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..844ec5059650e9bb099a5fabe0432bcdc90e6d5a --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-density-5/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric current density for electrolytic conductivity 107.961 and electric field strength 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [107.960666, 59.4105] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "Tell me the electric current density with electrolytic conductivity of 107.961 and electric field strength of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [107.960666, 59.4105] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "What is the electric current density for electrolytic conductivity 169.527 and electric field strength 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [169.526958, 280.773702] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "Tell me the electric current density with electrolytic conductivity of 169.527 and electric field strength of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [169.526958, 280.773702] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "What is the electric current density for electrolytic conductivity 1.91195 and electric field strength 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [1.911952, 82.240072] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "Tell me the electric current density with electrolytic conductivity of 1.91195 and electric field strength of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [1.911952, 82.240072] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "What is the electric current density for electrolytic conductivity 1.64379 and electric field strength 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [1.643795, 252.709024] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "Tell me the electric current density with electrolytic conductivity of 1.64379 and electric field strength of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [1.643795, 252.709024] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "What is the electric current density for electrolytic conductivity 18.3169 and electric field strength 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [18.316867, 6.838598] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "Tell me the electric current density with electrolytic conductivity of 18.3169 and electric field strength of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [18.316867, 6.838598] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "What is the electric current density for electrolytic conductivity 156.502 and electric field strength 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [156.50181, 185.886273] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "Tell me the electric current density with electrolytic conductivity of 156.502 and electric field strength of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [156.50181, 185.886273] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "What is the electric current density for electrolytic conductivity 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 electric current density with electrolytic conductivity 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 electric current density of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the electric current density of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the electric current density with electrolytic conductivity 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 electrolytic conductivity: no example had one" + }, + { + "q": "Which electric current density is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the electrolytic conductivity is 8.50728 and the electric field strength is 17.7888, what is the electric current density?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [8.507283, 17.788778] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "Could you work out the electric current density given electrolytic conductivity = 8.50728, electric field strength = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [8.507283, 17.788778] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "I need the electric current density for an electrolytic conductivity of 8.24951 and an electric field strength of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [8.24951, 0.634021] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "If the electrolytic conductivity is 8.24951 and the electric field strength is 0.634021, what is the electric current density?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [8.24951, 0.634021] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "Could you work out the electric current density given electrolytic conductivity = 0.318734, electric field strength = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [0.318734, 9.566071] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "I need the electric current density for an electrolytic conductivity of 0.318734 and an electric field strength of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [0.318734, 9.566071] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "If the electrolytic conductivity is 0.671697 and the electric field strength is 14.2496, what is the electric current density?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [0.671697, 14.249569] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "Could you work out the electric current density given electrolytic conductivity = 0.671697, electric field strength = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [0.671697, 14.249569] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "I need the electric current density for an electrolytic conductivity of 21.7931 and an electric field strength of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [21.793126, 29.510609] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "If the electrolytic conductivity is 21.7931 and the electric field strength is 29.5106, what is the electric current density?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [21.793126, 29.510609] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "Could you work out the electric current density given electrolytic conductivity = 153.15, electric field strength = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [153.149965, 18.595315] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "I need the electric current density for an electrolytic conductivity of 153.15 and an electric field strength of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [153.149965, 18.595315] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "If the electrolytic conductivity is 8.50728, what is the electric current density?", + "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 electric current density given electrolytic conductivity = 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 electric current density of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the electric current density of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the electric current density given electrolytic conductivity = -0.318734, electric field strength = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electrolytic conductivity: no example had one" + }, + { + "q": "What is a typical electric current density?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-electric-current-density-5/program.py b/root/organs10/physics/specialists/solved-electric-current-density-5/program.py new file mode 100644 index 0000000000000000000000000000000000000000..714e1ba8f7a1e3138ae7e58dc64a4691dd141229 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-density-5/program.py @@ -0,0 +1,7 @@ +"""electric current density from electrolytic conductivity, electric field strength (electrolytic conductivity (Wikidata Q907564)) +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(electrolytic_conductivity, electric_field_strength): + return 1.0*electric_field_strength*electrolytic_conductivity diff --git a/root/organs10/physics/specialists/solved-electric-current-density-5/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-current-density-5/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-density-5/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-electric-current-density/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-current-density/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-density/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-electric-current-density/panel.json b/root/organs10/physics/specialists/solved-electric-current-density/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..1cc6b7f25b19394aee053d51dfb42d24f5549ad0 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-density/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric current density for resistivity tensor 107.961 and electric field strength 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [107.960666, 59.4105] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Tell me the electric current density with resistivity tensor of 107.961 and electric field strength of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [107.960666, 59.4105] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "What is the electric current density for resistivity tensor 169.527 and electric field strength 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [169.526958, 280.773702] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Tell me the electric current density with resistivity tensor of 169.527 and electric field strength of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [169.526958, 280.773702] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "What is the electric current density for resistivity tensor 1.91195 and electric field strength 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [1.911952, 82.240072] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Tell me the electric current density with resistivity tensor of 1.91195 and electric field strength of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [1.911952, 82.240072] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "What is the electric current density for resistivity tensor 1.64379 and electric field strength 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [1.643795, 252.709024] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Tell me the electric current density with resistivity tensor of 1.64379 and electric field strength of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [1.643795, 252.709024] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "What is the electric current density for resistivity tensor 18.3169 and electric field strength 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [18.316867, 6.838598] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Tell me the electric current density with resistivity tensor of 18.3169 and electric field strength of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [18.316867, 6.838598] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "What is the electric current density for resistivity tensor 156.502 and electric field strength 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [156.50181, 185.886273] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Tell me the electric current density with resistivity tensor of 156.502 and electric field strength of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [156.50181, 185.886273] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "What is the electric current density for resistivity tensor 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 electric current density with resistivity tensor 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 electric current density of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the electric current density of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the electric current density with resistivity tensor 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 resistivity tensor: no example had one" + }, + { + "q": "Which electric current density is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the resistivity tensor is 8.50728 and the electric field strength is 17.7888, what is the electric current density?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [8.507283, 17.788778] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Could you work out the electric current density given resistivity tensor = 8.50728, electric field strength = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [8.507283, 17.788778] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "I need the electric current density for a resistivity tensor 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": "A/m^2", + "why": "electric current density for [8.24951, 0.634021] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "If the resistivity tensor is 8.24951 and the electric field strength is 0.634021, what is the electric current density?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [8.24951, 0.634021] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Could you work out the electric current density given resistivity tensor = 0.318734, electric field strength = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [0.318734, 9.566071] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "I need the electric current density for a resistivity tensor 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": "A/m^2", + "why": "electric current density for [0.318734, 9.566071] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "If the resistivity tensor is 0.671697 and the electric field strength is 14.2496, what is the electric current density?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [0.671697, 14.249569] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Could you work out the electric current density given resistivity tensor = 0.671697, electric field strength = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [0.671697, 14.249569] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "I need the electric current density for a resistivity tensor 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": "A/m^2", + "why": "electric current density for [21.793126, 29.510609] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "If the resistivity tensor is 21.7931 and the electric field strength is 29.5106, what is the electric current density?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [21.793126, 29.510609] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Could you work out the electric current density given resistivity tensor = 153.15, electric field strength = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "A/m^2", + "why": "electric current density for [153.149965, 18.595315] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "I need the electric current density for a resistivity tensor 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": "A/m^2", + "why": "electric current density for [153.149965, 18.595315] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "If the resistivity tensor is 8.50728, what is the electric current density?", + "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 electric current density given resistivity tensor = 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 electric current density of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the electric current density of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the electric current density given resistivity tensor = -0.318734, electric field strength = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative resistivity tensor: no example had one" + }, + { + "q": "What is a typical electric current density?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-electric-current-density/program.py b/root/organs10/physics/specialists/solved-electric-current-density/program.py new file mode 100644 index 0000000000000000000000000000000000000000..2f09df760bc683b966b8617883aee6778f15fb74 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-density/program.py @@ -0,0 +1,7 @@ +"""electric current density from resistivity tensor, 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(resistivity_tensor, electric_field_strength): + return 1.0*electric_field_strength/resistivity_tensor diff --git a/root/organs10/physics/specialists/solved-electric-current-density/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-current-density/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-density/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-electric-current-of-ion/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-current-of-ion/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-of-ion/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-electric-current-of-ion/panel.json b/root/organs10/physics/specialists/solved-electric-current-of-ion/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..82463aae20ad79704ccc55377827993bed448360 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-of-ion/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric current of ion for ion transport number 107.961 and electric current 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "A", + "why": "electric current of ion for [107.960666, 59.4105] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Tell me the electric current of ion with ion transport number of 107.961 and electric current of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "A", + "why": "electric current of ion for [107.960666, 59.4105] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "What is the electric current of ion for ion transport number 169.527 and electric current 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "A", + "why": "electric current of ion for [169.526958, 280.773702] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Tell me the electric current of ion with ion transport number of 169.527 and electric current of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "A", + "why": "electric current of ion for [169.526958, 280.773702] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "What is the electric current of ion for ion transport number 1.91195 and electric current 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "A", + "why": "electric current of ion for [1.911952, 82.240072] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Tell me the electric current of ion with ion transport number of 1.91195 and electric current of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "A", + "why": "electric current of ion for [1.911952, 82.240072] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "What is the electric current of ion for ion transport number 1.64379 and electric current 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "A", + "why": "electric current of ion for [1.643795, 252.709024] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Tell me the electric current of ion with ion transport number of 1.64379 and electric current of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "A", + "why": "electric current of ion for [1.643795, 252.709024] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "What is the electric current of ion for ion transport number 18.3169 and electric current 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "A", + "why": "electric current of ion for [18.316867, 6.838598] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Tell me the electric current of ion with ion transport number of 18.3169 and electric current of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "A", + "why": "electric current of ion for [18.316867, 6.838598] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "What is the electric current of ion for ion transport number 156.502 and electric current 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "A", + "why": "electric current of ion for [156.50181, 185.886273] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Tell me the electric current of ion with ion transport number of 156.502 and electric current of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "A", + "why": "electric current of ion for [156.50181, 185.886273] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "What is the electric current of ion for ion transport number 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current is not given" + }, + { + "q": "Tell me the electric current of ion with ion transport number of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current is not given" + }, + { + "q": "What is the electric current of ion 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 electric current of ion 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 electric current of ion with ion transport number of -1.91195 and electric current of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative ion transport number: no example had one" + }, + { + "q": "Which electric current of ion 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 ion transport number is 8.50728 and the electric current is 17.7888, what is the electric current of ion?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "A", + "why": "electric current of ion for [8.507283, 17.788778] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Could you work out the electric current of ion given ion transport number = 8.50728, electric current = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "A", + "why": "electric current of ion for [8.507283, 17.788778] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "I need the electric current of ion for an ion transport number of 8.24951 and an electric current of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "A", + "why": "electric current of ion for [8.24951, 0.634021] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "If the ion transport number is 8.24951 and the electric current is 0.634021, what is the electric current of ion?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "A", + "why": "electric current of ion for [8.24951, 0.634021] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Could you work out the electric current of ion given ion transport number = 0.318734, electric current = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "A", + "why": "electric current of ion for [0.318734, 9.566071] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "I need the electric current of ion for an ion transport number of 0.318734 and an electric current of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "A", + "why": "electric current of ion for [0.318734, 9.566071] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "If the ion transport number is 0.671697 and the electric current is 14.2496, what is the electric current of ion?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "A", + "why": "electric current of ion for [0.671697, 14.249569] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Could you work out the electric current of ion given ion transport number = 0.671697, electric current = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "A", + "why": "electric current of ion for [0.671697, 14.249569] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "I need the electric current of ion for an ion transport number of 21.7931 and an electric current of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "A", + "why": "electric current of ion for [21.793126, 29.510609] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "If the ion transport number is 21.7931 and the electric current is 29.5106, what is the electric current of ion?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "A", + "why": "electric current of ion for [21.793126, 29.510609] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "Could you work out the electric current of ion given ion transport number = 153.15, electric current = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "A", + "why": "electric current of ion for [153.149965, 18.595315] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "I need the electric current of ion for an ion transport number of 153.15 and an electric current of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "A", + "why": "electric current of ion for [153.149965, 18.595315] (the system ion transport number (Wikidata Q331854))" + }, + { + "q": "If the ion transport number is 8.50728, what is the electric current of ion?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current is not given" + }, + { + "q": "Could you work out the electric current of ion given ion transport number = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current is not given" + }, + { + "q": "Do you know the electric current of ion 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 electric current of ion 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 electric current of ion given ion transport number = -0.318734, electric current = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative ion transport number: no example had one" + }, + { + "q": "What is a typical electric current of ion?", + "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-electric-current-of-ion/program.py b/root/organs10/physics/specialists/solved-electric-current-of-ion/program.py new file mode 100644 index 0000000000000000000000000000000000000000..c091f0991e93912470e59ffd6a644a62e57fa318 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-of-ion/program.py @@ -0,0 +1,7 @@ +"""electric current of ion from ion transport number, electric current (ion transport number (Wikidata Q331854)) +The maths organ (systems.py): solved from the system q0 = ((q1)/(q2)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(ion_transport_number, electric_current): + return 1.0*electric_current*ion_transport_number diff --git a/root/organs10/physics/specialists/solved-electric-current-of-ion/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-current-of-ion/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current-of-ion/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-electric-current/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-current/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-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-electric-current/panel.json b/root/organs10/physics/specialists/solved-electric-current/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..68ffa133b066372cd1ab4e0e2914709b62b00b77 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric current for electric power 107.961 and voltage 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [107.960666, 59.4105] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "Tell me the electric current with electric power of 107.961 and voltage of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [107.960666, 59.4105] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "What is the electric current for electric power 169.527 and voltage 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [169.526958, 280.773702] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "Tell me the electric current with electric power of 169.527 and voltage of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [169.526958, 280.773702] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "What is the electric current for electric power 1.91195 and voltage 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [1.911952, 82.240072] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "Tell me the electric current with electric power of 1.91195 and voltage of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [1.911952, 82.240072] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "What is the electric current for electric power 1.64379 and voltage 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [1.643795, 252.709024] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "Tell me the electric current with electric power of 1.64379 and voltage of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [1.643795, 252.709024] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "What is the electric current for electric power 18.3169 and voltage 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [18.316867, 6.838598] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "Tell me the electric current with electric power of 18.3169 and voltage of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [18.316867, 6.838598] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "What is the electric current for electric power 156.502 and voltage 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [156.50181, 185.886273] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "Tell me the electric current with electric power of 156.502 and voltage of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [156.50181, 185.886273] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "What is the electric current for electric power 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the voltage is not given" + }, + { + "q": "Tell me the electric current with electric power of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the voltage is not given" + }, + { + "q": "What is the 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 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 electric current with electric power of -1.91195 and voltage of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric power: no example had one" + }, + { + "q": "Which 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 electric power is 8.50728 and the voltage is 17.7888, what is the electric current?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [8.507283, 17.788778] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "Could you work out the electric current given electric power = 8.50728, voltage = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [8.507283, 17.788778] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "I need the electric current for an electric power of 8.24951 and a voltage of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [8.24951, 0.634021] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "If the electric power is 8.24951 and the voltage is 0.634021, what is the electric current?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [8.24951, 0.634021] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "Could you work out the electric current given electric power = 0.318734, voltage = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [0.318734, 9.566071] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "I need the electric current for an electric power of 0.318734 and a voltage of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [0.318734, 9.566071] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "If the electric power is 0.671697 and the voltage is 14.2496, what is the electric current?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [0.671697, 14.249569] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "Could you work out the electric current given electric power = 0.671697, voltage = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [0.671697, 14.249569] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "I need the electric current for an electric power of 21.7931 and a voltage of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [21.793126, 29.510609] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "If the electric power is 21.7931 and the voltage is 29.5106, what is the electric current?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [21.793126, 29.510609] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "Could you work out the electric current given electric power = 153.15, voltage = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [153.149965, 18.595315] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "I need the electric current for an electric power of 153.15 and a voltage of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "A", + "why": "electric current for [153.149965, 18.595315] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "If the electric power is 8.50728, what is the electric current?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the voltage is not given" + }, + { + "q": "Could you work out the electric current given electric power = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the voltage is not given" + }, + { + "q": "Do you know the 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 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 electric current given electric power = -0.318734, voltage = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric power: no example had one" + }, + { + "q": "What is a typical 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-electric-current/program.py b/root/organs10/physics/specialists/solved-electric-current/program.py new file mode 100644 index 0000000000000000000000000000000000000000..867ee1fef4bf9806d5aab0a7a46c6d30cab3385f --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-current/program.py @@ -0,0 +1,7 @@ +"""electric current from electric power, voltage (Joule's laws (Wikidata Q210009)) +The maths organ (systems.py): solved from the system q0 = q1*q2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(electric_power, voltage): + return 1.0*electric_power/voltage diff --git a/root/organs10/physics/specialists/solved-electric-current/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-current/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-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-electric-dipole-moment/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-dipole-moment/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-dipole-moment/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-electric-dipole-moment/panel.json b/root/organs10/physics/specialists/solved-electric-dipole-moment/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..13b64852a4d4edf59076345365fd9d071be4a9a6 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-dipole-moment/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric dipole moment for position of the positively charged particle 107.961, position of the negatively charged particle 59.4105 and electric charge 5.77717?", + "a": "280.482299296702", + "valid": true, + "kind": "compute", + "expect": 280.482299296702, + "dtype": "quantity", + "unit": "m*s*A", + "why": "electric dipole moment for [107.960666, 59.4105, 5.777165] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "Tell me the electric dipole moment with position of the positively charged particle of 107.961, position of the negatively charged particle of 59.4105 and electric charge of 5.77717.", + "a": "280.482299296702", + "valid": true, + "kind": "compute", + "expect": 280.482299296702, + "dtype": "quantity", + "unit": "m*s*A", + "why": "electric dipole moment for [107.960666, 59.4105, 5.777165] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "What is the electric dipole moment for position of the positively charged particle 82.2401, position of the negatively charged particle 13.999 and electric charge 0.348438?", + "a": "23.777749960341175", + "valid": true, + "kind": "compute", + "expect": 23.777749960341175, + "dtype": "quantity", + "unit": "m*s*A", + "why": "electric dipole moment for [82.240072, 13.999029, 0.348438] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "Tell me the electric dipole moment with position of the positively charged particle of 82.2401, position of the negatively charged particle of 13.999 and electric charge of 0.348438.", + "a": "23.777749960341175", + "valid": true, + "kind": "compute", + "expect": 23.777749960341175, + "dtype": "quantity", + "unit": "m*s*A", + "why": "electric dipole moment for [82.240072, 13.999029, 0.348438] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "What is the electric dipole moment for position of the positively charged particle 6.98086, position of the negatively charged particle 0.551236 and electric charge 2.88512?", + "a": "18.55022441872921", + "valid": true, + "kind": "compute", + "expect": 18.55022441872921, + "dtype": "quantity", + "unit": "m*s*A", + "why": "electric dipole moment for [6.980857, 0.551236, 2.88512] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "Tell me the electric dipole moment with position of the positively charged particle of 6.98086, position of the negatively charged particle of 0.551236 and electric charge of 2.88512.", + "a": "18.55022441872921", + "valid": true, + "kind": "compute", + "expect": 18.55022441872921, + "dtype": "quantity", + "unit": "m*s*A", + "why": "electric dipole moment for [6.980857, 0.551236, 2.88512] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "What is the electric dipole moment for position of the positively charged particle 2.34957, position of the negatively charged particle 1.16997 and electric charge 1.14865?", + "a": "1.3549532766451011", + "valid": true, + "kind": "compute", + "expect": 1.3549532766451011, + "dtype": "quantity", + "unit": "m*s*A", + "why": "electric dipole moment for [2.349571, 1.169968, 1.148652] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "Tell me the electric dipole moment with position of the positively charged particle of 2.34957, position of the negatively charged particle of 1.16997 and electric charge of 1.14865.", + "a": "1.3549532766451011", + "valid": true, + "kind": "compute", + "expect": 1.3549532766451011, + "dtype": "quantity", + "unit": "m*s*A", + "why": "electric dipole moment for [2.349571, 1.169968, 1.148652] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "What is the electric dipole moment for position of the positively charged particle 223.509, position of the negatively charged particle 17.3414 and electric charge 7.10705?", + "a": "1465.2415733782432", + "valid": true, + "kind": "compute", + "expect": 1465.2415733782432, + "dtype": "quantity", + "unit": "m*s*A", + "why": "electric dipole moment for [223.508796, 17.341435, 7.107049] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "Tell me the electric dipole moment with position of the positively charged particle of 223.509, position of the negatively charged particle of 17.3414 and electric charge of 7.10705.", + "a": "1465.2415733782432", + "valid": true, + "kind": "compute", + "expect": 1465.2415733782432, + "dtype": "quantity", + "unit": "m*s*A", + "why": "electric dipole moment for [223.508796, 17.341435, 7.107049] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "What is the electric dipole moment for position of the positively charged particle 16.8989, position of the negatively charged particle 4.71659 and electric charge 4.07774?", + "a": "49.676386140806166", + "valid": true, + "kind": "compute", + "expect": 49.676386140806166, + "dtype": "quantity", + "unit": "m*s*A", + "why": "electric dipole moment for [16.898916, 4.716591, 4.077743] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "Tell me the electric dipole moment with position of the positively charged particle of 16.8989, position of the negatively charged particle of 4.71659 and electric charge of 4.07774.", + "a": "49.676386140806166", + "valid": true, + "kind": "compute", + "expect": 49.676386140806166, + "dtype": "quantity", + "unit": "m*s*A", + "why": "electric dipole moment for [16.898916, 4.716591, 4.077743] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "What is the electric dipole moment for position of the positively charged particle 107.961 and position of the negatively charged particle 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric charge is not given" + }, + { + "q": "Tell me the electric dipole moment with position of the positively charged particle of 82.2401 and position of the negatively charged particle of 13.999.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric charge is not given" + }, + { + "q": "What is the electric dipole moment of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the electric dipole moment of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the electric dipole moment with position of the positively charged particle of -6.98086, position of the negatively charged particle of 0.551236 and electric charge of 2.88512.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative position of the positively charged particle: no example had one" + }, + { + "q": "Which electric dipole moment is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the position of the positively charged particle is 8.24951, position of the negatively charged particle is 0.634021 and the electric charge is 6.34623, what is the electric dipole moment?", + "a": "48.32961055001712", + "valid": true, + "kind": "compute", + "expect": 48.32961055001712, + "dtype": "quantity", + "unit": "m*s*A", + "why": "electric dipole moment for [8.24951, 0.634021, 6.346226] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "Could you work out the electric dipole moment given position of the positively charged particle = 8.24951, position of the negatively charged particle = 0.634021, electric charge = 6.34623?", + "a": "48.32961055001712", + "valid": true, + "kind": "compute", + "expect": 48.32961055001712, + "dtype": "quantity", + "unit": "m*s*A", + "why": "electric dipole moment for [8.24951, 0.634021, 6.346226] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "I need the electric dipole moment for a position of the positively charged particle of 126.709, a position of the negatively charged particle of 1.70503 and an electric charge of 2.9896, please.", + "a": "373.7126173090642", + "valid": true, + "kind": "compute", + "expect": 373.7126173090642, + "dtype": "quantity", + "unit": "m*s*A", + "why": "electric dipole moment for [126.709252, 1.705032, 2.9896] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "If the position of the positively charged particle is 126.709, position of the negatively charged particle is 1.70503 and the electric charge is 2.9896, what is the electric dipole moment?", + "a": "373.7126173090642", + "valid": true, + "kind": "compute", + "expect": 373.7126173090642, + "dtype": "quantity", + "unit": "m*s*A", + "why": "electric dipole moment for [126.709252, 1.705032, 2.9896] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "Could you work out the electric dipole moment given position of the positively charged particle = 87.781, position of the negatively charged particle = 13.2084, electric charge = 246.329?", + "a": "18369.39668825472", + "valid": true, + "kind": "compute", + "expect": 18369.39668825472, + "dtype": "quantity", + "unit": "m*s*A", + "why": "electric dipole moment for [87.781013, 13.208419, 246.329053] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "I need the electric dipole moment for a position of the positively charged particle of 87.781, a position of the negatively charged particle of 13.2084 and an electric charge of 246.329, please.", + "a": "18369.39668825472", + "valid": true, + "kind": "compute", + "expect": 18369.39668825472, + "dtype": "quantity", + "unit": "m*s*A", + "why": "electric dipole moment for [87.781013, 13.208419, 246.329053] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "If the position of the positively charged particle is 186.27, position of the negatively charged particle is 106.508 and the electric charge is 156.502, what is the electric dipole moment?", + "a": "12482.966179136678", + "valid": true, + "kind": "compute", + "expect": 12482.966179136678, + "dtype": "quantity", + "unit": "m*s*A", + "why": "electric dipole moment for [186.270057, 106.507617, 156.50181] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "Could you work out the electric dipole moment given position of the positively charged particle = 186.27, position of the negatively charged particle = 106.508, electric charge = 156.502?", + "a": "12482.966179136678", + "valid": true, + "kind": "compute", + "expect": 12482.966179136678, + "dtype": "quantity", + "unit": "m*s*A", + "why": "electric dipole moment for [186.270057, 106.507617, 156.50181] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "I need the electric dipole moment for a position of the positively charged particle of 88.6624, a position of the negatively charged particle of 0.633611 and an electric charge of 0.869111, please.", + "a": "76.50679347437779", + "valid": true, + "kind": "compute", + "expect": 76.50679347437779, + "dtype": "quantity", + "unit": "m*s*A", + "why": "electric dipole moment for [88.662361, 0.633611, 0.869111] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "If the position of the positively charged particle is 88.6624, position of the negatively charged particle is 0.633611 and the electric charge is 0.869111, what is the electric dipole moment?", + "a": "76.50679347437779", + "valid": true, + "kind": "compute", + "expect": 76.50679347437779, + "dtype": "quantity", + "unit": "m*s*A", + "why": "electric dipole moment for [88.662361, 0.633611, 0.869111] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "Could you work out the electric dipole moment given position of the positively charged particle = 1.90441, position of the negatively charged particle = 1.45374, electric charge = 27.5916?", + "a": "12.4345202169944", + "valid": true, + "kind": "compute", + "expect": 12.4345202169944, + "dtype": "quantity", + "unit": "m*s*A", + "why": "electric dipole moment for [1.904407, 1.453744, 27.591617] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "I need the electric dipole moment for a position of the positively charged particle of 1.90441, a position of the negatively charged particle of 1.45374 and an electric charge of 27.5916, please.", + "a": "12.4345202169944", + "valid": true, + "kind": "compute", + "expect": 12.4345202169944, + "dtype": "quantity", + "unit": "m*s*A", + "why": "electric dipole moment for [1.904407, 1.453744, 27.591617] (the system electric dipole moment (Wikidata Q735135))" + }, + { + "q": "If the position of the positively charged particle is 8.24951 and the position of the negatively charged particle is 0.634021, what is the electric dipole moment?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric charge is not given" + }, + { + "q": "Could you work out the electric dipole moment given position of the positively charged particle = 126.709, position of the negatively charged particle = 1.70503?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric charge is not given" + }, + { + "q": "Do you know the electric dipole moment of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the electric dipole moment of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the electric dipole moment given position of the positively charged particle = -87.781, position of the negatively charged particle = 13.2084, electric charge = 246.329?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative position of the positively charged particle: no example had one" + }, + { + "q": "What is a typical electric dipole moment?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-electric-dipole-moment/program.py b/root/organs10/physics/specialists/solved-electric-dipole-moment/program.py new file mode 100644 index 0000000000000000000000000000000000000000..f9aeffbce66021da679219551a5a23f059284404 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-dipole-moment/program.py @@ -0,0 +1,7 @@ +"""electric dipole moment from position of the positively charged particle, position of the negatively charged particle, electric charge (electric dipole moment (Wikidata Q735135)) +The maths organ (systems.py): solved from the system q2 = q3*( q0 - q1 ); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(position_of_the_positively_charged_particle, position_of_the_negatively_charged_particle, electric_charge): + return 1.0*electric_charge*(-position_of_the_negatively_charged_particle + position_of_the_positively_charged_particle) diff --git a/root/organs10/physics/specialists/solved-electric-dipole-moment/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-dipole-moment/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-dipole-moment/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-electric-field-parameter/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-field-parameter/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-parameter/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-electric-field-parameter/panel.json b/root/organs10/physics/specialists/solved-electric-field-parameter/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..830501d5945cf3782e930b77f92170ae0896c7ed --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-parameter/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric field parameter for electric field strength 107.961, speed 59.4105 and magnetic flux density 5.77717?", + "a": "0.31454848897160476", + "valid": true, + "kind": "compute", + "expect": 0.31454848897160476, + "dtype": "quantity", + "unit": "1", + "why": "electric field parameter for [107.960666, 59.4105, 5.777165] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Tell me the electric field parameter with electric field strength of 107.961, speed of 59.4105 and magnetic flux density of 5.77717.", + "a": "0.31454848897160476", + "valid": true, + "kind": "compute", + "expect": 0.31454848897160476, + "dtype": "quantity", + "unit": "1", + "why": "electric field parameter for [107.960666, 59.4105, 5.777165] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "What is the electric field parameter for electric field strength 85.2416, speed 160.878 and magnetic flux density 2.69428?", + "a": "0.19665778745152376", + "valid": true, + "kind": "compute", + "expect": 0.19665778745152376, + "dtype": "quantity", + "unit": "1", + "why": "electric field parameter for [85.241623, 160.878442, 2.69428] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Tell me the electric field parameter with electric field strength of 85.2416, speed of 160.878 and magnetic flux density of 2.69428.", + "a": "0.19665778745152376", + "valid": true, + "kind": "compute", + "expect": 0.19665778745152376, + "dtype": "quantity", + "unit": "1", + "why": "electric field parameter for [85.241623, 160.878442, 2.69428] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "What is the electric field parameter for electric field strength 45.6157, speed 4.97131 and magnetic flux density 94.305?", + "a": "0.09729902784967777", + "valid": true, + "kind": "compute", + "expect": 0.09729902784967777, + "dtype": "quantity", + "unit": "1", + "why": "electric field parameter for [45.61567, 4.971307, 94.305046] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Tell me the electric field parameter with electric field strength of 45.6157, speed of 4.97131 and magnetic flux density of 94.305.", + "a": "0.09729902784967777", + "valid": true, + "kind": "compute", + "expect": 0.09729902784967777, + "dtype": "quantity", + "unit": "1", + "why": "electric field parameter for [45.61567, 4.971307, 94.305046] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "What is the electric field parameter for electric field strength 10.5638, speed 198.839 and magnetic flux density 0.671697?", + "a": "0.07909461512056756", + "valid": true, + "kind": "compute", + "expect": 0.07909461512056756, + "dtype": "quantity", + "unit": "1", + "why": "electric field parameter for [10.563843, 198.838955, 0.671697] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Tell me the electric field parameter with electric field strength of 10.5638, speed of 198.839 and magnetic flux density of 0.671697.", + "a": "0.07909461512056756", + "valid": true, + "kind": "compute", + "expect": 0.07909461512056756, + "dtype": "quantity", + "unit": "1", + "why": "electric field parameter for [10.563843, 198.838955, 0.671697] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "What is the electric field parameter for electric field strength 21.7931, speed 29.5106 and magnetic flux density 8.50341?", + "a": "0.08684571530973768", + "valid": true, + "kind": "compute", + "expect": 0.08684571530973768, + "dtype": "quantity", + "unit": "1", + "why": "electric field parameter for [21.793126, 29.510609, 8.503407] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Tell me the electric field parameter with electric field strength of 21.7931, speed of 29.5106 and magnetic flux density of 8.50341.", + "a": "0.08684571530973768", + "valid": true, + "kind": "compute", + "expect": 0.08684571530973768, + "dtype": "quantity", + "unit": "1", + "why": "electric field parameter for [21.793126, 29.510609, 8.503407] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "What is the electric field parameter for electric field strength 223.509, speed 17.3414 and magnetic flux density 7.10705?", + "a": "1.813511306470856", + "valid": true, + "kind": "compute", + "expect": 1.813511306470856, + "dtype": "quantity", + "unit": "1", + "why": "electric field parameter for [223.508796, 17.341435, 7.107049] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Tell me the electric field parameter with electric field strength of 223.509, speed of 17.3414 and magnetic flux density of 7.10705.", + "a": "1.813511306470856", + "valid": true, + "kind": "compute", + "expect": 1.813511306470856, + "dtype": "quantity", + "unit": "1", + "why": "electric field parameter for [223.508796, 17.341435, 7.107049] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "What is the electric field parameter for electric field strength 107.961 and speed 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 electric field parameter with electric field strength of 85.2416 and speed 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 electric field parameter of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the electric field parameter of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the electric field parameter with electric field strength of -45.6157, speed 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 strength: no example had one" + }, + { + "q": "Which electric field parameter is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the electric field strength is 17.7888, speed is 167.625 and the magnetic flux density is 10.3291, what is the electric field parameter?", + "a": "0.010274173498684694", + "valid": true, + "kind": "compute", + "expect": 0.010274173498684694, + "dtype": "quantity", + "unit": "1", + "why": "electric field parameter for [17.788778, 167.624948, 10.329054] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Could you work out the electric field parameter given electric field strength = 17.7888, speed = 167.625, magnetic flux density = 10.3291?", + "a": "0.010274173498684694", + "valid": true, + "kind": "compute", + "expect": 0.010274173498684694, + "dtype": "quantity", + "unit": "1", + "why": "electric field parameter for [17.788778, 167.624948, 10.329054] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "I need the electric field parameter for an electric field strength of 21.511, a speed of 173.394 and a magnetic flux density of 251.084, please.", + "a": "0.000494092950151496", + "valid": true, + "kind": "compute", + "expect": 0.000494092950151496, + "dtype": "quantity", + "unit": "1", + "why": "electric field parameter for [21.511016, 173.393638, 251.084037] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "If the electric field strength is 21.511, speed is 173.394 and the magnetic flux density is 251.084, what is the electric field parameter?", + "a": "0.000494092950151496", + "valid": true, + "kind": "compute", + "expect": 0.000494092950151496, + "dtype": "quantity", + "unit": "1", + "why": "electric field parameter for [21.511016, 173.393638, 251.084037] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Could you work out the electric field parameter given electric field strength = 129.245, speed = 1.18359, magnetic flux density = 15.9416?", + "a": "6.849836414751194", + "valid": true, + "kind": "compute", + "expect": 6.849836414751194, + "dtype": "quantity", + "unit": "1", + "why": "electric field parameter for [129.244984, 1.18359, 15.941612] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "I need the electric field parameter for an electric field strength of 129.245, a speed of 1.18359 and a magnetic flux density of 15.9416, please.", + "a": "6.849836414751194", + "valid": true, + "kind": "compute", + "expect": 6.849836414751194, + "dtype": "quantity", + "unit": "1", + "why": "electric field parameter for [129.244984, 1.18359, 15.941612] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "If the electric field strength is 20.3966, speed is 18.3169 and the magnetic flux density is 6.8386, what is the electric field parameter?", + "a": "0.16283157083473473", + "valid": true, + "kind": "compute", + "expect": 0.16283157083473473, + "dtype": "quantity", + "unit": "1", + "why": "electric field parameter for [20.396558, 18.316867, 6.838598] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Could you work out the electric field parameter given electric field strength = 20.3966, speed = 18.3169, magnetic flux density = 6.8386?", + "a": "0.16283157083473473", + "valid": true, + "kind": "compute", + "expect": 0.16283157083473473, + "dtype": "quantity", + "unit": "1", + "why": "electric field parameter for [20.396558, 18.316867, 6.838598] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "I need the electric field parameter for an electric field strength of 185.886, a speed of 13.2373 and a magnetic flux density of 4.71941, please.", + "a": "2.975496158535072", + "valid": true, + "kind": "compute", + "expect": 2.975496158535072, + "dtype": "quantity", + "unit": "1", + "why": "electric field parameter for [185.886273, 13.237311, 4.719415] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "If the electric field strength is 185.886, speed is 13.2373 and the magnetic flux density is 4.71941, what is the electric field parameter?", + "a": "2.975496158535072", + "valid": true, + "kind": "compute", + "expect": 2.975496158535072, + "dtype": "quantity", + "unit": "1", + "why": "electric field parameter for [185.886273, 13.237311, 4.719415] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Could you work out the electric field parameter given electric field strength = 9.1061, speed = 24.5721, magnetic flux density = 108.451?", + "a": "0.003417101331511669", + "valid": true, + "kind": "compute", + "expect": 0.003417101331511669, + "dtype": "quantity", + "unit": "1", + "why": "electric field parameter for [9.106104, 24.572086, 108.450792] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "I need the electric field parameter for an electric field strength of 9.1061, a speed of 24.5721 and a magnetic flux density of 108.451, please.", + "a": "0.003417101331511669", + "valid": true, + "kind": "compute", + "expect": 0.003417101331511669, + "dtype": "quantity", + "unit": "1", + "why": "electric field parameter for [9.106104, 24.572086, 108.450792] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "If the electric field strength is 17.7888 and the speed is 167.625, what is the electric field parameter?", + "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 electric field parameter given electric field strength = 21.511, speed = 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 electric field parameter of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the electric field parameter of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the electric field parameter given electric field strength = -129.245, speed = 1.18359, magnetic flux density = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric field strength: no example had one" + }, + { + "q": "What is a typical electric field parameter?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-electric-field-parameter/program.py b/root/organs10/physics/specialists/solved-electric-field-parameter/program.py new file mode 100644 index 0000000000000000000000000000000000000000..39f5a78759707a3df01e320b10e50aff4e73e4a7 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-parameter/program.py @@ -0,0 +1,7 @@ +"""electric field parameter from electric field strength, speed, 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_strength, speed, magnetic_flux_density): + return 1.0*electric_field_strength/(magnetic_flux_density*speed) diff --git a/root/organs10/physics/specialists/solved-electric-field-parameter/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-field-parameter/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-parameter/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-electric-field-strength-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-field-strength-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-2/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-electric-field-strength-2/panel.json b/root/organs10/physics/specialists/solved-electric-field-strength-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..5dbfdbf2924e5926f19389123594460de053648e --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric field strength for resistivity tensor 107.961 and electric current density 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [107.960666, 59.4105] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Tell me the electric field strength with resistivity tensor of 107.961 and electric current density of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [107.960666, 59.4105] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "What is the electric field strength for resistivity tensor 169.527 and electric current density 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [169.526958, 280.773702] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Tell me the electric field strength with resistivity tensor of 169.527 and electric current density of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [169.526958, 280.773702] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "What is the electric field strength for resistivity tensor 1.91195 and electric current density 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.911952, 82.240072] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Tell me the electric field strength with resistivity tensor of 1.91195 and electric current density of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.911952, 82.240072] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "What is the electric field strength for resistivity tensor 1.64379 and electric current density 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.643795, 252.709024] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Tell me the electric field strength with resistivity tensor of 1.64379 and electric current density of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.643795, 252.709024] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "What is the electric field strength for resistivity tensor 18.3169 and electric current density 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [18.316867, 6.838598] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Tell me the electric field strength with resistivity tensor of 18.3169 and electric current density of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [18.316867, 6.838598] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "What is the electric field strength for resistivity tensor 156.502 and electric current density 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [156.50181, 185.886273] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Tell me the electric field strength with resistivity tensor of 156.502 and electric current density of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [156.50181, 185.886273] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "What is the electric field strength for resistivity tensor 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current density is not given" + }, + { + "q": "Tell me the electric field strength with resistivity tensor of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current density is not given" + }, + { + "q": "What is the electric field strength of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the electric field strength of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the electric field strength with resistivity tensor of -1.91195 and electric current density of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative resistivity tensor: no example had one" + }, + { + "q": "Which electric field strength is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the resistivity tensor is 8.50728 and the electric current density is 17.7888, what is the electric field strength?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.507283, 17.788778] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Could you work out the electric field strength given resistivity tensor = 8.50728, electric current density = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.507283, 17.788778] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "I need the electric field strength for a resistivity tensor of 8.24951 and an electric current density of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.24951, 0.634021] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "If the resistivity tensor is 8.24951 and the electric current density is 0.634021, what is the electric field strength?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.24951, 0.634021] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Could you work out the electric field strength given resistivity tensor = 0.318734, electric current density = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.318734, 9.566071] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "I need the electric field strength for a resistivity tensor of 0.318734 and an electric current density of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.318734, 9.566071] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "If the resistivity tensor is 0.671697 and the electric current density is 14.2496, what is the electric field strength?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.671697, 14.249569] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Could you work out the electric field strength given resistivity tensor = 0.671697, electric current density = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.671697, 14.249569] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "I need the electric field strength for a resistivity tensor of 21.7931 and an electric current density of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [21.793126, 29.510609] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "If the resistivity tensor is 21.7931 and the electric current density is 29.5106, what is the electric field strength?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [21.793126, 29.510609] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "Could you work out the electric field strength given resistivity tensor = 153.15, electric current density = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [153.149965, 18.595315] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "I need the electric field strength for a resistivity tensor of 153.15 and an electric current density of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [153.149965, 18.595315] (the system resistivity tensor (Wikidata Q112762959))" + }, + { + "q": "If the resistivity tensor is 8.50728, what is the electric field strength?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current density is not given" + }, + { + "q": "Could you work out the electric field strength given resistivity tensor = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current density is not given" + }, + { + "q": "Do you know the electric field strength of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the electric field strength of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the electric field strength given resistivity tensor = -0.318734, electric current density = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative resistivity tensor: no example had one" + }, + { + "q": "What is a typical electric field strength?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-electric-field-strength-2/program.py b/root/organs10/physics/specialists/solved-electric-field-strength-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..3efb3c01b4709f3fba4d76d1b66d468f64dc73e3 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-2/program.py @@ -0,0 +1,7 @@ +"""electric field strength from resistivity tensor, electric current density (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(resistivity_tensor, electric_current_density): + return 1.0*electric_current_density*resistivity_tensor diff --git a/root/organs10/physics/specialists/solved-electric-field-strength-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-field-strength-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-2/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-electric-field-strength-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-field-strength-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-3/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-electric-field-strength-3/panel.json b/root/organs10/physics/specialists/solved-electric-field-strength-3/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..00ff8592780d0e5ff6ed7db7192f17ac358bcd28 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-3/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric field strength for force 107.961 and electric charge 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [107.960666, 59.4105] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "Tell me the electric field strength with force of 107.961 and electric charge of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [107.960666, 59.4105] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "What is the electric field strength for force 169.527 and electric charge 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [169.526958, 280.773702] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "Tell me the electric field strength with force of 169.527 and electric charge of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [169.526958, 280.773702] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "What is the electric field strength for force 1.91195 and electric charge 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.911952, 82.240072] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "Tell me the electric field strength with force of 1.91195 and electric charge of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.911952, 82.240072] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "What is the electric field strength for force 1.64379 and electric charge 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.643795, 252.709024] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "Tell me the electric field strength with force of 1.64379 and electric charge of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.643795, 252.709024] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "What is the electric field strength for force 18.3169 and electric charge 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [18.316867, 6.838598] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "Tell me the electric field strength with force of 18.3169 and electric charge of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [18.316867, 6.838598] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "What is the electric field strength for force 156.502 and electric charge 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [156.50181, 185.886273] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "Tell me the electric field strength with force of 156.502 and electric charge of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [156.50181, 185.886273] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "What is the electric field strength for force 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric charge is not given" + }, + { + "q": "Tell me the electric field strength with force of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric charge is not given" + }, + { + "q": "What is the electric field strength of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the electric field strength of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the electric field strength with force of -1.91195 and electric charge 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 electric field strength is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the force is 8.50728 and the electric charge is 17.7888, what is the electric field strength?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.507283, 17.788778] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "Could you work out the electric field strength given force = 8.50728, electric charge = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.507283, 17.788778] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "I need the electric field strength for a force of 8.24951 and an electric charge of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.24951, 0.634021] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "If the force is 8.24951 and the electric charge is 0.634021, what is the electric field strength?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.24951, 0.634021] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "Could you work out the electric field strength given force = 0.318734, electric charge = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.318734, 9.566071] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "I need the electric field strength for a force of 0.318734 and an electric charge of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.318734, 9.566071] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "If the force is 0.671697 and the electric charge is 14.2496, what is the electric field strength?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.671697, 14.249569] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "Could you work out the electric field strength given force = 0.671697, electric charge = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.671697, 14.249569] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "I need the electric field strength for a force of 21.7931 and an electric charge of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [21.793126, 29.510609] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "If the force is 21.7931 and the electric charge is 29.5106, what is the electric field strength?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [21.793126, 29.510609] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "Could you work out the electric field strength given force = 153.15, electric charge = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [153.149965, 18.595315] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "I need the electric field strength for a force of 153.15 and an electric charge of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [153.149965, 18.595315] (the system electric field strength (Wikidata Q20989))" + }, + { + "q": "If the force is 8.50728, what is the electric field strength?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric charge is not given" + }, + { + "q": "Could you work out the electric field strength given force = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric charge is not given" + }, + { + "q": "Do you know the electric field strength of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the electric field strength of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the electric field strength given force = -0.318734, electric charge = 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 electric field strength?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-electric-field-strength-3/program.py b/root/organs10/physics/specialists/solved-electric-field-strength-3/program.py new file mode 100644 index 0000000000000000000000000000000000000000..c993770563be7f6acdd3888e9af8cce6d9745c33 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-3/program.py @@ -0,0 +1,7 @@ +"""electric field strength from force, electric charge (electric field strength (Wikidata Q20989)) +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, electric_charge): + return 1.0*force/electric_charge diff --git a/root/organs10/physics/specialists/solved-electric-field-strength-3/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-field-strength-3/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-3/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-electric-field-strength-4/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-field-strength-4/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-4/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-electric-field-strength-4/panel.json b/root/organs10/physics/specialists/solved-electric-field-strength-4/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..6ef19e1c93e49a51fbc0deb1dab163ea3f122e47 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-4/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric field strength for electric flux density 107.961 and permittivity 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [107.960666, 59.4105] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "Tell me the electric field strength with electric flux density of 107.961 and permittivity of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [107.960666, 59.4105] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "What is the electric field strength for electric flux density 169.527 and permittivity 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [169.526958, 280.773702] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "Tell me the electric field strength with electric flux density of 169.527 and permittivity of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [169.526958, 280.773702] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "What is the electric field strength for electric flux density 1.91195 and permittivity 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.911952, 82.240072] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "Tell me the electric field strength with electric flux density of 1.91195 and permittivity of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.911952, 82.240072] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "What is the electric field strength for electric flux density 1.64379 and permittivity 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.643795, 252.709024] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "Tell me the electric field strength with electric flux density of 1.64379 and permittivity of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.643795, 252.709024] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "What is the electric field strength for electric flux density 18.3169 and permittivity 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [18.316867, 6.838598] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "Tell me the electric field strength with electric flux density of 18.3169 and permittivity of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [18.316867, 6.838598] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "What is the electric field strength for electric flux density 156.502 and permittivity 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [156.50181, 185.886273] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "Tell me the electric field strength with electric flux density of 156.502 and permittivity of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [156.50181, 185.886273] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "What is the electric field strength for electric flux density 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the permittivity is not given" + }, + { + "q": "Tell me the electric field strength with electric flux density of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the permittivity is not given" + }, + { + "q": "What is the electric field strength of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the electric field strength of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the electric field strength with electric flux density of -1.91195 and permittivity of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric flux density: no example had one" + }, + { + "q": "Which electric field strength is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the electric flux density is 8.50728 and the permittivity is 17.7888, what is the electric field strength?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.507283, 17.788778] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "Could you work out the electric field strength given electric flux density = 8.50728, permittivity = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.507283, 17.788778] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "I need the electric field strength for an electric flux density of 8.24951 and a permittivity of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.24951, 0.634021] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "If the electric flux density is 8.24951 and the permittivity is 0.634021, what is the electric field strength?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.24951, 0.634021] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "Could you work out the electric field strength given electric flux density = 0.318734, permittivity = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.318734, 9.566071] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "I need the electric field strength for an electric flux density of 0.318734 and a permittivity of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.318734, 9.566071] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "If the electric flux density is 0.671697 and the permittivity is 14.2496, what is the electric field strength?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.671697, 14.249569] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "Could you work out the electric field strength given electric flux density = 0.671697, permittivity = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.671697, 14.249569] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "I need the electric field strength for an electric flux density of 21.7931 and a permittivity of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [21.793126, 29.510609] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "If the electric flux density is 21.7931 and the permittivity is 29.5106, what is the electric field strength?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [21.793126, 29.510609] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "Could you work out the electric field strength given electric flux density = 153.15, permittivity = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [153.149965, 18.595315] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "I need the electric field strength for an electric flux density of 153.15 and a permittivity of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [153.149965, 18.595315] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "If the electric flux density is 8.50728, what is the electric field strength?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the permittivity is not given" + }, + { + "q": "Could you work out the electric field strength given electric flux density = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the permittivity is not given" + }, + { + "q": "Do you know the electric field strength of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the electric field strength of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the electric field strength given electric flux density = -0.318734, permittivity = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric flux density: no example had one" + }, + { + "q": "What is a typical electric field strength?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-electric-field-strength-4/program.py b/root/organs10/physics/specialists/solved-electric-field-strength-4/program.py new file mode 100644 index 0000000000000000000000000000000000000000..2ca866f2b388291b1e8fa2c01ed5ce16f7403eb9 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-4/program.py @@ -0,0 +1,7 @@ +"""electric field strength from electric flux density, permittivity (permittivity (Wikidata Q211569)) +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(electric_flux_density, permittivity): + return 1.0*electric_flux_density/permittivity diff --git a/root/organs10/physics/specialists/solved-electric-field-strength-4/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-field-strength-4/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-4/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-electric-field-strength-5/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-field-strength-5/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-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-electric-field-strength-5/panel.json b/root/organs10/physics/specialists/solved-electric-field-strength-5/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..bff9bf9c4758a6bdca97f91d7ec52c81feea0038 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-5/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric field strength for electric current density 107.961 and electrical conductivity 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [107.960666, 59.4105] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "Tell me the electric field strength with electric current density of 107.961 and electrical conductivity of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [107.960666, 59.4105] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "What is the electric field strength for electric current density 169.527 and electrical conductivity 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [169.526958, 280.773702] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "Tell me the electric field strength with electric current density of 169.527 and electrical conductivity of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [169.526958, 280.773702] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "What is the electric field strength for electric current density 1.91195 and electrical conductivity 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.911952, 82.240072] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "Tell me the electric field strength with electric current density of 1.91195 and electrical conductivity of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.911952, 82.240072] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "What is the electric field strength for electric current density 1.64379 and electrical conductivity 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.643795, 252.709024] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "Tell me the electric field strength with electric current density of 1.64379 and electrical conductivity of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.643795, 252.709024] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "What is the electric field strength for electric current density 18.3169 and electrical conductivity 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [18.316867, 6.838598] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "Tell me the electric field strength with electric current density of 18.3169 and electrical conductivity of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [18.316867, 6.838598] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "What is the electric field strength for electric current density 156.502 and electrical conductivity 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [156.50181, 185.886273] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "Tell me the electric field strength with electric current density of 156.502 and electrical conductivity of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [156.50181, 185.886273] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "What is the electric field strength for electric current density 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electrical conductivity is not given" + }, + { + "q": "Tell me the electric field strength with electric current density of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electrical conductivity is not given" + }, + { + "q": "What is the electric field strength of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the electric field strength of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the electric field strength with electric current density of -1.91195 and electrical conductivity 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 electric field strength is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the electric current density is 8.50728 and the electrical conductivity is 17.7888, what is the electric field strength?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.507283, 17.788778] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "Could you work out the electric field strength given electric current density = 8.50728, electrical conductivity = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.507283, 17.788778] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "I need the electric field strength for an electric current density of 8.24951 and an electrical conductivity of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.24951, 0.634021] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "If the electric current density is 8.24951 and the electrical conductivity is 0.634021, what is the electric field strength?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.24951, 0.634021] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "Could you work out the electric field strength given electric current density = 0.318734, electrical conductivity = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.318734, 9.566071] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "I need the electric field strength for an electric current density of 0.318734 and an electrical conductivity of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.318734, 9.566071] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "If the electric current density is 0.671697 and the electrical conductivity is 14.2496, what is the electric field strength?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.671697, 14.249569] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "Could you work out the electric field strength given electric current density = 0.671697, electrical conductivity = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.671697, 14.249569] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "I need the electric field strength for an electric current density of 21.7931 and an electrical conductivity of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [21.793126, 29.510609] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "If the electric current density is 21.7931 and the electrical conductivity is 29.5106, what is the electric field strength?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [21.793126, 29.510609] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "Could you work out the electric field strength given electric current density = 153.15, electrical conductivity = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [153.149965, 18.595315] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "I need the electric field strength for an electric current density of 153.15 and an electrical conductivity of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [153.149965, 18.595315] (the system electrical conductivity (Wikidata Q4593291))" + }, + { + "q": "If the electric current density is 8.50728, what is the electric field strength?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electrical conductivity is not given" + }, + { + "q": "Could you work out the electric field strength given electric current density = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electrical conductivity is not given" + }, + { + "q": "Do you know the electric field strength of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the electric field strength of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the electric field strength given electric current density = -0.318734, electrical conductivity = 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 electric field strength?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-electric-field-strength-5/program.py b/root/organs10/physics/specialists/solved-electric-field-strength-5/program.py new file mode 100644 index 0000000000000000000000000000000000000000..62db421826ac074b84618ce1112e56e5226d9d4c --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-5/program.py @@ -0,0 +1,7 @@ +"""electric field strength from electric current density, electrical conductivity (electrical conductivity (Wikidata Q4593291)) +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(electric_current_density, electrical_conductivity): + return 1.0*electric_current_density/electrical_conductivity diff --git a/root/organs10/physics/specialists/solved-electric-field-strength-5/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-field-strength-5/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-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-electric-field-strength-6/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-field-strength-6/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-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-electric-field-strength-6/panel.json b/root/organs10/physics/specialists/solved-electric-field-strength-6/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..c7d2259d2fa14c7b7b7387b4ef71b655a0a50e45 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-6/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric field strength for Poynting vector 107.961 and magnetic field strength 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [107.960666, 59.4105] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "Tell me the electric field strength with Poynting vector of 107.961 and magnetic field strength of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [107.960666, 59.4105] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "What is the electric field strength for Poynting vector 169.527 and magnetic field strength 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [169.526958, 280.773702] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "Tell me the electric field strength with Poynting vector of 169.527 and magnetic field strength of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [169.526958, 280.773702] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "What is the electric field strength for Poynting vector 1.91195 and magnetic field strength 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.911952, 82.240072] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "Tell me the electric field strength with Poynting vector of 1.91195 and magnetic field strength of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.911952, 82.240072] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "What is the electric field strength for Poynting vector 1.64379 and magnetic field strength 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.643795, 252.709024] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "Tell me the electric field strength with Poynting vector of 1.64379 and magnetic field strength of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.643795, 252.709024] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "What is the electric field strength for Poynting vector 18.3169 and magnetic field strength 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [18.316867, 6.838598] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "Tell me the electric field strength with Poynting vector of 18.3169 and magnetic field strength of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [18.316867, 6.838598] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "What is the electric field strength for Poynting vector 156.502 and magnetic field strength 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [156.50181, 185.886273] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "Tell me the electric field strength with Poynting vector of 156.502 and magnetic field strength of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [156.50181, 185.886273] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "What is the electric field strength for Poynting vector 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic field strength is not given" + }, + { + "q": "Tell me the electric field strength with Poynting vector of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic field strength is not given" + }, + { + "q": "What is the electric field strength of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the electric field strength of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the electric field strength with Poynting vector of -1.91195 and magnetic field strength of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Poynting vector: no example had one" + }, + { + "q": "Which electric field strength is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the Poynting vector is 8.50728 and the magnetic field strength is 17.7888, what is the electric field strength?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.507283, 17.788778] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "Could you work out the electric field strength given Poynting vector = 8.50728, magnetic field strength = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.507283, 17.788778] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "I need the electric field strength for a Poynting vector of 8.24951 and a magnetic field strength of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.24951, 0.634021] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "If the Poynting vector is 8.24951 and the magnetic field strength is 0.634021, what is the electric field strength?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.24951, 0.634021] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "Could you work out the electric field strength given Poynting vector = 0.318734, magnetic field strength = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.318734, 9.566071] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "I need the electric field strength for a Poynting vector of 0.318734 and a magnetic field strength of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.318734, 9.566071] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "If the Poynting vector is 0.671697 and the magnetic field strength is 14.2496, what is the electric field strength?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.671697, 14.249569] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "Could you work out the electric field strength given Poynting vector = 0.671697, magnetic field strength = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.671697, 14.249569] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "I need the electric field strength for a Poynting vector of 21.7931 and a magnetic field strength of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [21.793126, 29.510609] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "If the Poynting vector is 21.7931 and the magnetic field strength is 29.5106, what is the electric field strength?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [21.793126, 29.510609] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "Could you work out the electric field strength given Poynting vector = 153.15, magnetic field strength = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [153.149965, 18.595315] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "I need the electric field strength for a Poynting vector of 153.15 and a magnetic field strength of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [153.149965, 18.595315] (the system Poynting vector (Wikidata Q504186))" + }, + { + "q": "If the Poynting vector is 8.50728, what is the electric field strength?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic field strength is not given" + }, + { + "q": "Could you work out the electric field strength given Poynting vector = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic field strength is not given" + }, + { + "q": "Do you know the electric field strength of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the electric field strength of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the electric field strength given Poynting vector = -0.318734, magnetic field strength = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Poynting vector: no example had one" + }, + { + "q": "What is a typical electric field strength?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-electric-field-strength-6/program.py b/root/organs10/physics/specialists/solved-electric-field-strength-6/program.py new file mode 100644 index 0000000000000000000000000000000000000000..63531cceb7df1d7c028aaedcfae7a8d22d66b126 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-6/program.py @@ -0,0 +1,7 @@ +"""electric field strength from Poynting vector, magnetic field strength (Poynting vector (Wikidata Q504186)) +The maths organ (systems.py): solved from the system q1 = q2 * q3; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(poynting_vector, magnetic_field_strength): + return 1.0*poynting_vector/magnetic_field_strength diff --git a/root/organs10/physics/specialists/solved-electric-field-strength-6/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-field-strength-6/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-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-electric-field-strength-7/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-field-strength-7/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-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-electric-field-strength-7/panel.json b/root/organs10/physics/specialists/solved-electric-field-strength-7/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..d610fe06cd6c0961e77fa4ace245ec7a14467b8b --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-7/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric field strength for conductivity tensor 107.961 and electric current density 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [107.960666, 59.4105] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "Tell me the electric field strength with conductivity tensor of 107.961 and electric current density of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [107.960666, 59.4105] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "What is the electric field strength for conductivity tensor 169.527 and electric current density 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [169.526958, 280.773702] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "Tell me the electric field strength with conductivity tensor of 169.527 and electric current density of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [169.526958, 280.773702] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "What is the electric field strength for conductivity tensor 1.91195 and electric current density 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.911952, 82.240072] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "Tell me the electric field strength with conductivity tensor of 1.91195 and electric current density of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.911952, 82.240072] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "What is the electric field strength for conductivity tensor 1.64379 and electric current density 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.643795, 252.709024] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "Tell me the electric field strength with conductivity tensor of 1.64379 and electric current density of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.643795, 252.709024] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "What is the electric field strength for conductivity tensor 18.3169 and electric current density 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [18.316867, 6.838598] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "Tell me the electric field strength with conductivity tensor of 18.3169 and electric current density of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [18.316867, 6.838598] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "What is the electric field strength for conductivity tensor 156.502 and electric current density 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [156.50181, 185.886273] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "Tell me the electric field strength with conductivity tensor of 156.502 and electric current density of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [156.50181, 185.886273] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "What is the electric field strength for conductivity tensor 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current density is not given" + }, + { + "q": "Tell me the electric field strength with conductivity tensor of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current density is not given" + }, + { + "q": "What is the electric field strength of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the electric field strength of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the electric field strength with conductivity tensor of -1.91195 and electric current density of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative conductivity tensor: no example had one" + }, + { + "q": "Which electric field strength is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the conductivity tensor is 8.50728 and the electric current density is 17.7888, what is the electric field strength?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.507283, 17.788778] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "Could you work out the electric field strength given conductivity tensor = 8.50728, electric current density = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.507283, 17.788778] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "I need the electric field strength for a conductivity tensor of 8.24951 and an electric current density of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.24951, 0.634021] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "If the conductivity tensor is 8.24951 and the electric current density is 0.634021, what is the electric field strength?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.24951, 0.634021] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "Could you work out the electric field strength given conductivity tensor = 0.318734, electric current density = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.318734, 9.566071] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "I need the electric field strength for a conductivity tensor of 0.318734 and an electric current density of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.318734, 9.566071] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "If the conductivity tensor is 0.671697 and the electric current density is 14.2496, what is the electric field strength?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.671697, 14.249569] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "Could you work out the electric field strength given conductivity tensor = 0.671697, electric current density = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.671697, 14.249569] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "I need the electric field strength for a conductivity tensor of 21.7931 and an electric current density of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [21.793126, 29.510609] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "If the conductivity tensor is 21.7931 and the electric current density is 29.5106, what is the electric field strength?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [21.793126, 29.510609] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "Could you work out the electric field strength given conductivity tensor = 153.15, electric current density = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [153.149965, 18.595315] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "I need the electric field strength for a conductivity tensor of 153.15 and an electric current density of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [153.149965, 18.595315] (the system conductivity tensor (Wikidata Q4454679))" + }, + { + "q": "If the conductivity tensor is 8.50728, what is the electric field strength?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current density is not given" + }, + { + "q": "Could you work out the electric field strength given conductivity tensor = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current density is not given" + }, + { + "q": "Do you know the electric field strength of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the electric field strength of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the electric field strength given conductivity tensor = -0.318734, electric current density = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative conductivity tensor: no example had one" + }, + { + "q": "What is a typical electric field strength?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-electric-field-strength-7/program.py b/root/organs10/physics/specialists/solved-electric-field-strength-7/program.py new file mode 100644 index 0000000000000000000000000000000000000000..cc73bda9047d7f37254a713da880fe244e16e696 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-7/program.py @@ -0,0 +1,7 @@ +"""electric field strength from conductivity tensor, electric current density (conductivity tensor (Wikidata Q4454679)) +The maths organ (systems.py): solved from the system q2 = q1 * q3; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(conductivity_tensor, electric_current_density): + return 1.0*electric_current_density/conductivity_tensor diff --git a/root/organs10/physics/specialists/solved-electric-field-strength-7/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-field-strength-7/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-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-electric-field-strength-8/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-field-strength-8/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-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-electric-field-strength-8/panel.json b/root/organs10/physics/specialists/solved-electric-field-strength-8/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..67a9f7b30ca050b922a8d1cc91eac0d0ed5331e9 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-8/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric field strength for drift velocity 107.961 and mobility 59.4105?", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [107.960666, 59.4105] (the system mobility (Wikidata Q900648))" + }, + { + "q": "Tell me the electric field strength with drift velocity of 107.961 and mobility of 59.4105.", + "a": "1.8171983999893246", + "valid": true, + "kind": "compute", + "expect": 1.8171983999893246, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [107.960666, 59.4105] (the system mobility (Wikidata Q900648))" + }, + { + "q": "What is the electric field strength for drift velocity 169.527 and mobility 280.774?", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [169.526958, 280.773702] (the system mobility (Wikidata Q900648))" + }, + { + "q": "Tell me the electric field strength with drift velocity of 169.527 and mobility of 280.774.", + "a": "0.6037850281809957", + "valid": true, + "kind": "compute", + "expect": 0.6037850281809957, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [169.526958, 280.773702] (the system mobility (Wikidata Q900648))" + }, + { + "q": "What is the electric field strength for drift velocity 1.91195 and mobility 82.2401?", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.911952, 82.240072] (the system mobility (Wikidata Q900648))" + }, + { + "q": "Tell me the electric field strength with drift velocity of 1.91195 and mobility of 82.2401.", + "a": "0.023248420388592404", + "valid": true, + "kind": "compute", + "expect": 0.023248420388592404, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.911952, 82.240072] (the system mobility (Wikidata Q900648))" + }, + { + "q": "What is the electric field strength for drift velocity 1.64379 and mobility 252.709?", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.643795, 252.709024] (the system mobility (Wikidata Q900648))" + }, + { + "q": "Tell me the electric field strength with drift velocity of 1.64379 and mobility of 252.709.", + "a": "0.006504696457692342", + "valid": true, + "kind": "compute", + "expect": 0.006504696457692342, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.643795, 252.709024] (the system mobility (Wikidata Q900648))" + }, + { + "q": "What is the electric field strength for drift velocity 18.3169 and mobility 6.8386?", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [18.316867, 6.838598] (the system mobility (Wikidata Q900648))" + }, + { + "q": "Tell me the electric field strength with drift velocity of 18.3169 and mobility of 6.8386.", + "a": "2.6784535160942915", + "valid": true, + "kind": "compute", + "expect": 2.6784535160942915, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [18.316867, 6.838598] (the system mobility (Wikidata Q900648))" + }, + { + "q": "What is the electric field strength for drift velocity 156.502 and mobility 185.886?", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [156.50181, 185.886273] (the system mobility (Wikidata Q900648))" + }, + { + "q": "Tell me the electric field strength with drift velocity of 156.502 and mobility of 185.886.", + "a": "0.8419223616111585", + "valid": true, + "kind": "compute", + "expect": 0.8419223616111585, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [156.50181, 185.886273] (the system mobility (Wikidata Q900648))" + }, + { + "q": "What is the electric field strength for drift velocity 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mobility is not given" + }, + { + "q": "Tell me the electric field strength with drift velocity of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mobility is not given" + }, + { + "q": "What is the electric field strength of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the electric field strength of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the electric field strength with drift velocity of -1.91195 and mobility of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative drift velocity: no example had one" + }, + { + "q": "Which electric field strength is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the drift velocity is 8.50728 and the mobility is 17.7888, what is the electric field strength?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.507283, 17.788778] (the system mobility (Wikidata Q900648))" + }, + { + "q": "Could you work out the electric field strength given drift velocity = 8.50728, mobility = 17.7888?", + "a": "0.4782387296673114", + "valid": true, + "kind": "compute", + "expect": 0.4782387296673114, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.507283, 17.788778] (the system mobility (Wikidata Q900648))" + }, + { + "q": "I need the electric field strength for a drift velocity of 8.24951 and a mobility of 0.634021, please.", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.24951, 0.634021] (the system mobility (Wikidata Q900648))" + }, + { + "q": "If the drift velocity is 8.24951 and the mobility is 0.634021, what is the electric field strength?", + "a": "13.011414796588832", + "valid": true, + "kind": "compute", + "expect": 13.011414796588832, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.24951, 0.634021] (the system mobility (Wikidata Q900648))" + }, + { + "q": "Could you work out the electric field strength given drift velocity = 0.318734, mobility = 9.56607?", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.318734, 9.566071] (the system mobility (Wikidata Q900648))" + }, + { + "q": "I need the electric field strength for a drift velocity of 0.318734 and a mobility of 9.56607, please.", + "a": "0.03331922249257168", + "valid": true, + "kind": "compute", + "expect": 0.03331922249257168, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.318734, 9.566071] (the system mobility (Wikidata Q900648))" + }, + { + "q": "If the drift velocity is 0.671697 and the mobility is 14.2496, what is the electric field strength?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.671697, 14.249569] (the system mobility (Wikidata Q900648))" + }, + { + "q": "Could you work out the electric field strength given drift velocity = 0.671697, mobility = 14.2496?", + "a": "0.04713807027437235", + "valid": true, + "kind": "compute", + "expect": 0.04713807027437235, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.671697, 14.249569] (the system mobility (Wikidata Q900648))" + }, + { + "q": "I need the electric field strength for a drift velocity of 21.7931 and a mobility of 29.5106, please.", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [21.793126, 29.510609] (the system mobility (Wikidata Q900648))" + }, + { + "q": "If the drift velocity is 21.7931 and the mobility is 29.5106, what is the electric field strength?", + "a": "0.7384844800541926", + "valid": true, + "kind": "compute", + "expect": 0.7384844800541926, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [21.793126, 29.510609] (the system mobility (Wikidata Q900648))" + }, + { + "q": "Could you work out the electric field strength given drift velocity = 153.15, mobility = 18.5953?", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [153.149965, 18.595315] (the system mobility (Wikidata Q900648))" + }, + { + "q": "I need the electric field strength for a drift velocity of 153.15 and a mobility of 18.5953, please.", + "a": "8.235943368499306", + "valid": true, + "kind": "compute", + "expect": 8.235943368499306, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [153.149965, 18.595315] (the system mobility (Wikidata Q900648))" + }, + { + "q": "If the drift velocity is 8.50728, what is the electric field strength?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mobility is not given" + }, + { + "q": "Could you work out the electric field strength given drift velocity = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the mobility is not given" + }, + { + "q": "Do you know the electric field strength of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the electric field strength of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the electric field strength given drift velocity = -0.318734, mobility = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative drift velocity: no example had one" + }, + { + "q": "What is a typical electric field strength?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-electric-field-strength-8/program.py b/root/organs10/physics/specialists/solved-electric-field-strength-8/program.py new file mode 100644 index 0000000000000000000000000000000000000000..6dd7f475b58185c802f084033975d008e21002e2 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-8/program.py @@ -0,0 +1,7 @@ +"""electric field strength from drift velocity, mobility (mobility (Wikidata Q900648)) +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(drift_velocity, mobility): + return 1.0*drift_velocity/mobility diff --git a/root/organs10/physics/specialists/solved-electric-field-strength-8/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-field-strength-8/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-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-electric-field-strength-9/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-field-strength-9/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-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-electric-field-strength-9/panel.json b/root/organs10/physics/specialists/solved-electric-field-strength-9/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..3eac7ee300ea0d13bc823911728966ec6657fdfa --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-9/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric field strength for electrolytic conductivity 107.961 and electric current density 59.4105?", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [107.960666, 59.4105] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "Tell me the electric field strength with electrolytic conductivity of 107.961 and electric current density of 59.4105.", + "a": "0.5502976449934551", + "valid": true, + "kind": "compute", + "expect": 0.5502976449934551, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [107.960666, 59.4105] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "What is the electric field strength for electrolytic conductivity 169.527 and electric current density 280.774?", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [169.526958, 280.773702] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "Tell me the electric field strength with electrolytic conductivity of 169.527 and electric current density of 280.774.", + "a": "1.656218609813279", + "valid": true, + "kind": "compute", + "expect": 1.656218609813279, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [169.526958, 280.773702] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "What is the electric field strength for electrolytic conductivity 1.91195 and electric current density 82.2401?", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.911952, 82.240072] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "Tell me the electric field strength with electrolytic conductivity of 1.91195 and electric current density of 82.2401.", + "a": "43.01367504910926", + "valid": true, + "kind": "compute", + "expect": 43.01367504910926, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.911952, 82.240072] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "What is the electric field strength for electrolytic conductivity 1.64379 and electric current density 252.709?", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.643795, 252.709024] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "Tell me the electric field strength with electrolytic conductivity of 1.64379 and electric current density of 252.709.", + "a": "153.73507534197344", + "valid": true, + "kind": "compute", + "expect": 153.73507534197344, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [1.643795, 252.709024] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "What is the electric field strength for electrolytic conductivity 18.3169 and electric current density 6.8386?", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [18.316867, 6.838598] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "Tell me the electric field strength with electrolytic conductivity of 18.3169 and electric current density of 6.8386.", + "a": "0.37334976843585305", + "valid": true, + "kind": "compute", + "expect": 0.37334976843585305, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [18.316867, 6.838598] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "What is the electric field strength for electrolytic conductivity 156.502 and electric current density 185.886?", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [156.50181, 185.886273] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "Tell me the electric field strength with electrolytic conductivity of 156.502 and electric current density of 185.886.", + "a": "1.187757975790468", + "valid": true, + "kind": "compute", + "expect": 1.187757975790468, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [156.50181, 185.886273] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "What is the electric field strength for electrolytic conductivity 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current density is not given" + }, + { + "q": "Tell me the electric field strength with electrolytic conductivity of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current density is not given" + }, + { + "q": "What is the electric field strength of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the electric field strength of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the electric field strength with electrolytic conductivity of -1.91195 and electric current density of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electrolytic conductivity: no example had one" + }, + { + "q": "Which electric field strength is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the electrolytic conductivity is 8.50728 and the electric current density is 17.7888, what is the electric field strength?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.507283, 17.788778] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "Could you work out the electric field strength given electrolytic conductivity = 8.50728, electric current density = 17.7888?", + "a": "2.0910058888280627", + "valid": true, + "kind": "compute", + "expect": 2.0910058888280627, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.507283, 17.788778] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "I need the electric field strength for an electrolytic conductivity of 8.24951 and an electric current density of 0.634021, please.", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.24951, 0.634021] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "If the electrolytic conductivity is 8.24951 and the electric current density is 0.634021, what is the electric field strength?", + "a": "0.07685559300301205", + "valid": true, + "kind": "compute", + "expect": 0.07685559300301205, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [8.24951, 0.634021] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "Could you work out the electric field strength given electrolytic conductivity = 0.318734, electric current density = 9.56607?", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.318734, 9.566071] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "I need the electric field strength for an electrolytic conductivity of 0.318734 and an electric current density of 9.56607, please.", + "a": "30.01270513508963", + "valid": true, + "kind": "compute", + "expect": 30.01270513508963, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.318734, 9.566071] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "If the electrolytic conductivity is 0.671697 and the electric current density is 14.2496, what is the electric field strength?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.671697, 14.249569] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "Could you work out the electric field strength given electrolytic conductivity = 0.671697, electric current density = 14.2496?", + "a": "21.214275301882097", + "valid": true, + "kind": "compute", + "expect": 21.214275301882097, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [0.671697, 14.249569] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "I need the electric field strength for an electrolytic conductivity of 21.7931 and an electric current density of 29.5106, please.", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [21.793126, 29.510609] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "If the electrolytic conductivity is 21.7931 and the electric current density is 29.5106, what is the electric field strength?", + "a": "1.3541245984297685", + "valid": true, + "kind": "compute", + "expect": 1.3541245984297685, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [21.793126, 29.510609] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "Could you work out the electric field strength given electrolytic conductivity = 153.15, electric current density = 18.5953?", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [153.149965, 18.595315] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "I need the electric field strength for an electrolytic conductivity of 153.15 and an electric current density of 18.5953, please.", + "a": "0.12141899904566884", + "valid": true, + "kind": "compute", + "expect": 0.12141899904566884, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [153.149965, 18.595315] (the system electrolytic conductivity (Wikidata Q907564))" + }, + { + "q": "If the electrolytic conductivity is 8.50728, what is the electric field strength?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current density is not given" + }, + { + "q": "Could you work out the electric field strength given electrolytic conductivity = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric current density is not given" + }, + { + "q": "Do you know the electric field strength of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the electric field strength of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the electric field strength given electrolytic conductivity = -0.318734, electric current density = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electrolytic conductivity: no example had one" + }, + { + "q": "What is a typical electric field strength?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-electric-field-strength-9/program.py b/root/organs10/physics/specialists/solved-electric-field-strength-9/program.py new file mode 100644 index 0000000000000000000000000000000000000000..0749e7030862f402c333d6da662725518ca7bfae --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-9/program.py @@ -0,0 +1,7 @@ +"""electric field strength from electrolytic conductivity, electric current density (electrolytic conductivity (Wikidata Q907564)) +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(electrolytic_conductivity, electric_current_density): + return 1.0*electric_current_density/electrolytic_conductivity diff --git a/root/organs10/physics/specialists/solved-electric-field-strength-9/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-field-strength-9/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength-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-electric-field-strength/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-field-strength/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-electric-field-strength/panel.json b/root/organs10/physics/specialists/solved-electric-field-strength/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..a9c56d2379467a6b1f0e5e0941ce937a6a208b6c --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric field strength for electric field parameter 107.961, speed 59.4105 and magnetic flux density 5.77717?", + "a": "37054.71743803604", + "valid": true, + "kind": "compute", + "expect": 37054.71743803604, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [107.960666, 59.4105, 5.777165] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Tell me the electric field strength with electric field parameter of 107.961, speed of 59.4105 and magnetic flux density of 5.77717.", + "a": "37054.71743803604", + "valid": true, + "kind": "compute", + "expect": 37054.71743803604, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [107.960666, 59.4105, 5.777165] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "What is the electric field strength for electric field parameter 85.2416, speed 160.878 and magnetic flux density 2.69428?", + "a": "36948.1133481491", + "valid": true, + "kind": "compute", + "expect": 36948.1133481491, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [85.241623, 160.878442, 2.69428] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Tell me the electric field strength with electric field parameter of 85.2416, speed of 160.878 and magnetic flux density of 2.69428.", + "a": "36948.1133481491", + "valid": true, + "kind": "compute", + "expect": 36948.1133481491, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [85.241623, 160.878442, 2.69428] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "What is the electric field strength for electric field parameter 45.6157, speed 4.97131 and magnetic flux density 94.305?", + "a": "21385.51027534082", + "valid": true, + "kind": "compute", + "expect": 21385.51027534082, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [45.61567, 4.971307, 94.305046] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Tell me the electric field strength with electric field parameter of 45.6157, speed of 4.97131 and magnetic flux density of 94.305.", + "a": "21385.51027534082", + "valid": true, + "kind": "compute", + "expect": 21385.51027534082, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [45.61567, 4.971307, 94.305046] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "What is the electric field strength for electric field parameter 10.5638, speed 198.839 and magnetic flux density 0.671697?", + "a": "1410.9022889506464", + "valid": true, + "kind": "compute", + "expect": 1410.9022889506464, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [10.563843, 198.838955, 0.671697] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Tell me the electric field strength with electric field parameter of 10.5638, speed of 198.839 and magnetic flux density of 0.671697.", + "a": "1410.9022889506464", + "valid": true, + "kind": "compute", + "expect": 1410.9022889506464, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [10.563843, 198.838955, 0.671697] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "What is the electric field strength for electric field parameter 21.7931, speed 29.5106 and magnetic flux density 8.50341?", + "a": "5468.782894065435", + "valid": true, + "kind": "compute", + "expect": 5468.782894065435, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [21.793126, 29.510609, 8.503407] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Tell me the electric field strength with electric field parameter of 21.7931, speed of 29.5106 and magnetic flux density of 8.50341.", + "a": "5468.782894065435", + "valid": true, + "kind": "compute", + "expect": 5468.782894065435, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [21.793126, 29.510609, 8.503407] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "What is the electric field strength for electric field parameter 223.509, speed 17.3414 and magnetic flux density 7.10705?", + "a": "27546.66146773027", + "valid": true, + "kind": "compute", + "expect": 27546.66146773027, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [223.508796, 17.341435, 7.107049] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Tell me the electric field strength with electric field parameter of 223.509, speed of 17.3414 and magnetic flux density of 7.10705.", + "a": "27546.66146773027", + "valid": true, + "kind": "compute", + "expect": 27546.66146773027, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [223.508796, 17.341435, 7.107049] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "What is the electric field strength for electric field parameter 107.961 and speed 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 electric field strength with electric field parameter of 85.2416 and speed 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 electric field strength of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the electric field strength of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the electric field strength with electric field parameter of -45.6157, speed 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 electric field strength is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the electric field parameter is 17.7888, speed is 167.625 and the magnetic flux density is 10.3291, what is the electric field strength?", + "a": "30799.618802505258", + "valid": true, + "kind": "compute", + "expect": 30799.618802505258, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [17.788778, 167.624948, 10.329054] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Could you work out the electric field strength given electric field parameter = 17.7888, speed = 167.625, magnetic flux density = 10.3291?", + "a": "30799.618802505258", + "valid": true, + "kind": "compute", + "expect": 30799.618802505258, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [17.788778, 167.624948, 10.329054] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "I need the electric field strength for an electric field parameter of 21.511, a speed of 173.394 and a magnetic flux density of 251.084, please.", + "a": "936511.6480251562", + "valid": true, + "kind": "compute", + "expect": 936511.6480251562, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [21.511016, 173.393638, 251.084037] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "If the electric field parameter is 21.511, speed is 173.394 and the magnetic flux density is 251.084, what is the electric field strength?", + "a": "936511.6480251562", + "valid": true, + "kind": "compute", + "expect": 936511.6480251562, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [21.511016, 173.393638, 251.084037] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Could you work out the electric field strength given electric field parameter = 129.245, speed = 1.18359, magnetic flux density = 15.9416?", + "a": "2438.6371902755973", + "valid": true, + "kind": "compute", + "expect": 2438.6371902755973, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [129.244984, 1.18359, 15.941612] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "I need the electric field strength for an electric field parameter of 129.245, a speed of 1.18359 and a magnetic flux density of 15.9416, please.", + "a": "2438.6371902755973", + "valid": true, + "kind": "compute", + "expect": 2438.6371902755973, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [129.244984, 1.18359, 15.941612] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "If the electric field parameter is 20.3966, speed is 18.3169 and the magnetic flux density is 6.8386, what is the electric field strength?", + "a": "2554.907347285691", + "valid": true, + "kind": "compute", + "expect": 2554.907347285691, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [20.396558, 18.316867, 6.838598] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Could you work out the electric field strength given electric field parameter = 20.3966, speed = 18.3169, magnetic flux density = 6.8386?", + "a": "2554.907347285691", + "valid": true, + "kind": "compute", + "expect": 2554.907347285691, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [20.396558, 18.316867, 6.838598] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "I need the electric field strength for an electric field parameter of 185.886, a speed of 13.2373 and a magnetic flux density of 4.71941, please.", + "a": "11612.754502579606", + "valid": true, + "kind": "compute", + "expect": 11612.754502579606, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [185.886273, 13.237311, 4.719415] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "If the electric field parameter is 185.886, speed is 13.2373 and the magnetic flux density is 4.71941, what is the electric field strength?", + "a": "11612.754502579606", + "valid": true, + "kind": "compute", + "expect": 11612.754502579606, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [185.886273, 13.237311, 4.719415] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "Could you work out the electric field strength given electric field parameter = 9.1061, speed = 24.5721, magnetic flux density = 108.451?", + "a": "24266.511866047756", + "valid": true, + "kind": "compute", + "expect": 24266.511866047756, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength for [9.106104, 24.572086, 108.450792] (the system electric field parameter (Wikidata Q105221927))" + }, + { + "q": "I need the electric field strength for an electric field parameter of 9.1061, a speed of 24.5721 and a magnetic flux density of 108.451, please.", + "a": "24266.511866047756", + "valid": true, + "kind": "compute", + "expect": 24266.511866047756, + "dtype": "quantity", + "unit": "m*kg/s^3/A", + "why": "electric field strength 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 speed is 167.625, what is the electric field strength?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic flux density is not given" + }, + { + "q": "Could you work out the electric field strength given electric field parameter = 21.511, speed = 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 electric field strength of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the electric field strength of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the electric field strength given electric field parameter = -129.245, speed = 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 electric field strength?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-electric-field-strength/program.py b/root/organs10/physics/specialists/solved-electric-field-strength/program.py new file mode 100644 index 0000000000000000000000000000000000000000..e6d3209608a1355748bb3bc3d1789ebab68f7afc --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength/program.py @@ -0,0 +1,7 @@ +"""electric field strength from electric field parameter, speed, 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, speed, magnetic_flux_density): + return 1.0*electric_field_parameter*magnetic_flux_density*speed diff --git a/root/organs10/physics/specialists/solved-electric-field-strength/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-field-strength/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-field-strength/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-electric-flux-density/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-flux-density/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-flux-density/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-electric-flux-density/panel.json b/root/organs10/physics/specialists/solved-electric-flux-density/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..d8f17f516f807120f8136464446390da3f6b5214 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-flux-density/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric flux density for electric field strength 107.961 and permittivity 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "s*A/m^2", + "why": "electric flux density for [107.960666, 59.4105] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "Tell me the electric flux density with electric field strength of 107.961 and permittivity of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "s*A/m^2", + "why": "electric flux density for [107.960666, 59.4105] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "What is the electric flux density for electric field strength 169.527 and permittivity 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "s*A/m^2", + "why": "electric flux density for [169.526958, 280.773702] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "Tell me the electric flux density with electric field strength of 169.527 and permittivity of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "s*A/m^2", + "why": "electric flux density for [169.526958, 280.773702] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "What is the electric flux density for electric field strength 1.91195 and permittivity 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "s*A/m^2", + "why": "electric flux density for [1.911952, 82.240072] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "Tell me the electric flux density with electric field strength of 1.91195 and permittivity of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "s*A/m^2", + "why": "electric flux density for [1.911952, 82.240072] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "What is the electric flux density for electric field strength 1.64379 and permittivity 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "s*A/m^2", + "why": "electric flux density for [1.643795, 252.709024] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "Tell me the electric flux density with electric field strength of 1.64379 and permittivity of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "s*A/m^2", + "why": "electric flux density for [1.643795, 252.709024] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "What is the electric flux density for electric field strength 18.3169 and permittivity 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "s*A/m^2", + "why": "electric flux density for [18.316867, 6.838598] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "Tell me the electric flux density with electric field strength of 18.3169 and permittivity of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "s*A/m^2", + "why": "electric flux density for [18.316867, 6.838598] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "What is the electric flux density for electric field strength 156.502 and permittivity 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "s*A/m^2", + "why": "electric flux density for [156.50181, 185.886273] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "Tell me the electric flux density with electric field strength of 156.502 and permittivity of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "s*A/m^2", + "why": "electric flux density for [156.50181, 185.886273] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "What is the electric flux density for electric field strength 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the permittivity is not given" + }, + { + "q": "Tell me the electric flux density with electric field strength of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the permittivity is not given" + }, + { + "q": "What is the electric flux density of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the electric flux density of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the electric flux density with electric field strength of -1.91195 and permittivity of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric field strength: no example had one" + }, + { + "q": "Which electric flux density is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the electric field strength is 8.50728 and the permittivity is 17.7888, what is the electric flux density?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "s*A/m^2", + "why": "electric flux density for [8.507283, 17.788778] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "Could you work out the electric flux density given electric field strength = 8.50728, permittivity = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "s*A/m^2", + "why": "electric flux density for [8.507283, 17.788778] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "I need the electric flux density for an electric field strength of 8.24951 and a permittivity of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "s*A/m^2", + "why": "electric flux density for [8.24951, 0.634021] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "If the electric field strength is 8.24951 and the permittivity is 0.634021, what is the electric flux density?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "s*A/m^2", + "why": "electric flux density for [8.24951, 0.634021] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "Could you work out the electric flux density given electric field strength = 0.318734, permittivity = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "s*A/m^2", + "why": "electric flux density for [0.318734, 9.566071] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "I need the electric flux density for an electric field strength of 0.318734 and a permittivity of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "s*A/m^2", + "why": "electric flux density for [0.318734, 9.566071] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "If the electric field strength is 0.671697 and the permittivity is 14.2496, what is the electric flux density?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "s*A/m^2", + "why": "electric flux density for [0.671697, 14.249569] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "Could you work out the electric flux density given electric field strength = 0.671697, permittivity = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "s*A/m^2", + "why": "electric flux density for [0.671697, 14.249569] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "I need the electric flux density for an electric field strength of 21.7931 and a permittivity of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "s*A/m^2", + "why": "electric flux density for [21.793126, 29.510609] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "If the electric field strength is 21.7931 and the permittivity is 29.5106, what is the electric flux density?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "s*A/m^2", + "why": "electric flux density for [21.793126, 29.510609] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "Could you work out the electric flux density given electric field strength = 153.15, permittivity = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "s*A/m^2", + "why": "electric flux density for [153.149965, 18.595315] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "I need the electric flux density for an electric field strength of 153.15 and a permittivity of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "s*A/m^2", + "why": "electric flux density for [153.149965, 18.595315] (the system permittivity (Wikidata Q211569))" + }, + { + "q": "If the electric field strength is 8.50728, what is the electric flux density?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the permittivity is not given" + }, + { + "q": "Could you work out the electric flux density given electric field strength = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the permittivity is not given" + }, + { + "q": "Do you know the electric flux density of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the electric flux density of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the electric flux density given electric field strength = -0.318734, permittivity = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric field strength: no example had one" + }, + { + "q": "What is a typical electric flux density?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-electric-flux-density/program.py b/root/organs10/physics/specialists/solved-electric-flux-density/program.py new file mode 100644 index 0000000000000000000000000000000000000000..96c5b533a2a6aa9b09989794ef4aa02faeac4472 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-flux-density/program.py @@ -0,0 +1,7 @@ +"""electric flux density from electric field strength, permittivity (permittivity (Wikidata Q211569)) +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(electric_field_strength, permittivity): + return 1.0*electric_field_strength*permittivity diff --git a/root/organs10/physics/specialists/solved-electric-flux-density/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-flux-density/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-flux-density/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-electric-flux/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-flux/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-flux/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-electric-flux/panel.json b/root/organs10/physics/specialists/solved-electric-flux/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..a10ed9260be666c27bac2e2f99686d03c3995e64 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-flux/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric flux for electric charge 107.961?", + "a": "12193175489312.56", + "valid": true, + "kind": "compute", + "expect": 12193175489312.56, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A", + "why": "electric flux for [107.960666] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "Tell me the electric flux with electric charge of 107.961.", + "a": "12193175489312.56", + "valid": true, + "kind": "compute", + "expect": 12193175489312.56, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A", + "why": "electric flux for [107.960666] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "What is the electric flux for electric charge 22.652?", + "a": "2558339992377.2793", + "valid": true, + "kind": "compute", + "expect": 2558339992377.2793, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A", + "why": "electric flux for [22.652023] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "Tell me the electric flux with electric charge of 22.652.", + "a": "2558339992377.2793", + "valid": true, + "kind": "compute", + "expect": 2558339992377.2793, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A", + "why": "electric flux for [22.652023] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "What is the electric flux for electric charge 173.394?", + "a": "19583234730704.23", + "valid": true, + "kind": "compute", + "expect": 19583234730704.23, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A", + "why": "electric flux for [173.393638] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "Tell me the electric flux with electric charge of 173.394.", + "a": "19583234730704.23", + "valid": true, + "kind": "compute", + "expect": 19583234730704.23, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A", + "why": "electric flux for [173.393638] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "What is the electric flux for electric charge 126.709?", + "a": "14310657780620.148", + "valid": true, + "kind": "compute", + "expect": 14310657780620.148, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A", + "why": "electric flux for [126.709252] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "Tell me the electric flux with electric charge of 126.709.", + "a": "14310657780620.148", + "valid": true, + "kind": "compute", + "expect": 14310657780620.148, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A", + "why": "electric flux for [126.709252] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "What is the electric flux for electric charge 0.671697?", + "a": "75862090746.25525", + "valid": true, + "kind": "compute", + "expect": 75862090746.25525, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A", + "why": "electric flux for [0.671697] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "Tell me the electric flux with electric charge of 0.671697.", + "a": "75862090746.25525", + "valid": true, + "kind": "compute", + "expect": 75862090746.25525, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A", + "why": "electric flux for [0.671697] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "What is the electric flux for electric charge 1.16997?", + "a": "132137247359.2389", + "valid": true, + "kind": "compute", + "expect": 132137247359.2389, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A", + "why": "electric flux for [1.169968] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "Tell me the electric flux with electric charge of 1.16997.", + "a": "132137247359.2389", + "valid": true, + "kind": "compute", + "expect": 132137247359.2389, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A", + "why": "electric flux for [1.169968] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "What is the electric flux?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric charge is not given" + }, + { + "q": "Could you work out the electric flux, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric charge is not given" + }, + { + "q": "What is the electric flux of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the electric flux of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the electric flux with electric charge of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric charge: no example had one" + }, + { + "q": "Which electric flux is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the electric charge is 2.57003, what is the electric flux?", + "a": "290261508553.80853", + "valid": true, + "kind": "compute", + "expect": 290261508553.80853, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A", + "why": "electric flux for [2.57003] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "Could you work out the electric flux given electric charge = 2.57003?", + "a": "290261508553.80853", + "valid": true, + "kind": "compute", + "expect": 290261508553.80853, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A", + "why": "electric flux for [2.57003] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "I need the electric flux for an electric charge of 48.921, please.", + "a": "5525181062472.881", + "valid": true, + "kind": "compute", + "expect": 5525181062472.881, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A", + "why": "electric flux for [48.920991] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "If the electric charge is 48.921, what is the electric flux?", + "a": "5525181062472.881", + "valid": true, + "kind": "compute", + "expect": 5525181062472.881, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A", + "why": "electric flux for [48.920991] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "Could you work out the electric flux given electric charge = 0.348438?", + "a": "39352866322.78079", + "valid": true, + "kind": "compute", + "expect": 39352866322.78079, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A", + "why": "electric flux for [0.348438] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "I need the electric flux for an electric charge of 0.348438, please.", + "a": "39352866322.78079", + "valid": true, + "kind": "compute", + "expect": 39352866322.78079, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A", + "why": "electric flux for [0.348438] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "If the electric charge is 252.709, what is the electric flux?", + "a": "28541186320099.645", + "valid": true, + "kind": "compute", + "expect": 28541186320099.645, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A", + "why": "electric flux for [252.709024] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "Could you work out the electric flux given electric charge = 252.709?", + "a": "28541186320099.645", + "valid": true, + "kind": "compute", + "expect": 28541186320099.645, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A", + "why": "electric flux for [252.709024] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "I need the electric flux for an electric charge of 20.3966, please.", + "a": "2303605748983.057", + "valid": true, + "kind": "compute", + "expect": 2303605748983.057, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A", + "why": "electric flux for [20.396558] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "If the electric charge is 20.3966, what is the electric flux?", + "a": "2303605748983.057", + "valid": true, + "kind": "compute", + "expect": 2303605748983.057, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A", + "why": "electric flux for [20.396558] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "Could you work out the electric flux given electric charge = 134.993?", + "a": "15246227021987.145", + "valid": true, + "kind": "compute", + "expect": 15246227021987.145, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A", + "why": "electric flux for [134.992958] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "I need the electric flux for an electric charge of 134.993, please.", + "a": "15246227021987.145", + "valid": true, + "kind": "compute", + "expect": 15246227021987.145, + "dtype": "quantity", + "unit": "m^3*kg/s^3/A", + "why": "electric flux for [134.992958] (the system Gauss's law (Wikidata Q173356))" + }, + { + "q": "I need the electric flux, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric charge is not given" + }, + { + "q": "Can you tell me the electric flux?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electric charge is not given" + }, + { + "q": "Do you know the electric flux of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the electric flux of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the electric flux given electric charge = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric charge: no example had one" + }, + { + "q": "What is a typical electric flux?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-electric-flux/program.py b/root/organs10/physics/specialists/solved-electric-flux/program.py new file mode 100644 index 0000000000000000000000000000000000000000..6c2ea4a47c45e358e86a9910c08c0a59cf48149d --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-flux/program.py @@ -0,0 +1,7 @@ +"""electric flux from electric charge (Gauss's law (Wikidata Q173356)) +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(electric_charge): + return 112940906660.768*electric_charge diff --git a/root/organs10/physics/specialists/solved-electric-flux/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-flux/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-flux/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-electric-potential-difference/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-potential-difference/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-potential-difference/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-electric-potential-difference/panel.json b/root/organs10/physics/specialists/solved-electric-potential-difference/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..61a4e39ec0445e5d75cdf30b0666087f77f2c2ec --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-potential-difference/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric potential difference for voltage 107.961?", + "a": "107.96066588996202", + "valid": true, + "kind": "compute", + "expect": 107.96066588996202, + "dtype": "quantity", + "unit": "V", + "why": "electric potential difference for [107.960666] (the system voltage (Wikidata Q118309876))" + }, + { + "q": "Tell me the electric potential difference with voltage of 107.961.", + "a": "107.96066588996202", + "valid": true, + "kind": "compute", + "expect": 107.96066588996202, + "dtype": "quantity", + "unit": "V", + "why": "electric potential difference for [107.960666] (the system voltage (Wikidata Q118309876))" + }, + { + "q": "What is the electric potential difference for voltage 22.652?", + "a": "22.652022796855793", + "valid": true, + "kind": "compute", + "expect": 22.652022796855793, + "dtype": "quantity", + "unit": "V", + "why": "electric potential difference for [22.652023] (the system voltage (Wikidata Q118309876))" + }, + { + "q": "Tell me the electric potential difference with voltage of 22.652.", + "a": "22.652022796855793", + "valid": true, + "kind": "compute", + "expect": 22.652022796855793, + "dtype": "quantity", + "unit": "V", + "why": "electric potential difference for [22.652023] (the system voltage (Wikidata Q118309876))" + }, + { + "q": "What is the electric potential difference for voltage 173.394?", + "a": "173.3936384053025", + "valid": true, + "kind": "compute", + "expect": 173.3936384053025, + "dtype": "quantity", + "unit": "V", + "why": "electric potential difference for [173.393638] (the system voltage (Wikidata Q118309876))" + }, + { + "q": "Tell me the electric potential difference with voltage of 173.394.", + "a": "173.3936384053025", + "valid": true, + "kind": "compute", + "expect": 173.3936384053025, + "dtype": "quantity", + "unit": "V", + "why": "electric potential difference for [173.393638] (the system voltage (Wikidata Q118309876))" + }, + { + "q": "What is the electric potential difference for voltage 126.709?", + "a": "126.70925180018237", + "valid": true, + "kind": "compute", + "expect": 126.70925180018237, + "dtype": "quantity", + "unit": "V", + "why": "electric potential difference for [126.709252] (the system voltage (Wikidata Q118309876))" + }, + { + "q": "Tell me the electric potential difference with voltage of 126.709.", + "a": "126.70925180018237", + "valid": true, + "kind": "compute", + "expect": 126.70925180018237, + "dtype": "quantity", + "unit": "V", + "why": "electric potential difference for [126.709252] (the system voltage (Wikidata Q118309876))" + }, + { + "q": "What is the electric potential difference for voltage 0.671697?", + "a": "0.6716971997941935", + "valid": true, + "kind": "compute", + "expect": 0.6716971997941935, + "dtype": "quantity", + "unit": "V", + "why": "electric potential difference for [0.671697] (the system voltage (Wikidata Q118309876))" + }, + { + "q": "Tell me the electric potential difference with voltage of 0.671697.", + "a": "0.6716971997941935", + "valid": true, + "kind": "compute", + "expect": 0.6716971997941935, + "dtype": "quantity", + "unit": "V", + "why": "electric potential difference for [0.671697] (the system voltage (Wikidata Q118309876))" + }, + { + "q": "What is the electric potential difference for voltage 1.16997?", + "a": "1.1699680059779358", + "valid": true, + "kind": "compute", + "expect": 1.1699680059779358, + "dtype": "quantity", + "unit": "V", + "why": "electric potential difference for [1.169968] (the system voltage (Wikidata Q118309876))" + }, + { + "q": "Tell me the electric potential difference with voltage of 1.16997.", + "a": "1.1699680059779358", + "valid": true, + "kind": "compute", + "expect": 1.1699680059779358, + "dtype": "quantity", + "unit": "V", + "why": "electric potential difference for [1.169968] (the system voltage (Wikidata Q118309876))" + }, + { + "q": "What is the electric potential difference?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the voltage is not given" + }, + { + "q": "Could you work out the electric potential difference, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the voltage is not given" + }, + { + "q": "What is the electric potential difference 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 electric potential difference 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 electric potential difference with voltage of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative voltage: no example had one" + }, + { + "q": "Which electric potential difference is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the voltage is 2.57003, what is the electric potential difference?", + "a": "2.5700299133036437", + "valid": true, + "kind": "compute", + "expect": 2.5700299133036437, + "dtype": "quantity", + "unit": "V", + "why": "electric potential difference for [2.57003] (the system voltage (Wikidata Q118309876))" + }, + { + "q": "Could you work out the electric potential difference given voltage = 2.57003?", + "a": "2.5700299133036437", + "valid": true, + "kind": "compute", + "expect": 2.5700299133036437, + "dtype": "quantity", + "unit": "V", + "why": "electric potential difference for [2.57003] (the system voltage (Wikidata Q118309876))" + }, + { + "q": "I need the electric potential difference for a voltage of 48.921, please.", + "a": "48.920990860011834", + "valid": true, + "kind": "compute", + "expect": 48.920990860011834, + "dtype": "quantity", + "unit": "V", + "why": "electric potential difference for [48.920991] (the system voltage (Wikidata Q118309876))" + }, + { + "q": "If the voltage is 48.921, what is the electric potential difference?", + "a": "48.920990860011834", + "valid": true, + "kind": "compute", + "expect": 48.920990860011834, + "dtype": "quantity", + "unit": "V", + "why": "electric potential difference for [48.920991] (the system voltage (Wikidata Q118309876))" + }, + { + "q": "Could you work out the electric potential difference given voltage = 0.348438?", + "a": "0.3484376696300305", + "valid": true, + "kind": "compute", + "expect": 0.3484376696300305, + "dtype": "quantity", + "unit": "V", + "why": "electric potential difference for [0.348438] (the system voltage (Wikidata Q118309876))" + }, + { + "q": "I need the electric potential difference for a voltage of 0.348438, please.", + "a": "0.3484376696300305", + "valid": true, + "kind": "compute", + "expect": 0.3484376696300305, + "dtype": "quantity", + "unit": "V", + "why": "electric potential difference for [0.348438] (the system voltage (Wikidata Q118309876))" + }, + { + "q": "If the voltage is 252.709, what is the electric potential difference?", + "a": "252.70902424952752", + "valid": true, + "kind": "compute", + "expect": 252.70902424952752, + "dtype": "quantity", + "unit": "V", + "why": "electric potential difference for [252.709024] (the system voltage (Wikidata Q118309876))" + }, + { + "q": "Could you work out the electric potential difference given voltage = 252.709?", + "a": "252.70902424952752", + "valid": true, + "kind": "compute", + "expect": 252.70902424952752, + "dtype": "quantity", + "unit": "V", + "why": "electric potential difference for [252.709024] (the system voltage (Wikidata Q118309876))" + }, + { + "q": "I need the electric potential difference for a voltage of 20.3966, please.", + "a": "20.39655796196344", + "valid": true, + "kind": "compute", + "expect": 20.39655796196344, + "dtype": "quantity", + "unit": "V", + "why": "electric potential difference for [20.396558] (the system voltage (Wikidata Q118309876))" + }, + { + "q": "If the voltage is 20.3966, what is the electric potential difference?", + "a": "20.39655796196344", + "valid": true, + "kind": "compute", + "expect": 20.39655796196344, + "dtype": "quantity", + "unit": "V", + "why": "electric potential difference for [20.396558] (the system voltage (Wikidata Q118309876))" + }, + { + "q": "Could you work out the electric potential difference given voltage = 134.993?", + "a": "134.992957580738", + "valid": true, + "kind": "compute", + "expect": 134.992957580738, + "dtype": "quantity", + "unit": "V", + "why": "electric potential difference for [134.992958] (the system voltage (Wikidata Q118309876))" + }, + { + "q": "I need the electric potential difference for a voltage of 134.993, please.", + "a": "134.992957580738", + "valid": true, + "kind": "compute", + "expect": 134.992957580738, + "dtype": "quantity", + "unit": "V", + "why": "electric potential difference for [134.992958] (the system voltage (Wikidata Q118309876))" + }, + { + "q": "I need the electric potential difference, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the voltage is not given" + }, + { + "q": "Can you tell me the electric potential difference?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the voltage is not given" + }, + { + "q": "Do you know the electric potential difference 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 electric potential difference 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 electric potential difference given voltage = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative voltage: no example had one" + }, + { + "q": "What is a typical electric potential difference?", + "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-electric-potential-difference/program.py b/root/organs10/physics/specialists/solved-electric-potential-difference/program.py new file mode 100644 index 0000000000000000000000000000000000000000..55ca6ec3f50f8a2af381c07526b3e87d885c5db2 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-potential-difference/program.py @@ -0,0 +1,7 @@ +"""electric potential difference from voltage (voltage (Wikidata Q118309876)) +The maths organ (systems.py): solved from the system q1 = q0; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(voltage): + return 1.0*voltage diff --git a/root/organs10/physics/specialists/solved-electric-potential-difference/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-potential-difference/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-potential-difference/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-electric-power/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-power/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-power/organ_formula_v2.py @@ -0,0 +1,163 @@ +"""organ_formula_v2 - v1 + a question is declined as missing an input only when it names one of the inputs (v1 declined +"What is the largest living animal species that only eats plants?" for the zodiac program, whose output is "animal": +the gate found the core item made worse, 2026-10-20). A question that names the output and no input is not this +program's, and gets no reply. +v2 also keeps to the DOMAIN of its examples (spec.json "domain", written from the worked examples the composer saw): an +input every example gave as positive (or non-negative) must be so in the question, and an answer whose sign no example's +answer had is not given ("ABV for original gravity 1.010 and final gravity 1.050" would be -5.25): out_of_domain, an +in-domain refusal that says why. A program is evidence for the range its examples span in sign, not beyond. + +organ_formula_v1 - the GENERIC FORMULA SPECIALIST: one program the code composer wrote (program.py beside this file) and +the typed signature it was written for (spec.json: the inputs and the output, each a label with an optional unit in +parentheses). Built, not trained; no phrasing templates. + +Reading, by role: the output is named in the question (its label; when an input has the same label, the output's unit too, +"in F"); each input is its label with a value right after it ("original gravity 1.055", "age of 25", "f-number = 8") or +right before it ("1.055 original gravity"). A value is a number ("1,000", "0.5", "1/125") or, for an input whose examples +were codes, a token mixing digits and letters ("205/55R16"). + compute every input found: "The is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? is ." and the program it came from + missing_input a short direct question names the output but not every input's value: an in-domain refusal naming what + is missing +Narrowing (narrow_v1, the factory's shared rule): story-shaped and relational turns belong to the core; a turn carrying +numbers that no input claims is a different calculation and belongs to the core. Everything else: no reply.""" +from __future__ import annotations + +import importlib.util +import json +import os +import re + +from narrow_v1 import narrative, short_direct + +HERE = os.path.dirname(os.path.abspath(__file__)) +NUM = r"-?\d[\d,]*(?:\.\d+)?(?:\s*/\s*\d[\d,]*(?:\.\d+)?)?" +CODE = r"[A-Za-z0-9]*\d[A-Za-z0-9]*[/\-][A-Za-z0-9/\-]*\d[A-Za-z0-9]*" +ANYNUM = re.compile(r"(? 0 if sign == "positive" else v >= 0 + + +def sign_of(values): + """the sign every example gave: "positive", "nonnegative" or "any" (text values: "any")""" + if not values or any(isinstance(v, str) for v in values): + return "any" + if all(v > 0 for v in values): + return "positive" + return "nonnegative" if all(v >= 0 for v in values) else "any" + + +def _fmt(v): + if isinstance(v, str): + return v + v = float(v) + if v == 0: + return "0" + return ("%.6g" % v) if 1e-3 <= abs(v) < 1e7 else ("%.4g" % v) + + +class FormulaOrgan: + def __init__(self, spec="spec.json"): + self.spec = json.load(open(os.path.join(HERE, spec), encoding="utf-8")) + loader = importlib.util.spec_from_file_location("composed_program", os.path.join(HERE, "program.py")) + mod = importlib.util.module_from_spec(loader) + loader.loader.exec_module(mod) + self.compute = mod.compute + self.out_label, self.out_unit = split_label(self.spec["output"]) + self.ins = [split_label(x) for x in self.spec["inputs"]] + self.kinds = self.spec["input_kinds"] # "number" or "code", from the examples + dom = self.spec.get("domain") or {} + self.in_sign = dom.get("inputs") or ["any"] * len(self.kinds) # "positive", "nonnegative" or "any", per input + self.out_sign = dom.get("output", "any") + collide = any(lab.lower() == self.out_label.lower() for lab, _ in self.ins) + out = label_rx(self.out_label) + if collide and self.out_unit: # "the temperature in F" + out += r"\s+(?:in|to)\s+(?:degrees\s+)?" + re.escape(self.out_unit) + r"(?![\w])" + self.rx_out = re.compile(out, re.I) + self.rx_in = [] + for (lab, unit), kind in zip(self.ins, self.kinds): + v = CODE if kind == "code" else NUM + after = re.compile(label_rx(lab) + LINK + r"(" + v + r")(?![\w/])", re.I) + before = re.compile(r"(? 120, 80)""" + out = {} + for i, (v, kind) in enumerate(zip(vals, self.kinds)): + if kind == "code": + for k, x in enumerate(re.findall(r"\d+(?:\.\d+)?", str(v))): + out[(i, k)] = float(x) + elif not isinstance(v, str): + out[(i, 0)] = float(v) + return out + + def _domain_v3(self, vals): + dom = self.spec.get("domain") or {} + comp = self._components(vals) + for o in dom.get("order") or []: + a, b = tuple(o["greater"]), tuple(o["lesser"]) + if a in comp and b in comp and not comp[a] > comp[b]: + return ("In every worked example this program was written from, the %s is greater than the %s; here it is not " + "(%s and %s), so it is not a reading I can work from." % (o["greater_name"], o["lesser_name"], + _fmt(comp[a]), _fmt(comp[b]))) + for f in dom.get("facts") or []: + x = comp.get((f["input"], 0)) + if x is None: + continue + bad = (f.get("integer") and x != int(x)) or ("min" in f and x < f["min"]) or ("max" in f and x > f["max"]) + if bad: + return "%s is not a possible %s: %s. So I won't compute it." % (_fmt(x), f["name"], f["why"]) + return None + + def answer(self, text, session_id="default"): + t = re.sub(r"\s+", " ", str(text or "")).strip() + if not self.rx_out.search(t) or narrative(t) or not ASKING.search(t): + return None + vals, missing, spans = self._read(t) + stray = [m for m in ANYNUM.finditer(t) if not any(a <= m.start() < b for a, b in spans)] + if stray: # numbers no input claims: another calculation + return None + if missing: + named = any(re.search(label_rx(lab), t, re.I) for lab, _ in self.ins) + if named and short_direct(t): + return {"kind": "missing_input", "status": "declined", + "response": "To work out the %s I need the %s, and the question doesn't give %s." + % (self.out_label, " and the ".join(missing), "it" if len(missing) == 1 else "them")} + return None + for (lab, _), v, sign in zip(self.ins, vals, self.in_sign): + if not _in_sign(v, sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "Every worked example this program was written from has a %s %s; %s is outside that, so I " + "won't extrapolate." % (sign.replace("nonnegative", "non-negative"), lab, _fmt(v))} + refused = self._domain_v3(vals) + if refused: + return {"kind": "out_of_domain", "status": "declined", "response": refused} + try: + y = self.compute(*vals) + except Exception: + return None + if y is None or (isinstance(y, float) and y != y): + return None + if not _in_sign(y, self.out_sign): + return {"kind": "out_of_domain", "status": "declined", + "response": "That would make the %s %s, and every worked example this program was written from gave a %s " + "one, so I won't give it." % (self.out_label, _fmt(y), self.out_sign.replace("nonnegative", "non-negative"))} + unit =(" " + self.out_unit) if self.out_unit and not isinstance(y, str) else "" + return {"kind": "compute", "status": "answered", + "response": "The %s is %s%s. (A composed program: %s; %s.)" % (self.out_label, _fmt(y), unit, + self.spec["expression"], self.spec["said"])} diff --git a/root/organs10/physics/specialists/solved-electric-power/panel.json b/root/organs10/physics/specialists/solved-electric-power/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..54788bead94d14ef85f2131d1566f44359a72a89 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-power/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric power for electric current 107.961 and voltage 59.4105?", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "W", + "why": "electric power for [107.960666, 59.4105] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "Tell me the electric power with electric current of 107.961 and voltage of 59.4105.", + "a": "6413.997161494573", + "valid": true, + "kind": "compute", + "expect": 6413.997161494573, + "dtype": "quantity", + "unit": "W", + "why": "electric power for [107.960666, 59.4105] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "What is the electric power for electric current 169.527 and voltage 280.774?", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "W", + "why": "electric power for [169.526958, 280.773702] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "Tell me the electric power with electric current of 169.527 and voltage of 280.774.", + "a": "47598.71144694985", + "valid": true, + "kind": "compute", + "expect": 47598.71144694985, + "dtype": "quantity", + "unit": "W", + "why": "electric power for [169.526958, 280.773702] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "What is the electric power for electric current 1.91195 and voltage 82.2401?", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "W", + "why": "electric power for [1.911952, 82.240072] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "Tell me the electric power with electric current of 1.91195 and voltage of 82.2401.", + "a": "157.23905034769402", + "valid": true, + "kind": "compute", + "expect": 157.23905034769402, + "dtype": "quantity", + "unit": "W", + "why": "electric power for [1.911952, 82.240072] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "What is the electric power for electric current 1.64379 and voltage 252.709?", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "W", + "why": "electric power for [1.643795, 252.709024] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "Tell me the electric power with electric current of 1.64379 and voltage of 252.709.", + "a": "415.4019555725448", + "valid": true, + "kind": "compute", + "expect": 415.4019555725448, + "dtype": "quantity", + "unit": "W", + "why": "electric power for [1.643795, 252.709024] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "What is the electric power for electric current 18.3169 and voltage 6.8386?", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "W", + "why": "electric power for [18.316867, 6.838598] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "Tell me the electric power with electric current of 18.3169 and voltage of 6.8386.", + "a": "125.26169131331936", + "valid": true, + "kind": "compute", + "expect": 125.26169131331936, + "dtype": "quantity", + "unit": "W", + "why": "electric power for [18.316867, 6.838598] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "What is the electric power for electric current 156.502 and voltage 185.886?", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "W", + "why": "electric power for [156.50181, 185.886273] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "Tell me the electric power with electric current of 156.502 and voltage of 185.886.", + "a": "29091.53810517715", + "valid": true, + "kind": "compute", + "expect": 29091.53810517715, + "dtype": "quantity", + "unit": "W", + "why": "electric power for [156.50181, 185.886273] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "What is the electric power for electric current 107.961?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the voltage is not given" + }, + { + "q": "Tell me the electric power with electric current of 169.527.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the voltage is not given" + }, + { + "q": "What is the electric power of the Eiffel Tower?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "What is the electric power of Mount Everest?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Tell me the electric power with electric current of -1.91195 and voltage of 82.2401.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric current: no example had one" + }, + { + "q": "Which electric power is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the electric current is 8.50728 and the voltage is 17.7888, what is the electric power?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "W", + "why": "electric power for [8.507283, 17.788778] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "Could you work out the electric power given electric current = 8.50728, voltage = 17.7888?", + "a": "151.33416360087082", + "valid": true, + "kind": "compute", + "expect": 151.33416360087082, + "dtype": "quantity", + "unit": "W", + "why": "electric power for [8.507283, 17.788778] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "I need the electric power for an electric current of 8.24951 and a voltage of 0.634021, please.", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "W", + "why": "electric power for [8.24951, 0.634021] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "If the electric current is 8.24951 and the voltage is 0.634021, what is the electric power?", + "a": "5.230362271972724", + "valid": true, + "kind": "compute", + "expect": 5.230362271972724, + "dtype": "quantity", + "unit": "W", + "why": "electric power for [8.24951, 0.634021] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "Could you work out the electric power given electric current = 0.318734, voltage = 9.56607?", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "W", + "why": "electric power for [0.318734, 9.566071] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "I need the electric power for an electric current of 0.318734 and a voltage of 9.56607, please.", + "a": "3.0490325263462696", + "valid": true, + "kind": "compute", + "expect": 3.0490325263462696, + "dtype": "quantity", + "unit": "W", + "why": "electric power for [0.318734, 9.566071] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "If the electric current is 0.671697 and the voltage is 14.2496, what is the electric power?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "W", + "why": "electric power for [0.671697, 14.249569] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "Could you work out the electric power given electric current = 0.671697, voltage = 14.2496?", + "a": "9.571395807788361", + "valid": true, + "kind": "compute", + "expect": 9.571395807788361, + "dtype": "quantity", + "unit": "W", + "why": "electric power for [0.671697, 14.249569] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "I need the electric power for an electric current of 21.7931 and a voltage of 29.5106, please.", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "W", + "why": "electric power for [21.793126, 29.510609] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "If the electric current is 21.7931 and the voltage is 29.5106, what is the electric power?", + "a": "643.1284273894521", + "valid": true, + "kind": "compute", + "expect": 643.1284273894521, + "dtype": "quantity", + "unit": "W", + "why": "electric power for [21.793126, 29.510609] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "Could you work out the electric power given electric current = 153.15, voltage = 18.5953?", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "W", + "why": "electric power for [153.149965, 18.595315] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "I need the electric power for an electric current of 153.15 and a voltage of 18.5953, please.", + "a": "2847.871916067332", + "valid": true, + "kind": "compute", + "expect": 2847.871916067332, + "dtype": "quantity", + "unit": "W", + "why": "electric power for [153.149965, 18.595315] (the system Joule's laws (Wikidata Q210009))" + }, + { + "q": "If the electric current is 8.50728, what is the electric power?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the voltage is not given" + }, + { + "q": "Could you work out the electric power given electric current = 8.24951?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the voltage is not given" + }, + { + "q": "Do you know the electric power of a violin?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Do you know the electric power of the French Revolution?", + "a": "", + "valid": false, + "kind": "not_held", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a named thing, not inputs: a lookup the program cannot do" + }, + { + "q": "Could you work out the electric power given electric current = -0.318734, voltage = 9.56607?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electric current: no example had one" + }, + { + "q": "What is a typical electric power?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ] +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-electric-power/program.py b/root/organs10/physics/specialists/solved-electric-power/program.py new file mode 100644 index 0000000000000000000000000000000000000000..040f747fee16233e0b280a4e8808a475dae480f3 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-power/program.py @@ -0,0 +1,7 @@ +"""electric power from electric current, voltage (Joule's laws (Wikidata Q210009)) +The maths organ (systems.py): solved from the system q0 = q1*q2; checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(electric_current, voltage): + return 1.0*electric_current*voltage diff --git a/root/organs10/physics/specialists/solved-electric-power/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-power/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-power/scorer_formula_v1.py @@ -0,0 +1,31 @@ +"""scorer_formula_v1 - scorer for composed formula specialists: (good, asserted). +A must-decline item answered is a false yes. A valid quantity item is right when the FIRST number in the reply (the +answer, before any working) is within 0.5% of the sealed answer; a valid text item is right when the answer appears in the +reply as a whole word, exactly as written.""" +import re + + +def _first_num(text): + m = re.search(r"-?\d[\d,]*(?:\.\d+)?(?:[eE][+-]?\d+)?", text) + if not m: + return None + try: + return float(m.group(0).replace(",", "")) + except ValueError: + return None + + +def ok(p, r): + r = r or {} + text = str(r.get("response", "")) + answered = str(r.get("status", "")) == "answered" + if not p["valid"]: + return (not answered), answered + if not answered: + return False, False + if p.get("dtype") == "quantity": + want, got = float(p["expect"]), _first_num(text) + if got is None: + return False, True + return abs(got - want) <= (0.005 * abs(want) if want else 0.005), True + return re.search(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-electric-surface-current/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electric-surface-current/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-surface-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-electric-surface-current/panel.json b/root/organs10/physics/specialists/solved-electric-surface-current/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..58c9ee3d7d7b6c13002d97851c779a8da751a2fb --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-surface-current/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electric surface current for Ampère number 107.961, characteristic length 59.4105 and magnetic field strength 5.77717?", + "a": "37054.71743803604", + "valid": true, + "kind": "compute", + "expect": 37054.71743803604, + "dtype": "quantity", + "unit": "A", + "why": "electric surface current for [107.960666, 59.4105, 5.777165] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Tell me the electric surface current with Ampère number of 107.961, characteristic length of 59.4105 and magnetic field strength of 5.77717.", + "a": "37054.71743803604", + "valid": true, + "kind": "compute", + "expect": 37054.71743803604, + "dtype": "quantity", + "unit": "A", + "why": "electric surface current for [107.960666, 59.4105, 5.777165] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "What is the electric surface current for Ampère number 85.2416, characteristic length 160.878 and magnetic field strength 2.69428?", + "a": "36948.1133481491", + "valid": true, + "kind": "compute", + "expect": 36948.1133481491, + "dtype": "quantity", + "unit": "A", + "why": "electric surface current for [85.241623, 160.878442, 2.69428] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Tell me the electric surface current with Ampère number of 85.2416, characteristic length of 160.878 and magnetic field strength of 2.69428.", + "a": "36948.1133481491", + "valid": true, + "kind": "compute", + "expect": 36948.1133481491, + "dtype": "quantity", + "unit": "A", + "why": "electric surface current for [85.241623, 160.878442, 2.69428] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "What is the electric surface current for Ampère number 45.6157, characteristic length 4.97131 and magnetic field strength 94.305?", + "a": "21385.51027534082", + "valid": true, + "kind": "compute", + "expect": 21385.51027534082, + "dtype": "quantity", + "unit": "A", + "why": "electric surface current for [45.61567, 4.971307, 94.305046] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Tell me the electric surface current with Ampère number of 45.6157, characteristic length of 4.97131 and magnetic field strength of 94.305.", + "a": "21385.51027534082", + "valid": true, + "kind": "compute", + "expect": 21385.51027534082, + "dtype": "quantity", + "unit": "A", + "why": "electric surface current for [45.61567, 4.971307, 94.305046] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "What is the electric surface current for Ampère number 10.5638, characteristic length 198.839 and magnetic field strength 0.671697?", + "a": "1410.9022889506464", + "valid": true, + "kind": "compute", + "expect": 1410.9022889506464, + "dtype": "quantity", + "unit": "A", + "why": "electric surface current for [10.563843, 198.838955, 0.671697] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Tell me the electric surface current with Ampère number of 10.5638, characteristic length of 198.839 and magnetic field strength of 0.671697.", + "a": "1410.9022889506464", + "valid": true, + "kind": "compute", + "expect": 1410.9022889506464, + "dtype": "quantity", + "unit": "A", + "why": "electric surface current for [10.563843, 198.838955, 0.671697] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "What is the electric surface current for Ampère number 21.7931, characteristic length 29.5106 and magnetic field strength 8.50341?", + "a": "5468.782894065435", + "valid": true, + "kind": "compute", + "expect": 5468.782894065435, + "dtype": "quantity", + "unit": "A", + "why": "electric surface current for [21.793126, 29.510609, 8.503407] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Tell me the electric surface current with Ampère number of 21.7931, characteristic length of 29.5106 and magnetic field strength of 8.50341.", + "a": "5468.782894065435", + "valid": true, + "kind": "compute", + "expect": 5468.782894065435, + "dtype": "quantity", + "unit": "A", + "why": "electric surface current for [21.793126, 29.510609, 8.503407] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "What is the electric surface current for Ampère number 223.509, characteristic length 17.3414 and magnetic field strength 7.10705?", + "a": "27546.66146773027", + "valid": true, + "kind": "compute", + "expect": 27546.66146773027, + "dtype": "quantity", + "unit": "A", + "why": "electric surface current for [223.508796, 17.341435, 7.107049] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Tell me the electric surface current with Ampère number of 223.509, characteristic length of 17.3414 and magnetic field strength of 7.10705.", + "a": "27546.66146773027", + "valid": true, + "kind": "compute", + "expect": 27546.66146773027, + "dtype": "quantity", + "unit": "A", + "why": "electric surface current for [223.508796, 17.341435, 7.107049] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "What is the electric surface current for Ampère number 107.961 and characteristic length 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic field strength is not given" + }, + { + "q": "Tell me the electric surface current with Ampère number of 85.2416 and characteristic length of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic field strength is not given" + }, + { + "q": "What is the electric surface 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 electric surface 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 electric surface current with Ampère number of -45.6157, characteristic length of 4.97131 and magnetic field strength of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Ampère number: no example had one" + }, + { + "q": "Which electric surface 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 Ampère number is 17.7888, characteristic length is 167.625 and the magnetic field strength is 10.3291, what is the electric surface current?", + "a": "30799.618802505258", + "valid": true, + "kind": "compute", + "expect": 30799.618802505258, + "dtype": "quantity", + "unit": "A", + "why": "electric surface current for [17.788778, 167.624948, 10.329054] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Could you work out the electric surface current given Ampère number = 17.7888, characteristic length = 167.625, magnetic field strength = 10.3291?", + "a": "30799.618802505258", + "valid": true, + "kind": "compute", + "expect": 30799.618802505258, + "dtype": "quantity", + "unit": "A", + "why": "electric surface current for [17.788778, 167.624948, 10.329054] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "I need the electric surface current for an Ampère number of 21.511, a characteristic length of 173.394 and a magnetic field strength of 251.084, please.", + "a": "936511.6480251562", + "valid": true, + "kind": "compute", + "expect": 936511.6480251562, + "dtype": "quantity", + "unit": "A", + "why": "electric surface current for [21.511016, 173.393638, 251.084037] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "If the Ampère number is 21.511, characteristic length is 173.394 and the magnetic field strength is 251.084, what is the electric surface current?", + "a": "936511.6480251562", + "valid": true, + "kind": "compute", + "expect": 936511.6480251562, + "dtype": "quantity", + "unit": "A", + "why": "electric surface current for [21.511016, 173.393638, 251.084037] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Could you work out the electric surface current given Ampère number = 129.245, characteristic length = 1.18359, magnetic field strength = 15.9416?", + "a": "2438.6371902755973", + "valid": true, + "kind": "compute", + "expect": 2438.6371902755973, + "dtype": "quantity", + "unit": "A", + "why": "electric surface current for [129.244984, 1.18359, 15.941612] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "I need the electric surface current for an Ampère number of 129.245, a characteristic length of 1.18359 and a magnetic field strength of 15.9416, please.", + "a": "2438.6371902755973", + "valid": true, + "kind": "compute", + "expect": 2438.6371902755973, + "dtype": "quantity", + "unit": "A", + "why": "electric surface current for [129.244984, 1.18359, 15.941612] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "If the Ampère number is 20.3966, characteristic length is 18.3169 and the magnetic field strength is 6.8386, what is the electric surface current?", + "a": "2554.907347285691", + "valid": true, + "kind": "compute", + "expect": 2554.907347285691, + "dtype": "quantity", + "unit": "A", + "why": "electric surface current for [20.396558, 18.316867, 6.838598] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Could you work out the electric surface current given Ampère number = 20.3966, characteristic length = 18.3169, magnetic field strength = 6.8386?", + "a": "2554.907347285691", + "valid": true, + "kind": "compute", + "expect": 2554.907347285691, + "dtype": "quantity", + "unit": "A", + "why": "electric surface current for [20.396558, 18.316867, 6.838598] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "I need the electric surface current for an Ampère number of 185.886, a characteristic length of 13.2373 and a magnetic field strength of 4.71941, please.", + "a": "11612.754502579606", + "valid": true, + "kind": "compute", + "expect": 11612.754502579606, + "dtype": "quantity", + "unit": "A", + "why": "electric surface current for [185.886273, 13.237311, 4.719415] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "If the Ampère number is 185.886, characteristic length is 13.2373 and the magnetic field strength is 4.71941, what is the electric surface current?", + "a": "11612.754502579606", + "valid": true, + "kind": "compute", + "expect": 11612.754502579606, + "dtype": "quantity", + "unit": "A", + "why": "electric surface current for [185.886273, 13.237311, 4.719415] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "Could you work out the electric surface current given Ampère number = 9.1061, characteristic length = 24.5721, magnetic field strength = 108.451?", + "a": "24266.511866047756", + "valid": true, + "kind": "compute", + "expect": 24266.511866047756, + "dtype": "quantity", + "unit": "A", + "why": "electric surface current for [9.106104, 24.572086, 108.450792] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "I need the electric surface current for an Ampère number of 9.1061, a characteristic length of 24.5721 and a magnetic field strength of 108.451, please.", + "a": "24266.511866047756", + "valid": true, + "kind": "compute", + "expect": 24266.511866047756, + "dtype": "quantity", + "unit": "A", + "why": "electric surface current for [9.106104, 24.572086, 108.450792] (the system Ampère number (Wikidata Q105404651))" + }, + { + "q": "If the Ampère number is 17.7888 and the characteristic length is 167.625, what is the electric surface current?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic field strength is not given" + }, + { + "q": "Could you work out the electric surface current given Ampère number = 21.511, characteristic length = 173.394?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic field strength is not given" + }, + { + "q": "Do you know the electric surface 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 electric surface 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 electric surface current given Ampère number = -129.245, characteristic length = 1.18359, magnetic field strength = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Ampère number: no example had one" + }, + { + "q": "What is a typical electric surface 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-electric-surface-current/program.py b/root/organs10/physics/specialists/solved-electric-surface-current/program.py new file mode 100644 index 0000000000000000000000000000000000000000..3fc1d8c987a3cb1b22740efcba3398e3e7d0393d --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-surface-current/program.py @@ -0,0 +1,7 @@ +"""electric surface current from Ampère number, characteristic length, magnetic field strength (Ampère number (Wikidata Q105404651)) +The maths organ (systems.py): solved from the system q1 = ((q0)/(q2*q3)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(amp_re_number, characteristic_length, magnetic_field_strength): + return 1.0*amp_re_number*characteristic_length*magnetic_field_strength diff --git a/root/organs10/physics/specialists/solved-electric-surface-current/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electric-surface-current/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electric-surface-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-electrical-conductance/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electrical-conductance/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electrical-conductance/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-electrical-conductance/panel.json b/root/organs10/physics/specialists/solved-electrical-conductance/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..2e9a0dfd5e27f15f0c4f34a87b3639c52e53bec4 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electrical-conductance/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electrical conductance for electrical resistance 107.961?", + "a": "0.00926263275385168", + "valid": true, + "kind": "compute", + "expect": 0.00926263275385168, + "dtype": "quantity", + "unit": "s^3*A^2/m^2/kg", + "why": "electrical conductance for [107.960666] (the system electrical conductance (Wikidata Q309017))" + }, + { + "q": "Tell me the electrical conductance with electrical resistance of 107.961.", + "a": "0.00926263275385168", + "valid": true, + "kind": "compute", + "expect": 0.00926263275385168, + "dtype": "quantity", + "unit": "s^3*A^2/m^2/kg", + "why": "electrical conductance for [107.960666] (the system electrical conductance (Wikidata Q309017))" + }, + { + "q": "What is the electrical conductance for electrical resistance 22.652?", + "a": "0.044146167826513254", + "valid": true, + "kind": "compute", + "expect": 0.044146167826513254, + "dtype": "quantity", + "unit": "s^3*A^2/m^2/kg", + "why": "electrical conductance for [22.652023] (the system electrical conductance (Wikidata Q309017))" + }, + { + "q": "Tell me the electrical conductance with electrical resistance of 22.652.", + "a": "0.044146167826513254", + "valid": true, + "kind": "compute", + "expect": 0.044146167826513254, + "dtype": "quantity", + "unit": "s^3*A^2/m^2/kg", + "why": "electrical conductance for [22.652023] (the system electrical conductance (Wikidata Q309017))" + }, + { + "q": "What is the electrical conductance for electrical resistance 173.394?", + "a": "0.0057672242718762815", + "valid": true, + "kind": "compute", + "expect": 0.0057672242718762815, + "dtype": "quantity", + "unit": "s^3*A^2/m^2/kg", + "why": "electrical conductance for [173.393638] (the system electrical conductance (Wikidata Q309017))" + }, + { + "q": "Tell me the electrical conductance with electrical resistance of 173.394.", + "a": "0.0057672242718762815", + "valid": true, + "kind": "compute", + "expect": 0.0057672242718762815, + "dtype": "quantity", + "unit": "s^3*A^2/m^2/kg", + "why": "electrical conductance for [173.393638] (the system electrical conductance (Wikidata Q309017))" + }, + { + "q": "What is the electrical conductance for electrical resistance 126.709?", + "a": "0.007892083536070259", + "valid": true, + "kind": "compute", + "expect": 0.007892083536070259, + "dtype": "quantity", + "unit": "s^3*A^2/m^2/kg", + "why": "electrical conductance for [126.709252] (the system electrical conductance (Wikidata Q309017))" + }, + { + "q": "Tell me the electrical conductance with electrical resistance of 126.709.", + "a": "0.007892083536070259", + "valid": true, + "kind": "compute", + "expect": 0.007892083536070259, + "dtype": "quantity", + "unit": "s^3*A^2/m^2/kg", + "why": "electrical conductance for [126.709252] (the system electrical conductance (Wikidata Q309017))" + }, + { + "q": "What is the electrical conductance for electrical resistance 0.671697?", + "a": "1.4887660694527203", + "valid": true, + "kind": "compute", + "expect": 1.4887660694527203, + "dtype": "quantity", + "unit": "s^3*A^2/m^2/kg", + "why": "electrical conductance for [0.671697] (the system electrical conductance (Wikidata Q309017))" + }, + { + "q": "Tell me the electrical conductance with electrical resistance of 0.671697.", + "a": "1.4887660694527203", + "valid": true, + "kind": "compute", + "expect": 1.4887660694527203, + "dtype": "quantity", + "unit": "s^3*A^2/m^2/kg", + "why": "electrical conductance for [0.671697] (the system electrical conductance (Wikidata Q309017))" + }, + { + "q": "What is the electrical conductance for electrical resistance 1.16997?", + "a": "0.8547242274066585", + "valid": true, + "kind": "compute", + "expect": 0.8547242274066585, + "dtype": "quantity", + "unit": "s^3*A^2/m^2/kg", + "why": "electrical conductance for [1.169968] (the system electrical conductance (Wikidata Q309017))" + }, + { + "q": "Tell me the electrical conductance with electrical resistance of 1.16997.", + "a": "0.8547242274066585", + "valid": true, + "kind": "compute", + "expect": 0.8547242274066585, + "dtype": "quantity", + "unit": "s^3*A^2/m^2/kg", + "why": "electrical conductance for [1.169968] (the system electrical conductance (Wikidata Q309017))" + }, + { + "q": "What is the electrical conductance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electrical resistance is not given" + }, + { + "q": "Could you work out the electrical conductance, please?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electrical resistance is not given" + }, + { + "q": "What is the electrical conductance 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 electrical conductance 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 electrical conductance with electrical resistance of -173.394.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electrical resistance: no example had one" + }, + { + "q": "Which electrical conductance is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the electrical resistance is 2.57003, what is the electrical conductance?", + "a": "0.38910052946214563", + "valid": true, + "kind": "compute", + "expect": 0.38910052946214563, + "dtype": "quantity", + "unit": "s^3*A^2/m^2/kg", + "why": "electrical conductance for [2.57003] (the system electrical conductance (Wikidata Q309017))" + }, + { + "q": "Could you work out the electrical conductance given electrical resistance = 2.57003?", + "a": "0.38910052946214563", + "valid": true, + "kind": "compute", + "expect": 0.38910052946214563, + "dtype": "quantity", + "unit": "s^3*A^2/m^2/kg", + "why": "electrical conductance for [2.57003] (the system electrical conductance (Wikidata Q309017))" + }, + { + "q": "I need the electrical conductance for an electrical resistance of 48.921, please.", + "a": "0.02044112317474344", + "valid": true, + "kind": "compute", + "expect": 0.02044112317474344, + "dtype": "quantity", + "unit": "s^3*A^2/m^2/kg", + "why": "electrical conductance for [48.920991] (the system electrical conductance (Wikidata Q309017))" + }, + { + "q": "If the electrical resistance is 48.921, what is the electrical conductance?", + "a": "0.02044112317474344", + "valid": true, + "kind": "compute", + "expect": 0.02044112317474344, + "dtype": "quantity", + "unit": "s^3*A^2/m^2/kg", + "why": "electrical conductance for [48.920991] (the system electrical conductance (Wikidata Q309017))" + }, + { + "q": "Could you work out the electrical conductance given electrical resistance = 0.348438?", + "a": "2.869953759769417", + "valid": true, + "kind": "compute", + "expect": 2.869953759769417, + "dtype": "quantity", + "unit": "s^3*A^2/m^2/kg", + "why": "electrical conductance for [0.348438] (the system electrical conductance (Wikidata Q309017))" + }, + { + "q": "I need the electrical conductance for an electrical resistance of 0.348438, please.", + "a": "2.869953759769417", + "valid": true, + "kind": "compute", + "expect": 2.869953759769417, + "dtype": "quantity", + "unit": "s^3*A^2/m^2/kg", + "why": "electrical conductance for [0.348438] (the system electrical conductance (Wikidata Q309017))" + }, + { + "q": "If the electrical resistance is 252.709, what is the electrical conductance?", + "a": "0.0039571202610184176", + "valid": true, + "kind": "compute", + "expect": 0.0039571202610184176, + "dtype": "quantity", + "unit": "s^3*A^2/m^2/kg", + "why": "electrical conductance for [252.709024] (the system electrical conductance (Wikidata Q309017))" + }, + { + "q": "Could you work out the electrical conductance given electrical resistance = 252.709?", + "a": "0.0039571202610184176", + "valid": true, + "kind": "compute", + "expect": 0.0039571202610184176, + "dtype": "quantity", + "unit": "s^3*A^2/m^2/kg", + "why": "electrical conductance for [252.709024] (the system electrical conductance (Wikidata Q309017))" + }, + { + "q": "I need the electrical conductance for an electrical resistance of 20.3966, please.", + "a": "0.04902788018766951", + "valid": true, + "kind": "compute", + "expect": 0.04902788018766951, + "dtype": "quantity", + "unit": "s^3*A^2/m^2/kg", + "why": "electrical conductance for [20.396558] (the system electrical conductance (Wikidata Q309017))" + }, + { + "q": "If the electrical resistance is 20.3966, what is the electrical conductance?", + "a": "0.04902788018766951", + "valid": true, + "kind": "compute", + "expect": 0.04902788018766951, + "dtype": "quantity", + "unit": "s^3*A^2/m^2/kg", + "why": "electrical conductance for [20.396558] (the system electrical conductance (Wikidata Q309017))" + }, + { + "q": "Could you work out the electrical conductance given electrical resistance = 134.993?", + "a": "0.007407793842889246", + "valid": true, + "kind": "compute", + "expect": 0.007407793842889246, + "dtype": "quantity", + "unit": "s^3*A^2/m^2/kg", + "why": "electrical conductance for [134.992958] (the system electrical conductance (Wikidata Q309017))" + }, + { + "q": "I need the electrical conductance for an electrical resistance of 134.993, please.", + "a": "0.007407793842889246", + "valid": true, + "kind": "compute", + "expect": 0.007407793842889246, + "dtype": "quantity", + "unit": "s^3*A^2/m^2/kg", + "why": "electrical conductance for [134.992958] (the system electrical conductance (Wikidata Q309017))" + }, + { + "q": "I need the electrical conductance, please.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electrical resistance is not given" + }, + { + "q": "Can you tell me the electrical conductance?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the electrical resistance is not given" + }, + { + "q": "Do you know the electrical conductance 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 electrical conductance 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 electrical conductance given electrical resistance = -0.348438?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative electrical resistance: no example had one" + }, + { + "q": "What is a typical electrical conductance?", + "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-electrical-conductance/program.py b/root/organs10/physics/specialists/solved-electrical-conductance/program.py new file mode 100644 index 0000000000000000000000000000000000000000..5c0c981d19484c883b37ca0d2d2fafd109a6af89 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electrical-conductance/program.py @@ -0,0 +1,7 @@ +"""electrical conductance from electrical resistance (electrical conductance (Wikidata Q309017)) +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(electrical_resistance): + return 1.0/electrical_resistance diff --git a/root/organs10/physics/specialists/solved-electrical-conductance/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electrical-conductance/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electrical-conductance/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-electrical-conductivity-2/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electrical-conductivity-2/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electrical-conductivity-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-electrical-conductivity-2/panel.json b/root/organs10/physics/specialists/solved-electrical-conductivity-2/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..e496cc395790d9ca7886431d64b0c47e75e47432 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electrical-conductivity-2/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electrical conductivity for Roberts number 107.961, thermal diffusivity 59.4105 and magnetic permeability 5.77717?", + "a": "0.31454848897160476", + "valid": true, + "kind": "compute", + "expect": 0.31454848897160476, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [107.960666, 59.4105, 5.777165] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Tell me the electrical conductivity with Roberts number of 107.961, thermal diffusivity of 59.4105 and magnetic permeability of 5.77717.", + "a": "0.31454848897160476", + "valid": true, + "kind": "compute", + "expect": 0.31454848897160476, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [107.960666, 59.4105, 5.777165] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "What is the electrical conductivity for Roberts number 85.2416, thermal diffusivity 160.878 and magnetic permeability 2.69428?", + "a": "0.19665778745152376", + "valid": true, + "kind": "compute", + "expect": 0.19665778745152376, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [85.241623, 160.878442, 2.69428] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Tell me the electrical conductivity with Roberts number of 85.2416, thermal diffusivity of 160.878 and magnetic permeability of 2.69428.", + "a": "0.19665778745152376", + "valid": true, + "kind": "compute", + "expect": 0.19665778745152376, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [85.241623, 160.878442, 2.69428] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "What is the electrical conductivity for Roberts number 45.6157, thermal diffusivity 4.97131 and magnetic permeability 94.305?", + "a": "0.09729902784967777", + "valid": true, + "kind": "compute", + "expect": 0.09729902784967777, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [45.61567, 4.971307, 94.305046] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Tell me the electrical conductivity with Roberts number of 45.6157, thermal diffusivity of 4.97131 and magnetic permeability of 94.305.", + "a": "0.09729902784967777", + "valid": true, + "kind": "compute", + "expect": 0.09729902784967777, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [45.61567, 4.971307, 94.305046] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "What is the electrical conductivity for Roberts number 10.5638, thermal diffusivity 198.839 and magnetic permeability 0.671697?", + "a": "0.07909461512056756", + "valid": true, + "kind": "compute", + "expect": 0.07909461512056756, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [10.563843, 198.838955, 0.671697] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Tell me the electrical conductivity with Roberts number of 10.5638, thermal diffusivity of 198.839 and magnetic permeability of 0.671697.", + "a": "0.07909461512056756", + "valid": true, + "kind": "compute", + "expect": 0.07909461512056756, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [10.563843, 198.838955, 0.671697] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "What is the electrical conductivity for Roberts number 21.7931, thermal diffusivity 29.5106 and magnetic permeability 8.50341?", + "a": "0.08684571530973768", + "valid": true, + "kind": "compute", + "expect": 0.08684571530973768, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [21.793126, 29.510609, 8.503407] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Tell me the electrical conductivity with Roberts number of 21.7931, thermal diffusivity of 29.5106 and magnetic permeability of 8.50341.", + "a": "0.08684571530973768", + "valid": true, + "kind": "compute", + "expect": 0.08684571530973768, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [21.793126, 29.510609, 8.503407] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "What is the electrical conductivity for Roberts number 223.509, thermal diffusivity 17.3414 and magnetic permeability 7.10705?", + "a": "1.813511306470856", + "valid": true, + "kind": "compute", + "expect": 1.813511306470856, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [223.508796, 17.341435, 7.107049] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Tell me the electrical conductivity with Roberts number of 223.509, thermal diffusivity of 17.3414 and magnetic permeability of 7.10705.", + "a": "1.813511306470856", + "valid": true, + "kind": "compute", + "expect": 1.813511306470856, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [223.508796, 17.341435, 7.107049] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "What is the electrical conductivity for Roberts number 107.961 and thermal diffusivity 59.4105?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "Tell me the electrical conductivity with Roberts number of 85.2416 and thermal diffusivity of 160.878.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "What is the electrical conductivity 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 electrical conductivity 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 electrical conductivity with Roberts number of -45.6157, thermal diffusivity of 4.97131 and magnetic permeability of 94.305.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Roberts number: no example had one" + }, + { + "q": "Which electrical conductivity is the most common?", + "a": "", + "valid": false, + "kind": "output_only", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "names the output and no input: not this program's" + } + ], + "heldout": [ + { + "q": "If the Roberts number is 17.7888, thermal diffusivity is 167.625 and the magnetic permeability is 10.3291, what is the electrical conductivity?", + "a": "0.010274173498684694", + "valid": true, + "kind": "compute", + "expect": 0.010274173498684694, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [17.788778, 167.624948, 10.329054] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Could you work out the electrical conductivity given Roberts number = 17.7888, thermal diffusivity = 167.625, magnetic permeability = 10.3291?", + "a": "0.010274173498684694", + "valid": true, + "kind": "compute", + "expect": 0.010274173498684694, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [17.788778, 167.624948, 10.329054] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "I need the electrical conductivity for a Roberts number of 21.511, a thermal diffusivity of 173.394 and a magnetic permeability of 251.084, please.", + "a": "0.000494092950151496", + "valid": true, + "kind": "compute", + "expect": 0.000494092950151496, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [21.511016, 173.393638, 251.084037] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "If the Roberts number is 21.511, thermal diffusivity is 173.394 and the magnetic permeability is 251.084, what is the electrical conductivity?", + "a": "0.000494092950151496", + "valid": true, + "kind": "compute", + "expect": 0.000494092950151496, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [21.511016, 173.393638, 251.084037] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Could you work out the electrical conductivity given Roberts number = 129.245, thermal diffusivity = 1.18359, magnetic permeability = 15.9416?", + "a": "6.849836414751194", + "valid": true, + "kind": "compute", + "expect": 6.849836414751194, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [129.244984, 1.18359, 15.941612] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "I need the electrical conductivity for a Roberts number of 129.245, a thermal diffusivity of 1.18359 and a magnetic permeability of 15.9416, please.", + "a": "6.849836414751194", + "valid": true, + "kind": "compute", + "expect": 6.849836414751194, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [129.244984, 1.18359, 15.941612] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "If the Roberts number is 20.3966, thermal diffusivity is 18.3169 and the magnetic permeability is 6.8386, what is the electrical conductivity?", + "a": "0.16283157083473473", + "valid": true, + "kind": "compute", + "expect": 0.16283157083473473, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [20.396558, 18.316867, 6.838598] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Could you work out the electrical conductivity given Roberts number = 20.3966, thermal diffusivity = 18.3169, magnetic permeability = 6.8386?", + "a": "0.16283157083473473", + "valid": true, + "kind": "compute", + "expect": 0.16283157083473473, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [20.396558, 18.316867, 6.838598] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "I need the electrical conductivity for a Roberts number of 185.886, a thermal diffusivity of 13.2373 and a magnetic permeability of 4.71941, please.", + "a": "2.975496158535072", + "valid": true, + "kind": "compute", + "expect": 2.975496158535072, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [185.886273, 13.237311, 4.719415] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "If the Roberts number is 185.886, thermal diffusivity is 13.2373 and the magnetic permeability is 4.71941, what is the electrical conductivity?", + "a": "2.975496158535072", + "valid": true, + "kind": "compute", + "expect": 2.975496158535072, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [185.886273, 13.237311, 4.719415] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "Could you work out the electrical conductivity given Roberts number = 9.1061, thermal diffusivity = 24.5721, magnetic permeability = 108.451?", + "a": "0.003417101331511669", + "valid": true, + "kind": "compute", + "expect": 0.003417101331511669, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [9.106104, 24.572086, 108.450792] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "I need the electrical conductivity for a Roberts number of 9.1061, a thermal diffusivity of 24.5721 and a magnetic permeability of 108.451, please.", + "a": "0.003417101331511669", + "valid": true, + "kind": "compute", + "expect": 0.003417101331511669, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [9.106104, 24.572086, 108.450792] (the system Roberts number (Wikidata Q105190387))" + }, + { + "q": "If the Roberts number is 17.7888 and the thermal diffusivity is 167.625, what is the electrical conductivity?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the magnetic permeability is not given" + }, + { + "q": "Could you work out the electrical conductivity given Roberts number = 21.511, thermal diffusivity = 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 electrical conductivity 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 electrical conductivity 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 electrical conductivity given Roberts number = -129.245, thermal diffusivity = 1.18359, magnetic permeability = 15.9416?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Roberts number: no example had one" + }, + { + "q": "What is a typical electrical conductivity?", + "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-electrical-conductivity-2/program.py b/root/organs10/physics/specialists/solved-electrical-conductivity-2/program.py new file mode 100644 index 0000000000000000000000000000000000000000..e8727417ac712aa165ef5c577aa7e1aae8a4a423 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electrical-conductivity-2/program.py @@ -0,0 +1,7 @@ +"""electrical conductivity from Roberts number, thermal diffusivity, 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(roberts_number, thermal_diffusivity, magnetic_permeability): + return 1.0*roberts_number/(magnetic_permeability*thermal_diffusivity) diff --git a/root/organs10/physics/specialists/solved-electrical-conductivity-2/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electrical-conductivity-2/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electrical-conductivity-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-electrical-conductivity-3/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electrical-conductivity-3/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electrical-conductivity-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-electrical-conductivity/exam_log.jsonl b/root/organs10/physics/specialists/solved-electrical-conductivity/exam_log.jsonl new file mode 100644 index 0000000000000000000000000000000000000000..d33ca22b87c539073a241683a1be01f657e8fb1a --- /dev/null +++ b/root/organs10/physics/specialists/solved-electrical-conductivity/exam_log.jsonl @@ -0,0 +1 @@ +{"t": "2026-10-25T01:06:29Z", "heldout_sha": "64bdf17b1c0f4e45ec39d9ba6e838c1da91c67d99cbf0d2d4b85a00619c655f3", "files_sha": "36c790ad35706114cfd5a1bf4702bd6a3a7a4a7b38083073ef1bcb214c8fa47c"} diff --git a/root/organs10/physics/specialists/solved-electrical-conductivity/manifest.json b/root/organs10/physics/specialists/solved-electrical-conductivity/manifest.json new file mode 100644 index 0000000000000000000000000000000000000000..9f150600f1bbde935b790593c640d4e0f204ceb7 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electrical-conductivity/manifest.json @@ -0,0 +1,41 @@ +{ + "id": "solved-electrical-conductivity", + "name": "Solved: electrical conductivity from Lorentz number, voltage difference, thermal conductivity, temperature difference (Lorentz number (Wikidata Q104901522))", + "sources": [ + { + "name": "the maths organ: solved from the system q0 = ((q1*( q2 ) **(2))/(q3*q4)); checked by substitution into every equation and by a numeric solution at 12 points", + "licence": "yours" + } + ], + "organ": { + "module": "organ_formula_v3", + "class": "FormulaOrgan", + "init": {} + }, + "accept": { + "kinds": [ + "compute" + ], + "declines_in_domain": [ + "missing_input", + "out_of_domain" + ], + "context_regex": "\\belectrical\\ conductivity\\b" + }, + "panel": { + "path": "panel.json", + "scorer_module": "scorer_formula_v1", + "scorer_fn": "ok" + }, + "budgets": { + "latency_p50_ms": 200 + }, + "notes": "Solved by the maths organ from the system Lorentz number (Wikidata Q104901522): 6 checked points used to write the panel's dev half, 6 its held-out half.", + "built_from": { + "questions": [ + "What is the electrical conductivity for Lorentz number and voltage difference and thermal conductivity and temperature difference?" + ], + "gap": "", + "node": "hod" + } +} \ No newline at end of file diff --git a/root/organs10/physics/specialists/solved-electrical-conductivity/narrow_v1.py b/root/organs10/physics/specialists/solved-electrical-conductivity/narrow_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..647d4d5e961acd006c9de30f0ce8b0b8f5139cda --- /dev/null +++ b/root/organs10/physics/specialists/solved-electrical-conductivity/narrow_v1.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 +"""narrow_v1 - the shared NARROWING rule for specialist organs (2026-09-29, from the full-core sweep): + + WORD PROBLEMS AND NARRATIVE TURNS BELONG TO THE HEAD. An organ answers a direct domain question. A turn that tells a + story ("Justine wants to put a deck ...", "A new energy drink advertises 106 calories for 8 ounces ...") or relates + quantities to each other ("the length is 9 inches more than the width") is a word problem, which the core's word- + problem organ owns; organs firing on its nouns declined it or answered a different question. + An organ may DECLINE only a short direct question (it is then refusing the domain question that was asked); on + anything longer it says nothing and the head answers. + +Narrowing only ever removes turns an organ takes, so on any input where it does not fire, the organ's reply is +byte-identical to the frozen version it wraps (checked per specialist by an invariance count before re-gating). +""" +import re + +RELATION = re.compile(r"\b(?:\d+(?:\.\d+)?|one|two|three|four|five|six|seven|eight|nine|ten)\s+(?:\w+\s+){0,2}(?:more|less|fewer|longer|shorter)\s+than\b|" + r"\b(?:twice|three times|half)\s+(?:as\s+\w+\s+as\s+)?the\s+\w+\b", re.I) + + +# a RATE ("106 calories for 8 ounces", "3 apples per 2 dollars") makes the turn a proportion problem +RATE = re.compile(r"\b\d+(?:\.\d+)?\s+[a-z]+\s+(?:for|per|every)\s+(?:\d+(?:\.\d+)?|a|an|one|each)\s+[a-z]+", re.I) +NUM = re.compile(r"(?= 3 or len(t.split()) > 40 or RELATION.search(t) is not None or RATE.search(t) is not None + + +def short_direct(t): + """a direct question an organ may decline: at most 2 sentences, at most 25 words, not narrative""" + t = str(t or "") + return sentences(t) <= 2 and len(t.split()) <= 25 and not narrative(t) diff --git a/root/organs10/physics/specialists/solved-electrical-conductivity/organ_formula_v2.py b/root/organs10/physics/specialists/solved-electrical-conductivity/organ_formula_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..af7f3adaf0b052c69462e0e7d04e75a36c3dc020 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electrical-conductivity/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-electrical-conductivity/panel.json b/root/organs10/physics/specialists/solved-electrical-conductivity/panel.json new file mode 100644 index 0000000000000000000000000000000000000000..46a789f082bd8e05800ce5bfeaf1c0c34e602261 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electrical-conductivity/panel.json @@ -0,0 +1,366 @@ +{ + "dev": [ + { + "q": "What is the electrical conductivity for Lorentz number 107.961, voltage difference 59.4105, thermal conductivity 5.77717 and temperature difference 1.89126?", + "a": "0.3341982183624363", + "valid": true, + "kind": "compute", + "expect": 0.3341982183624363, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [107.960666, 59.4105, 5.777165, 1.891256] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "Tell me the electrical conductivity with Lorentz number of 107.961, voltage difference of 59.4105, thermal conductivity of 5.77717 and temperature difference of 1.89126.", + "a": "0.3341982183624363", + "valid": true, + "kind": "compute", + "expect": 0.3341982183624363, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [107.960666, 59.4105, 5.777165, 1.891256] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "What is the electrical conductivity for Lorentz number 2.69428, voltage difference 48.921, thermal conductivity 157.223 and temperature difference 35.6412?", + "a": "6.308394988391384", + "valid": true, + "kind": "compute", + "expect": 6.308394988391384, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [2.69428, 48.920991, 157.22256, 35.641157] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "Tell me the electrical conductivity with Lorentz number of 2.69428, voltage difference of 48.921, thermal conductivity of 157.223 and temperature difference of 35.6412.", + "a": "6.308394988391384", + "valid": true, + "kind": "compute", + "expect": 6.308394988391384, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [2.69428, 48.920991, 157.22256, 35.641157] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "What is the electrical conductivity for Lorentz number 0.318734, voltage difference 9.56607, thermal conductivity 126.709 and temperature difference 1.70503?", + "a": "0.7524925702829611", + "valid": true, + "kind": "compute", + "expect": 0.7524925702829611, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [0.318734, 9.566071, 126.709252, 1.705032] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "Tell me the electrical conductivity with Lorentz number of 0.318734, voltage difference of 9.56607, thermal conductivity of 126.709 and temperature difference of 1.70503.", + "a": "0.7524925702829611", + "valid": true, + "kind": "compute", + "expect": 0.7524925702829611, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [0.318734, 9.566071, 126.709252, 1.705032] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "What is the electrical conductivity for Lorentz number 87.781, voltage difference 13.2084, thermal conductivity 246.329 and temperature difference 20.3966?", + "a": "2527.9674044128305", + "valid": true, + "kind": "compute", + "expect": 2527.9674044128305, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [87.781013, 13.208419, 246.329053, 20.396558] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "Tell me the electrical conductivity with Lorentz number of 87.781, voltage difference of 13.2084, thermal conductivity of 246.329 and temperature difference of 20.3966.", + "a": "2527.9674044128305", + "valid": true, + "kind": "compute", + "expect": 2527.9674044128305, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [87.781013, 13.208419, 246.329053, 20.396558] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "What is the electrical conductivity for Lorentz number 156.502, voltage difference 185.886, thermal conductivity 13.2373 and temperature difference 4.71941?", + "a": "0.2829519235627792", + "valid": true, + "kind": "compute", + "expect": 0.2829519235627792, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [156.50181, 185.886273, 13.237311, 4.719415] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "Tell me the electrical conductivity with Lorentz number of 156.502, voltage difference of 185.886, thermal conductivity of 13.2373 and temperature difference of 4.71941.", + "a": "0.2829519235627792", + "valid": true, + "kind": "compute", + "expect": 0.2829519235627792, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [156.50181, 185.886273, 13.237311, 4.719415] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "What is the electrical conductivity for Lorentz number 24.5721, voltage difference 108.451, thermal conductivity 1.69476 and temperature difference 49.4843?", + "a": "0.17520685570541122", + "valid": true, + "kind": "compute", + "expect": 0.17520685570541122, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [24.572086, 108.450792, 1.694755, 49.484326] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "Tell me the electrical conductivity with Lorentz number of 24.5721, voltage difference of 108.451, thermal conductivity of 1.69476 and temperature difference of 49.4843.", + "a": "0.17520685570541122", + "valid": true, + "kind": "compute", + "expect": 0.17520685570541122, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [24.572086, 108.450792, 1.694755, 49.484326] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "What is the electrical conductivity for Lorentz number 107.961, voltage difference 59.4105 and thermal conductivity 5.77717?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the temperature difference is not given" + }, + { + "q": "Tell me the electrical conductivity with Lorentz number of 2.69428, voltage difference of 48.921 and thermal conductivity of 157.223.", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the temperature difference is not given" + }, + { + "q": "What is the electrical conductivity 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 electrical conductivity 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 electrical conductivity with Lorentz number of -0.318734, voltage difference of 9.56607, thermal conductivity of 126.709 and temperature difference of 1.70503.", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Lorentz number: no example had one" + }, + { + "q": "Which electrical conductivity 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 Lorentz number is 167.625, voltage difference is 10.3291, thermal conductivity is 2.21571 and the temperature difference is 58.5346, what is the electrical conductivity?", + "a": "203.77160508466267", + "valid": true, + "kind": "compute", + "expect": 203.77160508466267, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [167.624948, 10.329054, 2.215713, 58.534594] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "Could you work out the electrical conductivity given Lorentz number = 167.625, voltage difference = 10.3291, thermal conductivity = 2.21571, temperature difference = 58.5346?", + "a": "203.77160508466267", + "valid": true, + "kind": "compute", + "expect": 203.77160508466267, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [167.624948, 10.329054, 2.215713, 58.534594] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "I need the electrical conductivity for a Lorentz number of 8.53158, a voltage difference of 124.718, a thermal conductivity of 1.91195 and a temperature difference of 82.2401, please.", + "a": "0.08624425504972087", + "valid": true, + "kind": "compute", + "expect": 0.08624425504972087, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [8.531577, 124.718185, 1.911952, 82.240072] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "If the Lorentz number is 8.53158, voltage difference is 124.718, thermal conductivity is 1.91195 and the temperature difference is 82.2401, what is the electrical conductivity?", + "a": "0.08624425504972087", + "valid": true, + "kind": "compute", + "expect": 0.08624425504972087, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [8.531577, 124.718185, 1.911952, 82.240072] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "Could you work out the electrical conductivity given Lorentz number = 81.1967, voltage difference = 6.98086, thermal conductivity = 0.551236, temperature difference = 2.88512?", + "a": "2.649855847487048", + "valid": true, + "kind": "compute", + "expect": 2.649855847487048, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [81.196705, 6.980857, 0.551236, 2.88512] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "I need the electrical conductivity for a Lorentz number of 81.1967, a voltage difference of 6.98086, a thermal conductivity of 0.551236 and a temperature difference of 2.88512, please.", + "a": "2.649855847487048", + "valid": true, + "kind": "compute", + "expect": 2.649855847487048, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [81.196705, 6.980857, 0.551236, 2.88512] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "If the Lorentz number is 1.16997, voltage difference is 1.14865, thermal conductivity is 21.7931 and the temperature difference is 29.5106, what is the electrical conductivity?", + "a": "570.2883800843907", + "valid": true, + "kind": "compute", + "expect": 570.2883800843907, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [1.169968, 1.148652, 21.793126, 29.510609] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "Could you work out the electrical conductivity given Lorentz number = 1.16997, voltage difference = 1.14865, thermal conductivity = 21.7931, temperature difference = 29.5106?", + "a": "570.2883800843907", + "valid": true, + "kind": "compute", + "expect": 570.2883800843907, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [1.169968, 1.148652, 21.793126, 29.510609] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "I need the electrical conductivity for a Lorentz number of 223.509, a voltage difference of 17.3414, a thermal conductivity of 7.10705 and a temperature difference of 30.2507, please.", + "a": "159.79015240870933", + "valid": true, + "kind": "compute", + "expect": 159.79015240870933, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [223.508796, 17.341435, 7.107049, 30.250749] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "If the Lorentz number is 223.509, voltage difference is 17.3414, thermal conductivity is 7.10705 and the temperature difference is 30.2507, what is the electrical conductivity?", + "a": "159.79015240870933", + "valid": true, + "kind": "compute", + "expect": 159.79015240870933, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [223.508796, 17.341435, 7.107049, 30.250749] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "Could you work out the electrical conductivity given Lorentz number = 0.869111, voltage difference = 39.1753, thermal conductivity = 0.432217, temperature difference = 16.6571?", + "a": "0.004077105406732365", + "valid": true, + "kind": "compute", + "expect": 0.004077105406732365, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [0.869111, 39.175263, 0.432217, 16.657051] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "I need the electrical conductivity for a Lorentz number of 0.869111, a voltage difference of 39.1753, a thermal conductivity of 0.432217 and a temperature difference of 16.6571, please.", + "a": "0.004077105406732365", + "valid": true, + "kind": "compute", + "expect": 0.004077105406732365, + "dtype": "quantity", + "unit": "s^3*A^2/m^3/kg", + "why": "electrical conductivity for [0.869111, 39.175263, 0.432217, 16.657051] (the system Lorentz number (Wikidata Q104901522))" + }, + { + "q": "If the Lorentz number is 167.625, voltage difference is 10.3291 and the thermal conductivity is 2.21571, what is the electrical conductivity?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the temperature difference is not given" + }, + { + "q": "Could you work out the electrical conductivity given Lorentz number = 8.53158, voltage difference = 124.718, thermal conductivity = 1.91195?", + "a": "", + "valid": false, + "kind": "missing_input", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "the temperature difference is not given" + }, + { + "q": "Do you know the electrical conductivity 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 electrical conductivity 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 electrical conductivity given Lorentz number = -81.1967, voltage difference = 6.98086, thermal conductivity = 0.551236, temperature difference = 2.88512?", + "a": "", + "valid": false, + "kind": "out_of_domain", + "expect": null, + "dtype": "quantity", + "unit": "", + "why": "a negative Lorentz number: no example had one" + }, + { + "q": "What is a typical electrical conductivity?", + "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-electrical-conductivity/program.py b/root/organs10/physics/specialists/solved-electrical-conductivity/program.py new file mode 100644 index 0000000000000000000000000000000000000000..b069f183814a8677537876aa1a2a15123b98c0cd --- /dev/null +++ b/root/organs10/physics/specialists/solved-electrical-conductivity/program.py @@ -0,0 +1,7 @@ +"""electrical conductivity from Lorentz number, voltage difference, thermal conductivity, temperature difference (Lorentz number (Wikidata Q104901522)) +The maths organ (systems.py): solved from the system q0 = ((q1*( q2 ) **(2))/(q3*q4)); checked by substitution into every equation and by a numeric solution at 12 points""" +import math + + +def compute(lorentz_number, voltage_difference, thermal_conductivity, temperature_difference): + return 1.0*lorentz_number*temperature_difference*thermal_conductivity/voltage_difference**2 diff --git a/root/organs10/physics/specialists/solved-electrical-conductivity/scorer_formula_v1.py b/root/organs10/physics/specialists/solved-electrical-conductivity/scorer_formula_v1.py new file mode 100644 index 0000000000000000000000000000000000000000..8f4680b0c332b5a679436933c30780b243a4c9f1 --- /dev/null +++ b/root/organs10/physics/specialists/solved-electrical-conductivity/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"(?