Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/speech.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- encoding: binary -*-
require 'rubygems'
require 'curb'
require 'json'

Expand Down
9 changes: 6 additions & 3 deletions lib/speech/audio_to_text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions test/audio_splitter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions test/audio_to_text_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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?
Expand All @@ -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
Expand Down