Delete test_sentiment.py
Browse files- test_sentiment.py +0 -152
test_sentiment.py
DELETED
|
@@ -1,152 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Test Script untuk Sentiment Analysis System
|
| 3 |
-
Menguji fungsionalitas model dan evaluasi
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
import sys
|
| 7 |
-
from app import SentimentAnalyzer, SAMPLE_DATA
|
| 8 |
-
|
| 9 |
-
def test_single_analysis():
|
| 10 |
-
"""Test analisis single text"""
|
| 11 |
-
print("\n" + "="*60)
|
| 12 |
-
print("TEST 1: Single Text Analysis")
|
| 13 |
-
print("="*60)
|
| 14 |
-
|
| 15 |
-
analyzer = SentimentAnalyzer()
|
| 16 |
-
|
| 17 |
-
test_cases = [
|
| 18 |
-
"Bantuan sangat lambat, sudah 3 hari belum ada makanan!",
|
| 19 |
-
"Terima kasih banyak atas bantuan yang cepat",
|
| 20 |
-
"Kapan bantuan akan tiba di lokasi kami?",
|
| 21 |
-
"Hadeh parah banget pelayanannya gak jelas!",
|
| 22 |
-
"Mantap jiwa pelayanannya cepet banget"
|
| 23 |
-
]
|
| 24 |
-
|
| 25 |
-
for i, text in enumerate(test_cases, 1):
|
| 26 |
-
print(f"\n{i}. Text: {text}")
|
| 27 |
-
result = analyzer.analyze(text)
|
| 28 |
-
print(f" Kategori: {result['kategori']}")
|
| 29 |
-
print(f" Confidence: {result['confidence']:.2%}")
|
| 30 |
-
print(f" Level: {result['confidence_level']}")
|
| 31 |
-
print(f" Interpretasi: {result['interpretation']}")
|
| 32 |
-
|
| 33 |
-
print("\n✅ Test 1 PASSED")
|
| 34 |
-
|
| 35 |
-
def test_batch_analysis():
|
| 36 |
-
"""Test analisis batch texts"""
|
| 37 |
-
print("\n" + "="*60)
|
| 38 |
-
print("TEST 2: Batch Analysis")
|
| 39 |
-
print("="*60)
|
| 40 |
-
|
| 41 |
-
analyzer = SentimentAnalyzer()
|
| 42 |
-
|
| 43 |
-
texts = [
|
| 44 |
-
"Posko pengungsian penuh sekali!",
|
| 45 |
-
"Alhamdulillah bantuan sudah sampai",
|
| 46 |
-
"Bagaimana cara mendaftar bantuan?"
|
| 47 |
-
]
|
| 48 |
-
|
| 49 |
-
results = analyzer.batch_analyze(texts)
|
| 50 |
-
|
| 51 |
-
print(f"\nJumlah teks: {len(texts)}")
|
| 52 |
-
for i, (text, result) in enumerate(zip(texts, results), 1):
|
| 53 |
-
print(f"\n{i}. {text}")
|
| 54 |
-
print(f" → {result['kategori']} ({result['confidence']:.1%})")
|
| 55 |
-
|
| 56 |
-
print("\n✅ Test 2 PASSED")
|
| 57 |
-
|
| 58 |
-
def test_evaluation():
|
| 59 |
-
"""Test evaluasi model"""
|
| 60 |
-
print("\n" + "="*60)
|
| 61 |
-
print("TEST 3: Model Evaluation")
|
| 62 |
-
print("="*60)
|
| 63 |
-
|
| 64 |
-
analyzer = SentimentAnalyzer()
|
| 65 |
-
|
| 66 |
-
eval_results = analyzer.evaluate_model(
|
| 67 |
-
SAMPLE_DATA['texts'],
|
| 68 |
-
SAMPLE_DATA['labels']
|
| 69 |
-
)
|
| 70 |
-
|
| 71 |
-
print(f"\nAccuracy: {eval_results['accuracy']:.2%}")
|
| 72 |
-
print(f"Total samples: {len(SAMPLE_DATA['texts'])}")
|
| 73 |
-
print(f"Classes: {', '.join(eval_results['labels'])}")
|
| 74 |
-
|
| 75 |
-
# Per-class metrics
|
| 76 |
-
report = eval_results['classification_report']
|
| 77 |
-
print("\nPer-class Metrics:")
|
| 78 |
-
for label in eval_results['labels']:
|
| 79 |
-
if label in report:
|
| 80 |
-
print(f"\n{label}:")
|
| 81 |
-
print(f" Precision: {report[label]['precision']:.3f}")
|
| 82 |
-
print(f" Recall: {report[label]['recall']:.3f}")
|
| 83 |
-
print(f" F1-Score: {report[label]['f1-score']:.3f}")
|
| 84 |
-
|
| 85 |
-
print("\n✅ Test 3 PASSED")
|
| 86 |
-
|
| 87 |
-
def test_slang_handling():
|
| 88 |
-
"""Test kemampuan menangani slang Indonesia"""
|
| 89 |
-
print("\n" + "="*60)
|
| 90 |
-
print("TEST 4: Slang & Informal Language Handling")
|
| 91 |
-
print("="*60)
|
| 92 |
-
|
| 93 |
-
analyzer = SentimentAnalyzer()
|
| 94 |
-
|
| 95 |
-
slang_tests = [
|
| 96 |
-
("Hadeh parah banget nih pelayanan lambat bgt!", "NEGATIVE"),
|
| 97 |
-
("Mantap jiwa pelayanannya, keren abis!", "POSITIVE"),
|
| 98 |
-
("Gimana sih cara daftar bantuan?", "NEUTRAL"),
|
| 99 |
-
("Gak jelas banget nih, ribet!", "NEGATIVE"),
|
| 100 |
-
("Josss gandos pelayanannya!", "POSITIVE")
|
| 101 |
-
]
|
| 102 |
-
|
| 103 |
-
correct = 0
|
| 104 |
-
for text, expected in slang_tests:
|
| 105 |
-
result = analyzer.analyze(text)
|
| 106 |
-
predicted = result['label']
|
| 107 |
-
status = "✅" if predicted == expected else "❌"
|
| 108 |
-
|
| 109 |
-
print(f"\n{status} Text: {text}")
|
| 110 |
-
print(f" Expected: {expected}, Got: {predicted} ({result['confidence']:.1%})")
|
| 111 |
-
|
| 112 |
-
if predicted == expected:
|
| 113 |
-
correct += 1
|
| 114 |
-
|
| 115 |
-
accuracy = correct / len(slang_tests)
|
| 116 |
-
print(f"\n📊 Slang Handling Accuracy: {accuracy:.1%} ({correct}/{len(slang_tests)})")
|
| 117 |
-
|
| 118 |
-
if accuracy >= 0.6:
|
| 119 |
-
print("✅ Test 4 PASSED (Good slang handling)")
|
| 120 |
-
else:
|
| 121 |
-
print("⚠️ Test 4 WARNING (Moderate slang handling)")
|
| 122 |
-
|
| 123 |
-
def run_all_tests():
|
| 124 |
-
"""Jalankan semua tests"""
|
| 125 |
-
print("\n" + "="*60)
|
| 126 |
-
print("🧪 SENTIMENT ANALYSIS SYSTEM - COMPREHENSIVE TESTS")
|
| 127 |
-
print("="*60)
|
| 128 |
-
print("Model: w11wo/indonesian-roberta-base-sentiment-classifier")
|
| 129 |
-
print("="*60)
|
| 130 |
-
|
| 131 |
-
try:
|
| 132 |
-
test_single_analysis()
|
| 133 |
-
test_batch_analysis()
|
| 134 |
-
test_evaluation()
|
| 135 |
-
test_slang_handling()
|
| 136 |
-
|
| 137 |
-
print("\n" + "="*60)
|
| 138 |
-
print("🎉 ALL TESTS COMPLETED SUCCESSFULLY!")
|
| 139 |
-
print("="*60)
|
| 140 |
-
print("\n✅ Sistem siap digunakan untuk production")
|
| 141 |
-
print("✅ Model dapat menangani berbagai jenis teks Indonesia")
|
| 142 |
-
print("✅ Evaluasi menunjukkan performa yang baik")
|
| 143 |
-
print("\n💡 Jalankan 'python sentiment_app.py' untuk memulai aplikasi")
|
| 144 |
-
|
| 145 |
-
except Exception as e:
|
| 146 |
-
print(f"\n❌ TEST FAILED: {str(e)}")
|
| 147 |
-
import traceback
|
| 148 |
-
traceback.print_exc()
|
| 149 |
-
sys.exit(1)
|
| 150 |
-
|
| 151 |
-
if __name__ == "__main__":
|
| 152 |
-
run_all_tests()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|