Class: AlexaGenerator::InteractionModel

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

Defined Under Namespace

Classes: Builder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(intents, utterance_templates, slot_bindings) ⇒ InteractionModel

Returns a new instance of InteractionModel.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/alexa_generator/interaction_model.rb', line 30

def initialize(intents, utterance_templates, slot_bindings)
  # Validate that utterance templates reference only defined slots
  all_referenced_slots = utterance_templates.map(&:referenced_slots).flatten
  slot_names = Set.new(intents.map(&:slots).flatten.map(&:name))
  undefined_slots = all_referenced_slots.reject { |x| slot_names.include?(x) }

  if undefined_slots.any?
    raise AlexaSyntaxError,
          "The following slots referenced in utterances are undefined: #{undefined_slots.join ','}"
  end

  @intents = Hash[ intents.map {|x| [x.name, x]} ]

  @utterance_templates = utterance_templates.group_by { |x| x.intent_name }
  @slot_bindings = slot_bindings.group_by { |x| x.slot_name }
end

Instance Attribute Details

#intentsObject (readonly)

Returns the value of attribute intents.



9
10
11
# File 'lib/alexa_generator/interaction_model.rb', line 9

def intents
  @intents
end

Class Method Details

.build(&block) ⇒ Object



146
147
148
149
150
# File 'lib/alexa_generator/interaction_model.rb', line 146

def self.build(&block)
  builder = Builder.new
  block.call(builder)
  builder.create
end

Instance Method Details

#collect_slot_typesObject



136
137
138
139
140
141
142
143
144
# File 'lib/alexa_generator/interaction_model.rb', line 136

def collect_slot_types
  out = {}
  @intents.values.each do |intent|
    intent.slots.map do |slot|
      out[slot.name.to_sym] = slot.type.to_s
    end
  end
  out
end

#custom_slot_typesObject



128
129
130
131
132
133
134
# File 'lib/alexa_generator/interaction_model.rb', line 128

def custom_slot_types
  slots = collect_slot_types
  custom_types = slots.values.select do |x|
    AlexaGenerator::Slot::SlotType.custom?(x)
  end
  Set.new(custom_types).to_a
end

#intent_schemaObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/alexa_generator/interaction_model.rb', line 47

def intent_schema
  intents = []

  @intents.values.each do |intent|
    if intent.slots.any?
      slots = []

      intent.slots.each do |slot|
        slots << {name: slot.name, type: slot.type}
      end

      intents << {intent: intent.name, slots: slots}
    else
      intents << {intent: intent.name}
    end
  end

  { intents: intents }
end

#sample_utterances(intent_name) ⇒ Object



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
# File 'lib/alexa_generator/interaction_model.rb', line 67

def sample_utterances(intent_name)
  templates = @utterance_templates[intent_name] || []
  slot_types = collect_slot_types
  utterances = Set.new

  templates.each do |template|
    # Consider only the slots that are referenced in this template
    relevant_slots = template.referenced_slots

    # Amazon wants only the LITERAL ones
    relevant_slots.select! do |s|
      AlexaGenerator::Slot::SlotType.literal?(slot_types[s.to_sym])
    end

    # Compute all possible value bindings for the relevant slots
    slot_values = relevant_slots.
        # Extract value bindings for each slot
        map { |slot| @slot_bindings[slot] }

    if slot_values.any?
      slot_value_combinations = slot_values.first

      if slot_values.count > 1
        remaining_values = slot_values[1..-1]
        slot_value_combinations = slot_value_combinations.product(*remaining_values)
      else
        slot_value_combinations = slot_value_combinations.map { |x| [x] }
      end

      slot_value_combinations.each do |value_binding|
        raw_template = template.template.dup

        value_binding.each do |binding|
          binding.bind_to_template!( raw_template )
        end

        utterances.add( raw_template )
      end
    # If there are no slot values, then just stuff the untouched template into utterances.
    else
      utterances.add( template.template )
    end
  end

  utterances.sort.map do |utterance|
    "#{intent_name} #{utterance}"
  end
end

#slot_type_values(slot_type) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/alexa_generator/interaction_model.rb', line 116

def slot_type_values(slot_type)
  bindings = Set.new
  @intents.values.each do |intent|
    intent.slots.each do |slot|
      if slot.type.to_s == slot_type.to_s
        bindings += slot.bindings
      end
    end
  end
  bindings.to_a
end