Spaces:
Runtime error
Runtime error
| """ | |
| Test Language Detection and Translation Services | |
| Run with: python test_language_features.py | |
| """ | |
| import sys | |
| import os | |
| # Add parent directory to path | |
| sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| from app.services.language_detector import ( | |
| LanguageDetector, | |
| Language, | |
| detect_language, | |
| detect_language_simple | |
| ) | |
| from app.services.translation_service import ( | |
| TranslationService, | |
| get_translation_service, | |
| translate | |
| ) | |
| def test_language_detection(): | |
| """Test language detection""" | |
| print("=" * 60) | |
| print("TESTING LANGUAGE DETECTION") | |
| print("=" * 60) | |
| detector = LanguageDetector() | |
| # Test cases | |
| test_cases = [ | |
| ("Hello, how are you?", Language.ENGLISH), | |
| ("مجھے بخار ہے", Language.URDU), | |
| ("kya hal hai? mujhe madad chahiye", Language.ROMAN_URDU), | |
| ("admission ke liye kya documents chahiye?", Language.ROMAN_URDU), | |
| ("What are your clinic hours?", Language.ENGLISH), | |
| ("یہ کیا ہے؟", Language.URDU), | |
| ("shukriya bohot acha hai", Language.ROMAN_URDU), | |
| ] | |
| print("\nTest Results:") | |
| for text, expected in test_cases: | |
| language, confidence = detector.detect(text) | |
| status = "✓" if language == expected else "✗" | |
| print(f"\n{status} Text: '{text}'") | |
| print(f" Detected: {language.value} (confidence: {confidence:.2f})") | |
| print(f" Expected: {expected.value}") | |
| # Show detailed analysis | |
| details = detector.detect_with_details(text) | |
| print(f" Details: {details['details']}") | |
| def test_translation(): | |
| """Test translation service""" | |
| print("\n" + "=" * 60) | |
| print("TESTING TRANSLATION SERVICE") | |
| print("=" * 60) | |
| service = TranslationService() | |
| # Test English to Urdu | |
| print("\n📝 English to Urdu Translation:") | |
| english_texts = [ | |
| "Hello, how can I help you?", | |
| "Welcome to our clinic", | |
| "What are your symptoms?", | |
| ] | |
| for text in english_texts: | |
| try: | |
| translated, from_cache = service.translate(text, source="en", target="ur") | |
| cache_status = "📦 (cached)" if from_cache else "🔄 (fresh)" | |
| print(f"\n EN: {text}") | |
| print(f" UR: {translated} {cache_status}") | |
| except Exception as e: | |
| print(f"\n EN: {text}") | |
| print(f" ⚠️ Translation error: {e}") | |
| # Test Roman Urdu normalization | |
| print("\n📝 Roman Urdu Normalization:") | |
| roman_urdu_texts = [ | |
| "salam kya hal hai", | |
| "mujhe admission ke bare mein batao", | |
| "shukriya bohot acha hai", | |
| ] | |
| for text in roman_urdu_texts: | |
| normalized = service.normalize_roman_urdu(text) | |
| print(f"\n Roman: {text}") | |
| print(f" Urdu: {normalized}") | |
| # Test caching | |
| print("\n📝 Testing Cache:") | |
| test_text = "Good morning" | |
| translated1, from_cache1 = service.translate(test_text, "en", "ur") | |
| translated2, from_cache2 = service.translate(test_text, "en", "ur") | |
| print(f" First call: from_cache={from_cache1}") | |
| print(f" Second call: from_cache={from_cache2}") | |
| print(f" Cache working: {'✓' if from_cache2 else '✗'}") | |
| # Get stats | |
| stats = service.get_translation_stats() | |
| print(f"\n📊 Translation Stats:") | |
| for key, value in stats.items(): | |
| print(f" {key}: {value}") | |
| def test_combined_workflow(): | |
| """Test combined language detection and translation""" | |
| print("\n" + "=" * 60) | |
| print("TESTING COMBINED WORKFLOW") | |
| print("=" * 60) | |
| detector = LanguageDetector() | |
| translator = TranslationService() | |
| # Simulate user queries in different languages | |
| queries = [ | |
| "I have a fever and headache", | |
| "مجھے بخار ہے", | |
| "admission ke liye kya documents chahiye", | |
| ] | |
| for query in queries: | |
| print(f"\n📩 User Query: '{query}'") | |
| # Detect language | |
| language, confidence = detector.detect(query) | |
| print(f" Language: {language.value} (confidence: {confidence:.2f})") | |
| # Translate to English if needed | |
| if language == Language.URDU: | |
| try: | |
| english_query = translator.translate_to_english(query, source="ur") | |
| print(f" Translated: '{english_query}'") | |
| except Exception as e: | |
| print(f" Translation error: {e}") | |
| english_query = query | |
| elif language == Language.ROMAN_URDU: | |
| normalized = translator.normalize_roman_urdu(query) | |
| print(f" Normalized: '{normalized}'") | |
| try: | |
| english_query = translator.translate_to_english(normalized, source="ur") | |
| print(f" Translated: '{english_query}'") | |
| except Exception as e: | |
| print(f" Translation error: {e}") | |
| english_query = query | |
| else: | |
| english_query = query | |
| print(f" Final Query (EN): '{english_query}'") | |
| if __name__ == "__main__": | |
| print("\n🚀 Starting Language Feature Tests\n") | |
| try: | |
| test_language_detection() | |
| test_translation() | |
| test_combined_workflow() | |
| print("\n" + "=" * 60) | |
| print("✅ ALL TESTS COMPLETED") | |
| print("=" * 60 + "\n") | |
| except Exception as e: | |
| print(f"\n❌ TEST FAILED: {e}") | |
| import traceback | |
| traceback.print_exc() | |