User Control Panel
Advertisements

HELP US, HELP YOU!

Problem in module...

 
Post new topic   Reply to topic    Bot Depot Forum Index -> Perl
View unanswered posts
Author Message
Nate
God Like
God Like


Joined: 12 Nov 2003
Posts: 553

Reputation: 41.3Reputation: 41.3Reputation: 41.3Reputation: 41.3

PostPosted: Sat Dec 20, 2003 4:17 pm    Post subject: Reply with quote

I have this weird problem with one of my modules. Here's what the DOS prints:

Code:
Chaos AI Technology HyperKoda Module.<br /><br />What is your name?> Cerone<br />        DEBUG :: Folder = ./koda<br />        DEBUG :: Debug = 1<br />        DEBUG :: Client = Cerone<br />        DEBUG :: Name = Hyper Koda<br />        DEBUG :: FIELDS{Reply} =<br />        DEBUG :: FIELDS{Folder} = ./koda<br />        DEBUG :: FIELDS{Debug} = 1<br />        DEBUG :: FIELDS{Msg} =<br />        DEBUG :: FIELDS{Client} = Cerone<br />        DEBUG :: FIELDS{Init} = CONNECT<br />        DEBUG :: FIELDS{Name} = Hyper Koda<br />        DEBUG :: Sub _init has been called.<br />        DEBUG :: The bot's folder exists.<br />        DEBUG :: Initilization complete.<br />Can't call method "console" without a package or object reference at koda.pl lin<br />e 28, <STDIN> line 1.<br />Press any key to continue . . .


Here's "koda.pl"

Code:
#!/usr/bin/perl<br /><br /># Chaos AI Technology<br /># AI::CKS::Koda Module Example Script<br /><br /># Use the local library.<br />use lib "./lib";<br /><br /># Require the Chaos module.<br />use AI::CKS::Koda;<br /><br /># Ask the client his name.<br />print "Chaos AI Technology HyperKoda Module.\n\n";<br />print "What is your name?> ";<br />my $client_name = <STDIN>;<br />chomp $client_name;<br /><br /># Create a new Chaos bot.<br />my $koda = new AI::CKS::Koda (<br />   Name   => "Hyper Koda",<br />   Client => "$client_name",<br />   Folder => "./koda",<br />   Debug  => 1,<br />);<br /><br /># Go on a console loop.<br />while (1) {<br />   $koda->console();<br />}


And here's Koda.pm:

