File size: 6,878 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# generator/templates_context_advanced.py
import random
from typing import List, Dict, Any
from .meaning_model import StructuredMeaning, Emotion, Context
from .encoder import encode_term
def generate_advanced_context_samples(
dictionaries: Dict[str, Any],
count: int,
source_language: str = "en",
) -> List[Dict[str, Any]]:
"""
Generates advanced multi-context samples:
- time + place + activity + sensory + social
- optional emotion overlay
- multi-clause natural language
"""
actions = dictionaries.get("actions", [])
objects = dictionaries.get("objects", [])
actors = dictionaries.get("actors", [])
times = dictionaries.get("context_time", [])
places = dictionaries.get("context_place", [])
activities = dictionaries.get("context_activity", [])
sensory = dictionaries.get("context_sensory", [])
social = dictionaries.get("context_social", [])
emotions = dictionaries.get("emotions", [])
results = []
if not actions or not objects or not actors:
raise ValueError("Missing required dictionaries: actions, objects, actors.")
for _ in range(count):
action = random.choice(actions)
obj = random.choice(objects)
actor = random.choice(actors)
time_ctx = random.choice(times) if times else None
place_ctx = random.choice(places) if places else None
activity_ctx = random.choice(activities) if activities else None
sensory_ctx = random.choice(sensory) if sensory else None
social_ctx = random.choice(social) if social else None
emotion_ctx = random.choice(emotions) if emotions else None
action_id = action.get("id", "action.unknown")
object_id = obj.get("id", "object.unknown")
actor_id = actor.get("id", "actor.unknown")
time_id = time_ctx.get("id") if time_ctx else None
place_id = place_ctx.get("id") if place_ctx else None
activity_id = activity_ctx.get("id") if activity_ctx else None
sensory_id = sensory_ctx.get("id") if sensory_ctx else None
social_id = social_ctx.get("id") if social_ctx else None
emotion_id = emotion_ctx.get("id") if emotion_ctx else None
intensity = round(random.uniform(0.3, 0.9), 2)
clauses = []
clause_main = f"{actor_id} {action_id} the {object_id}"
clauses.append(clause_main)
if place_id:
clauses.append(f"in the {place_id}")
if activity_id:
clauses.append(f"while {activity_id}")
if sensory_id:
clauses.append(f"as the environment felt {sensory_id}")
if social_id:
clauses.append(f"around {social_id}")
if time_id:
clauses.append(f"({time_id})")
if emotion_id:
clauses.append(f"and felt {emotion_id} ({intensity})")
input_text = " ".join(clauses) + "."
meaning = StructuredMeaning(
actor=actor_id,
action=action_id,
object=object_id,
modifiers=[],
emotion=Emotion(type=emotion_id, intensity=intensity) if emotion_id else None,
context=Context(
time=time_id,
place=place_id,
activity=activity_id,
sensory=sensory_id,
social=social_id,
),
intent="intent.describe_event",
meta={
"source_language": source_language,
"confidence": 0.95,
},
)
actor_enc = encode_term(actor_id, dictionaries)
action_enc = encode_term(action_id, dictionaries)
object_enc = encode_term(object_id, dictionaries)
time_enc = encode_term(time_id, dictionaries) if time_id else None
place_enc = encode_term(place_id, dictionaries) if place_id else None
activity_enc = encode_term(activity_id, dictionaries) if activity_id else None
sensory_enc = encode_term(sensory_id, dictionaries) if sensory_id else None
social_enc = encode_term(social_id, dictionaries) if social_id else None
emotion_enc = encode_term(emotion_id, dictionaries) if emotion_id else None
glyphic_human = (
f"ACTOR[{actor_enc['human']}] "
f"ACTION[{action_enc['human']}] "
f"OBJECT[{object_enc['human']}]"
)
if time_enc:
glyphic_human += f" CONTEXT.TIME[{time_enc['human']}]"
if place_enc:
glyphic_human += f" CONTEXT.PLACE[{place_enc['human']}]"
if activity_enc:
glyphic_human += f" CONTEXT.ACTIVITY[{activity_enc['human']}]"
if sensory_enc:
glyphic_human += f" CONTEXT.SENSORY[{sensory_enc['human']}]"
if social_enc:
glyphic_human += f" CONTEXT.SOCIAL[{social_enc['human']}]"
if emotion_enc:
glyphic_human += f" EMOTION[{emotion_enc['human']}:{intensity}]"
glyphic_human += " INTENT.describe"
glyphic_compact = (
f"ACT{{{actor_enc['compact']}}} "
f"ACTN{{{action_enc['compact']}}} "
f"OBJ{{{object_enc['compact']}}}"
)
if time_enc:
glyphic_compact += f" CTX{{TIME:{time_enc['compact']}}}"
if place_enc:
glyphic_compact += f" CTX{{PLACE:{place_enc['compact']}}}"
if activity_enc:
glyphic_compact += f" CTX{{ACT:{activity_enc['compact']}}}"
if sensory_enc:
glyphic_compact += f" CTX{{SENSE:{sensory_enc['compact']}}}"
if social_enc:
glyphic_compact += f" CTX{{SOC:{social_enc['compact']}}}"
if emotion_enc:
glyphic_compact += f" EMO{{{emotion_enc['compact']}@{intensity}}}"
glyphic_compact += " INT{DESC}"
glyphic_tokens = (
f"{actor_enc['tokens']} "
f"{action_enc['tokens']} "
f"{object_enc['tokens']}"
)
if time_enc:
glyphic_tokens += f" <CTX:TIME:{time_enc['compact']}>"
if place_enc:
glyphic_tokens += f" <CTX:PLACE:{place_enc['compact']}>"
if activity_enc:
glyphic_tokens += f" <CTX:ACT:{activity_enc['compact']}>"
if sensory_enc:
glyphic_tokens += f" <CTX:SENSE:{sensory_enc['compact']}>"
if social_enc:
glyphic_tokens += f" <CTX:SOC:{social_enc['compact']}>"
if emotion_enc:
glyphic_tokens += f" <EMO:{emotion_enc['compact']}:{intensity}>"
glyphic_tokens += " <INT:DESC>"
results.append(
{
"input_text": input_text,
"glyphic_output_human": glyphic_human,
"glyphic_output_compact": glyphic_compact,
"glyphic_output_tokens": glyphic_tokens,
"structured_meaning": meaning.to_dict(),
}
)
return results
|