Spaces:
Runtime error
Runtime error
File size: 1,887 Bytes
f34c5f4 0a2d8ac f34c5f4 1f04d3e 0a2d8ac 298c4f8 0a2d8ac f34c5f4 1f04d3e 720192c 1f04d3e 720192c f34c5f4 720192c f34c5f4 0bd7d7b f34c5f4 298c4f8 f34c5f4 298c4f8 |
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 |
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import re
def label_sentiment(rating):
"""Label sentiment based on the rating."""
if rating in [1, 2]:
return 'negative'
elif rating == 3:
return 'neutral'
elif rating in [4, 5]:
return 'positive'
else:
return 'unknown'
def process_review(review):
"""Simple processing for the review text."""
review = review.lower()
review = re.sub(r'[^a-z\s]', '', review) # Remove non-alphabetical characters
return review
def display_eda(data):
# Derive the 'sentiment' column from 'rating' if it doesn't exist
if 'sentiment' not in data.columns:
if 'rating' not in data.columns:
st.error("The dataset does not contain a 'rating' or 'sentiment' column. Please check the data source.")
return
else:
data['sentiment'] = data['rating'].apply(label_sentiment)
# Distribution of sentiments
st.subheader("Distribution of Sentiments")
sentiment_counts = data['sentiment'].value_counts()
fig, ax = plt.subplots()
sentiment_counts.plot(kind='bar', ax=ax)
ax.set_title('Distribution of Sentiments')
ax.set_xlabel('Sentiment')
ax.set_ylabel('Count')
st.pyplot(fig)
# Word cloud for each sentiment
st.subheader("Word Clouds for Sentiments")
sentiments = data['sentiment'].unique()
for sentiment in sentiments:
st.write(f"Word Cloud for {sentiment}")
subset = data[data['sentiment'] == sentiment]
text = " ".join(process_review(review) for review in subset['review_description'])
wordcloud = WordCloud(max_words=100, background_color="white").generate(text)
fig = plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
st.pyplot(fig)
|