Class: AlexaGenerator::InteractionModel
- Inherits:
-
Object
- Object
- AlexaGenerator::InteractionModel
- Defined in:
- lib/alexa_generator/interaction_model.rb
Defined Under Namespace
Classes: Builder
Instance Attribute Summary collapse
-
#intents ⇒ Object
readonly
Returns the value of attribute intents.
Class Method Summary collapse
Instance Method Summary collapse
- #collect_slot_types ⇒ Object
-
#initialize(intents, utterance_templates, slot_bindings) ⇒ InteractionModel
constructor
A new instance of InteractionModel.
- #intent_schema ⇒ Object
- #sample_utterances(intent_name) ⇒ Object
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(slot_bindings.map(&:slot_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
#intents ⇒ Object (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
Instance Method Details
#collect_slot_types ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/alexa_generator/interaction_model.rb', line 115 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 |
#intent_schema ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/alexa_generator/interaction_model.rb', line 47 def intent_schema intents = [] @intents.values.each do |intent| slots = [] intent.slots.each do |slot| slots << {name: slot.name, type: slot.type} end intents << {intent: intent.name, slots: slots} end { intents: intents } end |
#sample_utterances(intent_name) ⇒ Object
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/alexa_generator/interaction_model.rb', line 63 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 # puts value_binding.inspect value_binding.each do |binding| # puts "----> #{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 |