Class: Google::Cloud::Speech::Operation

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

Overview

Operation

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

See Project#process and Audio#process.

Examples:

require "google/cloud/speech"

speech = Google::Cloud::Speech.new

op = speech.process "path/to/audio.raw",
                    encoding: :linear16,
                    language: "en-US",
                    sample_rate: 16000

op.done? #=> false
op.reload! # API call
op.done? #=> true
results = op.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

op = speech.process "path/to/audio.raw",
                    encoding: :linear16,
                    language: "en-US",
                    sample_rate: 16000

op.done? #=> false

Returns:

  • (boolean)

    true when complete, false otherwise.



101
102
103
# File 'lib/google/cloud/speech/operation.rb', line 101

def done?
  @grpc.done?
end

#errorGoogle::Cloud::Error

The error information if the speech-recognition processing of the audio data has returned an error.

Examples:

require "google/cloud/speech"

speech = Google::Cloud::Speech.new

op = speech.process "path/to/audio.raw",
                    encoding: :linear16,
                    language: "en-US",
                    sample_rate: 16000

op.done? #=> true
op.error? #=> true
error = op.error

Returns:

  • (Google::Cloud::Error)

    The error.



176
177
178
179
# File 'lib/google/cloud/speech/operation.rb', line 176

def error
  return nil unless error?
  Google::Cloud::Error.from_error @grpc.error
end

#error?boolean

Checks if the speech-recognition processing of the audio data has returned an error.

Examples:

require "google/cloud/speech"

speech = Google::Cloud::Speech.new

op = speech.process "path/to/audio.raw",
                    encoding: :linear16,
                    language: "en-US",
                    sample_rate: 16000

op.done? #=> true
op.error? #=> true
error = op.error

Returns:

  • (boolean)

    true when errored, false otherwise.



201
202
203
# File 'lib/google/cloud/speech/operation.rb', line 201

def error?
  @grpc.error?
end

#idString

The unique identifier for the long running operation.

Examples:

require "google/cloud/speech"

speech = Google::Cloud::Speech.new

op = speech.process "path/to/audio.raw",
                    encoding: :linear16,
                    language: "en-US",
                    sample_rate: 16000

op.id #=> "1234567890"

Returns:

  • (String)

    The unique identifier for the long running operation.



79
80
81
# File 'lib/google/cloud/speech/operation.rb', line 79

def id
  @grpc.name
end

#reload!Object Also known as: refresh!

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

Examples:

require "google/cloud/speech"

speech = Google::Cloud::Speech.new

op = speech.process "path/to/audio.raw",
                    encoding: :linear16,
                    language: "en-US",
                    sample_rate: 16000

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


223
224
225
226
# File 'lib/google/cloud/speech/operation.rb', line 223

def reload!
  @grpc.reload!
  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

op = speech.process "path/to/audio.raw",
                    encoding: :linear16,
                    language: "en-US",
                    sample_rate: 16000

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

Returns:

  • (Array<Result>)

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



125
126
127
128
129
130
# File 'lib/google/cloud/speech/operation.rb', line 125

def results
  return nil unless results?
  @grpc.response.results.map do |result_grpc|
    Result.from_grpc result_grpc
  end
end

#results?boolean

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

Examples:

require "google/cloud/speech"

speech = Google::Cloud::Speech.new

op = speech.process "path/to/audio.raw",
                    encoding: :linear16,
                    language: "en-US",
                    sample_rate: 16000

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

Returns:

  • (boolean)

    true when complete, false otherwise.



152
153
154
# File 'lib/google/cloud/speech/operation.rb', line 152

def results?
  @grpc.response?
end

#wait_until_done!Object

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

Examples:

require "google/cloud/speech"

speech = Google::Cloud::Speech.new

op = speech.process "path/to/audio.raw",
                    encoding: :linear16,
                    language: "en-US",
                    sample_rate: 16000

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


247
248
249
# File 'lib/google/cloud/speech/operation.rb', line 247

def wait_until_done!
  @grpc.wait_until_done!
end