はじめに
機械学習の勉強として、「基礎から学ぶ 人工知能の教科書」 を読んでいます。
この本の特徴は、章末問題にPythonの簡単なプログラムが載っていてるところです。
ここでは、それをRubyにて写経しています。
第4章 章末問題
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 "「AはBですか?」という質問を扱います。AとBを入力してください"
print "Aを入力:"
a = gets.chomp
print "Bを入力:"
b = gets.chomp
puts "質問:「#{a}は#{b}ですか?」"
puts "推論を開始します"
if semnet[a].nil?
puts "「#{a}」がわかりません"
next
end
obj = a
while obj != b
puts "#{obj}は#{semnet[obj]}です"
if semnet[obj] == b
puts "結論:#{a}は#{b}です"
break
end
unless semnet.keys.include?(semnet[obj])
puts "結論:#{a}は#{b}ではありません"
break
end
obj = semnet[obj]
end
puts "推論終了"
end
意味ネットワーク(semantic network)におけるリンクの一つであるis-aリンクを用いた推論を実装する問題です。
pythonは日本語入力でもうまくいくのですが、rubyではうまくいかなかったので英単語に変更しています。
「AはBですか?」という質問を扱います。AとBを入力してください
Aを入力:smartphone
Bを入力:computer
質問:「smartphoneはcomputerですか?」
推論を開始します
smartphoneはportable terminalです
portable terminalはcomputerです
結論:smartphoneはcomputerです
推論終了
まとめ
- 第4章まで読んだ