Certificate in Perl Programming, UW Extension
eliza.pl

eliza.pl


#!/usr/bin/perl

use strict;

print "Hello, I'm Elly.  What would you like to discuss today?\n";

# an array of some canned responses that sound like real questions
my @canned = ('Tell me more about',
              'Why do you think',
              'What else is going on with',
              'Who else is involved with',
              'When did you start noticing',
              'How did you feel about');


# whether or not we've talked about parents yet
my $parents = 0;

# how many times we haven't talked about parents
my $count = 0;

# every time through the loop, ask for user input and put it in $input
while (my $input = <>) {

    # get rid of final newline
    chomp $input;

    # get rid of all punctuation and put it in lowercase
    $input = lc($input);
    $input =~ s![^\w\s]!!g;

    # user didn't type anything!
    if (! $input) {
        print "I'd like to hear what you think.\n";
        next;
    }

    # change you -> me and vice versa, leaving a marker (#) to show those changed
    $input =~ s!\byou\b!#me!g;
    $input =~ s!\byour!#my!g;
    $input =~ s!\bmy!#your!g;
    $input =~ s!\bmine\b!#yours!g;
    $input =~ s!\bme\b!#you!g;
    $input =~ s!\bi\b!#you!g;
    $input =~ s!\bim\b!#you're!g;

    # remove all the markers
    $input =~ s!#!!g;

    # ooh - user mentioned his mother!
    if ($input =~ m!\b(mom|mother|ma|mommy|mama)\b!) {
        print "Tell me more about your $1\n";
        $parents = 1;
        next;
    }

    # okay - so why isn't user talking about mother?
    if (! $parents && $count++ > 10) {
        print "Why don't we talk about your mother?\n";
        $parents = 1;
        next;
    }

    # half the time we'll parrot back to the user
    if(int(rand(2))) {
        $input =~ s/.*?you/you/;
        print $canned[ rand(6) ], " $input ?\n";
        next;
    }
    else {
        # and half the time we won't
        print $canned[ rand(6) ], " that.\n";
    }
}


UW Educational Outreach
5001 25th Avenue Northeast
Seattle, Washington 98105
Telephone: (206) 543-2320
Toll-free: (800) 543-2320
TTY: (206) 543-0898
Please send questions about programs and courses to uweo@u.washington.edu and questions or comments about the Perl Programming class to dtreder@amazon.com