lib/google/cloud/speech/result.rb in google-cloud-speech-0.25.0 vs lib/google/cloud/speech/result.rb in google-cloud-speech-0.26.0

- old
+ new

@@ -12,10 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. require "google/cloud/speech/v1" +require "google/cloud/speech/convert" module Google module Cloud module Speech ## @@ -33,10 +34,14 @@ # @attr_reader [Float] confidence The confidence estimate between 0.0 and # 1.0. A higher number means the system is more confident that the # recognition is correct. This field is typically provided only for the # top hypothesis. A value of 0.0 is a sentinel value indicating # confidence was not set. + # @attr_reader [Array<Result::Word>] words A list of words with additional + # information about each word. Currently, the only additional + # information provided is the the start and end time offsets. Available + # when using the `words` argument in relevant methods. # @attr_reader [Array<Result::Alternative>] alternatives Additional # recognition hypotheses (up to the value specified in # `max_alternatives`). The server may return fewer than # `max_alternatives`. # @@ -54,32 +59,65 @@ # result = results.first # result.transcript #=> "how old is the Brooklyn Bridge" # result.confidence #=> 0.9826789498329163 # class Result - attr_reader :transcript, :confidence, :alternatives + attr_reader :transcript, :confidence, :words, :alternatives ## # @private Creates a new Results instance. - def initialize transcript, confidence, alternatives = [] - @transcript = transcript - @confidence = confidence + def initialize transcript, confidence, words = [], alternatives = [] + @transcript = transcript + @confidence = confidence + @words = words @alternatives = alternatives end ## # @private New Results from a SpeechRecognitionAlternative object. def self.from_grpc grpc head, *tail = *grpc.alternatives return nil if head.nil? + words = Array(head.words).map do |w| + Word.new w.word, Convert.duration_to_number(w.start_time), + Convert.duration_to_number(w.end_time) + end alternatives = tail.map do |alt| Alternative.new alt.transcript, alt.confidence end - new head.transcript, head.confidence, alternatives + new head.transcript, head.confidence, words, alternatives end ## + # Word-specific information for recognized words. Currently, the only + # additional information provided is the the start and end time offsets. + # Available when using the `words` argument in relevant methods. + # + # @attr_reader [String] word The word corresponding to this set of + # information. + # @attr_reader [Numeric] start_time Time offset relative to the + # beginning of the audio, and corresponding to the start of the spoken + # word. This field is only set if `words` was specified. This is an + # experimental feature and the accuracy of the time offset can vary. + # @attr_reader [Numeric] end_time Time offset relative to the + # beginning of the audio, and corresponding to the end of the spoken + # word. This field is only set if `words` was specified. This is an + # experimental feature and the accuracy of the time offset can vary. + class Word + attr_reader :word, :start_time, :end_time + alias_method :to_str, :word + + ## + # @private Creates a new Result::Word instance. + def initialize word, start_time, end_time + @word = word + @start_time = start_time + @end_time = end_time + end + end + + ## # # Result::Alternative # # A speech recognition result corresponding to a portion of the audio. # # @attr_reader [String] transcript Transcript text representing the @@ -112,10 +150,10 @@ attr_reader :transcript, :confidence ## # @private Creates a new Result::Alternative instance. def initialize transcript, confidence - @transcript = transcript + @transcript = transcript @confidence = confidence end end end