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,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require("json")
4
+ require_relative("./../test_helper.rb")
5
+ require("minitest/hooks/test")
6
+
7
+ # Integration tests for the Language Translator V3 Service
8
+ class LanguageTranslatorV3Test < Minitest::Test
9
+ include Minitest::Hooks
10
+ attr_accessor :service
11
+ def before_all
12
+ @service = IBMWatson::LanguageTranslatorV3.new(
13
+ username: ENV["LANGUAGE_TRANSLATOR_V3_USERNAME"],
14
+ password: ENV["LANGUAGE_TRANSLATOR_V3_PASSWORD"],
15
+ version: "2018-05-01"
16
+ )
17
+ @service.add_default_headers(
18
+ headers: {
19
+ "X-Watson-Learning-Opt-Out" => "1",
20
+ "X-Watson-Test" => "1"
21
+ }
22
+ )
23
+ end
24
+
25
+ def test_get_model
26
+ service_response = service.get_model(
27
+ model_id: "en-it"
28
+ ).result
29
+ refute(service_response.nil?)
30
+ end
31
+
32
+ def test_list_models
33
+ service_response = service.list_models.result
34
+ refute(service_response.nil?)
35
+ end
36
+
37
+ def test_translate_source_target
38
+ service_response = service.translate(
39
+ text: "Hola, cómo estás? €",
40
+ source: "es",
41
+ target: "en"
42
+ ).result
43
+ refute(service_response.nil?)
44
+ end
45
+
46
+ def test_translate_model_id
47
+ service_response = service.translate(
48
+ text: "Messi is the best ever",
49
+ model_id: "en-es"
50
+ ).result
51
+ refute(service_response.nil?)
52
+ end
53
+
54
+ def test_identify
55
+ service_response = service.identify(
56
+ text: "祝你有美好的一天"
57
+ ).result
58
+ refute(service_response.nil?)
59
+ end
60
+
61
+ def test_list_identifiable_languages
62
+ service_response = service.list_identifiable_languages.result
63
+ refute(service_response.nil?)
64
+ end
65
+
66
+ def test_create_delete_model
67
+ skip "Methods not available in lite plans"
68
+ custom_model = File.open(Dir.getwd + "/resources/language_translator_model.tmx")
69
+ service_response = service.create_model(
70
+ base_model_id: "en-fr",
71
+ name: "test_glossary_ruby_integration",
72
+ forced_glossary: custom_model
73
+ ).result
74
+ refute(service_response.nil?)
75
+ model_id = service_response["model_id"]
76
+ service_response = service.delete_model(
77
+ model_id: model_id
78
+ ).result
79
+ assert_equal("OK", service_response["status"])
80
+ end
81
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require("json")
4
+ require_relative("./../test_helper.rb")
5
+ require("minitest/hooks/test")
6
+
7
+ # Integration tests for the Natural Language Classifier V1 Service
8
+ class NaturalLanguageClassifierV1Test < Minitest::Test
9
+ include Minitest::Hooks
10
+ attr_accessor :service
11
+ def before_all
12
+ @service = IBMWatson::NaturalLanguageClassifierV1.new(
13
+ username: ENV["NATURAL_LANGUAGE_CLASSIFIER_USERNAME"],
14
+ password: ENV["NATURAL_LANGUAGE_CLASSIFIER_PASSWORD"]
15
+ )
16
+ @service.add_default_headers(
17
+ headers: {
18
+ "X-Watson-Learning-Opt-Out" => "1",
19
+ "X-Watson-Test" => "1"
20
+ }
21
+ )
22
+ end
23
+
24
+ def test_list_classifier
25
+ list_classifiers = @service.list_classifiers.result
26
+ refute(list_classifiers.nil?)
27
+ end
28
+
29
+ def test_classify_text
30
+ skip "The classifier takes more than a minute"
31
+ training_data = File.open(Dir.getwd + "/resources/weather_data_train.csv")
32
+ metadata = { "name" => "my-classifier", "language" => "en" }
33
+ status = nil
34
+ classifier = @service.create_classifier(
35
+ metadata: metadata,
36
+ training_data: training_data
37
+ ).result
38
+ classifier_id = classifier["classifier_id"]
39
+ iterations = 0
40
+ while iterations < 15
41
+ status = @service.get_classifier(classifier_id: classifier_id)
42
+ iterations += 1
43
+ sleep(5) unless status["status"] == "Available"
44
+ end
45
+ unless status["status"] == "Available"
46
+ @service.delete_classifier(
47
+ classifier_id: classifier_id
48
+ )
49
+ assert(false, msg: "Classifier is not available")
50
+ end
51
+
52
+ classes = @service.classify(
53
+ classifier_id: classifier_id,
54
+ text: "How hot will it be tomorrow?"
55
+ ).result
56
+ refute(classes.nil?)
57
+
58
+ collection = [{ "text" => "How hot will it be today?" }, { "text" => "Is it hot outside?" }]
59
+ classes = @service.classify_collection(
60
+ classifier_id: classifier_id,
61
+ collection: collection
62
+ ).result
63
+ refute(classes.nil?)
64
+
65
+ @service.delete_classifier(
66
+ classifier_id: classifier_id
67
+ )
68
+ end
69
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require("json")
4
+ require_relative("./../test_helper.rb")
5
+
6
+ # Integration tests for the Natural Language Understanding V1 Service
7
+ class NaturalLanguageUnderstandingV1Test < Minitest::Test
8
+ def test_text_analyze
9
+ service = IBMWatson::NaturalLanguageUnderstandingV1.new(
10
+ version: "2018-03-16",
11
+ username: ENV["NATURAL_LANGUAGE_UNDERSTANDING_USERNAME"],
12
+ password: ENV["NATURAL_LANGUAGE_UNDERSTANDING_PASSWORD"]
13
+ )
14
+ service.add_default_headers(
15
+ headers: {
16
+ "X-Watson-Learning-Opt-Out" => "1",
17
+ "X-Watson-Test" => "1"
18
+ }
19
+ )
20
+ text = "IBM is an American multinational technology company "
21
+ text += "headquartered in Armonk, New York, United States, "
22
+ text += "with operations in over 170 countries."
23
+ service_response = service.analyze(
24
+ features: {
25
+ entities: {
26
+ emotion: true,
27
+ sentiment: true,
28
+ limit: 2
29
+ },
30
+ keywords: {
31
+ emotion: true,
32
+ sentiment: true,
33
+ limit: 2
34
+ }
35
+ },
36
+ text: text
37
+ )
38
+ assert((200..299).cover?(service_response.status))
39
+ end
40
+
41
+ def test_html_analyze
42
+ service = IBMWatson::NaturalLanguageUnderstandingV1.new(
43
+ version: "2018-03-16",
44
+ username: ENV["NATURAL_LANGUAGE_UNDERSTANDING_USERNAME"],
45
+ password: ENV["NATURAL_LANGUAGE_UNDERSTANDING_PASSWORD"]
46
+ )
47
+ service.add_default_headers(
48
+ headers: {
49
+ "X-Watson-Learning-Opt-Out" => "1",
50
+ "X-Watson-Test" => "1"
51
+ }
52
+ )
53
+ service_response = service.analyze(
54
+ features: {
55
+ sentiment: {}
56
+ },
57
+ html: "<span>hello this is a test </span>"
58
+ )
59
+ assert((200..299).cover?(service_response.status))
60
+ end
61
+
62
+ def test_url_analyze
63
+ service = IBMWatson::NaturalLanguageUnderstandingV1.new(
64
+ version: "2018-03-16",
65
+ username: ENV["NATURAL_LANGUAGE_UNDERSTANDING_USERNAME"],
66
+ password: ENV["NATURAL_LANGUAGE_UNDERSTANDING_PASSWORD"]
67
+ )
68
+ service.add_default_headers(
69
+ headers: {
70
+ "X-Watson-Learning-Opt-Out" => "1",
71
+ "X-Watson-Test" => "1"
72
+ }
73
+ )
74
+ service_response = service.analyze(
75
+ features: {
76
+ categories: {}
77
+ },
78
+ url: "www.ibm.com"
79
+ )
80
+ assert((200..299).cover?(service_response.status))
81
+ end
82
+
83
+ def test_list_models
84
+ service = IBMWatson::NaturalLanguageUnderstandingV1.new(
85
+ version: "2018-03-16",
86
+ username: ENV["NATURAL_LANGUAGE_UNDERSTANDING_USERNAME"],
87
+ password: ENV["NATURAL_LANGUAGE_UNDERSTANDING_PASSWORD"]
88
+ )
89
+ service.add_default_headers(
90
+ headers: {
91
+ "X-Watson-Learning-Opt-Out" => "1",
92
+ "X-Watson-Test" => "1"
93
+ }
94
+ )
95
+ service_response = service.list_models
96
+ assert((200..299).cover?(service_response.status))
97
+ end
98
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require("json")
4
+ require_relative("./../test_helper.rb")
5
+
6
+ # Integration tests for the Personality Insights V3 Service
7
+ class PersonalityInsightsV3Test < Minitest::Test
8
+ def test_plain_to_json
9
+ personality_text = File.read(Dir.getwd + "/resources/personality-v3.txt")
10
+ service = IBMWatson::PersonalityInsightsV3.new(
11
+ version: "2017-10-13",
12
+ username: ENV["PERSONALITY_INSIGHTS_USERNAME"],
13
+ password: ENV["PERSONALITY_INSIGHTS_PASSWORD"]
14
+ )
15
+ service.add_default_headers(
16
+ headers: {
17
+ "X-Watson-Learning-Opt-Out" => "1",
18
+ "X-Watson-Test" => "1"
19
+ }
20
+ )
21
+ service_response = service.profile(
22
+ content: personality_text,
23
+ content_type: "text/plain;charset=utf-8"
24
+ )
25
+ assert((200..299).cover?(service_response.status))
26
+ end
27
+
28
+ def test_json_to_json
29
+ personality_text = File.read(Dir.getwd + "/resources/personality-v3.json")
30
+ service = IBMWatson::PersonalityInsightsV3.new(
31
+ version: "2017-10-13",
32
+ username: ENV["PERSONALITY_INSIGHTS_USERNAME"],
33
+ password: ENV["PERSONALITY_INSIGHTS_PASSWORD"]
34
+ )
35
+ service.add_default_headers(
36
+ headers: {
37
+ "X-Watson-Learning-Opt-Out" => "1",
38
+ "X-Watson-Test" => "1"
39
+ }
40
+ )
41
+ service_response = service.profile(
42
+ content: personality_text,
43
+ content_type: "application/json",
44
+ raw_scores: true,
45
+ consumption_preferences: true
46
+ )
47
+ assert((200..299).cover?(service_response.status))
48
+ end
49
+
50
+ def test_json_to_csv
51
+ personality_text = File.read(Dir.getwd + "/resources/personality-v3.json")
52
+ service = IBMWatson::PersonalityInsightsV3.new(
53
+ version: "2017-10-13",
54
+ username: ENV["PERSONALITY_INSIGHTS_USERNAME"],
55
+ password: ENV["PERSONALITY_INSIGHTS_PASSWORD"]
56
+ )
57
+ service.add_default_headers(
58
+ headers: {
59
+ "X-Watson-Learning-Opt-Out" => "1",
60
+ "X-Watson-Test" => "1"
61
+ }
62
+ )
63
+ service_response = service.profile(
64
+ content: personality_text,
65
+ content_type: "application/json",
66
+ accept: "text/csv",
67
+ csv_headers: true,
68
+ raw_scores: true,
69
+ consumption_preferences: true
70
+ )
71
+ assert((200..299).cover?(service_response.status))
72
+ end
73
+
74
+ def test_plain_to_json_es
75
+ personality_text = File.read(Dir.getwd + "/resources/personality-v3-es.txt")
76
+ service = IBMWatson::PersonalityInsightsV3.new(
77
+ version: "2017-10-13",
78
+ username: ENV["PERSONALITY_INSIGHTS_USERNAME"],
79
+ password: ENV["PERSONALITY_INSIGHTS_PASSWORD"]
80
+ )
81
+ service.add_default_headers(
82
+ headers: {
83
+ "X-Watson-Learning-Opt-Out" => "1",
84
+ "X-Watson-Test" => "1"
85
+ }
86
+ )
87
+ service_response = service.profile(
88
+ content: personality_text,
89
+ content_type: "text/plain;charset=utf-8",
90
+ content_language: "es",
91
+ accept_language: "es"
92
+ )
93
+ assert((200..299).cover?(service_response.status))
94
+ end
95
+ end
@@ -0,0 +1,187 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative("./../test_helper.rb")
4
+ require_relative("./../../lib/ibm_watson/websocket/recognize_callback.rb")
5
+ require_relative("./../../lib/ibm_watson/websocket/speech_to_text_websocket_listener.rb")
6
+ require("minitest/hooks/test")
7
+ require("concurrent")
8
+
9
+ # Recognize Callback class
10
+ class MyRecognizeCallback < IBMWatson::RecognizeCallback
11
+ def initialize(atomic_boolean: nil)
12
+ super
13
+ @atomic_boolean = atomic_boolean
14
+ end
15
+
16
+ def on_error(error:)
17
+ puts "Error received: #{error}"
18
+ end
19
+
20
+ def on_inactivity_timeout(*)
21
+ @atomic_boolean.make_true
22
+ end
23
+ end
24
+
25
+ # Integration tests for the Speech to Text V1 Service
26
+ class SpeechToTextV1Test < Minitest::Test
27
+ include Minitest::Hooks
28
+
29
+ attr_accessor :service
30
+ def before_all
31
+ @service = IBMWatson::SpeechToTextV1.new(
32
+ username: ENV["SPEECH_TO_TEXT_USERNAME"],
33
+ password: ENV["SPEECH_TO_TEXT_PASSWORD"]
34
+ )
35
+ @service.add_default_headers(
36
+ headers: {
37
+ "X-Watson-Learning-Opt-Out" => "1",
38
+ "X-Watson-Test" => "1"
39
+ }
40
+ )
41
+ end
42
+
43
+ def test_models
44
+ output = @service.list_models.result
45
+ refute_nil(output)
46
+
47
+ model = @service.get_model(
48
+ model_id: "ko-KR_BroadbandModel"
49
+ ).result
50
+ model = model
51
+ refute_nil(model)
52
+
53
+ begin
54
+ @service.get_model(
55
+ model_id: "bogus"
56
+ )
57
+ rescue StandardError => e
58
+ refute_nil(e.global_transaction_id)
59
+ end
60
+ end
61
+
62
+ def test_recognize_await
63
+ audio_file = File.open(Dir.getwd + "/resources/speech.wav")
64
+ future = @service.await.recognize(
65
+ audio: audio_file,
66
+ content_type: "audio/l16; rate=44100"
67
+ )
68
+ output = future.value.result
69
+ refute_nil(output["results"][0]["alternatives"][0]["transcript"])
70
+ end
71
+
72
+ def test_recognize_async
73
+ audio_file = File.open(Dir.getwd + "/resources/speech.wav")
74
+ future = @service.async.recognize(
75
+ audio: audio_file,
76
+ content_type: "audio/l16; rate=44100"
77
+ )
78
+ future.wait!
79
+ output = future.value.result
80
+ refute_nil(output["results"][0]["alternatives"][0]["transcript"])
81
+ end
82
+
83
+ def test_recognitions
84
+ output = @service.check_jobs.result
85
+ refute_nil(output)
86
+ end
87
+
88
+ def test_custom_corpora
89
+ model = @service.create_language_model(
90
+ name: "integration_test_model",
91
+ base_model_name: "en-US_BroadbandModel"
92
+ ).result
93
+ customization_id = model["customization_id"]
94
+
95
+ output = @service.list_corpora(
96
+ customization_id: customization_id
97
+ ).result
98
+ refute_nil(output)
99
+
100
+ @service.delete_language_model(
101
+ customization_id: customization_id
102
+ )
103
+ end
104
+
105
+ def test_acoustic_model
106
+ list_models = @service.list_acoustic_models.result
107
+ refute_nil(list_models)
108
+
109
+ create_acoustic_model = @service.create_acoustic_model(
110
+ name: "integration_test_model_ruby",
111
+ base_model_name: "en-US_BroadbandModel"
112
+ ).result
113
+ refute_nil(create_acoustic_model)
114
+
115
+ get_acoustic_model = @service.get_acoustic_model(
116
+ customization_id: create_acoustic_model["customization_id"]
117
+ ).result
118
+ refute_nil(get_acoustic_model)
119
+
120
+ @service.reset_acoustic_model(
121
+ customization_id: get_acoustic_model["customization_id"]
122
+ )
123
+
124
+ @service.delete_acoustic_model(
125
+ customization_id: get_acoustic_model["customization_id"]
126
+ )
127
+ end
128
+
129
+ def test_recognize_websocket_as_chunks
130
+ audio_file = File.open(Dir.getwd + "/resources/speech.wav")
131
+ mycallback = MyRecognizeCallback.new
132
+ speech = @service.recognize_with_websocket(
133
+ chunk_data: true,
134
+ recognize_callback: mycallback,
135
+ interim_results: true,
136
+ timestamps: true,
137
+ max_alternatives: 2,
138
+ word_alternatives_threshold: 0.5,
139
+ model: "en-US_BroadbandModel"
140
+ )
141
+ Thread.new do
142
+ until audio_file.eof?
143
+ chunk = audio_file.read(1024)
144
+ speech.add_audio_chunk(chunk: chunk)
145
+ end
146
+ sleep(1)
147
+ speech.stop_audio # Tell the websocket object that no more audio will be added
148
+ end
149
+ thr = Thread.new { speech.start }
150
+ thr.join
151
+ end
152
+
153
+ def test_recognize_websocket
154
+ audio_file = File.open(Dir.getwd + "/resources/speech.wav")
155
+ mycallback = MyRecognizeCallback.new
156
+ speech = @service.recognize_with_websocket(
157
+ audio: audio_file,
158
+ recognize_callback: mycallback,
159
+ interim_results: true,
160
+ timestamps: true,
161
+ max_alternatives: 2,
162
+ word_alternatives_threshold: 0.5,
163
+ model: "en-US_BroadbandModel"
164
+ )
165
+ thr = Thread.new { speech.start }
166
+ thr.join
167
+ end
168
+
169
+ def test_inactivity_timeout_with_websocket
170
+ audio_file = File.open(Dir.getwd + "/resources/sound-with-pause.wav")
171
+ atomic_boolean = Concurrent::AtomicBoolean.new
172
+ mycallback = MyRecognizeCallback.new(atomic_boolean: atomic_boolean)
173
+ speech = @service.recognize_with_websocket(
174
+ audio: audio_file,
175
+ recognize_callback: mycallback,
176
+ interim_results: true,
177
+ inactivity_timeout: 3,
178
+ timestamps: true,
179
+ max_alternatives: 2,
180
+ word_alternatives_threshold: 0.5,
181
+ model: "en-US_BroadbandModel"
182
+ )
183
+ thr = Thread.new { speech.start }
184
+ thr.join
185
+ assert(atomic_boolean.true?)
186
+ end
187
+ end