1+ from typing import Dict , Literal , List
2+ import re
3+
4+ # ============================================================================
5+ # METHOD 1: Using Transformers (BERT, RoBERTa, DistilBERT)
6+ # ============================================================================
7+ class TransformerSentimentAnalyzer :
8+ """
9+ Sentiment analysis using pre-trained transformer models.
10+ Install: pip install transformers torch
11+ """
12+
13+ def __init__ (self , model_name : str = "distilbert-base-uncased-finetuned-sst-2-english" ):
14+ """
15+ Initialize with a pre-trained model.
16+
17+ Popular models:
18+ - distilbert-base-uncased-finetuned-sst-2-english (fast, lightweight)
19+ - cardiffnlp/twitter-roberta-base-sentiment (social media)
20+ - nlptown/bert-base-multilingual-uncased-sentiment (multilingual)
21+ """
22+ from transformers import pipeline
23+ self .classifier = pipeline ('sentiment-analysis' , model = model_name )
24+
25+
26+ def analyze (self , text : str ) -> Dict :
27+ """Analyze sentiment using transformer model."""
28+ result = self .classifier (text )[0 ]
29+ return {
30+ 'sentiment' : result ['label' ].lower (),
31+ 'confidence' : round (result ['score' ], 4 )
32+ }
33+
34+
35+ def batch_analyze (self , texts : List [str ]) -> List [Dict ]:
36+ """Analyze multiple texts efficiently."""
37+ results = self .classifier (text )
38+
39+ return [
40+ {
41+ 'sentiment' :r ['label' ].lower (),
42+ 'confidence' : round (r ['score' ], 4 )
43+ }
44+ for r in results
45+ ]
46+
47+
48+
49+ # ============================================================================
50+ # TESTING AND EXAMPLES
51+ # ============================================================================
52+ if __name__ == "__main__" :
53+
54+ print ("=" * 80 )
55+ print ("TRANSFORMER SENTIMENT ANALYSIS - TESTING" )
56+ print ("=" * 80 + "\n " )
57+
58+ # Initialize the analyzer
59+ print ("Loading model... (this may take a moment on first run)" )
60+ analyzer = TransformerSentimentAnalyzer ()
61+ print ("Model loaded successfully!\n " )
62+
63+ # ========================================================================
64+ # TEST 1: Single Text Analysis
65+ # ========================================================================
66+ print ("TEST 1: Single Text Analysis" )
67+ print ("-" * 80 )
68+
69+ single_text = "This movie is absolutely amazing! I loved every minute of it."
70+ result = analyzer .analyze (single_text )
71+
72+ print (f"Text: { single_text } " )
73+ print (f"Sentiment: { result ['sentiment' ].upper ()} " )
74+ print (f"Confidence: { result ['confidence' ]* 100 :.2f} %\n " )
75+
76+ # ========================================================================
77+ # TEST 2: Multiple Different Sentiments
78+ # ========================================================================
79+ print ("\n TEST 2: Multiple Different Sentiments" )
80+ print ("-" * 80 )
81+
82+ test_cases = [
83+ "I absolutely love this product! It's the best thing ever!" ,
84+ "This is terrible. Worst experience of my life." ,
85+ "It's okay, nothing special but not bad either." ,
86+ "The customer service was outstanding and very helpful." ,
87+ "I'm so disappointed and frustrated with this purchase." ,
88+ ]
89+
90+ for i , text in enumerate (test_cases , 1 ):
91+ result = analyzer .analyze (text )
92+ print (f"\n { i } . Text: { text } " )
93+ print (f" Sentiment: { result ['sentiment' ].upper ()} " )
94+ print (f" Confidence: { result ['confidence' ]* 100 :.2f} %" )
95+
96+ # ========================================================================
97+ # TEST 3: Batch Analysis (More Efficient)
98+ # ========================================================================
99+ print ("\n \n TEST 3: Batch Analysis" )
100+ print ("-" * 80 )
101+
102+ batch_texts = [
103+ "Great service!" ,
104+ "Horrible experience." ,
105+ "Not impressed at all." ,
106+ "Fantastic product, highly recommend!" ,
107+ "Could be better, but acceptable."
108+ ]
109+
110+ batch_results = analyzer .batch_analyze (batch_texts )
111+
112+ for text , result in zip (batch_texts , batch_results ):
113+ print (f"\n Text: { text } " )
114+ print (f"Sentiment: { result ['sentiment' ].upper ()} ({ result ['confidence' ]* 100 :.2f} %)" )
115+
116+ # ========================================================================
117+ # TEST 4: Edge Cases
118+ # ========================================================================
119+ print ("\n \n TEST 4: Edge Cases" )
120+ print ("-" * 80 )
121+
122+ edge_cases = [
123+ "😊❤️" , # Emojis
124+ "Not bad at all!" , # Negation
125+ "I don't hate it." , # Double negation
126+ "" , # Empty (will handle gracefully)
127+ "Meh." , # Ambiguous
128+ ]
129+
130+ for text in edge_cases :
131+ if text : # Skip empty strings
132+ result = analyzer .analyze (text )
133+ print (f"\n Text: '{ text } '" )
134+ print (f"Sentiment: { result ['sentiment' ].upper ()} " )
135+ print (f"Confidence: { result ['confidence' ]* 100 :.2f} %" )
136+
137+ # ========================================================================
138+ # TEST 5: Summary Statistics
139+ # ========================================================================
140+ print ("\n \n " + "=" * 80 )
141+ print ("SUMMARY STATISTICS" )
142+ print ("=" * 80 )
143+
144+ all_texts = test_cases + batch_texts
145+ all_results = analyzer .batch_analyze (all_texts )
146+
147+ positive_count = sum (1 for r in all_results if r ['sentiment' ] == 'positive' )
148+ negative_count = sum (1 for r in all_results if r ['sentiment' ] == 'negative' )
149+ avg_confidence = sum (r ['confidence' ] for r in all_results ) / len (all_results )
150+
151+ print (f"\n Total texts analyzed: { len (all_texts )} " )
152+ print (f"Positive sentiments: { positive_count } " )
153+ print (f"Negative sentiments: { negative_count } " )
154+ print (f"Average confidence: { avg_confidence * 100 :.2f} %" )
155+
156+ print ("\n " + "=" * 80 )
157+ print ("Testing completed!" )
158+ print ("=" * 80 )
0 commit comments