text-classification-demo / text_classifier.py
Ahmed766's picture
Upload text_classifier.py with huggingface_hub
6a8f2f4 verified
"""
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()