module SpeechToText::GoogleS2T
Public Class Methods
check_status(operation_name)
click to toggle source
# File lib/speech_to_text/google.rb, line 69 def self.check_status(operation_name) # rubocop:disable Metrics/MethodLength # construct a new operation object from the id speech = Google::Cloud::Speech.new(version: :v1p1beta1) operation2 = speech.get_operation operation_name status = 'not found' if operation2.error? status = 'failed' elsif operation2.done? status = 'completed' else status = 'inProgress' end return status end
create_array_google(results)
click to toggle source
create an array with the start time, stop time and words rubocop:disable Metrics/MethodLength
# File lib/speech_to_text/google.rb, line 20 def self.create_array_google(results) # rubocop:disable Metrics/AbcSize data_array = [] results.each do |result| result.alternatives.each do |alternative| alternative.words.each_with_index do |word, _i| start_time = word.start_time.seconds + word.start_time.nanos / 1_000_000_000.0 end_time = word.end_time.seconds + word.end_time.nanos / 1_000_000_000.0 data_array.push(start_time) data_array.push(end_time) data_array.push(word.word) end end end data_array end
create_job(audio_name, audio_content_type, bucket_name, language_code)
click to toggle source
# File lib/speech_to_text/google.rb, line 53 def self.create_job(audio_name, audio_content_type, bucket_name, language_code) speech = Google::Cloud::Speech.new(version: :v1p1beta1) # The audio file's encoding and sample rate config = { language_code: language_code, enable_word_time_offsets: true } audio = { # content: audio_file #using local audio file uri: "gs://#{bucket_name}/#{audio_name}.#{audio_content_type}" # using the now uploaded audio file from the bucket } operation = speech.long_running_recognize config, audio operation.name end
delete_google_storage(bucket_name, audio_name, audio_content_type)
click to toggle source
# File lib/speech_to_text/google.rb, line 91 def self.delete_google_storage(bucket_name, audio_name, audio_content_type) storage = Google::Cloud::Storage.new project_id: bucket_name bucket = storage.bucket bucket_name file = bucket.file "#{audio_name}.#{audio_content_type}" file.delete end
get_words(operation_name)
click to toggle source
# File lib/speech_to_text/google.rb, line 84 def self.get_words(operation_name) # construct a new operation object from the id speech = Google::Cloud::Speech.new(version: :v1p1beta1) operation2 = speech.get_operation operation_name operation2.results end
google_storage(audio_file_path, audio_name, audio_content_type, bucket_name)
click to toggle source
rubocop:enable Naming/AccessorMethodName uploads audio file to a google bucket
# File lib/speech_to_text/google.rb, line 46 def self.google_storage(audio_file_path, audio_name, audio_content_type, bucket_name) audio_file = "#{audio_file_path}/#{audio_name}.#{audio_content_type}" storage = Google::Cloud::Storage.new project_id: bucket_name bucket = storage.bucket bucket_name file = bucket.create_file audio_file, "#{audio_name}.#{audio_content_type}" end
set_environment(auth_file)
click to toggle source
set environment for worker rubocop:disable Naming/AccessorMethodName
# File lib/speech_to_text/google.rb, line 40 def self.set_environment(auth_file) ENV['GOOGLE_APPLICATION_CREDENTIALS'] = auth_file end