Class: NluAdapter::Adapters::Lex

Inherits:
Object
  • Object
show all
Includes:
ParseHelper
Defined in:
lib/nlu_adapter/lex.rb

Overview

AWS Lex wrapper class

Defined Under Namespace

Classes: Intent, IntentCollection

Instance Method Summary collapse

Methods included from ParseHelper

#bulk_parse, #parse_test, #parse_test_report

Constructor Details

#initialize(options = {}) ⇒ Lex

Constructor



11
12
13
14
15
16
17
18
# File 'lib/nlu_adapter/lex.rb', line 11

def initialize(options = {})
  #creds & region from config/env
  @bot_name = options[:bot_name]
  @bot_alias = options[:bot_alias]
  @user_id = options[:user_id]
  @lex_client = Aws::Lex::Client.new()
  @lexm_client = Aws::LexModelBuildingService::Client.new()
end

Instance Method Details

#create_intent(intent) ⇒ Intent

TODO:

convert response -> Intent

Given an Intent object, create/update it in Lex



93
94
95
# File 'lib/nlu_adapter/lex.rb', line 93

def create_intent(intent)
  resp = @lexm_client.put_intent(intent.to_h)
end

#create_intent_collection(collection) ⇒ Object

Given an IntentCollection object, create it in Lex



128
129
130
131
132
133
134
135
# File 'lib/nlu_adapter/lex.rb', line 128

def create_intent_collection(collection)
  #create/update intents
  collection.intents.each do |i|
    create_intent(i)
  end

  resp = @lexm_client.put_bot(collection.to_h)
end

#get_intent(name, version = nil) ⇒ Intent

Get an instance of Intent, if it exists else nil



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nlu_adapter/lex.rb', line 56

def get_intent(name, version = nil)
  version = '$LATEST'

  begin
    resp = @lexm_client.get_intent({name: name, version: version})
    return Intent.new(resp.to_h)

  rescue Aws::LexModelBuildingService::Errors::NotFoundException => e
    puts "Error: #{e.inspect}"
  end
  return nil
end

#get_intent_collection(name, version = nil) ⇒ Object

Get an instance of IntentCollection, if it exists



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/nlu_adapter/lex.rb', line 100

def get_intent_collection(name, version = nil)
  version = '$LATEST'
  begin
    resp = @lexm_client.get_bot({name: name, version_or_alias: version})

    return IntentCollection.new(resp.to_h)
  rescue Aws::LexModelBuildingService::Errors::NotFoundException => e
    puts "Error: #{e.inspect}"
  end
  return nil
end

#new_intent(name, utterences = []) ⇒ Intent

Get a new instance of Intent



75
76
77
78
79
80
81
82
83
84
# File 'lib/nlu_adapter/lex.rb', line 75

def new_intent(name, utterences= [])
  #check for existing intent, and get checksum

  i = get_intent(name)
  i = Intent.new if !i
  i.name = name
  i.utterences = utterences

  return i
end

#new_intent_collection(name, intents) ⇒ Object

Get a new instance of Intent



115
116
117
118
119
120
121
122
# File 'lib/nlu_adapter/lex.rb', line 115

def new_intent_collection(name, intents)
  ic = get_intent_collection(name)

  ic = IntentCollection.new if !ic
  ic.name = name
  ic.intents = intents
  return ic
end

#parse(text) ⇒ Json

Understand a given text



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nlu_adapter/lex.rb', line 25

def parse(text)
  intent_name = :NO_INTENT_FOUND
  begin
    resp = @lex_client.post_text({
      bot_name: @bot_name, # required
      bot_alias: @bot_alias, # required
      user_id: @user_id, # required
      session_attributes: {
        "String" => "String",
      },
      request_attributes: {
        "String" => "String",
      },
      input_text: text, # required
    })

    unless resp.intent_name.nil? || resp.intent_name.empty?
      intent_name = resp.intent_name  #=> String
    end
  rescue Aws::Lex::Errors::ServiceError => e
    puts "Error: #{e.inspect}"
  end
  return { intent_name: intent_name }
end