Learning from the basics Artificial intelligence textbook Chapter 2 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.

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>Do you want to hear my opinion?'
  elsif inputline.include?('I'm worried')
    puts 'Dr>' << inputline.sub(/I'm worried/, 'Are you worried?')
  else
    puts 'Dr>Please continue'
  end
  count += 1
  endcount += 1
end
puts('Dr>Let's finish it now. Thank you for your support.')

This is a problem of creating 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. ʻAtCoder` is fresh because there is no such processing. However, it is necessary to select the legacy console due to a bug in the command prompt. 20200709.png

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