Class: WitAI::Charyf::Processor

Inherits:
Charyf::Engine::Intent::Processor::Base
  • Object
show all
Defined in:
lib/witai/charyf/processor.rb

Defined Under Namespace

Classes: NoKeyProvided

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProcessor

Returns a new instance of Processor.

Raises:



17
18
19
# 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

Class Method Details

.get_for(skill = nil) ⇒ Object



13
14
15
# File 'lib/witai/charyf/processor.rb', line 13

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

Instance Method Details

#determine(request) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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