Class: Nuance::Transcription
- Inherits:
-
Object
- Object
- Nuance::Transcription
- Includes:
- Celluloid
- Defined in:
- lib/nuance/transcription.rb
Instance Attribute Summary collapse
-
#base_uri ⇒ Object
readonly
Returns the value of attribute base_uri.
-
#ssl_verify ⇒ Object
readonly
Returns the value of attribute ssl_verify.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Object
constructor
Creates an ATTSpeech object.
-
#sample_rate(file_contents) ⇒ String
Determines the sample rate of the file.
-
#transcribe(options = {}, &block) ⇒ Hash
Allows you to send a file and return the transcription.
Constructor Details
#initialize(options = {}) ⇒ Object
Creates an ATTSpeech object
@param [Hash] args the options to intantiate with
@option args [String] :api_id the Nuance App API ID
@option args [String] :app_key the Nuance App Key
@option args [String] :id the Nuance id
@option args [String] :base_url the url for the Nuance API, default is 'https://api.att.com'
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/nuance/transcription.rb', line 18 def initialize(={}) raise ArgumentError, ':app_id must be set' if ![:app_id] raise ArgumentError, ':app_key must be set' if ![:app_key] raise ArgumentError, ':id must be set' if ![:id] @base_uri = [:base_uri] || 'https://dictation.nuancemobility.net' @app_id = [:app_id] || ENV['NUANCE_APP_ID'] @app_key = [:app_key] || ENV['NUANCE_APP_KEY'] @id = [:id] || rand(100000000) @logging = [:logging] || false @resource = "/NMDPAsrCmdServlet/dictation?appId=#{@app_id}&appKey=#{@app_key}&id=#{@id}" @ssl_verify = [:ssl_verify].nil? ? true : [:ssl_verify] self end |
Instance Attribute Details
#base_uri ⇒ Object (readonly)
Returns the value of attribute base_uri.
6 7 8 |
# File 'lib/nuance/transcription.rb', line 6 def base_uri @base_uri end |
#ssl_verify ⇒ Object (readonly)
Returns the value of attribute ssl_verify.
6 7 8 |
# File 'lib/nuance/transcription.rb', line 6 def ssl_verify @ssl_verify end |
Instance Method Details
#sample_rate(file_contents) ⇒ String
Determines the sample rate of the file
84 85 86 87 88 89 90 |
# File 'lib/nuance/transcription.rb', line 84 def sample_rate(file_contents) filename = '/tmp/' + SecureRandom.uuid + '.wav' IO.binwrite filename, file_contents wave = WaveInfo.new filename File.delete filename wave.sample_rate end |
#transcribe(options = {}, &block) ⇒ Hash
Allows you to send a file and return the transcription
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/nuance/transcription.rb', line 46 def transcribe(={}, &block) @accept_topic = [:accept_topic] || 'Dictation' @accept_language = [:language] || 'en_US' @accept = [:accept] || 'application/xml' @ssl_verify = [:ssl_verify].nil? ? @ssl_verify : [:ssl_verify] raise ArgumentError, ':content_type must be valid' if !content_type_valid?([:content_type]) raise ArgumentError, ':file_contents must be present' if ![:file_contents] raise ArgumentError, ':language must be valid' if !language_valid?([:language]) @content_type = [:content_type].nil? ? "audio/x-wav;codec=pcm;bit=16;rate=#{sample_rate([:file_contents]).to_s}" : [:content_type] create_connection begin response = @connection.post @resource, [:file_contents], { :Content_Type => @content_type, :Accept_Language => @accept_language, :Accept_Topic => @accept_topic, :Accept => @accept, :Transfer_Encoding => 'chunked' } if response.status == 200 block.call process(response) if block_given? process(response) else response end rescue => e raise RuntimeError, e.to_s end end |