File size: 5,413 Bytes
cd73740 |
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 |
import streamlit as st
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
# Set custom theme and configuration
st.set_page_config(page_title="SentimentSense", page_icon="π§ ", layout="wide")
# Add custom styling
st.markdown(
"""
<style>
.main {
background-color: #f4f4f9;
}
.stButton>button {
background-color: #4CAF50;
color: white;
border: None;
border-radius: 12px;
height: 40px;
width: 100%;
margin: 0.5rem 0;
}
.stDownloadButton>button {
background-color: #1E88E5;
color: white;
border: None;
border-radius: 12px;
height: 40px;
width: 100%;
margin: 0.5rem 0;
}
</style>
""", unsafe_allow_html=True
)
# Title and description with an emoji
st.title("π§ SentimentSense: Your Text Analysis Companion")
st.markdown(
"""
**SentimentSense** provides in-depth sentiment analysis to understand the emotions conveyed in your text.
Simply input your text below and let the magic happen!
"""
)
# Initialize or load session state to keep track of the results
if 'results' not in st.session_state:
st.session_state['results'] = []
# Sidebar for text input and actions
st.sidebar.header("π Input Text for Sentiment Analysis")
text_input = st.sidebar.text_area("Enter text for sentiment analysis:", height=150)
clear_button = st.sidebar.button("Clear Input")
reset_button = st.sidebar.button("Reset Analysis History")
if clear_button:
text_input = ""
if reset_button:
st.session_state['results'] = []
# Function to analyze sentiment
def analyze_sentiment(text):
analyzer = SentimentIntensityAnalyzer()
sentiment = analyzer.polarity_scores(text)
return sentiment
# Function to get primary sentiment with an emoji
def get_primary_sentiment(sentiment):
if sentiment['compound'] >= 0.05:
return "Positive π"
elif sentiment['compound'] <= -0.05:
return "Negative π "
else:
return "Neutral π"
# Analyze button
if st.sidebar.button("Analyze Sentiment"):
if text_input:
sentiment = analyze_sentiment(text_input)
primary_sentiment = get_primary_sentiment(sentiment)
# Display results in the main area
st.subheader("Analysis Results")
col1, col2 = st.columns(2)
with col1:
st.metric(label="Primary Sentiment", value=primary_sentiment)
st.metric(label="Compound Score", value=round(sentiment['compound'], 2))
with col2:
st.metric(label="Positive Score", value=round(sentiment['pos'], 2))
st.metric(label="Neutral Score", value=round(sentiment['neu'], 2))
st.metric(label="Negative Score", value=round(sentiment['neg'], 2))
# Interactive Visualization - Pie Chart for Sentiment Breakdown
st.subheader("Sentiment Score Distribution")
pie_chart = px.pie(
names=['Positive', 'Neutral', 'Negative'],
values=[sentiment['pos'], sentiment['neu'], sentiment['neg']],
color=['Positive', 'Neutral', 'Negative'],
color_discrete_map={'Positive': '#00C853', 'Neutral': '#039BE5', 'Negative': '#D32F2F'},
title='Sentiment Breakdown'
)
st.plotly_chart(pie_chart, use_container_width=True)
# Interactive Visualization - Gauge Chart for Compound Score
gauge_chart = go.Figure(go.Indicator(
mode="gauge+number",
value=sentiment['compound'],
title={'text': "Compound Sentiment Score"},
gauge={'axis': {'range': [-1, 1]},
'bar': {'color': "#1E88E5"},
'steps': [
{'range': [-1, -0.05], 'color': '#D32F2F'},
{'range': [-0.05, 0.05], 'color': '#039BE5'},
{'range': [0.05, 1], 'color': '#00C853'}],
}))
st.plotly_chart(gauge_chart, use_container_width=True)
# Store results in session state
st.session_state['results'].append({
"Text": text_input,
"Primary Sentiment": primary_sentiment,
"Positive": sentiment['pos'],
"Neutral": sentiment['neu'],
"Negative": sentiment['neg'],
"Compound": sentiment['compound']
})
else:
st.warning("Please enter text for analysis.")
# Show past analysis results with a unique style
if st.session_state['results']:
st.subheader("π Sentiment Analysis History")
result_df = pd.DataFrame(st.session_state['results'])
# Interactive Timeline of Results using Plotly
timeline_chart = px.scatter(
result_df,
x='Compound',
y='Primary Sentiment',
color='Primary Sentiment',
hover_data=['Text'],
title='Sentiment Analysis History Timeline',
color_discrete_map={"Positive π": "#00C853", "Neutral π": "#039BE5", "Negative π ": "#D32F2F"}
)
st.plotly_chart(timeline_chart, use_container_width=True)
# Download button for results
csv = result_df.to_csv(index=False).encode('utf-8')
st.download_button(
label="Download Results as CSV",
data=csv,
file_name='sentiment_analysis_results.csv',
mime='text/csv',
)
|