|
|
from typing import Dict, Any, List, Optional |
|
|
from .glyph_dictionary_loader import get_dictionary |
|
|
|
|
|
|
|
|
def _glyph_from_id(entry_id: str) -> Optional[str]: |
|
|
entry = get_dictionary().get_entry_by_id(entry_id) |
|
|
return entry.get("glyph") if entry else None |
|
|
|
|
|
|
|
|
def encode_meaning(structured: Dict[str, Any]) -> str: |
|
|
""" |
|
|
Strict encoder: |
|
|
- enforces canonical ordering |
|
|
- actor β action β object β modifiers β context |
|
|
- context ordered: place β time β emotion β sensory β social |
|
|
- guaranteed reversible with decode() |
|
|
""" |
|
|
glyphs: List[str] = [] |
|
|
|
|
|
|
|
|
actor = structured.get("actor") |
|
|
if actor and actor.get("id"): |
|
|
g = _glyph_from_id(actor["id"]) |
|
|
if g: |
|
|
glyphs.append(g) |
|
|
|
|
|
|
|
|
action = structured.get("action") |
|
|
if action and action.get("id"): |
|
|
g = _glyph_from_id(action["id"]) |
|
|
if g: |
|
|
glyphs.append(g) |
|
|
|
|
|
|
|
|
obj = structured.get("object") |
|
|
if obj and obj.get("id"): |
|
|
g = _glyph_from_id(obj["id"]) |
|
|
if g: |
|
|
glyphs.append(g) |
|
|
|
|
|
|
|
|
for m in structured.get("modifiers", []): |
|
|
if m.get("id"): |
|
|
g = _glyph_from_id(m["id"]) |
|
|
if g: |
|
|
glyphs.append(g) |
|
|
|
|
|
|
|
|
context = structured.get("context", {}) |
|
|
|
|
|
for ctx_key in ["place", "time", "emotion", "sensory", "social"]: |
|
|
for ctx in context.get(ctx_key, []): |
|
|
if ctx.get("id"): |
|
|
g = _glyph_from_id(ctx["id"]) |
|
|
if g: |
|
|
glyphs.append(g) |
|
|
|
|
|
return "".join(glyphs) |
|
|
|
|
|
|