SlideE6B has asked for the wisdom of the Perl Monks concerning the following question:

I'm pretty new to perl. I've been working on an AIM bot and I finially got one to work. The only thing is that he is kind of stupid. He only responds to certian things. If I could some how parse the data that the bot recieves I could make it more intelligent. Can anyone explain how this works or even suggest another way of doing it? Thanks.

edited: Sun Mar 9 15:04:32 2003 by jeffa - title change (was: Parsing Data)

Replies are listed 'Best First'.
Re: Parsing user input for AIM bot
by xylus (Pilgrim) on Mar 09, 2003 at 06:52 UTC
    It sounds like you are trying to write a chatbot type program... you may want to look at the following nodes:

    Perl Alicebot?

    Dueling Alicebots

    And the following module:
    Chatbot::Eliza
    perl -e '$_=$0;split??;chop$_;$;=pop@_;$;++for(0..9060420);$_.=reverse +$;;print'
Re: Parsing user input for AIM bot
by artist (Parson) on Mar 09, 2003 at 01:17 UTC
    Hi,
    What's an AIM bot?
    Also tell us what type of data you want to parse.
    You can use Parse::RecDescent for parsing natural language data if the format is somewhat defined.

    artist

      By AIM bot I think he means a robot that hangs out on AOL's Instant Messanger service and responds to messages sent to it. Kinda like an IRC channel bot.
Re: Parsing user input for AIM bot
by SlideE6B (Initiate) on Mar 09, 2003 at 04:58 UTC
    an AIM bot uses perl to logon to Aol Instant Messenger. It responds to users that instant messenge it. The data is a scalar. It is text. $msg. It might equal "How are you?" or something like that. It is all fine and dandy until someone says "How are u". The bot doesn't know how to respond to that. I would need to be able to parse the "how" in "How are u" into $parsed and the are in "How are you" into $parsed2 or something like that so I could say

    if($parsed eq "how" && $parsed2 eq "are"){
    $msg = "I'm fine. How are you?"
    }

    and give a responce. So basically what I need is to parse $msg at every space and create a variable for each one. Or something like that. Any suggestions? Thanks

      I'm not sure that what you described above is the best solution to your problem, but this will take the message and shove the words into an array:
      @words = split /\s+/, <DATA> print join "\n", @words, ""; __DATA__ How are you?
      That prints:
      How are you?
      Note that the last word contains punctuation. To remove punctuation, change the first line to:
      map { s/[\?\.\!]*//g; } (@words = split /\s+/, <DATA>);
      Update: Added bit about punctuation.

      Update 2: Without a more thorough description or code to look at, a better solution might be to just replace common abbreviations with the full word. For example:
      $msg =~ s/\bu\b/you/g; # Replace "u" with "you" $msg =~ s/\br\b/are/g; # Replace "r" with "are" ...