rorshi commited on
Commit
ff9aa0c
·
1 Parent(s): 65ed704

복합 감정 계산 및 상태 요약

Browse files
Files changed (2) hide show
  1. npc_social_network/npc/npc_base.py +16 -2
  2. test.ipynb +25 -14
npc_social_network/npc/npc_base.py CHANGED
@@ -127,11 +127,11 @@ class NPC:
127
  return f"{emotion_line} {job_line}"
128
 
129
  # NPC가 새로운 기억을 저장하고 감정 상태에 반영
130
- def remember(self, content: str, importance: int = 5, emotion: str = None):
131
  memory = Memory(content=content, importance = importance, emotion=emotion or "neutral")
132
  self.memory_store.add_memory(memory)
133
  if emotion:
134
- self.update_emotion(emotion)
135
 
136
  # 저장된 모든 기억(단기+장기)을 리스트로 반환
137
  def recall(self):
@@ -172,3 +172,17 @@ class NPC:
172
  max_value = val
173
  max_emotion = emo
174
  return max_emotion
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  return f"{emotion_line} {job_line}"
128
 
129
  # NPC가 새로운 기억을 저장하고 감정 상태에 반영
130
+ def remember(self, content: str, importance: int = 5, emotion: str = None, strength: float = 1.0):
131
  memory = Memory(content=content, importance = importance, emotion=emotion or "neutral")
132
  self.memory_store.add_memory(memory)
133
  if emotion:
134
+ self.update_emotion(emotion, strength) # strength = importance / 5.0으로도 가능
135
 
136
  # 저장된 모든 기억(단기+장기)을 리스트로 반환
137
  def recall(self):
 
172
  max_value = val
173
  max_emotion = emo
174
  return max_emotion
175
+
176
+ # 복합 감정 계산 (상위 N개 감정 반환)
177
+ def get_composite_emotion_state(self, top_n: int = 3):
178
+ sorted_emotions = sorted(self._emotion_buffer.items(), key=lambda x: x[1], reverse=True)
179
+ composite = [(emo,round(score, 2)) for emo, score in sorted_emotions[:top_n] if score > 0]
180
+ return composite # 예 [('fear', 2.0), ('anger'), 1.5]
181
+
182
+ # 감정 상태 요약 텍스트 생성
183
+ def summarize_emotional_state(self):
184
+ nonzero = [v for v in self._emotion_buffer.values() if v > 0]
185
+ avg_strength = round(sum(nonzero) / len(nonzero), 2) if nonzero else 0.0
186
+ composite = self.get_composite_emotion_state()
187
+ composite_str = ", ".join(f"{emo}({val})" for emo, val in composite)
188
+ return f"감정 평균 강도: {avg_strength} / 대표 감정: {composite_str if composite else '없음' }"
test.ipynb CHANGED
@@ -2754,7 +2754,7 @@
2754
  },
