Last active
April 22, 2016 23:07
-
-
Save devStepsize/ab9cadfbac4d46ea4c5689797faba49f to your computer and use it in GitHub Desktop.
Method to return the given overall sentiment its strength for a given string using the Alchemy API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
def get_sentiment(body): | |
endpoint = "http://gateway-a.watsonplatform.net/calls/text/TextGetTextSentiment" | |
parameters = { | |
'apikey': API_KEY, | |
'outputMode': 'json', | |
'text': body | |
} | |
r = requests.get(endpoint, params=parameters) | |
sentiment = None | |
if r.status_code == 200: | |
resp = r.json() | |
sentiment = resp['docSentiment']['type'] | |
strength = resp['docSentiment'].get('score') | |
return sentiment |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment