From d2bbb8d14a9b0872cdf569795e8b14f251aa3393 Mon Sep 17 00:00:00 2001 From: Yuri Tatishchev Date: Tue, 28 Apr 2026 06:24:23 -0700 Subject: [PATCH] lab18: init --- lab18/eliza.rb | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 lab18/eliza.rb diff --git a/lab18/eliza.rb b/lab18/eliza.rb new file mode 100644 index 0000000..56c8f04 --- /dev/null +++ b/lab18/eliza.rb @@ -0,0 +1,66 @@ +#! /usr/bin/ruby -w + +#Represents a Rogerian psychiatrist +class Shrink + + #initializes 'memory' of Eliza. + def initialize() + @he="he" + @she="she" + end + + #read a statement and convert it to a psychiatric response. + def generateResponse(blather) + #downcase for ease of substitution + blather = blather.downcase + + #change 'you', 'your', etc. to uppercase 'I', 'MY' + blather.gsub!(/\byour\b/,"MY") + blather.gsub!(/\byou\b/,'I') + + #Replace 'my' with 'your', 'me' with 'you', 'I' with 'you', etc. + blather.gsub!(/\bmy\b/,"your") + blather.gsub!(/\bme\b/,"you") + blather.gsub!(/\bi\b/,'you') + + #Sub in past references, but only for the 1st occurrence or it looks weird + blather.sub!(/\b(he|him)\b/, @he) + blather.sub!(/\b(she|her)\b/, @she) + + #Get future references -- note that these do NOT change the immediate output + hePat=/.*\b(your (father|brother|(ex-?)?(husband|boyfriend)))\b.*/ + shePat = /.*\b(your (mother|sister|(ex-?)?(wife|girlfriend)))\b.*/ + @he=blather.sub(hePat, '\1').chomp if blather =~ hePat + @she=blather.sub(shePat, '\1').chomp if blather =~ shePat + + #Deal with name + namePat=/.*\byour name is (\w+).*/ + @name=blather.sub(namePat,'\1') + blather.sub!(namePat,'nice to meet you, \1. How can I help you') + + #results are uppercased, for aesthetics. + return blather.upcase + "?" + end +end + + +#main -- reads from standard input unless -test is the first parameter. +eliza = Shrink.new() +if ARGV[0] == "-test" + ['My girlfriend never listens to me', + "I think she might be deaf", + "yes", + "I am afraid of clowns", + "Well, they just seem creepy", + "Also, when I was a kid, a clown killed my dad", + "Are you a clown in disguise?", + ].each do |stmt| + puts stmt + puts eliza.generateResponse(stmt) + end +else + while line = gets + response = eliza.generateResponse line + puts response + end +end