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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import seaborn as sns
|
| 5 |
+
import nltk
|
| 6 |
+
import re
|
| 7 |
+
import string
|
| 8 |
+
|
| 9 |
+
from nltk.corpus import stopwords
|
| 10 |
+
from nltk.stem import WordNetLemmatizer
|
| 11 |
+
from sklearn.model_selection import train_test_split
|
| 12 |
+
from sklearn.pipeline import Pipeline
|
| 13 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 14 |
+
from sklearn.linear_model import LogisticRegression
|
| 15 |
+
from sklearn.metrics import (
|
| 16 |
+
accuracy_score,
|
| 17 |
+
classification_report,
|
| 18 |
+
confusion_matrix,
|
| 19 |
+
f1_score,
|
| 20 |
+
precision_score,
|
| 21 |
+
recall_score,
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
st.set_page_config(
|
| 25 |
+
page_title="Multi-Class Sentiment Classifier",
|
| 26 |
+
page_icon="π¬",
|
| 27 |
+
layout="wide",
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
@st.cache_resource
|
| 31 |
+
def download_nltk_data():
|
| 32 |
+
for package in ("stopwords", "wordnet", "omw-1.4"):
|
| 33 |
+
nltk.download(package, quiet=True)
|
| 34 |
+
return set(stopwords.words("english")), WordNetLemmatizer()
|
| 35 |
+
|
| 36 |
+
STOP_WORDS, LEMMATIZER = download_nltk_data()
|
| 37 |
+
|
| 38 |
+
def preprocess_text(text):
|
| 39 |
+
text = str(text).lower()
|
| 40 |
+
text = re.sub(r"http\S+|www\.\S+", " ", text)
|
| 41 |
+
text = re.sub(r"\S+@\S+", " ", text)
|
| 42 |
+
text = re.sub(r"\d+", " ", text)
|
| 43 |
+
text = text.translate(str.maketrans("", "", string.punctuation))
|
| 44 |
+
words = re.findall(r"\b[a-z]+\b", text)
|
| 45 |
+
words = [
|
| 46 |
+
LEMMATIZER.lemmatize(word)
|
| 47 |
+
for word in words
|
| 48 |
+
if word not in STOP_WORDS and len(word) > 1
|
| 49 |
+
]
|
| 50 |
+
return " ".join(words)
|
| 51 |
+
|
| 52 |
+
@st.cache_data
|
| 53 |
+
def sample_dataset():
|
| 54 |
+
examples = {
|
| 55 |
+
"positive": [
|
| 56 |
+
"I absolutely love this product, it works wonderfully",
|
| 57 |
+
"The service was excellent and the staff were helpful",
|
| 58 |
+
"This is a fantastic experience, I am very happy",
|
| 59 |
+
"The movie was brilliant and entertaining",
|
| 60 |
+
"I am pleased with the quality and value",
|
| 61 |
+
"The food tasted amazing and fresh",
|
| 62 |
+
"Everything worked perfectly and I recommend it",
|
| 63 |
+
"The delivery was quick and the package was great",
|
| 64 |
+
"The support team solved my issue quickly",
|
| 65 |
+
"I had a wonderful day and feel great",
|
| 66 |
+
"The app is easy to use and very useful",
|
| 67 |
+
"Beautiful design and impressive performance",
|
| 68 |
+
"I enjoyed every moment of the event",
|
| 69 |
+
"The results are better than I expected",
|
| 70 |
+
"Excellent quality, friendly service, and fair price",
|
| 71 |
+
"This book is inspiring and well written",
|
| 72 |
+
"The update made the software much better",
|
| 73 |
+
"I am satisfied with my purchase",
|
| 74 |
+
"A lovely place with a welcoming atmosphere",
|
| 75 |
+
"The course is helpful and easy to understand",
|
| 76 |
+
"The new feature saves time and works well",
|
| 77 |
+
"The staff were polite and made us feel welcome",
|
| 78 |
+
"The trip was comfortable and memorable",
|
| 79 |
+
"The headphones sound clear and feel comfortable",
|
| 80 |
+
"I would happily buy this again",
|
| 81 |
+
],
|
| 82 |
+
"negative": [
|
| 83 |
+
"I hate this product, it is terrible",
|
| 84 |
+
"The service was awful and the staff were rude",
|
| 85 |
+
"This was a disappointing experience",
|
| 86 |
+
"The movie was boring and poorly made",
|
| 87 |
+
"I am unhappy with the quality",
|
| 88 |
+
"The food tasted stale and unpleasant",
|
| 89 |
+
"Nothing worked and I cannot recommend it",
|
| 90 |
+
"The delivery was late and the package was damaged",
|
| 91 |
+
"The support team ignored my problem",
|
| 92 |
+
"I had a horrible day and feel upset",
|
| 93 |
+
"The app is confusing and keeps crashing",
|
| 94 |
+
"Poor design and disappointing performance",
|
| 95 |
+
"I regret attending this event",
|
| 96 |
+
"The results were much worse than expected",
|
| 97 |
+
"Bad quality, rude service, and high price",
|
| 98 |
+
"This book is dull and badly written",
|
| 99 |
+
"The update made the software worse",
|
| 100 |
+
"I am dissatisfied with my purchase",
|
| 101 |
+
"A noisy place with an unpleasant atmosphere",
|
| 102 |
+
"The course is confusing and not helpful",
|
| 103 |
+
"The new feature wastes time and fails often",
|
| 104 |
+
"The staff were impolite and unwelcoming",
|
| 105 |
+
"The trip was uncomfortable and stressful",
|
| 106 |
+
"The headphones sound poor and hurt my ears",
|
| 107 |
+
"I would never buy this again",
|
| 108 |
+
],
|
| 109 |
+
"neutral": [
|
| 110 |
+
"The product arrived in a standard box",
|
| 111 |
+
"The service started at nine in the morning",
|
| 112 |
+
"I watched the movie yesterday",
|
| 113 |
+
"The package contains three separate items",
|
| 114 |
+
"The food was served on a white plate",
|
| 115 |
+
"The app has five tabs on the main screen",
|
| 116 |
+
"The delivery took two days",
|
| 117 |
+
"The support team replied to my message",
|
| 118 |
+
"I attended the event on Friday",
|
| 119 |
+
"The book has twelve chapters",
|
| 120 |
+
"The software update was released today",
|
| 121 |
+
"I purchased the item last week",
|
| 122 |
+
"The restaurant is located near the station",
|
| 123 |
+
"The course includes six lessons",
|
| 124 |
+
"The feature is available in the settings menu",
|
| 125 |
+
"The staff asked me to wait outside",
|
| 126 |
+
"The trip lasted around four hours",
|
| 127 |
+
"The headphones come with a charging cable",
|
| 128 |
+
"The meeting is scheduled for tomorrow",
|
| 129 |
+
"The report contains several sections",
|
| 130 |
+
"The website has a search bar",
|
| 131 |
+
"The order includes a receipt",
|
| 132 |
+
"The room has two windows",
|
| 133 |
+
"The class begins at ten o'clock",
|
| 134 |
+
"The device is available in black and white",
|
| 135 |
+
],
|
| 136 |
+
}
|
| 137 |
+
return pd.DataFrame(
|
| 138 |
+
[{"text": text, "sentiment": label}
|
| 139 |
+
for label, sentences in examples.items()
|
| 140 |
+
for text in sentences]
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
@st.cache_resource
|
| 144 |
+
def train_model(data):
|
| 145 |
+
df = data.copy()
|
| 146 |
+
df["clean_text"] = df["text"].apply(preprocess_text)
|
| 147 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
| 148 |
+
df["clean_text"],
|
| 149 |
+
df["sentiment"],
|
| 150 |
+
test_size=0.25,
|
| 151 |
+
random_state=42,
|
| 152 |
+
stratify=df["sentiment"],
|
| 153 |
+
)
|
| 154 |
+
model = Pipeline([
|
| 155 |
+
("tfidf", TfidfVectorizer(ngram_range=(1, 2), sublinear_tf=True)),
|
| 156 |
+
("classifier", LogisticRegression(max_iter=1000, class_weight="balanced")),
|
| 157 |
+
])
|
| 158 |
+
model.fit(X_train, y_train)
|
| 159 |
+
predictions = model.predict(X_test)
|
| 160 |
+
metrics = {
|
| 161 |
+
"accuracy": accuracy_score(y_test, predictions),
|
| 162 |
+
"f1": f1_score(y_test, predictions, average="macro", zero_division=0),
|
| 163 |
+
"precision": precision_score(y_test, predictions, average="macro", zero_division=0),
|
| 164 |
+
"recall": recall_score(y_test, predictions, average="macro", zero_division=0),
|
| 165 |
+
"y_test": y_test,
|
| 166 |
+
"predictions": predictions,
|
| 167 |
+
"labels": sorted(df["sentiment"].unique()),
|
| 168 |
+
"report": classification_report(
|
| 169 |
+
y_test, predictions, output_dict=True, zero_division=0
|
| 170 |
+
),
|
| 171 |
+
}
|
| 172 |
+
return model, metrics
|
| 173 |
+
|
| 174 |
+
st.title("π¬ Intelligent Multi-Class Sentiment Classifier")
|
| 175 |
+
st.write(
|
| 176 |
+
"Classify text as **positive**, **negative**, or **neutral** using text "
|
| 177 |
+
"preprocessing, TF-IDF features, and a Scikit-learn machine learning model."
|
| 178 |
+
)
|
| 179 |
+
|
| 180 |
+
with st.sidebar:
|
| 181 |
+
st.header("About the model")
|
| 182 |
+
st.write("**Algorithm:** Logistic Regression")
|
| 183 |
+
st.write("**Text features:** TF-IDF with unigrams and bigrams")
|
| 184 |
+
st.write("**Preprocessing:** lowercasing, punctuation and stop-word removal, lemmatization")
|
| 185 |
+
st.info(
|
| 186 |
+
"This demo trains on a small built-in sample dataset. It is intended for "
|
| 187 |
+
"demonstration and coursework, not as a production-grade sentiment model."
|
| 188 |
+
)
|
| 189 |
+
|
| 190 |
+
data = sample_dataset()
|
| 191 |
+
model, metrics = train_model(data)
|
| 192 |
+
|
| 193 |
+
tab_predict, tab_evaluate, tab_data = st.tabs(
|
| 194 |
+
["π Predict Sentiment", "π Model Evaluation", "ποΈ Dataset"]
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
with tab_predict:
|
| 198 |
+
st.subheader("Try your own text")
|
| 199 |
+
user_text = st.text_area(
|
| 200 |
+
"Enter a sentence or review",
|
| 201 |
+
placeholder="Example: The product is excellent and easy to use!",
|
| 202 |
+
height=130,
|
| 203 |
+
)
|
| 204 |
+
if st.button("Analyze sentiment", type="primary"):
|
| 205 |
+
if not user_text.strip():
|
| 206 |
+
st.warning("Please enter some text first.")
|
| 207 |
+
else:
|
| 208 |
+
cleaned = preprocess_text(user_text)
|
| 209 |
+
if not cleaned.strip():
|
| 210 |
+
st.warning("After preprocessing, no useful words remain. Try a longer sentence.")
|
| 211 |
+
else:
|
| 212 |
+
prediction = model.predict([cleaned])[0]
|
| 213 |
+
probabilities = model.predict_proba([cleaned])[0]
|
| 214 |
+
probability_map = dict(zip(model.classes_, probabilities))
|
| 215 |
+
icons = {"positive": "π", "negative": "π", "neutral": "π"}
|
| 216 |
+
st.markdown(f"### {icons.get(prediction, 'π¬')} Predicted sentiment: **{prediction.title()}**")
|
| 217 |
+
st.progress(float(probability_map[prediction]))
|
| 218 |
+
st.write(f"Model confidence for this class: **{probability_map[prediction]:.1%}**")
|
| 219 |
+
st.caption("Confidence is the model's estimated probability, not a guarantee.")
|
| 220 |
+
st.write("Class probabilities")
|
| 221 |
+
probability_df = pd.DataFrame({
|
| 222 |
+
"Sentiment": list(probability_map.keys()),
|
| 223 |
+
"Probability": list(probability_map.values()),
|
| 224 |
+
}).sort_values("Probability", ascending=False)
|
| 225 |
+
st.bar_chart(probability_df.set_index("Sentiment"))
|
| 226 |
+
|
| 227 |
+
with st.expander("See preprocessed text"):
|
| 228 |
+
st.write(cleaned)
|
| 229 |
+
|
| 230 |
+
with tab_evaluate:
|
| 231 |
+
st.subheader("Held-out test set performance")
|
| 232 |
+
st.caption("Metrics are calculated on a stratified test split of the built-in sample dataset.")
|
| 233 |
+
c1, c2, c3, c4 = st.columns(4)
|
| 234 |
+
c1.metric("Accuracy", f"{metrics['accuracy']:.1%}")
|
| 235 |
+
c2.metric("Macro F1-score", f"{metrics['f1']:.3f}")
|
| 236 |
+
c3.metric("Macro precision", f"{metrics['precision']:.3f}")
|
| 237 |
+
c4.metric("Macro recall", f"{metrics['recall']:.3f}")
|
| 238 |
+
|
| 239 |
+
st.markdown("#### Classification report")
|
| 240 |
+
report_df = pd.DataFrame(metrics["report"]).T
|
| 241 |
+
st.dataframe(report_df.round(3), use_container_width=True)
|
| 242 |
+
|
| 243 |
+
st.markdown("#### Confusion matrix")
|
| 244 |
+
cm = confusion_matrix(metrics["y_test"], metrics["predictions"], labels=metrics["labels"])
|
| 245 |
+
fig, ax = plt.subplots(figsize=(6, 4))
|
| 246 |
+
sns.heatmap(
|
| 247 |
+
cm,
|
| 248 |
+
annot=True,
|
| 249 |
+
fmt="d",
|
| 250 |
+
cmap="Blues",
|
| 251 |
+
xticklabels=metrics["labels"],
|
| 252 |
+
yticklabels=metrics["labels"],
|
| 253 |
+
ax=ax,
|
| 254 |
+
)
|
| 255 |
+
ax.set_xlabel("Predicted label")
|
| 256 |
+
ax.set_ylabel("True label")
|
| 257 |
+
ax.set_title("Confusion Matrix")
|
| 258 |
+
st.pyplot(fig)
|
| 259 |
+
plt.close(fig)
|
| 260 |
+
|
| 261 |
+
with tab_data:
|
| 262 |
+
st.subheader("Sample dataset")
|
| 263 |
+
st.write(f"Dataset contains **{len(data)} text examples** across three classes.")
|
| 264 |
+
st.dataframe(data, use_container_width=True, hide_index=True)
|
| 265 |
+
st.markdown("#### Number of examples per class")
|
| 266 |
+
st.bar_chart(data["sentiment"].value_counts())
|
| 267 |
+
csv = data.to_csv(index=False).encode("utf-8")
|
| 268 |
+
st.download_button(
|
| 269 |
+
"Download sample dataset as CSV",
|
| 270 |
+
data=csv,
|
| 271 |
+
file_name="sentiment_dataset.csv",
|
| 272 |
+
mime="text/csv",
|
| 273 |
+
)
|
| 274 |
+
|
| 275 |
+
st.divider()
|
| 276 |
+
st.caption("Task 2 β’ NLP Text Classification β’ Built with Python, Streamlit, NLTK and Scikit-learn")
|