Code:
package AI::CKS::Koda;<br /><br />#############################################<br />## Chaos A.I. Technology                   ##<br />##-----------------------------------------##<br />## AI::CKS::Koda - The CKS Hyper Koda      ##<br />##     static chatterbot module.           ##<br />##-----------------------------------------##<br />$AUTHOR = "Kirsle";  ## This module is     ##<br />$VERSION = "1.00";   ## under the GPL      ##<br />$COPYRIGHT = "2003"; ## license.           ##<br />#############################################<br /><br />## NAME:<br />##     AI::CKS::Koda - The CKS Hyper Koda<br />##     static chatterbot module.<br />## SYNOPSIS:<br />##     use AI::CKS::Koda;<br />##<br />##     my $koda = new AI::CKS::Koda (<br />##          Name   => "Hyper Koda",<br />##          Folder => "./",<br />##          Debug  => 0,<br />##     );<br />##<br />##     while (1) {<br />##          $koda->console();<br />##     }<br />## DESCRIPTION:<br />##     The Hyper Koda bot module that uses static<br />##     preprogrammed responses to chat with the client.<br />## MODULE HOMEPAGE:<br />##     http://ai.chaos.kirsle.net/<br /><br /># Require the root AI::CKS module.<br />use AI::CKS;<br /><br /># Use LWP::Simple for retrieval of text files.<br />use LWP::Simple;<br /><br /># Create a new AI::CKS instance.<br />my $cks = new AI::CKS;<br /><br /># Declare the default hash information.<br />%fields = (<br />   Name     => "Hyper Koda",<br />   Folder   => "./",<br />   Debug    => 0,<br />   Init     => "CONNECT",<br />   Client   => "User",<br />   Msg      => undef,<br />   Reply    => undef,<br />);<br /><br /># Sub: new ($name,$folder,$debug,$etc);<br />#<br /># Creates a new AI::CKS::Chaos instance. Variables<br /># must be sent as a hash:<br /># $cks = new AI::CKS::Chaos (<br />#   Name   => "Ultimate Chaos",<br />#   Folder => "./bot_files",<br />#   Debug  => 1,<br /># );<br />sub new {<br />   my $class = shift;<br />   my %hash = @_;<br /><br />   my $self = $class;<br /><br />   # Print the data in %hash.<br />   foreach my $item (keys %hash) {<br />  $cks->debug ("$item = $hash{$item}") if $hash{"Debug"};<br /><br />  # Set the items in this hash as items<br />  # which are required in the %fields hash.<br />  $fields{$item} = $hash{$item};<br />   }<br /><br />   # Print the data in %fields.<br />   foreach my $value (keys %fields) {<br />  $cks->debug ("FIELDS{$value} = $fields{$value}") if $fields{"Debug"};<br />   }<br /><br />   # Initialize the robot.<br />   $self->_init;<br /><br />   # Return true.<br />   return 1;<br />}<br /><br /># Sub: _init ();<br />#<br /># Activates the initialization process of the Chaos robot.<br /># Requires no arguments and must be called after all the<br /># bot's information has been defined. The bot's folder and<br /># name are required at this point.<br />sub _init {<br />   my $self = shift;<br /><br />   $cks->debug ("Sub _init has been called.") if $fields{"Debug"};<br /><br />   # Step 1: Make sure the path and name have been defined.<br />   my $error;<br />   if ($fields{"Name"} eq "") {<br />  $error = 1;<br />  $cks->panic ("You failed to give your bot a name!",1);<br />  return 0;<br />   }<br />   if ($fields{"Folder"} eq "") {<br />  $error = 1;<br />  $cks->panic ("You failed to specify a bot folder!",1);<br />  return 0;<br />   }<br /><br />   # If we didn't get any errors...<br />   # Step 2: See if the bot needs to be "born"<br />   if (!$error) {<br />  if (-e $fields{"Folder"} == 1) {<br />     $cks->debug ("The bot's folder exists.") if $fields{"Debug"};<br />  }<br />  else {<br />     $cks->debug ("The bot does not have a folder.") if $fields{"Debug"};<br />     # It doesn't have a folder yet.<br /><br />     # Create all folder trees.<br />     mkdir ($fields{"Folder"});<br />     mkdir ($fields{"Folder"} . "/clients");<br /><br />     # Create the initial reply data.<br />     open (REPLY, ">$fields{'Folder'}/reply.txt");<br />     print REPLY ("connect][Hello there <client> and thanks for connecting!\n"<br />    . "hello|hi|hey][Hello there!|Hey!|Hi!\n"<br />    . "whats up|what is up|wassup][Not much, you?\n"<br />    . "bye|cya|later][See you later <client>!");<br />     close (REPLY);<br />  }<br />   }<br /><br />   $cks->debug ("Initilization complete.") if $fields{"Debug"};<br />}<br /><br /># Sub: console();<br />#<br /># Calls the console for single user chat with the bot.<br /># No arguments are passed.<br />sub console {<br />   my $self = shift;<br /><br />   # If this is the first time.<br />   my $first = 0;<br />   if (not defined $reply) {<br />  $cks->debug ("First message.") if $fields{"Debug"};<br />  $first = 1;<br />  $fields{"Msg"} = $fields{"Init"};<br />  $fields{"Reply"} = $self->respond ($fields{"Client"},$fields{"Msg"});<br />   }<br /><br />   # Setup the console window.<br />   print ($fields{"Client"} . "\n"<br />  . "   >" . $fields{"Msg"} . "\n"<br />  . $fields{"Name"} . "\n"<br />  . "   >" . $fields{"Reply"} . "\n"<br />  . ">> ");<br />   my $msg = <STDIN>;<br />   chomp $msg;<br /><br />   $fields{"Msg"} = $msg;<br />   $fields{"Reply"} = $self->respond ($fields{"Client"},$fields{"Msg"});<br />}<br /><br /># Sub: respond($client,$msg);<br />#<br /># Commands the bot to get a response to your message.<br /># Responses are kept in "[folder]/reply.txt" in a simple<br /># plain text format.<br />sub respond {<br />   my ($self,$client,$msg) = @_;<br /><br />   $cks->debug ("Respond sub called.") if $fields{"Debug"};<br /><br />   # Format the message... make lower-case, chomp off extra characters.<br />   $msg = lc($msg);<br />   chomp $msg;<br /><br />   # Format the client... make lower-case, remove spaces.<br />   $client = lc($client);<br />   $client =~ s/ //g;<br /><br />   # Now... get a reply.<br />   # Get data from the reply file.<br />   open (DATA, "$fields{'Folder'}/reply.txt");<br />   my @data = <DATA>;<br />   close (DATA);<br /><br />   # Go through the data.<br />   my $found = 0;<br />   foreach $item (@data) {<br />  if ($item ne "" && $found == 0) {<br />     my ($first,$last) = split(/\]\[/, $item);<br />     if ($msg =~ /^$first$/i) {<br />    $msg =~ s/<(.|\n)+?>//g;<br />    $found = 1;<br />    @last = split(/\|/, $last);<br />    $reply = $last [ int(rand(scalar(@last))) ];<br />     }<br />  }<br />   }<br /><br />   # If we didn't find a response... say something.<br />   if ($reply eq "") {<br />  my @replies = (<br />     "That's interesting.",<br />     "Go on...",<br />     "This is interesting.",<br />     "I see...",<br />     "Cool...",<br />  );<br />  $reply = $replies [ int(rand(scalar(@replies))) ];<br />   }<br /><br />   # Or if they forgot to make a damn message.<br />   if ($msg eq "") {<br />  $reply = "Let's change the subject.";<br />   }<br /><br />   # Return the response.<br />   return $reply;<br />}<br /><br />1;
Back to top
Nate
God Like
God Like


