Spaces:
Sleeping
Sleeping
# κ°μ μν, κ°μ μ λ°μ΄νΈ, decay μ²λ¦¬ | |
# portfolio/npc_social_network/npc/npc_emotion.py | |
from .emotion_config import EMOTION_LIST, EMOTION_DECAY_RATE, EMOTION_CATEGORY_MAP | |
class EmotionManager: | |
def __init__(self, decay_rate, personality=None, initial_baseline=None): | |
# λ΄λΆ: float κ°μ μμΉ κ΄λ¦¬μ© λ²νΌ | |
self._emotion_buffer = {emo: 0.0 for emo in EMOTION_LIST} | |
# baseline λ²νΌ μΆκ° | |
self._baseline_buffer = initial_baseline or {emo: 0.0 for emo in EMOTION_LIST} | |
# κ°μ λ³ ν볡 μλ (ms λ¨μκ° μλ λ¨μλΉ κ°μ μλ) | |
self.decay_rate = decay_rate | |
self.personality = personality or {} | |
def update_emotion(self, emotion: str, strength: float = 1.0, context: str = "general"): | |
""" | |
νΉμ κ°μ μ κ°μ μνμ λ°μ (μΉ΄ν κ³ λ¦¬ κΈ°λ°) | |
""" | |
if emotion not in self._emotion_buffer: | |
print(f"[κ²½κ³ ] '{emotion}'μ(λ) μ μλ κ°μ μ΄ μλλλ€.") | |
return | |
# κ°μ± κ³μ λ°μ | |
multiplier = self.personality.get("sensitive", 1.0) | |
category = EMOTION_CATEGORY_MAP.get(emotion, None) | |
if category == "core": | |
multiplier *= self.personality.get("affect_bias", 1.0) | |
elif category == "social": | |
# μ¬νμ κ°μ μ social contextμμλ§ λ°μ | |
if context != "social": | |
return # 무μ | |
multiplier *= self.personality.get("social_bias", 1.0) | |
elif category == "cognitive": | |
multiplier *= self.personality.get("cognitive_bias", 1.0) | |
elif category == "complex": | |
multiplier *= self.personality.get("complex_bias", 1.0) | |
# κ°μ λ°μ | |
self._emotion_buffer[emotion] += strength * multiplier | |
# clip μ μ© (μ΅λ 100) | |
self._emotion_buffer[emotion] = min(max(0.0, self._emotion_buffer[emotion]), 100.0) | |
def decay_emotions(self): | |
""" | |
μκ°μ΄ μ§λ¨μ λ°λΌ λͺ¨λ κ°μ μ΄ μμν κ°μ | |
""" | |
for emotion in EMOTION_LIST: | |
rate = self.decay_rate.get(emotion, 0.5) | |
modifier = 1.0 - self.personality.get("stoic", 0.0) | |
self._emotion_buffer[emotion] = max(0.0, self._emotion_buffer[emotion] - rate * modifier) | |
def get_state(self): | |
""" | |
κ°μ μν λ°ν (μΆλ ₯μ©, μ μλ‘ λ³ν) | |
""" | |
return {emo: int(self._emotion_buffer[emo]) for emo in EMOTION_LIST} | |
def get_dominant_emotion(self, layer: str = None): | |
""" | |
νμ¬ κ°μ₯ κ°ν κ°μ λ°ν (μ 체 κ°μ μ€ μ΅λκ° 1κ°) | |
""" | |
if layer: | |
# νΉμ layer λμ | |
layer_emotions = [emo for emo, cat in EMOTION_CATEGORY_MAP.items() if cat == layer] | |
if not layer_emotions: | |
return None | |
dominant_emo, score = max( | |
((emo, self._emotion_buffer[emo]) for emo in layer_emotions), | |
key=lambda x: x[1], | |
default=(None, 0.0) | |
) | |
if score == 0.0: | |
return None | |
return dominant_emo | |
else: | |
# μ 체 λμ | |
dominant_emo, score = max(self._emotion_buffer.items(), key=lambda x:x[1]) | |
if score == 0.0: | |
return None | |
return dominant_emo | |
def get_buffer(self): | |
""" | |
emotion_buffer 볡μ¬λ³Έ λ°ν | |
""" | |
return self._emotion_buffer.copy() | |
def get_current_emotions_state(self): | |
""" | |
νμ¬ κ°μ μν λ°ν (dict) | |
""" | |
return self._emotion_buffer | |
def get_top_emotions(self, top_n=5): | |
""" | |
νμ¬ κ°μ λ²νΌμμ μμ Nκ° κ°μ λ°ν | |
:return: List of (emotion_name, value) tuples | |
""" | |
# μ λ ¬ ν μμ Nκ° λ°ν | |
sorted_emotions = sorted(self._emotion_buffer.items(), key=lambda x: x[1], reverse=True) | |
top_emotions = sorted_emotions[:top_n] | |
return top_emotions | |
def set_baseline(self, baseline_buffer): | |
""" | |
baseline bufferλ₯Ό μΈλΆμμ μ€μ (npc_base.py μ΄κΈ°ν μ μ¬μ©) | |
""" | |
self._baseline_buffer = baseline_buffer.copy() | |