I am creating a simple chatterbot program like ELIZA.
I'm taking questions from the terminal and sending reply with dialog, but my program takes only the first input and repeats.
For example, when I run my script the output may be something like this:
[Eliza]: Hi, I'm a psychotherapist. What is your name? user Input: hello my name is adam. [Eliza]: hello adam, how are you? [Eliza]: your name is adam [Eliza]: your name is adam [Eliza]: your name is adam [Eliza]: your name is adam [Eliza]: your name is adam And it repeats endlessly.
I don't know where I am doing wrong. How can I get my program to read the next line from the keyboard?
sub hello { print "[Eliza]: Hi, I'm a psychotherapist. What is your name? \n"; } sub getPatientName { my ($reply) = @_; my @responses = ( "my name is", "i'm", "i am", "my name's" ); foreach my $response ( @responses ) { if ( lc($reply) =~ /$response/ ) { return "$'"; } } return lc($reply); } sub makeQuestion { my ($patient) = @_; my %reflections = ( "am" => "are", "was" => "were", "i" => "you", "i'd" => "you would", "i've" => "you have", "i'll" => "you will", "my" => "your", "are" => "am", "you've"=> "I have", "you'll"=> "I will", "your" => "my", "yours" => "mine", "you" => "me", "me" => "you" ); if ( $count == 0 ) { $patientName = getPatientName($patient); $count += 1; print "Hello $patientName , How are you? \n"; } my @toBes = keys %reflections; foreach my $toBe (@toBes) { if ($patient =~/$toBe/) { $patient=~ s/$toBe/$reflections{$toBe}/i; print "$patient? \n"; } } } sub eliza { hello(); my $answer = <STDIN>; while ($answer) { chomp $answer; #remove . ! ; $answer =~ s/[.!,;]/ /; makeQuestion($answer); } } eliza();
while (my $answer = <STDIN>). What you have "loops over"$answer... which is always true (so it keeps going), and which never again reads new input (so it keeps printing the same).forstatement.