Customer Feedback sentiment analysis¶
To perform sentiment analysis on the provided JSON data, we can use Python libraries such as TextBlob or VADER from the nltk library. Below, I'll demonstrate how to use TextBlob to analyze the sentiment of each review.
Explanation: Positive: The review expresses a positive sentiment.
Negative: The review expresses a negative sentiment.
Neutral: The review is neutral or mixed.
This script uses TextBlob to determine the sentiment of each review based on the polarity of the text. You can further customize or enhance the analysis by using more advanced NLP techniques or libraries like VADER or spaCy.
Step 1 Install Required Libraries¶
First, ensure you have the necessary libraries installed. You can install them using pip:
pip install textblob
Requirement already satisfied: textblob in c:\users\jki\anaconda3\lib\site-packages (0.19.0) Requirement already satisfied: nltk>=3.9 in c:\users\jki\anaconda3\lib\site-packages (from textblob) (3.9.1) Requirement already satisfied: click in c:\users\jki\anaconda3\lib\site-packages (from nltk>=3.9->textblob) (8.1.7) Requirement already satisfied: joblib in c:\users\jki\anaconda3\lib\site-packages (from nltk>=3.9->textblob) (1.2.0) Requirement already satisfied: regex>=2021.8.3 in c:\users\jki\anaconda3\lib\site-packages (from nltk>=3.9->textblob) (2023.10.3) Requirement already satisfied: tqdm in c:\users\jki\anaconda3\lib\site-packages (from nltk>=3.9->textblob) (4.65.0) Requirement already satisfied: colorama in c:\users\jki\anaconda3\lib\site-packages (from click->nltk>=3.9->textblob) (0.4.6) Note: you may need to restart the kernel to use updated packages.
Step 2: Perform Sentiment Analysis¶
Here's a Python script that reads the JSON data, performs sentiment analysis on each review, and outputs the results:
from textblob import TextBlob
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
reviews_df = pd.read_json(r"C:\Users\jki\Desktop\NLP Projects\Kaptagat Springs Custyomer Feedback LLM\data\feedback.json")
reviews_df.head(5)
product | review | |
---|---|---|
0 | 18.9 Litres | his is perfect for our office! We don’t have t... |
1 | 18.9 Litres | The bottle is too bulky and hard to move aroun... |
2 | 18.9 Litres | It does the job, but the design could be more ... |
3 | 10 Litres | Great for family use. We use it every day, and... |
4 | 10 Litres | The handle broke after a week. Disappointed wi... |
# Function to perform sentiment analysis
def analyze_sentiment(review):
analysis = TextBlob(review)
if analysis.sentiment.polarity > 0:
return "Positive"
elif analysis.sentiment.polarity < 0:
return "Negative"
else:
return "Neutral"
# Analyze each review
for index, row in reviews_df.iterrows():
product = row["product"]
review = row["review"]
sentiment = analyze_sentiment(review)
print(f"Product: {product}, Review: {review}, Sentiment: {sentiment}")
Product: 18.9 Litres, Review: his is perfect for our office! We don’t have to refill it often, and the quality is great, Sentiment: Positive Product: 18.9 Litres, Review: The bottle is too bulky and hard to move around. Not practical for small spaces, Sentiment: Negative Product: 18.9 Litres, Review: It does the job, but the design could be more modern., Sentiment: Positive Product: 10 Litres, Review: Great for family use. We use it every day, and it’s very durable., Sentiment: Positive Product: 10 Litres, Review: The handle broke after a week. Disappointed with the quality., Sentiment: Negative Product: 10 Litres, Review: It’s okay, but I expected it to be lighter for its size., Sentiment: Positive Product: 5 Litres, Review: Perfect size for camping trips. Easy to carry and lasts for days., Sentiment: Positive Product: 5 Litres, Review: The cap leaks sometimes, which is annoying., Sentiment: Negative Product: 5 Litres, Review: It’s a standard water bottle. Nothing special, but it works., Sentiment: Positive Product: 1.5 Litres, Review: I love this bottle for my daily workouts. It’s lightweight and easy to carry., Sentiment: Positive Product: 1.5 Litres, Review: The plastic smells weird, and I’m not sure if it’s safe to use., Sentiment: Negative Product: 1.5 Litres, Review: It’s fine, but I wish it had a better grip., Sentiment: Positive Product: 1 Litre, Review: I use this bottle at work every day. It’s compact and fits perfectly in my bag., Sentiment: Positive Product: 1 Litre, Review: The bottle cracked after a few weeks. Not durable at all., Sentiment: Negative Product: 1 Litre, Review: It’s a decent bottle, but the design is a bit boring., Sentiment: Negative Product: 500 ml, Review: This is my favorite bottle for hiking. It’s lightweight and fits in my backpack easily., Sentiment: Positive Product: 500 ml, Review: The lid is hard to open, and it leaks sometimes., Sentiment: Negative Product: 500 ml, Review: It’s a good size for kids, but the colors are too plain., Sentiment: Positive Product: 300 ml, Review: Perfect for my child’s lunchbox. It’s small and easy to carry., Sentiment: Positive Product: 300 ml, Review: The bottle is too small for the price. Not worth it., Sentiment: Negative Product: 300 ml, Review: It’s cute, but the material feels cheap., Sentiment: Positive Product: 18.9 Litres, Review: It’s a standard water bottle. Nothing special, but it works, Sentiment: Positive
import json
# Function to perform sentiment analysis
def analyze_sentiment(review):
analysis = TextBlob(review)
if analysis.sentiment.polarity > 0:
return "Positive"
elif analysis.sentiment.polarity < 0:
return "Negative"
else:
return "Neutral"
# Apply sentiment analysis
reviews_df["sentiment"] = reviews_df["review"].apply(analyze_sentiment)
# Convert DataFrame to JSON format
sentiment_json = reviews_df.to_json(orient="records", indent=4)
# Save to a JSON file
output_path = "sentiment_results.json"
with open(output_path, "w") as json_file:
json_file.write(sentiment_json)
print(f"Sentiment data saved successfully to {output_path}")
Sentiment data saved successfully to sentiment_results.json