Skip to content

Instantly share code, notes, and snippets.

@devStepsize
Last active May 7, 2016 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devStepsize/a215918ceda27dbe9b025fd6d85565b1 to your computer and use it in GitHub Desktop.
Save devStepsize/a215918ceda27dbe9b025fd6d85565b1 to your computer and use it in GitHub Desktop.
Analyse the tone / emotion of a text with this beta IBM Watson service
import requests
creds = {
"url": "https://gateway.watsonplatform.net/tone-analyzer-beta/api",
"password": "your_password",
"username": "your_username"
}
def get_tone(text):
endpoint = "https://gateway.watsonplatform.net/tone-analyzer-beta/api/v3/tone"
params = {
"version": "2016-02-11"
}
text = {"text": text}
headers = {'content-type': 'application/json'}
resp = requests.post(endpoint, json=text, auth=(creds['username'], creds['password']), params=params, headers=headers)
r = resp.json()
return [(e['tone_name'], e['score']) for e in r['document_tone']['tone_categories'][0]['tones']]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment