import streamlit as st # --- App Configuration --- st.set_page_config( page_title="Basic Python Sentiment Analyzer", page_icon="✍️", layout="centered", initial_sidebar_state="auto" ) # --- Define Sentiment Keywords (Pure Python Logic) --- # These are very basic lists for demonstration purposes. # A real-world rule-based system would be much more extensive and nuanced. POSITIVE_KEYWORDS = [ "good", "great", "excellent", "amazing", "fantastic", "love", "happy", "joy", "wonderful", "positive", "awesome", "beautiful", "perfect", "like", "enjoy", "best", "super", "nice", "pleased", "delightful", "brilliant" ] NEGATIVE_KEYWORDS = [ "bad", "terrible", "horrible", "awful", "hate", "sad", "unhappy", "poor", "negative", "disappointing", "worst", "ugly", "frustrating", "dislike", "annoying", "miserable", "stressful", "difficult", "problem", "fail", "ruin", "never" ] # --- Sentiment Analysis Function (Pure Python) --- def analyze_sentiment_basic(text): """ Performs a very basic sentiment analysis based on predefined positive and negative keywords. This function does not use any external NLP models or libraries. """ if not text: return "Neutral", 0, 0 # Return neutral if no text text_lower = text.lower() positive_count = 0 negative_count = 0 # Count positive keywords for keyword in POSITIVE_KEYWORDS: positive_count += text_lower.count(keyword) # Count negative keywords for keyword in NEGATIVE_KEYWORDS: negative_count += text_lower.count(keyword) # Determine sentiment if positive_count > negative_count: return "Positive", positive_count, negative_count elif negative_count > positive_count: return "Negative", positive_count, negative_count else: return "Neutral", positive_count, negative_count # --- Streamlit UI --- # Header Section st.markdown( """
Discover the sentiment of your text with a simple keyword-based analysis.
Enter any text below, and I'll tell you if its sentiment is positive, negative, or neutral based on a predefined list of keywords.
""", unsafe_allow_html=True ) # Text input from the user user_input = st.text_area( "📝 Enter your text here:", "This is a good example, but it could be even better. I really enjoy using Streamlit!", height=180, key="user_text_input" # Added a key for better control ) col1, col2 = st.columns([1, 1]) with col1: analyze_button = st.button("✨ Analyze Sentiment", key="analyze_btn") with col2: clear_button = st.button("🗑️ Clear Text", key="clear_btn") # Clear button functionality if clear_button: st.session_state.user_text_input = "" # Clear the text area st.experimental_rerun() # Rerun to clear the output # Analyze button logic if analyze_button: if user_input: sentiment, pos_count, neg_count = analyze_sentiment_basic(user_input) st.markdown("---") st.subheader("📊 Analysis Result:") # Display result with appropriate styling and icons if sentiment == "Positive": st.success(f"**Sentiment:** Positive 😊") elif sentiment == "Negative": st.error(f"**Sentiment:** Negative 😠") else: st.warning(f"**Sentiment:** Neutral 😐") st.markdown(f"Positive keyword matches: {pos_count}
", unsafe_allow_html=True) st.markdown(f"Negative keyword matches: {neg_count}
", unsafe_allow_html=True) st.markdown("---") st.write(f"**Original Text:**") st.markdown(f"Built with ❤️ using Streamlit and pure Python.