Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

# Load necessary libraries import spacy from spacytextblob.spacytextblob import SpacyTextBlob import numpy as np import pandas as pd # Load spaCy model and add textblob

# Load necessary libraries
import spacy
from spacytextblob.spacytextblob import SpacyTextBlob
import numpy as np
import pandas as pd
# Load spaCy model and add textblob pipeline
nlp = spacy.load('en_core_web_sm')
nlp.add_pipe('spacytextblob')
# Read the CSV file
dataframe = pd.read_csv(r"C:\Users\User\OneDrive\Desktop\1429_1.csv", low_memory=False)
# Remove missing values and replace NaN with an empty string
clean_data = dataframe.dropna(subset=['reviews.text']).fillna('')
# Function to preprocess text
def preprocess_text(text):
if isinstance(text, str):
doc = nlp(text)
tokens =[token.text.lower().strip() for token in doc if not token.is_stop]
return ''.join(tokens)
else:
return ''
# Apply preprocessing to the 'reviews.text' column
clean_data['reviews.text']= clean_data['reviews.text'].apply(preprocess_text)
# Function for sentiment analysis
def analyze_sentiment(review):
doc = nlp(review)
sentiment = doc.sentiment.polarity
if sentiment >0.5:
return 'Positive'
elif sentiment -0.5:
return 'Negative'
else:
return 'Neutral'
# Function to test sentiment analysis
def test_sentiment_analysis(review):
sentiment_result = analyze_sentiment(review)
print(f"Review: {review}")
print(f"Sentiment: {sentiment_result}")
print("="*30)
# Test sentiment analysis on sample reviews
sample_review_1= "Love this product. It's amazing!"
sample_review_2= "This product is terrible. I regret buying it."
test_sentiment_analysis(sample_review_1)
test_sentiment_analysis(sample_review_2)
# Choose two reviews for testing
review_index_1=0
review_index_2=1
# Retrieve the reviews using indexing
review_1= clean_data['reviews.text'][review_index_1]
review_2= clean_data['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_score = nlp(review_1).similarity(nlp(review_2))
print(f"Similarity Score: {similarity_score}")
(B) PS C: UUsers \User> & C:/Users/User/AppData/Local/Programs/Python/Python312/python.exe c:/Users/User/Downloads/new.py
Traceback (most recent call last):
test_sentiment_analysis(sample_review_1)
sentiment_result = analyze_sentiment(review)
File "c: \Users\User\Downloads\
new.py", line 32, in analyze_sentiment
sentiment = doc._.sentiment. polarity
raise AttributeError(Errors.E046.format(name=name))
AttributeError: [E046] Can't retrieve unregistered extension attribute 'sentiment'. Did you forgei to call the 'set_extension' method?
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions