-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathspeech.rb
More file actions
148 lines (134 loc) · 4.99 KB
/
speech.rb
File metadata and controls
148 lines (134 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
require 'json'
require 'immutable_struct'
module Att
module Codekit
module Model
# Response of a speech request
class SpeechResponse < ImmutableStruct.new(:id, :status, :nbest)
# @!attribute [r] id
# @return [String] the id of the request made
# @!attribute [r] status
# @return [String] status of the request made
# @!attribute [r] nbest
# @return [Array<#NBest>] list of nbest objects
# Factory method to create an object from a json string
#
# @param json [String] a json encoded string
#
# @return [SpeechResponse] a parsed object
def self.createFromJson(json)
#puts json
self.createFromParsedJson(JSON.parse(json))
end
# Factory method to create an object from a json string
#
# @param json [Object] a decoded json string
#
# @return [SpeechResponse] a parsed object
def self.createFromParsedJson(json)
root = json["Recognition"]
id = root["ResponseId"]
status = root["Status"]
nbest = NBest.createFromParsedJson(root["NBest"])
new(id, status, nbest)
end
end
# Container for a nbest object
class NBest < ImmutableStruct.new(:confidence, :grade, :hypothesis,
:language, :nlu_hypothesis, :result,
:scores, :words)
# @!attribute [r] confidence
# @return [Float] accuracy of request on a scale of 0.0 - 1.0
# @!attribute [r] grade
# @return [String] quantification of the accuracy
# @!attribute [r] hypothesis
# @return [String] transcription of the audio
# @!attribute [r] language
# @return [String] language used on request
# @!attribute [r] nlu_hypothesis
# @return [#NLUHypothesis] complex structure containing a set of
# keyword-and-value pairs derived from the speech recognition output (may be nil)
# @!attribute [r] result
# @return [String] similar to hypothesis, usually a formatted version
# @!attribute [r] scores
# @return [Array<Float>] accuracy of each word on a scale of 0.0 - 1.0
# @!attribute [r] words
# @return [Array<String>] list of each word in the response
# Factory method to create an object from a json string
#
# @param json [String] a json encoded string
#
# @return [Array<NBest>] a parsed object
def self.createFromJson(json)
self.createFromParsedJson(JSON.parse(json))
end
# Factory method to create an object from a json string
#
# @param json [Object] a decoded json string
#
# @return [Array<NBest>] a parsed object
def self.createFromParsedJson(json)
list = Array.new
Array(json).each do |nb|
list << new(nb["Confidence"],
nb["Grade"],
nb["Hypothesis"],
nb["LanguageId"],
NLUHypothesis.createFromParsedJson(nb["NluHypothesis"]),
nb["ResultText"],
nb["WordScores"],
nb["Words"] )
end
list
end
end
#Contains nlu results
class NLUHypothesis < ImmutableStruct.new(:grammar, :out)
# @!attribute [r] grammar
# @return [String]
# @!attribute [r] out
# @return [String]
# Factory method to create an object from a json string
#
# @param json [String] a json encoded string
#
# @return [Array<NLUHypothesis>] a parsed object
def self.createFromJson(json)
self.createFromParsedJson(JSON.parse(json))
end
# Factory method to create an object from a json string
#
# @param json [Object] a decoded json string
#
# @return [Array<NLUHypothesis>] a parsed object
def self.createFromParsedJson(json)
list = Array.new
if json
#Array(json["OutComposite"]).each do |nlu|
#list << new(nlu["Grammar"], nlu["Out"] )
#end
list << new("default.grxml",json["Out"])
end
list
end
end
#Contains the tts data and content type
class TTSResponse < ImmutableStruct.new(:data, :type)
# Response for a text to speech request
#
# @!attribute [r] data
# @return [String] audio file based on input text
# @!attribute [r] type
# @return [String] content type of the data
# Factory method to create an object from a restful response
#
# @param response [RestClient::Response] a restful response
#
# @return [TTSResponse] a parsed object
def self.createFromResponse(response)
new(response, response.headers[:content_type])
end
end
end
end
end