Skip to content

Commit 0a42949

Browse files
committed
added back version arg as mandatory; replaced generic 'params' by specific accepted arguments
1 parent 9c24cd0 commit 0a42949

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

test/test_tone_analyzer_v3.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test_success():
1717
content_type='application/json')
1818

1919
with open(os.path.join(os.path.dirname(__file__), '../resources/personality.txt')) as tone_text:
20-
tone_analyzer = watson_developer_cloud.ToneAnalyzerV3(
20+
tone_analyzer = watson_developer_cloud.ToneAnalyzerV3("2016-05-19",
2121
username="username", password="password")
2222
tone_analyzer.tone(tone_text.read())
2323

@@ -40,11 +40,10 @@ def test_with_args():
4040
body=tone_response, status=200,
4141
content_type='application/json')
4242

43-
params = { 'tones': 'social', 'sentences': 'false' }
4443
with open(os.path.join(os.path.dirname(__file__), '../resources/personality.txt')) as tone_text:
45-
tone_analyzer = watson_developer_cloud.ToneAnalyzerV3(
44+
tone_analyzer = watson_developer_cloud.ToneAnalyzerV3("2016-05-19",
4645
username="username", password="password")
47-
tone_analyzer.tone(tone_text.read(), params=params)
46+
tone_analyzer.tone(tone_text.read(), tones="social", sentences=False)
4847

4948

5049
assert responses.calls[0].request.url.split('?')[0] == tone_url

watson_developer_cloud/tone_analyzer_v3.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,25 @@ class ToneAnalyzerV3(WatsonDeveloperCloudService):
2525
default_url = 'https://gateway.watsonplatform.net/tone-analyzer/api'
2626
latest_version = '2016-05-19'
2727

28-
def __init__(self, version=latest_version, url=default_url, username=None, password=None, use_vcap_services=True):
28+
def __init__(self, version, url=default_url, username=None, password=None, use_vcap_services=True):
2929
WatsonDeveloperCloudService.__init__(
3030
self, 'tone_analyzer', url, username, password, use_vcap_services)
3131
self.version = version
3232

33-
def tone(self, text, params=None):
33+
def tone(self, text, tones=None, sentences=None):
3434
"""
3535
The tone API is the main API call: it analyzes the "tone" of a piece of text. The message is analyzed from
3636
several tones (social tone, emotional tone, writing tone), and for each of them various traits are derived
3737
(such as conscientiousness, agreeableness, openness).
3838
:param text: Text to analyze
39+
:param sentences: If "false", sentence-level analysis is omitted
40+
:param tones: Can be one or more of 'social', 'language', 'emotion'; comma-separated.
3941
"""
40-
args = copy.copy(params) if params is not None else {}
41-
args['version'] = self.version
42+
queryArgs = {}
43+
queryArgs['version'] = self.version
44+
if tones is not None:
45+
queryArgs['tones'] = tones
46+
if sentences is not None:
47+
queryArgs['sentences'] = str(sentences).lower() # Cast boolean to "false" / "true"
4248
data = {'text': text}
43-
return self.request(method='POST', url='/v3/tone', params=args, json=data, accept_json=True)
49+
return self.request(method='POST', url='/v3/tone', params=queryArgs, json=data, accept_json=True)

0 commit comments

Comments
 (0)