Skip to content

Commit d6cde31

Browse files
committed
Tone Analyzer: Add support for JSON and HTML input to tone() method
1 parent 272412e commit d6cde31

4 files changed

Lines changed: 73 additions & 20 deletions

File tree

examples/tone_analyzer_v3.py

100644100755
Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,45 @@
11
import json
2+
import os
3+
from os.path import join, dirname
24
from watson_developer_cloud import ToneAnalyzerV3
35

4-
56
tone_analyzer = ToneAnalyzerV3(
67
username='YOUR SERVICE USERNAME',
78
password='YOUR SERVICE PASSWORD',
8-
version='2016-02-11')
9-
10-
print(json.dumps(tone_analyzer.tone(text='I am very happy'), indent=2))
9+
version='2016-05-19')
1110

11+
print("\ntone_chat() example 1:\n")
1212
utterances = [{'text': 'I am very happy', 'user': 'glenn'}]
1313
print(json.dumps(tone_analyzer.tone_chat(utterances), indent=2))
14+
15+
print("\ntone() example 1:\n")
16+
print(json.dumps(tone_analyzer.tone(text='I am very happy. It is a good day.'),
17+
indent=2))
18+
19+
print("\ntone() example 2:\n")
20+
with open(join(dirname(__file__),
21+
'../resources/tone-example.json')) as tone_json:
22+
tone = tone_analyzer.tone(json.load(tone_json)['text'],
23+
tones='emotion')
24+
print(json.dumps(tone, indent=2))
25+
26+
print("\ntone() example 3:\n")
27+
with open(join(dirname(__file__),
28+
'../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+
print(json.dumps(tone, indent=2))
32+
33+
print("\ntone() example 4:\n")
34+
with open(join(dirname(__file__),
35+
'../resources/tone-example.json')) as tone_json:
36+
tone = tone_analyzer.tone(json.load(tone_json),
37+
content_type='application/json', tones='emotion')
38+
print(json.dumps(tone, indent=2))
39+
40+
print("\ntone() example 5:\n")
41+
with open(join(dirname(__file__),
42+
'../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+
print(json.dumps(tone, indent=2))

resources/tone-example-html.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"text": "<p>Team, I know that times are tough!</p> <p>Product sales have been disappointing for the past three quarters.</p> <p>We have a competitive product, but <b>we need to do a better job of selling it!</b></p>"
3+
}

resources/tone-example.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"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!"
3+
}

watson_developer_cloud/tone_analyzer_v3.py

100644100755
Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
from .watson_developer_cloud_service import WatsonDeveloperCloudService
3030

31-
3231
class ToneAnalyzerV3(WatsonDeveloperCloudService):
3332
"""Client for the ToneAnalyzer service."""
3433

@@ -44,27 +43,43 @@ def __init__(self, version, url=default_url, **kwargs):
4443
# tone
4544
#########################
4645

47-
def tone(self, text, tones=None, sentences=None):
46+
def tone(self, text, content_type='text/plain', tones=None, sentences=None):
4847
"""
49-
The tone API is the main API call: it analyzes the "tone" of a piece
50-
of text. The message is analyzed from
51-
several tones (social tone, emotional tone, writing tone), and for
52-
each of them various traits are derived
53-
(such as conscientiousness, agreeableness, openness).
54-
:param text: Text to analyze
55-
:param sentences: If "false", sentence-level analysis is omitted
56-
:param tones: Can be one or more of 'social', 'language', 'emotion';
57-
comma-separated.
48+
The general purpose tone API analyzes the "tone" of input text.
49+
The message is analyzed for several tones (social, emotional, and
50+
writing), with various characteristics derived for each tone.
51+
: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".
54+
:param sentences: If false, sentence-level analysis is omitted; by
55+
default (or if true), each sentence is analyzed.
56+
:param tones: A comma-separated list of one or more of the following
57+
tones for which to analyze the input text, 'social', 'language', and
58+
'emotion'; the default is all tones.
5859
"""
5960
params = {'version': self.version}
6061
if tones is not None:
6162
params['tones'] = tones
6263
if sentences is not None:
63-
params['sentences'] = str(
64-
sentences).lower() # Cast boolean to "false" / "true"
65-
data = {'text': text}
66-
return self.request(method='POST', url='/v3/tone', params=params,
67-
json=data, accept_json=True)
64+
# Cast boolean to "false" / "true"
65+
params['sentences'] = str(sentences).lower()
66+
if content_type == 'text/plain':
67+
text = {'text': text}
68+
content_type = 'application/json'
69+
headers = {'content-type': content_type}
70+
71+
if content_type == 'application/json':
72+
return self.request(
73+
method='POST', headers=headers, url='/v3/tone', params=params,
74+
json=text, accept_json=True)
75+
else:
76+
return self.request(
77+
method='POST', headers=headers, url='/v3/tone', params=params,
78+
data=text, accept_json=True)
79+
80+
#########################
81+
# tone_chat
82+
#########################
6883

6984
def tone_chat(self, utterances):
7085
"""

0 commit comments

Comments
 (0)