AI-MegaHAL
view release on metacpan or search on metacpan
libmegahal.c view on Meta::CPAN
#else
#include <sys/types.h>
#endif
#include "megahal.h"
#if defined(DEBUG)
#include "debug.h"
#endif
#define P_THINK 40
#define D_KEY 100000
#define V_KEY 50000
#define D_THINK 500000
#define V_THINK 250000
#define MIN(a,b) ((a)<(b))?(a):(b)
#define COOKIE "MegaHALv8"
#define TIMEOUT 1
#define DEFAULT "."
#define COMMAND_SIZE (sizeof(command)/sizeof(command[0]))
#define BYTE1 unsigned char
#define BYTE2 unsigned short
#define BYTE4 unsigned int
#ifdef __mac_os
#define bool Boolean
#endif
#ifdef DOS
#define SEP "\\"
#else
#define SEP "/"
#endif
#ifdef AMIGA
#undef toupper
#define toupper(x) ToUpper(x)
#undef tolower
#define tolower(x) ToLower(x)
#undef isalpha
#define isalpha(x) IsAlpha(_AmigaLocale,x)
#undef isalnum
#define isalnum(x) IsAlNum(_AmigaLocale,x)
#undef isdigit
#define isdigit(x) IsDigit(_AmigaLocale,x)
#undef isspace
#define isspace(x) IsSpace(_AmigaLocale,x)
#endif
#ifndef __mac_os
#undef FALSE
#undef TRUE
typedef enum { FALSE, TRUE } bool;
#endif
typedef struct {
BYTE1 length;
char *word;
} STRING;
typedef struct {
BYTE4 size;
STRING *entry;
BYTE2 *index;
} DICTIONARY;
typedef struct {
BYTE2 size;
STRING *from;
STRING *to;
} SWAP;
typedef struct NODE {
BYTE2 symbol;
BYTE4 usage;
BYTE2 count;
BYTE2 branch;
struct NODE **tree;
} TREE;
typedef struct {
BYTE1 order;
TREE *forward;
TREE *backward;
TREE **context;
DICTIONARY *dictionary;
} MODEL;
typedef enum { UNKNOWN, QUIT, EXIT, SAVE, DELAY, HELP, SPEECH, VOICELIST, VOICE, BRAIN, QUIET} COMMAND_WORDS;
typedef struct {
STRING word;
char *helpstring;
COMMAND_WORDS command;
} COMMAND;
/*===========================================================================*/
static int width=75;
static int order=5;
static bool typing_delay=FALSE;
static bool noprompt=FALSE;
static bool speech=FALSE;
static bool quiet=FALSE;
static bool nowrap=FALSE;
static bool nobanner=FALSE;
static char *errorfilename = "megahal.log";
static char *statusfilename = "megahal.txt";
static DICTIONARY *words=NULL;
static DICTIONARY *greets=NULL;
static MODEL *model=NULL;
static FILE *errorfp;
static FILE *statusfp;
libmegahal.c view on Meta::CPAN
output=generate_reply(model, greets);
write_output(output);
return 1;
case QUIET:
quiet=!quiet;
return 1;
default:
return 0;
}
return 0;
}
/*
megahal_cleanup --
Clean up everything. Prepare for exit.
*/
void megahal_cleanup(void)
{
save_model("megahal.brn", model);
#ifdef AMIGA
CloseLocale(_AmigaLocale);
#endif
}
/*---------------------------------------------------------------------------*/
/*
* Function: Execute_Command
*
* Purpose: Detect whether the user has typed a command, and
* execute the corresponding function.
*/
COMMAND_WORDS execute_command(DICTIONARY *words, int *position)
{
unsigned int i;
unsigned int j;
/*
* If there is only one word, then it can't be a command.
*/
*position=words->size+1;
if(words->size<=1) return(UNKNOWN);
/*
* Search through the word array. If a command prefix is found,
* then try to match the following word with a command word. If
* a match is found, then return a command identifier. If the
* Following word is a number, then change the judge. Otherwise,
* continue the search.
*/
for(i=0; i<words->size-1; ++i)
/*
* The command prefix was found.
*/
if(words->entry[i].word[words->entry[i].length - 1] == '#') {
/*
* Look for a command word.
*/
for(j = 0; j < COMMAND_SIZE; ++j)
if(wordcmp(command[j].word, words->entry[i + 1]) == 0) {
*position = i + 1;
return(command[j].command);
}
}
return(UNKNOWN);
}
/*---------------------------------------------------------------------------*/
/*
* Function: ExitHAL
*
* Purpose: Terminate the program.
*/
void exithal(void)
{
#ifdef __mac_os
/*
* Must be called because it does use some system memory
*/
if (gSpeechChannel) {
StopSpeech(gSpeechChannel);
DisposeSpeechChannel(gSpeechChannel);
gSpeechChannel = nil;
}
#endif
exit(0);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Read_Input
*
* Purpose: Read an input string from the user.
*/
char *read_input(char *prompt)
{
static char *input=NULL;
bool finish;
int length;
int c;
/*
* Perform some initializations. The finish boolean variable is used
* to detect a double line-feed, while length contains the number of
* characters in the input string.
*/
finish=FALSE;
length=0;
if(input==NULL) {
input=(char *)malloc(sizeof(char));
if(input==NULL) {
error("read_input", "Unable to allocate the input string");
return(input);
}
}
/*
* Display the prompt to the user.
*/
fprintf(stdout, prompt);
fflush(stdout);
/*
* Loop forever, reading characters and putting them into the input
* string.
*/
while(TRUE) {
/*
* Read a single character from stdin.
*/
c=getc(stdin);
/*
* If the character is a line-feed, then set the finish variable
* to TRUE. If it already is TRUE, then this is a double line-feed,
* in which case we should exit. After a line-feed, display the
* prompt again, and set the character to the space character, as
* we don't permit linefeeds to appear in the input.
*/
if((char)(c)=='\n') {
if(finish==TRUE) break;
fprintf(stdout, prompt);
fflush(stdout);
finish=TRUE;
c=32;
} else {
finish=FALSE;
}
/*
* Re-allocate the input string so that it can hold one more
* character.
*/
++length;
input=(char *)realloc((char *)input,sizeof(char)*(length+1));
if(input==NULL) {
error("read_input", "Unable to re-allocate the input string");
return(NULL);
}
/*
* Add the character just read to the input string.
*/
input[length-1]=(char)c;
input[length]='\0';
}
while(isspace(input[length-1])) --length;
input[length]='\0';
/*
* We have finished, so return the input string.
*/
return(input);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Initialize_Error
*
* Purpose: Close the current error file pointer, and open a new one.
*/
bool initialize_error(char *filename)
{
if(errorfp!=stderr) fclose(errorfp);
if(filename==NULL) return(TRUE);
errorfp = fopen(filename, "a");
if(errorfp==NULL) {
errorfp=stderr;
return(FALSE);
}
return(print_header(errorfp));
}
/*---------------------------------------------------------------------------*/
/*
* Function: Error
*
* Purpose: Print the specified message to the error file.
*/
void error(char *title, char *fmt, ...)
{
va_list argp;
fprintf(errorfp, "%s: ", title);
va_start(argp, fmt);
vfprintf(errorfp, fmt, argp);
va_end(argp);
fprintf(errorfp, ".\n");
fflush(errorfp);
// fprintf(stderr, "MegaHAL died for some reason; check the error log.\n");
exit(1);
}
/*---------------------------------------------------------------------------*/
bool warn(char *title, char *fmt, ...)
{
va_list argp;
fprintf(errorfp, "%s: ", title);
va_start(argp, fmt);
vfprintf(errorfp, fmt, argp);
libmegahal.c view on Meta::CPAN
/*---------------------------------------------------------------------------*/
/*
* Function: Add_Word
*
* Purpose: Add a word to a dictionary, and return the identifier
* assigned to the word. If the word already exists in
* the dictionary, then return its current identifier
* without adding it again.
*/
BYTE2 add_word(DICTIONARY *dictionary, STRING word)
{
unsigned int i;
int position;
bool found;
/*
* If the word's already in the dictionary, there is no need to add it
*/
position=search_dictionary(dictionary, word, &found);
if(found==TRUE) goto succeed;
/*
* Increase the number of words in the dictionary
*/
dictionary->size+=1;
/*
* Allocate one more entry for the word index
*/
if(dictionary->index==NULL) {
dictionary->index=(BYTE2 *)malloc(sizeof(BYTE2)*
(dictionary->size));
} else {
dictionary->index=(BYTE2 *)realloc((BYTE2 *)
(dictionary->index),sizeof(BYTE2)*(dictionary->size));
}
if(dictionary->index==NULL) {
error("add_word", "Unable to reallocate the index.");
goto fail;
}
/*
* Allocate one more entry for the word array
*/
if(dictionary->entry==NULL) {
dictionary->entry=(STRING *)malloc(sizeof(STRING)*(dictionary->size));
} else {
dictionary->entry=(STRING *)realloc((STRING *)(dictionary->entry),
sizeof(STRING)*(dictionary->size));
}
if(dictionary->entry==NULL) {
error("add_word", "Unable to reallocate the dictionary to %d elements.", dictionary->size);
goto fail;
}
/*
* Copy the new word into the word array
*/
dictionary->entry[dictionary->size-1].length=word.length;
dictionary->entry[dictionary->size-1].word=(char *)malloc(sizeof(char)*
(word.length));
if(dictionary->entry[dictionary->size-1].word==NULL) {
error("add_word", "Unable to allocate the word.");
goto fail;
}
for(i=0; i<word.length; ++i)
dictionary->entry[dictionary->size-1].word[i]=word.word[i];
/*
* Shuffle the word index to keep it sorted alphabetically
*/
for(i=(dictionary->size-1); i>position; --i)
dictionary->index[i]=dictionary->index[i-1];
/*
* Copy the new symbol identifier into the word index
*/
dictionary->index[position]=dictionary->size-1;
succeed:
return(dictionary->index[position]);
fail:
return(0);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Search_Dictionary
*
* Purpose: Search the dictionary for the specified word, returning its
* position in the index if found, or the position where it
* should be inserted otherwise.
*/
int search_dictionary(DICTIONARY *dictionary, STRING word, bool *find)
{
int position;
int min;
int max;
int middle;
int compar;
/*
* If the dictionary is empty, then obviously the word won't be found
*/
if(dictionary->size==0) {
position=0;
goto notfound;
}
/*
* Initialize the lower and upper bounds of the search
*/
min=0;
max=dictionary->size-1;
/*
* Search repeatedly, halving the search space each time, until either
* the entry is found, or the search space becomes empty
*/
while(TRUE) {
/*
* See whether the middle element of the search space is greater
* than, equal to, or less than the element being searched for.
*/
middle=(min+max)/2;
libmegahal.c view on Meta::CPAN
position=middle;
goto found;
} else if(compar>0) {
if(max==middle) {
position=middle+1;
goto notfound;
}
min=middle+1;
} else {
if(min==middle) {
position=middle;
goto notfound;
}
max=middle-1;
}
}
found:
*find=TRUE;
return(position);
notfound:
*find=FALSE;
return(position);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Find_Word
*
* Purpose: Return the symbol corresponding to the word specified.
* We assume that the word with index zero is equal to a
* NULL word, indicating an error condition.
*/
BYTE2 find_word(DICTIONARY *dictionary, STRING word)
{
int position;
bool found;
position=search_dictionary(dictionary, word, &found);
if(found==TRUE) return(dictionary->index[position]);
else return(0);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Wordcmp
*
* Purpose: Compare two words, and return an integer indicating whether
* the first word is less than, equal to or greater than the
* second word.
*/
int wordcmp(STRING word1, STRING word2)
{
unsigned int i;
unsigned int bound;
bound=MIN(word1.length,word2.length);
for(i=0; i<bound; ++i)
if(toupper(word1.word[i])!=toupper(word2.word[i]))
return((int)(toupper(word1.word[i])-toupper(word2.word[i])));
if(word1.length<word2.length) return(-1);
if(word1.length>word2.length) return(1);
return(0);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Free_Dictionary
*
* Purpose: Release the memory consumed by the dictionary.
*/
void free_dictionary(DICTIONARY *dictionary)
{
if(dictionary==NULL) return;
if(dictionary->entry!=NULL) {
free(dictionary->entry);
dictionary->entry=NULL;
}
if(dictionary->index!=NULL) {
free(dictionary->index);
dictionary->index=NULL;
}
dictionary->size=0;
}
/*---------------------------------------------------------------------------*/
void free_model(MODEL *model)
{
if(model==NULL) return;
if(model->forward!=NULL) {
free_tree(model->forward);
}
if(model->backward!=NULL) {
free_tree(model->backward);
}
if(model->context!=NULL) {
free(model->context);
}
if(model->dictionary!=NULL) {
free_dictionary(model->dictionary);
free(model->dictionary);
}
free(model);
}
/*---------------------------------------------------------------------------*/
void free_tree(TREE *tree)
{
static int level=0;
unsigned int i;
if(tree==NULL) return;
if(tree->tree!=NULL) {
if(level==0) progress("Freeing tree", 0, 1);
for(i=0; i<tree->branch; ++i) {
++level;
free_tree(tree->tree[i]);
libmegahal.c view on Meta::CPAN
dictionary->size=0;
dictionary->index=NULL;
dictionary->entry=NULL;
return(dictionary);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Save_Dictionary
*
* Purpose: Save a dictionary to the specified file.
*/
void save_dictionary(FILE *file, DICTIONARY *dictionary)
{
unsigned int i;
fwrite(&(dictionary->size), sizeof(BYTE4), 1, file);
progress("Saving dictionary", 0, 1);
for(i=0; i<dictionary->size; ++i) {
save_word(file, dictionary->entry[i]);
progress(NULL, i, dictionary->size);
}
progress(NULL, 1, 1);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Load_Dictionary
*
* Purpose: Load a dictionary from the specified file.
*/
void load_dictionary(FILE *file, DICTIONARY *dictionary)
{
unsigned int i;
int size;
fread(&size, sizeof(BYTE4), 1, file);
progress("Loading dictionary", 0, 1);
for(i=0; i<size; ++i) {
load_word(file, dictionary);
progress(NULL, i, size);
}
progress(NULL, 1, 1);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Save_Word
*
* Purpose: Save a dictionary word to a file.
*/
void save_word(FILE *file, STRING word)
{
unsigned int i;
fwrite(&(word.length), sizeof(BYTE1), 1, file);
for(i=0; i<word.length; ++i)
fwrite(&(word.word[i]), sizeof(char), 1, file);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Load_Word
*
* Purpose: Load a dictionary word from a file.
*/
void load_word(FILE *file, DICTIONARY *dictionary)
{
unsigned int i;
STRING word;
fread(&(word.length), sizeof(BYTE1), 1, file);
word.word=(char *)malloc(sizeof(char)*word.length);
if(word.word==NULL) {
error("load_word", "Unable to allocate word");
return;
}
for(i=0; i<word.length; ++i)
fread(&(word.word[i]), sizeof(char), 1, file);
add_word(dictionary, word);
free(word.word);
}
/*---------------------------------------------------------------------------*/
/*
* Function: New_Node
*
* Purpose: Allocate a new node for the n-gram tree, and initialise
* its contents to sensible values.
*/
TREE *new_node(void)
{
TREE *node=NULL;
/*
* Allocate memory for the new node
*/
node=(TREE *)malloc(sizeof(TREE));
if(node==NULL) {
error("new_node", "Unable to allocate the node.");
goto fail;
}
/*
* Initialise the contents of the node
*/
node->symbol=0;
node->usage=0;
node->count=0;
node->branch=0;
node->tree=NULL;
return(node);
fail:
if(node!=NULL) free(node);
return(NULL);
}
/*---------------------------------------------------------------------------*/
/*
* Function: New_Model
*
* Purpose: Create and initialise a new ngram model.
*/
MODEL *new_model(int order)
{
MODEL *model=NULL;
model=(MODEL *)malloc(sizeof(MODEL));
if(model==NULL) {
error("new_model", "Unable to allocate model.");
goto fail;
}
model->order=order;
libmegahal.c view on Meta::CPAN
BYTE2 symbol;
/*
* We only learn from inputs which are long enough
*/
if(words->size<=(model->order)) return;
/*
* Train the model in the forwards direction. Start by initializing
* the context of the model.
*/
initialize_context(model);
model->context[0]=model->forward;
for(i=0; i<words->size; ++i) {
/*
* Add the symbol to the model's dictionary if necessary, and then
* update the forward model accordingly.
*/
symbol=add_word(model->dictionary, words->entry[i]);
update_model(model, symbol);
}
/*
* Add the sentence-terminating symbol.
*/
update_model(model, 1);
/*
* Train the model in the backwards direction. Start by initializing
* the context of the model.
*/
initialize_context(model);
model->context[0]=model->backward;
for(i=words->size-1; i>=0; --i) {
/*
* Find the symbol in the model's dictionary, and then update
* the backward model accordingly.
*/
symbol=find_word(model->dictionary, words->entry[i]);
update_model(model, symbol);
}
/*
* Add the sentence-terminating symbol.
*/
update_model(model, 1);
return;
}
/*---------------------------------------------------------------------------*/
/*
* Function: Train
*
* Purpose: Infer a MegaHAL brain from the contents of a text file.
*/
void train(MODEL *model, char *filename)
{
FILE *file;
char buffer[1024];
DICTIONARY *words=NULL;
int length;
if(filename==NULL) return;
file=fopen(filename, "r");
if(file==NULL) {
printf("Unable to find the personality %s\n", filename);
return;
}
fseek(file, 0, 2);
length=ftell(file);
rewind(file);
words=new_dictionary();
progress("Training from file", 0, 1);
while(!feof(file)) {
if(fgets(buffer, 1024, file)==NULL) break;
if(buffer[0]=='#') continue;
buffer[strlen(buffer)-1]='\0';
upper(buffer);
make_words(buffer, words);
learn(model, words);
progress(NULL, ftell(file), length);
}
progress(NULL, 1, 1);
free_dictionary(words);
fclose(file);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Show_Dictionary
*
* Purpose: Display the dictionary for training purposes.
*/
void show_dictionary(DICTIONARY *dictionary)
{
register int i;
register int j;
FILE *file;
file=fopen("megahal.dic", "w");
if(file==NULL) {
warn("show_dictionary", "Unable to open file");
return;
}
for(i=0; i<dictionary->size; ++i) {
for(j=0; j<dictionary->entry[i].length; ++j)
fprintf(file, "%c", dictionary->entry[i].word[j]);
fprintf(file, "\n");
}
fclose(file);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Save_Model
*
* Purpose: Save the current state to a MegaHAL brain file.
*/
void save_model(char *modelname, MODEL *model)
{
FILE *file;
static char *filename=NULL;
if(filename==NULL) filename=(char *)malloc(sizeof(char)*1);
/*
* Allocate memory for the filename
*/
filename=(char *)realloc(filename,
sizeof(char)*(strlen(directory)+strlen(SEP)+12));
if(filename==NULL) error("save_model","Unable to allocate filename");
show_dictionary(model->dictionary);
if(filename==NULL) return;
sprintf(filename, "%s%smegahal.brn", directory, SEP);
file=fopen(filename, "wb");
if(file==NULL) {
warn("save_model", "Unable to open file `%s'", filename);
return;
}
fwrite(COOKIE, sizeof(char), strlen(COOKIE), file);
fwrite(&(model->order), sizeof(BYTE1), 1, file);
save_tree(file, model->forward);
save_tree(file, model->backward);
save_dictionary(file, model->dictionary);
fclose(file);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Save_Tree
*
* Purpose: Save a tree structure to the specified file.
*/
void save_tree(FILE *file, TREE *node)
{
static int level=0;
register int i;
fwrite(&(node->symbol), sizeof(BYTE2), 1, file);
libmegahal.c view on Meta::CPAN
goto fail;
}
fread(&(model->order), sizeof(BYTE1), 1, file);
load_tree(file, model->forward);
load_tree(file, model->backward);
load_dictionary(file, model->dictionary);
return(TRUE);
fail:
fclose(file);
return(FALSE);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Make_Words
*
* Purpose: Break a string into an array of words.
*/
void make_words(char *input, DICTIONARY *words)
{
int offset=0;
/*
* Clear the entries in the dictionary
*/
free_dictionary(words);
/*
* If the string is empty then do nothing, for it contains no words.
*/
if(strlen(input)==0) return;
/*
* Loop forever.
*/
while(1) {
/*
* 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(boundary(input, offset)) {
/*
* Add the word to the dictionary
*/
if(words->entry==NULL)
words->entry=(STRING *)malloc((words->size+1)*sizeof(STRING));
else
words->entry=(STRING *)realloc(words->entry, (words->size+1)*sizeof(STRING));
if(words->entry==NULL) {
error("make_words", "Unable to reallocate dictionary");
return;
}
words->entry[words->size].length=offset;
words->entry[words->size].word=input;
words->size+=1;
if(offset==(int)strlen(input)) break;
input+=offset;
offset=0;
} else {
++offset;
}
}
/*
* If the last word isn't punctuation, then replace it with a
* full-stop character.
*/
if(isalnum(words->entry[words->size-1].word[0])) {
if(words->entry==NULL)
words->entry=(STRING *)malloc((words->size+1)*sizeof(STRING));
else
words->entry=(STRING *)realloc(words->entry, (words->size+1)*sizeof(STRING));
if(words->entry==NULL) {
error("make_words", "Unable to reallocate dictionary");
return;
}
words->entry[words->size].length=1;
words->entry[words->size].word=".";
++words->size;
}
else if(strchr("!.?", words->entry[words->size-1].word[words->entry[words->size-1].length-1])==NULL) {
words->entry[words->size-1].length=1;
words->entry[words->size-1].word=".";
}
return;
}
/*---------------------------------------------------------------------------*/
/*
* Function: Boundary
*
* Purpose: Return whether or not a word boundary exists in a string
* at the specified location.
*/
bool boundary(char *string, int position)
{
if(position==0)
return(FALSE);
if(position==(int)strlen(string))
return(TRUE);
if(
(string[position]=='\'')&&
(isalpha(string[position-1])!=0)&&
(isalpha(string[position+1])!=0)
)
return(FALSE);
if(
(position>1)&&
(string[position-1]=='\'')&&
(isalpha(string[position-2])!=0)&&
(isalpha(string[position])!=0)
)
return(FALSE);
if(
(isalpha(string[position])!=0)&&
(isalpha(string[position-1])==0)
)
return(TRUE);
if(
(isalpha(string[position])==0)&&
(isalpha(string[position-1])!=0)
)
return(TRUE);
if(isdigit(string[position])!=isdigit(string[position-1]))
return(TRUE);
return(FALSE);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Make_Greeting
*
* Purpose: Put some special words into the dictionary so that the
* program will respond as if to a new judge.
libmegahal.c view on Meta::CPAN
int symbol;
symbol=find_word(model->dictionary, word);
if(symbol==0) return;
if(isalnum(word.word[0])==0) return;
symbol=find_word(aux, word);
if(symbol==0) return;
add_word(keys, word);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Reply
*
* Purpose: Generate a dictionary of reply words appropriate to the
* given dictionary of keywords.
*/
DICTIONARY *reply(MODEL *model, DICTIONARY *keys)
{
static DICTIONARY *replies=NULL;
register int i;
int symbol;
bool start=TRUE;
if(replies==NULL) replies=new_dictionary();
free_dictionary(replies);
/*
* Start off by making sure that the model's context is empty.
*/
initialize_context(model);
model->context[0]=model->forward;
used_key=FALSE;
/*
* Generate the reply in the forward direction.
*/
while(TRUE) {
/*
* Get a random symbol from the current context.
*/
if(start==TRUE) symbol=seed(model, keys);
else symbol=babble(model, keys, replies);
if((symbol==0)||(symbol==1)) break;
start=FALSE;
/*
* Append the symbol to the reply dictionary.
*/
if(replies->entry==NULL)
replies->entry=(STRING *)malloc((replies->size+1)*sizeof(STRING));
else
replies->entry=(STRING *)realloc(replies->entry, (replies->size+1)*sizeof(STRING));
if(replies->entry==NULL) {
error("reply", "Unable to reallocate dictionary");
return(NULL);
}
replies->entry[replies->size].length=
model->dictionary->entry[symbol].length;
replies->entry[replies->size].word=
model->dictionary->entry[symbol].word;
replies->size+=1;
/*
* Extend the current context of the model with the current symbol.
*/
update_context(model, symbol);
}
/*
* Start off by making sure that the model's context is empty.
*/
initialize_context(model);
model->context[0]=model->backward;
/*
* Re-create the context of the model from the current reply
* dictionary so that we can generate backwards to reach the
* beginning of the string.
*/
if(replies->size>0) for(i=MIN(replies->size-1, model->order); i>=0; --i) {
symbol=find_word(model->dictionary, replies->entry[i]);
update_context(model, symbol);
}
/*
* Generate the reply in the backward direction.
*/
while(TRUE) {
/*
* Get a random symbol from the current context.
*/
symbol=babble(model, keys, replies);
if((symbol==0)||(symbol==1)) break;
/*
* Prepend the symbol to the reply dictionary.
*/
if(replies->entry==NULL)
replies->entry=(STRING *)malloc((replies->size+1)*sizeof(STRING));
else
replies->entry=(STRING *)realloc(replies->entry, (replies->size+1)*sizeof(STRING));
if(replies->entry==NULL) {
error("reply", "Unable to reallocate dictionary");
return(NULL);
}
/*
* Shuffle everything up for the prepend.
*/
for(i=replies->size; i>0; --i) {
replies->entry[i].length=replies->entry[i-1].length;
replies->entry[i].word=replies->entry[i-1].word;
}
replies->entry[0].length=model->dictionary->entry[symbol].length;
replies->entry[0].word=model->dictionary->entry[symbol].word;
replies->size+=1;
/*
* Extend the current context of the model with the current symbol.
*/
update_context(model, symbol);
}
return(replies);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Evaluate_Reply
*
* Purpose: Measure the average surprise of keywords relative to the
* language model.
*/
float evaluate_reply(MODEL *model, DICTIONARY *keys, DICTIONARY *words)
{
register int i;
register int j;
int symbol;
float probability;
int count;
float entropy=(float)0.0;
TREE *node;
int num=0;
if(words->size<=0) return((float)0.0);
initialize_context(model);
model->context[0]=model->forward;
for(i=0; i<words->size; ++i) {
symbol=find_word(model->dictionary, words->entry[i]);
if(find_word(keys, words->entry[i])!=0) {
probability=(float)0.0;
count=0;
++num;
for(j=0; j<model->order; ++j) if(model->context[j]!=NULL) {
node=find_symbol(model->context[j], symbol);
probability+=(float)(node->count)/
(float)(model->context[j]->usage);
++count;
}
if(count>0.0) entropy-=(float)log(probability/(float)count);
}
update_context(model, symbol);
}
initialize_context(model);
model->context[0]=model->backward;
for(i=words->size-1; i>=0; --i) {
symbol=find_word(model->dictionary, words->entry[i]);
if(find_word(keys, words->entry[i])!=0) {
probability=(float)0.0;
count=0;
++num;
for(j=0; j<model->order; ++j) if(model->context[j]!=NULL) {
node=find_symbol(model->context[j], symbol);
probability+=(float)(node->count)/
(float)(model->context[j]->usage);
++count;
}
if(count>0.0) entropy-=(float)log(probability/(float)count);
}
update_context(model, symbol);
}
if(num>=8) entropy/=(float)sqrt(num-1);
if(num>=16) entropy/=(float)num;
return(entropy);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Make_Output
*
* Purpose: Generate a string from the dictionary of reply words.
*/
char *make_output(DICTIONARY *words)
{
static char *output=NULL;
register int i;
register int j;
int length;
static char *output_none=NULL;
if(output_none==NULL) output_none=malloc(40);
if(output==NULL) {
output=(char *)malloc(sizeof(char));
if(output==NULL) {
error("make_output", "Unable to allocate output");
return(output_none);
}
}
if(words->size==0) {
if(output_none!=NULL)
strcpy(output_none, "I am utterly speechless!");
return(output_none);
}
length=1;
for(i=0; i<words->size; ++i) length+=words->entry[i].length;
output=(char *)realloc(output, sizeof(char)*length);
if(output==NULL) {
error("make_output", "Unable to reallocate output.");
if(output_none!=NULL)
strcpy(output_none, "I forgot what I was going to say!");
return(output_none);
}
length=0;
for(i=0; i<words->size; ++i)
for(j=0; j<words->entry[i].length; ++j)
output[length++]=words->entry[i].word[j];
output[length]='\0';
return(output);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Babble
*
* Purpose: Return a random symbol from the current context, or a
* zero symbol identifier if we've reached either the
* start or end of the sentence. Select the symbol based
* on probabilities, favouring keywords. In all cases,
* use the longest available context to choose the symbol.
*/
int babble(MODEL *model, DICTIONARY *keys, DICTIONARY *words)
{
TREE *node;
register int i;
int count;
int symbol;
/*
* Select the longest available context.
*/
for(i=0; i<=model->order; ++i)
if(model->context[i]!=NULL)
node=model->context[i];
if(node->branch==0) return(0);
/*
* Choose a symbol at random from this context.
*/
i=rnd(node->branch);
count=rnd(node->usage);
while(count>=0) {
/*
* If the symbol occurs as a keyword, then use it. Only use an
* auxilliary keyword if a normal keyword has already been used.
*/
symbol=node->tree[i]->symbol;
if(
(find_word(keys, model->dictionary->entry[symbol])!=0)&&
((used_key==TRUE)||
(find_word(aux, model->dictionary->entry[symbol])==0))&&
(word_exists(words, model->dictionary->entry[symbol])==FALSE)
) {
used_key=TRUE;
break;
}
count-=node->tree[i]->count;
i=(i>=(node->branch-1))?0:i+1;
}
return(symbol);
}
/*---------------------------------------------------------------------------*/
libmegahal.c view on Meta::CPAN
/*
* Function: New_Swap
*
* Purpose: Allocate a new swap structure.
*/
SWAP *new_swap(void)
{
SWAP *list;
list=(SWAP *)malloc(sizeof(SWAP));
if(list==NULL) {
error("new_swap", "Unable to allocate swap");
return(NULL);
}
list->size=0;
list->from=NULL;
list->to=NULL;
return(list);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Add_Swap
*
* Purpose: Add a new entry to the swap structure.
*/
void add_swap(SWAP *list, char *s, char *d)
{
list->size+=1;
if(list->from==NULL) {
list->from=(STRING *)malloc(sizeof(STRING));
if(list->from==NULL) {
error("add_swap", "Unable to allocate list->from");
return;
}
}
if(list->to==NULL) {
list->to=(STRING *)malloc(sizeof(STRING));
if(list->to==NULL) {
error("add_swap", "Unable to allocate list->to");
return;
}
}
list->from=(STRING *)realloc(list->from, sizeof(STRING)*(list->size));
if(list->from==NULL) {
error("add_swap", "Unable to reallocate from");
return;
}
list->to=(STRING *)realloc(list->to, sizeof(STRING)*(list->size));
if(list->to==NULL) {
error("add_swap", "Unable to reallocate to");
return;
}
list->from[list->size-1].length=strlen(s);
list->from[list->size-1].word=strdup(s);
list->to[list->size-1].length=strlen(d);
list->to[list->size-1].word=strdup(d);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Initialize_Swap
*
* Purpose: Read a swap structure from a file.
*/
SWAP *initialize_swap(char *filename)
{
SWAP *list;
FILE *file=NULL;
char buffer[1024];
char *from;
char *to;
list=new_swap();
if(filename==NULL) return(list);
file=fopen(filename, "r");
if(file==NULL) return(list);
while(!feof(file)) {
if(fgets(buffer, 1024, file)==NULL) break;
if(buffer[0]=='#') continue;
from=strtok(buffer, "\t ");
to=strtok(NULL, "\t \n#");
add_swap(list, from, to);
}
fclose(file);
return(list);
}
/*---------------------------------------------------------------------------*/
void free_swap(SWAP *swap)
{
register int i;
if(swap==NULL) return;
for(i=0; i<swap->size; ++i) {
free_word(swap->from[i]);
free_word(swap->to[i]);
}
free(swap->from);
free(swap->to);
free(swap);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Initialize_List
*
* Purpose: Read a dictionary from a file.
*/
DICTIONARY *initialize_list(char *filename)
{
DICTIONARY *list;
FILE *file=NULL;
STRING word;
char *string;
char buffer[1024];
list=new_dictionary();
if(filename==NULL) return(list);
file=fopen(filename, "r");
if(file==NULL) return(list);
while(!feof(file)) {
if(fgets(buffer, 1024, file)==NULL) break;
if(buffer[0]=='#') continue;
string=strtok(buffer, "\t \n#");
if((string!=NULL)&&(strlen(string)>0)) {
word.length=strlen(string);
word.word=strdup(buffer);
add_word(list, word);
}
}
fclose(file);
return(list);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Delay
*
* Purpose: Display the string to stdout as if it was typed by a human.
*/
void delay(char *string)
{
register int i;
/*
* Don't simulate typing if the feature is turned off
*/
if(typing_delay==FALSE) {
fprintf(stdout, string);
return;
}
/*
* Display the entire string, one character at a time
*/
for(i=0; i<(int)strlen(string)-1; ++i) typein(string[i]);
usleep((D_THINK+rnd(V_THINK)-rnd(V_THINK))/2);
typein(string[i]);
}
/*---------------------------------------------------------------------------*/
/*
* Function: Typein
*
* Purpose: Display a character to stdout as if it was typed by a human.
*/
void typein(char c)
{
/*
* Standard keyboard delay
*/
usleep(D_KEY+rnd(V_KEY)-rnd(V_KEY));
fprintf(stdout, "%c", c);
fflush(stdout);
/*
* A random thinking delay
*/
if((!isalnum(c))&&((rnd(100))<P_THINK))
usleep(D_THINK+rnd(V_THINK)-rnd(V_THINK));
}
/*---------------------------------------------------------------------------*/
libmegahal.c view on Meta::CPAN
free_model(*model);
free_words(ban);
free_dictionary(ban);
free_words(aux);
free_dictionary(aux);
free_words(grt);
free_dictionary(grt);
free_swap(swp);
/*
* Create a language model.
*/
*model=new_model(order);
/*
* Train the model on a text if one exists
*/
sprintf(filename, "%s%smegahal.brn", directory, SEP);
if(load_model(filename, *model)==FALSE) {
sprintf(filename, "%s%smegahal.trn", directory, SEP);
train(*model, filename);
}
/*
* Read a dictionary containing banned keywords, auxiliary keywords,
* greeting keywords and swap keywords
*/
sprintf(filename, "%s%smegahal.ban", directory, SEP);
ban=initialize_list(filename);
sprintf(filename, "%s%smegahal.aux", directory, SEP);
aux=initialize_list(filename);
sprintf(filename, "%s%smegahal.grt", directory, SEP);
grt=initialize_list(filename);
sprintf(filename, "%s%smegahal.swp", directory, SEP);
swp=initialize_swap(filename);
}
/*---------------------------------------------------------------------------*/
void change_personality(DICTIONARY *command, int position, MODEL **model)
{
if(directory == NULL) {
directory = (char *)malloc(sizeof(char)*(strlen(DEFAULT)+1));
if(directory == NULL) {
error("change_personality", "Unable to allocate directory");
} else {
strcpy(directory, DEFAULT);
}
}
if(last == NULL) {
last = strdup(directory);
}
if((command == NULL)||((position+2)>=command->size)) {
/* no dir set, so we leave it to whatever was set above */
} else {
directory=(char *)realloc(directory,
sizeof(char)*(command->entry[position+2].length+1));
if(directory == NULL)
error("change_personality", "Unable to allocate directory");
strncpy(directory, command->entry[position+2].word,
command->entry[position+2].length);
directory[command->entry[position+2].length]='\0';
}
load_personality(model);
}
/*---------------------------------------------------------------------------*/
void free_words(DICTIONARY *words)
{
unsigned int i;
if(words == NULL) return;
if(words->entry != NULL)
for(i=0; i<words->size; ++i) free_word(words->entry[i]);
}
/*---------------------------------------------------------------------------*/
void free_word(STRING word)
{
free(word.word);
}
/*===========================================================================*/
/*
* $Log: megahal.c,v $
* Revision 1.6 2002/10/16 04:32:53 davidw
* * megahal.c (change_personality): [ 541667 ] Added patch from Andrew
* Burke to rework logic in change_personality.
*
* * megahal.c: Trailing white space cleanup.
*
* * python-interface.c: [ 546397 ] Change python include path to a more
* recent version. This should really be fixed by using some of
* Python's build automation tools.
*
* Revision 1.5 2000/11/08 11:07:11 davidw
* Moved README to docs directory.
*
* Changes to debian files.
*
* Revision 1.4 2000/09/07 21:51:12 davidw
* Created some library functions that I think are workable, and moved
* everything else into megahal.c as static variables/functions.
*
* Revision 1.3 2000/09/07 11:43:43 davidw
* Started hacking:
*
* Reduced makefile targets, eliminating non-Linux OS's. There should be
* a cleaner way to do this.
*
* Added Tcl and Python C level interfaces.
*
* Revision 1.23 1998/05/19 03:02:02 hutch
* Removed a small malloc() bug, and added a progress display for
* generate_reply().
*
* Revision 1.22 1998/04/24 03:47:03 hutch
( run in 1.061 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )