2828
2929from .watson_developer_cloud_service import WatsonDeveloperCloudService
3030
31-
3231class 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