Skip to content

Repository files navigation

Text Classification Project - Sentiment Analysis

A complete Python implementation of text classification using TF-IDF vectorization and machine learning models for sentiment analysis.

Project Overview

This project demonstrates a comprehensive text classification pipeline that includes:

  • Data preprocessing and text cleaning
  • TF-IDF feature extraction
  • Multiple machine learning model training and evaluation
  • Performance metrics and model comparison

Features

  • Text Preprocessing: Tokenization, normalization, stop-word removal, stemming
  • Feature Extraction: TF-IDF vectorization with unigrams and bigrams
  • Multiple Models: Logistic Regression, Naive Bayes, and SVM
  • Comprehensive Evaluation: Accuracy, Precision, Recall, F1-Score
  • Visualization: Classification report and confusion matrix
  • Self-contained: Includes sample dataset within the code

Dataset

The project uses a built-in sample dataset with 40 text samples for sentiment analysis:

  • 20 positive sentiment examples
  • 20 negative sentiment examples
  • Covers various domains: movies, products, restaurants, weather, etc.

Requirements

Install the required packages using:

pip install -r requirements.txt

Required packages:

  • numpy>=1.21.0
  • pandas>=1.3.0
  • scikit-learn>=1.0.0
  • nltk>=3.6.0

Usage

Run the complete text classification pipeline:

python text_classification_project.py

The script will:

  1. Download required NLTK data automatically
  2. Load and preprocess the dataset
  3. Create TF-IDF features
  4. Train multiple machine learning models
  5. Evaluate and compare model performance
  6. Demonstrate predictions on new text samples

Project Structure

text_classification_project.py  # Main implementation file
requirements.txt               # Package dependencies
README.md                     # Project documentation

Functions

Core Functions

  • load_dataset(): Creates and returns the sample dataset
  • preprocess_text(): Performs comprehensive text preprocessing
  • create_corpus_and_features(): Converts text to TF-IDF features
  • train_model(): Trains machine learning models
  • evaluate_model(): Evaluates model performance with detailed metrics
  • predict_sentiment(): Predicts sentiment for new text samples

Text Preprocessing Steps

  1. Normalization: Convert to lowercase
  2. Cleaning: Remove punctuation, numbers, and special characters
  3. Tokenization: Split text into individual words
  4. Stop-word Removal: Remove common English stop words
  5. Stemming: Reduce words to their root form

Models Implemented

  1. Logistic Regression: Linear model with regularization
  2. Naive Bayes: Multinomial Naive Bayes classifier
  3. Support Vector Machine: Linear SVM classifier

Sample Output

The script provides detailed evaluation metrics for each model:

EVALUATION RESULTS FOR LOGISTIC REGRESSION
============================================================
Accuracy:  0.9167
Precision: 0.9000
Recall:    1.0000
F1-Score:  0.9474

DETAILED CLASSIFICATION REPORT:
              precision    recall  f1-score   support

    negative       1.00      0.83      0.91         6
    positive       0.90      1.00      0.95         6

    accuracy                           0.92        12
   macro avg       0.95      0.92      0.93        12
weighted avg       0.95      0.92      0.93        12

CONFUSION MATRIX:
                Predicted
Actual      negative  positive
negative           5         1
positive           0         6

Model Performance Comparison

The script automatically identifies the best performing model based on F1-Score and provides a comparison of all three models.

Customization

Adding New Data

Replace the dataset in load_dataset() function with your own text samples.

Preprocessing Options

Modify preprocess_text() function to:

  • Switch between stemming and lemmatization
  • Adjust tokenization parameters
  • Add custom preprocessing steps

Model Parameters

Tune model parameters in train_model() function for better performance.

Feature Engineering

Modify TF-IDF parameters in create_corpus_and_features():

  • Adjust max_features for vocabulary size
  • Change ngram_range for different n-gram combinations
  • Modify min_df and max_df for document frequency filtering

Future Enhancements

  • Add deep learning models (LSTM, BERT)
  • Implement cross-validation
  • Add more preprocessing options
  • Include feature importance analysis
  • Support for multi-class classification
  • Web interface for real-time predictions

License

This project is open source and available under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages