Class: Knowledge

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

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



7
8
9
10
11
# File 'lib/cogibara/operators/knowledge.rb', line 7

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)


13
14
15
16
# File 'lib/cogibara/operators/knowledge.rb', line 13

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

#process(query) ⇒ Object



18
19
20
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
52
53
54
# File 'lib/cogibara/operators/knowledge.rb', line 18

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

  if is_question_word?(word)
    result = Wolfram::HashPresenter.new(Wolfram.fetch(query.text)).to_hash
    if result[:pods]["Result"] || result[:pods]["Exact result"] || result[:pods]["Basic information"]
      valid_result = true
      if result[:pods]["Result"]
        msg = result[:pods]["Result"][0]
      elsif result[:pods]["Exact result"]
        msg = result[:pods]["Exact result"][0]
      elsif result[:pods]["Basic information"]
        msg = result[:pods]["Basic information"][0]
      end
    else
      valid_result == false
    end
    # valid_result = !(!result[:pods]["Result"] || result[:pods]["Result"][0] == "(data not available)")
  else
    valid_result = false
  end

  if valid_result
    if msg == "(data not available)"
      # msg = @cleverbot.write(query.text) + " (dk)"
      # # puts msg
      # msg
      nil
    else
      msg
    end
  else
    # @cleverbot.write(query.text)
    nil
  end
  # valid_result ? result[:pods]["Result"][0] : @cleverbot.write(query.text)
end