Skip to content

Instantly share code, notes, and snippets.

@devStepsize
Last active April 22, 2016 23:07
Show Gist options
  • Save devStepsize/ab9cadfbac4d46ea4c5689797faba49f to your computer and use it in GitHub Desktop.
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
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