Class: AlexaModelBuilder

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

Instance Method Summary collapse

Constructor Details

#initialize(s = nil, debug: false, locale: 'en-GB') ⇒ AlexaModelBuilder

Returns a new instance of AlexaModelBuilder.



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

def initialize(s=nil, debug: false, locale: 'en-GB')

  @debug, @locale = debug, locale
  parse(s) if s

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



125
126
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
# File 'lib/alexa_modelbuilder.rb', line 125

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



24
25
26
# File 'lib/alexa_modelbuilder.rb', line 24

def intents()
  self.to_h[:intent].map {|x| x.keys.first}
end

#read(obj) ⇒ Object

Read an Alexa Interaction Model (in JSON format)



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
# File 'lib/alexa_modelbuilder.rb', line 30

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



95
96
97
# File 'lib/alexa_modelbuilder.rb', line 95

def to_h()
  @h
end

#to_json(pretty: true, copy: false) ⇒ Object

Returns an Alexa Interaction Model in JSON format



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

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



101
102
103
# File 'lib/alexa_modelbuilder.rb', line 101

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



207
208
209
# File 'lib/alexa_modelbuilder.rb', line 207

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

#to_sObject



221
222
223
# File 'lib/alexa_modelbuilder.rb', line 221

def to_s()
  @s
end