Sentiment Analysis with TextBlob

  • Share this:

Code introduction


This function takes a text argument, analyzes the sentiment of the text using the TextBlob library, and returns 'Positive', 'Negative', or 'Neutral'.


Technology Stack : TextBlob

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
from textblob import TextBlob

def analyze_sentiment(text):
    blob = TextBlob(text)
    sentiment_score = blob.sentiment.polarity
    if sentiment_score > 0:
        return "Positive"
    elif sentiment_score < 0:
        return "Negative"
    else:
        return "Neutral"                
              
Tags: