ToneAnalyzer
public class ToneAnalyzer
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. Regardless of whether you set the
X-Watson-Learning-Opt-Out
request header, the service does not log or retain data from requests and responses.
-
The base URL to use when contacting the service.
Declaration
Swift
public var serviceURL: String?
-
Release date of the version of the API you want to use. Specify dates in YYYY-MM-DD format. The current version is
2017-09-21
.Declaration
Swift
public var version: String
-
Service identifiers
Declaration
Swift
public static let defaultServiceName: String
-
The default HTTP headers for all requests to the service.
Declaration
Swift
public var defaultHeaders: [String : String]
-
Undocumented
Declaration
Swift
public let authenticator: Authenticator
-
Create a
ToneAnalyzer
object.If an authenticator is not supplied, the initializer will retrieve credentials from the environment or a local credentials file and construct an appropriate authenticator using these credentials. The credentials file can be downloaded from your service instance on IBM Cloud as ibm-credentials.env. Make sure to add the credentials file to your project so that it can be loaded at runtime.
If an authenticator is not supplied and credentials are not available in the environment or a local credentials file, initialization will fail by throwing an exception. In that case, try another initializer that directly passes in the credentials.
- serviceName: String = defaultServiceName
-
Create a
ToneAnalyzer
object.Declaration
Swift
public init(version: String, authenticator: Authenticator)
Parameters
version
Release date of the version of the API you want to use. Specify dates in YYYY-MM-DD format. The current version is
2017-09-21
.authenticator
The Authenticator object used to authenticate requests to the service
-
Allow network requests to a server without verification of the server certificate. IMPORTANT: This should ONLY be used if truly intended, as it is unsafe otherwise.
Declaration
Swift
public func disableSSLVerification()
-
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
. Fortext/html
, the service removes HTML tags and analyzes only the textual content. See also: Using the general-purpose endpoint.Declaration
Swift
public func tone( toneContent: ToneContent, sentences: Bool? = nil, tones: [String]? = nil, contentLanguage: String? = nil, acceptLanguage: String? = nil, headers: [String: String]? = nil, completionHandler: @escaping (WatsonResponse<ToneAnalysis>?, WatsonError?) -> Void)
Parameters
toneContent
JSON, plain text, or HTML input that contains the content to be analyzed. For JSON input, provide an object of type
ToneInput
.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.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.contentLanguage
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 asen
. 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
: Acceptsen
orfr
.2016-05-19
: Accepts onlyen
.
acceptLanguage
The desired language of the response. For two-character arguments, regional variants are treated as their parent language; for example,
en-US
is interpreted asen
. You can use different languages for Content-Language and Accept-Language.headers
A dictionary of request headers to be sent with this request.
completionHandler
A function executed when the request completes with a successful result or error
-
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.
Declaration
Swift
public func toneChat( utterances: [Utterance], contentLanguage: String? = nil, acceptLanguage: String? = nil, headers: [String: String]? = nil, completionHandler: @escaping (WatsonResponse<UtteranceAnalyses>?, WatsonError?) -> Void)
Parameters
utterances
An array of
Utterance
objects that provides the input content that the service is to analyze.contentLanguage
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 asen
. 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
: Acceptsen
orfr
.2016-05-19
: Accepts onlyen
.
acceptLanguage
The desired language of the response. For two-character arguments, regional variants are treated as their parent language; for example,
en-US
is interpreted asen
. You can use different languages for Content-Language and Accept-Language.headers
A dictionary of request headers to be sent with this request.
completionHandler
A function executed when the request completes with a successful result or error