-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentiment Analysis Python Project.txt
More file actions
52 lines (45 loc) · 1.35 KB
/
Sentiment Analysis Python Project.txt
File metadata and controls
52 lines (45 loc) · 1.35 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
from textblob import TextBlob
import matplotlib.pyplot as plt
def analyze_sentiment(text):
blob = TextBlob(text)
polarity = blob.sentiment.polarity
if polarity > 0.1:
return 'Positive'
elif polarity < -0.1:
return 'Negative'
else:
return 'Neutral'
def analyze_texts(texts):
results = [analyze_sentiment(text) for text in texts]
return results
def summary_report(results):
from collections import Counter
counts = Counter(results)
total = sum(counts.values())
print("Sentiment Summary Report:")
for sentiment, count in counts.items():
print(f"{sentiment}: {count} ({count/total:.1%})")
return counts
def plot_results(counts):
labels = counts.keys()
values = counts.values()
plt.bar(labels, values, color=['green', 'gray', 'red'])
plt.title("Sentiment Analysis")
plt.xlabel("Sentiment")
plt.ylabel("Count")
plt.show()
# Example usage:
# texts = ["I love this!", "It's okay.", "I hate this."]
# For file input:
# with open('input.txt') as f:
# texts = f.readlines()
texts = [
"I love this product!",
"This is the worst experience ever.",
"Meh, it's fine.",
"Absolutely fantastic!",
"Not good, not bad."
]
results = analyze_texts(texts)
counts = summary_report(results)
plot_results(counts)