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+
import pandas as pd
|
| 3 |
+
import plotly.express as px
|
| 4 |
+
import plotly.graph_objects as go
|
| 5 |
+
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
|
| 6 |
+
|
| 7 |
+
# Set custom theme and configuration
|
| 8 |
+
st.set_page_config(page_title="SentimentSense", page_icon="π§ ", layout="wide")
|
| 9 |
+
|
| 10 |
+
# Add custom styling
|
| 11 |
+
st.markdown(
|
| 12 |
+
"""
|
| 13 |
+
<style>
|
| 14 |
+
.main {
|
| 15 |
+
background-color: #f4f4f9;
|
| 16 |
+
}
|
| 17 |
+
.stButton>button {
|
| 18 |
+
background-color: #4CAF50;
|
| 19 |
+
color: white;
|
| 20 |
+
border: None;
|
| 21 |
+
border-radius: 12px;
|
| 22 |
+
height: 40px;
|
| 23 |
+
width: 100%;
|
| 24 |
+
margin: 0.5rem 0;
|
| 25 |
+
}
|
| 26 |
+
.stDownloadButton>button {
|
| 27 |
+
background-color: #1E88E5;
|
| 28 |
+
color: white;
|
| 29 |
+
border: None;
|
| 30 |
+
border-radius: 12px;
|
| 31 |
+
height: 40px;
|
| 32 |
+
width: 100%;
|
| 33 |
+
margin: 0.5rem 0;
|
| 34 |
+
}
|
| 35 |
+
</style>
|
| 36 |
+
""", unsafe_allow_html=True
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Title and description with an emoji
|
| 40 |
+
st.title("π§ SentimentSense: Your Text Analysis Companion")
|
| 41 |
+
st.markdown(
|
| 42 |
+
"""
|
| 43 |
+
**SentimentSense** provides in-depth sentiment analysis to understand the emotions conveyed in your text.
|
| 44 |
+
Simply input your text below and let the magic happen!
|
| 45 |
+
"""
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Initialize or load session state to keep track of the results
|
| 49 |
+
if 'results' not in st.session_state:
|
| 50 |
+
st.session_state['results'] = []
|
| 51 |
+
|
| 52 |
+
# Sidebar for text input and actions
|
| 53 |
+
st.sidebar.header("π Input Text for Sentiment Analysis")
|
| 54 |
+
text_input = st.sidebar.text_area("Enter text for sentiment analysis:", height=150)
|
| 55 |
+
clear_button = st.sidebar.button("Clear Input")
|
| 56 |
+
reset_button = st.sidebar.button("Reset Analysis History")
|
| 57 |
+
|
| 58 |
+
if clear_button:
|
| 59 |
+
text_input = ""
|
| 60 |
+
if reset_button:
|
| 61 |
+
st.session_state['results'] = []
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# Function to analyze sentiment
|
| 65 |
+
def analyze_sentiment(text):
|
| 66 |
+
analyzer = SentimentIntensityAnalyzer()
|
| 67 |
+
sentiment = analyzer.polarity_scores(text)
|
| 68 |
+
return sentiment
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
# Function to get primary sentiment with an emoji
|
| 72 |
+
def get_primary_sentiment(sentiment):
|
| 73 |
+
if sentiment['compound'] >= 0.05:
|
| 74 |
+
return "Positive π"
|
| 75 |
+
elif sentiment['compound'] <= -0.05:
|
| 76 |
+
return "Negative π "
|
| 77 |
+
else:
|
| 78 |
+
return "Neutral π"
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
# Analyze button
|
| 82 |
+
if st.sidebar.button("Analyze Sentiment"):
|
| 83 |
+
if text_input:
|
| 84 |
+
sentiment = analyze_sentiment(text_input)
|
| 85 |
+
primary_sentiment = get_primary_sentiment(sentiment)
|
| 86 |
+
|
| 87 |
+
# Display results in the main area
|
| 88 |
+
st.subheader("Analysis Results")
|
| 89 |
+
col1, col2 = st.columns(2)
|
| 90 |
+
with col1:
|
| 91 |
+
st.metric(label="Primary Sentiment", value=primary_sentiment)
|
| 92 |
+
st.metric(label="Compound Score", value=round(sentiment['compound'], 2))
|
| 93 |
+
with col2:
|
| 94 |
+
st.metric(label="Positive Score", value=round(sentiment['pos'], 2))
|
| 95 |
+
st.metric(label="Neutral Score", value=round(sentiment['neu'], 2))
|
| 96 |
+
st.metric(label="Negative Score", value=round(sentiment['neg'], 2))
|
| 97 |
+
|
| 98 |
+
# Interactive Visualization - Pie Chart for Sentiment Breakdown
|
| 99 |
+
st.subheader("Sentiment Score Distribution")
|
| 100 |
+
pie_chart = px.pie(
|
| 101 |
+
names=['Positive', 'Neutral', 'Negative'],
|
| 102 |
+
values=[sentiment['pos'], sentiment['neu'], sentiment['neg']],
|
| 103 |
+
color=['Positive', 'Neutral', 'Negative'],
|
| 104 |
+
color_discrete_map={'Positive': '#00C853', 'Neutral': '#039BE5', 'Negative': '#D32F2F'},
|
| 105 |
+
title='Sentiment Breakdown'
|
| 106 |
+
)
|
| 107 |
+
st.plotly_chart(pie_chart, use_container_width=True)
|
| 108 |
+
|
| 109 |
+
# Interactive Visualization - Gauge Chart for Compound Score
|
| 110 |
+
gauge_chart = go.Figure(go.Indicator(
|
| 111 |
+
mode="gauge+number",
|
| 112 |
+
value=sentiment['compound'],
|
| 113 |
+
title={'text': "Compound Sentiment Score"},
|
| 114 |
+
gauge={'axis': {'range': [-1, 1]},
|
| 115 |
+
'bar': {'color': "#1E88E5"},
|
| 116 |
+
'steps': [
|
| 117 |
+
{'range': [-1, -0.05], 'color': '#D32F2F'},
|
| 118 |
+
{'range': [-0.05, 0.05], 'color': '#039BE5'},
|
| 119 |
+
{'range': [0.05, 1], 'color': '#00C853'}],
|
| 120 |
+
}))
|
| 121 |
+
st.plotly_chart(gauge_chart, use_container_width=True)
|
| 122 |
+
|
| 123 |
+
# Store results in session state
|
| 124 |
+
st.session_state['results'].append({
|
| 125 |
+
"Text": text_input,
|
| 126 |
+
"Primary Sentiment": primary_sentiment,
|
| 127 |
+
"Positive": sentiment['pos'],
|
| 128 |
+
"Neutral": sentiment['neu'],
|
| 129 |
+
"Negative": sentiment['neg'],
|
| 130 |
+
"Compound": sentiment['compound']
|
| 131 |
+
})
|
| 132 |
+
else:
|
| 133 |
+
st.warning("Please enter text for analysis.")
|
| 134 |
+
|
| 135 |
+
# Show past analysis results with a unique style
|
| 136 |
+
if st.session_state['results']:
|
| 137 |
+
st.subheader("π Sentiment Analysis History")
|
| 138 |
+
result_df = pd.DataFrame(st.session_state['results'])
|
| 139 |
+
|
| 140 |
+
# Interactive Timeline of Results using Plotly
|
| 141 |
+
timeline_chart = px.scatter(
|
| 142 |
+
result_df,
|
| 143 |
+
x='Compound',
|
| 144 |
+
y='Primary Sentiment',
|
| 145 |
+
color='Primary Sentiment',
|
| 146 |
+
hover_data=['Text'],
|
| 147 |
+
title='Sentiment Analysis History Timeline',
|
| 148 |
+
color_discrete_map={"Positive π": "#00C853", "Neutral π": "#039BE5", "Negative π ": "#D32F2F"}
|
| 149 |
+
)
|
| 150 |
+
st.plotly_chart(timeline_chart, use_container_width=True)
|
| 151 |
+
|
| 152 |
+
# Download button for results
|
| 153 |
+
csv = result_df.to_csv(index=False).encode('utf-8')
|
| 154 |
+
st.download_button(
|
| 155 |
+
label="Download Results as CSV",
|
| 156 |
+
data=csv,
|
| 157 |
+
file_name='sentiment_analysis_results.csv',
|
| 158 |
+
mime='text/csv',
|
| 159 |
+
)
|