[RUBY] Learning from the basics Artificial intelligence textbook Chapter 4 Chapter end problems

Introduction

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.

Chapter 4 End of Chapter Problem

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.

is-a - wikipedia

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

Summary

Recommended Posts

Learning from the basics Artificial intelligence textbook Chapter 4 Chapter end problems
Learning from the basics Artificial intelligence textbook Chapter 2 Chapter end problems
Learning from the basics Artificial intelligence textbook Chapter 3 End-of-chapter problems
[Challenge CircleCI from 0] Learn the basics of CircleCI
[Ruby] From the basics to the inject method
Deep Learning Java from scratch Chapter 1 Introduction
Deep Learning Java from scratch Chapter 2 Perceptron