Re: Perl beginner seeks help
[email protected] (zentara)
| Newsgroups | perl.beginners |
|---|---|
| Message-ID | <[email protected]> |
On Tue, 20 May 2008 22:30:10 -0700 (PDT), [email protected] ("Mr. low level newb of stupidity ;\)") wrote: >hello, I am a, well, not a beginner per say, but rather still a >learner of Perl. I guess you could call me a beginner. anyway, I need >some help with my robot. it is not really Perl help, but you guys >could help with the code, too. I am making a talking robot like >A.L.I.C.E., a robot designed by a guy named Richard s. Wallace, and I >am having trouble deciding what to make him know. he knows basic >stuff, and the coding is somewhat fine, but I was wondering if you >guys could help me out here. he knows how to respond to his and >hellos, also more interesting stuff like his birthday and to >understand when you say something like 'I feel like crap' or >something, but he is still kinda dumb. any advice on how to improve >him with knowledge would be appreciated. > > >P.S. the code for what to say and what to say it to is regular >expressions. tell me if you have a better method. >e.g. elsif($reply =~ /\bwhat\b/ and $reply =~ /\btime\b/){ > print"I surely dont know the time."; >talk(); >} >talk is the subroutine that asks for input. I would setup a hash of regognized words/phrases as keys, with responses as the valaues. Then make a quoted regex of all the hash keys for efficiency. Read "perldoc -q efficiently match many regular expressions at once" There are many other tricks you could incorporate, like some sort of fuzzy matching, where all phrases are broken into words, and you count the number of word matches, to see if it's a close phase match. #!/usr/bin/perl use Data::Dumper; my %data; while (<DATA>) { chomp; /(.*)::=(.*)/; $data{$1} = $2; } my @recog = keys %data; print "@recog\n"; #make regexes just once for efficiency my @patterns = map { qr/$_/i } @recog; my $input = 'what'; # input() foreach my $pattern ( @patterns ){ if ($input =~ /$pattern/){ print "\n\n","$input-> ",$data{$input},"\n\n"; #this is your response } } #print Dumper(\%data); __DATA__ hello::=what's happening bye::=so long what::=please elaborate __END__ zentara -- I'm not really a human, but I play one on earth. http://zentara.net/japh.html