Module: SpeechToText::GoogleS2T

Includes:
Util
Defined in:
lib/speech_to_text/google.rb

Overview

rubocop:disable Style/Documentation

Class Method Summary collapse

Methods included from Util

captions_json, recording_json, seconds_to_timestamp, video_to_audio, write_to_webvtt

Class Method Details

.check_status(operation_name) ⇒ Object

rubocop:disable Metrics/MethodLength



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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) ⇒ Object

create an array with the start time, stop time and words rubocop:disable Metrics/MethodLength



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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) ⇒ Object



91
92
93
94
95
96
# 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) ⇒ Object



84
85
86
87
88
89
# 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) ⇒ Object

rubocop:enable Naming/AccessorMethodName uploads audio file to a google bucket



46
47
48
49
50
51
# 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) ⇒ Object

set environment for worker rubocop:disable Naming/AccessorMethodName



40
41
42
# File 'lib/speech_to_text/google.rb', line 40

def self.set_environment(auth_file)
  ENV['GOOGLE_APPLICATION_CREDENTIALS'] = auth_file
end