core/generate/semantic_templates.py
Shay c1e723f185 feat: integrate 3-core-language depth into PropositionGraph spine for bidirectional unification
- Add LexicalResolution dataclass + resolve_entry() in chat/pack_resolver.py
  that returns language, root, morphology_id, gloss, semantic_domains from
  he/grc/en packs (lru-cached, first-match, full depth support).

- Extend GraphNode (generate/graph_planner.py) with optional language/root/
  morphology_id fields (defaults preserve all call sites). Update as_dict()
  to include them conditionally. ground_graph() now propagates depth.

- Generalize enrichment in core/cognition/pipeline.py:
  - Per-subject resolution map using depth packs.
  - Enrich all matching nodes before ground (subject→node map).
  - Pass depth alongside recalled_words to ground_graph().

- Consume depth on articulation side:
  - realize_semantic() and render_semantic() now accept/use language+root
    for etymological/Logos framing on Hebrew/Greek nodes (e.g. "אמת (Hebrew
    root: א-מ-ן) is defined as..."). English unchanged.

- Enrich oov_geometric_context with node_depths for future geometric
  anti-unification using roots.

- Extend recognition/connector.py to forward depth from EpistemicNode
  paths into GraphNode.

- Add full Hebrew turn test under realizer_grounded_authority flag.
- Update related tests (semantic realizer, OOV context, surface resolution).
- Cleaned legacy type() hack immediately on discovery (hard-stop rule).

All targeted tests green (52+ in slices), broad relevant suite 581 passed.
Invariants preserved: versor only at owned boundaries, exact recall,
immutable updates, no new legacy parsers. 3 pillars upheld.

Work continues tomorrow from this checkpoint.
2026-07-06 09:01:43 -07:00

106 lines
3.6 KiB
Python

"""Intent-aware semantic templates for the realizer.
Maps (IntentTag, relation_predicate) pairs to deterministic surface
templates that use the seed pack's relation predicates (defines, means,
grounds, supports, contrasts_with, corrects).
Design constraints:
- No LLM fallback
- No random template selection
- Deterministic: same (intent, predicate, subject, object) -> same surface
- Uses seed pack vocabulary directly
"""
from __future__ import annotations
from generate.intent import IntentTag
_INTENT_TEMPLATES: dict[IntentTag, str] = {
IntentTag.DEFINITION: "{subject} is defined as {obj}",
IntentTag.CAUSE: "{subject} is grounded in {obj}",
IntentTag.PROCEDURE: "first, {obj}; then, {subject} follows",
IntentTag.COMPARISON: "{subject} and {secondary} are distinguished: {subject} {predicate_h} {secondary}",
IntentTag.CORRECTION: "correction: {subject} {predicate_h} {obj}",
IntentTag.RECALL: "recalling {subject}: {obj}",
IntentTag.VERIFICATION: "{subject} is verified: {obj}",
IntentTag.UNKNOWN: "{subject} {predicate_h} {obj}",
}
_PREDICATE_HUMANIZE: dict[str, str] = {
"is_defined_as": "is defined as",
"is_caused_by": "is caused by",
"has_steps": "has the following steps",
"contrasts_with": "contrasts with",
"corrects": "corrects",
"recalls": "recalls",
"is_verified_as": "is verified as",
"addresses": "addresses",
"defines": "defines",
"means": "means",
"grounds": "grounds",
"supports": "supports",
"causes": "causes",
"reveals": "reveals",
"precedes": "precedes",
"follows": "follows",
"belongs_to": "belongs to",
"answers": "answers",
"is_grounded_in": "is grounded in",
"is_distinguished_from": "is distinguished from",
"implies": "implies",
"entails": "entails",
"requires": "requires",
"verifies": "verifies",
"evidences": "evidences",
"orders": "orders",
}
def humanize_predicate(predicate: str) -> str:
return _PREDICATE_HUMANIZE.get(predicate, predicate.replace("_", " "))
def render_semantic(
intent: IntentTag,
subject: str,
predicate: str,
obj: str,
secondary: str | None = None,
language: str | None = None,
root: str | None = None,
) -> str:
"""Render a semantic surface from intent, subject, predicate, and object.
When language + root are supplied (from enriched PropositionGraph nodes
carrying 3-core-language depth), the surface incorporates etymological
precision for Hebrew (root density) and Koine Greek (Logos precision).
English base remains unchanged.
"""
template = _INTENT_TEMPLATES.get(intent, _INTENT_TEMPLATES[IntentTag.UNKNOWN])
predicate_h = humanize_predicate(predicate)
obj_display = obj if obj not in ("<pending>", "<prior>") else "..."
# Masterful 3-language depth framing on the articulation side.
# Depth travels with the shared GraphNode from resolve_entry grounding.
if language and root and language != "en":
if language == "he":
depth_note = f" (Hebrew root: {root})"
elif language in ("grc", "el"):
depth_note = f" (Koine Greek: {root})"
else:
depth_note = f" ({language} root: {root})"
# For definition-style intents, highlight the term itself.
# For others, qualify the object referent.
if intent in (IntentTag.DEFINITION, IntentTag.RECALL, IntentTag.VERIFICATION):
subject = f"{subject}{depth_note}"
else:
obj_display = f"{obj_display}{depth_note}"
return template.format(
subject=subject,
predicate_h=predicate_h,
obj=obj_display,
secondary=secondary or obj_display,
)