File size: 6,781 Bytes
c024705
 
 
 
 
 
 
 
 
 
 
 
eeacc46
c024705
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env python3
"""
Enhanced test script for improved language detection accuracy
"""

import requests
import json
import time

def test_language_detection_accuracy():
    """Test the chatbot with various language inputs to verify accuracy"""
    
    base_url = "https://prodevroger-ishingiro.hf.space"
    
    # Comprehensive test cases with different languages and scenarios
    test_cases = [
        # English tests
        {
            "language": "English",
            "code": "en", 
            "query": "Hello, I'm feeling anxious today. Can you help me?",
            "expected_keywords": ["hello", "help", "support", "anxiety"]
        },
        {
            "language": "English",
            "code": "en", 
            "query": "I need help with my mental health",
            "expected_keywords": ["help", "mental", "health"]
        },
        
        # French tests
        {
            "language": "French",
            "code": "fr",
            "query": "Bonjour, je me sens anxieux aujourd'hui. Pouvez-vous m'aider?",
            "expected_keywords": ["bonjour", "anxieux", "aider", "vous"]
        },
        {
            "language": "French",
            "code": "fr",
            "query": "J'ai des problèmes de santé mentale",
            "expected_keywords": ["problèmes", "santé", "mentale"]
        },
        {
            "language": "French",
            "code": "fr",
            "query": "Merci beaucoup pour votre aide",
            "expected_keywords": ["merci", "aide", "votre"]
        },
        
        # Kinyarwanda tests
        {
            "language": "Kinyarwanda", 
            "code": "rw",
            "query": "Muraho, ndabishimye uyu munsi. Murakoze mufasha?",
            "expected_keywords": ["muraho", "ndabishimye", "murakoze", "mufasha"]
        },
        {
            "language": "Kinyarwanda", 
            "code": "rw",
            "query": "Nshaka ubufasha bw'ubuzima bw'ubwoba",
            "expected_keywords": ["nshaka", "ubufasha", "ubuzima", "ubwoba"]
        },
        {
            "language": "Kinyarwanda", 
            "code": "rw",
            "query": "Murakoze cyane, ndabishimye cane",
            "expected_keywords": ["murakoze", "cyane", "ndabishimye", "cane"]
        },
        
        # Kiswahili tests
        {
            "language": "Kiswahili",
            "code": "sw", 
            "query": "Hujambo, nina wasiwasi leo. Unaweza kunisaidia?",
            "expected_keywords": ["hujambo", "wasiwasi", "kunisaidia"]
        },
        {
            "language": "Kiswahili",
            "code": "sw", 
            "query": "Nina shida za afya ya akili",
            "expected_keywords": ["shida", "afya", "akili"]
        },
        {
            "language": "Kiswahili",
            "code": "sw", 
            "query": "Asante sana kwa msaada wako",
            "expected_keywords": ["asante", "msaada", "wako"]
        },
        
        # Mixed language tests (should detect dominant language)
        {
            "language": "Mixed (French dominant)",
            "code": "fr",
            "query": "Bonjour, I need help avec mon anxiety",
            "expected_keywords": ["bonjour", "avec"]
        },
        {
            "language": "Mixed (Kinyarwanda dominant)",
            "code": "rw", 
            "query": "Muraho, I am feeling ndabishimye today",
            "expected_keywords": ["muraho", "ndabishimye"]
        },
        
        # Short message tests
        {
            "language": "French",
            "code": "fr",
            "query": "Bonjour",
            "expected_keywords": ["bonjour"]
        },
        {
            "language": "Kinyarwanda",
            "code": "rw",
            "query": "Muraho",
            "expected_keywords": ["muraho"]
        },
        {
            "language": "Kiswahili",
            "code": "sw",
            "query": "Hujambo",
            "expected_keywords": ["hujambo"]
        }
    ]
    
    print("🧪 Testing Enhanced AIMHSA Language Detection Accuracy")
    print("=" * 70)
    
    correct_detections = 0
    total_tests = len(test_cases)
    
    for i, test_case in enumerate(test_cases, 1):
        print(f"\n{i}. Testing {test_case['language']} ({test_case['code']})")
        print(f"   Query: {test_case['query']}")
        
        try:
            # Make request to the chatbot
            response = requests.post(f"{base_url}/ask", json={
                "query": test_case['query'],
                "account": "test_user"
            }, timeout=10)
            
            if response.status_code == 200:
                data = response.json()
                answer = data.get('answer', 'No answer received')
                print(f"   Response: {answer[:80]}...")
                
                # Check if response contains expected language keywords
                answer_lower = answer.lower()
                found_keywords = []
                for keyword in test_case['expected_keywords']:
                    if keyword.lower() in answer_lower:
                        found_keywords.append(keyword)
                
                if found_keywords:
                    print(f"   ✅ Language keywords found: {found_keywords}")
                    correct_detections += 1
                else:
                    print(f"   ❌ Expected keywords not found: {test_case['expected_keywords']}")
                    
            else:
                print(f"   ❌ Error: {response.status_code} - {response.text}")
                
        except Exception as e:
            print(f"   ❌ Exception: {e}")
        
        # Small delay to avoid overwhelming the server
        time.sleep(0.5)
    
    # Calculate accuracy
    accuracy = (correct_detections / total_tests) * 100
    print("\n" + "=" * 70)
    print(f"🎯 Test Results:")
    print(f"   Correct detections: {correct_detections}/{total_tests}")
    print(f"   Accuracy: {accuracy:.1f}%")
    
    if accuracy >= 80:
        print("   ✅ Language detection is working well!")
    elif accuracy >= 60:
        print("   ⚠️  Language detection needs improvement")
    else:
        print("   ❌ Language detection needs significant improvement")
    
    print("\n🔧 Key Improvements Made:")
    print("• Enhanced pattern matching with comprehensive word lists")
    print("• Scoring system for multiple language indicators")
    print("• Better confidence thresholds (0.8 for high confidence)")
    print("• Pattern override for medium confidence cases")
    print("• False positive detection for English")
    print("• Conversation history analysis for consistency")
    print("• Detailed logging for debugging")

if __name__ == "__main__":
    test_language_detection_accuracy()