Skip to content

Instantly share code, notes, and snippets.

@hsauers5
Created February 19, 2020 00:05
Show Gist options
  • Save hsauers5/c521de698b1087cde1ea9033793815cc to your computer and use it in GitHub Desktop.
Save hsauers5/c521de698b1087cde1ea9033793815cc to your computer and use it in GitHub Desktop.
from textblob import TextBlob
import nltk
nltk.download('movie_reviews')
nltk.download('punkt')
'''
Note that a key weakness of TextBlob is that it was trained on IMDB movie reviews, and will not pick up on everything a human would.
'''
''' returns the sentiment of provided text '''
def get_sentiment_of_text(text='UCF drivers are terrible.'):
text_blob = TextBlob(text)
# polarity is a score ranging from -1.0 to 1.0, where -1 is extremely negative and 1.0 is extremely positive
polarity = text_blob.sentiment.polarity
return polarity
# driver code
if __name__ == '__main__':
my_text = 'UCF drivers are terrible.'
sentiment = get_sentiment_of_text(my_text)
print('Text: ' + my_text)
print('Sentiment score: ' + str(sentiment))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment