watson_developer_cloud.tone_analyzer_v3 module

The IBM Watson™ Tone Analyzer service uses linguistic analysis to detect emotional and language tones in written text. The service can analyze tone at both the document and sentence levels. You can use the service to understand how your written communications are perceived and then to improve the tone of your communications. Businesses can use the service to learn the tone of their customers’ communications and to respond to each customer appropriately, or to understand and improve their customer conversations. Note: Request logging is disabled for the Tone Analyzer service. The service neither logs nor retains data from requests and responses, regardless of whether the X-Watson-Learning-Opt-Out request header is set.

class ToneAnalyzerV3(version, url='https://gateway.watsonplatform.net/tone-analyzer/api', username=None, password=None, iam_apikey=None, iam_access_token=None, iam_url=None)[source]

Bases: watson_developer_cloud.watson_service.WatsonService

The Tone Analyzer V3 service.

default_url = 'https://gateway.watsonplatform.net/tone-analyzer/api'
tone(tone_input, content_type, sentences=None, tones=None, content_language=None, accept_language=None, **kwargs)[source]

Analyze general tone.

Use the general purpose endpoint to analyze the tone of your input content. The service analyzes the content for emotional and language tones. The method always analyzes the tone of the full document; by default, it also analyzes the tone of each individual sentence of the content. You can submit no more than 128 KB of total input content and no more than 1000 individual sentences in JSON, plain text, or HTML format. The service analyzes the first 1000 sentences for document-level analysis and only the first 100 sentences for sentence-level analysis. Per the JSON specification, the default character encoding for JSON content is effectively always UTF-8; per the HTTP specification, the default encoding for plain text and HTML is ISO-8859-1 (effectively, the ASCII character set). When specifying a content type of plain text or HTML, include the charset parameter to indicate the character encoding of the input text; for example: Content-Type: text/plain;charset=utf-8. For text/html, the service removes HTML tags and analyzes only the textual content. See also: [Using the general-purpose endpoint](https://console.bluemix.net/docs/services/tone-analyzer/using-tone.html#using-the-general-purpose-endpoint).

Parameters:tone_input (ToneInput) – JSON, plain text, or HTML input that contains the

content to be analyzed. For JSON input, provide an object of type ToneInput. :param str content_type: The type of the input. A character encoding can be specified by including a charset parameter. For example, ‘text/plain;charset=utf-8’. :param bool sentences: Indicates whether the service is to return an analysis of each individual sentence in addition to its analysis of the full document. If true (the default), the service returns results for each sentence. :param list[str] tones: `2017-09-21`: Deprecated. The service continues to accept the parameter for backward-compatibility, but the parameter no longer affects the response. `2016-05-19`: A comma-separated list of tones for which the service is to return its analysis of the input; the indicated tones apply both to the full document and to individual sentences of the document. You can specify one or more of the valid values. Omit the parameter to request results for all three tones. :param str content_language: The language of the input text for the request: English or French. Regional variants are treated as their parent language; for example, en-US is interpreted as en. The input content must match the specified language. Do not submit content that contains both languages. You can use different languages for Content-Language and Accept-Language. * `2017-09-21`: Accepts en or fr. * `2016-05-19`: Accepts only en. :param str accept_language: The desired language of the response. For two-character arguments, regional variants are treated as their parent language; for example, en-US is interpreted as en. You can use different languages for Content-Language and Accept-Language. :param dict headers: A dict containing the request headers :return: A DetailedResponse containing the result, headers and HTTP status code. :rtype: DetailedResponse

tone_chat(utterances, content_language=None, accept_language=None, **kwargs)[source]

Analyze customer engagement tone.

Use the customer engagement endpoint to analyze the tone of customer service and customer support conversations. For each utterance of a conversation, the method reports the most prevalent subset of the following seven tones: sad, frustrated, satisfied, excited, polite, impolite, and sympathetic. If you submit more than 50 utterances, the service returns a warning for the overall content and analyzes only the first 50 utterances. If you submit a single utterance that contains more than 500 characters, the service returns an error for that utterance and does not analyze the utterance. The request fails if all utterances have more than 500 characters. Per the JSON specification, the default character encoding for JSON content is effectively always UTF-8. See also: [Using the customer-engagement endpoint](https://console.bluemix.net/docs/services/tone-analyzer/using-tone-chat.html#using-the-customer-engagement-endpoint).

Parameters:utterances (list[Utterance]) – An array of Utterance objects that provides

the input content that the service is to analyze. :param str content_language: The language of the input text for the request: English or French. Regional variants are treated as their parent language; for example, en-US is interpreted as en. The input content must match the specified language. Do not submit content that contains both languages. You can use different languages for Content-Language and Accept-Language. * `2017-09-21`: Accepts en or fr. * `2016-05-19`: Accepts only en. :param str accept_language: The desired language of the response. For two-character arguments, regional variants are treated as their parent language; for example, en-US is interpreted as en. You can use different languages for Content-Language and Accept-Language. :param dict headers: A dict containing the request headers :return: A DetailedResponse containing the result, headers and HTTP status code. :rtype: DetailedResponse

class DocumentAnalysis(tones=None, tone_categories=None, warning=None)[source]

Bases: object

DocumentAnalysis.

Attr list[ToneScore] tones:
 (optional) `2017-09-21`: An array of ToneScore

objects that provides the results of the analysis for each qualifying tone of the document. The array includes results for any tone whose score is at least 0.5. The array is empty if no tone has a score that meets this threshold. `2016-05-19`: Not returned. :attr list[ToneCategory] tone_categories: (optional) `2017-09-21`: Not returned. `2016-05-19`: An array of ToneCategory objects that provides the results of the tone analysis for the full document of the input content. The service returns results only for the tones specified with the tones parameter of the request. :attr str warning: (optional) `2017-09-21`: A warning message if the overall content exceeds 128 KB or contains more than 1000 sentences. The service analyzes only the first 1000 sentences for document-level analysis and the first 100 sentences for sentence-level analysis. `2016-05-19`: Not returned.

class SentenceAnalysis(sentence_id, text, tones=None, tone_categories=None, input_from=None, input_to=None)[source]

Bases: object

SentenceAnalysis.

Attr int sentence_id:
 The unique identifier of a sentence of the input content. The

first sentence has ID 0, and the ID of each subsequent sentence is incremented by one. :attr str text: The text of the input sentence. :attr list[ToneScore] tones: (optional) `2017-09-21`: An array of ToneScore objects that provides the results of the analysis for each qualifying tone of the sentence. The array includes results for any tone whose score is at least 0.5. The array is empty if no tone has a score that meets this threshold. `2016-05-19`: Not returned. :attr list[ToneCategory] tone_categories: (optional) `2017-09-21`: Not returned. `2016-05-19`: An array of ToneCategory objects that provides the results of the tone analysis for the sentence. The service returns results only for the tones specified with the tones parameter of the request. :attr int input_from: (optional) `2017-09-21`: Not returned. `2016-05-19`: The offset of the first character of the sentence in the overall input content. :attr int input_to: (optional) `2017-09-21`: Not returned. `2016-05-19`: The offset of the last character of the sentence in the overall input content.

class ToneAnalysis(document_tone, sentences_tone=None)[source]

Bases: object

ToneAnalysis.

Attr DocumentAnalysis document_tone:
 An object of type DocumentAnalysis that

provides the results of the analysis for the full input document. :attr list[SentenceAnalysis] sentences_tone: (optional) An array of SentenceAnalysis objects that provides the results of the analysis for the individual sentences of the input content. The service returns results only for the first 100 sentences of the input. The field is omitted if the sentences parameter of the request is set to false.

class ToneCategory(tones, category_id, category_name)[source]

Bases: object

ToneCategory.

Attr list[ToneScore] tones:
 An array of ToneScore objects that provides the results

for the tones of the category. :attr str category_id: The unique, non-localized identifier of the category for the results. The service can return results for the following category IDs: emotion_tone, language_tone, and social_tone. :attr str category_name: The user-visible, localized name of the category.

class ToneChatScore(score, tone_id, tone_name)[source]

Bases: object

ToneChatScore.

Attr float score:
 The score for the tone in the range of 0.5 to 1. A score greater

than 0.75 indicates a high likelihood that the tone is perceived in the utterance. :attr str tone_id: The unique, non-localized identifier of the tone for the results. The service returns results only for tones whose scores meet a minimum threshold of 0.5. :attr str tone_name: The user-visible, localized name of the tone.

class ToneInput(text)[source]

Bases: object

ToneInput.

Attr str text:The input content that the service is to analyze.
class ToneScore(score, tone_id, tone_name)[source]

Bases: object

ToneScore.

Attr float score:
 The score for the tone.
  • `2017-09-21`: The score that is returned lies in the range of 0.5 to 1. A score

greater than 0.75 indicates a high likelihood that the tone is perceived in the content. * `2016-05-19`: The score that is returned lies in the range of 0 to 1. A score less than 0.5 indicates that the tone is unlikely to be perceived in the content; a score greater than 0.75 indicates a high likelihood that the tone is perceived. :attr str tone_id: The unique, non-localized identifier of the tone. * `2017-09-21`: The service can return results for the following tone IDs: anger, fear, joy, and sadness (emotional tones); analytical, confident, and tentative (language tones). The service returns results only for tones whose scores meet a minimum threshold of 0.5. * `2016-05-19`: The service can return results for the following tone IDs of the different categories: for the emotion category: anger, disgust, fear, joy, and sadness; for the language category: analytical, confident, and tentative; for the social category: openness_big5, conscientiousness_big5, extraversion_big5, agreeableness_big5, and emotional_range_big5. The service returns scores for all tones of a category, regardless of their values. :attr str tone_name: The user-visible, localized name of the tone.

class Utterance(text, user=None)[source]

Bases: object

Utterance.

Attr str text:An utterance contributed by a user in the conversation that is to be

analyzed. The utterance can contain multiple sentences. :attr str user: (optional) A string that identifies the user who contributed the utterance specified by the text parameter.

class UtteranceAnalyses(utterances_tone, warning=None)[source]

Bases: object

UtteranceAnalyses.

Attr list[UtteranceAnalysis] utterances_tone:
 An array of UtteranceAnalysis objects

that provides the results for each utterance of the input. :attr str warning: (optional) `2017-09-21`: A warning message if the content contains more than 50 utterances. The service analyzes only the first 50 utterances. `2016-05-19`: Not returned.

class UtteranceAnalysis(utterance_id, utterance_text, tones, error=None)[source]

Bases: object

UtteranceAnalysis.

Attr int utterance_id:
 The unique identifier of the utterance. The first utterance

has ID 0, and the ID of each subsequent utterance is incremented by one. :attr str utterance_text: The text of the utterance. :attr list[ToneChatScore] tones: An array of ToneChatScore objects that provides results for the most prevalent tones of the utterance. The array includes results for any tone whose score is at least 0.5. The array is empty if no tone has a score that meets this threshold. :attr str error: (optional) `2017-09-21`: An error message if the utterance contains more than 500 characters. The service does not analyze the utterance. `2016-05-19`: Not returned.