Skip to content

Repository files navigation

Sentimental_Analysis_Using_NLP

An NLP-based sentiment analysis project using the Amazon Kindle Reviews dataset, comparing Bag of Words (BoW), TF-IDF, and Word2Vec text representations with Naive Bayes classifiers to analyze Kindle user reviews and predict sentiment.

Sentiment Analysis Using NLP - Kindle Reviews

An end-to-end Natural Language Processing (NLP) and Machine Learning project for analyzing Amazon Kindle customer reviews and predicting their sentiment or rating.

This project explores and compares multiple text representation techniques, including Bag of Words (BoW), TF-IDF, and Word2Vec, combined with machine learning classifiers.

The project also includes a simple Flask web application that allows users to enter custom reviews and generate predictions using the trained models.


Project Overview

Text data cannot be directly processed by traditional machine learning algorithms. Therefore, NLP techniques are used to convert raw Kindle reviews into numerical feature representations.

This project experiments with three different NLP feature extraction approaches:

  1. Bag of Words (BoW)
  2. TF-IDF
  3. Word2Vec

1. Bag of Words (BoW)

Bag of Words represents text based on the frequency of words appearing in a document.

Raw Review
    |
    v
CountVectorizer
    |
    v
Bag of Words Features
    |
    v
Naive Bayes Classifier
    |
    v
Prediction

2. TF-IDF

TF-IDF (Term Frequency-Inverse Document Frequency) represents words based on their importance within a document relative to the entire dataset.

Raw Review
    |
    v
TfidfVectorizer
    |
    v
TF-IDF Features
    |
    v
Naive Bayes Classifier
    |
    v
Prediction

3. Word2Vec

Word2Vec learns dense vector representations of words based on their context.

In this project, a 100-dimensional Word2Vec model is trained using Gensim.

Since each review contains multiple words, the word embeddings of all known words in a review are averaged to generate a fixed-size representation for the complete review.

Raw Review
    |
    v
Tokenization
    |
    v
Word2Vec
    |
    v
100-Dimensional Word Embeddings
    |
    v
Average Word Vectors
    |
    v
100-Dimensional Review Vector
    |
    v
Gaussian Naive Bayes
    |
    v
Prediction

Dataset

The project uses an Amazon Kindle Reviews dataset containing customer reviews and associated ratings.

The dataset contains approximately:

  • 12,000 reviews
  • Review text
  • Customer ratings

The review text is used as the input feature, while the rating or sentiment is used as the target variable.


Machine Learning Pipeline

The overall NLP pipeline is:

Kindle Review
      |
      v
Text Preprocessing
      |
      v
Feature Extraction
      |
      |---- Bag of Words
      |
      |---- TF-IDF
      |
      |---- Word2Vec
      |
      v
Machine Learning Classifier
      |
      v
Prediction

Technologies Used

Programming Language

  • Python

Natural Language Processing

  • Bag of Words
  • TF-IDF
  • Word2Vec
  • Gensim

Machine Learning

  • Scikit-learn
  • Naive Bayes
  • Gaussian Naive Bayes

Data Processing

  • Pandas
  • NumPy

Web Application

  • Flask
  • HTML
  • CSS
  • JavaScript

Word2Vec Implementation

The Word2Vec model converts individual words into 100-dimensional vectors.

To represent an entire review, the vectors of all known words are averaged:

def avg_word2vec(words):
    vectors = [
        model.wv[word]
        for word in words
        if word in model.wv.key_to_index
    ]

    if len(vectors) == 0:
        return np.zeros(model.vector_size)

    return np.mean(vectors, axis=0)

This converts each review into a fixed 100-dimensional feature vector that can be used by a machine learning classifier.


Web Application

The project includes a Flask-based web interface for demonstrating the trained NLP models.

Users can:

  1. Enter a custom Kindle-style product review.
  2. Select an NLP feature extraction technique.
  3. Submit the review for analysis.
  4. Receive a prediction from the trained machine learning model.

The application supports:

  • Bag of Words
  • TF-IDF
  • Word2Vec

Project Structure

Sentimental_Analysis_Using_NLP/
|
|-- app.py
|-- README.md
|-- requirements.txt
|
|-- models/
|   |-- trained model files
|   |-- vectorizer files
|   |-- Word2Vec model
|
|-- templates/
|   |-- index.html
|
|-- static/
|   |-- css/
|   |-- js/
|
|-- notebooks/
|   |-- model training and experimentation
|
|-- data/
    |-- Kindle reviews dataset

The exact structure may vary depending on the local project setup.


Installation

Clone the repository:

git clone <your-repository-url>

Navigate to the project directory:

cd Sentimental_Analysis_Using_NLP

Install the required dependencies:

pip install -r requirements.txt

Running the Application

Start the Flask application:

python app.py

The application will run locally at:

http://127.0.0.1:5000

Open this address in your browser to use the sentiment analysis interface.


Model Comparison

This project compares three different approaches to representing textual data:

Technique Representation Classifier
Bag of Words Sparse word-frequency vectors Naive Bayes
TF-IDF Weighted sparse vectors Naive Bayes
Word2Vec Dense averaged word embeddings Gaussian Naive Bayes

The comparison demonstrates how different NLP feature extraction techniques affect the representation of textual data and machine learning predictions.


Learning Objectives

The main objective of this project is to understand:

  • How raw text is converted into numerical features.
  • How Bag of Words works.
  • How TF-IDF represents word importance.
  • How Word2Vec creates dense word embeddings.
  • How word vectors can be averaged to represent complete documents.
  • How NLP features can be used with machine learning classifiers.
  • How trained ML models can be integrated into a Flask web application.

Future Improvements

Potential improvements include:

  • Advanced text preprocessing and normalization.
  • Hyperparameter tuning.
  • Comparison with Logistic Regression and Support Vector Machines.
  • Using pretrained Word2Vec embeddings.
  • Implementing FastText or GloVe embeddings.
  • Using deep learning models such as LSTM or GRU.
  • Experimenting with transformer-based models such as BERT.
  • Deploying the Flask application to a cloud platform.

Author

Shomay Singh Parihar

Machine Learning and NLP Project

About

An NLP-based sentiment analysis project using the Amazon Kindle Reviews dataset, comparing Bag of Words (BoW), TF-IDF, and Word2Vec text representations with Naive Bayes classifiers to analyze Kindle user reviews and predict sentiment.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages