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,755 @@
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 Speech to Text V1 Service
10
+ class SpeechToTextV1Test < Minitest::Test
11
+ def test_success
12
+ models_response = {
13
+ "models" => [
14
+ {
15
+ "url" => "https://stream.watsonplatform.net/speech-to-text/api/v1/models/WatsonModel",
16
+ "rate" => 16_000,
17
+ "name" => "WatsonModel",
18
+ "language" => "en-US",
19
+ "description" => "Watson model \"v7w_134k.3\" for Attila 2-5 reco engine."
20
+ }
21
+ ]
22
+ }
23
+ service = IBMWatson::SpeechToTextV1.new(
24
+ username: "username",
25
+ password: "password"
26
+ )
27
+ stub_request(:get, "https://stream.watsonplatform.net/speech-to-text/api/v1/models")
28
+ .with(
29
+ headers: {
30
+ "Accept" => "application/json",
31
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
32
+ "Host" => "stream.watsonplatform.net"
33
+ }
34
+ ).to_return(status: 200, body: models_response.to_json, headers: { "Content-Type" => "application/json" })
35
+ service_response = service.list_models
36
+ assert_equal(models_response, service_response.result)
37
+
38
+ recognize_response = {
39
+ "results" => [
40
+ {
41
+ "alternatives" => [
42
+ {
43
+ "transcript" => "thunderstorms could produce large hail isolated tornadoes and heavy rain"
44
+ }
45
+ ],
46
+ "final" => true
47
+ }
48
+ ],
49
+ "result_index" => 0
50
+ }
51
+ audio_file = File.open(Dir.getwd + "/resources/speech.wav")
52
+ stub_request(:post, "https://stream.watsonplatform.net/speech-to-text/api/v1/recognize")
53
+ .with(
54
+ headers: {
55
+ "Accept" => "application/json",
56
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
57
+ "Content-Type" => "audio/l16; rate=44100",
58
+ "Host" => "stream.watsonplatform.net"
59
+ }
60
+ ).to_return(status: 200, body: recognize_response.to_json, headers: { "Content-Type" => "application/json" })
61
+ service_response = service.recognize(
62
+ audio: audio_file,
63
+ content_type: "audio/l16; rate=44100"
64
+ )
65
+ assert_equal(recognize_response, service_response.result)
66
+ end
67
+
68
+ def test_get_model
69
+ service = IBMWatson::SpeechToTextV1.new(
70
+ username: "username",
71
+ password: "password"
72
+ )
73
+ stub_request(:get, "https://stream.watsonplatform.net/speech-to-text/api/v1/models/modelid")
74
+ .with(
75
+ headers: {
76
+ "Accept" => "application/json",
77
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
78
+ "Host" => "stream.watsonplatform.net"
79
+ }
80
+ ).to_return(status: 200, body: { "bogus_response" => "yep" }.to_json, headers: { "Content-Type" => "application/json" })
81
+ service_response = service.get_model(
82
+ model_id: "modelid"
83
+ )
84
+ assert_equal({ "bogus_response" => "yep" }, service_response.result)
85
+ end
86
+
87
+ def test_recognitions
88
+ get_response = {
89
+ "recognitions" => [
90
+ {
91
+ "created" => "2018-02-01T17:43:15.432Z",
92
+ "id" => "6193190c-0777-11e8-9b4b-43ad845196dd",
93
+ "updated" => "2018-02-01T17:43:17.998Z",
94
+ "status" => "failed"
95
+ }
96
+ ]
97
+ }
98
+ service = IBMWatson::SpeechToTextV1.new(
99
+ username: "username",
100
+ password: "password"
101
+ )
102
+ stub_request(:get, "https://stream.watsonplatform.net/speech-to-text/api/v1/recognitions")
103
+ .with(
104
+ headers: {
105
+ "Accept" => "application/json",
106
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
107
+ "Host" => "stream.watsonplatform.net"
108
+ }
109
+ ).to_return(status: 200, body: get_response.to_json, headers: { "Content-Type" => "application/json" })
110
+ service_response = service.check_jobs
111
+ assert_equal("6193190c-0777-11e8-9b4b-43ad845196dd", service_response.result["recognitions"][0]["id"])
112
+
113
+ stub_request(:get, "https://stream.watsonplatform.net/speech-to-text/api/v1/recognitions/jobid")
114
+ .with(
115
+ headers: {
116
+ "Accept" => "application/json",
117
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
118
+ "Host" => "stream.watsonplatform.net"
119
+ }
120
+ ).to_return(status: 200, body: { "status" => "waiting" }.to_json, headers: { "Content-Type" => "application/json" })
121
+ service_response = service.check_job(
122
+ id: "jobid"
123
+ )
124
+ assert_equal({ "status" => "waiting" }, service_response.result)
125
+
126
+ stub_request(:post, "https://stream.watsonplatform.net/speech-to-text/api/v1/recognitions")
127
+ .with(
128
+ headers: {
129
+ "Accept" => "application/json",
130
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
131
+ "Content-Type" => "audio/basic",
132
+ "Host" => "stream.watsonplatform.net"
133
+ }
134
+ ).to_return(status: 200, body: { "status" => "waiting" }.to_json, headers: { "Content-Type" => "application/json" })
135
+ audio_file = File.open(Dir.getwd + "/resources/speech.wav")
136
+ service_response = service.create_job(
137
+ audio: audio_file,
138
+ content_type: "audio/basic"
139
+ )
140
+ assert_equal({ "status" => "waiting" }, service_response.result)
141
+
142
+ stub_request(:delete, "https://stream.watsonplatform.net/speech-to-text/api/v1/recognitions/jobid")
143
+ .with(
144
+ headers: {
145
+ "Accept" => "application/json",
146
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
147
+ "Host" => "stream.watsonplatform.net"
148
+ }
149
+ ).to_return(status: 200, body: { "description" => "deleted successfully" }.to_json, headers: { "Content-Type" => "application/json" })
150
+ service_response = service.delete_job(
151
+ id: "jobid"
152
+ )
153
+ assert_nil(service_response)
154
+ end
155
+
156
+ def test_callbacks
157
+ service = IBMWatson::SpeechToTextV1.new(
158
+ username: "username",
159
+ password: "password"
160
+ )
161
+ stub_request(:post, "https://stream.watsonplatform.net/speech-to-text/api/v1/register_callback?callback_url=monitorcalls.com")
162
+ .with(
163
+ headers: {
164
+ "Accept" => "application/json",
165
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
166
+ "Host" => "stream.watsonplatform.net"
167
+ }
168
+ ).to_return(status: 200, body: { "status" => "created", "url" => "monitorcalls.com" }.to_json, headers: { "Content-Type" => "application/json" })
169
+ service_response = service.register_callback(
170
+ callback_url: "monitorcalls.com"
171
+ )
172
+ assert_equal({ "status" => "created", "url" => "monitorcalls.com" }, service_response.result)
173
+
174
+ stub_request(:post, "https://stream.watsonplatform.net/speech-to-text/api/v1/unregister_callback?callback_url=monitorcalls.com")
175
+ .with(
176
+ headers: {
177
+ "Accept" => "application/json",
178
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
179
+ "Host" => "stream.watsonplatform.net"
180
+ }
181
+ ).to_return(status: 200, body: { "response" => "The callback URL was successfully unregistered" }.to_json, headers: { "Content-Type" => "applicaton/json" })
182
+ service_response = service.unregister_callback(
183
+ callback_url: "monitorcalls.com"
184
+ )
185
+ assert_nil(service_response)
186
+ end
187
+
188
+ def test_custom_model
189
+ service = IBMWatson::SpeechToTextV1.new(
190
+ username: "username",
191
+ password: "password"
192
+ )
193
+ stub_request(:get, "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations")
194
+ .with(
195
+ headers: {
196
+ "Accept" => "application/json",
197
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
198
+ "Host" => "stream.watsonplatform.net"
199
+ }
200
+ ).to_return(status: 200, body: { "get response" => "yep" }.to_json, headers: { "Content-Type" => "application/json" })
201
+ service_response = service.list_language_models
202
+ assert_equal({ "get response" => "yep" }, service_response.result)
203
+
204
+ stub_request(:post, "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations")
205
+ .with(
206
+ body: "{\"name\":\"Example model\",\"base_model_name\":\"en-US_BroadbandModel\"}",
207
+ headers: {
208
+ "Accept" => "application/json",
209
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
210
+ "Content-Type" => "application/json",
211
+ "Host" => "stream.watsonplatform.net"
212
+ }
213
+ ).to_return(status: 200, body: { "bogus_response" => "yep" }.to_json, headers: { "Content-Type" => "application/json" })
214
+ service_response = service.create_language_model(
215
+ name: "Example model",
216
+ base_model_name: "en-US_BroadbandModel"
217
+ )
218
+ assert_equal({ "bogus_response" => "yep" }, service_response.result)
219
+
220
+ stub_request(:post, "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/customid/train")
221
+ .with(
222
+ headers: {
223
+ "Accept" => "application/json",
224
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
225
+ "Host" => "stream.watsonplatform.net"
226
+ }
227
+ ).to_return(status: 200, body: { "bogus_response" => "yep" }.to_json, headers: { "Content-Type" => "application/json" })
228
+ service_response = service.train_language_model(
229
+ customization_id: "customid"
230
+ )
231
+ assert_nil(service_response)
232
+
233
+ stub_request(:get, "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/modelid")
234
+ .with(
235
+ headers: {
236
+ "Accept" => "application/json",
237
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
238
+ "Host" => "stream.watsonplatform.net"
239
+ }
240
+ ).to_return(status: 200, body: { "bogus_response" => "yep" }.to_json, headers: { "Content-Type" => "application/json" })
241
+ service_response = service.get_language_model(
242
+ customization_id: "modelid"
243
+ )
244
+ assert_equal({ "bogus_response" => "yep" }, service_response.result)
245
+
246
+ stub_request(:delete, "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/modelid")
247
+ .with(
248
+ headers: {
249
+ "Accept" => "application/json",
250
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
251
+ "Host" => "stream.watsonplatform.net"
252
+ }
253
+ ).to_return(status: 200, body: { "bogus_response" => "yep" }.to_json, headers: { "Content-Type" => "application/json" })
254
+ service_response = service.delete_language_model(
255
+ customization_id: "modelid"
256
+ )
257
+ assert_nil(service_response)
258
+ end
259
+
260
+ def test_acoustic_model
261
+ service = IBMWatson::SpeechToTextV1.new(
262
+ username: "username",
263
+ password: "password"
264
+ )
265
+ stub_request(:get, "https://stream.watsonplatform.net/speech-to-text/api/v1/acoustic_customizations")
266
+ .with(
267
+ headers: {
268
+ "Accept" => "application/json",
269
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
270
+ "Host" => "stream.watsonplatform.net"
271
+ }
272
+ ).to_return(status: 200, body: { "get response" => "yep" }.to_json, headers: { "Content-Type" => "application/json" })
273
+ service_response = service.list_acoustic_models
274
+ assert_equal({ "get response" => "yep" }, service_response.result)
275
+
276
+ stub_request(:post, "https://stream.watsonplatform.net/speech-to-text/api/v1/acoustic_customizations")
277
+ .with(
278
+ body: "{\"name\":\"Example model\",\"base_model_name\":\"en-US_BroadbandModel\",\"description\":\"Example custom language model\"}",
279
+ headers: {
280
+ "Accept" => "application/json",
281
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
282
+ "Content-Type" => "application/json",
283
+ "Host" => "stream.watsonplatform.net"
284
+ }
285
+ ).to_return(status: 200, body: { "bogus_response" => "yep" }.to_json, headers: { "Content-Type" => "application/json" })
286
+ service_response = service.create_acoustic_model(
287
+ name: "Example model",
288
+ base_model_name: "en-US_BroadbandModel",
289
+ description: "Example custom language model"
290
+ )
291
+ assert_equal({ "bogus_response" => "yep" }, service_response.result)
292
+
293
+ stub_request(:post, "https://stream.watsonplatform.net/speech-to-text/api/v1/acoustic_customizations/customid/train")
294
+ .with(
295
+ headers: {
296
+ "Accept" => "application/json",
297
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
298
+ "Host" => "stream.watsonplatform.net"
299
+ }
300
+ ).to_return(status: 200, body: "", headers: {})
301
+ service_response = service.train_acoustic_model(
302
+ customization_id: "customid"
303
+ )
304
+ assert_nil(service_response)
305
+
306
+ stub_request(:get, "https://stream.watsonplatform.net/speech-to-text/api/v1/acoustic_customizations/modelid")
307
+ .with(
308
+ headers: {
309
+ "Accept" => "application/json",
310
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
311
+ "Host" => "stream.watsonplatform.net"
312
+ }
313
+ ).to_return(status: 200, body: { "bogus_response" => "yep" }.to_json, headers: { "Content-Type" => "application/json" })
314
+ service_response = service.get_acoustic_model(
315
+ customization_id: "modelid"
316
+ )
317
+ assert_equal({ "bogus_response" => "yep" }, service_response.result)
318
+
319
+ stub_request(:delete, "https://stream.watsonplatform.net/speech-to-text/api/v1/acoustic_customizations/modelid")
320
+ .with(
321
+ headers: {
322
+ "Accept" => "application/json",
323
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
324
+ "Host" => "stream.watsonplatform.net"
325
+ }
326
+ ).to_return(status: 200, body: { "bogus_response" => "yep" }.to_json, headers: { "Content-Type" => "application/json" })
327
+ service_response = service.delete_acoustic_model(
328
+ customization_id: "modelid"
329
+ )
330
+ assert_nil(service_response)
331
+ end
332
+
333
+ def test_custom_corpora
334
+ service = IBMWatson::SpeechToTextV1.new(
335
+ username: "username",
336
+ password: "password"
337
+ )
338
+ stub_request(:get, "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/customid/corpora")
339
+ .with(
340
+ headers: {
341
+ "Accept" => "application/json",
342
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
343
+ "Host" => "stream.watsonplatform.net"
344
+ }
345
+ ).to_return(status: 200, body: { "get response" => "yep" }.to_json, headers: { "Content-Type" => "application/json" })
346
+ service_response = service.list_corpora(
347
+ customization_id: "customid"
348
+ )
349
+ assert_equal({ "get response" => "yep" }, service_response.result)
350
+
351
+ stub_request(:post, "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/customid/corpora/corpus")
352
+ .with(
353
+ headers: {
354
+ "Accept" => "application/json",
355
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
356
+ "Host" => "stream.watsonplatform.net"
357
+ }
358
+ ).to_return(status: 200, body: { "get response" => "yep" }.to_json, headers: { "Content-Type" => "application/json" })
359
+ corpus_file = File.open(Dir.getwd + "/resources/speech_to_text/corpus-short-1.txt")
360
+ service_response = service.add_corpus(
361
+ customization_id: "customid",
362
+ corpus_name: "corpus",
363
+ corpus_file: corpus_file,
364
+ corpus_filename: "corpus-short-1.txt"
365
+ )
366
+ assert_nil(service_response)
367
+
368
+ service_response = service.add_corpus(
369
+ customization_id: "customid",
370
+ corpus_name: "corpus",
371
+ corpus_file: "corpus_file"
372
+ )
373
+ assert_nil(service_response)
374
+
375
+ stub_request(:get, "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/customid/corpora/corpus")
376
+ .with(
377
+ headers: {
378
+ "Accept" => "application/json",
379
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
380
+ "Host" => "stream.watsonplatform.net"
381
+ }
382
+ ).to_return(status: 200, body: { "get response" => "yep" }.to_json, headers: { "Content-Type" => "application/json" })
383
+ service_response = service.get_corpus(
384
+ customization_id: "customid",
385
+ corpus_name: "corpus"
386
+ )
387
+ assert_equal({ "get response" => "yep" }, service_response.result)
388
+
389
+ stub_request(:delete, "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/customid/corpora/corpus")
390
+ .with(
391
+ headers: {
392
+ "Accept" => "application/json",
393
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
394
+ "Host" => "stream.watsonplatform.net"
395
+ }
396
+ ).to_return(status: 200, body: { "get response" => "yep" }.to_json, headers: { "Content-Type" => "application/json" })
397
+ service_response = service.delete_corpus(
398
+ customization_id: "customid",
399
+ corpus_name: "corpus"
400
+ )
401
+ assert_nil(service_response)
402
+ end
403
+
404
+ def test_custom_words
405
+ service = IBMWatson::SpeechToTextV1.new(
406
+ username: "username",
407
+ password: "password"
408
+ )
409
+ stub_request(:put, "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/custid/words/IEEE")
410
+ .with(
411
+ body: "{\"sounds_like\":[\"i triple e\"],\"display_as\":\"IEEE\"}",
412
+ headers: {
413
+ "Accept" => "application/json",
414
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
415
+ "Content-Type" => "application/json",
416
+ "Host" => "stream.watsonplatform.net"
417
+ }
418
+ ).to_return(status: 200, body: { "get response" => "yep" }.to_json, headers: { "Content-Type" => "application/json" })
419
+ service_response = service.add_word(
420
+ customization_id: "custid",
421
+ word_name: "IEEE",
422
+ sounds_like: ["i triple e"],
423
+ display_as: "IEEE"
424
+ )
425
+ assert_nil(service_response)
426
+
427
+ stub_request(:delete, "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/custid/words/wordname")
428
+ .with(
429
+ headers: {
430
+ "Accept" => "application/json",
431
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
432
+ "Host" => "stream.watsonplatform.net"
433
+ }
434
+ ).to_return(status: 200, body: { "get response" => "yep" }.to_json, headers: { "Content-Type" => "application/json" })
435
+ service_response = service.delete_word(
436
+ customization_id: "custid",
437
+ word_name: "wordname"
438
+ )
439
+ assert_nil(service_response)
440
+
441
+ stub_request(:delete, "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/custid/words/IEEE")
442
+ .with(
443
+ headers: {
444
+ "Accept" => "application/json",
445
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
446
+ "Host" => "stream.watsonplatform.net"
447
+ }
448
+ ).to_return(status: 200, body: { "get response" => "yep" }.to_json, headers: { "Content-Type" => "application/json" })
449
+ service_response = service.delete_word(
450
+ customization_id: "custid",
451
+ word_name: "IEEE"
452
+ )
453
+ assert_nil(service_response)
454
+
455
+ custom_word = {
456
+ word: "IEEE",
457
+ sounds_like: [" i triple e"],
458
+ display_as: "IEEE"
459
+ }
460
+ custom_words = [custom_word, custom_word, custom_word]
461
+
462
+ stub_request(:post, "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/custid/words")
463
+ .with(
464
+ body: "{\"words\":[{\"word\":\"IEEE\",\"sounds_like\":[\" i triple e\"],\"display_as\":\"IEEE\"},{\"word\":\"IEEE\",\"sounds_like\":[\" i triple e\"],\"display_as\":\"IEEE\"},{\"word\":\"IEEE\",\"sounds_like\":[\" i triple e\"],\"display_as\":\"IEEE\"}]}",
465
+ headers: {
466
+ "Accept" => "application/json",
467
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
468
+ "Content-Type" => "application/json",
469
+ "Host" => "stream.watsonplatform.net"
470
+ }
471
+ ).to_return(status: 200, body: { "get response" => "yep" }.to_json, headers: { "Content-Type" => "application/json" })
472
+ service_response = service.add_words(
473
+ customization_id: "custid",
474
+ words: custom_words
475
+ )
476
+ assert_nil(service_response)
477
+
478
+ stub_request(:get, "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/custid/words/IEEE")
479
+ .with(
480
+ headers: {
481
+ "Accept" => "application/json",
482
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
483
+ "Host" => "stream.watsonplatform.net"
484
+ }
485
+ ).to_return(status: 200, body: { "get response" => "yep" }.to_json, headers: { "Content-Type" => "application/json" })
486
+ service_response = service.get_word(
487
+ customization_id: "custid",
488
+ word_name: "IEEE"
489
+ )
490
+ assert_equal({ "get response" => "yep" }, service_response.result)
491
+
492
+ stub_request(:get, "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/custid/words/wordname")
493
+ .with(
494
+ headers: {
495
+ "Accept" => "application/json",
496
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
497
+ "Host" => "stream.watsonplatform.net"
498
+ }
499
+ ).to_return(status: 200, body: { "get response" => "yep" }.to_json, headers: { "Content-Type" => "application/json" })
500
+ service_response = service.get_word(
501
+ customization_id: "custid",
502
+ word_name: "wordname"
503
+ )
504
+ assert_equal({ "get response" => "yep" }, service_response.result)
505
+
506
+ stub_request(:get, "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/custid/words")
507
+ .with(
508
+ headers: {
509
+ "Accept" => "application/json",
510
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
511
+ "Host" => "stream.watsonplatform.net"
512
+ }
513
+ ).to_return(status: 200, body: { "get response" => "yep" }.to_json, headers: { "Content-Type" => "application/json" })
514
+ service_response = service.list_words(
515
+ customization_id: "custid"
516
+ )
517
+ assert_equal({ "get response" => "yep" }, service_response.result)
518
+
519
+ stub_request(:get, "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/custid/words?sort=alphabetical")
520
+ .with(
521
+ headers: {
522
+ "Accept" => "application/json",
523
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
524
+ "Host" => "stream.watsonplatform.net"
525
+ }
526
+ ).to_return(status: 200, body: { "get response" => "yep" }.to_json, headers: { "Content-Type" => "application/json" })
527
+ service_response = service.list_words(
528
+ customization_id: "custid",
529
+ sort: "alphabetical"
530
+ )
531
+ assert_equal({ "get response" => "yep" }, service_response.result)
532
+
533
+ stub_request(:get, "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/custid/words?word_type=all")
534
+ .with(
535
+ headers: {
536
+ "Accept" => "application/json",
537
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
538
+ "Host" => "stream.watsonplatform.net"
539
+ }
540
+ ).to_return(status: 200, body: { "get response" => "yep" }.to_json, headers: { "Content-Type" => "application/json" })
541
+ service_response = service.list_words(
542
+ customization_id: "custid",
543
+ word_type: "all"
544
+ )
545
+ assert_equal({ "get response" => "yep" }, service_response.result)
546
+ end
547
+
548
+ def test_custom_audio_resources
549
+ service = IBMWatson::SpeechToTextV1.new(
550
+ username: "username",
551
+ password: "password"
552
+ )
553
+ audio_file = File.open(Dir.getwd + "/resources/speech.wav")
554
+ stub_request(:post, "https://stream.watsonplatform.net/speech-to-text/api/v1/acoustic_customizations/custid/audio/hiee")
555
+ .with(
556
+ headers: {
557
+ "Accept" => "application/json",
558
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
559
+ "Content-Type" => "application/json",
560
+ "Host" => "stream.watsonplatform.net"
561
+ }
562
+ ).to_return(status: 200, body: { "post response" => "done" }.to_json, headers: { "Content-Type" => "application/json" })
563
+ service_response = service.add_audio(
564
+ customization_id: "custid",
565
+ audio_name: "hiee",
566
+ audio_resource: audio_file,
567
+ content_type: "application/json"
568
+ )
569
+ assert_nil(service_response)
570
+
571
+ stub_request(:delete, "https://stream.watsonplatform.net/speech-to-text/api/v1/acoustic_customizations/custid/audio/hiee")
572
+ .with(
573
+ headers: {
574
+ "Accept" => "application/json",
575
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
576
+ "Host" => "stream.watsonplatform.net"
577
+ }
578
+ ).to_return(status: 200, body: { "post response" => "done" }.to_json, headers: { "Content-Type" => "application/json" })
579
+ service_response = service.delete_audio(
580
+ customization_id: "custid",
581
+ audio_name: "hiee"
582
+ )
583
+ assert_nil(service_response)
584
+
585
+ stub_request(:get, "https://stream.watsonplatform.net/speech-to-text/api/v1/acoustic_customizations/custid/audio/hiee")
586
+ .with(
587
+ headers: {
588
+ "Accept" => "application/json",
589
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
590
+ "Host" => "stream.watsonplatform.net"
591
+ }
592
+ ).to_return(status: 200, body: { "get response" => "done" }.to_json, headers: { "Content-Type" => "application/json" })
593
+ service_response = service.get_audio(
594
+ customization_id: "custid",
595
+ audio_name: "hiee"
596
+ )
597
+ assert_equal({ "get response" => "done" }, service_response.result)
598
+
599
+ stub_request(:get, "https://stream.watsonplatform.net/speech-to-text/api/v1/acoustic_customizations/custid/audio")
600
+ .with(
601
+ headers: {
602
+ "Accept" => "application/json",
603
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
604
+ "Host" => "stream.watsonplatform.net"
605
+ }
606
+ ).to_return(status: 200, body: { "get response all" => "done" }.to_json, headers: { "Content-Type" => "application/json" })
607
+ service_response = service.list_audio(
608
+ customization_id: "custid"
609
+ )
610
+ assert_equal({ "get response all" => "done" }, service_response.result)
611
+ end
612
+
613
+ def test_delete_user_data
614
+ service = IBMWatson::SpeechToTextV1.new(
615
+ username: "username",
616
+ password: "password"
617
+ )
618
+ stub_request(:delete, "https://stream.watsonplatform.net/speech-to-text/api/v1/user_data?customer_id=id")
619
+ .with(
620
+ headers: {
621
+ "Accept" => "application/json",
622
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
623
+ "Host" => "stream.watsonplatform.net"
624
+ }
625
+ ).to_return(status: 200, body: { "description" => "success" }.to_json, headers: { "Content-Type" => "application/json" })
626
+ service_response = service.delete_user_data(
627
+ customer_id: "id"
628
+ )
629
+ assert_nil(service_response)
630
+ end
631
+
632
+ def test_recognize_await
633
+ service = IBMWatson::SpeechToTextV1.new(
634
+ username: "username",
635
+ password: "password"
636
+ )
637
+ audio_file = File.open(Dir.getwd + "/resources/speech.wav")
638
+ recognize_response = {
639
+ "results" => [
640
+ {
641
+ "alternatives" => [
642
+ {
643
+ "transcript" => "thunderstorms could produce large hail isolated tornadoes and heavy rain"
644
+ }
645
+ ],
646
+ "final" => true
647
+ }
648
+ ],
649
+ "result_index" => 0
650
+ }
651
+ stub_request(:post, "https://stream.watsonplatform.net/speech-to-text/api/v1/recognize")
652
+ .with(
653
+ headers: {
654
+ "Accept" => "application/json",
655
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
656
+ "Content-Type" => "audio/l16; rate=44100",
657
+ "Host" => "stream.watsonplatform.net"
658
+ }
659
+ ).to_return(status: 200, body: recognize_response.to_json, headers: { "Content-Type" => "application/json" })
660
+ future = service.await.recognize(
661
+ audio: audio_file,
662
+ content_type: "audio/l16; rate=44100"
663
+ )
664
+ output = future.value.result
665
+ assert_equal(recognize_response["results"][0]["alternatives"][0]["transcript"], output["results"][0]["alternatives"][0]["transcript"])
666
+ end
667
+
668
+ def test_recognize_async
669
+ service = IBMWatson::SpeechToTextV1.new(
670
+ username: "username",
671
+ password: "password"
672
+ )
673
+ audio_file = File.open(Dir.getwd + "/resources/speech.wav")
674
+ recognize_response = {
675
+ "results" => [
676
+ {
677
+ "alternatives" => [
678
+ {
679
+ "transcript" => "thunderstorms could produce large hail isolated tornadoes and heavy rain"
680
+ }
681
+ ],
682
+ "final" => true
683
+ }
684
+ ],
685
+ "result_index" => 0
686
+ }
687
+ stub_request(:post, "https://stream.watsonplatform.net/speech-to-text/api/v1/recognize")
688
+ .with(
689
+ headers: {
690
+ "Accept" => "application/json",
691
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
692
+ "Content-Type" => "audio/l16; rate=44100",
693
+ "Host" => "stream.watsonplatform.net"
694
+ }
695
+ ).to_return(status: 200, body: recognize_response.to_json, headers: { "Content-Type" => "application/json" })
696
+ future = service.async.recognize(
697
+ audio: audio_file,
698
+ content_type: "audio/l16; rate=44100"
699
+ )
700
+ future.wait!
701
+ output = future.value.result
702
+ assert_equal(recognize_response["results"][0]["alternatives"][0]["transcript"], output["results"][0]["alternatives"][0]["transcript"])
703
+ end
704
+
705
+ def test_reset_language_model
706
+ service = IBMWatson::SpeechToTextV1.new(
707
+ username: "username",
708
+ password: "password"
709
+ )
710
+ stub_request(:post, "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/customization_id/reset")
711
+ .with(
712
+ headers: {
713
+ "Accept" => "application/json",
714
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
715
+ "Host" => "stream.watsonplatform.net"
716
+ }
717
+ ).to_return(status: 200, body: "", headers: {})
718
+ service_response = service.reset_language_model(customization_id: "customization_id")
719
+ assert_nil(service_response)
720
+ end
721
+
722
+ def test_upgrade_language_model
723
+ service = IBMWatson::SpeechToTextV1.new(
724
+ username: "username",
725
+ password: "password"
726
+ )
727
+ stub_request(:post, "https://stream.watsonplatform.net/speech-to-text/api/v1/customizations/customization_id/upgrade_model")
728
+ .with(
729
+ headers: {
730
+ "Accept" => "application/json",
731
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
732
+ "Host" => "stream.watsonplatform.net"
733
+ }
734
+ ).to_return(status: 200, body: "", headers: {})
735
+ service_response = service.upgrade_language_model(customization_id: "customization_id")
736
+ assert_nil(service_response)
737
+ end
738
+
739
+ def test_upgrade_acoustic_model
740
+ service = IBMWatson::SpeechToTextV1.new(
741
+ username: "username",
742
+ password: "password"
743
+ )
744
+ stub_request(:post, "https://stream.watsonplatform.net/speech-to-text/api/v1/acoustic_customizations/customization_id/upgrade_model")
745
+ .with(
746
+ headers: {
747
+ "Accept" => "application/json",
748
+ "Authorization" => "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
749
+ "Host" => "stream.watsonplatform.net"
750
+ }
751
+ ).to_return(status: 200, body: "", headers: {})
752
+ service_response = service.upgrade_acoustic_model(customization_id: "customization_id")
753
+ assert_nil(service_response)
754
+ end
755
+ end