Class: AlexaModelBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/alexa_modelbuilder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#invocationObject (readonly)

Returns the value of attribute invocation.



15
16
17
# File 'lib/alexa_modelbuilder.rb', line 15

def invocation
  @invocation
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
# 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]
  
  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

#intentsObject

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_hObject



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



214
215
216
217
218
219
220
221
# File 'lib/alexa_modelbuilder.rb', line 214

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



209
210
211
# File 'lib/alexa_modelbuilder.rb', line 209

def to_model(json: false)
  json ? JSON.pretty_generate(@interact_model) : @interact_model
end

#to_sObject



223
224
225
# File 'lib/alexa_modelbuilder.rb', line 223

def to_s()
  @s
end