Skip to content

Commit 1cf3e72

Browse files
maxnussbaumGERMAN ATTANASIO RUIZ
authored andcommitted
test(speech to text): Add more tests for speech to text & websockets
1 parent 58435e8 commit 1cf3e72

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed

lib/watson_apis/websocket/speech_to_text_websocket_listener.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ def initialize(audio: nil, chunk_data:, options:, recognize_callback:, url:, hea
2727
end
2828

2929
def start
30-
on_open = lambda do |_event|
30+
on_open = lambda do |event|
31+
on_connect(event)
3132
@client.send(build_start_message(options: @options))
3233
@mic_running = true if @chunk_data
3334
send_audio(data: @audio)
@@ -84,6 +85,7 @@ def start
8485
@client.add_listener(Faye::WebSocket::API::Event.create("open"))
8586
@client.add_listener(Faye::WebSocket::API::Event.create("message"))
8687
@client.add_listener(Faye::WebSocket::API::Event.create("close"))
88+
@client.add_listener(Faye::WebSocket::API::Event.create("error"))
8789
end
8890
end
8991

test/integration/test_speech_to_text_v1.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,30 @@ def test_acoustic_model
126126
)
127127
end
128128

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+
129153
def test_recognize_websocket
130154
audio_file = File.open(Dir.getwd + "/resources/speech.wav")
131155
mycallback = MyRecognizeCallback.new

test/unit/test_speech_to_text_v1.rb

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,15 @@ def test_custom_corpora
360360
service_response = service.add_corpus(
361361
customization_id: "customid",
362362
corpus_name: "corpus",
363-
corpus_file: corpus_file
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"
364372
)
365373
assert_nil(service_response)
366374

@@ -693,4 +701,55 @@ def test_recognize_async
693701
output = future.value.body
694702
assert_equal(recognize_response["results"][0]["alternatives"][0]["transcript"], output["results"][0]["alternatives"][0]["transcript"])
695703
end
704+
705+
def test_reset_language_model
706+
service = WatsonAPIs::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 = WatsonAPIs::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 = WatsonAPIs::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
696755
end

0 commit comments

Comments
 (0)