forked from microsoft/AI-For-Beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04-text-sentiment.py
More file actions
268 lines (209 loc) Β· 9.03 KB
/
04-text-sentiment.py
File metadata and controls
268 lines (209 loc) Β· 9.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
"""
Simple Text Sentiment Analysis
================================
This example shows how to analyze the sentiment (emotion) of text.
It's a simplified version that teaches NLP concepts without complex libraries.
What you'll learn:
- Text preprocessing (cleaning and preparing text)
- Feature extraction (converting words to numbers)
- Sentiment classification (positive vs negative)
Use case: Determine if a movie review is positive or negative.
"""
import re
from collections import Counter
class SimpleSentimentAnalyzer:
"""
A basic sentiment analyzer that learns from labeled examples.
How it works:
1. Learns which words appear more in positive vs negative texts
2. Calculates a "sentiment score" for each word
3. Uses these scores to predict sentiment of new text
"""
def __init__(self):
# Store word scores (positive words get positive scores)
self.word_scores = {}
# Track if we've trained
self.is_trained = False
def preprocess_text(self, text):
"""
Clean and prepare text for analysis.
Steps:
1. Convert to lowercase
2. Remove punctuation
3. Split into words
Args:
text: Raw text string
Returns:
List of cleaned words
"""
# Convert to lowercase
text = text.lower()
# Remove punctuation and special characters
text = re.sub(r'[^a-z\s]', '', text)
# Split into words
words = text.split()
# Remove very short words (like "a", "i")
words = [w for w in words if len(w) > 2]
return words
def train(self, training_data):
"""
Learn sentiment patterns from labeled examples.
Args:
training_data: List of (text, sentiment) tuples
where sentiment is 'positive' or 'negative'
"""
print("π Training sentiment analyzer...")
# Count words in positive and negative texts
positive_words = Counter()
negative_words = Counter()
for text, sentiment in training_data:
words = self.preprocess_text(text)
if sentiment == 'positive':
positive_words.update(words)
else:
negative_words.update(words)
# Calculate sentiment score for each word
# Score > 0 means more positive, < 0 means more negative
all_words = set(positive_words.keys()) | set(negative_words.keys())
for word in all_words:
pos_count = positive_words[word]
neg_count = negative_words[word]
# Calculate score: difference in appearances
# Add smoothing (+1) to avoid division by zero
total = pos_count + neg_count
self.word_scores[word] = (pos_count - neg_count) / (total + 1)
self.is_trained = True
# Show some learned words
print(f"β
Learned sentiment for {len(self.word_scores)} words")
print("\nπ Most positive words:")
sorted_words = sorted(self.word_scores.items(), key=lambda x: x[1], reverse=True)
for word, score in sorted_words[:5]:
print(f" '{word}': {score:+.3f}")
print("\nπ Most negative words:")
for word, score in sorted_words[-5:]:
print(f" '{word}': {score:+.3f}")
def analyze(self, text):
"""
Predict the sentiment of new text.
Args:
text: Text to analyze
Returns:
Tuple of (sentiment, confidence, score)
"""
if not self.is_trained:
raise Exception("Please train the analyzer first!")
# Preprocess text
words = self.preprocess_text(text)
# Calculate total sentiment score
total_score = 0
word_count = 0
for word in words:
if word in self.word_scores:
total_score += self.word_scores[word]
word_count += 1
# Average score
if word_count > 0:
avg_score = total_score / word_count
else:
avg_score = 0
# Determine sentiment and confidence
sentiment = "positive" if avg_score > 0 else "negative"
confidence = min(abs(avg_score) * 100, 100) # Convert to percentage
return sentiment, confidence, avg_score
def create_training_data():
"""
Create sample training data (movie reviews with labels).
In a real application, you'd have thousands of examples!
Returns:
List of (review_text, sentiment) tuples
"""
return [
# Positive reviews
("This movie was absolutely amazing and wonderful! I loved every minute.", "positive"),
("Brilliant performance! The acting was superb and the story captivating.", "positive"),
("Fantastic film! Highly recommend to everyone. Best movie of the year!", "positive"),
("Loved it! Great storytelling and beautiful cinematography.", "positive"),
("Excellent movie with outstanding performances. A must watch!", "positive"),
("Amazing! This film exceeded all my expectations. Truly remarkable.", "positive"),
("Wonderful experience! The plot was engaging and entertaining.", "positive"),
("Superb direction and acting! One of the best films I've seen.", "positive"),
# Negative reviews
("Terrible movie. Waste of time and money. Very disappointed.", "negative"),
("Awful film! Poor acting and boring story. Would not recommend.", "negative"),
("Horrible! The worst movie I have ever seen. Extremely disappointing.", "negative"),
("Bad movie with terrible plot. Boring and predictable.", "negative"),
("Disappointing film. Poor execution and weak performances.", "negative"),
("Worst movie ever! Horrible acting and stupid storyline.", "negative"),
("Terrible experience. Boring and poorly made. Don't waste your time.", "negative"),
("Awful! Poor quality and uninteresting. Complete waste of time.", "negative"),
]
def main():
"""
Main function - Let's analyze some sentiments!
"""
print("=" * 70)
print("Simple Text Sentiment Analysis")
print("=" * 70)
print("\nπ Task: Learn to identify positive and negative movie reviews")
print()
# Step 1: Create training data
training_data = create_training_data()
print(f"π Training data: {len(training_data)} movie reviews")
print()
# Step 2: Create and train analyzer
analyzer = SimpleSentimentAnalyzer()
analyzer.train(training_data)
print()
# Step 3: Test on new reviews
print("π§ͺ Testing on new movie reviews:")
print("=" * 70)
test_reviews = [
"This movie was fantastic! I really enjoyed it.",
"Boring and terrible. Not worth watching.",
"Amazing cinematography and wonderful acting!",
"The worst film I've seen this year. Awful.",
"Pretty good movie with some great moments.",
"Disappointing and poorly directed.",
]
for i, review in enumerate(test_reviews, 1):
sentiment, confidence, score = analyzer.analyze(review)
# Visual indicator
indicator = "π" if sentiment == "positive" else "π"
print(f"\nReview {i}:")
print(f" Text: \"{review}\"")
print(f" {indicator} Sentiment: {sentiment.upper()}")
print(f" π Confidence: {confidence:.1f}%")
print(f" π Score: {score:+.3f}")
print("\n" + "=" * 70)
# Interactive mode
print("\n㪠Try it yourself! Enter your own review (or 'quit' to exit):")
print("-" * 70)
while True:
user_input = input("\nYour review: ").strip()
if user_input.lower() in ['quit', 'exit', 'q']:
break
if not user_input:
continue
try:
sentiment, confidence, score = analyzer.analyze(user_input)
indicator = "π" if sentiment == "positive" else "π"
print(f"\n{indicator} Sentiment: {sentiment.upper()}")
print(f"π Confidence: {confidence:.1f}%")
print(f"π Score: {score:+.3f}")
except Exception as e:
print(f"Error: {e}")
# Explanation
print("\nπ‘ What just happened?")
print("1. The analyzer learned word patterns from example reviews")
print("2. It calculated 'sentiment scores' for words")
print("3. For new text, it combines word scores to predict sentiment")
print()
print("π You just built a sentiment analyzer!")
print()
print("π Next steps:")
print(" - Add more training examples to improve accuracy")
print(" - Try analyzing tweets, product reviews, or comments")
print(" - Explore more advanced NLP in lessons/5-NLP/")
print()
if __name__ == "__main__":
main()