sarmadhassan commited on
Commit
d25d261
·
verified ·
1 Parent(s): 6ee2387

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +123 -0
app.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ # --- App Configuration ---
4
+ st.set_page_config(
5
+ page_title="Basic Python Sentiment Analyzer",
6
+ page_icon="✍️",
7
+ layout="centered",
8
+ initial_sidebar_state="auto"
9
+ )
10
+
11
+ # --- Define Sentiment Keywords (Pure Python Logic) ---
12
+ # These are very basic lists for demonstration purposes.
13
+ # A real-world rule-based system would be much more extensive and nuanced.
14
+ POSITIVE_KEYWORDS = [
15
+ "good", "great", "excellent", "amazing", "fantastic", "love", "happy",
16
+ "joy", "wonderful", "positive", "awesome", "beautiful", "perfect", "like",
17
+ "enjoy", "best", "super", "nice", "pleased", "delightful", "brilliant"
18
+ ]
19
+
20
+ NEGATIVE_KEYWORDS = [
21
+ "bad", "terrible", "horrible", "awful", "hate", "sad", "unhappy",
22
+ "poor", "negative", "disappointing", "worst", "ugly", "frustrating",
23
+ "dislike", "annoying", "miserable", "stressful", "difficult", "problem",
24
+ "fail", "ruin", "never"
25
+ ]
26
+
27
+ # --- Sentiment Analysis Function (Pure Python) ---
28
+ def analyze_sentiment_basic(text):
29
+ """
30
+ Performs a very basic sentiment analysis based on predefined positive and negative keywords.
31
+ This function does not use any external NLP models or libraries.
32
+ """
33
+ if not text:
34
+ return "Neutral", 0, 0 # Return neutral if no text
35
+
36
+ text_lower = text.lower()
37
+ positive_count = 0
38
+ negative_count = 0
39
+
40
+ # Count positive keywords
41
+ for keyword in POSITIVE_KEYWORDS:
42
+ positive_count += text_lower.count(keyword)
43
+
44
+ # Count negative keywords
45
+ for keyword in NEGATIVE_KEYWORDS:
46
+ negative_count += text_lower.count(keyword)
47
+
48
+ # Determine sentiment
49
+ if positive_count > negative_count:
50
+ return "Positive", positive_count, negative_count
51
+ elif negative_count > positive_count:
52
+ return "Negative", positive_count, negative_count
53
+ else:
54
+ return "Neutral", positive_count, negative_count
55
+
56
+ # --- Streamlit UI ---
57
+ st.title("✍️ Basic Python Sentiment Analyzer")
58
+ st.markdown(
59
+ """
60
+ This app performs sentiment analysis using only pure Python, based on a simple keyword matching approach.
61
+ It's a demonstration of a rule-based system without external NLP models.
62
+ """
63
+ )
64
+
65
+ # Text input from the user
66
+ user_input = st.text_area(
67
+ "Enter your text here:",
68
+ "This is a good example, but it could be even better.",
69
+ height=150
70
+ )
71
+
72
+ # Analyze button
73
+ if st.button("Analyze Sentiment"):
74
+ if user_input:
75
+ sentiment, pos_count, neg_count = analyze_sentiment_basic(user_input)
76
+
77
+ st.subheader("Analysis Result:")
78
+
79
+ # Display result with appropriate styling
80
+ if sentiment == "Positive":
81
+ st.success(f"**Sentiment:** Positive 😊")
82
+ elif sentiment == "Negative":
83
+ st.error(f"**Sentiment:** Negative 😠")
84
+ else:
85
+ st.warning(f"**Sentiment:** Neutral 😐")
86
+
87
+ st.info(f"Positive keyword matches: {pos_count}")
88
+ st.info(f"Negative keyword matches: {neg_count}")
89
+
90
+ st.markdown("---")
91
+ st.write(f"**Original Text:**")
92
+ st.write(f"> {user_input}")
93
+ else:
94
+ st.warning("Please enter some text to analyze.")
95
+
96
+ # Custom CSS for styling
97
+ st.markdown(
98
+ """
99
+ <style>
100
+ .stButton>button {
101
+ background-color: #007bff; /* Blue color for the button */
102
+ color: white;
103
+ padding: 10px 20px;
104
+ border-radius: 8px;
105
+ border: none;
106
+ cursor: pointer;
107
+ font-size: 16px;
108
+ box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
109
+ transition: 0.3s;
110
+ }
111
+ .stButton>button:hover {
112
+ background-color: #0056b3; /* Darker blue on hover */
113
+ box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
114
+ }
115
+ .stTextArea>div>div>textarea {
116
+ border-radius: 8px;
117
+ border: 1px solid #ccc;
118
+ padding: 10px;
119
+ }
120
+ </style>
121
+ """,
122
+ unsafe_allow_html=True
123
+ )