ibm_watson 2.1.3 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,267 +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
- # On 9 August 2021, IBM announced the deprecation of IBM Watson™ Natural Language
20
- # Classifier. As of 9 September 2021, you cannot create new instances. However, existing
21
- # instances are supported until 8 August 2022. The service will no longer be available on
22
- # 8 August 2022.<br/><br/>As an alternative, consider migrating to IBM Watson Natural
23
- # Language Understanding. For more information, see [Migrating to Natural Language
24
- # Understanding](https://cloud.ibm.com/docs/natural-language-classifier?topic=natural-language-classifier-migrating).
25
- # {: deprecated}
26
- #
27
- # Natural Language Classifier uses machine learning algorithms to return the top matching
28
- # predefined classes for short text input. You create and train a classifier to connect
29
- # predefined classes to example texts so that the service can apply those classes to new
30
- # inputs.
31
-
32
- require "concurrent"
33
- require "erb"
34
- require "json"
35
- require "ibm_cloud_sdk_core"
36
- require_relative "./common.rb"
37
-
38
- module IBMWatson
39
- ##
40
- # The Natural Language Classifier V1 service.
41
- class NaturalLanguageClassifierV1 < IBMCloudSdkCore::BaseService
42
- include Concurrent::Async
43
- DEFAULT_SERVICE_NAME = "natural_language_classifier"
44
- DEFAULT_SERVICE_URL = "https://api.us-south.natural-language-classifier.watson.cloud.ibm.com"
45
- ##
46
- # @!method initialize(args)
47
- # Construct a new client for the Natural Language Classifier service.
48
- #
49
- # @param args [Hash] The args to initialize with
50
- # @option args service_url [String] The base service URL to use when contacting the service.
51
- # The base service_url may differ between IBM Cloud regions.
52
- # @option args authenticator [Object] The Authenticator instance to be configured for this service.
53
- # @option args service_name [String] The name of the service to configure. Will be used as the key to load
54
- # any external configuration, if applicable.
55
- def initialize(args = {})
56
- warn "On 9 August 2021, IBM announced the deprecation of the Natural Language Classifier service. The service will no longer be available from 8 August 2022. As of 9 September 2021, you will not be able to create new instances. Existing instances will be supported until 8 August 2022. Any instance that still exists on that date will be deleted. For more information, see https://github.com/watson-developer-cloud/ruby-sdk#natural-language-classifier-deprecation."
57
- @__async_initialized__ = false
58
- defaults = {}
59
- defaults[:service_url] = DEFAULT_SERVICE_URL
60
- defaults[:service_name] = DEFAULT_SERVICE_NAME
61
- defaults[:authenticator] = nil
62
- user_service_url = args[:service_url] unless args[:service_url].nil?
63
- args = defaults.merge(args)
64
- args[:authenticator] = IBMCloudSdkCore::ConfigBasedAuthenticatorFactory.new.get_authenticator(service_name: args[:service_name]) if args[:authenticator].nil?
65
- super
66
- @service_url = user_service_url unless user_service_url.nil?
67
- end
68
-
69
- #########################
70
- # Classify text
71
- #########################
72
-
73
- ##
74
- # @!method classify(classifier_id:, text:)
75
- # Classify a phrase.
76
- # Returns label information for the input. The status must be `Available` before you
77
- # can use the classifier to classify text.
78
- # @param classifier_id [String] Classifier ID to use.
79
- # @param text [String] The submitted phrase. The maximum length is 2048 characters.
80
- # @return [IBMCloudSdkCore::DetailedResponse] A `IBMCloudSdkCore::DetailedResponse` object representing the response.
81
- def classify(classifier_id:, text:)
82
- raise ArgumentError.new("classifier_id must be provided") if classifier_id.nil?
83
-
84
- raise ArgumentError.new("text must be provided") if text.nil?
85
-
86
- headers = {
87
- }
88
- sdk_headers = Common.new.get_sdk_headers("natural_language_classifier", "V1", "classify")
89
- headers.merge!(sdk_headers)
90
-
91
- data = {
92
- "text" => text
93
- }
94
-
95
- method_url = "/v1/classifiers/%s/classify" % [ERB::Util.url_encode(classifier_id)]
96
-
97
- response = request(
98
- method: "POST",
99
- url: method_url,
100
- headers: headers,
101
- json: data,
102
- accept_json: true
103
- )
104
- response
105
- end
106
-
107
- ##
108
- # @!method classify_collection(classifier_id:, collection:)
109
- # Classify multiple phrases.
110
- # Returns label information for multiple phrases. The status must be `Available`
111
- # before you can use the classifier to classify text.
112
- #
113
- # Note that classifying Japanese texts is a beta feature.
114
- # @param classifier_id [String] Classifier ID to use.
115
- # @param collection [Array[ClassifyInput]] The submitted phrases.
116
- # @return [IBMCloudSdkCore::DetailedResponse] A `IBMCloudSdkCore::DetailedResponse` object representing the response.
117
- def classify_collection(classifier_id:, collection:)
118
- raise ArgumentError.new("classifier_id must be provided") if classifier_id.nil?
119
-
120
- raise ArgumentError.new("collection must be provided") if collection.nil?
121
-
122
- headers = {
123
- }
124
- sdk_headers = Common.new.get_sdk_headers("natural_language_classifier", "V1", "classify_collection")
125
- headers.merge!(sdk_headers)
126
-
127
- data = {
128
- "collection" => collection
129
- }
130
-
131
- method_url = "/v1/classifiers/%s/classify_collection" % [ERB::Util.url_encode(classifier_id)]
132
-
133
- response = request(
134
- method: "POST",
135
- url: method_url,
136
- headers: headers,
137
- json: data,
138
- accept_json: true
139
- )
140
- response
141
- end
142
- #########################
143
- # Manage classifiers
144
- #########################
145
-
146
- ##
147
- # @!method create_classifier(training_metadata:, training_data:)
148
- # Create classifier.
149
- # Sends data to create and train a classifier and returns information about the new
150
- # classifier.
151
- # @param training_metadata [File] Metadata in JSON format. The metadata identifies the language of the data, and an
152
- # optional name to identify the classifier. Specify the language with the 2-letter
153
- # primary language code as assigned in ISO standard 639.
154
- #
155
- # Supported languages are English (`en`), Arabic (`ar`), French (`fr`), German,
156
- # (`de`), Italian (`it`), Japanese (`ja`), Korean (`ko`), Brazilian Portuguese
157
- # (`pt`), and Spanish (`es`).
158
- # @param training_data [File] Training data in CSV format. Each text value must have at least one class. The
159
- # data can include up to 3,000 classes and 20,000 records. For details, see [Data
160
- # preparation](https://cloud.ibm.com/docs/natural-language-classifier?topic=natural-language-classifier-using-your-data).
161
- # @return [IBMCloudSdkCore::DetailedResponse] A `IBMCloudSdkCore::DetailedResponse` object representing the response.
162
- def create_classifier(training_metadata:, training_data:)
163
- raise ArgumentError.new("training_metadata must be provided") if training_metadata.nil?
164
-
165
- raise ArgumentError.new("training_data must be provided") if training_data.nil?
166
-
167
- headers = {
168
- }
169
- sdk_headers = Common.new.get_sdk_headers("natural_language_classifier", "V1", "create_classifier")
170
- headers.merge!(sdk_headers)
171
-
172
- form_data = {}
173
-
174
- unless training_metadata.instance_of?(StringIO) || training_metadata.instance_of?(File)
175
- training_metadata = training_metadata.respond_to?(:to_json) ? StringIO.new(training_metadata.to_json) : StringIO.new(training_metadata)
176
- end
177
- form_data[:training_metadata] = HTTP::FormData::File.new(training_metadata, content_type: "application/json", filename: training_metadata.respond_to?(:path) ? training_metadata.path : nil)
178
-
179
- unless training_data.instance_of?(StringIO) || training_data.instance_of?(File)
180
- training_data = training_data.respond_to?(:to_json) ? StringIO.new(training_data.to_json) : StringIO.new(training_data)
181
- end
182
- form_data[:training_data] = HTTP::FormData::File.new(training_data, content_type: "text/csv", filename: training_data.respond_to?(:path) ? training_data.path : nil)
183
-
184
- method_url = "/v1/classifiers"
185
-
186
- response = request(
187
- method: "POST",
188
- url: method_url,
189
- headers: headers,
190
- form: form_data,
191
- accept_json: true
192
- )
193
- response
194
- end
195
-
196
- ##
197
- # @!method list_classifiers
198
- # List classifiers.
199
- # Returns an empty array if no classifiers are available.
200
- # @return [IBMCloudSdkCore::DetailedResponse] A `IBMCloudSdkCore::DetailedResponse` object representing the response.
201
- def list_classifiers
202
- headers = {
203
- }
204
- sdk_headers = Common.new.get_sdk_headers("natural_language_classifier", "V1", "list_classifiers")
205
- headers.merge!(sdk_headers)
206
-
207
- method_url = "/v1/classifiers"
208
-
209
- response = request(
210
- method: "GET",
211
- url: method_url,
212
- headers: headers,
213
- accept_json: true
214
- )
215
- response
216
- end
217
-
218
- ##
219
- # @!method get_classifier(classifier_id:)
220
- # Get information about a classifier.
221
- # Returns status and other information about a classifier.
222
- # @param classifier_id [String] Classifier ID to query.
223
- # @return [IBMCloudSdkCore::DetailedResponse] A `IBMCloudSdkCore::DetailedResponse` object representing the response.
224
- def get_classifier(classifier_id:)
225
- raise ArgumentError.new("classifier_id must be provided") if classifier_id.nil?
226
-
227
- headers = {
228
- }
229
- sdk_headers = Common.new.get_sdk_headers("natural_language_classifier", "V1", "get_classifier")
230
- headers.merge!(sdk_headers)
231
-
232
- method_url = "/v1/classifiers/%s" % [ERB::Util.url_encode(classifier_id)]
233
-
234
- response = request(
235
- method: "GET",
236
- url: method_url,
237
- headers: headers,
238
- accept_json: true
239
- )
240
- response
241
- end
242
-
243
- ##
244
- # @!method delete_classifier(classifier_id:)
245
- # Delete classifier.
246
- # @param classifier_id [String] Classifier ID to delete.
247
- # @return [nil]
248
- def delete_classifier(classifier_id:)
249
- raise ArgumentError.new("classifier_id must be provided") if classifier_id.nil?
250
-
251
- headers = {
252
- }
253
- sdk_headers = Common.new.get_sdk_headers("natural_language_classifier", "V1", "delete_classifier")
254
- headers.merge!(sdk_headers)
255
-
256
- method_url = "/v1/classifiers/%s" % [ERB::Util.url_encode(classifier_id)]
257
-
258
- request(
259
- method: "DELETE",
260
- url: method_url,
261
- headers: headers,
262
- accept_json: true
263
- )
264
- nil
265
- end
266
- end
267
- end
@@ -1,223 +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
- # IBM Watson&trade; Personality Insights is discontinued. Existing instances are
20
- # supported until 1 December 2021, but as of 1 December 2020, you cannot create new
21
- # instances. Any instance that exists on 1 December 2021 will be deleted.<br/><br/>No
22
- # direct replacement exists for Personality Insights. However, you can consider using [IBM
23
- # Watson&trade; Natural Language
24
- # Understanding](https://cloud.ibm.com/docs/natural-language-understanding?topic=natural-language-understanding-about)
25
- # on IBM Cloud&reg; as part of a replacement analytic workflow for your Personality
26
- # Insights use cases. You can use Natural Language Understanding to extract data and
27
- # insights from text, such as keywords, categories, sentiment, emotion, and syntax. For
28
- # more information about the personality models in Personality Insights, see [The science
29
- # behind the
30
- # service](https://cloud.ibm.com/docs/personality-insights?topic=personality-insights-science).
31
- # {: deprecated}
32
- #
33
- # The IBM Watson Personality Insights service enables applications to derive insights from
34
- # social media, enterprise data, or other digital communications. The service uses
35
- # linguistic analytics to infer individuals' intrinsic personality characteristics,
36
- # including Big Five, Needs, and Values, from digital communications such as email, text
37
- # messages, tweets, and forum posts.
38
- #
39
- # The service can automatically infer, from potentially noisy social media, portraits of
40
- # individuals that reflect their personality characteristics. The service can infer
41
- # consumption preferences based on the results of its analysis and, for JSON content that
42
- # is timestamped, can report temporal behavior.
43
- # * For information about the meaning of the models that the service uses to describe
44
- # personality characteristics, see [Personality
45
- # models](https://cloud.ibm.com/docs/personality-insights?topic=personality-insights-models#models).
46
- # * For information about the meaning of the consumption preferences, see [Consumption
47
- # preferences](https://cloud.ibm.com/docs/personality-insights?topic=personality-insights-preferences#preferences).
48
- #
49
- #
50
- # **Note:** Request logging is disabled for the Personality Insights service. Regardless
51
- # of whether you set the `X-Watson-Learning-Opt-Out` request header, the service does not
52
- # log or retain data from requests and responses.
53
-
54
- require "concurrent"
55
- require "erb"
56
- require "json"
57
- require "ibm_cloud_sdk_core"
58
- require_relative "./common.rb"
59
-
60
- module IBMWatson
61
- ##
62
- # The Personality Insights V3 service.
63
- class PersonalityInsightsV3 < IBMCloudSdkCore::BaseService
64
- include Concurrent::Async
65
- DEFAULT_SERVICE_NAME = "personality_insights"
66
- DEFAULT_SERVICE_URL = "https://api.us-south.personality-insights.watson.cloud.ibm.com"
67
- attr_accessor :version
68
- ##
69
- # @!method initialize(args)
70
- # Construct a new client for the Personality Insights service.
71
- #
72
- # @param args [Hash] The args to initialize with
73
- # @option args version [String] Release date of the version of the API you want to use. Specify dates in
74
- # YYYY-MM-DD format. The current version is `2017-10-13`.
75
- # @option args service_url [String] The base service URL to use when contacting the service.
76
- # The base service_url may differ between IBM Cloud regions.
77
- # @option args authenticator [Object] The Authenticator instance to be configured for this service.
78
- # @option args service_name [String] The name of the service to configure. Will be used as the key to load
79
- # any external configuration, if applicable.
80
- def initialize(args = {})
81
- warn "On 1 December 2021, Personality Insights will no longer be available. For more information, see https://github.com/watson-developer-cloud/ruby-sdk/tree/master#personality-insights-deprecation."
82
- @__async_initialized__ = false
83
- defaults = {}
84
- defaults[:service_url] = DEFAULT_SERVICE_URL
85
- defaults[:service_name] = DEFAULT_SERVICE_NAME
86
- defaults[:authenticator] = nil
87
- defaults[:version] = nil
88
- user_service_url = args[:service_url] unless args[:service_url].nil?
89
- args = defaults.merge(args)
90
- @version = args[:version]
91
- raise ArgumentError.new("version must be provided") if @version.nil?
92
-
93
- args[:authenticator] = IBMCloudSdkCore::ConfigBasedAuthenticatorFactory.new.get_authenticator(service_name: args[:service_name]) if args[:authenticator].nil?
94
- super
95
- @service_url = user_service_url unless user_service_url.nil?
96
- end
97
-
98
- #########################
99
- # Methods
100
- #########################
101
-
102
- ##
103
- # @!method profile(content:, accept:, content_type: nil, content_language: nil, accept_language: nil, raw_scores: nil, csv_headers: nil, consumption_preferences: nil)
104
- # Get profile.
105
- # Generates a personality profile for the author of the input text. The service
106
- # accepts a maximum of 20 MB of input content, but it requires much less text to
107
- # produce an accurate profile. The service can analyze text in Arabic, English,
108
- # Japanese, Korean, or Spanish. It can return its results in a variety of languages.
109
- #
110
- #
111
- # **See also:**
112
- # * [Requesting a
113
- # profile](https://cloud.ibm.com/docs/personality-insights?topic=personality-insights-input#input)
114
- # * [Providing sufficient
115
- # input](https://cloud.ibm.com/docs/personality-insights?topic=personality-insights-input#sufficient)
116
- #
117
- #
118
- # ### Content types
119
- #
120
- # You can provide input content as plain text (`text/plain`), HTML (`text/html`),
121
- # or JSON (`application/json`) by specifying the **Content-Type** parameter. The
122
- # default is `text/plain`.
123
- # * Per the JSON specification, the default character encoding for JSON content is
124
- # effectively always UTF-8.
125
- # * Per the HTTP specification, the default encoding for plain text and HTML is
126
- # ISO-8859-1 (effectively, the ASCII character set).
127
- #
128
- # When specifying a content type of plain text or HTML, include the `charset`
129
- # parameter to indicate the character encoding of the input text; for example,
130
- # `Content-Type: text/plain;charset=utf-8`.
131
- #
132
- # **See also:** [Specifying request and response
133
- # formats](https://cloud.ibm.com/docs/personality-insights?topic=personality-insights-input#formats)
134
- #
135
- #
136
- # ### Accept types
137
- #
138
- # You must request a response as JSON (`application/json`) or comma-separated
139
- # values (`text/csv`) by specifying the **Accept** parameter. CSV output includes a
140
- # fixed number of columns. Set the **csv_headers** parameter to `true` to request
141
- # optional column headers for CSV output.
142
- #
143
- # **See also:**
144
- # * [Understanding a JSON
145
- # profile](https://cloud.ibm.com/docs/personality-insights?topic=personality-insights-output#output)
146
- # * [Understanding a CSV
147
- # profile](https://cloud.ibm.com/docs/personality-insights?topic=personality-insights-outputCSV#outputCSV).
148
- # @param content [Content] A maximum of 20 MB of content to analyze, though the service requires much less
149
- # text; for more information, see [Providing sufficient
150
- # input](https://cloud.ibm.com/docs/personality-insights?topic=personality-insights-input#sufficient).
151
- # For JSON input, provide an object of type `Content`.
152
- # @param accept [String] The type of the response. For more information, see **Accept types** in the method
153
- # description.
154
- # @param content_type [String] The type of the input. For more information, see **Content types** in the method
155
- # description.
156
- # @param content_language [String] The language of the input text for the request: Arabic, English, Japanese, Korean,
157
- # or Spanish. Regional variants are treated as their parent language; for example,
158
- # `en-US` is interpreted as `en`.
159
- #
160
- # The effect of the **Content-Language** parameter depends on the **Content-Type**
161
- # parameter. When **Content-Type** is `text/plain` or `text/html`,
162
- # **Content-Language** is the only way to specify the language. When
163
- # **Content-Type** is `application/json`, **Content-Language** overrides a language
164
- # specified with the `language` parameter of a `ContentItem` object, and content
165
- # items that specify a different language are ignored; omit this parameter to base
166
- # the language on the specification of the content items. You can specify any
167
- # combination of languages for **Content-Language** and **Accept-Language**.
168
- # @param accept_language [String] The desired language of the response. For two-character arguments, regional
169
- # variants are treated as their parent language; for example, `en-US` is interpreted
170
- # as `en`. You can specify any combination of languages for the input and response
171
- # content.
172
- # @param raw_scores [Boolean] Indicates whether a raw score in addition to a normalized percentile is returned
173
- # for each characteristic; raw scores are not compared with a sample population. By
174
- # default, only normalized percentiles are returned.
175
- # @param csv_headers [Boolean] Indicates whether column labels are returned with a CSV response. By default, no
176
- # column labels are returned. Applies only when the response type is CSV
177
- # (`text/csv`).
178
- # @param consumption_preferences [Boolean] Indicates whether consumption preferences are returned with the results. By
179
- # default, no consumption preferences are returned.
180
- # @return [IBMCloudSdkCore::DetailedResponse] A `IBMCloudSdkCore::DetailedResponse` object representing the response.
181
- def profile(content:, accept:, content_type: nil, content_language: nil, accept_language: nil, raw_scores: nil, csv_headers: nil, consumption_preferences: nil)
182
- raise ArgumentError.new("version must be provided") if version.nil?
183
-
184
- raise ArgumentError.new("content must be provided") if content.nil?
185
-
186
- raise ArgumentError.new("accept must be provided") if accept.nil?
187
-
188
- headers = {
189
- "Accept" => accept,
190
- "Content-Type" => content_type,
191
- "Content-Language" => content_language,
192
- "Accept-Language" => accept_language
193
- }
194
- sdk_headers = Common.new.get_sdk_headers("personality_insights", "V3", "profile")
195
- headers.merge!(sdk_headers)
196
-
197
- params = {
198
- "version" => @version,
199
- "raw_scores" => raw_scores,
200
- "csv_headers" => csv_headers,
201
- "consumption_preferences" => consumption_preferences
202
- }
203
-
204
- if content_type.start_with?("application/json") && content.instance_of?(Hash)
205
- data = content.to_json
206
- else
207
- data = content
208
- end
209
-
210
- method_url = "/v3/profile"
211
-
212
- response = request(
213
- method: "POST",
214
- url: method_url,
215
- headers: headers,
216
- params: params,
217
- data: data,
218
- accept_json: true
219
- )
220
- response
221
- end
222
- end
223
- end