View Single Post
Re: megaHAL enthusiasm
Old
  (#2)
Pierre-Marie Baty
Roi de France
 
Pierre-Marie Baty's Avatar
 
Status: Offline
Posts: 5,049
Join Date: Nov 2003
Location: 46°43'60N 0°43'0W 0.187A
Default Re: megaHAL enthusiasm - 05-01-2004

first off you won't "need" to release the source of your bot since I'm using the BSD license which is a non-contaminative license (i.e you can do anything with the source code, including commercial or closed-source stuff).

The HAL_EmptyDictionary() does not free() the dictionary's memory space taken up by each of its words, it's just a speed hack. Instead of freeing all the stuff and slap malloc() calls everywhere again later, we use the realloc() handy function to reallocate each word. Since dictionaries are only altered to grow (not to shrink) no memory is leaked. An example of a dictionary can be,

"hello stupid bot"
(1)HELLO (2)STUPID (3)BOT
"you are a bot"
(1)HELLO (2)STUPID (3)BOT (4)YOU (5)ARE (6)A
"a bot is a stupid program"
(1)HELLO (2)STUPID (3)BOT (4)YOU (5)ARE (6)A (7)IS (PROGRAM

If you were to free each word and allocating it again after that, you would end up manipulating memory twice too much

Also, adding a trailing dot to the generated sentences was a bad idea : I removed it (that is now, the sentences output like they come). Yes, you can't free() that one. It was one of the first hacks I put into the MegaHAL code but I wasn't really knewing what I was doing then

Finally, I don't see why you add spaces as words : dictionaries are not supposed to contain spaces. And I don't quite understand the why of your modification to the HAL_MakeWords(). Do you really get something working with this ???

Here's my current HAL_MakeWords() function:
Code:
void HAL_MakeWords (char *input, HAL_DICTIONARY *words)
{
   // this function breaks a string into an array of words
   int offset = 0;
   // clear the entries in the dictionary
   HAL_EmptyDictionary (words);
   if (strlen (input) == 0)
	  return; // if void, return
   // loop forever
   while (TRUE)
   {
	  // if the current character is of the same type as the previous character, then include
	  // it in the word. Otherwise, terminate the current word.
	  if (HAL_BoundaryExists (input, offset))
	  {
		 // add the word to the dictionary
		 if (words->entry == NULL)
			words->entry = (HAL_STRING *) malloc ((words->size + 1) * sizeof (HAL_STRING));
		 else
			words->entry = (HAL_STRING *) realloc (words->entry,  (words->size + 1) * sizeof (HAL_STRING));
		 if (words->entry == NULL)
			TerminateOnError ("RACC: HAL_MakeWords() unable to reallocate dictionary\n");
		 words->entry[words->size].length = (unsigned char) offset;
		 words->entry[words->size].word = input;
		 words->size += 1;
		 if (offset == (int) strlen (input))
			break;
		 input += offset;
		 offset = 0;
	  }
	  else
		 offset++;
   }
   return; // finished, no need to add punctuation (it's an ACTION game, woohoo!)
}



RACC home - Bots-United: beer, babies & bots (especially the latter)
"Learn to think by yourself, else others will do it for you."
  
Reply With Quote