import streamlit as st from textblob import TextBlob def main(): # Page configuration st.set_page_config( page_title="Basic Sentiment Analysis", page_icon="🔄", layout="centered" ) # Custom CSS for styling st.markdown(""" """, unsafe_allow_html=True) # Header st.title("Basic Sentiment Analysis") st.markdown("Enter text below to analyze its sentiment (without using pre-trained models).") # Text input user_input = st.text_area("Enter your text:", height=100, placeholder="Type something like 'I love this!' or 'This is terrible.'") if st.button("Analyze Sentiment"): if user_input: # Analyze sentiment using TextBlob analysis = TextBlob(user_input) polarity = analysis.sentiment.polarity # Determine sentiment category if polarity > 0.2: sentiment = "Positive 😊" emotion_class = "positive" elif polarity < -0.2: sentiment = "Negative 😞" emotion_class = "negative" else: sentiment = "Neutral 😐" emotion_class = "neutral" # Display results st.markdown(f"
", unsafe_allow_html=True) st.subheader("Sentiment Analysis Results:") col1, col2 = st.columns(2) with col1: st.metric("Sentiment", sentiment) with col2: st.metric("Polarity Score", round(polarity, 3)) st.progress((polarity + 1) / 2) st.markdown(""" **Polarity Scale:** -1.0 (Very Negative) —— 0.0 (Neutral) —— +1.0 (Very Positive) """) st.markdown("
", unsafe_allow_html=True) # Additional analysis with st.expander("Detailed Analysis:"): st.write(f"- **Subjectivity:** {'Subjective' if analysis.sentiment.subjectivity > 0.5 else 'Objective'} " f"(Score: {round(analysis.sentiment.subjectivity, 3)})") st.write("- **Word Count:**", len(analysis.words)) else: st.warning("Please enter some text to analyze.") if __name__ == "__main__": main()