azure_stt 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +7 -0
  2. data/.github/dependabot.yml +6 -0
  3. data/.github/workflows/ci.yml +33 -0
  4. data/.gitignore +56 -0
  5. data/.rubocop.yml +22 -0
  6. data/CHANGELOG.md +28 -0
  7. data/CODE_OF_CONDUCT.md +74 -0
  8. data/Gemfile +24 -0
  9. data/Gemfile.lock +141 -0
  10. data/LICENSE +21 -0
  11. data/README.md +129 -0
  12. data/Rakefile +27 -0
  13. data/azure_stt.gemspec +29 -0
  14. data/bin/console +15 -0
  15. data/bin/setup +8 -0
  16. data/docs/AzureBatchTranscription.html +301 -0
  17. data/docs/AzureBatchTranscription/Configuration.html +212 -0
  18. data/docs/AzureSTT.html +305 -0
  19. data/docs/AzureSTT/Client.html +1008 -0
  20. data/docs/AzureSTT/Configuration.html +282 -0
  21. data/docs/AzureSTT/Error.html +305 -0
  22. data/docs/AzureSTT/Models.html +135 -0
  23. data/docs/AzureSTT/Models/Base.html +139 -0
  24. data/docs/AzureSTT/Models/CombinedRecognizedPhrases.html +538 -0
  25. data/docs/AzureSTT/Models/File.html +794 -0
  26. data/docs/AzureSTT/Models/RecognizedPhrase.html +769 -0
  27. data/docs/AzureSTT/Models/Report.html +384 -0
  28. data/docs/AzureSTT/Models/Result.html +796 -0
  29. data/docs/AzureSTT/Models/Sentence.html +615 -0
  30. data/docs/AzureSTT/Models/Transcription.html +1415 -0
  31. data/docs/AzureSTT/Models/Word.html +615 -0
  32. data/docs/AzureSTT/NetError.html +159 -0
  33. data/docs/AzureSTT/Parsers.html +128 -0
  34. data/docs/AzureSTT/Parsers/Base.html +404 -0
  35. data/docs/AzureSTT/Parsers/CombinedRecognizedPhrases.html +156 -0
  36. data/docs/AzureSTT/Parsers/File.html +156 -0
  37. data/docs/AzureSTT/Parsers/RecognizedPhrase.html +156 -0
  38. data/docs/AzureSTT/Parsers/Report.html +156 -0
  39. data/docs/AzureSTT/Parsers/Result.html +156 -0
  40. data/docs/AzureSTT/Parsers/Sentence.html +156 -0
  41. data/docs/AzureSTT/Parsers/Transcription.html +156 -0
  42. data/docs/AzureSTT/Parsers/Word.html +156 -0
  43. data/docs/AzureSTT/ServiceError.html +159 -0
  44. data/docs/AzureSTT/Session.html +767 -0
  45. data/docs/AzureSTT/Transcription.html +635 -0
  46. data/docs/AzureSTT/Types.html +116 -0
  47. data/docs/AzureStt_.html +121 -0
  48. data/docs/_index.html +392 -0
  49. data/docs/class_list.html +51 -0
  50. data/docs/css/common.css +1 -0
  51. data/docs/css/full_list.css +58 -0
  52. data/docs/css/style.css +497 -0
  53. data/docs/file.README.html +201 -0
  54. data/docs/file_list.html +56 -0
  55. data/docs/frames.html +17 -0
  56. data/docs/index.html +201 -0
  57. data/docs/js/app.js +314 -0
  58. data/docs/js/full_list.js +216 -0
  59. data/docs/js/jquery.js +4 -0
  60. data/docs/method_list.html +707 -0
  61. data/docs/top-level-namespace.html +110 -0
  62. data/env.sample +2 -0
  63. data/lib/azure_stt.rb +20 -0
  64. data/lib/azure_stt/client.rb +189 -0
  65. data/lib/azure_stt/configuration.rb +28 -0
  66. data/lib/azure_stt/errors.rb +25 -0
  67. data/lib/azure_stt/models.rb +30 -0
  68. data/lib/azure_stt/models/base.rb +27 -0
  69. data/lib/azure_stt/models/combined_recognized_phrases.rb +48 -0
  70. data/lib/azure_stt/models/file.rb +72 -0
  71. data/lib/azure_stt/models/recognized_phrase.rb +70 -0
  72. data/lib/azure_stt/models/report.rb +31 -0
  73. data/lib/azure_stt/models/result.rb +76 -0
  74. data/lib/azure_stt/models/sentence.rb +53 -0
  75. data/lib/azure_stt/models/transcription.rb +185 -0
  76. data/lib/azure_stt/models/word.rb +54 -0
  77. data/lib/azure_stt/parsers.rb +19 -0
  78. data/lib/azure_stt/parsers/base.rb +42 -0
  79. data/lib/azure_stt/parsers/combined_recognized_phrases.rb +28 -0
  80. data/lib/azure_stt/parsers/file.rb +28 -0
  81. data/lib/azure_stt/parsers/recognized_phrase.rb +45 -0
  82. data/lib/azure_stt/parsers/report.rb +25 -0
  83. data/lib/azure_stt/parsers/result.rb +56 -0
  84. data/lib/azure_stt/parsers/sentence.rb +45 -0
  85. data/lib/azure_stt/parsers/transcription.rb +34 -0
  86. data/lib/azure_stt/parsers/word.rb +28 -0
  87. data/lib/azure_stt/session.rb +100 -0
  88. data/lib/azure_stt/version.rb +5 -0
  89. metadata +158 -0
