ibm_watson 2.1.3 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,230 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # (C) Copyright IBM Corp. 2018, 2020.
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
- #
17
- # IBM OpenAPI SDK Code Generator Version: 3.38.0-07189efd-20210827-205025
18
- #
19
- # The IBM Watson™ Tone Analyzer service uses linguistic analysis to detect
20
- # emotional and language tones in written text. The service can analyze tone at both the
21
- # document and sentence levels. You can use the service to understand how your written
22
- # communications are perceived and then to improve the tone of your communications.
23
- # Businesses can use the service to learn the tone of their customers' communications and
24
- # to respond to each customer appropriately, or to understand and improve their customer
25
- # conversations.
26
- #
27
- # **Note:** Request logging is disabled for the Tone Analyzer service. Regardless of
28
- # whether you set the `X-Watson-Learning-Opt-Out` request header, the service does not log
29
- # or retain data from requests and responses.
30
-
31
- require "concurrent"
32
- require "erb"
33
- require "json"
34
- require "ibm_cloud_sdk_core"
35
- require_relative "./common.rb"
36
-
37
- module IBMWatson
38
- ##
39
- # The Tone Analyzer V3 service.
40
- class ToneAnalyzerV3 < IBMCloudSdkCore::BaseService
41
- include Concurrent::Async
42
- DEFAULT_SERVICE_NAME = "tone_analyzer"
43
- DEFAULT_SERVICE_URL = "https://api.us-south.tone-analyzer.watson.cloud.ibm.com"
44
- attr_accessor :version
45
- ##
46
- # @!method initialize(args)
47
- # Construct a new client for the Tone Analyzer service.
48
- #
49
- # @param args [Hash] The args to initialize with
50
- # @option args version [String] Release date of the version of the API you want to use. Specify dates in
51
- # YYYY-MM-DD format. The current version is `2017-09-21`.
52
- # @option args service_url [String] The base service URL to use when contacting the service.
53
- # The base service_url may differ between IBM Cloud regions.
54
- # @option args authenticator [Object] The Authenticator instance to be configured for this service.
55
- # @option args service_name [String] The name of the service to configure. Will be used as the key to load
56
- # any external configuration, if applicable.
57
- def initialize(args = {})
58
- @__async_initialized__ = false
59
- defaults = {}
60
- defaults[:service_url] = DEFAULT_SERVICE_URL
61
- defaults[:service_name] = DEFAULT_SERVICE_NAME
62
- defaults[:authenticator] = nil
63
- defaults[:version] = nil
64
- user_service_url = args[:service_url] unless args[:service_url].nil?
65
- args = defaults.merge(args)
66
- @version = args[:version]
67
- raise ArgumentError.new("version must be provided") if @version.nil?
68
-
69
- args[:authenticator] = IBMCloudSdkCore::ConfigBasedAuthenticatorFactory.new.get_authenticator(service_name: args[:service_name]) if args[:authenticator].nil?
70
- super
71
- @service_url = user_service_url unless user_service_url.nil?
72
- end
73
-
74
- #########################
75
- # Methods
76
- #########################
77
-
78
- ##
79
- # @!method tone(tone_input:, content_type: nil, sentences: nil, tones: nil, content_language: nil, accept_language: nil)
80
- # Analyze general tone.
81
- # Use the general-purpose endpoint to analyze the tone of your input content. The
82
- # service analyzes the content for emotional and language tones. The method always
83
- # analyzes the tone of the full document; by default, it also analyzes the tone of
84
- # each individual sentence of the content.
85
- #
86
- # You can submit no more than 128 KB of total input content and no more than 1000
87
- # individual sentences in JSON, plain text, or HTML format. The service analyzes the
88
- # first 1000 sentences for document-level analysis and only the first 100 sentences
89
- # for sentence-level analysis.
90
- #
91
- # Per the JSON specification, the default character encoding for JSON content is
92
- # effectively always UTF-8; per the HTTP specification, the default encoding for
93
- # plain text and HTML is ISO-8859-1 (effectively, the ASCII character set). When
94
- # specifying a content type of plain text or HTML, include the `charset` parameter
95
- # to indicate the character encoding of the input text; for example: `Content-Type:
96
- # text/plain;charset=utf-8`. For `text/html`, the service removes HTML tags and
97
- # analyzes only the textual content.
98
- #
99
- # **See also:** [Using the general-purpose
100
- # endpoint](https://cloud.ibm.com/docs/tone-analyzer?topic=tone-analyzer-utgpe#utgpe).
101
- # @param tone_input [ToneInput] JSON, plain text, or HTML input that contains the content to be analyzed. For JSON
102
- # input, provide an object of type `ToneInput`.
103
- # @param content_type [String] The type of the input. A character encoding can be specified by including a
104
- # `charset` parameter. For example, 'text/plain;charset=utf-8'.
105
- # @param sentences [Boolean] Indicates whether the service is to return an analysis of each individual sentence
106
- # in addition to its analysis of the full document. If `true` (the default), the
107
- # service returns results for each sentence.
108
- # @param tones [Array[String]] **`2017-09-21`:** Deprecated. The service continues to accept the parameter for
109
- # backward-compatibility, but the parameter no longer affects the response.
110
- #
111
- # **`2016-05-19`:** A comma-separated list of tones for which the service is to
112
- # return its analysis of the input; the indicated tones apply both to the full
113
- # document and to individual sentences of the document. You can specify one or more
114
- # of the valid values. Omit the parameter to request results for all three tones.
115
- # @param content_language [String] The language of the input text for the request: English or French. Regional
116
- # variants are treated as their parent language; for example, `en-US` is interpreted
117
- # as `en`. The input content must match the specified language. Do not submit
118
- # content that contains both languages. You can use different languages for
119
- # **Content-Language** and **Accept-Language**.
120
- # * **`2017-09-21`:** Accepts `en` or `fr`.
121
- # * **`2016-05-19`:** Accepts only `en`.
122
- # @param accept_language [String] The desired language of the response. For two-character arguments, regional
123
- # variants are treated as their parent language; for example, `en-US` is interpreted
124
- # as `en`. You can use different languages for **Content-Language** and
125
- # **Accept-Language**.
126
- # @return [IBMCloudSdkCore::DetailedResponse] A `IBMCloudSdkCore::DetailedResponse` object representing the response.
127
- def tone(tone_input:, content_type: nil, sentences: nil, tones: nil, content_language: nil, accept_language: nil)
128
- raise ArgumentError.new("version must be provided") if version.nil?
129
-
130
- raise ArgumentError.new("tone_input must be provided") if tone_input.nil?
131
-
132
- headers = {
133
- "Content-Type" => content_type,
134
- "Content-Language" => content_language,
135
- "Accept-Language" => accept_language
136
- }
137
- sdk_headers = Common.new.get_sdk_headers("tone_analyzer", "V3", "tone")
138
- headers.merge!(sdk_headers)
139
- tones *= "," unless tones.nil?
140
-
141
- params = {
142
- "version" => @version,
143
- "sentences" => sentences,
144
- "tones" => tones
145
- }
146
-
147
- if content_type.start_with?("application/json") && tone_input.instance_of?(Hash)
148
- data = tone_input.to_json
149
- else
150
- data = tone_input
151
- end
152
-
153
- method_url = "/v3/tone"
154
-
155
- response = request(
156
- method: "POST",
157
- url: method_url,
158
- headers: headers,
159
- params: params,
160
- data: data,
161
- accept_json: true
162
- )
163
- response
164
- end
165
-
166
- ##
167
- # @!method tone_chat(utterances:, content_language: nil, accept_language: nil)
168
- # Analyze customer-engagement tone.
169
- # Use the customer-engagement endpoint to analyze the tone of customer service and
170
- # customer support conversations. For each utterance of a conversation, the method
171
- # reports the most prevalent subset of the following seven tones: sad, frustrated,
172
- # satisfied, excited, polite, impolite, and sympathetic.
173
- #
174
- # If you submit more than 50 utterances, the service returns a warning for the
175
- # overall content and analyzes only the first 50 utterances. If you submit a single
176
- # utterance that contains more than 500 characters, the service returns an error for
177
- # that utterance and does not analyze the utterance. The request fails if all
178
- # utterances have more than 500 characters. Per the JSON specification, the default
179
- # character encoding for JSON content is effectively always UTF-8.
180
- #
181
- # **See also:** [Using the customer-engagement
182
- # endpoint](https://cloud.ibm.com/docs/tone-analyzer?topic=tone-analyzer-utco#utco).
183
- # @param utterances [Array[Utterance]] An array of `Utterance` objects that provides the input content that the service
184
- # is to analyze.
185
- # @param content_language [String] The language of the input text for the request: English or French. Regional
186
- # variants are treated as their parent language; for example, `en-US` is interpreted
187
- # as `en`. The input content must match the specified language. Do not submit
188
- # content that contains both languages. You can use different languages for
189
- # **Content-Language** and **Accept-Language**.
190
- # * **`2017-09-21`:** Accepts `en` or `fr`.
191
- # * **`2016-05-19`:** Accepts only `en`.
192
- # @param accept_language [String] The desired language of the response. For two-character arguments, regional
193
- # variants are treated as their parent language; for example, `en-US` is interpreted
194
- # as `en`. You can use different languages for **Content-Language** and
195
- # **Accept-Language**.
196
- # @return [IBMCloudSdkCore::DetailedResponse] A `IBMCloudSdkCore::DetailedResponse` object representing the response.
197
- def tone_chat(utterances:, content_language: nil, accept_language: nil)
198
- raise ArgumentError.new("version must be provided") if version.nil?
199
-
200
- raise ArgumentError.new("utterances must be provided") if utterances.nil?
201
-
202
- headers = {
203
- "Content-Language" => content_language,
204
- "Accept-Language" => accept_language
205
- }
206
- sdk_headers = Common.new.get_sdk_headers("tone_analyzer", "V3", "tone_chat")
207
- headers.merge!(sdk_headers)
208
-
209
- params = {
210
- "version" => @version
211
- }
212
-
213
- data = {
214
- "utterances" => utterances
215
- }
216
-
217
- method_url = "/v3/tone_chat"
218
-
219
- response = request(
220
- method: "POST",
221
- url: method_url,
222
- headers: headers,
223
- params: params,
224
- json: data,
225
- accept_json: true
226
- )
227
- response
228
- end
229
- end
230
- end