class Knowledge

Constants

PODS

Public Instance Methods

initialize_operator() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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