Skip to content

Commit 7a2af41

Browse files
authored
Merge pull request #198 from watson-developer-cloud/tone-analyzer
Add tone_chat
2 parents de88fe5 + c39072e commit 7a2af41

5 files changed

Lines changed: 96 additions & 9 deletions

File tree

examples/tone_analyzer_v3.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@
88
version='2016-02-11')
99

1010
print(json.dumps(tone_analyzer.tone(text='I am very happy'), indent=2))
11+
12+
utterances = [{'text': 'I am very happy', 'user': 'glenn'}]
13+
print(json.dumps(tone_analyzer.tone_chat(utterances), indent=2))

resources/tone-v3-expect2.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"utterances_tone": [
3+
{
4+
"utterance_id": 0,
5+
"utterance_text": "I am very happy",
6+
"tones": [
7+
{
8+
"score": 0.875529,
9+
"tone_id": "polite",
10+
"tone_name": "polite"
11+
},
12+
{
13+
"score": 0.838693,
14+
"tone_id": "satisfied",
15+
"tone_name": "satisfied"
16+
},
17+
{
18+
"score": 0.844135,
19+
"tone_id": "sympathetic",
20+
"tone_name": "sympathetic"
21+
},
22+
{
23+
"score": 0.916255,
24+
"tone_id": "excited",
25+
"tone_name": "excited"
26+
}
27+
]
28+
}
29+
]
30+
}

test/test_tone_analyzer_v3.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
@responses.activate
77
## Simple test, just calling tone() with some text
8-
def test_success():
8+
def test_tone():
99
tone_url = 'https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone'
1010
tone_args = '?version=2016-05-19'
1111
tone_response = None
12-
with open(os.path.join(os.path.dirname(__file__), '../resources/tone_response.json')) as response_json:
12+
with open(os.path.join(os.path.dirname(__file__), '../resources/tone-v3-expect1.json')) as response_json:
1313
tone_response = response_json.read()
1414

1515
responses.add(responses.POST, tone_url,
@@ -29,11 +29,11 @@ def test_success():
2929

3030
@responses.activate
3131
## Invoking tone() with some modifiers given in 'params': specific tones requested, and sentences skipped
32-
def test_with_args():
32+
def test_tone_with_args():
3333
tone_url = 'https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone'
3434
tone_args = { 'version': '2016-05-19', 'tones': 'social', 'sentences': 'false' }
3535
tone_response = None
36-
with open(os.path.join(os.path.dirname(__file__), '../resources/tone_response.json')) as response_json:
36+
with open(os.path.join(os.path.dirname(__file__), '../resources/tone-v3-expect1.json')) as response_json:
3737
tone_response = response_json.read()
3838

3939
responses.add(responses.POST, tone_url,
@@ -54,3 +54,25 @@ def test_with_args():
5454
assert actualArgs == tone_args
5555
assert responses.calls[0].response.text == tone_response
5656
assert len(responses.calls) == 1
57+
58+
@responses.activate
59+
## Invoking tone_chat()
60+
def test_tone_chat():
61+
tone_url = 'https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone_chat'
62+
tone_args = '?version=2016-05-19'
63+
tone_response = None
64+
with open(os.path.join(os.path.dirname(__file__), '../resources/tone-v3-expect2.json')) as response_json:
65+
tone_response = response_json.read()
66+
67+
responses.add(responses.POST, tone_url,
68+
body=tone_response, status=200,
69+
content_type='application/json')
70+
71+
tone_analyzer = watson_developer_cloud.ToneAnalyzerV3("2016-05-19",
72+
username="username", password="password")
73+
utterances = [{'text': 'I am very happy', 'user': 'glenn'}]
74+
tone_analyzer.tone_chat(utterances)
75+
76+
assert responses.calls[0].request.url == tone_url + tone_args
77+
assert responses.calls[0].response.text == tone_response
78+
assert len(responses.calls) == 1

watson_developer_cloud/tone_analyzer_v3.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2016 IBM All Rights Reserved.
1+
# Copyright 2017 IBM All Rights Reserved.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -13,15 +13,25 @@
1313
# limitations under the License.
1414

1515
"""
16-
The v3 Tone Analyzer service
17-
(https://www.ibm.com/watson/developercloud/tone-analyzer.html)
16+
The IBM Watson Tone Analyzer Service uses linguistic analysis to detect
17+
three types of tones from written text: emotions, social tendencies, and
18+
language style. Emotions identified include things like anger, cheerfulness
19+
and sadness. Identified social tendencies include things from the Big Five
20+
personality traits used by some psychologists. These include openness,
21+
conscientiousness, extraversion, agreeableness, and neuroticism. Identified
22+
language styles include things like confident, analytical, and tentative.
23+
Input email and other written media into the Tone Analyzer service, and use
24+
the results to determine if your writing comes across with the tone,
25+
personality traits, and writing style that you want for your intended
26+
audience.
1827
"""
1928

20-
from watson_developer_cloud.watson_developer_cloud_service import \
21-
WatsonDeveloperCloudService
29+
from .watson_developer_cloud_service import WatsonDeveloperCloudService
2230

2331

2432
class ToneAnalyzerV3(WatsonDeveloperCloudService):
33+
"""Client for the ToneAnalyzer service."""
34+
2535
default_url = 'https://gateway.watsonplatform.net/tone-analyzer/api'
2636
latest_version = '2016-05-19'
2737

@@ -30,6 +40,10 @@ def __init__(self, version, url=default_url, **kwargs):
3040
**kwargs)
3141
self.version = version
3242

43+
#########################
44+
# tone
45+
#########################
46+
3347
def tone(self, text, tones=None, sentences=None):
3448
"""
3549
The tone API is the main API call: it analyzes the "tone" of a piece
@@ -51,3 +65,21 @@ def tone(self, text, tones=None, sentences=None):
5165
data = {'text': text}
5266
return self.request(method='POST', url='/v3/tone', params=params,
5367
json=data, accept_json=True)
68+
69+
def tone_chat(self, utterances):
70+
"""
71+
Analyze customer engagement tone.
72+
73+
Use the Tone Analyzer for Customer Engagement Endpoint to monitor
74+
customer service and customer support conversations.
75+
76+
:param utterances: The content to be analyzed.
77+
"""
78+
params = {'version': self.version}
79+
data = {'utterances': utterances}
80+
return self.request(
81+
method='POST',
82+
url='/v3/tone_chat',
83+
params=params,
84+
json=data,
85+
accept_json=True)

0 commit comments

Comments
 (0)