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.
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.
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:
- Bag of Words (BoW)
- TF-IDF
- Word2Vec
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
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
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
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.
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
- Python
- Bag of Words
- TF-IDF
- Word2Vec
- Gensim
- Scikit-learn
- Naive Bayes
- Gaussian Naive Bayes
- Pandas
- NumPy
- Flask
- HTML
- CSS
- JavaScript
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.
The project includes a Flask-based web interface for demonstrating the trained NLP models.
Users can:
- Enter a custom Kindle-style product review.
- Select an NLP feature extraction technique.
- Submit the review for analysis.
- Receive a prediction from the trained machine learning model.
The application supports:
- Bag of Words
- TF-IDF
- Word2Vec
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.
Clone the repository:
git clone <your-repository-url>Navigate to the project directory:
cd Sentimental_Analysis_Using_NLPInstall the required dependencies:
pip install -r requirements.txtStart the Flask application:
python app.pyThe application will run locally at:
http://127.0.0.1:5000
Open this address in your browser to use the sentiment analysis interface.
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.
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.
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.
Shomay Singh Parihar
Machine Learning and NLP Project