#!/usr/bin/env python3 """SQUIRCLE ROUTER v5 (round 6): THE LANGUAGE LAYER. The kernel's mechanism (parts, one shared typed pool, attention = the fit between what a part takes and what the pool holds, depth = rounds of composition) applied to words, as Ben set it on 2026-11-06 ("the transformer part ... applied to the language part will be the thing that unlocks it all"). Round 5's probe (1/60 good) named the class: v4 SELECTS among finished outputs, each of which alone had to carry 40% of the turn; the inverted check emptied the pool. v5 COMPOSES. READING LAYER the turn reader's frame -> typed terms in the pool: key (each frame key and each rare content word of the corrected turn, weighted by IDF over Oracle7's own records), topic, status, text-request, turn. PARTS (heads) each takes a term type and writes a term back: records key -> sentences of Oracle7's records that hold it (cited by file) define key -> its gloss in the head's own dictionary, for a word the records do not hold context key -> the earlier turn of THIS conversation that holds it (binds what it/this/that point at) essay topic + records -> a written piece (layer 2: only when layer 1 found records on the topic) + every v4 port (the head, the corrected-turn head, the ledger, commands that run or wait for go) A part's yield is counted per term type; a part that has yielded nothing on a type after 40 tries stops being asked for that type (its face, learned from its behaviour). ATTENTION which terms a part reads = the type fit; which outputs are kept = a greedy cover of the pool's key mass: each next piece is the one adding the most not-yet-covered IDF mass, up to 3 pieces, distinct sources. SPEAKING LAYER the chosen pieces said in the turn's act (question -> the answer; request -> what I hold, then what no operation of mine does yet; statement -> what it connects to). Then the INVERTED SQUIRCLE (v4) reads the whole composed reply back: it leaves only if it carries >= 40% of the frame's key mass. Nothing is written about any turn or any topic. usage: module (SquircleRouterV5(..., reader=Reader))""" from __future__ import annotations import collections import re from squircle_reply import content_words from squircle_reply_v4 import SquircleRouterV4, COVER, GENERIC MAX_PIECES = 3 MIN_GAIN = 2.5 # a further piece must add this much uncovered IDF mass RARE_Q = 3.0 # a turn word joins the pool's keys when its IDF over the records is >= this DEF_IDF = 6.0 # a word is defined from the dictionary when the records barely hold it FACE_TRIES = 40 class SquircleRouterV5(SquircleRouterV4): def __init__(self, *a, **k): super().__init__(*a, **k) self.tries = collections.Counter() self.yields = collections.Counter() # ---------------- faces learned from behaviour def _asked(self, part, typ): if self.tries[(part, typ)] >= FACE_TRIES and self.yields[(part, typ)] == 0: return False self.tries[(part, typ)] += 1 return True def _got(self, part, typ, ok): if ok: self.yields[(part, typ)] += 1 def faces(self): return {"%s<-%s" % k: [self.yields[k], n] for k, n in self.tries.items()} # ---------------- reading layer: frame -> typed terms def pool(self, turn, fr): idf = self.rec.idf q = {} for k in self.kw(fr): q[k] = idf.get(k, 6.0) for w in content_words(fr["fixed"] or turn): if w in GENERIC or self.reader.is_closed(w) or len(w) <= 2: continue if idf.get(w, 6.0) >= RARE_Q and w not in q: q[w] = idf.get(w, 6.0) return q # key -> weight # ---------------- parts def part_records(self, q): if not self._asked("records", "key"): return [] sc = collections.Counter() for w in q: for i in self.rec.inv.get(w, ())[:4000]: sc[i] += q[w] out = [] for i, s in sc.most_common(60): sent, f, ws = self.rec.sents[i] cov = {w for w in q if w in ws} if len(cov) >= 2 or any(q[w] >= 5.0 for w in cov): out.append({"src": "records:" + f, "text": "%s (%s)" % (sent, f), "cov": cov, "sub": True}) self._got("records", "key", bool(out)) return out def part_define(self, q): out = [] for w in q: if self.rec.idf.get(w, 99.0) < DEF_IDF or not self._asked("define", "key"): continue g = None for pos, gs in self.reader.d.entries(w): if pos in ("noun", "verb", "adj", "name"): for x in gs: if not re.search(r"\b(?:spelling|plural|past|participle|form) of\b", x, re.I): g = x break if g: break self._got("define", "key", bool(g)) if g: out.append({"src": "dictionary:" + w, "text": "%s: %s" % (w, g.rstrip(".") + "."), "cov": {w}, "sub": True}) return out def part_context(self, session, q): if not self._asked("context", "key"): return [] for t in reversed(self.turns[session][:-1]): ws = set(content_words(t)) cov = {w for w in q if w in ws} if sum(q[w] for w in cov) >= 8.0 and len(cov) >= 2: self._got("context", "key", True) return [{"src": "context", "text": "You raised this earlier: \"%s\"" % t[:220], "cov": cov, "sub": False}] self._got("context", "key", False) return [] # ---------------- attention: greedy cover of the pool's key mass def cover(self, q, pieces): mass = sum(q.values()) or 1.0 chosen, covered, srcs = [], set(), set() while len(chosen) < MAX_PIECES: best, gain = None, MIN_GAIN for p in pieces: fam = p["src"].split(":")[0] + ":" + p["text"][:40] if fam in srcs: continue g = sum(q[w] for w in p["cov"] - covered) if g > gain: best, gain = p, g if not best: break chosen.append(best) covered |= best["cov"] srcs.add(best["src"].split(":")[0] + ":" + best["text"][:40]) return chosen, sum(q[w] for w in covered) / mass, covered # ---------------- speaking layer def speak(self, fr, chosen, covered): body = " ".join(p["text"] for p in chosen) about = ", ".join(sorted(covered, key=lambda w: -self.rec.idf.get(w, 6.0))[:4]) mood = fr["mood"] if mood == "question": return "From what I hold on %s: %s" % (about, body) if mood in ("request", "commitment"): hole = self.hole_text(fr) or "" hole = hole.replace("You asked me to", "What you asked for,").replace( "so I can't do it from here; it goes", "so it goes") if hole else "" return "What I hold on %s: %s%s" % (about, body, (" " + hole) if hole else "") return "That connects to what I hold on %s: %s" % (about, body) # ---------------- forward squircle with the language layer as one more port def candidates(self, turn, fr, session, cg, head): out = super().candidates(turn, fr, session, cg, head) q = self.pool(turn, fr) if not q: return out pieces = self.part_records(q) + self.part_define(q) + self.part_context(session, q) chosen, frac, covered = self.cover(q, pieces) if chosen and any(p["sub"] for p in chosen) and frac >= COVER: out.append(("inform", "composed%d" % len(chosen), self.speak(fr, chosen, covered), True, "")) # layer 2: a question asking how/why/what, or a request for text, with records on its topic -> the essay part tw = self.topic_words(fr) or [w for w in sorted(covered, key=lambda w: -q[w])[:3]] recs = [p for p in chosen if p["src"].startswith("records")] if (len(recs) >= 2 and fr["mood"] == "question" and fr.get("wh") in ("what", "how", "why") and self._asked("essay", "topic+records")): r = self.run_safe("essay", tw, set()) self._got("essay", "topic+records", bool(r)) if r: out.append(("commissive", "essay2", r, True, "")) return out