Class: Kogno::Nlp

Inherits:
Object
  • Object
show all
Defined in:
lib/core/lib/nlp.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(phrase = nil, locale = :en, context_reference = nil) ⇒ Nlp

Returns a new instance of Nlp.



7
8
9
10
11
12
13
# File 'lib/core/lib/nlp.rb', line 7

def initialize(phrase=nil,locale=:en, context_reference=nil)
  self.wit = Wit.new(access_token: Nlp.get_wit_token(locale), api_version: Kogno::Application.config.nlp.wit[:api_version])
  self.processed = false
  self.phrase = phrase
  self.context_reference = context_reference
  self.entities = {}
end

Instance Attribute Details

#context_referenceObject

Returns the value of attribute context_reference.



5
6
7
# File 'lib/core/lib/nlp.rb', line 5

def context_reference
  @context_reference
end

#entitiesObject

Returns the value of attribute entities.



5
6
7
# File 'lib/core/lib/nlp.rb', line 5

def entities
  @entities
end

#intentsObject

Returns the value of attribute intents.



5
6
7
# File 'lib/core/lib/nlp.rb', line 5

def intents
  @intents
end

#phraseObject

Returns the value of attribute phrase.



5
6
7
# File 'lib/core/lib/nlp.rb', line 5

def phrase
  @phrase
end

#processedObject

Returns the value of attribute processed.



5
6
7
# File 'lib/core/lib/nlp.rb', line 5

def processed
  @processed
end

#traitsObject

Returns the value of attribute traits.



5
6
7
# File 'lib/core/lib/nlp.rb', line 5

def traits
  @traits
end

#witObject

Returns the value of attribute wit.



5
6
7
# File 'lib/core/lib/nlp.rb', line 5

def wit
  @wit
end

Class Method Details

.get_wit_token(locale) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/core/lib/nlp.rb', line 15

def self.get_wit_token(locale)
  tokens = Kogno::Application.config.nlp.wit[:apps]
  if !locale.nil? and !tokens[locale.to_sym].nil?
    return tokens[locale.to_sym]
  else # Hash      
    return tokens[:default]
  end
end

.most_confidence(elements) ⇒ Object



177
178
179
180
181
182
183
# File 'lib/core/lib/nlp.rb', line 177

def self.most_confidence(elements)
  if elements.class == Array
    return elements.sort_by{|element| element["confidence"]}.first
  else
    return false
  end
end

Instance Method Details

#add_pre_processed_entitiesObject



185
186
187
188
# File 'lib/core/lib/nlp.rb', line 185

def add_pre_processed_entities    
  self.entities[:datetime_range] = self.datetime_range if self.datetime_range?
  self.entities[:datetime] = self.entities[:"wit$datetime:datetime"].map{|v| v[:value] || v[:from][:value] rescue nil} unless self.entities[:"wit$datetime:datetime"].nil?
end

#datesObject



116
117
118
# File 'lib/core/lib/nlp.rb', line 116

def dates
  self.entities[:datetime].map{|d| d[:values][0][:value].to_date} rescue []
end

#datetime_rangeObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/core/lib/nlp.rb', line 120

def datetime_range

  params = self.entities[:"wit$datetime:datetime"] rescue []

  dates = {
    :from => nil,
    :to => nil
  }

  # case (params.count rescue 0)
  count = params.count rescue 0

  # when 1
  if count == 1
    if params[0][:value].nil?
      dates[:from] = (params[0][:values][0][:from][:value].to_date rescue nil)
      dates[:to] = (params[0][:values][0][:to][:value].to_date rescue nil)
      dates[:to] = dates[:to] - 1.day unless dates[:to].nil? #esto es por que el git al traer rango trae un dia mas
    else
      dates[:from] = (params[0][:value].to_date rescue nil)
      unless self.entities[:duration].nil?
        dates[:to] = get_return_date_from_duration(dates[:from]) unless dates[:from].nil?
      end
    end
    # when 2
  elsif count > 1
    dates[:from] = (params[0][:value].to_date rescue nil)
    dates[:to] = (params[1][:value].to_date rescue nil)
  end

  if ((dates[:from] > dates[:to]) rescue false) #solo para asegurar que la fecha de salida sea anterior a la de llegada
    from_tmp = dates[:from]
    dates[:from] = dates[:to]
    dates[:to] = from_tmp
  end

  return (dates)