2755
  {
2756
  "cell_type": "code",
2757
- "execution_count": 1,
2758
  "id": "ac67ce84",
2759
  "metadata": {},
2760
  "outputs": [
@@ -2762,26 +2762,34 @@
2762
  "name": "stdout",
2763
  "output_type": "stream",
2764
  "text": [
2765
- "[DEBUG] update_emotion: Attempting to update 'anger' by 1.0.\n",
2766
- "[DEBUG] update_emotion: Attempting to update 'fear' by 1.0.\n",
2767
- "[DEBUG] update_emotion: Attempting to update 'pride' by 1.0.\n",
2768
  "초기 감정 상태:\n",
2769
- "{'core_positive': {'joy': 0.0, 'satisfaction': 0.0, 'gratitude': 0.0, 'calm': 0.0, 'anticipation': 0.0, 'pride': 1.0, 'connectedness': 0.0}, 'core_negative': {'sadness': 0.0, 'anger': 1.0, 'anxiety': 0.0, 'disgust': 0.0, 'fear': 1.0, 'regret': 0.0, 'frustration': 0.0}, 'social_emotion': {'jealousy': 0.0, 'shame': 0.0, 'guilt': 0.0, 'compassion': 0.0, 'awe': 0.0, 'empathy': 0.0}, 'cognitive_state': {'confusion': 0.0, 'nervousness': 0.0, 'apathy': 0.0, 'excitement': 0.0, 'immersion': 0.0, 'skepticism': 0.0}}\n",
 
2770
  "\n",
2771
  "[1회 decay 후]\n",
2772
- "{'core_positive': {'joy': 0.0, 'satisfaction': 0.0, 'gratitude': 0.0, 'calm': 0.0, 'anticipation': 0.0, 'pride': 0.0, 'connectedness': 0.0}, 'core_negative': {'sadness': 0.0, 'anger': 0.5, 'anxiety': 0.0, 'disgust': 0.0, 'fear': 0.4, 'regret': 0.0, 'frustration': 0.0}, 'social_emotion': {'jealousy': 0.0, 'shame': 0.0, 'guilt': 0.0, 'compassion': 0.0, 'awe': 0.0, 'empathy': 0.0}, 'cognitive_state': {'confusion': 0.0, 'nervousness': 0.0, 'apathy': 0.0, 'excitement': 0.0, 'immersion': 0.0, 'skepticism': 0.0}}\n",
 
 
2773
  "\n",
2774
  "[2회 decay 후]\n",
2775
- "{'core_positive': {'joy': 0.0, 'satisfaction': 0.0, 'gratitude': 0.0, 'calm': 0.0, 'anticipation': 0.0, 'pride': 0.0, 'connectedness': 0.0}, 'core_negative': {'sadness': 0.0, 'anger': 0.0, 'anxiety': 0.0, 'disgust': 0.0, 'fear': 0.0, 'regret': 0.0, 'frustration': 0.0}, 'social_emotion': {'jealousy': 0.0, 'shame': 0.0, 'guilt': 0.0, 'compassion': 0.0, 'awe': 0.0, 'empathy': 0.0}, 'cognitive_state': {'confusion': 0.0, 'nervousness': 0.0, 'apathy': 0.0, 'excitement': 0.0, 'immersion': 0.0, 'skepticism': 0.0}}\n",
 
 
2776
  "\n",
2777
  "[3회 decay 후]\n",
2778
- "{'core_positive': {'joy': 0.0, 'satisfaction': 0.0, 'gratitude': 0.0, 'calm': 0.0, 'anticipation': 0.0, 'pride': 0.0, 'connectedness': 0.0}, 'core_negative': {'sadness': 0.0, 'anger': 0.0, 'anxiety': 0.0, 'disgust': 0.0, 'fear': 0.0, 'regret': 0.0, 'frustration': 0.0}, 'social_emotion': {'jealousy': 0.0, 'shame': 0.0, 'guilt': 0.0, 'compassion': 0.0, 'awe': 0.0, 'empathy': 0.0}, 'cognitive_state': {'confusion': 0.0, 'nervousness': 0.0, 'apathy': 0.0, 'excitement': 0.0, 'immersion': 0.0, 'skepticism': 0.0}}\n",
 
 
2779
  "\n",
2780
  "[4회 decay 후]\n",
2781
  "{'core_positive': {'joy': 0.0, 'satisfaction': 0.0, 'gratitude': 0.0, 'calm': 0.0, 'anticipation': 0.0, 'pride': 0.0, 'connectedness': 0.0}, 'core_negative': {'sadness': 0.0, 'anger': 0.0, 'anxiety': 0.0, 'disgust': 0.0, 'fear': 0.0, 'regret': 0.0, 'frustration': 0.0}, 'social_emotion': {'jealousy': 0.0, 'shame': 0.0, 'guilt': 0.0, 'compassion': 0.0, 'awe': 0.0, 'empathy': 0.0}, 'cognitive_state': {'confusion': 0.0, 'nervousness': 0.0, 'apathy': 0.0, 'excitement': 0.0, 'immersion': 0.0, 'skepticism': 0.0}}\n",
 
 
2782
  "\n",
2783
  "[5회 decay 후]\n",
2784
- "{'core_positive': {'joy': 0.0, 'satisfaction': 0.0, 'gratitude': 0.0, 'calm': 0.0, 'anticipation': 0.0, 'pride': 0.0, 'connectedness': 0.0}, 'core_negative': {'sadness': 0.0, 'anger': 0.0, 'anxiety': 0.0, 'disgust': 0.0, 'fear': 0.0, 'regret': 0.0, 'frustration': 0.0}, 'social_emotion': {'jealousy': 0.0, 'shame': 0.0, 'guilt': 0.0, 'compassion': 0.0, 'awe': 0.0, 'empathy': 0.0}, 'cognitive_state': {'confusion': 0.0, 'nervousness': 0.0, 'apathy': 0.0, 'excitement': 0.0, 'immersion': 0.0, 'skepticism': 0.0}}\n"
 
 
2785
  ]
2786
  }
2787
  ],
@@ -2795,18 +2803,21 @@
2795
  "\n",
2796
  "# 3. NPC 생성 및 감정 입력\n",
2797
  "npc = NPC(name=\"아린\", job=\"farmer\", path=[(0,0)], image=None)\n",
2798
- "npc.remember(\"플레이어가 모욕함\", importance=6, emotion=\"anger\")\n",
2799
- "npc.remember(\"무서운 소리를 들음\", importance=5, emotion=\"fear\")\n",
2800
- "npc.remember(\"친구가 칭찬함\", importance=5, emotion=\"pride\")\n",
2801
  "\n",
2802
  "print(\"초기 감정 상태:\")\n",
2803
- "print(npc.get_current_emotion_state())\n",
 
2804
  "\n",
2805
  "# 4. decay 반복 적용\n",
2806
  "for step in range(1, 6):\n",
2807
  " npc.decay_emotion()\n",
2808
  " print(f\"\\n[{step}회 decay 후]\")\n",
2809
- " print(npc.get_current_emotion_state())"
 
 
2810
  ]
2811
  },
2812
  {
 
2754
  },
2755
  {
2756
  "cell_type": "code",
2757
+ "execution_count": 2,
2758
  "id": "ac67ce84",
2759
  "metadata": {},
2760
  "outputs": [
 
2762
  "name": "stdout",
2763
  "output_type": "stream",
2764
  "text": [
 
 
 
2765
  "초기 감정 상태:\n",
2766
+ "[('fear', 2.2), ('anger', 1.5), ('pride', 1.2)]\n",
2767
+ "감정 평균 강도: 1.63 / 대표 감정: fear(2.2), anger(1.5), pride(1.2)\n",
2768
  "\n",
2769
  "[1회 decay 후]\n",
2770
+ "{'core_positive': {'joy': 0.0, 'satisfaction': 0.0, 'gratitude': 0.0, 'calm': 0.0, 'anticipation': 0.0, 'pride': 0.19999999999999996, 'connectedness': 0.0}, 'core_negative': {'sadness': 0.0, 'anger': 1.0, 'anxiety': 0.0, 'disgust': 0.0, 'fear': 1.6, 'regret': 0.0, 'frustration': 0.0}, 'social_emotion': {'jealousy': 0.0, 'shame': 0.0, 'guilt': 0.0, 'compassion': 0.0, 'awe': 0.0, 'empathy': 0.0}, 'cognitive_state': {'confusion': 0.0, 'nervousness': 0.0, 'apathy': 0.0, 'excitement': 0.0, 'immersion': 0.0, 'skepticism': 0.0}}\n",
2771
+ "[('fear', 1.6), ('anger', 1.0), ('pride', 0.2)]\n",
2772
+ "감정 평균 강도: 0.93 / 대표 감정: fear(1.6), anger(1.0), pride(0.2)\n",
2773
  "\n",
2774
  "[2회 decay 후]\n",
2775
+ "{'core_positive': {'joy': 0.0, 'satisfaction': 0.0, 'gratitude': 0.0, 'calm': 0.0, 'anticipation': 0.0, 'pride': 0.0, 'connectedness': 0.0}, 'core_negative': {'sadness': 0.0, 'anger': 0.5, 'anxiety': 0.0, 'disgust': 0.0, 'fear': 1.0, 'regret': 0.0, 'frustration': 0.0}, 'social_emotion': {'jealousy': 0.0, 'shame': 0.0, 'guilt': 0.0, 'compassion': 0.0, 'awe': 0.0, 'empathy': 0.0}, 'cognitive_state': {'confusion': 0.0, 'nervousness': 0.0, 'apathy': 0.0, 'excitement': 0.0, 'immersion': 0.0, 'skepticism': 0.0}}\n",
2776
+ "[('fear', 1.0), ('anger', 0.5)]\n",
2777
+ "감정 평균 강도: 0.75 / 대표 감정: fear(1.0), anger(0.5)\n",
2778
  "\n",
2779
  "[3회 decay 후]\n",
2780
+ "{'core_positive': {'joy': 0.0, 'satisfaction': 0.0, 'gratitude': 0.0, 'calm': 0.0, 'anticipation': 0.0, 'pride': 0.0, 'connectedness': 0.0}, 'core_negative': {'sadness': 0.0, 'anger': 0.0, 'anxiety': 0.0, 'disgust': 0.0, 'fear': 0.4, 'regret': 0.0, 'frustration': 0.0}, 'social_emotion': {'jealousy': 0.0, 'shame': 0.0, 'guilt': 0.0, 'compassion': 0.0, 'awe': 0.0, 'empathy': 0.0}, 'cognitive_state': {'confusion': 0.0, 'nervousness': 0.0, 'apathy': 0.0, 'excitement': 0.0, 'immersion': 0.0, 'skepticism': 0.0}}\n",
2781
+ "[('fear', 0.4)]\n",
2782
+ "감정 평균 강도: 0.4 / 대표 감정: fear(0.4)\n",
2783
  "\n",
2784
  "[4회 decay 후]\n",
2785
  "{'core_positive': {'joy': 0.0, 'satisfaction': 0.0, 'gratitude': 0.0, 'calm': 0.0, 'anticipation': 0.0, 'pride': 0.0, 'connectedness': 0.0}, 'core_negative': {'sadness': 0.0, 'anger': 0.0, 'anxiety': 0.0, 'disgust': 0.0, 'fear': 0.0, 'regret': 0.0, 'frustration': 0.0}, 'social_emotion': {'jealousy': 0.0, 'shame': 0.0, 'guilt': 0.0, 'compassion': 0.0, 'awe': 0.0, 'empathy': 0.0}, 'cognitive_state': {'confusion': 0.0, 'nervousness': 0.0, 'apathy': 0.0, 'excitement': 0.0, 'immersion': 0.0, 'skepticism': 0.0}}\n",
2786
+ "[]\n",
2787
+ "감정 평균 강도: 0.0 / 대표 감정: 없음\n",
2788
  "\n",
2789
  "[5회 decay 후]\n",
2790
+ "{'core_positive': {'joy': 0.0, 'satisfaction': 0.0, 'gratitude': 0.0, 'calm': 0.0, 'anticipation': 0.0, 'pride': 0.0, 'connectedness': 0.0}, 'core_negative': {'sadness': 0.0, 'anger': 0.0, 'anxiety': 0.0, 'disgust': 0.0, 'fear': 0.0, 'regret': 0.0, 'frustration': 0.0}, 'social_emotion': {'jealousy': 0.0, 'shame': 0.0, 'guilt': 0.0, 'compassion': 0.0, 'awe': 0.0, 'empathy': 0.0}, 'cognitive_state': {'confusion': 0.0, 'nervousness': 0.0, 'apathy': 0.0, 'excitement': 0.0, 'immersion': 0.0, 'skepticism': 0.0}}\n",
2791
+ "[]\n",
2792
+ "감정 평균 강도: 0.0 / 대표 감정: 없음\n"
2793
  ]
2794
  }
2795
  ],
 
2803
  "\n",
2804
  "# 3. NPC 생성 및 감정 입력\n",
2805
  "npc = NPC(name=\"아린\", job=\"farmer\", path=[(0,0)], image=None)\n",
2806
+ "npc.remember(\"플레이어가 모욕함\", importance=6, emotion=\"anger\", strength=1.5)\n",
2807
+ "npc.remember(\"무서운 소리를 들음\", importance=5, emotion=\"fear\", strength=2.2)\n",
2808
+ "npc.remember(\"친구가 칭찬함\", importance=5, emotion=\"pride\", strength=1.2)\n",
2809
  "\n",
2810
  "print(\"초기 감정 상태:\")\n",
2811
+ "print(npc.get_composite_emotion_state()) # [('fear', 2.0), ('anger', 1.5), ('pride', 1.2)]\n",
2812
+ "print(npc.summarize_emotional_state()) # 감정 평균 강도: 1.57 / 대표 감정: fear(2.0), anger(1.5), pride(1.2)\n",
2813
  "\n",
2814
  "# 4. decay 반복 적용\n",
2815
  "for step in range(1, 6):\n",
2816
  " npc.decay_emotion()\n",
2817
  " print(f\"\\n[{step}회 decay 후]\")\n",
2818
+ " print(npc.get_current_emotion_state())\n",
2819
+ " print(npc.get_composite_emotion_state())\n",
2820
+ " print(npc.summarize_emotional_state())"
2821
  ]
2822
  },
2823
  {