Sha256: 56ec621012aeb814ea293d6f6fa83121fd7001c11133d73014fa83c53d4fa38a

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

require 'charyf' # TODO require 'charyf/engine/intent/processors/processor'

require 'wit'

module WitAI
  module Charyf
    class Processor < ::Charyf::Engine::Intent::Processor::Base

      class NoKeyProvided < StandardError; end

      strategy_name :wit

      def self.get_for(skill = nil)
        return self.new
      end

      def initialize
        raise NoKeyProvided.new('WitAI::Charyf.api_key has not been set') if !WitAI::Charyf.api_key || WitAI::Charyf.api_key.empty?
      end

      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

          data = intent["value"].split(":")
          if data.size < 2
            return unknown
          end

          action = data.pop
          skill = data.delete_at(0)
          controller = (data.size > 0 ? data : ["Base"]).join("::")
          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(skill, controller, action, confidence, matches)
        end

        unknown
      rescue Wit::Error => error
        return unknown
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
witai-charyf-0.1.0 lib/witai/charyf/processor.rb