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"

gcloud = Google::Cloud.new
speech = gcloud.speech

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"

gcloud = Google::Cloud.new
speech = gcloud.speech

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

job.done? #=> false

Returns:

  • (boolean)

    true when complete, false otherwise.



112
113
114
# File 'lib/google/cloud/speech/job.rb', line 112

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"

gcloud = Google::Cloud.new
speech = gcloud.speech

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

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


133
134
135
136
# File 'lib/google/cloud/speech/job.rb', line 133

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"

gcloud = Google::Cloud.new
speech = gcloud.speech

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.



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

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"

gcloud = Google::Cloud.new
speech = gcloud.speech

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

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


156
157
158
159
160
161
162
163
164
# File 'lib/google/cloud/speech/job.rb', line 156

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