eliza.pl

#!perl
# Mini Eliza

print "Hello, how are you? \n";
my $text=<STDIN>;
chomp($text);

while($text=~/.+/){
   $text =~ s/\bI('m| am)\b/you are/gi;
   $text =~ s/\bmy\b/your/gi;
   $text =~ s/\bme\b/you/gi;
   $text =~ s/\bmine\b /yours/gi;
   $text =~ s/(.*).$/$1/;
   print "MY_ELIZA: $text?\n";
   if($text =~ /.*you are (depressed|sad).*/) {
      my $random = int(rand(10));
      if($random < 5){
         $text =~ s/.*you are (depressed|sad).*/MY_ELIZA: I am sorry to hear you are $1./i;
   	  } else {
   	     $text =~ s/.*you are (depressed|sad).*/MY_ELIZA: What makes you feel $1?/i;
   	  }
   } else {	
   	  $text =~ s/.*you are (.*)/MY_ELIZA: Why do you think you are $1\?/i;
   }
   $text =~ s/.*all.*/MY_ELIZA: In what way\?/i;
   $text =~ s/.*always.*/MY_ELIZA: Can you think of a specific example\?/i;
   if($text !~ /^MY_ELIZA/){
      $text= "MY_ELIZA: Please tell me more.";
   }	
   print "$text\n\n";
   $text=<STDIN>;
   chomp($text);
}
