Class: Knowledge

Inherits:
Cogibara::OperatorBase show all
Defined in:
lib/cogibara/operators/knowledge.rb

Constant Summary collapse

PODS =
%w(Result Exact\ result Basic\ information Decimal\ approximation)

Instance Attribute Summary

Attributes inherited from Cogibara::OperatorBase

#clientID, #message_structure, #message_text, #operator_config

Instance Method Summary collapse

Methods inherited from Cogibara::OperatorBase

#confirm, #initialize, #process_file, #receive_message, #say

Constructor Details

This class inherits a constructor from Cogibara::OperatorBase

Instance Method Details

#initialize_operatorObject



9
10
11
12
13
# File 'lib/cogibara/operators/knowledge.rb', line 9

def initialize_operator
  Wolfram.appid = self.operator_config["WOLFRAM_KEY"]
  # @wolfram = WolframAlpha.new
  @cleverbot = Cleverbot::Client.new
end

#is_question_word?(word) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
# File 'lib/cogibara/operators/knowledge.rb', line 15

def is_question_word?(word)
  qwords = %w{who what why where when how are can if}
  qwords.include? word.downcase
end

#process(query) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cogibara/operators/knowledge.rb', line 20

def process(query)
  word = query.text.split[0]

  valid_result = false
  if is_question_word?(word)
    result = Wolfram::HashPresenter.new(Wolfram.fetch(query.text)).to_hash
    recognized = result[:pods].keys & PODS
    unless recognized.empty?
      valid_result = true
      msg = result[:pods][recognized[0]][0]
      if recognized.length > 1
        recognized.shift
        msg += ", " + recognized.map{|x| x + ": " + result[:pods][x][0].to_s}.join(", ")
      end
    end
  end

  valid_result ? msg : nil
end