Skip to content

Instantly share code, notes, and snippets.

@stevemar
Last active September 19, 2020 19:03
Show Gist options
  • Save stevemar/c3d692c18fa373b340407a2d3fd02bfd to your computer and use it in GitHub Desktop.
Save stevemar/c3d692c18fa373b340407a2d3fd02bfd to your computer and use it in GitHub Desktop.
Tone Analyzer example
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))
{
"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