Spaces:
Sleeping
Sleeping
File size: 2,264 Bytes
b694fa0 9dea01b | 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 | import streamlit as st
import pandas as pd
from textblob import TextBlob
from wordcloud import WordCloud
st.set_page_config(page_title="App Reviews Sentiment", layout="centered")
st.title("Sentiment Analysis of App Reviews (TextBlob)")
st.write("Enter a review text and get sentiment (pos/neg/neu) + polarity score.")
# ---------- User input ----------
user_text = st.text_area("Review text", height=120, placeholder="Type a review here...")
if st.button("Analyze"):
if user_text.strip() == "":
st.warning("Please enter some text.")
else:
blob = TextBlob(user_text)
polarity = blob.sentiment.polarity
subjectivity = blob.sentiment.subjectivity
if polarity > 0:
label = "pos"
elif polarity < 0:
label = "neg"
else:
label = "neu"
st.subheader("Result")
st.write(f"**Sentiment:** {label}")
st.write(f"**Polarity:** {polarity:.3f}")
st.write(f"**Subjectivity:** {subjectivity:.3f}")
st.divider()
# ---------- Optional: Dataset + Wordclouds ----------
st.subheader("Optional: WordClouds from Dataset")
uploaded = st.file_uploader("Upload your CSV (must contain columns: Review, Rating)", type=["csv"])
if uploaded is not None:
df = pd.read_csv(uploaded)
# Basic checks
if "Review" not in df.columns:
st.error("CSV must contain a 'Review' column.")
else:
# Compute polarity + sentiment
df["polarity"] = df["Review"].astype(str).apply(lambda x: TextBlob(x).sentiment.polarity)
df["sentiment"] = df["polarity"].apply(lambda x: "pos" if x > 0 else "neg" if x < 0 else "neu")
st.write("Sentiment counts:")
st.dataframe(df["sentiment"].value_counts().rename_axis("sentiment").to_frame("count"))
choice = st.selectbox("Choose sentiment for WordCloud", ["neg", "neu", "pos"])
text = " ".join(df[df["sentiment"] == choice]["Review"].astype(str))
if text.strip() == "":
st.warning("No text available for this sentiment group.")
else:
wc_img = WordCloud(background_color="white", width=800, height=400).generate(text).to_image()
st.image(wc_img, caption=f"WordCloud: {choice}", use_container_width=True) |