File size: 2,197 Bytes
af7b60b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Example script for using IndoHoaxDetector model
This script demonstrates how to load the model and make predictions on Indonesian news text.
"""

import pickle

def load_model(model_path='logreg_model.pkl'):
    """Load the trained logistic regression model."""
    with open(model_path, 'rb') as f:
        model = pickle.load(f)
    return model

def predict_hoax(text, model):
    """
    Predict if the given text is a hoax or legitimate news.

    Args:
        text (str): Indonesian news text to classify
        model: Loaded sklearn model

    Returns:
        dict: Prediction results with label and confidence
    """
    # Make prediction
    prediction = model.predict([text])[0]
    probabilities = model.predict_proba([text])[0]

    # Interpret results
    label = "Hoax" if prediction == 1 else "Legitimate"
    confidence = probabilities[prediction]

    return {
        'prediction': label,
        'confidence': confidence,
        'probabilities': {
            'legitimate': probabilities[0],
            'hoax': probabilities[1]
        }
    }

def main():
    """Main function to demonstrate model usage."""
    # Load the model
    print("Loading IndoHoaxDetector model...")
    model = load_model()

    # Example texts (Indonesian news snippets)
    example_texts = [
        "Presiden mengumumkan kebijakan baru untuk ekonomi nasional hari ini di Jakarta.",
        "Alien mendarat di Monas dan bertemu dengan presiden secara rahasia.",
        "Harga bahan pokok naik 50% akibat cuaca ekstrem di beberapa daerah.",
        "Minum air kelapa bisa menyembuhkan semua penyakit termasuk kanker stadium 4."
    ]

    print("\n" + "="*60)
    print("IndoHoaxDetector Predictions")
    print("="*60)

    for i, text in enumerate(example_texts, 1):
        print(f"\nExample {i}:")
        print(f"Text: {text[:100]}{'...' if len(text) > 100 else ''}")

        result = predict_hoax(text, model)
        print(f"Prediction: {result['prediction']}")
        print(".4f")

    print("\n" + "="*60)
    print("Note: This is a demonstration. Always verify predictions with human expertise.")
    print("="*60)

if __name__ == "__main__":
    main()