As a study of machine learning, I am reading "* Learning from the basics: Artificial intelligence textbook *".
The feature of this book is that the end-of-chapter problem contains a simple program of Python.
Here, it is copied with Ruby.
isa.rb
semnet = {"portable terminal"=>"computer",
          "laptop PC"=>"computer",
          "stationary PC"=>"computer",
          "smartphone"=>"portable terminal",
          "tablet"=>"portable terminal",
          "desktop PC"=>"stationary PC",
          "server PC"=>"stationary PC"}
while true
  puts "Handles the question "Is A a B?" Please enter A and B"
  print "Enter A:"
  a = gets.chomp
  print "Enter B:"
  b = gets.chomp
  puts "Question: "#{a}Is#{b}Is it? ""
  puts "Start inference"
  if semnet[a].nil?
    puts "「#{a}"I do not know"
    next
  end
  obj = a
  while obj != b
    puts "#{obj}Is#{semnet[obj]}is"
    if semnet[obj] == b
      puts "Conclusion:#{a}Is#{b}is"
      break
    end
    unless semnet.keys.include?(semnet[obj])
      puts "Conclusion:#{a}Is#{b}でIsありません"
      break
    end
    obj = semnet[obj]
  end
  puts "Inference end"
end
It is a problem to implement inference using ʻis-a link` which is one of the links in the semantic network.
python works well with Japanese input, but it didn't work with ruby, so I changed it to English words.
Handles the question "Is A a B?" Please enter A and B
Enter A: smartphone
Enter B: computer
Question: "Is your smartphone a computer?"
Start inference
smartphone is a portable terminal
portable terminal is computer
Conclusion: smartphone is computer
Inference end
Recommended Posts