end

#datetime_range?Boolean

Returns:

  • (Boolean)


159
160
161
162
# File 'lib/core/lib/nlp.rb', line 159

def datetime_range?
  datetime_range = self.datetime_range
  !datetime_range[:from].nil? && !datetime_range[:to].nil?
end

#duration_in_daysObject



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/core/lib/nlp.rb', line 165

def duration_in_days

  multiplier = {"day"=>1,"week"=>7,"month"=>31}
  duration = (self.entities[:duration][0] rescue nil)
  unless duration.nil?
    return(duration[:value]*multiplier[duration[:unit]] rescue 0)
  else
    return nil
  end

end

#empty_entities?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/core/lib/nlp.rb', line 76

def empty_entities?
  self.processed and self.entities.empty?
end

#expressionObject



99
100
101
# File 'lib/core/lib/nlp.rb', line 99

def expression
  self.entities[:expression].first[:value] rescue nil
end

#intent(name_only = true) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/core/lib/nlp.rb', line 80

def intent(name_only=true)
  final_intent = nil
  intents = (self.intents rescue nil)
  unless intents.nil?
    intents.each do |intent|
      if final_intent.nil?
        final_intent = intent
      elsif intent[:confidence] > final_intent[:confidence]
        final_intent = intent
      end
    end
  end
  if final_intent.nil?
    return nil
  else
    return name_only ? final_intent[:name] : final_intent
  end
end

#local_search_queryObject



111
112
113
# File 'lib/core/lib/nlp.rb', line 111

def local_search_query
  self.entities[:local_search_query] rescue nil
end

#locationObject



103
104
105
# File 'lib/core/lib/nlp.rb', line 103

def location
  self.entities[:location] rescue []
end

#process_phrase(phrase = nil, context_reference = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/core/lib/nlp.rb', line 28

def process_phrase(phrase=nil, context_reference=nil)
  if !Kogno::Application.config.nlp.wit[:enable]
    logger.write "NLP Service is not enabled in config/initializers/nlp.rb", :red
    return false
  end

  wit_tries = 0

  phrase = phrase.nil? ? self.phrase : phrase
  context_reference = context_reference.nil? ? self.context_reference : context_reference

  self.processed = true
  if phrase.nil? or phrase.empty?
    {}
  elsif phrase.length > 256
    {}  
  else

    phrase = Spelling::correction(phrase)
    # logger.write "-----", :red
    # logger.write phrase, :red

    wit_output = nil
    loop do
      wit_output = self.wit.message_with_context(phrase, context_reference) rescue :error
      break if wit_output != :error || wit_tries == 3
      logger.debug "Wit error!!!", :red
      wit_tries+=1
      sleep(1)
    end

    self.intents = wit_output["intents"].deep_symbolize_keys! rescue {}
    self.entities = wit_output["entities"].deep_symbolize_keys! rescue {}
    self.traits = wit_output["traits"].deep_symbolize_keys! rescue {}

    logger.debug "  NLP RESOLVED VALUES => #{wit_output}", :light_blue

    self.add_pre_processed_entities()
  end

end

#process_phrase_onceObject



70
71
72
73
74
# File 'lib/core/lib/nlp.rb', line 70

def process_phrase_once
  unless self.processed
    self.process_phrase
  end
end

#search_queryObject



107
108
109
# File 'lib/core/lib/nlp.rb', line 107

def search_query
  self.entities[:search_query] rescue nil
end

#set_context_reference(context_reference) ⇒ Object



24
25
26
# File 'lib/core/lib/nlp.rb', line 24

def set_context_reference(context_reference)
  self.context_reference = context_reference
end