ibm_watson 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +258 -0
  3. data/bin/console +14 -0
  4. data/bin/setup +8 -0
  5. data/lib/ibm_watson.rb +16 -0
  6. data/lib/ibm_watson/assistant_v1.rb +1997 -0
  7. data/lib/ibm_watson/detailed_response.rb +21 -0
  8. data/lib/ibm_watson/discovery_v1.rb +2039 -0
  9. data/lib/ibm_watson/iam_token_manager.rb +166 -0
  10. data/lib/ibm_watson/language_translator_v3.rb +411 -0
  11. data/lib/ibm_watson/natural_language_classifier_v1.rb +309 -0
  12. data/lib/ibm_watson/natural_language_understanding_v1.rb +297 -0
  13. data/lib/ibm_watson/personality_insights_v3.rb +260 -0
  14. data/lib/ibm_watson/speech_to_text_v1.rb +2153 -0
  15. data/lib/ibm_watson/text_to_speech_v1.rb +716 -0
  16. data/lib/ibm_watson/tone_analyzer_v3.rb +287 -0
  17. data/lib/ibm_watson/version.rb +3 -0
  18. data/lib/ibm_watson/visual_recognition_v3.rb +579 -0
  19. data/lib/ibm_watson/watson_api_exception.rb +41 -0
  20. data/lib/ibm_watson/watson_service.rb +180 -0
  21. data/lib/ibm_watson/websocket/recognize_callback.rb +32 -0
  22. data/lib/ibm_watson/websocket/speech_to_text_websocket_listener.rb +162 -0
  23. data/rakefile +45 -0
  24. data/test/integration/test_assistant_v1.rb +645 -0
  25. data/test/integration/test_discovery_v1.rb +200 -0
  26. data/test/integration/test_iam_assistant_v1.rb +707 -0
  27. data/test/integration/test_language_translator_v3.rb +81 -0
  28. data/test/integration/test_natural_language_classifier_v1.rb +69 -0
  29. data/test/integration/test_natural_language_understanding_v1.rb +98 -0
  30. data/test/integration/test_personality_insights_v3.rb +95 -0
  31. data/test/integration/test_speech_to_text_v1.rb +187 -0
  32. data/test/integration/test_text_to_speech_v1.rb +81 -0
  33. data/test/integration/test_tone_analyzer_v3.rb +72 -0
  34. data/test/integration/test_visual_recognition_v3.rb +64 -0
  35. data/test/test_helper.rb +22 -0
  36. data/test/unit/test_assistant_v1.rb +1598 -0
  37. data/test/unit/test_discovery_v1.rb +1144 -0
  38. data/test/unit/test_iam_token_manager.rb +165 -0
  39. data/test/unit/test_language_translator_v3.rb +461 -0
  40. data/test/unit/test_natural_language_classifier_v1.rb +187 -0
  41. data/test/unit/test_natural_language_understanding_v1.rb +132 -0
  42. data/test/unit/test_personality_insights_v3.rb +172 -0
  43. data/test/unit/test_speech_to_text_v1.rb +755 -0
  44. data/test/unit/test_text_to_speech_v1.rb +336 -0
  45. data/test/unit/test_tone_analyzer_v3.rb +200 -0
  46. data/test/unit/test_vcap_using_personality_insights.rb +150 -0
  47. data/test/unit/test_visual_recognition_v3.rb +345 -0
  48. metadata +302 -0
