[Ruby] Learning from the basics Artificial intelligence textbook Chapter 2 End-of-chapter problem

1 minute read

Introduction

As a study of machine learning, I read “Learn from basics textbook of artificial intelligence””.

The feature of this book is that a simple program of Python is included in the chapter end problem.

Chapter 2 End of Chapter Problem

ELIZA.rb


LIMIT = 20
CYCLE = 5

count = 0
endcount = 0
puts('Dr> I'm Doctor, talk to you')
while endcount <LIMIT
  print('you>')
  inputline = gets.chomp.encode("UTF-8", "CP932", :invalid => :replace)
  if count >= CYCLE
    puts'Dr>' << inputline <<'?   '
    count = 0
  elsif inputline.include? ('teacher')
    puts'Dr> let's talk about you, not me'
  elsif inputline.include?('mother')
    puts'Dr> talk about your mom'
  elsif inputline.include? ('Father')
    puts'Dr> talk about your dad'
  elsif inputline.include? ('opinion')
    puts'Dr> Want to hear my opinion? '
  elsif inputline.include?('I'm worried')
    puts'Dr>' << inputline.sub(I'm worried about //, are you worried about'?)
  else
    puts'Dr> continue'
  end
  count += 1
  endcount += 1
end
puts('Dr> Let's finish it. Thank you.')

This is a problem to create a simplified version of ELIZA.

CP932.rb


  inputline = gets.chomp.encode("UTF-8", "CP932", :invalid => :replace)

In the case of Windows10, the process of converting the character code is required. With AtCoder, there is no such processing, so it is fresh. However, it is necessary to select the legacy console due to a bug in the command prompt. 20200709.png

Summary

  • I read up to Chapter 2
  • G test was difficult