att_speech 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +11 -10
- data/VERSION +1 -1
- data/lib/att_speech/att_speech.rb +42 -10
- data/lib/att_speech/version.rb +1 -1
- data/spec/att_speech_spec.rb +66 -7
- metadata +1 -1
data/README.md
CHANGED
@@ -15,26 +15,27 @@ gem install att_speech
|
|
15
15
|
```ruby
|
16
16
|
require 'att_speech'
|
17
17
|
|
18
|
-
api_key
|
19
|
-
secret_key
|
18
|
+
att_speech = ATTSpeech.new({ :api_key => ENV['ATT_SPEECH_KEY'],
|
19
|
+
:secret_key => ENV['ATT_SPEECH_SECRET'] })
|
20
20
|
|
21
|
-
|
21
|
+
# Read the audio file contents
|
22
|
+
file_contents = File.read(File.expand_path(File.dirname(File.dirname(__FILE__))) + "/bostonSeltics.wav")
|
22
23
|
|
23
24
|
# Blocking operation
|
24
|
-
p att_speech.speech_to_text(
|
25
|
-
#<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">>
|
25
|
+
p att_speech.speech_to_text(file_contents, type='audio/wav')
|
26
26
|
|
27
27
|
# Non-blocking operation with a future, if you have a longer file that requires more processing time
|
28
|
-
|
28
|
+
sleep 2
|
29
|
+
future = att_speech.future(:speech_to_text, file_contents, type='audio/wav')
|
29
30
|
p future.value
|
30
|
-
#<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">>
|
31
31
|
|
32
32
|
# Non-blocking operation that will call a block when the transcrption is returned
|
33
33
|
# Note: Remember, this is a concurrent operation so don't pass self and avoid mutable objects in the block
|
34
34
|
# from the calling context, better to have discreet actions contained in the block, such as inserting in a
|
35
|
-
# datastore
|
36
|
-
|
37
|
-
|
35
|
+
# datastore
|
36
|
+
sleep 2
|
37
|
+
att_speech.speech_to_text!(file_contents) { |transcription| p transcription }
|
38
|
+
sleep 5
|
38
39
|
```
|
39
40
|
|
40
41
|
## Copyright
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.3
|
@@ -2,21 +2,41 @@ class ATTSpeech
|
|
2
2
|
include Celluloid
|
3
3
|
Celluloid.logger = nil
|
4
4
|
|
5
|
-
attr_reader :api_key, :secret_key, :access_token, :refresh_token
|
5
|
+
attr_reader :api_key, :secret_key, :access_token, :refresh_token, :base_url, :ssl_verify
|
6
6
|
|
7
7
|
##
|
8
8
|
# Creates an ATTSpeech object
|
9
9
|
#
|
10
|
-
# @
|
11
|
-
#
|
12
|
-
#
|
10
|
+
# @overload initialize(args)
|
11
|
+
# @param [Hash] args the options to intantiate with
|
12
|
+
# @option args [String] :api_key the AT&T Speech API Key
|
13
|
+
# @option args [String] :secret_key the AT&T Speech API Secret Key
|
14
|
+
# @option args [String] :base_url the url for the AT&T Speech API, default is 'https://api.att.com'
|
15
|
+
# @option args [Boolean] :ssl_verify determines if the peer Cert is verified for SSL, default is true
|
16
|
+
# @overload initialize(api_key, secret_key, base_url='https://api.att.com')
|
17
|
+
# @param [String] api_key the AT&T Speech API Key
|
18
|
+
# @param [String] secret_key the AT&T Speech API Secret Key
|
19
|
+
# @param [String] base_url, the url for the AT&T Speech API, default is 'https://api.att.com'
|
20
|
+
# @param [Boolean] ssl_verify determines if the peer Cert is verified for SSL, default is true
|
13
21
|
#
|
14
22
|
# @return [Object] an instance of ATTSpeech
|
15
|
-
def initialize(
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
23
|
+
def initialize(*args)
|
24
|
+
raise ArgumentError, "Requres at least the api_key and secret_key when instatiating" if args.size == 0
|
25
|
+
|
26
|
+
base_url = 'https://api.att.com'
|
27
|
+
|
28
|
+
if args.size == 1 && args[0].instance_of?(Hash)
|
29
|
+
@api_key = args[0][:api_key]
|
30
|
+
@secret_key = args[0][:secret_key]
|
31
|
+
@base_url = args[0][:base_url] || base_url
|
32
|
+
set_ssl_verify args[0][:ssl_verify]
|
33
|
+
else
|
34
|
+
@api_key = args[0]
|
35
|
+
@secret_key = args[1]
|
36
|
+
@base_url = args[2] || base_url
|
37
|
+
set_ssl_verify args[3]
|
38
|
+
end
|
39
|
+
|
20
40
|
@grant_type = 'client_credentials'
|
21
41
|
@scope = 'SPEECH'
|
22
42
|
@access_token = ''
|
@@ -64,7 +84,7 @@ class ATTSpeech
|
|
64
84
|
##
|
65
85
|
# Creates the Faraday connection object
|
66
86
|
def create_connection
|
67
|
-
@connection = Faraday.new(:url => @base_url) do |faraday|
|
87
|
+
@connection = Faraday.new(:url => @base_url, :ssl => { :verify => @ssl_verify }) do |faraday|
|
68
88
|
faraday.headers['Accept'] = 'application/json'
|
69
89
|
faraday.adapter Faraday.default_adapter
|
70
90
|
end
|
@@ -106,6 +126,18 @@ class ATTSpeech
|
|
106
126
|
Hashie::Mash.new(underscore_hash(JSON.parse(response.body)))
|
107
127
|
end
|
108
128
|
|
129
|
+
##
|
130
|
+
# Sets the ssl_verify option
|
131
|
+
#
|
132
|
+
# @param [Boolean] ssl_verify the variable to set
|
133
|
+
def set_ssl_verify(ssl_verify)
|
134
|
+
if ssl_verify == false
|
135
|
+
@ssl_verify = false
|
136
|
+
else
|
137
|
+
@ssl_verify = true
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
109
141
|
##
|
110
142
|
# Decamelizes the keys in a hash to be more Ruby friendly
|
111
143
|
#
|
data/lib/att_speech/version.rb
CHANGED
data/spec/att_speech_spec.rb
CHANGED
@@ -2,37 +2,83 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
|
3
3
|
describe "AttSpeech" do
|
4
4
|
FakeWeb.allow_net_connect = false
|
5
|
+
|
5
6
|
FakeWeb.register_uri(:post,
|
6
7
|
"https://api.att.com/oauth/access_token?client_id=1234&client_secret=abcd&grant_type=client_credentials&scope=SPEECH",
|
7
8
|
:status => ['200', 'OK'],
|
8
9
|
:body => '{"access_token":"5678","refresh_token":"wxyz"}')
|
9
10
|
|
11
|
+
FakeWeb.register_uri(:post,
|
12
|
+
"http://foobar.com/oauth/access_token?client_id=1234&client_secret=abcd&grant_type=client_credentials&scope=SPEECH",
|
13
|
+
:status => ['200', 'OK'],
|
14
|
+
:body => '{"access_token":"5678","refresh_token":"wxyz"}')
|
15
|
+
|
10
16
|
FakeWeb.register_uri(:post,
|
11
17
|
"https://api.att.com/rest/1/SpeechToText",
|
12
18
|
:status => ['200', 'OK'],
|
13
19
|
: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
20
|
|
15
|
-
let(:att_speech)
|
21
|
+
let(:att_speech) { att_speech = ATTSpeech.new '1234', 'abcd' }
|
22
|
+
let(:att_speech_hash) { att_speech = ATTSpeech.new({ :api_key => '1234',
|
23
|
+
:secret_key => 'abcd' })}
|
16
24
|
|
17
|
-
|
18
|
-
|
19
|
-
|
25
|
+
describe 'initializing' do
|
26
|
+
it "should raise an error of no parameters passed when creating object" do
|
27
|
+
begin
|
28
|
+
ATTSpeech.new
|
29
|
+
rescue => e
|
30
|
+
e.to_s.should eql "Requres at least the api_key and secret_key when instatiating"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should create an ATTSpeech object" do
|
35
|
+
att_speech.class.should eql ATTSpeech
|
36
|
+
att_speech_hash.class.should eql ATTSpeech
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should set the url to something different' do
|
40
|
+
as = ATTSpeech.new('1234', 'abcd', 'http://foobar.com', false)
|
41
|
+
as.base_url.should == 'http://foobar.com'
|
42
|
+
as.ssl_verify.should == false
|
43
|
+
|
44
|
+
as = ATTSpeech.new({ :api_key => '1234',
|
45
|
+
:secret_key => 'abcd',
|
46
|
+
:base_url => 'http://foobar.com',
|
47
|
+
:ssl_verify => false })
|
48
|
+
as.base_url.should == 'http://foobar.com'
|
49
|
+
as.ssl_verify.should == false
|
50
|
+
end
|
20
51
|
|
21
|
-
|
22
|
-
|
23
|
-
|
52
|
+
it "should set the access_token and refresh_token" do
|
53
|
+
att_speech.access_token.should eql '5678'
|
54
|
+
att_speech.refresh_token.should eql 'wxyz'
|
55
|
+
att_speech.base_url.should == 'https://api.att.com'
|
56
|
+
att_speech.ssl_verify.should == true
|
57
|
+
|
58
|
+
att_speech_hash.access_token.should eql '5678'
|
59
|
+
att_speech_hash.refresh_token.should eql 'wxyz'
|
60
|
+
att_speech_hash.base_url.should == 'https://api.att.com'
|
61
|
+
att_speech_hash.ssl_verify.should == true
|
62
|
+
end
|
24
63
|
end
|
25
64
|
|
26
65
|
describe 'blocking call' do
|
27
66
|
it "should return a Hashie::Mash object when processing an audio file" do
|
28
67
|
result = att_speech.speech_to_text 'spec/spec_helper.rb'
|
29
68
|
result.instance_of?(Hashie::Mash).should eql true
|
69
|
+
|
70
|
+
result = att_speech_hash.speech_to_text 'spec/spec_helper.rb'
|
71
|
+
result.instance_of?(Hashie::Mash).should eql true
|
30
72
|
end
|
31
73
|
|
32
74
|
it "should attempt to process an audio file" do
|
33
75
|
result = att_speech.speech_to_text 'spec/spec_helper.rb'
|
34
76
|
result[:recognition][:response_id].should eql '2b0bdcf4301f5c4aba57e2765b59bcbe'
|
35
77
|
result[:recognition][:n_best][:confidence].should eql 1
|
78
|
+
|
79
|
+
result = att_speech_hash.speech_to_text 'spec/spec_helper.rb'
|
80
|
+
result[:recognition][:response_id].should eql '2b0bdcf4301f5c4aba57e2765b59bcbe'
|
81
|
+
result[:recognition][:n_best][:confidence].should eql 1
|
36
82
|
end
|
37
83
|
end
|
38
84
|
|
@@ -40,12 +86,19 @@ describe "AttSpeech" do
|
|
40
86
|
it "should return a Celluloid::Future object when processing an audio file" do
|
41
87
|
future = att_speech.future(:speech_to_text, 'spec/spec_helper.rb')
|
42
88
|
future.instance_of?(Celluloid::Future).should eql true
|
89
|
+
|
90
|
+
future = att_speech_hash.future(:speech_to_text, 'spec/spec_helper.rb')
|
91
|
+
future.instance_of?(Celluloid::Future).should eql true
|
43
92
|
end
|
44
93
|
|
45
94
|
it "should allow us to use a future to process an audio file" do
|
46
95
|
future = att_speech.future(:speech_to_text, 'spec/spec_helper.rb')
|
47
96
|
future.value[:recognition][:response_id].should eql '2b0bdcf4301f5c4aba57e2765b59bcbe'
|
48
97
|
future.value[:recognition][:n_best][:confidence].should eql 1
|
98
|
+
|
99
|
+
future = att_speech_hash.future(:speech_to_text, 'spec/spec_helper.rb')
|
100
|
+
future.value[:recognition][:response_id].should eql '2b0bdcf4301f5c4aba57e2765b59bcbe'
|
101
|
+
future.value[:recognition][:n_best][:confidence].should eql 1
|
49
102
|
end
|
50
103
|
end
|
51
104
|
|
@@ -56,6 +109,12 @@ describe "AttSpeech" do
|
|
56
109
|
sleep 0.5
|
57
110
|
result[:recognition][:response_id].should eql '2b0bdcf4301f5c4aba57e2765b59bcbe'
|
58
111
|
result[:recognition][:n_best][:confidence].should eql 1
|
112
|
+
|
113
|
+
result = nil
|
114
|
+
att_speech_hash.speech_to_text!('spec/spec_helper.rb') { |transcription| result = transcription }
|
115
|
+
sleep 0.5
|
116
|
+
result[:recognition][:response_id].should eql '2b0bdcf4301f5c4aba57e2765b59bcbe'
|
117
|
+
result[:recognition][:n_best][:confidence].should eql 1
|
59
118
|
end
|
60
119
|
end
|
61
120
|
end
|