Class: Aws::Lex::Conversation::Simulator

Inherits:
Object
  • Object
show all
Defined in:
lib/aws/lex/conversation/simulator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Simulator

Returns a new instance of Simulator.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/aws/lex/conversation/simulator.rb', line 9

def initialize(opts = {})
  self.lex = opts.fetch(:lex) do
    Type::Event.shrink_wrap(
      bot: default_bot,
      inputMode: 'Text',
      inputTranscript: '',
      interpretations: [],
      invocationSource: 'DialogCodeHook',
      messageVersion: '1.0',
      requestAttributes: {},
      responseContentType: 'text/plain; charset=utf-8',
      sessionId: '1234567890000',
      sessionState: default_session_state
    )
  end
  interpretation(name: 'SIMULATION')
end

Instance Attribute Details

#lexObject

Returns the value of attribute lex.



7
8
9
# File 'lib/aws/lex/conversation/simulator.rb', line 7

def lex
  @lex
end

Instance Method Details

#bot(opts = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/aws/lex/conversation/simulator.rb', line 31

def bot(opts = {})
  changes = {
    aliasName: opts[:alias_name],
    aliasId: opts[:alias_id],
    name: opts[:name],
    version: opts[:version],
    localeId: opts[:locale_id],
    id: opts[:id]
  }.compact
  lex.bot = Type::Bot.shrink_wrap(lex.bot.to_lex.merge(changes))
  self
end

#context(opts = {}) ⇒ Object

rubocop:enable Metrics/AbcSize



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/aws/lex/conversation/simulator.rb', line 82

def context(opts = {})
  data = {
    name: opts.fetch(:name),
    contextAttributes: opts.fetch(:context_attributes) { {} },
    timeToLive: {
      timeToLiveInSeconds: opts.fetch(:seconds) { 600 },
      turnsToLive: opts.fetch(:turns) { 100 }
    }
  }
  context = Type::Context.shrink_wrap(data)
  lex.session_state.active_contexts.delete_if { |c| c.name == context.name }
  lex.session_state.active_contexts << context
  self
end

#eventObject



27
28
29
# File 'lib/aws/lex/conversation/simulator.rb', line 27

def event
  lex.to_lex
end

#input_mode(mode) ⇒ Object



118
119
120
121
# File 'lib/aws/lex/conversation/simulator.rb', line 118

def input_mode(mode)
  lex.input_mode = Type::InputMode.new(mode)
  self
end

#intent(opts = {}) ⇒ Object



49
50
51
52
53
54
# File 'lib/aws/lex/conversation/simulator.rb', line 49

def intent(opts = {})
  data = default_intent(opts)
  intent = Type::Intent.shrink_wrap(data)
  lex.session_state.intent = intent
  interpretation(data)
end

#interpretation(opts = {}) ⇒ Object

rubocop:disable Metrics/AbcSize



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/aws/lex/conversation/simulator.rb', line 57

def interpretation(opts = {})
  name = opts.fetch(:name)
  slots = opts.fetch(:slots) { {} }
  sentiment_score = opts.dig(:sentiment_response, :sentiment_score)
  sentiment = opts.dig(:sentiment_response, :sentiment)
  sentiment_response = opts[:sentiment_response] && {
    sentiment: sentiment,
    sentimentScore: sentiment_score
  }
  data = {
    intent: default_intent(opts),
    sentimentResponse: sentiment_response,
    nluConfidence: opts[:nlu_confidence]
  }.compact
  lex.interpretations.delete_if { |i| i.intent.name == name }
  lex.interpretations << Type::Interpretation.shrink_wrap(data)
  slots.each do |key, value|
    slot_data = { name: key }.merge(value)
    slot(slot_data)
  end
  reset_computed_properties!
  self
end

#invocation_source(source) ⇒ Object



113
114
115
116
# File 'lib/aws/lex/conversation/simulator.rb', line 113

def invocation_source(source)
  lex.invocation_source = Type::InvocationSource.new(source)
  self
end

#proposed_next_state(opts = {}) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/aws/lex/conversation/simulator.rb', line 123

def proposed_next_state(opts = {})
  data = {
    dialogAction: lex_attributes(opts.fetch(:dialog_action)),
    intent: lex_attributes(opts.fetch(:intent))
  }
  lex.proposed_next_state = Type::ProposedNextState.shrink_wrap(data)
  self
end

#session(data) ⇒ Object



132
133
134
135
# File 'lib/aws/lex/conversation/simulator.rb', line 132

def session(data)
  lex.session_state.session_attributes.merge!(Type::SessionAttributes[data])
  self
end

#slot(opts = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/aws/lex/conversation/simulator.rb', line 97

def slot(opts = {})
  name = opts.fetch(:name).to_sym
  raw_slots = {
    shape: opts.fetch(:shape) { 'Scalar' },
    value: {
      originalValue: opts.fetch(:original_value) { opts.fetch(:value) },
      resolvedValues: opts.fetch(:resolved_values) { [opts.fetch(:value)] },
      interpretedValue: opts.fetch(:interpreted_value) { opts.fetch(:value) }
    }
  }
  lex.session_state.intent.raw_slots[name] = raw_slots
  current_interpretation.intent.raw_slots[name] = raw_slots
  reset_computed_properties!
  self
end

#transcript(message) ⇒ Object



44
45
46
47
# File 'lib/aws/lex/conversation/simulator.rb', line 44

def transcript(message)
  lex.input_transcript = message
  self
end

#transcription(opts = {}) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/aws/lex/conversation/simulator.rb', line 137

def transcription(opts = {})
  data = {
    transcription: opts.fetch(:transcription),
    transcriptionConfidence: opts.fetch(:confidence, 1),
    resolvedContext: {
      intent: opts.fetch(:intent) { 'FallbackIntent' }
    },
    resolvedSlots: opts.fetch(:resolved_slots) { {} }
  }
  lex.transcriptions.push(Type::Transcription.shrink_wrap(data))
  self
end