File size: 1,629 Bytes
ed6bec6 |
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 60 |
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] = []
# 1. Actor
actor = structured.get("actor")
if actor and actor.get("id"):
g = _glyph_from_id(actor["id"])
if g:
glyphs.append(g)
# 2. Action
action = structured.get("action")
if action and action.get("id"):
g = _glyph_from_id(action["id"])
if g:
glyphs.append(g)
# 3. Object
obj = structured.get("object")
if obj and obj.get("id"):
g = _glyph_from_id(obj["id"])
if g:
glyphs.append(g)
# 4. Modifiers
for m in structured.get("modifiers", []):
if m.get("id"):
g = _glyph_from_id(m["id"])
if g:
glyphs.append(g)
# 5. Context (strict order)
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)
|