You can download this code by clicking the button below.
This code is now available for download.
This function uses the TextBlob library to analyze the sentiment of a given text and returns the sentiment polarity (positive, negative, or neutral) of the text.
Technology Stack : Python, TextBlob
Code Type : Python Function
Code Difficulty : Intermediate
import random
import fire
import pandas as pd
def analyze_sentiment(text):
"""
Analyze the sentiment of a given text using the TextBlob library.
Args:
text (str): The text to analyze.
Returns:
str: The sentiment of the text ('positive', 'negative', or 'neutral').
"""
from textblob import TextBlob
blob = TextBlob(text)
if blob.sentiment.polarity > 0:
return 'positive'
elif blob.sentiment.polarity < 0:
return 'negative'
else:
return 'neutral'
if __name__ == "__main__":
fire.Fire(analyze_sentiment)