-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentimentGraph.py
31 lines (25 loc) · 1.09 KB
/
sentimentGraph.py
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
import json
import seaborn as sns
import matplotlib.pyplot as plt
# makes a graph comparing star rating with sentiment analysis results
def plot_density_sentiment_vs_rating(input_file):
star_ratings = {1: [], 2: [], 3: [], 4: [], 5: []}
with open(input_file, 'r', encoding='utf-8') as infile:
for line in infile:
review = json.loads(line)
star_rating = review['stars']
sentiment_score = review['sentiment']['compound']
star_ratings[star_rating].append(sentiment_score)
for star_rating, sentiment_scores in star_ratings.items():
sns.kdeplot(sentiment_scores, label=f'{star_rating} Stars', fill=True)
plt.title('Density Plot of Sentiment Score for Each Star Rating')
plt.xlabel('Sentiment Score')
plt.ylabel('Density')
plt.legend(title='Star Rating')
plt.show()
sentiment_file = 'data/reviews_with_sentiment.json'
try:
with open(sentiment_file, 'r', encoding='utf-8'):
plot_density_sentiment_vs_rating(sentiment_file)
except FileNotFoundError:
print(f"The file '{sentiment_file}' does not exist.")