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,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AzureSTT
4
+ module Models
5
+ #
6
+ # Model for a word. A word is optional in a sentence
7
+ #
8
+ class Word < Base
9
+ #
10
+ # The word
11
+ #
12
+ # @!attribute [r] word
13
+ # @return [Types::Coercible::String]
14
+ attribute :word, Types::Coercible::String
15
+
16
+ #
17
+ # The offset of the word in the audio. Ex: 'PT0.13S' means that the
18
+ # phrase begins at 13 seconds on the audio file
19
+ #
20
+ # @!attribute [r] offset
21
+ # @return [Types::Coercible::String]
22
+ attribute? :offset, Types::Coercible::String
23
+
24
+ #
25
+ # The duration of the word. Ex: 'PT0.13S' means that the phrase lasts 13
26
+ # seconds
27
+ #
28
+ # @!attribute [r] duration
29
+ # @return [Types::Coercible::String]
30
+ attribute? :duration, Types::Coercible::String
31
+
32
+ #
33
+ # The offset of the word in the audio in ticks.
34
+ #
35
+ # @!attribute [r] offset_in_ticks
36
+ # @return [Types::Coercible::Integer]
37
+ attribute? :offset_in_ticks, Types::Coercible::Integer
38
+
39
+ #
40
+ # The duration of the word in ticks
41
+ #
42
+ # @!attribute [r] duration_in_ticks
43
+ # @return [Types::Coercible::Integer]
44
+ attribute? :duration_in_ticks, Types::Coercible::Integer
45
+
46
+ #
47
+ # The confidence score of the transcripted word.
48
+ #
49
+ # @!attribute [r] confidence
50
+ # @return [Types::Coercible::Float]
51
+ attribute :confidence, Types::Coercible::Float
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AzureSTT
4
+ #
5
+ # Module for the parsers
6
+ #
7
+ module Parsers
8
+ end
9
+ end
10
+
11
+ require_relative 'parsers/base'
12
+ require_relative 'parsers/transcription'
13
+ require_relative 'parsers/file'
14
+ require_relative 'parsers/report'
15
+ require_relative 'parsers/result'
16
+ require_relative 'parsers/combined_recognized_phrases'
17
+ require_relative 'parsers/recognized_phrase'
18
+ require_relative 'parsers/sentence'
19
+ require_relative 'parsers/word'
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AzureSTT
4
+ module Parsers
5
+ #
6
+ # Base class for the parsers
7
+ #
8
+ class Base
9
+ attr_reader :hash
10
+
11
+ #
12
+ # Initialize the parser
13
+ #
14
+ # @param [Hash] hash The hash containing the information from the API
15
+ #
16
+ def initialize(hash)
17
+ @hash = hash
18
+ end
19
+
20
+ #
21
+ # Get the attributes parsed to be able to build the model
22
+ #
23
+ # @return [Hash]
24
+ #
25
+ def attributes
26
+ @attributes ||= build_attributes
27
+ end
28
+
29
+ protected
30
+
31
+ #
32
+ # This method must be overriden in the children
33
+ #
34
+ # @return [Hash] The attributes parsed to be able to build the model
35
+ #
36
+ def build_attributes
37
+ raise NoMethodError, 'Implement method #build_attributes in your' \
38
+ 'custom parser'
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_String_literal: true
2
+
3
+ module AzureSTT
4
+ module Parsers
5
+ #
6
+ # Parse the CombinedRecognizedPhrases from the result file to a
7
+ # Models::CombinedRecognizedPhrases
8
+ #
9
+ class CombinedRecognizedPhrases < Base
10
+ protected
11
+
12
+ #
13
+ # Build a hash to instantiate a Models::CombinedRecognizedPhrase
14
+ #
15
+ # @return [Hash]
16
+ #
17
+ def build_attributes
18
+ {
19
+ channel: hash['channel'],
20
+ lexical: hash['lexical'],
21
+ itn: hash['itn'],
22
+ masked_itn: hash['maskedITN'],
23
+ transcript: hash['display']
24
+ }.compact
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AzureSTT
4
+ module Parsers
5
+ #
6
+ # Parse a file from the call to the API to a Models::File
7
+ #
8
+ class File < Base
9
+ protected
10
+
11
+ #
12
+ # Build a hash which can create a Models::File
13
+ #
14
+ # @return [Hash] file's initializer parameters
15
+ #
16
+ def build_attributes
17
+ {
18
+ id: hash['self'].split('/').last,
19
+ name: hash['name'],
20
+ kind: hash['kind'],
21
+ properties: hash['properties'],
22
+ created_date_time: Date.parse(hash['createdDateTime']),
23
+ content_url: hash.dig('links', 'contentUrl')
24
+ }
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AzureSTT
4
+ module Parsers
5
+ #
6
+ # Parse a recognized phrase from a result file to Models::RecognizedPhrase
7
+ #
8
+ class RecognizedPhrase < Base
9
+ protected
10
+
11
+ #
12
+ # Build a hash to instantiate a Models::RecognizedPhrase
13
+ #
14
+ # @return [Hash]
15
+ #
16
+ def build_attributes
17
+ {
18
+ recognition_status: hash['recognitionStatus'],
19
+ channel: hash['channel'],
20
+ speaker: hash['speaker'],
21
+ offset: hash['offset'],
22
+ duration: hash['duration'],
23
+ offset_in_ticks: hash['offsetInTicks'],
24
+ duration_in_ticks: hash['durationInTicks'],
25
+ n_best: build_n_best
26
+ }.compact
27
+ end
28
+
29
+ private
30
+
31
+ #
32
+ # Build the n_best fiels if the RecognizedPhrase.
33
+ #
34
+ # @return [Array[Models::Sentence]]
35
+ #
36
+ def build_n_best
37
+ hash['nBest'].map do |sentence_hash|
38
+ Models::Sentence.new(
39
+ Sentence.new(sentence_hash).attributes
40
+ )
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AzureSTT
4
+ module Parsers
5
+ #
6
+ # Parse a report from a contentUrl to a Models::Report
7
+ #
8
+ class Report < Base
9
+ protected
10
+
11
+ #
12
+ # Build a hash which can create a Models::Report
13
+ #
14
+ # @return [Hash] report's initializer parameters
15
+ #
16
+ def build_attributes
17
+ {
18
+ successful_transcriptions_count: hash['successfulTranscriptionsCount'],
19
+ failed_transcriptions_count: hash['failedTranscriptionsCount'],
20
+ details: hash['details']
21
+ }
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AzureSTT
4
+ module Parsers
5
+ #
6
+ # Parse a result file from the API to a Models::Result
7
+ #
8
+ class Result < Base
9
+ protected
10
+
11
+ #
12
+ # Build a hash which can create a Models::Result
13
+ #
14
+ # @return [Hash] report's initializer parameters
15
+ #
16
+ def build_attributes
17
+ {
18
+ source: hash['source'],
19
+ timestamp: Date.parse(hash['timestamp']),
20
+ duration_in_ticks: hash['durationInTicks'],
21
+ duration: hash['dureation'],
22
+ combined_recognized_phrases: build_combined_recognized_phrases,
23
+ recognized_phrases: build_recognized_phrases
24
+ }
25
+ end
26
+
27
+ private
28
+
29
+ #
30
+ # Build the array of Models::CombinedRecognizedPhrases
31
+ #
32
+ # @return [Array[Models::CombinedRecognizedPhrases]]
33
+ #
34
+ def build_combined_recognized_phrases
35
+ hash['combinedRecognizedPhrases'].map do |combined_phrases_hash|
36
+ Models::CombinedRecognizedPhrases.new(
37
+ CombinedRecognizedPhrases.new(combined_phrases_hash).attributes
38
+ )
39
+ end
40
+ end
41
+
42
+ #
43
+ # Build the array of Models::RecognizedPhrase in a Models::Result
44
+ #
45
+ # @return [Array[RecognizedPhrase]]
46
+ #
47
+ def build_recognized_phrases
48
+ hash['recognizedPhrases'].map do |recognized_phrase_hash|
49
+ Models::RecognizedPhrase.new(
50
+ RecognizedPhrase.new(recognized_phrase_hash).attributes
51
+ )
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ # frozen_string_literal
4
+
5
+ module AzureSTT
6
+ module Parsers
7
+ #
8
+ # Parse the "nBest" field from the result file to a Models::Sentence
9
+ #
10
+ class Sentence < Base
11
+ protected
12
+
13
+ #
14
+ # Build the attributes needed to instantiate a Models::Sentence
15
+ #
16
+ # @return [Hash]
17
+ #
18
+ def build_attributes
19
+ {
20
+ confidence: hash['confidence'],
21
+ lexical: hash['lexical'],
22
+ itn: hash['itn'],
23
+ masked_itn: hash['maskedITN'],
24
+ transcript: hash['display'],
25
+ words: build_words
26
+ }.compact
27
+ end
28
+
29
+ private
30
+
31
+ #
32
+ # Build the words. Returns nil if they are not defined
33
+ #
34
+ # @return [Array[Word]]
35
+ #
36
+ def build_words
37
+ hash['words']&.map do |word_hash|
38
+ Models::Word.new(
39
+ Word.new(word_hash).attributes
40
+ )
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AzureSTT
4
+ module Parsers
5
+ #
6
+ # Parse a transcription from the call to the API to a Models::Transcription
7
+ #
8
+ class Transcription < Base
9
+ protected
10
+
11
+ # rubocop:disable Metrics/AbcSize
12
+
13
+ #
14
+ # Build a hash which can create a Models::Transcription
15
+ #
16
+ # @return [Hash] transcription's initializer parameters
17
+ #
18
+ def build_attributes
19
+ {
20
+ id: hash['self'].split('/').last,
21
+ model: hash.dig('model', 'self'),
22
+ links: hash['links'],
23
+ properties: hash['properties'],
24
+ last_action_date_time: Date.parse(hash['lastActionDateTime']),
25
+ created_date_time: Date.parse(hash['createdDateTime']),
26
+ status: hash['status'],
27
+ locale: hash['locale'],
28
+ display_name: hash['displayName']
29
+ }
30
+ end
31
+ # rubocop:enable Metrics/AbcSize
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AzureSTT
4
+ module Parsers
5
+ #
6
+ # Parse the words in the result file to Models::Word
7
+ #
8
+ class Word < Base
9
+ protected
10
+
11
+ #
12
+ # Build the attributes needed to instantiate a Models::Word
13
+ #
14
+ # @return [Hash]
15
+ #
16
+ def build_attributes
17
+ {
18
+ word: hash['word'],
19
+ offset: hash['offset'],
20
+ duration: hash['duration'],
21
+ offset_in_ticks: hash['offsetInTicks'],
22
+ duration_in_ticks: hash['durationInTicks'],
23
+ confidence: hash['confidence']
24
+ }.compact
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AzureSTT
4
+ #
5
+ # A session is the class the end user uses to retrieve the Transcription.
6
+ # It contains a client.
7
+ #
8
+ class Session
9
+ attr_reader :client
10
+
11
+ #
12
+ # Create a session. If you don't provide any subscription_key or region, the
13
+ # value is read from configuration.
14
+ #
15
+ # @param [String] region The region, optional, default is read from
16
+ # configuration
17
+ # @param [String] subscription_key The subscription, optional, default is
18
+ # read from configuration
19
+ #
20
+ def initialize(region: AzureSTT.configuration.region,
21
+ subscription_key: AzureSTT.configuration.subscription_key)
22
+ @client = Client.new(region: region, subscription_key: subscription_key)
23
+ end
24
+
25
+ #
26
+ # Create a transcription by calling the API.
27
+ #
28
+ # @see https://centralus.dev.cognitive.microsoft.com/docs/services/speech-to-text-api-v3-0/operations/CreateTranscription
29
+ #
30
+ # @param [Array[String]] content_urls The urls of your files
31
+ # @param [Hash] properties The properties you want to use for the
32
+ # transcription
33
+ # @param [String] locale The locale of the contained data
34
+ # @param [String] display_name The name of the transcription (can be
35
+ # left empty)
36
+ #
37
+ # @return [Models::Transcription] The transcription
38
+ #
39
+ def create_transcription(content_urls:, properties:, locale:, display_name:)
40
+ transcription_hash = client.create_transcription(
41
+ {
42
+ contentUrls: content_urls,
43
+ properties: properties,
44
+ locale: locale,
45
+ displayName: display_name
46
+ }
47
+ )
48
+ build_transcription_from_hash(transcription_hash)
49
+ end
50
+
51
+ #
52
+ # Get a transcription identified by an id.
53
+ #
54
+ # @see https://centralus.dev.cognitive.microsoft.com/docs/services/speech-to-text-api-v3-0/operations/GetTranscription
55
+ #
56
+ # @param [String] id The identifier of the transcription
57
+ #
58
+ # @return [Models::Transcription] the transcription
59
+ #
60
+ def get_transcription(id)
61
+ transcription_hash = client.get_transcription(id)
62
+ build_transcription_from_hash(transcription_hash)
63
+ end
64
+
65
+ #
66
+ # Get multiple transcriptions.
67
+ #
68
+ # @see https://centralus.dev.cognitive.microsoft.com/docs/services/speech-to-text-api-v3-0/operations/GetTranscriptions
69
+ #
70
+ # @param [Integer] skip Number of transcriptions that will be skipped (optional)
71
+ # @param [Integer] top Number of transcriptions that will be included after skipping
72
+ # (optional)
73
+ #
74
+ # @return [Array[Models::Transcription]]
75
+ #
76
+ def get_transcriptions(skip: nil, top: nil)
77
+ transcriptions_array = client.get_transcriptions(skip: skip, top: top)
78
+ transcriptions_array.map do |transcription_hash|
79
+ build_transcription_from_hash(transcription_hash)
80
+ end
81
+ end
82
+
83
+ private
84
+
85
+ #
86
+ # Build a transcription from a hash returned by the client. This hash
87
+ # contains the information from the JSON. It is then parsed and the
88
+ # transcription is intantiated.
89
+ #
90
+ # @param [Hash] hash The hash that the client returned
91
+ #
92
+ # @return [Models::Transcription] the created transcription
93
+ #
94
+ def build_transcription_from_hash(hash)
95
+ Models::Transcription.new(
96
+ Parsers::Transcription.new(hash).attributes.merge({ client: client })
97
+ )
98
+ end
99
+ end
100
+ end