Class: Adapt::Intent::Processor

Inherits:
Charyf::Engine::Intent::Processor::Base
  • Object
show all
Extended by:
PyCall::Import
Defined in:
lib/adapt/processor.rb

Defined Under Namespace

Classes: NameCollisionError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProcessor

Returns a new instance of Processor.



68
69
70
71
72
73
# File 'lib/adapt/processor.rb', line 68

def initialize
  self.class.setup
  @engine = self.class.engine

  @parser = self.class.parser
end

Class Method Details

._intentsObject



27
28
29
# File 'lib/adapt/processor.rb', line 27

def _intents
  @_intents ||= {}
end

.engineObject



17
18
19
20
21
22
23
24
25
# File 'lib/adapt/processor.rb', line 17

def engine
  unless @_python_loaded
    self.pyfrom 'adapt.intent', import: :IntentBuilder
    self.pyfrom 'adapt.engine', import: :IntentDeterminationEngine
    @_python_loaded = true
  end

  @_engine ||= IntentDeterminationEngine.new
end

.parserObject



38
39
40
41
42
43
44
# File 'lib/adapt/processor.rb', line 38

def parser
  return @_parser if @_parser_initialized

  @_parser = Adapt::Helpers::Parser.get(Adapt.config.locale)
  @_parser_initialized = true
  puts "No language helper for locale '#{Adapt.config.locale}' available" if @_parser.nil?
end

.setupObject



31
32
33
34
35
36
# File 'lib/adapt/processor.rb', line 31

def setup
  return if @_initialized
  @_initialized = true

  load_files
end

Instance Method Details

#define(&block) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/adapt/processor.rb', line 75

def define(&block)
  builder = Adapt::RoutingBuilder.new

  block.call(builder)

  intents = builder.build(@engine, IntentBuilder)

  intents.each do |intent|
    # TODO handle already existing
    raise NameCollisionError.new("Intent name '#{intent.name}' already in use.") if self.class._intents[intent.name]

    self.class._intents[intent.name] = intent
  end
end

#determine(request) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/adapt/processor.rb', line 90

def determine(request)
  adapt_intent = nil

  # Normalize text
  text = @parser ? @parser.normalize(request.text) : request.text

  generator = @engine.determine_intent(text)
  begin
    adapt_intent = PyCall.builtins.next(generator)
  rescue PyCall::PyError
    # ignored
  end

  return unknown unless adapt_intent

  confidence = adapt_intent['confidence']
  app_intent = self.class._intents[adapt_intent['intent_type']]


  entities = app_intent.entities.map { |e| e.keys.first }.inject({}) do |h, entity|
    h[entity] = {
        value: adapt_intent[entity]
    }

    h
  end

  return Charyf::Engine::Intent.new(app_intent.name, confidence, entities)
end