@@ -0,0 +1,110 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>
7
+ Top Level Namespace
8
+
9
+ &mdash; Documentation by YARD 0.9.26
10
+
11
+ </title>
12
+
13
+ <link rel="stylesheet" href="css/style.css" type="text/css" />
14
+
15
+ <link rel="stylesheet" href="css/common.css" type="text/css" />
16
+
17
+ <script type="text/javascript">
18
+ pathId = "";
19
+ relpath = '';
20
+ </script>
21
+
22
+
23
+ <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
24
+
25
+ <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
26
+
27
+
28
+ </head>
29
+ <body>
30
+ <div class="nav_wrap">
31
+ <iframe id="nav" src="class_list.html?1"></iframe>
32
+ <div id="resizer"></div>
33
+ </div>
34
+
35
+ <div id="main" tabindex="-1">
36
+ <div id="header">
37
+ <div id="menu">
38
+
39
+ <a href="_index.html">Index</a> &raquo;
40
+
41
+
42
+ <span class="title">Top Level Namespace</span>
43
+
44
+ </div>
45
+
46
+ <div id="search">
47
+
48
+ <a class="full_list_link" id="class_list_link"
49
+ href="class_list.html">
50
+
51
+ <svg width="24" height="24">
52
+ <rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
53
+ <rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
54
+ <rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
55
+ </svg>
56
+ </a>
57
+
58
+ </div>
59
+ <div class="clear"></div>
60
+ </div>
61
+
62
+ <div id="content"><h1>Top Level Namespace
63
+
64
+
65
+
66
+ </h1>
67
+ <div class="box_info">
68
+
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+
79
+ </div>
80
+
81
+ <h2>Defined Under Namespace</h2>
82
+ <p class="children">
83
+
84
+
85
+ <strong class="modules">Modules:</strong> <span class='object_link'><a href="AzureSTT.html" title="AzureSTT (module)">AzureSTT</a></span>, <span class='object_link'><a href="AzureStt_.html" title="AzureStt (module)">AzureStt</a></span>
86
+
87
+
88
+
89
+
90
+ </p>
91
+
92
+
93
+
94
+
95
+
96
+
97
+
98
+
99
+
100
+ </div>
101
+
102
+ <div id="footer">
103
+ Generated on Thu Jun 24 14:16:27 2021 by
104
+ <a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
105
+ 0.9.26 (ruby-2.6.6).
106
+ </div>
107
+
108
+ </div>
109
+ </body>
110
+ </html>
data/env.sample ADDED
@@ -0,0 +1,2 @@
1
+ REGION=centralus
2
+ SUBSCRIPTION_KEY=k3ah8ztrc4ojeh98r05zh7v6x9w62lqp
data/lib/azure_stt.rb ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Top level module for AzureSTT
5
+ #
6
+ module AzureSTT
7
+ end
8
+
9
+ require_relative 'azure_stt/version'
10
+ require_relative 'azure_stt/configuration'
11
+ require_relative 'azure_stt/errors'
12
+ require_relative 'azure_stt/client'
13
+ require_relative 'azure_stt/models'
14
+ require_relative 'azure_stt/parsers'
15
+ require_relative 'azure_stt/session'
16
+
17
+ AzureSTT.configure do |config|
18
+ config.subscription_key = ENV['SUBSCRIPTION_KEY']
19
+ config.region = ENV['REGION']
20
+ end
@@ -0,0 +1,189 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'httparty'
4
+
5
+ module AzureSTT
6
+ #
7
+ # Client class that uses HTTParty to communicate with the API
8
+ #
9
+ class Client
10
+ include HTTParty
11
+
12
+ attr_reader :region, :subscription_key
13
+
14
+ #
15
+ # Initialize the client
16
+ #
17
+ # @param [String] subscription_key Cognitive Services API Key
18
+ # @param [String] region The region of your resources
19
+ #
20
+ def initialize(region:, subscription_key:)
21
+ @subscription_key = subscription_key
22
+ @region = region
23
+ self.class.base_uri "https://#{region}.api.cognitive.microsoft.com/speechtotext/v3.0"
24
+ end
25
+
26
+ #
27
+ # Create a transcription for a batch or a single file.
28
+ #
29
+ # @see https://francecentral.dev.cognitive.microsoft.com/docs/services/speech-to-text-api-v3-0/operations/CreateTranscription
30
+ #
31
+ # @param [Hash] args
32
+ #
33
+ # @return [Hash] The JSON body response, parsed by HTTParty
34
+ #
35
+ def create_transcription(**args)
36
+ results = post(
37
+ '/transcriptions',
38
+ args.to_json
39
+ )
40
+
41
+ results.parsed_response
42
+ end
43
+
44
+ #
45
+ # Get a transcription by giving it's id
46
+ #
47
+ # @param [String] id The identifier of the transcription
48
+ #
49
+ # @return [Hash] The JSON body response, parsed by HTTParty
50
+ #
51
+ def get_transcription(id)
52
+ results = get("/transcriptions/#{id}")
53
+
54
+ results.parsed_response
55
+ end
56
+
57
+ #
58
+ # Get an Array of all the transcriptions
59
+ #
60
+ # @param [Integer] skip Number of transcriptions that will be skipped (optional)
61
+ # @param [Integer] top Number of transcriptions that will be included (optional)
62
+ #
63
+ # @return [Array[Hash]] Array of all the transcriptions. The transcriptions
64
+ # are Hashes parsed by HTTParty.
65
+ #
66
+ def get_transcriptions(skip: nil, top: nil)
67
+ results = get(
68
+ '/transcriptions',
69
+ {
70
+ skip: skip,
71
+ top: top
72
+ }.compact
73
+ )
74
+
75
+ results.parsed_response['values']
76
+ end
77
+
78
+ #
79
+ # Get an array containing the files for a given transcription
80
+ #
81
+ # @see https://uscentral.dev.cognitive.microsoft.com/docs/services/speech-to-text-api-v3-0/operations/GetTranscriptionFiles
82
+ #
83
+ # @param [Integer] id The identifier of the transcription
84
+ #
85
+ # @return [Array[Hash]] Array of the files of a transcription
86
+ #
87
+ def get_transcription_files(id)
88
+ results = get("/transcriptions/#{id}/files")
89
+
90
+ results.parsed_response['values']
91
+ end
92
+
93
+ #
94
+ # Read a JSON file and parse it.
95
+ #
96
+ # @param [String] file_url The url of the content
97
+ #
98
+ # @return [Hash] the file parsed
99
+ #
100
+ def get_file(file_url)
101
+ response = self.class.get(file_url)
102
+
103
+ results = handle_response(response)
104
+
105
+ results.parsed_response
106
+ end
107
+
108
+ private
109
+
110
+ #
111
+ # Make a post request by giving a path and a body
112
+ #
113
+ # @param [String] path the path, which is added to the base_uri
114
+ # @param [String] body the body of the request
115
+ #
116
+ # @return [HTTParty::Response]
117
+ #
118
+ def post(path, body)
119
+ options = {
120
+ headers: headers,
121
+ body: body
122
+ }
123
+
124
+ response = self.class.post(path, options)
125
+ handle_response(response)
126
+ end
127
+
128
+ #
129
+ # Make a get request to the API.
130
+ #
131
+ # @param [String] path the path, which is added to the base_uri
132
+ # @param [Hash] parameters The parameters you want to add to the headers (empty by default)
133
+ #
134
+ # @return [HTTParty::Response]
135
+ #
136
+ def get(path, parameters = {})
137
+ options = {
138
+ headers: headers.merge(parameters)
139
+ }.compact
140
+
141
+ response = self.class.get(path, options)
142
+ handle_response(response)
143
+ end
144
+
145
+ #
146
+ # Handle the HTTParty::Response. If an error occured, an exception will be
147
+ # raised.
148
+ #
149
+ # @param [HTTParty] response The response received from the API
150
+ #
151
+ # @return [<Type>] <description>
152
+ #
153
+ # @raise [ServiceError] if an error occured from the API, for instance if
154
+ # subscription_key is invalid.
155
+ #
156
+ # @raise [NetError] if the server has not been reached
157
+ #
158
+ def handle_response(response)
159
+ case response.code
160
+ when 200..299
161
+ response
162
+ else
163
+ if response.request.format == :json
164
+ raise ServiceError.new(
165
+ code: response.code,
166
+ message: response.response.message
167
+ )
168
+ else
169
+ raise NetError.new(
170
+ code: response.code,
171
+ message: response.response.message
172
+ )
173
+ end
174
+ end
175
+ end
176
+
177
+ #
178
+ # The header needed to make a request
179
+ #
180
+ # @return [Hash]
181
+ #
182
+ def headers
183
+ {
184
+ 'Ocp-Apim-Subscription-Key' => subscription_key,
185
+ 'Content-Type' => 'application/json'
186
+ }
187
+ end
188
+ end
189
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AzureSTT
4
+ #
5
+ # Configurator pattern to be able to change the subscription key. By default
6
+ # the key is in a .env file
7
+ #
8
+ class Configuration
9
+ attr_accessor :subscription_key, :region
10
+ end
11
+
12
+ #
13
+ # Get the configuration for AzureSTT
14
+ #
15
+ # @return [Configuration]
16
+ #
17
+ def self.configuration
18
+ @configuration ||= Configuration.new
19
+ end
20
+
21
+ #
22
+ # Modify the configuration
23
+ #
24
+ def self.configure
25
+ yield configuration
26
+ @client = nil
27
+ end
28
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AzureSTT
4
+ #
5
+ # Top level error for AzureSTT specific errors
6
+ #
7
+ class Error < StandardError
8
+ attr_reader :code
9
+
10
+ def initialize(code:, message:)
11
+ @code = code
12
+ super("#{message} (#{code})")
13
+ end
14
+ end
15
+
16
+ #
17
+ # Error raised when there is an error 400
18
+ #
19
+ class ServiceError < Error; end
20
+
21
+ #
22
+ # Error raised when there is an error 500
23
+ #
24
+ class NetError < Error; end
25
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry-struct'
4
+
5
+ module AzureSTT
6
+ #
7
+ # Module for the models. Those models use dry-struct.
8
+ #
9
+ # @see https://dry-rb.org/gems/dry-struct/
10
+ #
11
+ module Models
12
+ end
13
+
14
+ #
15
+ # Include Dry.Types()
16
+ #
17
+ module Types
18
+ include Dry.Types()
19
+ end
20
+ end
21
+
22
+ require_relative 'models/base'
23
+ require_relative 'models/file'
24
+ require_relative 'models/report'
25
+ require_relative 'models/combined_recognized_phrases'
26
+ require_relative 'models/word'
27
+ require_relative 'models/sentence'
28
+ require_relative 'models/recognized_phrase'
29
+ require_relative 'models/result'
30
+ require_relative 'models/transcription'
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AzureSTT
4
+ module Models
5
+ #
6
+ # Base class for the models
7
+ #
8
+ class Base < Dry::Struct
9
+ # throw an error when unknown keys provided
10
+ schema schema.strict
11
+
12
+ # convert string keys to symbols
13
+ transform_keys(&:to_sym)
14
+
15
+ # resolve default types on nil
16
+ transform_types do |type|
17
+ if type.default?
18
+ type.constructor do |value|
19
+ value.nil? ? Dry::Types::Undefined : value
20
+ end
21
+ else
22
+ type
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AzureSTT
4
+ module Models
5
+ #
6
+ # Model class for the combinedRecognizedPhrases field in the results of
7
+ # the transcriptions
8
+ #
9
+ class CombinedRecognizedPhrases < Base
10
+ #
11
+ # The channel of the combination. The API supports only two channels, so
12
+ # it is 0 or 1
13
+ #
14
+ # @!attribute [r] channel
15
+ # @return [Types::Coercible::Integer]
16
+ attribute :channel, Types::Coercible::Integer
17
+
18
+ #
19
+ # The lexical field
20
+ #
21
+ # @!attribute [r] lexical
22
+ # @return [Types::Coercible::String]
23
+ attribute :lexical, Types::Coercible::String
24
+
25
+ #
26
+ # The itn field
27
+ #
28
+ # @!attribute [r] itn
29
+ # @return [Types::Coercible::String]
30
+ attribute :itn, Types::Coercible::String
31
+
32
+ #
33
+ # The maskedITN field
34
+ #
35
+ # @!attribute [r] masked_itn
36
+ # @return [Types::Coercible::String]
37
+ attribute :masked_itn, Types::Coercible::String
38
+
39
+ #
40
+ # The display field. Displays the text with the format you specified in
41
+ # the paramters of the transcription.
42
+ #
43
+ # @!attribute [r] transcript
44
+ # @return [Types::Coercible::String]
45
+ attribute :transcript, Types::Coercible::String
46
+ end
47
+ end
48
+ end