Skip to content

About

A machine learning-based Twitter Sentiment Analysis project that classifies tweets as Positive, Negative, or Neutral using NLP. It performs text cleaning, stopword removal, lemmatization, TF-IDF feature extraction, and Multinomial Naive Bayes classification, with model evaluation and real-time sentiment prediction.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

Twitter Sentiment Analysis Using NLP

A simple machine learning project that analyzes tweets and classifies their sentiment as Positive, Negative, or Neutral using Natural Language Processing (NLP) and Machine Learning.

Project Overview

This project demonstrates a basic end-to-end NLP pipeline for sentiment classification. Tweets are cleaned and preprocessed before being converted into numerical features using TF-IDF. A Multinomial Naive Bayes classifier is then trained to predict the sentiment of tweets.

The project also provides an interactive command-line interface where users can enter their own tweets and receive a sentiment prediction.

Features

  • 🧹 Tweet text cleaning and preprocessing
  • 🔗 URL and mention removal
  • #️⃣ Hashtag processing
  • 🛑 Stopword removal
  • 🌱 Word lemmatization
  • 📊 TF-IDF feature extraction
  • 🤖 Multinomial Naive Bayes classification
  • 📈 Accuracy and classification report
  • 🔲 Confusion matrix
  • 💬 Real-time sentiment prediction

🛠️ Technologies Used

  • Python
  • Pandas – Dataset handling
  • NLTK – Natural Language Processing
  • Scikit-learn – Machine Learning
  • Regex – Text cleaning
  • TF-IDF – Feature extraction
  • Multinomial Naive Bayes – Classification

📂 Project Structure

Twitter-Sentiment-Analysis/
│
├── Sentiment Analysis.py
├── twitter_sentiment_modified.csv
└── README.md

🔄 Project Workflow

Tweet Dataset
      ↓
Data Loading
      ↓
Missing Value Handling
      ↓
Text Cleaning
      ↓
Stopword Removal
      ↓
Lemmatization
      ↓
TF-IDF Vectorization
      ↓
Train/Test Split
      ↓
Multinomial Naive Bayes
      ↓
Model Evaluation
      ↓
User Input Prediction

📊 Dataset

The dataset contains tweets along with their corresponding sentiment labels.

The dataset uses two columns:

Column Description
Tweet Text of the tweet
Sentiment Sentiment label: Positive, Negative, or Neutral

The current dataset contains 150 tweets, with 50 examples for each sentiment category.

🧹 Text Preprocessing

Each tweet goes through several preprocessing steps:

  1. Convert text to lowercase
  2. Remove URLs
  3. Remove user mentions
  4. Remove hashtag symbols
  5. Remove numbers
  6. Remove punctuation
  7. Remove English stopwords
  8. Perform lemmatization

Example:

Original:
"I absolutely LOVE this product! https://example.com"

After preprocessing:
"absolutely love product"

📊 TF-IDF Feature Extraction

After preprocessing, the text cannot be directly given to the machine learning model.

TF-IDF (Term Frequency–Inverse Document Frequency) is used to convert the cleaned tweets into numerical feature vectors.

vectorizer = TfidfVectorizer(max_features=5000)

X = vectorizer.fit_transform(data["Clean_Tweet"])

These numerical features are then used to train the classifier.

🤖 Machine Learning Model

The project uses Multinomial Naive Bayes:

model = MultinomialNB()
model.fit(X_train, y_train)

Naive Bayes is a popular algorithm for text classification because it works well with high-dimensional word-based features.

📈 Model Evaluation

The model is evaluated using:

Accuracy

Measures the proportion of correctly classified tweets.

Classification Report

Provides:

  • Precision
  • Recall
  • F1-score
  • Support

Confusion Matrix

Shows how many tweets were correctly and incorrectly classified for each sentiment category.

💬 Real-Time Prediction

After training, the program allows users to enter their own tweets:

==============================
Twitter Sentiment Predictor
==============================

Enter a Tweet (or type exit):
I really love this application!

Predicted Sentiment: Positive

Type exit to terminate the program.

⚙️ Installation

Clone the repository:

git clone <your-repository-url>
cd Twitter-Sentiment-Analysis

Install the required libraries:

pip install pandas nltk scikit-learn

Download the required NLTK resources:

import nltk

nltk.download('stopwords')
nltk.download('wordnet')
nltk.download('omw-1.4')

▶️ Run the Project

Make sure Sentiment Analysis.py and twitter_sentiment_modified.csv are in the same directory.

Then run:

python "Sentiment Analysis.py"

🎯 Learning Outcomes

Through this project, you can understand the basic workflow of an NLP machine learning project:

  • Working with textual datasets
  • Cleaning and preprocessing natural language
  • Extracting numerical features from text
  • Using TF-IDF
  • Training a classification model
  • Evaluating machine learning performance
  • Building a simple interactive prediction system

🔮 Future Improvements

Possible improvements include:

  • Use a much larger real-world Twitter dataset
  • Compare multiple NLP classification algorithms
  • Handle emojis and negation more effectively
  • Add hyperparameter tuning
  • Improve preprocessing for social-media text
  • Build a web interface using Flask
  • Deploy the sentiment-analysis application

👩‍💻 Author

Mega Satish

Built as a beginner-friendly NLP and Machine Learning project to explore sentiment analysis and text classification.

About

A machine learning-based Twitter Sentiment Analysis project that classifies tweets as Positive, Negative, or Neutral using NLP. It performs text cleaning, stopword removal, lemmatization, TF-IDF feature extraction, and Multinomial Naive Bayes classification, with model evaluation and real-time sentiment prediction.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages