att_speech 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source "http://rubygems.org"
2
+ gem "faraday", ">= 0.8.1"
3
+ gem "celluloid", ">= 0.11.1"
4
+ gem "hashie", ">=1.2.0"
5
+ gem "activesupport"
6
+
7
+ group :development do
8
+ gem "rspec", ">= 2.8.0"
9
+ gem "yard", ">= 0.7"
10
+ gem "rdoc", ">= 3.12"
11
+ gem "bundler", ">= 1.0.0"
12
+ gem "jeweler", ">= 1.8.4"
13
+ gem "simplecov", ">= 0"
14
+ gem "fakeweb"
15
+ end
@@ -0,0 +1,8 @@
1
+ The MIT License (MIT)
2
+ Copyright (c) 2012 Jason Goecke
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5
+
6
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7
+
8
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,34 @@
1
+ # att_speech
2
+
3
+ A Ruby library for consuming the AT&T [Speech API](https://developer.att.com/developer/apiDetailPage.jsp?passedItemId=10700023) for speech to text. API details may be found [here](http://developer.att.com/developer/apiDetailPage.jsp?passedItemId=10900039).
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ gem install att_speech
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ruby
14
+ require 'att_speech'
15
+
16
+ api_key = 'foo'
17
+ secret_key = 'bar'
18
+
19
+ att_speech = ATTSpeech.new(api_key, secret_key)
20
+
21
+ # Block operation
22
+ p att_speech.speech_to_text('bostonSeltics.wav', type='audio/wav')
23
+ #<Hashie::Mash recognition=#<Hashie::Mash n_best=#<Hashie::Mash confidence=1 grade="accept" hypothesis="Boston celtics." language_id="en-us" result_text="Boston celtics." word_scores=[1, 1] words=["Boston", "celtics."]> response_id="452d848c6d1a4be3f2bc987e5201ae38">>
24
+
25
+ # Non-blocking operation with a future, if you have a longer file that requires more processing time
26
+ future = att_speech.future(:speech_to_text, 'bostinSeltics.wav', type='audio/wav')
27
+ p future.value
28
+ #<Hashie::Mash recognition=#<Hashie::Mash n_best=#<Hashie::Mash confidence=1 grade="accept" hypothesis="Boston celtics." language_id="en-us" result_text="Boston celtics." word_scores=[1, 1] words=["Boston", "celtics."]> response_id="452d848c6d1a4be3f2bc987e5201ae38">>
29
+ ```
30
+
31
+ ## Copyright
32
+
33
+ Copyright (c) 2012 Jason Goecke. See LICENSE.txt for further details.
34
+
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "att_speech"
18
+ gem.homepage = "http://github.com/jsgoecke/att_speech"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{TODO: one-line summary of your gem}
21
+ gem.description = %Q{TODO: longer description of your gem}
22
+ gem.email = "jason@goecke.net"
23
+ gem.authors = ["Jason Goecke"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'yard'
42
+ YARD::Rake::YardocTask.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,9 @@
1
+ %w{
2
+ json
3
+ faraday
4
+ hashie
5
+ celluloid
6
+ active_support/core_ext/string/inflections
7
+ att_speech/version
8
+ att_speech/att_speech
9
+ }.each { |lib| require lib }
@@ -0,0 +1,121 @@
1
+ class ATTSpeech
2
+ include Celluloid
3
+ Celluloid.logger = nil
4
+
5
+ attr_reader :api_key, :secret_key, :access_token, :refresh_token
6
+
7
+ ##
8
+ # Creates an ATTSpeech object
9
+ #
10
+ # @param [String] api_key
11
+ # @param [String] secret_key
12
+ # @param [String] base_url
13
+ #
14
+ # @return [Object] an instance of ATTSpeech
15
+ def initialize(api_key, secret_key, base_url='https://api.att.com')
16
+ @api_key = api_key
17
+ @secret_key = secret_key
18
+ @base_url = base_url
19
+ @grant_type = 'client_credentials'
20
+ @scope = 'SPEECH'
21
+ @access_token = ''
22
+ @refresh_token = ''
23
+
24
+ create_connection
25
+ get_tokens
26
+
27
+ self
28
+ end
29
+
30
+ ##
31
+ # Allows you to send a file and return the speech to text result
32
+ # @param [String] file_contents to be processed
33
+ # @param [String] type of file to be processed, may be audio/wav, application/octet-stream or audio/amr
34
+ # @param [String] speech_context to use to evaluate the audio Generic, UVerseEPG, BusinessSearch, Websearch, SMS, Voicemail, QuestionAndAnswer
35
+ #
36
+ # @return [Hash] the resulting response from the AT&T Speech API
37
+ def speech_to_text(file_contents, type='audio/wav', speech_context='Generic')
38
+ resource = "/rest/1/SpeechToText"
39
+
40
+ if type == "application/octet-stream"
41
+ type = "audio/amr"
42
+ end
43
+
44
+ begin
45
+ response = @connection.post resource, file_contents,
46
+ :Authorization => "Bearer #{@access_token}",
47
+ :Content_Transfer_Encoding => 'chunked',
48
+ :X_SpeechContext => speech_context,
49
+ :Content_Type => type,
50
+ :Accept => 'application/json'
51
+
52
+ process_response(response)
53
+ rescue => e
54
+ raise RuntimeError, e.to_s
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ ##
61
+ # Creates the Faraday connection object
62
+ def create_connection
63
+ @connection = Faraday.new(:url => @base_url) do |faraday|
64
+ faraday.headers['Accept'] = 'application/json'
65
+ faraday.adapter Faraday.default_adapter
66
+ end
67
+ end
68
+
69
+ ##
70
+ # Obtains the session tokens
71
+ def get_tokens
72
+ resource = "/oauth/access_token"
73
+
74
+ begin
75
+ response = @connection.post resource do |request|
76
+ request.params['client_id'] = @api_key
77
+ request.params['client_secret'] = @secret_key
78
+ request.params['grant_type'] = @grant_type
79
+ request.params['scope'] = @scope
80
+ end
81
+
82
+ result = process_response(response)
83
+
84
+ if result[:access_token].nil? || result[:refresh_token].nil?
85
+ raise RuntimeError, "Unable to complete oauth: #{response[:error]}"
86
+ else
87
+ @access_token = result[:access_token]
88
+ @refresh_token = result[:refresh_token]
89
+ end
90
+ rescue => e
91
+ raise RuntimeError, e.to_s
92
+ end
93
+ end
94
+
95
+ ##
96
+ # Process the JSON returned into a Hashie::Mash and making it more Ruby friendly
97
+ #
98
+ # @param [String] reponse json
99
+ #
100
+ # @return [Object] a Hashie::Mash object
101
+ def process_response(response)
102
+ Hashie::Mash.new(underscore_hash(JSON.parse(response.body)))
103
+ end
104
+
105
+ ##
106
+ # Decamelizes the keys in a hash to be more Ruby friendly
107
+ #
108
+ # @param [Hash] hash to be decamelized
109
+ #
110
+ # @return [Hash] the hash with the keys decamalized
111
+ def underscore_hash(hash)
112
+ hash.inject({}) do |underscored, (key, value)|
113
+ value = underscore_hash(value) if value.is_a?(Hash)
114
+ if value.is_a?(Array)
115
+ value = underscore_hash(value[0]) if value[0].is_a?(Hash)
116
+ end
117
+ underscored[key.underscore] = value
118
+ underscored
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,3 @@
1
+ class ATTSpeech
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,51 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "AttSpeech" do
4
+ FakeWeb.allow_net_connect = false
5
+ FakeWeb.register_uri(:post,
6
+ "https://api.att.com/oauth/access_token?client_id=1234&client_secret=abcd&grant_type=client_credentials&scope=SPEECH",
7
+ :status => ['200', 'OK'],
8
+ :body => '{"access_token":"5678","refresh_token":"wxyz"}')
9
+
10
+ FakeWeb.register_uri(:post,
11
+ "https://api.att.com/rest/1/SpeechToText",
12
+ :status => ['200', 'OK'],
13
+ :body => "{\"Recognition\":{\"ResponseId\":\"2b0bdcf4301f5c4aba57e2765b59bcbe\",\"NBest\":[{\"WordScores\":[1,1],\"Confidence\":1,\"Grade\":\"accept\",\"ResultText\":\"Boston celtics.\",\"Words\":[\"Boston\",\"celtics.\"],\"LanguageId\":\"en-us\",\"Hypothesis\":\"Boston celtics.\"}]}}")
14
+
15
+ let(:att_speech) { att_speech = ATTSpeech.new '1234', 'abcd' }
16
+
17
+ it "should create an ATTSpeech object" do
18
+ att_speech.class.should eql ATTSpeech
19
+ end
20
+
21
+ it "should set the access_token and refresh_token" do
22
+ att_speech.access_token.should eql '5678'
23
+ att_speech.refresh_token.should eql 'wxyz'
24
+ end
25
+
26
+ describe 'blocking call' do
27
+ it "should return a Hashie::Mash object when processing an audio file" do
28
+ result = att_speech.speech_to_text 'spec/spec_helper.rb'
29
+ result.instance_of?(Hashie::Mash).should eql true
30
+ end
31
+
32
+ it "should attempt to process an audio file" do
33
+ result = att_speech.speech_to_text 'spec/spec_helper.rb'
34
+ result[:recognition][:response_id].should eql '2b0bdcf4301f5c4aba57e2765b59bcbe'
35
+ result[:recognition][:n_best][:confidence].should eql 1
36
+ end
37
+ end
38
+
39
+ describe 'non-blocking call' do
40
+ it "should return a Celluloid::Future object when processing an audio file" do
41
+ future = att_speech.future(:speech_to_text, 'spec/spec_helper.rb')
42
+ future.instance_of?(Celluloid::Future).should eql true
43
+ end
44
+
45
+ it "should allow us to user a future to process an audio file" do
46
+ future = att_speech.future(:speech_to_text, 'spec/spec_helper.rb')
47
+ future.value[:recognition][:response_id].should eql '2b0bdcf4301f5c4aba57e2765b59bcbe'
48
+ future.value[:recognition][:n_best][:confidence].should eql 1
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'fakeweb'
5
+ require 'att_speech'
6
+
7
+ # Requires supporting files with custom matchers and macros, etc,
8
+ # in ./support/ and its subdirectories.
9
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
10
+
11
+ RSpec.configure do |config|
12
+
13
+ end
metadata ADDED
@@ -0,0 +1,237 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: att_speech
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jason Goecke
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.8.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.8.1
30
+ - !ruby/object:Gem::Dependency
31
+ name: celluloid
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 0.11.1
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.11.1
46
+ - !ruby/object:Gem::Dependency
47
+ name: hashie
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 1.2.0
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.2.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: activesupport
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: 2.8.0
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: 2.8.0
94
+ - !ruby/object:Gem::Dependency
95
+ name: yard
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0.7'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0.7'
110
+ - !ruby/object:Gem::Dependency
111
+ name: rdoc
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '3.12'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '3.12'
126
+ - !ruby/object:Gem::Dependency
127
+ name: bundler
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: 1.0.0
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: 1.0.0
142
+ - !ruby/object:Gem::Dependency
143
+ name: jeweler
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: 1.8.4
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: 1.8.4
158
+ - !ruby/object:Gem::Dependency
159
+ name: simplecov
160
+ requirement: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - ! '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ type: :development
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - ! '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ - !ruby/object:Gem::Dependency
175
+ name: fakeweb
176
+ requirement: !ruby/object:Gem::Requirement
177
+ none: false
178
+ requirements:
179
+ - - ! '>='
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ type: :development
183
+ prerelease: false
184
+ version_requirements: !ruby/object:Gem::Requirement
185
+ none: false
186
+ requirements:
187
+ - - ! '>='
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ description: A Ruby library for consuming the AT&T Speech API for speech to text.
191
+ email: jason@goecke.net
192
+ executables: []
193
+ extensions: []
194
+ extra_rdoc_files:
195
+ - LICENSE.txt
196
+ - README.md
197
+ files:
198
+ - .document
199
+ - .rspec
200
+ - Gemfile
201
+ - LICENSE.txt
202
+ - Rakefile
203
+ - VERSION
204
+ - lib/att_speech.rb
205
+ - lib/att_speech/version.rb
206
+ - lib/att_speech/att_speech.rb
207
+ - spec/att_speech_spec.rb
208
+ - spec/spec_helper.rb
209
+ - README.md
210
+ homepage: http://github.com/jsgoecke/att_speech
211
+ licenses:
212
+ - MIT
213
+ post_install_message:
214
+ rdoc_options: []
215
+ require_paths:
216
+ - lib
217
+ required_ruby_version: !ruby/object:Gem::Requirement
218
+ none: false
219
+ requirements:
220
+ - - ! '>='
221
+ - !ruby/object:Gem::Version
222
+ version: '0'
223
+ required_rubygems_version: !ruby/object:Gem::Requirement
224
+ none: false
225
+ requirements:
226
+ - - ! '>='
227
+ - !ruby/object:Gem::Version
228
+ version: '0'
229
+ requirements: []
230
+ rubyforge_project:
231
+ rubygems_version: 1.8.24
232
+ signing_key:
233
+ specification_version: 3
234
+ summary: A Ruby library for consuming the AT&T Speech API https://developer.att.com/developer/apiDetailPage.jsp?passedItemId=10700023
235
+ for speech to text.
236
+ test_files: []
237
+ has_rdoc: