Hi pierre. I'm hoping you know much about megaHAL.
I have been trying to get this megaHAL to work with my bot mostly from your source. I have got it working and everything so far, just a few questions on memory issues with megaHAL and bug type things I've noticed with it.
I've had a look in original megaHal source/docs/google about this but found nothing. Why does the Free/Empty dictionary function NOT free the actual words that were stored? This causes memory leaks unless they are freed elsewhere somehow, but I don't know where it could be (i.e. could it get copied into bot_model and stored until end?)
It depends also on how you implement it, I'm using dynamically allocated strings most of the time, I know with yours you use the message stored in the bot which is static.
The question is, is it safe to free all the strings in Empty_Dictionary function? Or are they supposed to mingle around in memory?!
Some bugs etc:
The input_words dictionary holds words that are from a message that the bot wants to learn from. The Make_Words function doesn't actually get a word, it gets a string from a point until the end of the whole message.
(this bit-->)
Code:
words->entry[words->size].word = input;
I have altered the function to put the proper word(s) into array positions. I will share it
Code:
void HAL_MakeWords (char *input, HAL_DICTIONARY *words)
{
// this function breaks a string into an array of words
int iLen;
int iStart;
int iEnd;
char *szNewWord;
int iNewWordLength;
HAL_STRING *pNewWord;
//int offset = 0;
// clear the entries in the dictionary
HAL_EmptyDictionary (words);
if ( !input || !*input )
return; // if void, return
// re-written
iLen = strlen(input);
iStart = 0;
iEnd = 0;
while ( iStart < iLen )
{
if ( HAL_BoundaryExists(input,iEnd) || (iEnd == iLen) )
// If there is a new word to take in or at the end of the string
// then add the word to the dictionary
{
// 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));
// get pointer to new word for quick access
pNewWord = &words->entry[words->size];
// work out new word length for new string
iNewWordLength = iEnd - iStart;
szNewWord = (char*)malloc(sizeof(char)*(iNewWordLength+1));
// copy word into string szNewWord
// word starts from position iStart until iEnd
// (goes on for iNewWordLength)
strncpy(szNewWord,&input[iStart],iNewWordLength);
szNewWord[iNewWordLength] = 0;
// update new word store
pNewWord->length = iNewWordLength;
pNewWord->word = szNewWord;
// increment number of words stored
words->size++;
iStart = iEnd;
}
iEnd++;
}
// rest of function below except the add word stuff
...
...
important:
This change also adds spaces as words, is this correct? I thought the boundary function would find that?
Another small problem: When you free the input_words at the end when the server deactivates, you sometimes encounter the word "." added in the MakeWords function, and you can't free it as it isn't malloc'd in MakeWords, and instead comes up with an assertion failure and doesn't free it.
Anyway. looking good, your code is all credited and also I'll need to release the source of my bot soon