Skip to content

Instantly share code, notes, and snippets.

@reddgr
Created July 25, 2024 13:37
Show Gist options
  • Save reddgr/00ae2a3ab8eed7b7a330b0af2131ec70 to your computer and use it in GitHub Desktop.
Save reddgr/00ae2a3ab8eed7b7a330b0af2131ec70 to your computer and use it in GitHub Desktop.
TextBlob for sentiment analysis. Example usage
from textblob import TextBlob
text = "As a large language model I'm proud of my ability to pass butter."
sentiment = TextBlob(text).sentiment
polarity_score = sentiment.polarity # Between -1 (negative) and 1 (positive)
subjectivity_score = sentiment.subjectivity # Between 0 (objective) and 1 (subjective)
print(text)
print(f"Polarity: {polarity_score}")
print(f"Subjectivity: {subjectivity_score}")
text2 = "As a small language model I lack the ability to pass butter."
sentiment2 = TextBlob(text2).sentiment
print(text2)
print(f"Polarity: {sentiment2.polarity}")
print(f"Subjectivity: {sentiment2.subjectivity}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment