Class: AlexaModelBuilder
- Inherits:
-
Object
- Object
- AlexaModelBuilder
- Defined in:
- lib/alexa_modelbuilder.rb
Instance Attribute Summary collapse
-
#invocation ⇒ Object
readonly
Returns the value of attribute invocation.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#utterances ⇒ Object
readonly
Returns the value of attribute utterances.
Instance Method Summary collapse
-
#build_manifest(h) ⇒ Object
manifest.
-
#initialize(s = nil, debug: false, locale: 'en-GB') ⇒ AlexaModelBuilder
constructor
A new instance of AlexaModelBuilder.
-
#intents ⇒ Object
fetch the intents from the document model.
-
#read(obj) ⇒ Object
Read an Alexa Interaction Model (in JSON format).
- #to_h ⇒ Object
-
#to_json(pretty: true, copy: false) ⇒ Object
Returns an Alexa Interaction Model in JSON format.
-
#to_manifest(json: false) ⇒ Object
Returns a generated manifest from the document model.
-
#to_model(json: false) ⇒ Object
Returns an Alexa Interaction model as a Hash object.
- #to_s ⇒ Object
Constructor Details
#initialize(s = nil, debug: false, locale: 'en-GB') ⇒ AlexaModelBuilder
Returns a new instance of AlexaModelBuilder.
17 18 19 20 21 22 |
# File 'lib/alexa_modelbuilder.rb', line 17 def initialize(s=nil, debug: false, locale: 'en-GB') @debug, @locale = debug, locale parse(s) if s end |
Instance Attribute Details
#invocation ⇒ Object (readonly)
Returns the value of attribute invocation.
15 16 17 |
# File 'lib/alexa_modelbuilder.rb', line 15 def invocation @invocation end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
15 16 17 |
# File 'lib/alexa_modelbuilder.rb', line 15 def name @name end |
#utterances ⇒ Object (readonly)
Returns the value of attribute utterances.
15 16 17 |
# File 'lib/alexa_modelbuilder.rb', line 15 def utterances @utterances end |
Instance Method Details
#build_manifest(h) ⇒ Object
manifest
vendorId: [generate from vendors.first] locale: input (default) name: input summary: input (default) description: input (default) keywords: input (default) examplePhrases: [generate from intent utterances] testingInstructions: input [generate from intent utterances] endpoint: input
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/alexa_modelbuilder.rb', line 127 def build_manifest(h) manifest = { "manifest" => { "publishingInformation" => { "locales" => { }, "isAvailableWorldwide" => false, "testingInstructions" => "1) Say 'Alexa, say something'", "category" => "SMART_HOME", "distributionCountries" => [ ] }, "apis" => { "custom" => { "endpoint" => { "sslCertificateType"=>"Trusted", "uri" => "https://someserver.com/alexaskills" } } }, "manifestVersion" => "1.0", "privacyAndCompliance" => { "allowsPurchases" => false, "locales" => { }, "isExportCompliant" => true, "isChildDirected" => false, "usesPersonalInfo" => false } } } manifest['vendorId'] = h[:vendor_id] if h[:vendor_id] m = manifest['manifest'] h[:locale] ||= @locale info = {} info['summary'] = h[:summary] || h[:name] @utterances = h[:intent].select{|x| x.is_a? Hash}.flat_map do |x| x.first[-1][:utterance] end examples = ["Alexa, open %s." % h[:invocation]] examples += h[:intent].select{|x| x.is_a? Hash}.take(2).map do |x| phrases = x.first[-1][:utterance] puts 'phrases: ' + phrases.inspect if @debug "Alexa, %s." % (phrases.is_a?(Array) ? phrases.first : phrases) end info['examplePhrases'] = examples info['keywords'] = h[:keywords] ? h[:keywords] : \ h[:name].split.map(&:downcase) info['name'] = h[:name] if h[:name] info['description'] = h[:description] || info['summary'] m['publishingInformation']['locales'] = {h[:locale] => info} countries = {gb: %w(GB US), us: %w(US GB)} m['publishingInformation']['distributionCountries'] = \ countries[h[:locale][/[A-Z]+/].downcase.to_sym] m['apis']['custom']['endpoint']['uri'] = h[:endpoint] if h[:endpoint] toc = { 'termsOfUseUrl' => 'http://www.termsofuse.sampleskill.com', 'privacyPolicyUrl' => 'http://www.myprivacypolicy.sampleskill.com' } m['privacyAndCompliance']['locales'] = {h[:locale] => toc} instruct = ["open %s" % h[:invocation]] instruct << h[:intent].select{|x| x.is_a? Hash}\ .first.values.first[:utterance].first tests = instruct.map.with_index {|x, i| "%s) Say 'Alexa, %s'" % [i+1, x]} m['publishingInformation']['testingInstructions'] = tests.join(' ') manifest end |
#intents ⇒ Object
fetch the intents from the document model
26 27 28 |
# File 'lib/alexa_modelbuilder.rb', line 26 def intents() self.to_h[:intent].map {|x| x.keys.first} end |
#read(obj) ⇒ Object
Read an Alexa Interaction Model (in JSON format)
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 |
# File 'lib/alexa_modelbuilder.rb', line 32 def read(obj) s, _ = RXFHelper.read(obj) h = JSON.parse(s, symbolize_names: true) lm = h[:interactionModel][:languageModel] lm[:invocationName] out = [] out << 'invocation: ' + lm[:invocationName] + "\n" puts lm[:intents].inspect if @debug lm[:intents].each do |intent| puts ('intent: ' + intent[:name].inspect).debug if @debug out << intent[:name] + "\n" if intent[:samples] then intent[:samples].each do |utterance| puts 'utterance: ' + utterance.inspect if @debug out << " " + utterance end if intent[:slots] and intent[:slots].any? then out << "\n slots:" intent[:slots].each do |slot| out << " %s: %s" % [slot[:name], slot[:type]] end end out << "\n" if intent[:samples].any? end end if lm[:types] and lm[:types].any? then out << "types:" lm[:types].each do |type| values = type[:values].map do |x| synonyms = x[:name][:synonyms] val = x[:name][:value] val += ' (' + synonyms.join(', ') + ')' if synonyms and synonyms.any? val end out << " %s: %s" % [type[:name], values.join(', ')] end end out << "\n" @s = out.join("\n") #parse(@s) self end |
#to_h ⇒ Object
97 98 99 |
# File 'lib/alexa_modelbuilder.rb', line 97 def to_h() @h end |
#to_json(pretty: true, copy: false) ⇒ Object
Returns an Alexa Interaction Model in JSON format
218 219 220 221 222 223 224 225 |
# File 'lib/alexa_modelbuilder.rb', line 218 def to_json(pretty: true, copy: false) json = pretty ? JSON.pretty_generate(@interact_model) : \ @interact_model.to_json Clipboard.copy json if copy return json end |
#to_manifest(json: false) ⇒ Object
Returns a generated manifest from the document model
103 104 105 |
# File 'lib/alexa_modelbuilder.rb', line 103 def to_manifest(json: false) json ? JSON.pretty_generate(@manifest) : @manifest end |
#to_model(json: false) ⇒ Object
Returns an Alexa Interaction model as a Hash object
213 214 215 |
# File 'lib/alexa_modelbuilder.rb', line 213 def to_model(json: false) json ? JSON.pretty_generate(@interact_model) : @interact_model end |
#to_s ⇒ Object
227 228 229 |
# File 'lib/alexa_modelbuilder.rb', line 227 def to_s() @s end |