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.



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

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



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

def build_manifest(h)    
  
  manifest = {
      "vendorId" => '',
      "manifest" => {
          "publishingInformation" => {
              "locales" => {
              },
              "isAvailableWorldwide" => false,
              "testingInstructions" => "1) Say 'Alexa, say something'",
              "category" => "SMART_HOME",
              "distributionCountries" => [
              ]
          },
          "apis" => {
              "custom" => {
                  "endpoint" => {
                      "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

#read(s) ⇒ Object



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

def read(s)
  
  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 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



82
83
84
# File 'lib/alexa_modelbuilder.rb', line 82

def to_h()
  @h
end

#to_json(pretty: true) ⇒ Object



190
191
192
# File 'lib/alexa_modelbuilder.rb', line 190

def to_json(pretty: true)
  pretty ? JSON.pretty_generate(@interact_model) : @interact_model.to_json
end

#to_manifest(json: false) ⇒ Object



86
87
88
# File 'lib/alexa_modelbuilder.rb', line 86

def to_manifest(json: false)
  json ? JSON.pretty_generate(@manifest) : @manifest
end

#to_model(json: false) ⇒ Object



186
187
188
# File 'lib/alexa_modelbuilder.rb', line 186

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

#to_sObject



194
195
196
# File 'lib/alexa_modelbuilder.rb', line 194

def to_s()
  @s
end