File size: 3,017 Bytes
6a8f2f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Simplified Text Classification Model
A lightweight sentiment analysis model for demonstration
"""

import json
import re
from collections import Counter


class TextClassifier:
    def __init__(self):
        """
        Initialize the text classifier with simple heuristics
        In a real implementation, this would load a pre-trained model
        """
        # Simple sentiment word lists for demonstration
        self.positive_words = {
            'good', 'great', 'excellent', 'amazing', 'love', 'wonderful', 
            'fantastic', 'awesome', 'brilliant', 'perfect', 'nice', 'best',
            'superb', 'outstanding', 'delightful', 'pleasure', 'happy',
            'joy', 'pleased', 'satisfied', 'impressed'
        }
        self.negative_words = {
            'bad', 'terrible', 'awful', 'horrible', 'hate', 'worst',
            'disgusting', 'disappointing', 'annoying', 'frustrating',
            'sad', 'angry', 'upset', 'displeased', 'regret', 'miserable',
            'pathetic', 'useless', 'broken', 'wrong'
        }
    
    def predict(self, text):
        """
        Predict the sentiment of the input text using simple heuristics
        """
        # Convert to lowercase and split into words
        words = re.findall(r'\b\w+\b', text.lower())
        
        # Count positive and negative words
        pos_count = sum(1 for word in words if word in self.positive_words)
        neg_count = sum(1 for word in words if word in self.negative_words)
        
        # Determine sentiment based on counts
        if pos_count > neg_count:
            sentiment = "positive"
            confidence = min(0.95, 0.5 + (pos_count / (pos_count + neg_count + 1)))
        elif neg_count > pos_count:
            sentiment = "negative"
            confidence = min(0.95, 0.5 + (neg_count / (pos_count + neg_count + 1)))
        else:
            # If equal, look for sentiment intensifiers or just return neutral
            sentiment = "neutral"
            confidence = 0.5
        
        return {
            "label": sentiment,
            "confidence": round(confidence, 4),
            "text": text,
            "positive_words_found": pos_count,
            "negative_words_found": neg_count
        }


def main():
    # Example usage
    classifier = TextClassifier()
    
    examples = [
        "I love this product! It's amazing.",
        "This is terrible. I hate it.",
        "It's okay, nothing special.",
        "The service was excellent and the staff was wonderful!",
        "Completely disappointed with this purchase."
    ]
    
    print("Text Classification Demo:")
    print("=" * 50)
    
    for text in examples:
        result = classifier.predict(text)
        print(f"Text: {result['text']}")
        print(f"Label: {result['label']}, Confidence: {result['confidence']}")
        print(f"Positive words: {result['positive_words_found']}, Negative words: {result['negative_words_found']}")
        print("-" * 30)


if __name__ == "__main__":
    main()