Spaces:
Sleeping
Sleeping
| # -*- coding: utf-8 -*- | |
| """ | |
| Comprehensive Emotion Test - Test recognition across many text strings | |
| Tests: | |
| 1. Greetings (should be neutral) | |
| 2. Questions (should be neutral/curious) | |
| 3. Positive emotions | |
| 4. Negative emotions | |
| 5. Complex/mixed emotions | |
| 6. Edge cases | |
| Ensures the system interprets text correctly. | |
| """ | |
| import sys | |
| import os | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| from avatar.sentiment_transformer import SentimentAnalyzer as BinaryAnalyzer | |
| from avatar.sentiment_multi_emotion import MultiEmotionAnalyzer | |
| from avatar.sentiment_emoji_map import EmojiMapper | |
| def run_comprehensive_test(): | |
| """Run comprehensive emotion recognition test""" | |
| print("=" * 80) | |
| print("COMPREHENSIVE EMOTION RECOGNITION TEST") | |
| print("=" * 80) | |
| print() | |
| # Load analyzers | |
| print("Loading analyzers...") | |
| binary = BinaryAnalyzer() | |
| multi = MultiEmotionAnalyzer() | |
| mapper = EmojiMapper() | |
| # Test cases organized by category | |
| test_cases = { | |
| "GREETINGS (should be neutral/positive)": [ | |
| ("How are you?", "neutral"), | |
| ("Hello! How are you?", "neutral"), | |
| ("Hey, how are you?", "neutral"), | |
| ("Hi there!", "neutral"), | |
| ("What's up?", "neutral"), | |
| ("Good morning!", "neutral"), | |
| ("Good evening!", "neutral"), | |
| ("How is it going?", "neutral"), | |
| ("How have you been?", "neutral"), | |
| ("Are you okay?", "neutral"), | |
| ("Yo!", "neutral"), | |
| ("Hey!", "neutral"), | |
| ("Greetings!", "neutral"), | |
| ], | |
| "QUESTIONS (should be neutral/curious)": [ | |
| ("What time is it?", "neutral"), | |
| ("Where is the store?", "neutral"), | |
| ("Can you help me?", "neutral"), | |
| ("Do you know the answer?", "neutral"), | |
| ("What do you think?", "neutral"), | |
| ("Is this correct?", "neutral"), | |
| ("Why did that happen?", "neutral"), | |
| ("How does this work?", "neutral"), | |
| ("What is the meaning of life?", "neutral"), | |
| ("Who is that?", "neutral"), | |
| ], | |
| "POSITIVE EMOTIONS": [ | |
| ("I am so happy today!", "positive"), | |
| ("I love this!", "positive"), | |
| ("This is amazing!", "positive"), | |
| ("Thank you so much!", "positive"), | |
| ("I'm grateful for everything", "positive"), | |
| ("This makes me so excited!", "positive"), | |
| ("I feel wonderful!", "positive"), | |
| ("You're the best!", "positive"), | |
| ("I'm thrilled about this!", "positive"), | |
| ("What a beautiful day!", "positive"), | |
| ("I can't wait!", "positive"), | |
| ("This is fantastic!", "positive"), | |
| ("I'm so proud of you!", "positive"), | |
| ("Best day ever!", "positive"), | |
| ("I'm feeling great!", "positive"), | |
| ], | |
| "NEGATIVE EMOTIONS": [ | |
| ("I am so sad", "negative"), | |
| ("This is terrible", "negative"), | |
| ("I'm really angry", "negative"), | |
| ("This makes me furious", "negative"), | |
| ("I hate this", "negative"), | |
| ("I'm scared", "negative"), | |
| ("This is awful", "negative"), | |
| ("I feel miserable", "negative"), | |
| ("I'm disappointed", "negative"), | |
| ("This is frustrating", "negative"), | |
| ("I can't stand this", "negative"), | |
| ("I'm heartbroken", "negative"), | |
| ("Everything is wrong", "negative"), | |
| ("I feel hopeless", "negative"), | |
| ("This is disgusting", "negative"), | |
| ], | |
| "NEUTRAL STATEMENTS": [ | |
| ("The weather is okay", "neutral"), | |
| ("It's Monday", "neutral"), | |
| ("The meeting is at 3pm", "neutral"), | |
| ("I need to go shopping", "neutral"), | |
| ("The car is blue", "neutral"), | |
| ("I'm going to work", "neutral"), | |
| ("It's raining outside", "neutral"), | |
| ("The document is ready", "neutral"), | |
| ("I'll call you later", "neutral"), | |
| ("The report is on my desk", "neutral"), | |
| ], | |
| "EDGE CASES": [ | |
| ("I'm not happy", "negative"), | |
| ("I don't feel bad", "positive"), | |
| ("This isn't terrible", "neutral"), | |
| ("Meh", "neutral"), | |
| ("Whatever", "neutral"), | |
| ("Fine", "neutral"), | |
| ("Ok", "neutral"), | |
| ("Sure", "neutral"), | |
| ("I guess", "neutral"), | |
| ("Maybe", "neutral"), | |
| ], | |
| "SARCASM (tricky)": [ | |
| ("Oh great, just what I needed", "negative"), | |
| ("Yeah, that's exactly what I wanted", "negative"), | |
| ("Oh wonderful", "negative"), | |
| ("How fantastic", "negative"), | |
| ("Just perfect", "negative"), | |
| ], | |
| "MIXED EMOTIONS (should detect last sentiment)": [ | |
| ("I was sad, but now I'm happy!", "positive"), | |
| ("Started great, ended terribly", "negative"), | |
| ("Good news and bad news", "neutral"), | |
| ("I love this! Wait, no I hate it", "negative"), | |
| ("Happy at first, now confused", "neutral"), | |
| ], | |
| } | |
| # Run tests | |
| results = {"pass": 0, "fail": 0, "total": 0} | |
| failed_tests = [] | |
| for category, tests in test_cases.items(): | |
| print(f"\n{'='*80}") | |
| print(f"📋 {category}") | |
| print(f"{'='*80}") | |
| for text, expected_polarity in tests: | |
| results["total"] += 1 | |
| # Test binary analyzer | |
| binary_result = binary.analyze(text) | |
| binary_label = binary_result["label"] | |
| # Test multi-emotion analyzer | |
| multi_result = multi.analyze(text) | |
| multi_label = multi_result["label"] | |
| multi_polarity = multi_result["polarity"] | |
| # Get emoji | |
| emoji = mapper.get_emoji(multi_label) | |
| # Determine pass/fail based on polarity expectation | |
| if expected_polarity == "neutral": | |
| # Neutral can also be positive for greetings | |
| passed = multi_polarity in ["neutral", "positive"] | |
| elif expected_polarity == "positive": | |
| passed = multi_polarity == "positive" | |
| elif expected_polarity == "negative": | |
| passed = multi_polarity == "negative" | |
| else: | |
| passed = True | |
| status = "✅" if passed else "❌" | |
| if passed: | |
| results["pass"] += 1 | |
| else: | |
| results["fail"] += 1 | |
| failed_tests.append((text, expected_polarity, multi_label, multi_polarity)) | |
| print(f"{status} '{text[:40]:40}' -> {multi_label:15} ({multi_polarity:8}) {emoji}") | |
| # Summary | |
| print("\n" + "=" * 80) | |
| print("📊 SUMMARY") | |
| print("=" * 80) | |
| print(f"Total tests: {results['total']}") | |
| print(f"Passed: {results['pass']} ({results['pass']/results['total']*100:.1f}%)") | |
| print(f"Failed: {results['fail']} ({results['fail']/results['total']*100:.1f}%)") | |
| if failed_tests: | |
| print("\n❌ FAILED TESTS:") | |
| print("-" * 80) | |
| for text, expected, detected, polarity in failed_tests: | |
| print(f" '{text[:50]}' - expected {expected}, got {detected} ({polarity})") | |
| return results | |
| if __name__ == "__main__": | |
| run_comprehensive_test() | |