"""
TextEmotionAnalyzer
Generated by Eden via recursive self-improvement
2025-10-27 20:29:14.903337
"""

from textblob import TextBlob

class TextEmotionAnalyzer:
    """
    A text emotion analyzer that detects emotions in text.
    
    This capability uses predefined lists of emotion-related words to classify the 
    sentiment of a given text into specific emotional categories. It is based on keyword
    matching rather than machine learning models, making it simple but effective for basic
    emotion detection tasks.
    
    Emotion categories supported:
    - happiness
    - sadness
    - surprise
    - fear
    - anger
    - love
    - neutral (default if no emotion is detected)
    """
    
    def __init__(self):
        self.emotion_keywords = {
            'happiness': ['happy', 'joy', 'pleasure', 'delight', 'excited'],
            'sadness': ['sad', 'sorrow', 'grief', 'melancholy', 'unhappy'],
            'surprise': ['surprised', 'astonished', 'startled', 'shocked', 'amazed'],
            'fear': ['afraid', 'scared', 'alarmed', 'terror', 'panicked'],
            'anger': ['angry', 'mad', 'upset', 'frustrated', 'irritated'],
            'love': ['love', 'affection', 'care', 'devotion', 'passion']
        }
        
    def analyze_emotion(self, text):
        """
        Analyzes the given text and returns the dominant emotion.
        
        Args:
            text (str): The input text to analyze
            
        Returns:
            str: The dominant emotion category or 'neutral' if no emotion is detected
        """
        blob = TextBlob(text)
        words = blob.words
        
        # Convert to lowercase and strip punctuation
        processed_words = [word.lower().strip("'\".,!?") for word in words]
        
        emotion_counts = {category: 0 for category in self.emotion_keywords}
        
        for word in processed_words:
            for category, keywords in self.emotion_keywords.items():
                if word in keywords:
                    emotion_counts[category] += 1
        
        # Find the category with maximum count
        dominant_emotion = max(emotion_counts.items(), key=lambda x: x[1])[0]
        
        return dominant_emotion
    
# Example usage
if __name__ == "__main__":
    analyzer = TextEmotionAnalyzer()
    
    print("Example 1:")
    text = "I am so excited about the new job!"
    emotion = analyzer.analyze_emotion(text)
    print(f"Input: {text}")
    print(f"Detected emotion: {emotion}\n")
    
    print("Example 2:")
    text = "I feel really upset about what happened."
    emotion = analyzer.analyze_emotion(text)
    print(f"Input: {text}")
    print(f"Detected emotion: {emotion}\n")
    
    print("Example 3:")
    text = "The weather is lovely today."
    emotion = analyzer.analyze_emotion(text)
    print(f"Input: {text}")
    print(f"Detected emotion: {emotion}\n")