File size: 7,352 Bytes
d25d261
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7988190
 
d25d261
 
7988190
 
 
 
 
 
 
 
 
 
 
d25d261
7988190
 
 
 
 
d25d261
 
 
 
7988190
 
 
 
d25d261
 
7988190
 
 
 
 
 
 
 
 
 
 
 
 
 
d25d261
 
 
7988190
 
d25d261
7988190
d25d261
 
 
 
 
 
 
7988190
 
d25d261
 
 
7988190
d25d261
 
 
7988190
d25d261
 
 
7988190
 
 
 
 
 
 
 
d25d261
7988190
d25d261
7988190
 
d25d261
 
7988190
 
 
 
 
d25d261
 
7988190
 
 
d25d261
7988190
 
 
 
 
 
 
 
 
 
 
d25d261
7988190
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d25d261
 
7988190
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d25d261
 
 
 
7988190
 
 
 
 
 
 
 
 
 
 
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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(
    """
    <div style="text-align: center; padding: 20px; background-color: #f0f2f6; border-radius: 10px; margin-bottom: 30px;">
        <h1 style="color: #333; font-size: 2.5em;">✍️ Sentiment Analyzer</h1>
        <p style="color: #555; font-size: 1.1em;">
            Discover the sentiment of your text with a simple keyword-based analysis.
        </p>
    </div>
    """,
    unsafe_allow_html=True
)

st.markdown(
    """
    <p style="font-size: 1.1em; text-align: center; margin-bottom: 20px;">
    Enter any text below, and I'll tell you if its sentiment is positive, negative, or neutral based on a predefined list of keywords.
    </p>
    """,
    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"<p style='font-size: 1.05em;'>Positive keyword matches: <strong>{pos_count}</strong></p>", unsafe_allow_html=True)
        st.markdown(f"<p style='font-size: 1.05em;'>Negative keyword matches: <strong>{neg_count}</strong></p>", unsafe_allow_html=True)

        st.markdown("---")
        st.write(f"**Original Text:**")
        st.markdown(f"<div style='background-color: #e9ecef; padding: 15px; border-radius: 8px; border-left: 5px solid #007bff;'><em>{user_input}</em></div>", unsafe_allow_html=True)
    else:
        st.warning("Please enter some text to analyze.")

# Custom CSS for enhanced styling
st.markdown(
    """
    <style>
    /* General body and font */
    body {
        font-family: 'Inter', sans-serif;
        background-color: #f8f9fa;
        color: #343a40;
    }

    /* Streamlit widgets styling */
    .stButton>button {
        background-color: #28a745; /* Green for analyze */
        color: white;
        padding: 12px 25px;
        border-radius: 10px;
        border: none;
        cursor: pointer;
        font-size: 1.1em;
        font-weight: bold;
        box-shadow: 0 5px 15px rgba(40, 167, 69, 0.3);
        transition: all 0.3s ease-in-out;
        width: 100%; /* Make buttons full width in columns */
    }
    .stButton>button:hover {
        background-color: #218838;
        box-shadow: 0 8px 20px rgba(40, 167, 69, 0.4);
        transform: translateY(-2px);
    }

    /* Clear button specific style */
    .stButton[key="clear_btn"] > button {
        background-color: #dc3545; /* Red for clear */
        box-shadow: 0 5px 15px rgba(220, 53, 69, 0.3);
    }
    .stButton[key="clear_btn"] > button:hover {
        background-color: #c82333;
        box-shadow: 0 8px 20px rgba(220, 53, 69, 0.4);
    }

    .stTextArea>div>div>textarea {
        border-radius: 10px;
        border: 1px solid #ced4da;
        padding: 15px;
        font-size: 1.05em;
        box-shadow: inset 0 1px 3px rgba(0,0,0,0.1);
        transition: border-color 0.3s ease-in-out;
    }
    .stTextArea>div>div>textarea:focus {
        border-color: #007bff;
        outline: none;
    }

    /* Streamlit message boxes */
    .stSuccess {
        background-color: #d4edda;
        color: #155724;
        border-radius: 8px;
        padding: 15px;
        border: 1px solid #c3e6cb;
        font-weight: bold;
    }
    .stError {
        background-color: #f8d7da;
        color: #721c24;
        border-radius: 8px;
        padding: 15px;
        border: 1px solid #f5c6cb;
        font-weight: bold;
    }
    .stWarning {
        background-color: #fff3cd;
        color: #856404;
        border-radius: 8px;
        padding: 15px;
        border: 1px solid #ffeeba;
        font-weight: bold;
    }
    .stInfo {
        background-color: #d1ecf1;
        color: #0c5460;
        border-radius: 8px;
        padding: 10px;
        border: 1px solid #bee5eb;
        margin-top: 10px;
    }

    /* Markdown styling for titles and text */
    h1 {
        color: #007bff;
        text-align: center;
        font-weight: 700;
        margin-bottom: 20px;
    }
    h2, h3, h4, h5, h6 {
        color: #343a40;
        margin-top: 25px;
        margin-bottom: 15px;
    }
    p {
        line-height: 1.6;
    }
    </style>
    """,
    unsafe_allow_html=True
)

# Footer
st.markdown(
    """
    <div style="text-align: center; margin-top: 50px; padding: 20px; border-top: 1px solid #eee; color: #6c757d;">
        <p>Built with ❀️ using Streamlit and pure Python.</p>
    </div>
    """,
    unsafe_allow_html=True
)