@@ -0,0 +1,150 @@
1
+ # frozen_string_literal: true
2
+
3
+ require("json")
4
+ require_relative("./../test_helper.rb")
5
+ require("webmock/minitest")
6
+
7
+ WebMock.disable_net_connect!(allow_localhost: true)
8
+
9
+ ENV["VCAP_SERVICES"] = JSON.parse(File.read(Dir.getwd + "/resources/vcap-testing.json")).to_json
10
+
11
+ # Unit tests for VCAP Services using the Personality Insights V3 Service
12
+ class VcapPersonalityInsightsV3Test < Minitest::Test
13
+ def test_plain_to_json
14
+ profile_response = File.read(Dir.getwd + "/resources/personality-v3-expect1.txt")
15
+ personality_text = File.read(Dir.getwd + "/resources/personality-v3.txt")
16
+ headers = {
17
+ "Content-Type" => "application/json"
18
+ }
19
+ expected_response = DetailedResponse.new(status: 200, body: JSON.parse(profile_response), headers: headers)
20
+ stub_request(:post, "https://gateway.watsonplatform.net/personality-insights/api/v3/profile?version=2017-10-13")
21
+ .with(
22
+ body: personality_text,
23
+ headers: {
24
+ "Accept" => "application/json",
25
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
26
+ "Content-Type" => "text/plain;charset=utf-8",
27
+ "Host" => "gateway.watsonplatform.net"
28
+ }
29
+ ).to_return(status: 200, body: profile_response, headers: headers)
30
+ service = IBMWatson::PersonalityInsightsV3.new(
31
+ version: "2017-10-13"
32
+ )
33
+ service_response = service.profile(
34
+ content: personality_text,
35
+ content_type: "text/plain;charset=utf-8"
36
+ )
37
+ assert_equal(expected_response.status, service_response.status)
38
+ assert_equal(expected_response.result, service_response.result)
39
+ expected_response.headers.each_key do |key|
40
+ assert(service_response.headers.key?(key))
41
+ assert(expected_response.headers[key] == service_response.headers[key])
42
+ end
43
+ end
44
+
45
+ def test_json_to_json
46
+ profile_response = File.read(Dir.getwd + "/resources/personality-v3-expect2.txt")
47
+ personality_text = File.read(Dir.getwd + "/resources/personality-v3.json")
48
+ headers = {
49
+ "Content-Type" => "applicaiton/json"
50
+ }
51
+ expected_response = DetailedResponse.new(status: 200, body: profile_response, headers: headers)
52
+ stub_request(:post, "https://gateway.watsonplatform.net/personality-insights/api/v3/profile?consumption_preferences=true&raw_scores=true&version=2017-10-13")
53
+ .with(
54
+ body: personality_text,
55
+ headers: {
56
+ "Accept" => "application/json",
57
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
58
+ "Content-Type" => "application/json",
59
+ "Host" => "gateway.watsonplatform.net"
60
+ }
61
+ ).to_return(status: 200, body: profile_response, headers: headers)
62
+ service = IBMWatson::PersonalityInsightsV3.new(
63
+ version: "2017-10-13"
64
+ )
65
+ service_response = service.profile(
66
+ content: personality_text,
67
+ content_type: "application/json",
68
+ raw_scores: true,
69
+ consumption_preferences: true
70
+ )
71
+ assert_equal(expected_response.status, service_response.status)
72
+ assert_equal(expected_response.result, service_response.result)
73
+ expected_response.headers.each_key do |key|
74
+ assert(service_response.headers.key?(key))
75
+ assert(expected_response.headers[key] == service_response.headers[key])
76
+ end
77
+ end
78
+
79
+ def test_json_to_csv
80
+ profile_response = File.read(Dir.getwd + "/resources/personality-v3-expect3.txt")
81
+ personality_text = File.read(Dir.getwd + "/resources/personality-v3.json")
82
+ headers = {
83
+ "Content-Type" => "application/json"
84
+ }
85
+ expected_response = DetailedResponse.new(status: 200, body: profile_response, headers: headers)
86
+ stub_request(:post, "https://gateway.watsonplatform.net/personality-insights/api/v3/profile?consumption_preferences=true&csv_headers=true&raw_scores=true&version=2017-10-13")
87
+ .with(
88
+ body: personality_text,
89
+ headers: {
90
+ "Accept" => "application/json",
91
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
92
+ "Content-Type" => "application/json",
93
+ "Host" => "gateway.watsonplatform.net"
94
+ }
95
+ ).to_return(status: 200, body: profile_response.to_json, headers: headers)
96
+ service = IBMWatson::PersonalityInsightsV3.new(
97
+ version: "2017-10-13"
98
+ )
99
+ service_response = service.profile(
100
+ content: personality_text,
101
+ content_type: "application/json",
102
+ accept: "text/csv",
103
+ csv_headers: true,
104
+ raw_scores: true,
105
+ consumption_preferences: true
106
+ )
107
+ assert_equal(expected_response.status, service_response.status)
108
+ assert_equal(expected_response.result, service_response.result)
109
+ expected_response.headers.each_key do |key|
110
+ assert(service_response.headers.key?(key))
111
+ assert(expected_response.headers[key] == service_response.headers[key])
112
+ end
113
+ end
114
+
115
+ def test_plain_to_json_es
116
+ profile_response = JSON.parse(File.read(Dir.getwd + "/resources/personality-v3-expect4.txt"))
117
+ personality_text = File.read(Dir.getwd + "/resources/personality-v3-es.txt")
118
+ headers = {
119
+ "Content-Type" => "application/json"
120
+ }
121
+ expected_response = DetailedResponse.new(status: 200, body: profile_response, headers: headers)
122
+ stub_request(:post, "https://gateway.watsonplatform.net/personality-insights/api/v3/profile?version=2017-10-13")
123
+ .with(
124
+ body: personality_text,
125
+ headers: {
126
+ "Accept" => "application/json",
127
+ "Accept-Language" => "es",
128
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
129
+ "Content-Language" => "es",
130
+ "Content-Type" => "text/plain;charset=utf-8",
131
+ "Host" => "gateway.watsonplatform.net"
132
+ }
133
+ ).to_return(status: 200, body: profile_response.to_json, headers: headers)
134
+ service = IBMWatson::PersonalityInsightsV3.new(
135
+ version: "2017-10-13"
136
+ )
137
+ service_response = service.profile(
138
+ content: personality_text,
139
+ content_type: "text/plain;charset=utf-8",
140
+ content_language: "es",
141
+ accept_language: "es"
142
+ )
143
+ assert_equal(expected_response.status, service_response.status)
144
+ assert_equal(expected_response.result, service_response.result)
145
+ expected_response.headers.each_key do |key|
146
+ assert(service_response.headers.key?(key))
147
+ assert(expected_response.headers[key] == service_response.headers[key])
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,345 @@
1
+ # frozen_string_literal: true
2
+
3
+ require("json")
4
+ require_relative("./../test_helper.rb")
5
+ require("webmock/minitest")
6
+
7
+ WebMock.disable_net_connect!(allow_localhost: true)
8
+
9
+ # Unit tests for the Visual Recognition V3 Service
10
+ class VisualRecognitionV3Test < Minitest::Test
11
+ def test_get_classifier
12
+ service = IBMWatson::VisualRecognitionV3.new(
13
+ version: "2018-03-19"
14
+ )
15
+ service._iam_access_token(iam_access_token: "bogus_access_token")
16
+ service._iam_access_token(iam_access_token: "bogus_access_token")
17
+ response = {
18
+ "classifier_id" => "bogusnumber",
19
+ "name" => "Dog Breeds",
20
+ "owner" => "58b61352-678c-44d1-9f40-40edf4ea8d19",
21
+ "status" => "failed",
22
+ "created" => "2017-08-25T06:39:01.968Z",
23
+ "classes" => [
24
+ {
25
+ "class" => "goldenretriever"
26
+ }
27
+ ]
28
+ }
29
+ stub_request(:get, "https://gateway.watsonplatform.net/visual-recognition/api/v3/classifiers/bogusnumber?version=2018-03-19")
30
+ .with(
31
+ headers: {
32
+ "Accept" => "application/json",
33
+ "Host" => "gateway.watsonplatform.net",
34
+ "Authorization" => "Bearer bogus_access_token"
35
+ }
36
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
37
+ service_response = service.get_classifier(
38
+ classifier_id: "bogusnumber"
39
+ )
40
+ assert_equal(response, service_response.result)
41
+ end
42
+
43
+ def test_delete_classifier
44
+ service = IBMWatson::VisualRecognitionV3.new(
45
+ version: "2018-03-19",
46
+ iam_access_token: "bogus_access_token"
47
+ )
48
+ stub_request(:delete, "https://gateway.watsonplatform.net/visual-recognition/api/v3/classifiers/bogusnumber?version=2018-03-19")
49
+ .with(
50
+ headers: {
51
+ "Accept" => "application/json",
52
+ "Host" => "gateway.watsonplatform.net",
53
+ "Authorization" => "Bearer bogus_access_token"
54
+ }
55
+ ).to_return(status: 200, body: { "response" => 200 }.to_json, headers: { "Content-Type" => "applicaton/json" })
56
+ service_response = service.delete_classifier(
57
+ classifier_id: "bogusnumber"
58
+ )
59
+ assert_nil(service_response)
60
+ end
61
+
62
+ def test_list_classifiers
63
+ service = IBMWatson::VisualRecognitionV3.new(
64
+ version: "2018-03-19",
65
+ iam_access_token: "bogus_access_token"
66
+ )
67
+ response = {
68
+ "classifiers" =>
69
+ [
70
+ {
71
+ "classifier_id" => "InsuranceClaims_1362331461",
72
+ "name" => "Insurance Claims",
73
+ "status" => "ready"
74
+ },
75
+ {
76
+ "classifier_id" => "DogBreeds_1539707331",
77
+ "name" => "Dog Breeds",
78
+ "status" => "ready"
79
+ }
80
+ ]
81
+ }
82
+ stub_request(:get, "https://gateway.watsonplatform.net/visual-recognition/api/v3/classifiers?version=2018-03-19")
83
+ .with(
84
+ headers: {
85
+ "Accept" => "application/json",
86
+ "Host" => "gateway.watsonplatform.net",
87
+ "Authorization" => "Bearer bogus_access_token"
88
+ }
89
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
90
+ service_response = service.list_classifiers
91
+ assert_equal(response, service_response.result)
92
+ end
93
+
94
+ def test_create_classifier
95
+ service = IBMWatson::VisualRecognitionV3.new(
96
+ version: "2018-03-19",
97
+ iam_access_token: "bogus_access_token"
98
+ )
99
+ response = {
100
+ "classifier_id" => "DogBreeds_2014254824",
101
+ "name" => "Dog Breeds",
102
+ "owner" => "58b61352-678c-44d1-9f40-40edf4ea8d19",
103
+ "status" => "failed",
104
+ "created" => "2017-08-25T06:39:01.968Z",
105
+ "classes" => [{ "class" => "goldenretriever" }]
106
+ }
107
+ cars = File.open(Dir.getwd + "/resources/cars.zip")
108
+ trucks = File.open(Dir.getwd + "/resources/trucks.zip")
109
+
110
+ stub_request(:post, "https://gateway.watsonplatform.net/visual-recognition/api/v3/classifiers?version=2018-03-19")
111
+ .with(
112
+ headers: {
113
+ "Accept" => "application/json",
114
+ "Host" => "gateway.watsonplatform.net",
115
+ "Authorization" => "Bearer bogus_access_token"
116
+ }
117
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
118
+ service_response = service.create_classifier(
119
+ name: "Cars vs Trucks",
120
+ classname_positive_examples: cars,
121
+ negative_examples: trucks
122
+ )
123
+ assert_equal(response, service_response.result)
124
+
125
+ service_response = service.create_classifier(
126
+ name: "Cars vs Trucks",
127
+ classname_positive_examples: "cars",
128
+ classname_positive_examples_filename: "cars",
129
+ negative_examples: "trucks",
130
+ negative_examples_filename: "trucks"
131
+ )
132
+ assert_equal(response, service_response.result)
133
+ end
134
+
135
+ def test_update_classifier
136
+ service = IBMWatson::VisualRecognitionV3.new(
137
+ version: "2018-03-19",
138
+ iam_access_token: "bogus_access_token"
139
+ )
140
+ response = {
141
+ "classifier_id" => "bogusid",
142
+ "name" => "Insurance Claims",
143
+ "owner" => "58b61352-678c-44d1-9f40-40edf4ea8d19",
144
+ "status" => "ready",
145
+ "created" => "2017-07-17T22:17:14.860Z",
146
+ "classes" => [
147
+ { "class" => "motorcycleaccident" },
148
+ { "class" => "flattire" },
149
+ { "class" => "brokenwinshield" }
150
+ ]
151
+ }
152
+ stub_request(:post, "https://gateway.watsonplatform.net/visual-recognition/api/v3/classifiers/bogusid?version=2018-03-19")
153
+ .with(
154
+ headers: {
155
+ "Accept" => "application/json",
156
+ "Host" => "gateway.watsonplatform.net",
157
+ "Authorization" => "Bearer bogus_access_token"
158
+ }
159
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
160
+ service_response = service.update_classifier(
161
+ classifier_id: "bogusid",
162
+ classname_positive_examples: "positive examples classname",
163
+ negative_examples: "negative examples"
164
+ )
165
+ assert_equal(response, service_response.result)
166
+
167
+ service_response = service.update_classifier(
168
+ classifier_id: "bogusid",
169
+ classname_positive_examples: "positive examples file",
170
+ classname_positive_examples_filename: "positive_filename",
171
+ negative_examples: "negative examples",
172
+ negative_examples_filename: "negative_filename"
173
+ )
174
+ assert_equal(response, service_response.result)
175
+ end
176
+
177
+ def test_classify
178
+ service = IBMWatson::VisualRecognitionV3.new(
179
+ version: "2018-03-19",
180
+ iam_access_token: "bogus_access_token"
181
+ )
182
+ response = {
183
+ "images" =>
184
+ [
185
+ {
186
+ "image" => "test.jpg",
187
+ "classifiers" =>
188
+ [
189
+ {
190
+ "classes" =>
191
+ [
192
+ { "score" => 0.95, "class" => "tiger", "type_hierarchy" => "/animal/mammal/carnivore/feline/big cat/tiger" },
193
+ { "score" => 0.997, "class" => "big cat" },
194
+ { "score" => 0.998, "class" => "feline" },
195
+ { "score" => 0.998, "class" => "carnivore" },
196
+ { "score" => 0.998, "class" => "mammal" },
197
+ { "score" => 0.999, "class" => "animal" }
198
+ ],
199
+ "classifier_id" => "default",
200
+ "name" => "default"
201
+ }
202
+ ]
203
+ }
204
+ ],
205
+ "custom_classes" => 0,
206
+ "images_processed" => 1
207
+ }
208
+
209
+ stub_request(:post, "https://gateway.watsonplatform.net/visual-recognition/api/v3/classify?version=2018-03-19")
210
+ .with(
211
+ headers: {
212
+ "Accept" => "application/json",
213
+ "Host" => "gateway.watsonplatform.net",
214
+ "Authorization" => "Bearer bogus_access_token"
215
+ }
216
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
217
+ service_response = service.classify(
218
+ url: "http://google.com"
219
+ )
220
+ assert_equal(response, service_response.result)
221
+
222
+ service_response = service.classify(
223
+ url: "http://google.com",
224
+ classifier_ids: %w[one two three]
225
+ )
226
+ assert_equal(response, service_response.result)
227
+
228
+ service_response = service.classify(
229
+ url: "http://google.com",
230
+ owners: %w[me IBM]
231
+ )
232
+ assert_equal(response, service_response.result)
233
+
234
+ image_file = File.open(Dir.getwd + "/resources/test.jpg")
235
+ service_response = service.classify(
236
+ images_file: image_file,
237
+ images_filename: "test.jpg"
238
+ )
239
+ assert_equal(response, service_response.result)
240
+
241
+ service_response = service.classify(
242
+ images_file: "image_file"
243
+ )
244
+ assert_equal(response, service_response.result)
245
+ end
246
+
247
+ def test_detect_faces
248
+ service = IBMWatson::VisualRecognitionV3.new(
249
+ version: "2018-03-19",
250
+ iam_access_token: "bogus_access_token"
251
+ )
252
+ response = {
253
+ "images" => [
254
+ {
255
+ "faces" => [
256
+ {
257
+ "age" => {
258
+ "max" => 44,
259
+ "min" => 35,
260
+ "score" => 0.446989
261
+ },
262
+ "face_location" => {
263
+ "height" => 159,
264
+ "left" => 256,
265
+ "top" => 64,
266
+ "width" => 92
267
+ },
268
+ "gender" => {
269
+ "gender" => "MALE",
270
+ "score" => 0.99593
271
+ },
272
+ "identity" => {
273
+ "name" => "Barack Obama",
274
+ "score" => 0.970688,
275
+ "type_hierarchy" => "/people/politicians/democrats/barack obama"
276
+ }
277
+ }
278
+ ],
279
+ "resolved_url" => "https://watson-developer-cloud.github.io/doc-tutorial-downloads/visual-recognition/prez.jpg",
280
+ "source_url" => "https://watson-developer-cloud.github.io/doc-tutorial-downloads/visual-recognition/prez.jpg"
281
+ }
282
+ ],
283
+ "images_processed" => 1
284
+ }
285
+ stub_request(:post, "https://gateway.watsonplatform.net/visual-recognition/api/v3/detect_faces?version=2018-03-19")
286
+ .with(
287
+ headers: {
288
+ "Accept" => "application/json",
289
+ "Host" => "gateway.watsonplatform.net",
290
+ "Authorization" => "Bearer bogus_access_token"
291
+ }
292
+ ).to_return(status: 200, body: response.to_json, headers: { "Content-Type" => "application/json" })
293
+ service_response = service.detect_faces(
294
+ url: "http://google.com"
295
+ )
296
+ assert_equal(response, service_response.result)
297
+
298
+ image_file = File.open(Dir.getwd + "/resources/test.jpg")
299
+ service_response = service.detect_faces(
300
+ images_file: image_file,
301
+ images_filename: "test.jpg"
302
+ )
303
+ assert_equal(response, service_response.result)
304
+
305
+ service_response = service.detect_faces(
306
+ images_file: "image_file"
307
+ )
308
+ assert_equal(response, service_response.result)
309
+ end
310
+
311
+ def test_delete_user_data
312
+ service = IBMWatson::VisualRecognitionV3.new(
313
+ version: "2018-03-19",
314
+ iam_access_token: "bogus_access_token"
315
+ )
316
+ stub_request(:delete, "https://gateway.watsonplatform.net/visual-recognition/api/v3/user_data?customer_id=id&version=2018-03-19")
317
+ .with(
318
+ headers: {
319
+ "Accept" => "application/json",
320
+ "Host" => "gateway.watsonplatform.net",
321
+ "Authorization" => "Bearer bogus_access_token"
322
+ }
323
+ ).to_return(status: 200, body: "", headers: {})
324
+ service_response = service.delete_user_data(
325
+ customer_id: "id"
326
+ )
327
+ assert_nil(service_response)
328
+ end
329
+
330
+ def test_get_core_ml_model
331
+ service = IBMWatson::VisualRecognitionV3.new(
332
+ version: "2018-03-19",
333
+ iam_access_token: "bogus_access_token"
334
+ )
335
+ stub_request(:get, "https://gateway.watsonplatform.net/visual-recognition/api/v3/classifiers/classifierid/core_ml_model?version=2018-03-19")
336
+ .with(
337
+ headers: {
338
+ "Authorization" => "Bearer bogus_access_token",
339
+ "Host" => "gateway.watsonplatform.net"
340
+ }
341
+ ).to_return(status: 200, body: "ml_model", headers: {})
342
+ service_response = service.get_core_ml_model(classifier_id: "classifierid")
343
+ assert_equal("ml_model", service_response.result)
344
+ end
345
+ end