diff --git a/lib/speech.rb b/lib/speech.rb index ad4655f..f96e554 100644 --- a/lib/speech.rb +++ b/lib/speech.rb @@ -1,4 +1,5 @@ # -*- encoding: binary -*- +require 'rubygems' require 'curb' require 'json' diff --git a/lib/speech/audio_to_text.rb b/lib/speech/audio_to_text.rb index 4f8a344..98ba949 100644 --- a/lib/speech/audio_to_text.rb +++ b/lib/speech/audio_to_text.rb @@ -12,13 +12,16 @@ def initialize(file) def to_text url = "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=speech2text&lang=en-US&maxresults=10" - splitter = Speech::AudioSplitter.new(file) # based off the wave file because flac doesn't tell us the duration + splitter = Speech::AudioSplitter.new(file, 3) # based off the wave file because flac doesn't tell us the duration easy = Curl::Easy.new(url) + result = [] splitter.split.each do|chunk| chunk.build.to_flac convert_chunk(easy, chunk) + json = JSON.parse(File.read(self.captured_file)) + result << json end - JSON.parse(File.read(self.captured_file)) + result end def clean @@ -53,7 +56,7 @@ def convert_chunk(easy, chunk, options={}) self.captured_json['status'] = data['status'] self.captured_json['id'] = data['id'] self.captured_json['hypotheses'] = data['hypotheses'].map {|ut| [ut['utterance'], ut['confidence']] } - puts self.captured_json.inspect + puts "inspect: #{self.captured_json.inspect}" File.open("#{self.captured_file}", "wb") {|f| f << captured_json.to_json } retrying = false end diff --git a/test/audio_splitter_test.rb b/test/audio_splitter_test.rb index 5397dc0..bc4f1e0 100644 --- a/test/audio_splitter_test.rb +++ b/test/audio_splitter_test.rb @@ -6,10 +6,10 @@ class SpeechAudioSplitterTest < Test::Unit::TestCase def test_audio_splitter - splitter = Speech::AudioSplitter.new("samples/i-like-pickles.wav", 1) + splitter = Speech::AudioSplitter.new("test/samples/i-like-pickles.wav", 1) - assert_equal '00:00:03:52', splitter.duration.to_s - assert_equal 3.52, splitter.duration.to_f + assert_equal '00:00:03:51', splitter.duration.to_s + assert_equal 3.51, splitter.duration.to_f chunks = splitter.split assert_equal 3, chunks.size diff --git a/test/audio_to_text_test.rb b/test/audio_to_text_test.rb index bc0546b..9732821 100644 --- a/test/audio_to_text_test.rb +++ b/test/audio_to_text_test.rb @@ -5,8 +5,8 @@ class SpeechAudioToTextTest < Test::Unit::TestCase def test_audio_to_text - audio = Speech::AudioToText.new("samples/i-like-pickles.wav") - captured_json = audio.to_text + audio = Speech::AudioToText.new("test/samples/i-like-pickles.wav") + captured_json = audio.to_text.first assert captured_json assert captured_json.key?("hypotheses") assert !captured_json['hypotheses'].empty? @@ -24,7 +24,7 @@ def test_audio_to_text def test_short_audio_clip audio = Speech::AudioToText.new("samples/i-like-pickles.chunk5.wav") - captured_json = audio.to_text + captured_json = audio.to_text.first assert captured_json assert captured_json.key?("hypotheses") assert !captured_json['hypotheses'].empty? @@ -33,8 +33,8 @@ def test_short_audio_clip assert captured_json.keys.include?('id') assert captured_json.keys.include?('hypotheses') puts captured_json.inspect - assert_equal "eagles", captured_json['hypotheses'][0].first - assert_equal "pickles", captured_json['hypotheses'][1].first + assert_equal "eagles eagles eagles", captured_json['hypotheses'][0].first + assert_equal "pickles pickles pickles", captured_json['hypotheses'][1].first #assert captured_json['confidence'] > 0.9 ensure audio.clean