Class: Google::Cloud::Speech::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/speech/job.rb

Overview

Job

A resource represents the long-running, asynchronous processing of a speech-recognition operation. The job can be refreshed to retrieve recognition results once the audio data has been processed.

See Project#recognize_job and Audio#recognize_job.

Examples:

require "google/cloud/speech"

speech = Google::Cloud::Speech.new

job = speech.recognize_job "path/to/audio.raw",
                           encoding: :raw, sample_rate: 16000

job.done? #=> false
job.reload! # API call
job.done? #=> true
results = job.results

See Also:

Instance Method Summary collapse

Instance Method Details

#done?boolean

Checks if the speech-recognition processing of the audio data is complete.

Examples:

require "google/cloud/speech"

speech = Google::Cloud::Speech.new

job = speech.recognize_job "path/to/audio.raw",
                           encoding: :raw, sample_rate: 16000

job.done? #=> false

Returns:

  • (boolean)

    true when complete, false otherwise.



109
110
111
# File 'lib/google/cloud/speech/job.rb', line 109

def done?
  @grpc.done
end

#reload!Object Also known as: refresh!

Reloads the job with current data from the long-running, asynchronous processing of a speech-recognition operation.

Examples:

require "google/cloud/speech"

speech = Google::Cloud::Speech.new

job = speech.recognize_job "path/to/audio.raw",
                           encoding: :raw, sample_rate: 16000

job.done? #=> false
job.reload! # API call
job.done? #=> true


129
130
131
132
# File 'lib/google/cloud/speech/job.rb', line 129

def reload!
  @grpc = @service.get_op @grpc.name
  self
end

#resultsArray<Result>

A speech recognition result corresponding to a portion of the audio.

Examples:

require "google/cloud/speech"

speech = Google::Cloud::Speech.new

job = speech.recognize_job "path/to/audio.raw",
                           encoding: :raw, sample_rate: 16000

job.done? #=> true
results = job.results

Returns:

  • (Array<Result>)

    The transcribed text of audio recognized. If the job is not done this will return nil.



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/google/cloud/speech/job.rb', line 81

def results
  return nil unless done?
  return nil unless @grpc.result == :response
  resp = V1beta1::AsyncRecognizeResponse.decode(@grpc.response.value)
  resp.results.map do |result_grpc|
    Result.from_grpc result_grpc
  end
  # TODO: Ensure we are raising the proper error
  # TODO: Ensure GRPC behavior here, is an error already raised?
  # raise @grpc.error
end

#wait_until_done!Object

Reloads the job until the operation is complete. The delay between reloads will incrementally increase.

Examples:

require "google/cloud/speech"

speech = Google::Cloud::Speech.new

job = speech.recognize_job "path/to/audio.raw",
                           encoding: :raw, sample_rate: 16000

job.done? #=> false
job.wait_until_done!
job.done? #=> true


151
152
153
154
155
156
157
158
159
# File 'lib/google/cloud/speech/job.rb', line 151

def wait_until_done!
  backoff = ->(retries) { sleep 2 * retries + 5 }
  retries = 0
  until done?
    backoff.call retries
    retries += 1
    reload!
  end
end