Last active
September 19, 2020 19:03
-
-
Save stevemar/c3d692c18fa373b340407a2d3fd02bfd to your computer and use it in GitHub Desktop.
Tone Analyzer example
This file contains 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 json | |
from ibm_watson import ToneAnalyzerV3 | |
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator | |
# get this from the service credential | |
authenticator = IAMAuthenticator('t4a0vhxxxxxxxxFjW4') | |
tone_analyzer = ToneAnalyzerV3( | |
version='2017-09-21', | |
authenticator=authenticator | |
) | |
# get this from the service credential | |
tone_analyzer.set_service_url('https://api.us-south.tone-analyzer.watson.cloud.ibm.com/instances/51600xxxxxxxxb8b214') | |
text = 'Team, I know that times are tough! Product '\ | |
'sales have been disappointing for the past three '\ | |
'quarters. We have a competitive product, but we '\ | |
'need to do a better job of selling it!' | |
tone_analysis = tone_analyzer.tone( | |
{'text': text}, | |
content_type='application/json' | |
).get_result() | |
print(json.dumps(tone_analysis, indent=2)) |
This file contains 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
{ | |
"document_tone": { | |
"tones": [ | |
{ | |
"score": 0.6165, | |
"tone_id": "sadness", | |
"tone_name": "Sadness" | |
}, | |
{ | |
"score": 0.829888, | |
"tone_id": "analytical", | |
"tone_name": "Analytical" | |
} | |
] | |
}, | |
"sentences_tone": [ | |
{ | |
"sentence_id": 0, | |
"text": "Team, I know that times are tough!", | |
"tones": [ | |
{ | |
"score": 0.801827, | |
"tone_id": "analytical", | |
"tone_name": "Analytical" | |
} | |
] | |
}, | |
{ | |
"sentence_id": 1, | |
"text": "Product sales have been disappointing for the past three quarters.", | |
"tones": [ | |
{ | |
"score": 0.771241, | |
"tone_id": "sadness", | |
"tone_name": "Sadness" | |
}, | |
{ | |
"score": 0.687768, | |
"tone_id": "analytical", | |
"tone_name": "Analytical" | |
} | |
] | |
}, | |
{ | |
"sentence_id": 2, | |
"text": "We have a competitive product, but we need to do a better job of selling it!", | |
"tones": [ | |
{ | |
"score": 0.506763, | |
"tone_id": "analytical", | |
"tone_name": "Analytical" | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment