Skip to content

Commit 1e7020d

Browse files
committed
Tone Analyzer: Address comment for content-type position and add/update tests
1 parent d6cde31 commit 1e7020d

3 files changed

Lines changed: 42 additions & 15 deletions

File tree

examples/tone_analyzer_v3.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
from watson_developer_cloud import ToneAnalyzerV3
55

66
tone_analyzer = ToneAnalyzerV3(
7-
username='YOUR SERVICE USERNAME',
8-
password='YOUR SERVICE PASSWORD',
7+
username='6e9af982-31bf-45c4-99b0-51f6b576e433',
8+
password='2dXhAyPQjxD5',
9+
# username='YOUR SERVICE USERNAME',
10+
# password='YOUR SERVICE PASSWORD',
911
version='2016-05-19')
1012

1113
print("\ntone_chat() example 1:\n")
12-
utterances = [{'text': 'I am very happy', 'user': 'glenn'}]
14+
utterances = [{'text': 'I am very happy.', 'user': 'glenn'},
15+
{'text': 'It is a good day.', 'user': 'glenn'}]
1316
print(json.dumps(tone_analyzer.tone_chat(utterances), indent=2))
1417

1518
print("\ntone() example 1:\n")
@@ -19,27 +22,26 @@
1922
print("\ntone() example 2:\n")
2023
with open(join(dirname(__file__),
2124
'../resources/tone-example.json')) as tone_json:
22-
tone = tone_analyzer.tone(json.load(tone_json)['text'],
23-
tones='emotion')
25+
tone = tone_analyzer.tone(json.load(tone_json)['text'], 'emotion')
2426
print(json.dumps(tone, indent=2))
2527

2628
print("\ntone() example 3:\n")
2729
with open(join(dirname(__file__),
2830
'../resources/tone-example.json')) as tone_json:
29-
tone = tone_analyzer.tone(json.load(tone_json)['text'],
30-
content_type='text/plain', tones='emotion')
31+
tone = tone_analyzer.tone(json.load(tone_json)['text'], 'emotion',
32+
True, 'text/plain')
3133
print(json.dumps(tone, indent=2))
3234

3335
print("\ntone() example 4:\n")
3436
with open(join(dirname(__file__),
3537
'../resources/tone-example.json')) as tone_json:
36-
tone = tone_analyzer.tone(json.load(tone_json),
37-
content_type='application/json', tones='emotion')
38+
tone = tone_analyzer.tone(json.load(tone_json), 'emotion',
39+
content_type='application/json', )
3840
print(json.dumps(tone, indent=2))
3941

4042
print("\ntone() example 5:\n")
4143
with open(join(dirname(__file__),
4244
'../resources/tone-example-html.json')) as tone_json:
43-
tone = tone_analyzer.tone(json.load(tone_json)['text'],
44-
content_type='text/html', tones='emotion')
45+
tone = tone_analyzer.tone(json.load(tone_json)['text'], 'emotion',
46+
content_type='text/html')
4547
print(json.dumps(tone, indent=2))

test/test_tone_analyzer_v3.py

100644100755
Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_tone_with_args():
4646
tone_analyzer.tone(tone_text.read(), tones="social", sentences=False)
4747

4848

49-
assert responses.calls[0].request.url.split('?')[0] == tone_url
49+
assert responses.calls[0].request.url.split('?')[0] == tone_url
5050
# Compare args. Order is not deterministic!
5151
actualArgs = {}
5252
for arg in responses.calls[0].request.url.split('?')[1].split('&'):
@@ -55,6 +55,31 @@ def test_tone_with_args():
5555
assert responses.calls[0].response.text == tone_response
5656
assert len(responses.calls) == 1
5757

58+
59+
@responses.activate
60+
## Invoking tone() with some modifiers specified as positional parameters: tones are emotion and social, and sentences is false
61+
def test_tone():
62+
tone_url = 'https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone'
63+
tone_args = '?version=2016-05-19&tones=emotion%2Csocial&sentences=false'
64+
tone_response = None
65+
with open(os.path.join(os.path.dirname(__file__), '../resources/tone-v3-expect1.json')) as response_json:
66+
tone_response = response_json.read()
67+
68+
responses.add(responses.POST, tone_url,
69+
body=tone_response, status=200,
70+
content_type='application/json')
71+
72+
with open(os.path.join(os.path.dirname(__file__), '../resources/personality.txt')) as tone_text:
73+
tone_analyzer = watson_developer_cloud.ToneAnalyzerV3("2016-05-19",
74+
username="username", password="password")
75+
tone_analyzer.tone(tone_text.read(), 'emotion,social', False)
76+
77+
assert responses.calls[0].request.url == tone_url + tone_args
78+
assert responses.calls[0].response.text == tone_response
79+
80+
assert len(responses.calls) == 1
81+
82+
5883
@responses.activate
5984
## Invoking tone_chat()
6085
def test_tone_chat():

watson_developer_cloud/tone_analyzer_v3.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,19 @@ def __init__(self, version, url=default_url, **kwargs):
4343
# tone
4444
#########################
4545

46-
def tone(self, text, content_type='text/plain', tones=None, sentences=None):
46+
def tone(self, text, tones=None, sentences=None, content_type='text/plain'):
4747
"""
4848
The general purpose tone API analyzes the "tone" of input text.
4949
The message is analyzed for several tones (social, emotional, and
5050
writing), with various characteristics derived for each tone.
5151
:param text: The input content to analyze.
52-
:param content_type: The type of the input content: "text/plain"
53-
(the default), "text/html", or "application/json".
5452
:param sentences: If false, sentence-level analysis is omitted; by
5553
default (or if true), each sentence is analyzed.
5654
:param tones: A comma-separated list of one or more of the following
5755
tones for which to analyze the input text, 'social', 'language', and
5856
'emotion'; the default is all tones.
57+
:param content_type: The type of the input content: "text/plain"
58+
(the default), "text/html", or "application/json".
5959
"""
6060
params = {'version': self.version}
6161
if tones is not None:

0 commit comments

Comments
 (0)