class WitAI::Charyf::Processor

Public Class Methods

get_for(skill = nil) click to toggle source
# File lib/witai/charyf/processor.rb, line 13
def self.get_for(skill = nil)
  return self.new
end
new() click to toggle source
# File lib/witai/charyf/processor.rb, line 17
def initialize
  raise NoKeyProvided.new('WitAI::Charyf.api_key has not been set') if !WitAI::Charyf.api_key || WitAI::Charyf.api_key.empty?
end

Public Instance Methods

determine(request) click to toggle source
# File lib/witai/charyf/processor.rb, line 21
def determine(request)
  text = request.text

  client = Wit.new(access_token: WitAI::Charyf.api_key)

  res = client.message(text, 1)

  if (entities = res["entities"]) && res["entities"]["intent"] && res["entities"]["intent"].size > 0
    intent = entities.delete("intent").first

    if intent["value"].empty?
      return unknown
    end

    intent_name = intent["value"]
    confidence = intent["confidence"]

    matches = {}
    entities.keys.each do |entity_name|
      matches[entity_name] = entities[entity_name].map { |match| match.select { |k,v| ["value", "grain"].include? k } }
    end

    return ::Charyf::Engine::Intent.new(intent_name, confidence, matches)
  end

  unknown
rescue Wit::Error => error
  Charyf.error_handlers.handle_exception(error)

  return unknown
end