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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ============================================
|
| 2 |
+
# RULE-BASED SENTIMENT ANALYSIS
|
| 3 |
+
# Python + Gradio
|
| 4 |
+
# ============================================
|
| 5 |
+
|
| 6 |
+
# Install Gradio
|
| 7 |
+
!pip install -q gradio
|
| 8 |
+
|
| 9 |
+
# Import libraries
|
| 10 |
+
import re
|
| 11 |
+
import gradio as gr
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# ============================================
|
| 15 |
+
# SENTIMENT RULES
|
| 16 |
+
# ============================================
|
| 17 |
+
|
| 18 |
+
positive_words = {
|
| 19 |
+
"good",
|
| 20 |
+
"great",
|
| 21 |
+
"excellent",
|
| 22 |
+
"amazing",
|
| 23 |
+
"awesome",
|
| 24 |
+
"happy",
|
| 25 |
+
"love",
|
| 26 |
+
"like",
|
| 27 |
+
"wonderful",
|
| 28 |
+
"fantastic",
|
| 29 |
+
"best",
|
| 30 |
+
"beautiful",
|
| 31 |
+
"perfect",
|
| 32 |
+
"nice",
|
| 33 |
+
"enjoy",
|
| 34 |
+
"enjoyed",
|
| 35 |
+
"helpful",
|
| 36 |
+
"successful",
|
| 37 |
+
"success",
|
| 38 |
+
"brilliant",
|
| 39 |
+
"positive",
|
| 40 |
+
"thank",
|
| 41 |
+
"thanks"
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
negative_words = {
|
| 46 |
+
"bad",
|
| 47 |
+
"terrible",
|
| 48 |
+
"awful",
|
| 49 |
+
"horrible",
|
| 50 |
+
"sad",
|
| 51 |
+
"hate",
|
| 52 |
+
"dislike",
|
| 53 |
+
"worst",
|
| 54 |
+
"poor",
|
| 55 |
+
"ugly",
|
| 56 |
+
"wrong",
|
| 57 |
+
"disappointing",
|
| 58 |
+
"disappointed",
|
| 59 |
+
"failure",
|
| 60 |
+
"fail",
|
| 61 |
+
"failed",
|
| 62 |
+
"negative",
|
| 63 |
+
"angry",
|
| 64 |
+
"boring",
|
| 65 |
+
"problem",
|
| 66 |
+
"problems",
|
| 67 |
+
"difficult",
|
| 68 |
+
"useless",
|
| 69 |
+
"slow",
|
| 70 |
+
"broken"
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# ============================================
|
| 75 |
+
# SENTIMENT ANALYSIS FUNCTION
|
| 76 |
+
# ============================================
|
| 77 |
+
|
| 78 |
+
def sentiment_analyzer(text):
|
| 79 |
+
|
| 80 |
+
# Check empty input
|
| 81 |
+
if not text or not text.strip():
|
| 82 |
+
return "Please enter a sentence."
|
| 83 |
+
|
| 84 |
+
# Convert to lowercase
|
| 85 |
+
text_lower = text.lower()
|
| 86 |
+
|
| 87 |
+
# Extract words
|
| 88 |
+
words = re.findall(r'\b\w+\b', text_lower)
|
| 89 |
+
|
| 90 |
+
# Find positive words
|
| 91 |
+
positive_matches = [
|
| 92 |
+
word for word in words
|
| 93 |
+
if word in positive_words
|
| 94 |
+
]
|
| 95 |
+
|
| 96 |
+
# Find negative words
|
| 97 |
+
negative_matches = [
|
| 98 |
+
word for word in words
|
| 99 |
+
if word in negative_words
|
| 100 |
+
]
|
| 101 |
+
|
| 102 |
+
# Count sentiment words
|
| 103 |
+
positive_count = len(positive_matches)
|
| 104 |
+
negative_count = len(negative_matches)
|
| 105 |
+
|
| 106 |
+
# ========================================
|
| 107 |
+
# SENTIMENT DECISION RULE
|
| 108 |
+
# ========================================
|
| 109 |
+
|
| 110 |
+
if positive_count > negative_count:
|
| 111 |
+
|
| 112 |
+
sentiment = "π Positive"
|
| 113 |
+
|
| 114 |
+
elif negative_count > positive_count:
|
| 115 |
+
|
| 116 |
+
sentiment = "π Negative"
|
| 117 |
+
|
| 118 |
+
else:
|
| 119 |
+
|
| 120 |
+
sentiment = "π Neutral"
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
# ========================================
|
| 124 |
+
# CREATE RESULT
|
| 125 |
+
# ========================================
|
| 126 |
+
|
| 127 |
+
result = f"""
|
| 128 |
+
Sentiment: {sentiment}
|
| 129 |
+
|
| 130 |
+
Positive words detected: {positive_count}
|
| 131 |
+
Negative words detected: {negative_count}
|
| 132 |
+
|
| 133 |
+
Positive matches:
|
| 134 |
+
{', '.join(positive_matches) if positive_matches else 'None'}
|
| 135 |
+
|
| 136 |
+
Negative matches:
|
| 137 |
+
{', '.join(negative_matches) if negative_matches else 'None'}
|
| 138 |
+
"""
|
| 139 |
+
|
| 140 |
+
return result
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
# ============================================
|
| 144 |
+
# GRADIO USER INTERFACE
|
| 145 |
+
# ============================================
|
| 146 |
+
|
| 147 |
+
with gr.Blocks() as app:
|
| 148 |
+
|
| 149 |
+
gr.Markdown(
|
| 150 |
+
"""
|
| 151 |
+
# π Rule-Based Sentiment Analysis
|
| 152 |
+
|
| 153 |
+
Analyze the sentiment of a sentence using
|
| 154 |
+
**manually defined rules and keywords**.
|
| 155 |
+
|
| 156 |
+
### Sentiment Categories
|
| 157 |
+
|
| 158 |
+
π **Positive**
|
| 159 |
+
|
| 160 |
+
π **Negative**
|
| 161 |
+
|
| 162 |
+
π **Neutral**
|
| 163 |
+
|
| 164 |
+
---
|
| 165 |
+
|
| 166 |
+
**Note:** This application does not use an AI model,
|
| 167 |
+
machine learning, or an API.
|
| 168 |
+
"""
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
# Text input
|
| 173 |
+
text_input = gr.Textbox(
|
| 174 |
+
label="Enter your sentence",
|
| 175 |
+
placeholder="Example: I really love this application!",
|
| 176 |
+
lines=4
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
# Analyze button
|
| 181 |
+
analyze_button = gr.Button(
|
| 182 |
+
"Analyze Sentiment",
|
| 183 |
+
variant="primary"
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
# Output
|
| 188 |
+
output = gr.Textbox(
|
| 189 |
+
label="Analysis Result",
|
| 190 |
+
lines=10
|
| 191 |
+
)
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
# Button action
|
| 195 |
+
analyze_button.click(
|
| 196 |
+
fn=sentiment_analyzer,
|
| 197 |
+
inputs=text_input,
|
| 198 |
+
outputs=output
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
# Clear button
|
| 203 |
+
clear_button = gr.ClearButton(
|
| 204 |
+
components=[text_input, output],
|
| 205 |
+
value="Clear"
|
| 206 |
+
)
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
# ============================================
|
| 210 |
+
# LAUNCH APPLICATION
|
| 211 |
+
# ============================================
|
| 212 |
+
|
| 213 |
+
app.launch(
|
| 214 |
+
share=True
|
| 215 |
+
)
|