#!/usr/bin/env python3 """knowledge_composer_v1 - Oracle7 4.5: questions that are small programs over the relation store, answered by COMPOSING routes the Reach router already answers under admission (library learning for knowledge: no new lookup of its own). Each route below is examined on Mintaka TRAIN questions (gold answers) before use, and runs only when its parts all answer. HOP "Where was the singer of Genie in a Bottle born?": the inner phrase "the singer of Genie in a Bottle" is asked on its own ("What is the singer of Genie in a Bottle?"); its answer must be ONE named thing; that thing is pinned as the subject of the outer question ("Where was born?"), which must answer too. Both receipts are kept. ORDER "Which series is older, Metroid or Super Mario Bros?": two named things, one date relation both hold (birth for people; publication, inception, start or first performance otherwise), compared. Dates only: the store keeps quantities without units, so sizes are not compared. use: answer(question) -> {"answer", "route", "steps": [...]} or None""" import os import re import reach_router_v1 as R L = R.L INNER = re.compile(r"\bthe\s+(?P[a-z][a-z\- ]{1,40}?)\s+of\s+(?P(?:the\s+)?[A-Z0-9][\w'.:\-]*(?:\s+(?:[A-Z0-9][\w'.:\-]*|of|the|and|in|on|a|an|de|la|le|du|von|van))*)") # only the plain comparison: "Which/who is older, A or B?", "Which came first, A or B?" - no other event between ORDER = re.compile(r"^\s*(?:which|who|what)(?:\s+(?!died|was\s+born|elected|assumed|won|became)[a-z\-]+){0,2}?\s+" r"(?:is|was|are|were|came\s+out|came|come\s+out|appeared|released)\s+" r"(?Polder|newer|earlier|later|first|younger|more\s+recent)\s*,\s*" r"(?P.+?)\s+or\s+(?P.+?)\s*\?\s*$", re.I) BIRTH_ORDER = re.compile(r"^\s*(?:who|which\s+(?:person|individual))\s+was\s+born\s+" r"(?Pearlier|first|later|last)\s*,\s*" r"(?P.+?)\s+or\s+(?P.+?)\s*\?\s*$", re.I) PERSON_DATE = (569,) # date of birth THING_DATES = (577, 571, 580, 1191, 1619, 606, 729, 619) # publication, inception, start, first performance, opening, # first flight, service entry, launch EARLY = {"older", "earlier", "first", "the oldest"} def _one_item(a): raw = a.get("raw") or [] return raw[0] if len(raw) == 1 and isinstance(raw[0], int) else None def hop(q): for m in INNER.finditer(q): x, y = m.group("x"), m.group("y").rstrip(" ?.,") inner = R.answer("What is the %s of %s?" % (x, y)) if not inner: continue v = _one_item(inner) if v is None: continue name = R.label(v) if not name: continue outer_q = q[:m.start()] + name + q[m.start() + len("the %s of %s" % (x, y)):] outer = R.answer(outer_q, pinned=(name, v)) if not outer: continue return {"answer": outer["answer"], "route": "hop", "values": outer["values"], "steps": [dict(inner, question="What is the %s of %s?" % (x, y)), dict(outer, question=outer_q)]} return None def _date(qid): """Read every bounded stored date; unknown precision never proves an ordering.""" db = R._load()["db"] cls = R.classes(qid) for p in (PERSON_DATE if R.HUMAN in cls else THING_DATES): rows = [lit for (lit,) in db.execute("SELECT lit FROM fact INDEXED BY fact_sp WHERE s=? AND p=? LIMIT 101", (qid, p)) if lit] if rows: if len(rows) > 100: return None, None, None bounds = [R.DATE.interval(lit, qid, p) for lit in rows] if any(b is None for b in bounds) or len({b[0] for b in bounds}) != 1: return None, None, None envelope = bounds[0][0], min(b[1] for b in bounds), max(b[2] for b in bounds) return p, rows, envelope return None, None, None def _exact(text): sp = L.link(text, 3)[:1] if not sp or R._bare(sp[0][0]) != R._bare(text): return None cands = sp[0][1] if len(cands) > 1 and cands[0][1] < R.XR.DOMINANCE * max(1, cands[1][1]): return None # an ambiguous name: no comparison return cands[0][0] def order(q): birth = BIRTH_ORDER.match(q) m = birth or ORDER.match(q) if not m: return None a, b = _exact(m.group("a")), _exact(m.group("b")) if not a or not b or a == b: return None pa, da, ia = _date(a) pb, db_, ib = _date(b) if not pa or pa != pb or (birth and pa != 569) or ia[0] != ib[0]: return None # no calendar conversion is assumed if ia[2] < ib[1]: earlier, later = a, b elif ib[2] < ia[1]: earlier, later = b, a else: return None # overlapping uncertainty does not establish an order adj = m.group("adj").lower() pick = earlier if adj in EARLY else later plab = R._load()["plabel"].get(pa, "P%d" % pa) show_a = " / ".join(R.XR._show(v, "date", a, pa) for v in da) show_b = " / ".join(R.XR._show(v, "date", b, pb) for v in db_) return {"answer": R.label(pick), "route": "order", "values": [R.label(pick)], "steps": [{"subject_qid": a, "property": pa, "value": da[0] if len(da) == 1 else None, "values": da, "interval": ia, "display": show_a}, {"subject_qid": b, "property": pb, "value": db_[0] if len(db_) == 1 else None, "values": db_, "interval": ib, "display": show_b}], "receipt": "%s (Q%d) - %s (P%d) - %s; %s (Q%d) - %s (P%d) - %s" % (R.label(a), a, plab, pa, show_a, R.label(b), b, plab, pb, show_b)} HOP_ON = os.environ.get("KC_HOP") == "1" # not admitted (2026-10-21: 9 firings on Mintaka train): off def answer(q): q = str(q).strip() return order(q) or (hop(q) if HOP_ON else None) if __name__ == "__main__": import sys for q in sys.argv[1:]: print(q, "->", answer(q))