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.
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.
- 🧹 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
- Python
- Pandas – Dataset handling
- NLTK – Natural Language Processing
- Scikit-learn – Machine Learning
- Regex – Text cleaning
- TF-IDF – Feature extraction
- Multinomial Naive Bayes – Classification
Twitter-Sentiment-Analysis/
│
├── Sentiment Analysis.py
├── twitter_sentiment_modified.csv
└── README.md
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
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.
Each tweet goes through several preprocessing steps:
- Convert text to lowercase
- Remove URLs
- Remove user mentions
- Remove hashtag symbols
- Remove numbers
- Remove punctuation
- Remove English stopwords
- Perform lemmatization
Example:
Original:
"I absolutely LOVE this product! https://example.com"
After preprocessing:
"absolutely love product"
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.
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.
The model is evaluated using:
Measures the proportion of correctly classified tweets.
Provides:
- Precision
- Recall
- F1-score
- Support
Shows how many tweets were correctly and incorrectly classified for each sentiment category.
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.
Clone the repository:
git clone <your-repository-url>
cd Twitter-Sentiment-AnalysisInstall the required libraries:
pip install pandas nltk scikit-learnDownload the required NLTK resources:
import nltk
nltk.download('stopwords')
nltk.download('wordnet')
nltk.download('omw-1.4')Make sure Sentiment Analysis.py and twitter_sentiment_modified.csv are in the same directory.
Then run:
python "Sentiment Analysis.py"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
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
Mega Satish
Built as a beginner-friendly NLP and Machine Learning project to explore sentiment analysis and text classification.