This project implements a sentiment analysis system using classical machine learning techniques in Python. It classifies tweets as positive or negative by applying text preprocessing, feature extraction with TF-IDF, and a Linear Support Vector Classifier.
The code performs the following operations:
- Loads a tweet dataset from a CSV file
- Cleans and preprocesses each tweet by:
- removing links, mentions, hashtags, punctuation
- converting text to lowercase
- removing stopwords
- lemmatizing words
- Vectorizes the cleaned text using TF-IDF with n-grams up to trigrams
- Splits the dataset into training and testing sets (80%/20%)
- Trains a calibrated LinearSVC classifier for binary sentiment prediction
- Evaluates the model with a classification report and accuracy score
- Provides a function to predict sentiment probabilities for new text inputs
- Machine Learning Model: Linear Support Vector Classifier (LinearSVC) with probability calibration
- Feature Extraction: TF-IDF vectorizer (unigrams, bigrams, trigrams)
- Preprocessing Steps:
- Lowercasing
- Removing mentions, hashtags, URLs
- Removing non-alphabetic characters
- Removing stopwords
- Lemmatization
- Train/Test Split: 80% training data, 20% testing data
- Evaluation: Classification report and accuracy score
The project expects a CSV dataset named dataSet.csv with the following columns:
| Column | Description |
|---|---|
| target | Sentiment label (0 = negative, 4 = positive) |
| id | |
| date | |
| query | |
| user | |
| text | The tweet text |
| Note: Download the Sentiment140 dataset from Kaggle and rename it to dataSet.csv before using it. |
-
Clone the project:
git clone https://github.com/hafbldprin/SentimentAnalyzer.git
-
Navigate to the project folder:
cd SentimentAnalyzer -
a Run MakeFile:
make
-
b Set up a virtual environment:
python -m venv venv
-
Activate the environment:
-
macOS/Linux:
source venv/bin/activate -
Windows:
venv\Scripts\activate
-
-
Install dependencies:
pip install pandas scikit-learn nltk
-
Place your CSV dataset in this folder and rename it to
dataSet.csv. -
Train the model:
python train.py
-
Run the sentiment analyzer:
python analyzer.py
-
load.py
Loads the CSV dataset, applies filtering and cleaning, and trains the model -
analyzer.py
Runs the trained model and performs sentiment analysis on new input -
preprocess.pyHelper function for cleaing data set -
dataSet.csvHelper function for cleaing data set -
requirements.txtList of dependencies (Packages used by this project) -
Makeup
Auto-run script to train and run the model -
README.md
Description of the project