Thursday, February 13, 2025

AI-Powered Sentiment Analyzer using Python


Building a Simple AI-Powered Sentiment Analyzer in Python

Artificial Intelligence (AI) is transforming how we analyze text, making it easier to understand emotions behind words. In this post, we'll build a basic Sentiment Analyzer using Python and scikit-learn. This is a beginner-friendly project, perfect for those starting their AI journey.

πŸ“Œ What is Sentiment Analysis?

Sentiment analysis is a process where AI determines whether a given piece of text expresses a positive, negative, or neutral sentiment. Businesses use sentiment analysis to analyze customer reviews, social media posts, and feedback.

We will use the NaΓ―ve Bayes classifier, a simple but effective algorithm for text classification.

ebook - Unlocking AI: A Simple Guide for Beginners 

 import numpy as np

import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline

# Sample dataset (Students can expand this)
data = {
"text": [
"I love this product!", "This is the worst thing ever.", "Not bad, but could be better.",
"Amazing quality!", "I hate this so much.", "It was okay, nothing special.",
"Absolutely fantastic!", "Terrible experience, never again.", "I enjoyed using this.",
"It's just fine, nothing great."
],
"label": [1, 0, 1, 1, 0, 1, 1, 0, 1, 1] # 1 = Positive, 0 = Negative
}

# Convert to DataFrame
df = pd.DataFrame(data)

# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(df["text"], df["label"], test_size=0.2, random_state=42)

# Create a simple text classification pipeline
model = Pipeline([
("vectorizer", CountVectorizer()),
("classifier", MultinomialNB())
])

# Train the model
model.fit(X_train, y_train)

# Function for user input
def predict_sentiment(text):
prediction = model.predict([text])[0]
return "Positive 😊" if prediction == 1 else "Negative 😑"

# Test with user input
user_input = input("Enter a sentence: ")
print("Sentiment Prediction:", predict_sentiment(user_input))

✅ Pros of This Approach

  1. Simple & Easy to Implement – Works with just a few lines of code.
  2. Fast & Lightweight – Suitable for small datasets.
  3. Interpretable – Easy to understand how it makes decisions.

❌ Cons & Limitations

  1. Limited Vocabulary – Words not seen during training won’t be handled well.
  2. No Context Awareness – Cannot understand sarcasm or complex sentence structures.
  3. Binary Classification Only – No neutral or mixed sentiment detection.

πŸš€ How to Improve Further?

Use More Data – Train on a larger, real-world dataset.
Use TF-IDF Vectorization – Instead of CountVectorizer, TfidfVectorizer improves accuracy.
Try Deep Learning Models – Use LSTMs or transformers (BERT) for advanced sentiment analysis.
Make It Interactive – Use Streamlit to create a simple web app for user input.


It is a great starting point for AI beginners. With minimal effort, we created a working Sentiment Analyzer! While it has limitations, you can extend it further by using more advanced models or pre-trained AI like BERT.

No comments:

Search This Blog