Spaces:
Sleeping
Sleeping
| """ | |
| Sentiment Keyword Mapping - Direct keyword to emotion mapping | |
| This module maintains the word→emotion mappings | |
| It's the data layer for sentiment analysis | |
| """ | |
| from typing import Dict, Set, Optional | |
| class KeywordMap: | |
| """Maps emotion keywords to standardized emotion labels""" | |
| EMOTION_KEYWORDS: Dict[str, str] = { | |
| # JOY / HAPPINESS | |
| "happy": "happiness", | |
| "happiness": "happiness", | |
| "joyful": "joy", | |
| "joy": "joy", | |
| "glad": "happiness", | |
| "pleased": "happiness", | |
| "delighted": "delight", | |
| "cheerful": "cheerfulness", | |
| "merry": "joy", | |
| "jolly": "joy", | |
| "elated": "elation", | |
| "ecstatic": "ecstasy", | |
| "euphoric": "euphoria", | |
| "thrilled": "thrill", | |
| "overjoyed": "joy", | |
| "blissful": "contentment", | |
| # EXCITEMENT / ENTHUSIASM | |
| "excited": "excitement", | |
| "exciting": "excitement", | |
| "enthusiastic": "enthusiasm", | |
| "eager": "enthusiasm", | |
| "pumped": "excitement", | |
| "hyped": "excitement", | |
| "stoked": "excitement", | |
| "psyched": "excitement", | |
| # LOVE / AFFECTION | |
| "love": "love", | |
| "loving": "love", | |
| "adore": "adoration", | |
| "cherish": "love", | |
| "affection": "affection", | |
| "fond": "affection", | |
| "caring": "caring", | |
| "tender": "tenderness", | |
| "warm": "affection", | |
| "devoted": "love", | |
| # GRATITUDE | |
| "grateful": "gratitude", | |
| "thankful": "gratitude", | |
| "thanks": "gratitude", | |
| "thank": "gratitude", | |
| "appreciate": "gratitude", | |
| "blessed": "gratitude", | |
| # CONTENTMENT / SATISFACTION | |
| "good": "happiness", | |
| "great": "happiness", | |
| "excellent": "happiness", | |
| "awesome": "happiness", | |
| "ok": "contentment", | |
| "okay": "contentment", | |
| "fine": "contentment", | |
| "well": "contentment", | |
| "content": "contentment", | |
| "satisfied": "satisfaction", | |
| "peaceful": "serenity", | |
| "serene": "serenity", | |
| "calm": "calm", | |
| "relaxed": "relaxed", | |
| "comfortable": "contentment", | |
| "cozy": "contentment", | |
| "pleasant": "pleasure", | |
| # INTEREST / CURIOSITY | |
| "curious": "curiosity", | |
| "interested": "interest", | |
| "intrigued": "intrigue", | |
| "fascinated": "fascination", | |
| "captivated": "fascination", | |
| "wondering": "curiosity", | |
| "wonder": "wonder", | |
| # SURPRISE / AWE | |
| "surprised": "surprise", | |
| "surprising": "surprise", | |
| "amazed": "amazement", | |
| "amazing": "amazement", | |
| "astonished": "astonishment", | |
| "shocked": "shock", | |
| "stunned": "shock", | |
| "wow": "amazement", | |
| "incredible": "amazement", | |
| "unbelievable": "astonishment", | |
| "awesome": "awe", | |
| "awe": "awe", | |
| # SADNESS | |
| "sad": "sadness", | |
| "unhappy": "sadness", | |
| "depressed": "despair", | |
| "miserable": "misery", | |
| "sorrowful": "sorrow", | |
| "grief": "grief", | |
| "grieving": "grief", | |
| "heartbroken": "grief", | |
| "devastated": "despair", | |
| "gloomy": "melancholy", | |
| "melancholy": "melancholy", | |
| "down": "sadness", | |
| "tearful": "sadness", | |
| "crying": "sorrow", | |
| # LONELINESS | |
| "lonely": "loneliness", | |
| "alone": "loneliness", | |
| "isolated": "loneliness", | |
| "abandoned": "loneliness", | |
| "forsaken": "loneliness", | |
| # DISAPPOINTMENT | |
| "disappointed": "disappointment", | |
| "letdown": "disappointment", | |
| "dismayed": "disappointment", | |
| "disheartened": "disappointment", | |
| # FEAR / ANXIETY | |
| "afraid": "fear", | |
| "scared": "fear", | |
| "frightened": "fear", | |
| "terrified": "terror", | |
| "horrified": "horror", | |
| "fearful": "fear", | |
| "anxious": "anxiety", | |
| "nervous": "nervousness", | |
| "worried": "worry", | |
| "worrying": "worry", | |
| "uneasy": "anxiety", | |
| "tense": "nervousness", | |
| "stressed": "anxiety", | |
| "panicked": "panic", | |
| "panicking": "panic", | |
| "dread": "dread", | |
| "dreading": "dread", | |
| "apprehensive": "apprehension", | |
| # ANGER | |
| "angry": "anger", | |
| "mad": "anger", | |
| "furious": "fury", | |
| "enraged": "rage", | |
| "outraged": "rage", | |
| "livid": "fury", | |
| "irate": "anger", | |
| "irritated": "irritation", | |
| "annoyed": "annoyance", | |
| "frustrated": "frustration", | |
| "frustrating": "frustration", | |
| "aggravated": "irritation", | |
| "infuriated": "fury", | |
| "pissed": "anger", | |
| "resentful": "resentment", | |
| "bitter": "bitterness", | |
| "hostile": "hostility", | |
| "hate": "anger", | |
| "hating": "anger", | |
| "terrible": "anger", | |
| "horrible": "disgust", | |
| "awful": "disgust", | |
| # DISGUST | |
| "disgusted": "disgust", | |
| "disgusting": "disgust", | |
| "gross": "disgust", | |
| "revolting": "revulsion", | |
| "repulsed": "revulsion", | |
| "sickened": "disgust", | |
| "nauseated": "disgust", | |
| "appalled": "disgust", | |
| # CONTEMPT | |
| "contempt": "contempt", | |
| "disdain": "disdain", | |
| "scorn": "scorn", | |
| "dismissive": "contempt", | |
| "condescending": "contempt", | |
| # SHAME / EMBARRASSMENT | |
| "ashamed": "shame", | |
| "shameful": "shame", | |
| "embarrassed": "embarrassment", | |
| "embarrassing": "embarrassment", | |
| "humiliated": "humiliation", | |
| "mortified": "humiliation", | |
| "guilty": "guilt", | |
| "regret": "regret", | |
| "remorseful": "remorse", | |
| "sorry": "regret", | |
| # CONFUSION | |
| "confused": "confused", | |
| "confusing": "confused", | |
| "puzzled": "puzzled", | |
| "perplexed": "perplexed", | |
| "bewildered": "bewildered", | |
| "baffled": "baffled", | |
| "lost": "confused", | |
| "uncertain": "uncertain", | |
| # BOREDOM | |
| "bored": "boredom", | |
| "boring": "boredom", | |
| "dull": "boredom", | |
| "uninterested": "boredom", | |
| "tedious": "boredom", | |
| # TIREDNESS | |
| "tired": "tiredness", | |
| "exhausted": "exhaustion", | |
| "weary": "weariness", | |
| "fatigued": "fatigue", | |
| "sleepy": "sleepy", | |
| "drowsy": "sleepy", | |
| "drained": "exhaustion", | |
| # ENVY / JEALOUSY | |
| "jealous": "jealousy", | |
| "envious": "envy", | |
| "covetous": "envy", | |
| # NOSTALGIA | |
| "nostalgic": "nostalgia", | |
| "reminiscing": "nostalgia", | |
| "miss": "longing", | |
| "missing": "longing", | |
| "longing": "longing", | |
| "yearning": "yearning", | |
| # PLAYFUL / AMUSEMENT | |
| "playful": "playful", | |
| "silly": "silly", | |
| "goofy": "silly", | |
| "mischievous": "mischievous", | |
| "teasing": "teasing", | |
| "joking": "playful", | |
| "funny": "amusement", | |
| "hilarious": "amusement", | |
| "lol": "amusement", | |
| "haha": "amusement", | |
| "hehe": "amusement", | |
| "lmao": "amusement", | |
| "rofl": "amusement", | |
| # EMPATHY / SYMPATHY | |
| "sympathetic": "sympathy", | |
| "empathetic": "empathy", | |
| "compassionate": "compassion", | |
| "understanding": "empathy", | |
| "supportive": "caring", | |
| } | |
| # Intensifiers that strengthen emotion | |
| INTENSIFIERS: Set[str] = { | |
| "very", "really", "extremely", "so", "incredibly", "absolutely", | |
| "totally", "completely", "utterly", "deeply", "truly", "highly", | |
| "super", "mega", "ultra", "insanely", "ridiculously" | |
| } | |
| # Negations that flip emotion | |
| NEGATIONS: Set[str] = { | |
| "not", "no", "never", "dont", "doesnt", "didnt", "wont", | |
| "cant", "cannot", "couldnt", "wouldnt", "shouldnt", "isnt", | |
| "arent", "wasnt", "werent", "hardly", "barely", "neither" | |
| } | |
| # Emotion opposites for negation handling | |
| EMOTION_OPPOSITES: Dict[str, str] = { | |
| "happiness": "sadness", | |
| "joy": "sadness", | |
| "love": "neutral", | |
| "hope": "despair", | |
| "excitement": "boredom", | |
| "calm": "anxiety", | |
| "confidence": "fear", | |
| "satisfaction": "disappointment", | |
| "sadness": "happiness", | |
| "anger": "calm", | |
| "fear": "confidence", | |
| "boredom": "excitement", | |
| "despair": "hope", | |
| "anxiety": "calm", | |
| } | |
| def __init__(self, custom_keywords: Optional[Dict[str, str]] = None): | |
| """Initialize with default or custom keywords""" | |
| self.keywords = self.EMOTION_KEYWORDS.copy() | |
| if custom_keywords: | |
| self.keywords.update(custom_keywords) | |
| def get_emotion_for_word(self, word: str) -> Optional[str]: | |
| """Get emotion for a single word""" | |
| word_clean = word.lower().strip() | |
| return self.keywords.get(word_clean) | |
| def is_intensifier(self, word: str) -> bool: | |
| """Check if word is an intensifier""" | |
| return word.lower() in self.INTENSIFIERS | |
| def is_negation(self, word: str) -> bool: | |
| """Check if word is a negation""" | |
| return word.lower() in self.NEGATIONS | |
| def get_opposite_emotion(self, emotion: str) -> str: | |
| """Get opposite emotion (for negation handling)""" | |
| return self.EMOTION_OPPOSITES.get(emotion, "neutral") | |
| def get_all_emotions(self) -> Dict[str, str]: | |
| """Get all emotion keywords""" | |
| return self.keywords.copy() | |
| if __name__ == "__main__": | |
| kmap = KeywordMap() | |
| print(f"Loaded {len(kmap.keywords)} keywords") | |
| print(f"Sample: 'happy' -> {kmap.get_emotion_for_word('happy')}") | |
| print(f"Sample: 'sad' -> {kmap.get_emotion_for_word('sad')}") | |
| print(f"Is 'very' intensifier? {kmap.is_intensifier('very')}") | |
| print(f"Is 'not' negation? {kmap.is_negation('not')}") | |