google_speech 0.3.3 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ec2a81e64decb108511c6e41e180634d3c5e2fb0
4
- data.tar.gz: 547610e9ebb2c7c8f881a952eb089ec4f2ccd0fd
3
+ metadata.gz: 6813210970cbd391fc353833b73cf483fecb35be
4
+ data.tar.gz: 9800a202fb0bb0a66cf6e74f921021cb1f89f4f8
5
5
  SHA512:
6
- metadata.gz: 33efc21a61cf1bce77e10ca55c5df5c545da5638e56e12cb1598388360e3d539c9f75e720ad4349bf8d0323fe410d2019042b25081715e54fe242a9044e8567f
7
- data.tar.gz: 7658ba2000a389388bcc58d35ad120a375f6365634f0b9ffe16e5b399cb5cd94af83250091a40fd5ce3d588b364fc14e223ef3aae81bc86946002d532a371178
6
+ metadata.gz: f77c8400be4f09302c472fa3da92679ff557e8f1a44b1bf12830c76d233130e500bba99583e62d2921a7d8d6a5850fe717b716f923cd754f9b9a8e87ba52156b
7
+ data.tar.gz: c998ca0fee9037f8691ae0c68dd994b555a3c13073afde8f93d58e5e47f179555df5beb4f669356ef8bcff5297c5c7d1db379e98b016a3fc4dd6aef3457fca12
@@ -21,6 +21,7 @@ Gem::Specification.new do |gem|
21
21
  gem.require_paths = ["lib"]
22
22
 
23
23
  gem.add_dependency "excon"
24
+ gem.add_dependency "uuid"
24
25
 
25
26
  gem.add_development_dependency "rake"
26
27
  gem.add_development_dependency "minitest"
@@ -5,6 +5,8 @@ require 'logger'
5
5
 
6
6
  module GoogleSpeech
7
7
 
8
+ TMP_FILE_DIR = ENV['GOOGLE_SPEECH_TMP_DIR'] || '/tmp/'
9
+
8
10
  def self.logger
9
11
  @_logger ||= Logger.new(STDOUT)
10
12
  end
@@ -33,5 +33,11 @@ module GoogleSpeech
33
33
  GoogleSpeech.logger
34
34
  end
35
35
 
36
+ def close_file
37
+ return unless @chunk_file
38
+ @chunk_file.close rescue nil
39
+ @chunk_file.unlink rescue nil
40
+ end
41
+
36
42
  end
37
43
  end
@@ -18,9 +18,14 @@ module GoogleSpeech
18
18
  def each
19
19
  pos = 0
20
20
  while(pos < @original_duration) do
21
- chunk = Chunk.new(@original_file, @original_duration, pos, (@chunk_duration + @overlap), @rate)
22
- yield chunk
23
- pos = pos + [chunk.duration, @chunk_duration].min
21
+ chunk = nil
22
+ begin
23
+ chunk = Chunk.new(@original_file, @original_duration, pos, (@chunk_duration + @overlap), @rate)
24
+ yield chunk
25
+ pos = pos + [chunk.duration, @chunk_duration].min
26
+ ensure
27
+ chunk.close_file if chunk
28
+ end
24
29
  end
25
30
  end
26
31
 
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'excon'
4
4
  require 'json'
5
+ require 'uuid'
5
6
 
6
7
  module GoogleSpeech
7
8
 
@@ -31,21 +32,38 @@ module GoogleSpeech
31
32
  @last_ua = 0
32
33
  end
33
34
 
35
+ def open_working_file
36
+ Utility.check_local_file(@original_file.path)
37
+ wf_path = random_file_name(@original_file.path)
38
+ FileUtils.ln(@original_file.path, wf_path)
39
+ File.open(wf_path, 'r') {|f|
40
+ yield f
41
+ }
42
+ FileUtils.rm(wf_path, :force=>true)
43
+ end
44
+
45
+ def random_file_name(path)
46
+ File.join(GoogleSpeech::TMP_FILE_DIR, File.basename(path) + '_' + UUID.generate + '.wav')
47
+ end
48
+
34
49
  def transcribe
35
- chunk_factory = ChunkFactory.new(@original_file, options[:chunk_duration], options[:overlap], options[:rate])
36
- chunk_factory.each{ |chunk|
37
- result = chunk.to_hash
38
- transcript = transcribe_data(chunk.data)
39
- next unless transcript
50
+ open_working_file do |working_file|
51
+ chunk_factory = ChunkFactory.new(working_file, options[:chunk_duration], options[:overlap], options[:rate])
52
+ chunk_factory.each{ |chunk|
53
+ result = chunk.to_hash
54
+ transcript = transcribe_data(chunk.data)
55
+ next unless transcript
40
56
 
41
- result = result.merge(extract_result(transcript))
57
+ result = result.merge(extract_result(transcript))
42
58
 
43
- logger.debug "#{result[:start_time]}: #{(result[:confidence].to_f * 100).to_i}%: #{result[:text]}"
59
+ logger.debug "#{result[:start_time]}: #{(result[:confidence].to_f * 100).to_i}%: #{result[:text]}"
44
60
 
45
- @results << result
61
+ @results << result
62
+
63
+ sleep(options[:request_pause].to_i)
64
+ }
65
+ end
46
66
 
47
- sleep(options[:request_pause].to_i)
48
- }
49
67
  @results
50
68
  end
51
69
 
@@ -1,5 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
 
3
3
  module GoogleSpeech
4
- VERSION = "0.3.3"
4
+ VERSION = "0.4.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google_speech
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kuklewicz
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: uuid
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement