# dialog.pl # A Perl program that inputs sentences and tries to respond. # S. Tanimoto, for CSE 341. use strict; # Sample data: my $testSent; $testSent = "This is a test."; $testSent = "Go jump in a lake!"; # the most important function: sub rewrite { my $sent = shift; my $punc; my $mode; my $ans; if ($sent =~ /([\.\?\!])$/) { $punc = $1; } if ($punc eq ".") { $mode = "declarative"; } if ($punc eq "?") { $mode = "interrogative"; } if ($punc eq "!") { $mode = "imperative"; } # print "This is a $mode sentence: $sent\n"; if ($mode eq "declarative") { $sent =~ s/.$/?/; $sent =~ s/you/YYOOUU/g; $sent =~ s/I|me/you/g; $sent =~ s/YYOOUU/me/g; $ans = "Are you sure " . lc($sent); } elsif ($mode eq "interrogative") { $ans = "Why do you want to know?"; } elsif ($mode eq "imperative") { $sent =~ s/.$/./; $ans = "No, YOU should " . lc($sent); } else { $ans = "I can't understand you when you say, " . lc($sent); } return $ans; } my $done = 0; while (! $done) { print "Enter a sentence:\n"; my $sent = ; chomp($sent); if ($sent eq "") { $done = 1; } if (! $done) {my $ans = rewrite($sent); print $ans . "\n"; } }