File size: 3,481 Bytes
2419253 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | #!/usr/bin/env python3
"""The Oracle7 kernel, v9: v8's reader fixes (units with "/" read whole, never short; articles never goal units), WITHOUT
v8's IDF cue filter (it cost right answers on every sealed set), and three structural laws found by v8's OpenStax run.
Reading more questions surfaced more weak paths, the 09-30 law in action (dimensional analysis can't see hidden
factors: 1/2, 2, pi, geometry). So the law changes in structure, not by item:
1. AN IMPLICIT GOAL COMES FROM A PART THAT GIVES IT. When the question doesn't state the unit it wants ("What minimum
force...", "What is the coefficient of performance..."), the answer must be produced directly by a formula part or a
specialist face whose output is that quantity; core arithmetic may not finish the chain by combining leftover
givens. An explicit unit goal ("in kWh") keeps the arithmetic finish: the everyday unit-chain family where it has
held (every sealed set).
2. ONE QUESTION PER TURN. A turn that asks two things (two sentences ending in "?") is declined, not half-answered.
3. THE WANTED QUANTITY IS THE HEAD OF THE ASKING PHRASE: the first part-declared noun after "what is the / what / how
much", up to "of / for / when / if / that / ,", not any quantity noun elsewhere in the sentence.
usage: module (Kernel9, a drop-in for Kernel8)"""
from __future__ import annotations
import re
import kernel_v4
import kernel_v5
from kernel_v8 import Kernel8
reader = kernel_v5.reader
HEAD = re.compile(r"\b(?:what is the|what are the|what was the|what's the|find the|calculate the|determine the|what|how much)\s+"
r"(.+?)(?:\s+(?:of|for|when|if|that|which|in|at|on|to|does|do|is|was|must|would|will|can)\b|[,?.]|$)", re.I)
class Kernel9(Kernel8):
def _cues(self, f):
return kernel_v4.Kernel4._cues(self, f) # v8's IDF filter removed
def _wanted(self, text):
ask = self._asking(text)
m = HEAD.search(ask)
span = (m.group(1) if m else ask).lower()
for n in self.nouns: # longest first, inside the asking phrase's head only
if re.search(r"\b%s\b" % re.escape(n), span):
return n
return None
def solve(self, text, session="kernel"):
t = reader.normalise(text)
if len([s for s in re.split(r"(?<=[.?!])\s+", t.strip()) if s.strip().endswith("?")]) > 1:
h = self.head_chat(text, session)
if h.get("status") == "answered":
return {"status": "answered", "answer": h.get("response"), "reading": "head", "head": h,
"path": [str(h.get("path"))]}
return {"status": "declined", "reason": "two questions in one turn: ask them one at a time",
"check": "one question per turn"}
explicit = reader.read_goal(self._asking(t)) is not None
r = super().solve(text, session)
if r.get("status") == "answered" and r.get("reading") not in ("declared", "head") and not explicit:
tr = r.get("trace", [])
last = tr[-1] if tr else {}
if last.get("node") == "core_math" and len([x for x in str(last.get("formula", "")).split("×") if x.strip()]) > 1:
return {"status": "declined", "reason": "an implicit goal must come from a part that gives it, not from "
"arithmetic over leftover givens", "check": "implicit goal from a part"}
return r
|