2828
2929from .watson_developer_cloud_service import WatsonDeveloperCloudService
3030
31-
3231class ToneAnalyzerV3 (WatsonDeveloperCloudService ):
3332 """Client for the ToneAnalyzer service."""
3433
@@ -44,27 +43,48 @@ 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 , tones = None , sentences = None , content_type = 'text/plain' ):
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 sentences: If false, sentence-level analysis is omitted; by
53+ default (or if true), each sentence is analyzed.
54+ :param tones: A comma-separated list of one or more of the following
55+ tones for which to analyze the input text, 'social', 'language', and
56+ '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".
5859 """
60+
5961 params = {'version' : self .version }
6062 if tones is not None :
6163 params ['tones' ] = tones
6264 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 )
65+ # Cast boolean to "false" / "true"
66+ params ['sentences' ] = str (sentences ).lower ()
67+ if content_type == 'text/plain' :
68+ text = {'text' : text }
69+ content_type = 'application/json'
70+ headers = {'content-type' : content_type }
71+
72+ if content_type == 'application/json' :
73+ return self .request (
74+ method = 'POST' , headers = headers , url = '/v3/tone' , params = params ,
75+ json = text , accept_json = True )
76+
77+ # Use the equivalent of an 'else' rather than checking for explicit
78+ # 'text/html' so that the call is made and returns a meaningful error
79+ # for an invalid content type.
80+ if content_type != 'application/json' :
81+ return self .request (
82+ method = 'POST' , headers = headers , url = '/v3/tone' , params = params ,
83+ data = text , accept_json = True )
84+
85+ #########################
86+ # tone_chat
87+ #########################
6888
6989 def tone_chat (self , utterances ):
7090 """
0 commit comments