Class: Nuance::Transcription

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/nuance/transcription.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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'

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/nuance/transcription.rb', line 18

def initialize(options={})
  raise ArgumentError, ':app_id must be set' if !options[:app_id]
  raise ArgumentError, ':app_key must be set' if !options[:app_key]
  raise ArgumentError, ':id must be set' if !options[:id]

  @base_uri   = options[:base_uri]  || 'https://dictation.nuancemobility.net'
  @app_id     = options[:app_id]    || ENV['NUANCE_APP_ID']
  @app_key    = options[:app_key]   || ENV['NUANCE_APP_KEY']
  @id         = options[:id]        || rand(100000000)
  @logging    = options[:logging]   || false
  @resource   = "/NMDPAsrCmdServlet/dictation?appId=#{@app_id}&appKey=#{@app_key}&id=#{@id}"
  @ssl_verify = options[:ssl_verify].nil? ? true : options[:ssl_verify]

  self
end

Instance Attribute Details

#base_uriObject (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_verifyObject (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

Parameters:

  • file_contents (Object)

    of the file

Returns:

  • (String)

    the sample rate



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

Parameters:

  • file_contents (String)

    to be processed

  • block (Block)

    to be called when the transcription completes

  • args (Hash)

    a customizable set of options

Returns:

  • (Hash)

    the resulting response from the Nuance API

Raises:

  • (ArgumentError)


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(options={}, &block)
  @accept_topic    = options[:accept_topic] || 'Dictation'
  @accept_language = options[:language]     || 'en_US'
  @accept          = options[:accept]       || 'application/xml'
  @ssl_verify      = options[:ssl_verify].nil? ? @ssl_verify : options[:ssl_verify]

  raise ArgumentError, ':content_type must be valid'    if !content_type_valid?(options[:content_type])
  raise ArgumentError, ':file_contents must be present' if !options[:file_contents]
  raise ArgumentError, ':language must be valid'        if !language_valid?(options[:language])

  @content_type  = options[:content_type].nil? ? "audio/x-wav;codec=pcm;bit=16;rate=#{sample_rate(options[:file_contents]).to_s}" : options[:content_type]

  create_connection

  begin
    response = @connection.post @resource,
               options[: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