Joined: 12 Nov 2003
Posts: 553

Reputation: 41.3Reputation: 41.3Reputation: 41.3Reputation: 41.3

PostPosted: Sat Dec 20, 2003 4:35 pm    Post subject: Reply with quote

Never mind... I forgot to have the "new" sub return $self. Razz
Back to top
Mojave
Almost An Agent
Almost An Agent


Joined: 01 Nov 2003
Posts: 1434

Reputation: 66.4

PostPosted: Sat Dec 20, 2003 7:29 pm    Post subject: Reply with quote

You also haven't blessed it, which is required to instantiate an object. Also, %fields need to be hash reference inside the new method and blessed, otherwise %fields is a class variable and not the object variable I think you intend it to be.

Read through the new methods of other classes like AIM.pm or MSN.pm and make your "constructor" look more like theirs.
Back to top
Nate
God Like
God Like


Joined: 12 Nov 2003
Posts: 553

Reputation: 41.3Reputation: 41.3Reputation: 41.3Reputation: 41.3

PostPosted: Sat Dec 20, 2003 7:50 pm    Post subject: Reply with quote

I don't think the hash has to be inside the new() sub... it works just fine being outside it. I copied that %fields kind of thing from Chatbot::Eliza. It's outside a sub there too.

And I haven't had any problems with not blessing the variables.
Back to top
Mojave
Almost An Agent
Almost An Agent


Joined: 01 Nov 2003
Posts: 1434

Reputation: 66.4

PostPosted: Sat Dec 20, 2003 8:13 pm    Post subject: Reply with quote

OK, well I thought you were trying to do object-oriented Perl, where you could create multiple instances of your AI object. But I guess you're not. In that case, you should seriously think about renaming your new function, since most programmers will think you're creating objects, but you're not. Also, you can get rid of all references to $self, since they aren't actually doing anything. That will clean your code up quite a bit.
Back to top
Mojave
Almost An Agent
Almost An Agent


Joined: 01 Nov 2003
Posts: 1434

Reputation: 66.4

PostPosted: Sat Dec 20, 2003 8:17 pm    Post subject: Reply with quote

Also, since you're copying your code from ChatBot::Eliza, you should see that they do bless their object. And they are using %fields as class variables inside their object. But again, since you're not actually creating an object, you don't have to.
Back to top
Nate
God Like
God Like


Joined: 12 Nov 2003
Posts: 553

Reputation: 41.3Reputation: 41.3Reputation: 41.3Reputation: 41.3

PostPosted: Sat Dec 20, 2003 8:19 pm    Post subject: Reply with quote

Chatbot::Eliza was able to create multiple objects that could all chat with eachother and have different values inside the %fields hash. And their %fields hash was declared outside a sub. What's with that?
Back to top
Mojave
Almost An Agent
Almost An Agent


Joined: 01 Nov 2003
Posts: 1434

Reputation: 66.4

PostPosted: Sat Dec 20, 2003 8:32 pm    Post subject: Reply with quote

If you look at ChatBot::Eliza's constructor (new method), you'll see that they create a copy of %fields for each instance of the object.

Code:
sub new {<br />   my ($that,$name,$scriptfile) = @_;<br />   my $class = ref($that) || $that;<br />   my $self = {<br />  _permitted => \%fields,<br />  %fields,<br />   };<br />   bless $self, $class;<br />   $self->_initialize($name,$scriptfile);<br />   return $self;<br />} # end method new<br />


They create a new $self hash reference and include a copy of %fields, then bless the reference into the class and return that.

The way you are doing it, you have only one copy of %fields, so if you tried to create multiple objects (not really objects since you're not blessing them), you'd overwrite the values of %fields.

Without blessing the hashref into the class, you won't be able to have multiple AI objects chatting with each other like Eliza does.
Back to top
Nate
God Like
God Like


Joined: 12 Nov 2003
Posts: 553

Reputation: 41.3Reputation: 41.3Reputation: 41.3Reputation: 41.3

PostPosted: Sat Dec 20, 2003 10:10 pm    Post subject: Reply with quote

Ohhh I see. This wouldn't be the best bots to make chat with eachother though.. Maybe the Chaos bot, but the ones like Koda & Siko would just flood eachother. However, you could raise two or more Chaos bots that develop different personalities and stuff.

But I see what you're saying. I'll have to go and fix that.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Bot Depot Forum Index -> Perl All times are GMT
Page 1 of 1

 



Protected by phpBB Security phpBB-TweakS
phpBB Security Has Blocked 9 Exploit Attempts.
Antispam Captcha Mod by phpbb-security.com
Powered by phpBB © 2001, 2005 phpBB Group