Class: GoogleSpeech::Transcriber

Inherits:
Object
  • Object
show all
Defined in:
lib/google_speech/transcriber.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :language         => 'en-US',
  :chunk_duration   => 5,
  :overlap          => 1,
  :max_results      => 2,
  :request_pause    => 1,
  :profanity_filter => false
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original_file, options = nil) ⇒ Transcriber

Returns a new instance of Transcriber.



20
21
22
23
24
# File 'lib/google_speech/transcriber.rb', line 20

def initialize(original_file, options=nil)
  @original_file = original_file
  @options = DEFAULT_OPTIONS.merge(options || {})
  @results = []
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/google_speech/transcriber.rb', line 9

def options
  @options
end

#original_fileObject

Returns the value of attribute original_file.



9
10
11
# File 'lib/google_speech/transcriber.rb', line 9

def original_file
  @original_file
end

#resultsObject

Returns the value of attribute results.



9
10
11
# File 'lib/google_speech/transcriber.rb', line 9

def results
  @results
end

Instance Method Details

#loggerObject



83
84
85
# File 'lib/google_speech/transcriber.rb', line 83

def logger
  GoogleSpeech::Utility.logger
end

#pfilterObject



44
45
46
# File 'lib/google_speech/transcriber.rb', line 44

def pfilter
  options[:profanity_filter] ? '1' : '0'
end

#transcribeObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/google_speech/transcriber.rb', line 26

def transcribe
  chunk_factory = ChunkFactory.new(@original_file, options[:chunk_duration], options[:overlap])
  chunk_factory.each{ |chunk|
    result = chunk.to_hash
    transcript = transcribe_data(chunk.data)
    next unless transcript
    hypothesis = transcript['hypotheses'].first || Hash.new("")
    result[:text]       = hypothesis['utterance']
    result[:confidence] = hypothesis['confidence']
    @results << result

    logger.debug "#{result[:start_time]}: #{(result[:confidence].to_f * 100).to_i}%: #{result[:text]}"

    sleep(options[:request_pause].to_i)
  }
  @results
end

#transcribe_data(data) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/google_speech/transcriber.rb', line 48

def transcribe_data(data)
  params = {
    :scheme   => 'https',
    :host     => 'www.google.com',
    :port     => 443,
    :path     => "/speech-api/v1/recognize",
    :query    => "xjerr=1&client=google_speech&lang=#{options[:language]}&maxresults=#{options[:max_results].to_i}&pfilter=#{pfilter}",
    :body     => data,
    :method   => 'POST',
    :headers  => {
      'Content-Type'   => 'audio/x-flac; rate=16000',
      'Content-Length' => data.bytesize,
      'User-Agent'     => "google_speech"
    }
  }
  retry_max = options[:retry_max] ? [options[:retry_max].to_i, 1].max : 3
  retry_count = 0
  result = nil
  url = "#{params[:scheme]}://#{params[:host]}:#{params[:port]}#{params[:path]}"
  while(!result && retry_count < retry_max)
    connection = Excon.new(url)
    response = connection.request(params)
    # puts "response: #{response.inspect}\n\n"
    if response.status.to_s.start_with?('2')
      result = JSON.parse(response.body)
      # puts "results: #{result.inspect}\n\n"
    else
      sleep(1)
      retry_count += 1
    end
  end

  result
end