Question: # Description: This script performs sentiment analysis on the Amazon product reviews dataset using spaCy and TextBlob. # Load spaCy model import spacy from spacytextblob.spacytextblob

# Description: This script performs sentiment analysis on the Amazon product reviews dataset using spaCy and TextBlob.
# Load spaCy model
import spacy
from spacytextblob.spacytextblob import SpacyTextBlob
nlp = spacy.load('en_core_web_sm')
nlp.add_pipe('spacytextblob')
import numpy as np
import pandas as pd
dataframe=pd.read_csv(r"C:\Users\User\OneDrive\Desktop\1429_1.csv")
dataframe.head()
"""Preprocessing the text data"""
# Select 'review.text' column
reviews_data = dataframe['reviews.text']
# Remove missing values
clean_data = dataframe.dropna(subset=['reviews.text'])
# Function to apply preprocessing to the 'reviews.text' column using .loc
def preprocess_text(text):
# Use spaCy to tokenize and remove stopwords
doc = nlp(text)
tokens =[token.text.lower().strip() for token in doc if not token.is_stop]
return ''.join(tokens)
nlp = spacy.load('en_core_web_sm')
dataframe['reviews.text']= dataframe['reviews.text'].apply(preprocess_text)
# Function for sentiment analysis
def analyze_sentiment(review):
# Process the review using spaCy
doc = nlp(review)
# Get sentiment using the .sentiment attribute
sentiment = doc.sentiment
# Determine sentiment category (positive, negative, or neutral)
if sentiment >=0.5:
return 'Positive'
elif sentiment =-0.5:
return 'Negative'
else:
return 'Neutral'
# Text usage
sample_review = "I love this product. It's amazing!"
sentiment_result = analyze_sentiment(sample_review)
print(f"Sentiment: {sentiment_result}")
"""# **Test the model for Sample Model Reviews**"""
# Example usage of the sentiment analysis function
def test_sentiment_analysis(review):
sentiment_result = analyze_sentiment(review)
print(f"Review: {review}")
print(f"Sentiment: {sentiment_result}")
print("="*30)
# Choose two reviews for testing (make sure the indices are valid)
review_index_1=0
review_index_2=1
# Retrieve the reviews using indexing
review_1= dataframe['reviews.text'][review_index_1]
review_2= dataframe['reviews.text'][review_index_2]
# Test the sentiment analysis function on the selected reviews
test_sentiment_analysis(review_1)
test_sentiment_analysis(review_2)
# Compare the similarity of the two reviews using spaCy similarity
similarity_score = nlp(review_1).similarity(nlp(review_2))
print(f"Similarity Score: {similarity_score}")
Error:PROBLEMS OUTPUT DEBUG CONSOLE TERMINAL PORTS
Python
py"
r set low_memory=False.
dataframe=pd.read_csv(r"C: \Users \User\OneDrive \Desktop\1429_1.csv")
Traceback (most recent call last):
dataframe['reviews.text']= dataframe['reviews.text'].apply(preprocess_text)
. apply()
return self.apply_standard()
mapped = obj._map_values (
annanananananan
return algorithms.map_array(arr, mapper, na_action=na_action, convert=convert)
return lib.map_infer(values, mapper, convert=convert)
File "lib.pyx", line 2972, in pandas._libs.lib.map_infer
doc = nlp(text)
1(
doc = self._ensure_doc(text)
raise ValueError(Errors.E1041.format(type=type(doc_like)))
ValueError: [E1041] Expected a string, Doc, or bytes as input, but got: class 'float'>
PS C: VUsers \User> What do these mean and how do I correct these?
 # Description: This script performs sentiment analysis on the Amazon product

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!