Spaces:
Sleeping
Sleeping
Create emotion_core.py
Browse files- emotion_core.py +73 -0
emotion_core.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# emotion_core.py
|
| 2 |
+
# Lightweight sentiment + arousal + trait inferences (no external deps)
|
| 3 |
+
|
| 4 |
+
import re
|
| 5 |
+
|
| 6 |
+
_POS = {
|
| 7 |
+
"love","like","enjoy","great","good","amazing","excited","confident","proud","win",
|
| 8 |
+
"focus","focused","progress","grow","learning","curious","optimistic","strong","ready",
|
| 9 |
+
"grateful","hyped","calm","peace","solid","clear","vision","hungry","ambitious"
|
| 10 |
+
}
|
| 11 |
+
_NEG = {
|
| 12 |
+
"hate","dislike","bad","terrible","awful","sad","angry","upset","lost","stuck",
|
| 13 |
+
"tired","worried","anxious","stress","stressed","fail","failure","confused","quit",
|
| 14 |
+
"overwhelmed","mad","frustrated"
|
| 15 |
+
}
|
| 16 |
+
_AROUSAL_UP = {
|
| 17 |
+
"hyped","excited","now","today","urgent","fast","go","run","grind","push","shipping",
|
| 18 |
+
"launch","win","attack","dominate","scale"
|
| 19 |
+
}
|
| 20 |
+
_AROUSAL_DOWN = {"calm","slow","reflect","breathe","steady","patient","peace","chill"}
|
| 21 |
+
|
| 22 |
+
TRAIT_MAP = {
|
| 23 |
+
"ambition": {"keywords": {"ambitious","billionaire","build","empire","dominate","scale","win","grind","goal"}, "pos": +2, "neg": -1},
|
| 24 |
+
"optimism": {"keywords": {"optimistic","confident","great","good","amazing","believe","can","possible"}, "pos": +2, "neg": -2},
|
| 25 |
+
"resilience": {"keywords": {"again","retry","keep","persist","bounce","comeback","despite","even if"}, "pos": +2, "neg": 0},
|
| 26 |
+
"focus": {"keywords": {"focus","focused","discipline","plan","system","consistent","daily"}, "pos": +2, "neg": -1},
|
| 27 |
+
"curiosity": {"keywords": {"curious","learn","why","how","explore","research","test"}, "pos": +2, "neg": 0},
|
| 28 |
+
"calm": {"keywords": {"calm","steady","patient","peace","collected"}, "pos": +2, "neg": -2},
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
def _tokens(text: str):
|
| 32 |
+
return set(re.findall(r"[a-zA-Z']+", text.lower()))
|
| 33 |
+
|
| 34 |
+
class EmotionAnalyzer:
|
| 35 |
+
def analyze(self, text: str):
|
| 36 |
+
toks = _tokens(text)
|
| 37 |
+
|
| 38 |
+
pos_hits = len(toks & _POS)
|
| 39 |
+
neg_hits = len(toks & _NEG)
|
| 40 |
+
raw = pos_hits - neg_hits
|
| 41 |
+
# sentiment in [-1,1]
|
| 42 |
+
sentiment = 0.0
|
| 43 |
+
if raw != 0:
|
| 44 |
+
sentiment = max(-1.0, min(1.0, raw / 5.0))
|
| 45 |
+
|
| 46 |
+
# arousal in [0,1]
|
| 47 |
+
arousal = 0.5
|
| 48 |
+
up = len(toks & _AROUSAL_UP)
|
| 49 |
+
down = len(toks & _AROUSAL_DOWN)
|
| 50 |
+
arousal = max(0.0, min(1.0, 0.5 + 0.15 * (up - down)))
|
| 51 |
+
|
| 52 |
+
# tags
|
| 53 |
+
tags = []
|
| 54 |
+
if sentiment > 0.25: tags.append("positive")
|
| 55 |
+
if sentiment < -0.25: tags.append("negative")
|
| 56 |
+
if arousal >= 0.65: tags.append("high-energy")
|
| 57 |
+
if arousal <= 0.35: tags.append("low-energy")
|
| 58 |
+
|
| 59 |
+
# trait deltas
|
| 60 |
+
trait_deltas = {}
|
| 61 |
+
for trait, spec in TRAIT_MAP.items():
|
| 62 |
+
found = any(k in toks for k in spec["keywords"])
|
| 63 |
+
if found:
|
| 64 |
+
trait_deltas[trait] = spec["pos"]
|
| 65 |
+
elif sentiment < -0.4 and trait in ("optimism","calm","focus"):
|
| 66 |
+
trait_deltas[trait] = spec["neg"]
|
| 67 |
+
|
| 68 |
+
return {
|
| 69 |
+
"sentiment": sentiment,
|
| 70 |
+
"arousal": arousal,
|
| 71 |
+
"tags": tags,
|
| 72 |
+
"trait_deltas": trait_deltas
|
| 73 |
+
}
|