class NluAdapter::Adapters::Lex

AWS Lex wrapper class

Public Class Methods

new(options = {}) click to toggle source

Constructor

# 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

Public Instance Methods

create_intent(intent) click to toggle source

Given an Intent object, create/update it in Lex

@param intent [Intent] Intent object @return [Intent] Intent object

@todo convert response -> Intent

# 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) click to toggle source

Given an IntentCollection object, create it in Lex

@param collection [IntentCollection] the Bot to be created

# 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) click to toggle source

Get an instance of Intent, if it exists else nil

@param name [String] name of the intent @param version [String] version of the intent @return [Intent] intent object

# 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) click to toggle source

Get an instance of IntentCollection, if it exists @param name [String] intent collection name @param version [String] version

# 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= []) click to toggle source

Get a new instance of Intent

@param name [String] name of the intent @param utterences [Array] phrases for training @return [Intent] Intent object

# 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) click to toggle source

Get a new instance of Intent @param name [String] intent collection name @param intents [Array<Intent>] array of intent objects

# 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) click to toggle source

Understand a given text

@param text [String] a text to parse using the NLU provider @return [Json] return the identified intent name

# 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