sentiment.polarity doesn't seem to be working in python

45 Views Asked by At

Trying to generate a sentiment and polarity score for reviews. For some reason, the sentiment score is always negative. blob.sentiment.polarity does not seem to work Defining a new variable polarity_score = blob.polarity also fails.

import pandas as pd
from textblob import TextBlob
import spacy

# Load spaCy model - using _md
nlp = spacy.load('en_core_web_md')

# Function to perform sentiment analysis using spaCy
def predict_sentiment_spacy(text):
    try:
        # Tokenize the text
        doc = nlp(text)

        # Calculate sentiment score
        sentiment_score = doc.sentiment

        # Attach a label to the sentiment 
        sentiment = 'Positive' if sentiment_score >= 0.5 else 'Negative'

        return sentiment, sentiment_score
    except Exception as e:
        # Handle exception, such as SSL errors
        print(f"Error in spaCy sentiment analysis: {e}")
        return 'Error', None

# Function to perform sentiment analysis using TextBlob
def predict_sentiment_textblob(text):
    try:
        # Create a TextBlob object
        blob = TextBlob(text)

        # Get sentiment polarity score
        sentiment_score = blob.sentiment.polarity

        # Determine sentiment label
        sentiment = 'Positive' if sentiment_score > 0 else 'Negative' if sentiment_score < 0 else 'Neutral'

        return sentiment, sentiment_score
    except Exception as e:
        # Handle exception
        print(f"Error in TextBlob sentiment analysis: {e}")
        return 'Error', None

# Function to calculate similarity score using spaCy
def calculate_similarity(review1, review2):
    try:
        # Tokenize and preprocess the texts
        doc1 = nlp(review1)
        doc2 = nlp(review2)

        # Calculate similarity score
        similarity_score = doc1.similarity(doc2)

        return similarity_score
    except Exception as e:
        # Handle exception
        print(f"Error in calculating similarity: {e}")
        return None

# Load the data
df = pd.read_csv('amazon_product_reviews.csv')

# Select the 'reviews.text' column
reviews_data = df['reviews.text']

# Remove missing values
clean_data = df.dropna(subset=['reviews.text'])

# Perform sentiment analysis and similarity calculation
compare_more = True

while compare_more:
    try:
        # Get user input for indices
        index1 = int(input("Enter the index of the first review: "))
        index2 = int(input("Enter the index of the second review: "))

        # Get the reviews
        review1 = clean_data.iloc[index1]['reviews.text']
        review2 = clean_data.iloc[index2]['reviews.text']

        # SpaCy Sentiment Analysis
        sentiment_spacy_1, score_spacy_1 = predict_sentiment_spacy(review1)
        print(f"Sentiment for Review {index1}: {sentiment_spacy_1}")

        sentiment_spacy_2, score_spacy_2 = predict_sentiment_spacy(review2)
        print(f"Sentiment for Review {index2}: {sentiment_spacy_2}")

        # TextBlob Sentiment Analysis
        sentiment_textblob_1, score_textblob_1 = predict_sentiment_textblob(review1)
        print(f"TextBlob Sentiment for Review {index1}: {sentiment_textblob_1}")
        print(f"TextBlob Sentiment Score for Review {index1}: {score_textblob_1}")

        sentiment_textblob_2, score_textblob_2 = predict_sentiment_textblob(review2)
        print(f"TextBlob Sentiment for Review {index2}: {sentiment_textblob_2}")
        print(f"TextBlob Sentiment Score for Review {index2}: {score_textblob_2}")

        # Calculate Similarity
        similarity_score = calculate_similarity(review1, review2)
        print(f"Similarity Score between Review {index1} and Review {index2}: {similarity_score}")

    except ValueError:
        print("Invalid index. Please enter valid integer indices.")

    # Ask the user if they want to compare more reviews
    response = input("Do you want to compare more reviews? (yes/no): ").lower()
    compare_more = response == 'yes'

# Print sentiment analysis for all reviews
print("\nSentiment Analysis for All Reviews:")
for index, review in enumerate(clean_data['reviews.text']):
    sentiment_spacy, _ = predict_sentiment_spacy(review)
    sentiment_textblob, score_textblob = predict_sentiment_textblob(review)
    print(f"Review {index + 1}:")
    print(f"- SpaCy Predicted Sentiment: {sentiment_spacy}")
    print(f"- TextBlob Predicted Sentiment: {sentiment_textblob}")
    print(f"- TextBlob Sentiment Score: {score_textblob}")
    print('-' * 50)

type here

Tried to create a separate variable

polarity_score = blog.polarity

For some reason, polarity_score cannot be defined inside the function.

0

There are 0 best solutions below