When I owned my first Apple ][ computer back in 1985, it came with a little program called Animals. Just for fun this evening I rewrote it in Perl in a couple of hours… Here is a log of my conversation with it…
Basically, you think of an animal, and it tries to determine what it is.
If it fails, it learns the new animal by asking for information about it in the form of a simple Yes/No question.
does it live in the water? (y/n) : n
Ok, is the animal a moose? (y/n): n
Ok, I give up, what animal was it? dog
Ok, now please enter a question that would differentiate between ‘dog’ and ‘moose’: are they domesticated
And the correct answer to this question for ‘dog’? y
Thank you for teaching me about a new animal.
Another game? (y/n): y
does it live in the water? (y/n) : n
are they domesticated? (y/n) : y
Ok, is the animal a dog? (y/n): y
Sweet, I got it, I am so smart
Another game? (y/n): y
does it live in the water? (y/n) : n
are they domesticated? (y/n) : n
Ok, is the animal a moose? (y/n): n
Ok, I give up, what animal was it? spider
Ok, now please enter a question that would differentiate between ‘spider’ and ‘moose’: do people kill them by stepping on them
And the correct answer to this question for ‘spider’? y
Thank you for teaching me about a new animal.
Another game? (y/n): y
does it live in the water? (y/n) : n
are they domesticated? (y/n) : n
do people kill them by stepping on them? (y/n) : y
Ok, is the animal a spider? (y/n): y
Sweet, I got it, I am so smart
Another game? (y/n): y
does it live in the water? (y/n) : n
are they domesticated? (y/n) : n
do people kill them by stepping on them? (y/n) : n
Ok, is the animal a moose? (y/n): n
Ok, I give up, what animal was it? bear
Ok, now please enter a question that would differentiate between ‘bear’ and ‘moose’: does it have horns
And the correct answer to this question for ‘bear’? n
Thank you for teaching me about a new animal.
Another game? (y/n): y
does it live in the water? (y/n) : n
are they domesticated? (y/n) : n
do people kill them by stepping on them? (y/n) : n
does it have horns? (y/n) : y
Ok, is the animal a moose? (y/n): y
Sweet, I got it, I am so smart
————————————————————————————————————————-
Just for fun, the kids like it ![]()
Only 156 lines of Perl ....
my $another_game = 1;
while($another_game)
{
if(!open(F,"<database")) { print "can't open database.\n"; exit(1); }
my @l = <F>; close(F);
my $newrec = 0;
my %A = ();
my %Q = ();
my @L = ();
foreach my $L (@l)
{
chomp($L);
my ($rn,$str) = split(/=/,$L);
next unless($str);
$str =~ s/^\s*(.*?)\s*$/$1/;
if($rn > $newrec) { $newrec = $rn; }
if($str =~ m/a\((.*?)\)$/)
{
$A{$rn} = $1;
}
if($str =~ m/q\((.*?)\):\s*(\d+),(\d+)$/)
{
my $q_str = $1; my $yes = $2; my $no = $3;
$Q{$rn}{'qtxt'} = $q_str;
$Q{$rn}{'yes'} = $yes;
$Q{$rn}{'no'} = $no;
}
}
my $cr = 1;
$newrec++;
my $asking = 1;
my $next = 0;
while($asking)
{
my $qtxt = $Q{$cr}{'qtxt'};
my $r = '';
my $yon = 0;
while($yon==0)
{
$cmd = "espeak -s 120 \"$qtxt\"";
#`$cmd`;
print $qtxt."? (y/n) : ";
$r = <STDIN>;
chomp($r);
$r =~ tr/a-z/A-Z/;
if($r eq 'Y') { $yon = 1; }
if($r eq 'N') { $yon = 1; }
}
$next = 0;
if($r eq 'Y')
{
$next = $Q{$cr}{'yes'};
}
else
{
$next = $Q{$cr}{'no'};
}
if($Q{$next})
{
$cr = $next;
next;
}
$asking = 0;
}
if($A{$next})
{
my $r = ''; my $yon = 0;
$cmd = "espeak -s 120 \"Ok, is the animal a ".$A{$next}."?\"";
#`$cmd`;
print "Ok, is the animal a ".$A{$next}."? (y/n): ";
while($yon==0)
{
$r = <STDIN>;
chomp($r);
$r =~ tr/a-z/A-Z/;
if($r eq 'Y') { $yon = 1; }
if($r eq 'N') { $yon = 1; }
}
if($r eq 'Y')
{
print "Sweet, I got it, I am so smart\n";
#`espeak -s 120 \"Sweet I got it, I am so smart\"`;
print "\nAnother game? (y/n): "; my $r = <STDIN>;
chomp($r);
if($r eq 'n') { $another_game = 0; }
if($r eq 'y') { $another_game = 1; }
}
if($r eq 'N')
{
print "Ok, I give up, what animal was it? "; my $r = <STDIN>; chomp($r);
#$cmd = "espeak -s 120 \"Ok, now please enter a question that would differentiate between '$r' and '".$A{$next}."'\"";
#`$cmd`;
print "\nOk, now please enter a question that would differentiate between '$r' and '".$A{$next}."': "; my $newq = <STDIN>;
chomp($newq);
print "And the correct answer to this question for '".$r."'? "; my $newa = <STDIN>; chomp($newa);
my $wrongan = $A{$next};
my %newlines = ();
my $newrec2 = $newrec + 1;
$newa =~ tr/a-z/A-Z/;
if($newa eq 'Y')
{
$newlines{$next} = "$next=q(".$newq."):$newrec,$newrec2";
}
else
{
$newlines{$next} = "$next=q(".$newq."):$newrec2,$newrec";
}
$newlines{$newrec} = "$newrec=a(".$r.")";
$newlines{$newrec2} = "$newrec2=a(".$wrongan.")";
if(!open(F,">database")) { print "can't write to database\n"; next; }
foreach my $T (@l)
{
chomp($T);
my ($rn,$str) = split(/=/,$T);
if($newlines{$rn})
{
print F $newlines{$rn}."\n";
delete $newlines{$rn};
}
else
{
print F $T."\n";
}
}
foreach my $str (keys %newlines)
{
print F $newlines{$str}."\n";
}
print "Thank you for teaching me about a new animal.\n";
close(F);
print "\nAnother game? (y/n): "; my $r = <STDIN>;
chomp($r);
if($r eq 'n') { exit; }
print "\n";
}
}
}
The lines with “espeak” are commented out… I wrote this on my Ubuntu linux box, and ‘espeak’ is a speech synthesis program on it.

RSS AI Zone




