Random Sentence Based on Sentiment Polarity

  • Share this:

Code introduction


This function takes three arguments, uses the TextBlob library to analyze the sentiment polarity of the first argument, and returns the second or third argument based on the polarity of the sentiment. If the sentiment polarity is 0, it returns 'Neutral'.


Technology Stack : TextBlob

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random

def random_sentence(arg1, arg2, arg3):
    from textblob import TextBlob
    blob = TextBlob(arg1)
    sentence = blob.sentiment.polarity
    if sentence > 0:
        return arg2
    elif sentence < 0:
        return arg3
    else:
        return "Neutral"                
              
Tags: