-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive_predict.py
More file actions
204 lines (162 loc) · 7.03 KB
/
interactive_predict.py
File metadata and controls
204 lines (162 loc) · 7.03 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"""
Interactive Sentiment Analysis CLI
Enter text to get real-time sentiment predictions
"""
import sys
from pathlib import Path
# Add src to path
sys.path.append(str(Path(__file__).parent))
from src.pipelines.inference_pipeline import InferencePipeline
class InteractiveSentimentAnalyzer:
"""Interactive CLI for sentiment analysis"""
def __init__(self):
"""Initialize the inference pipeline"""
print("\n" + "="*70)
print(" "*15 + "INTERACTIVE SENTIMENT ANALYZER")
print("="*70)
print("\nInitializing model... Please wait...")
try:
self.pipeline = InferencePipeline()
print("✓ Model loaded successfully!")
except FileNotFoundError:
print("\n❌ ERROR: Model not found!")
print("Please train the model first by running: python train.py")
sys.exit(1)
def print_header(self):
"""Print welcome header"""
print("\n" + "="*70)
print("Welcome! Enter text to analyze its sentiment.")
print("Commands:")
print(" - Type 'quit' or 'exit' to close the application")
print(" - Type 'help' for more information")
print(" - Type 'examples' to see sample predictions")
print("="*70 + "\n")
def print_help(self):
"""Print help information"""
print("\n" + "="*70)
print(" "*25 + "HELP INFORMATION")
print("="*70)
print("\nThis tool analyzes the sentiment of text as:")
print(" • POSITIVE - Expresses happiness, satisfaction, or approval")
print(" • NEGATIVE - Expresses sadness, dissatisfaction, or disapproval")
print(" • NEUTRAL - Neither clearly positive nor negative")
print("\nThe model provides:")
print(" 1. Predicted sentiment label")
print(" 2. Confidence score (0-100%)")
print(" 3. Probability distribution across all sentiments")
print(" 4. Text features (length, word count, symbols)")
print("="*70 + "\n")
def show_examples(self):
"""Show example predictions"""
print("\n" + "="*70)
print(" "*22 + "EXAMPLE PREDICTIONS")
print("="*70)
examples = [
"This product is amazing! I love it!",
"Terrible experience, would not recommend.",
"The service was okay, nothing special.",
"Best purchase I've ever made! Highly recommend!",
"Disappointed with the quality."
]
print("\nAnalyzing example sentences...\n")
for i, text in enumerate(examples, 1):
result = self.analyze_text(text, show_details=False)
print(f"{i}. \"{text}\"")
print(f" → {result['predicted_sentiment'].upper()} "
f"({result['confidence']*100:.1f}% confidence)")
print()
print("="*70 + "\n")
def analyze_text(self, text: str, show_details: bool = True):
"""
Analyze sentiment of input text
Args:
text: Input text to analyze
show_details: Whether to show detailed results
Returns:
Dictionary with prediction results
"""
# Get prediction
results = self.pipeline.predict_with_details(text)
result = results[0] # Get first result
if show_details:
# Display results
print("\n" + "─"*70)
print("ANALYSIS RESULTS")
print("─"*70)
print(f"\n📝 Original Text:")
print(f" \"{result['original_text']}\"")
print(f"\n🔍 Cleaned Text:")
print(f" \"{result['cleaned_text']}\"")
# Sentiment with emoji
sentiment_emoji = {
'positive': '😊',
'negative': '😞',
'neutral': '😐'
}
sentiment = result['predicted_sentiment'].lower()
emoji = sentiment_emoji.get(sentiment, '🤔')
print(f"\n{emoji} PREDICTED SENTIMENT: {result['predicted_sentiment'].upper()}")
print(f" Confidence: {result['confidence']*100:.2f}%")
# Probability bar chart
print(f"\n📊 Probability Distribution:")
probs = result['probabilities']
max_bar_width = 50
for label in ['negative', 'neutral', 'positive']:
prob = probs[label]
bar_width = int(prob * max_bar_width)
bar = '█' * bar_width + '░' * (max_bar_width - bar_width)
print(f" {label.capitalize():8s} [{bar}] {prob*100:5.2f}%")
# Text features
print(f"\n📈 Text Features:")
features = result['features']
print(f" • Text Length: {features['text_length']} characters")
print(f" • Word Count: {features['word_count']} words")
print(f" • Symbol Count: {features['symbol_count']} symbols")
print("─"*70 + "\n")
return result
def run(self):
"""Run the interactive loop"""
self.print_header()
while True:
try:
# Get user input
user_input = input("Enter text to analyze (or command): ").strip()
# Handle empty input
if not user_input:
print("⚠️ Please enter some text to analyze.\n")
continue
# Handle commands
command = user_input.lower()
if command in ['quit', 'exit', 'q']:
print("\n" + "="*70)
print("Thank you for using the Sentiment Analyzer!")
print("="*70 + "\n")
break
elif command == 'help':
self.print_help()
continue
elif command == 'examples':
self.show_examples()
continue
elif command == 'clear':
# Clear screen (works on both Windows and Unix)
import os
os.system('cls' if os.name == 'nt' else 'clear')
self.print_header()
continue
# Analyze the text
self.analyze_text(user_input)
except KeyboardInterrupt:
print("\n\n" + "="*70)
print("Session interrupted by user.")
print("="*70 + "\n")
break
except Exception as e:
print(f"\n❌ Error: {str(e)}")
print("Please try again or type 'help' for assistance.\n")
def main():
"""Main entry point"""
analyzer = InteractiveSentimentAnalyzer()
analyzer.run()
if __name__ == "__main__":
main()