lab18: impl
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
#! /usr/bin/ruby -w
|
||||
#! /usr/bin/env ruby -w
|
||||
|
||||
#Represents a Rogerian psychiatrist
|
||||
class Shrink
|
||||
@@ -7,6 +7,7 @@ class Shrink
|
||||
def initialize()
|
||||
@he="he"
|
||||
@she="she"
|
||||
@they="they"
|
||||
end
|
||||
|
||||
#read a statement and convert it to a psychiatric response.
|
||||
@@ -14,7 +15,10 @@ class Shrink
|
||||
#downcase for ease of substitution
|
||||
blather = blather.downcase
|
||||
|
||||
#change 'you', 'your', etc. to uppercase 'I', 'MY'
|
||||
# filter out words like "well" or "perhaps" from the beginning
|
||||
blather.gsub!(/^(well|perhaps|anyway|actually),?\s+/i, '')
|
||||
|
||||
# change 'you', 'your', etc. to uppercase 'I', 'MY'
|
||||
blather.gsub!(/\byour\b/,"MY")
|
||||
blather.gsub!(/\byou\b/,'I')
|
||||
|
||||
@@ -23,21 +27,45 @@ class Shrink
|
||||
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.*/
|
||||
theyPat = /.*\b(your (parents|friends|siblings|colleagues|classmates))\b.*/
|
||||
@he=blather.sub(hePat, '\1').chomp if blather =~ hePat
|
||||
@she=blather.sub(shePat, '\1').chomp if blather =~ shePat
|
||||
@they=blather.sub(theyPat, '\1').chomp if blather =~ theyPat
|
||||
|
||||
# handle "always" and "never" responses
|
||||
if blather =~ /\b(always|never)\b/
|
||||
return "CAN YOU BE MORE SPECIFIC?"
|
||||
end
|
||||
|
||||
# handle "are you" questions
|
||||
if blather =~ /^are I (.+?)\?*$/
|
||||
return "IS IT IMPORTANT IF I AM #{$1.upcase}?"
|
||||
end
|
||||
|
||||
#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)
|
||||
blather.sub!(/\bthey\b/, @they)
|
||||
|
||||
#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')
|
||||
|
||||
# add some empathy for sad words
|
||||
if blather =~ /\b(sad|depressed|upset|hurt|angry|frustrated)\b/
|
||||
empathetic_responses = [
|
||||
"THAT SOUNDS DIFFICULT. ",
|
||||
"I CAN HEAR THE PAIN IN YOUR WORDS. ",
|
||||
"THAT MUST BE HARD FOR YOU. "
|
||||
]
|
||||
prefix = empathetic_responses.sample
|
||||
return prefix + "TELL ME MORE ABOUT " + blather.upcase + "?"
|
||||
end
|
||||
|
||||
#results are uppercased, for aesthetics.
|
||||
return blather.upcase + "?"
|
||||
end
|
||||
@@ -48,15 +76,35 @@ end
|
||||
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",
|
||||
"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?",
|
||||
# test "always" response
|
||||
"I always feel nervous",
|
||||
# test "never" response
|
||||
"My parents never understand me",
|
||||
# test "Are you" questions
|
||||
"Are you listening to me?",
|
||||
"Are you a real doctor?",
|
||||
# test word filtering
|
||||
"Perhaps my friends don't like me",
|
||||
"Well, I suppose that makes sense",
|
||||
"Actually, I think I'm getting better",
|
||||
# test "they" memory
|
||||
"My colleagues are very competitive",
|
||||
"They make me feel inadequate",
|
||||
"Sometimes they ignore my ideas",
|
||||
# Test empathetic responses
|
||||
"I am feeling very sad today",
|
||||
"My boss makes me frustrated",
|
||||
"I feel so depressed lately",
|
||||
].each do |stmt|
|
||||
puts stmt
|
||||
puts eliza.generateResponse(stmt)
|
||||
puts
|
||||
end
|
||||
else
|
||||
while line = gets
|
||||
|
||||
Reference in New Issue
Block a user