AI-MegaHAL
view release on metacpan or search on metacpan
libmegahal.c view on Meta::CPAN
* PC (Windows '95)
* ----------------
* Jason Hutchens (hutch@ciips.ee.uwa.edu.au)
*
* PPC (Linux)
* -----------
* Lucas Vergnettes (Lucasv@sdf.lonestar.org)
*
* SGI (Irix)
* ----------
* Jason Hutchens (hutch@ciips.ee.uwa.edu.au)
*
* Sun (SunOS)
* -----------
* Jason Hutchens (hutch@ciips.ee.uwa.edu.au)
*/
/*===========================================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#ifndef _MSC_VER
#include <unistd.h>
//#include <getopt.h>
#endif
#if !defined(AMIGA) && !defined(__mac_os) && !defined(__FreeBSD__) && !defined(__APPLE__)
// FreeBSD malloc.h is empty and gives error
// Tested on FreeBSD 5.4
#include <malloc.h>
#endif
#include <string.h>
#include <signal.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
#if defined(__mac_os)
#include <types.h>
#include <Speech.h>
#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;
static DICTIONARY *ban=NULL;
static DICTIONARY *aux=NULL;
static DICTIONARY *fin=NULL;
static DICTIONARY *grt=NULL;
static SWAP *swp=NULL;
static bool used_key;
static char *directory=NULL;
static char *last=NULL;
static COMMAND command[] = {
{ { 4, "QUIT" }, "quits the program and saves MegaHAL's brain", QUIT },
{ { 4, "EXIT" }, "exits the program *without* saving MegaHAL's brain", EXIT },
{ { 4, "SAVE" }, "saves the current MegaHAL brain", SAVE },
{ { 5, "DELAY" }, "toggles MegaHAL's typing delay (off by default)", DELAY },
{ { 6, "SPEECH" }, "toggles MegaHAL's speech (off by default)", SPEECH },
{ { 6, "VOICES" }, "list available voices for speech", VOICELIST },
{ { 5, "VOICE" }, "switches to voice specified", VOICE },
{ { 5, "BRAIN" }, "change to another MegaHAL personality", BRAIN },
{ { 4, "HELP" }, "displays this message", HELP },
{ { 5, "QUIET" }, "toggles MegaHAL's responses (on by default)",QUIET},
/*
{ { 5, "STATS" }, "Display stats", STATS},
{ { 5, "STATS-SESSION" }, "Display stats for this session only",STATS_SESSION},
{ { 5, "STATS-ALL" },"Display stats for the whole lifetime",STATS-ALL},
*/
};
#ifdef AMIGA
struct Locale *_AmigaLocale;
#endif
#ifdef __mac_os
Boolean gSpeechExists = false;
SpeechChannel gSpeechChannel = nil;
#endif
/* FIXME - these need to be static */
static void add_aux(MODEL *, DICTIONARY *, STRING);
static void add_key(MODEL *, DICTIONARY *, STRING);
static void add_node(TREE *, TREE *, int);
static void add_swap(SWAP *, char *, char *);
static TREE *add_symbol(TREE *, BYTE2);
static BYTE2 add_word(DICTIONARY *, STRING);
static int babble(MODEL *, DICTIONARY *, DICTIONARY *);
static bool boundary(char *, int);
static void capitalize(char *);
static void changevoice(DICTIONARY *, int);
static void change_personality(DICTIONARY *, int, MODEL **);
static void delay(char *);
static void die(int);
static bool dissimilar(DICTIONARY *, DICTIONARY *);
static void error(char *, char *, ...);
static float evaluate_reply(MODEL *, DICTIONARY *, DICTIONARY *);
static COMMAND_WORDS execute_command(DICTIONARY *, int *);
static void exithal(void);
static TREE *find_symbol(TREE *, int);
static TREE *find_symbol_add(TREE *, int);
static BYTE2 find_word(DICTIONARY *, STRING);
static char *generate_reply(MODEL *, DICTIONARY *);
static void help(void);
static void ignore(int);
static bool initialize_error(char *);
#ifdef __mac_os
static bool initialize_speech(void);
#endif
static bool initialize_status(char *);
static void learn(MODEL *, DICTIONARY *);
static void listvoices(void);
static void make_greeting(DICTIONARY *);
static void make_words(char *, DICTIONARY *);
static DICTIONARY *new_dictionary(void);
static char *read_input(char *);
static void save_model(char *, MODEL *);
#ifdef __mac_os
static char *strdup(const char *);
#endif
static void upper(char *);
static void write_input(char *);
static void write_output(char *);
#if defined(DOS) || defined(__mac_os)
static void usleep(int);
#endif
#if defined(_MSC_VER) || defined(__MINGW32_VERSION)
#include <windows.h>
#define usleep(i) Sleep(i)
#endif
static char *format_output(char *);
static void free_dictionary(DICTIONARY *);
static void free_model(MODEL *);
static void free_tree(TREE *);
static void free_word(STRING);
static void free_words(DICTIONARY *);
static void initialize_context(MODEL *);
static void initialize_dictionary(DICTIONARY *);
static DICTIONARY *initialize_list(char *);
static SWAP *initialize_swap(char *);
static void load_dictionary(FILE *, DICTIONARY *);
static bool load_model(char *, MODEL *);
static void load_personality(MODEL **);
static void load_tree(FILE *, TREE *);
static void load_word(FILE *, DICTIONARY *);
static DICTIONARY *make_keywords(MODEL *, DICTIONARY *);
static char *make_output(DICTIONARY *);
static MODEL *new_model(int);
static TREE *new_node(void);
static SWAP *new_swap(void);
static bool print_header(FILE *);
static bool progress(char *, int, int);
static DICTIONARY *reply(MODEL *, DICTIONARY *);
static void save_dictionary(FILE *, DICTIONARY *);
libmegahal.c view on Meta::CPAN
{
if (log != 0)
write_input(input); /* log input if so desired */
upper(input);
make_words(input, words);
learn(model, words);
}
/*
megahal_initial_greeting --
This function returns an initial greeting. It can be used to start
Megahal conversations, but it isn't necessary.
*/
char *megahal_initial_greeting(void)
{
char *output;
make_greeting(greets);
output = generate_reply(model, greets);
return output;
}
/*
megahal_output --
This function pretty prints output.
Wrapper function to have things in the right namespace.
*/
void megahal_output(char *output)
{
if(!quiet)
write_output(output);
}
/*
megahal_input --
Get a string from stdin, using a prompt.
*/
char *megahal_input(char *prompt)
{
if (noprompt)
return read_input("");
else
return read_input(prompt);
}
/*
megahal_command --
Check to see if input is a megahal command, and if so, act upon it.
Returns 1 if it is a command, 0 if it is not.
*/
int megahal_command(char *input)
{
int position = 0;
char *output;
make_words(input,words);
switch(execute_command(words, &position)) {
case EXIT:
exithal();
break;
case QUIT:
save_model("megahal.brn", model);
exithal();
break;
case SAVE:
save_model("megahal.brn", model);
break;
case DELAY:
typing_delay=!typing_delay;
printf("MegaHAL typing is now %s.\n", typing_delay?"on":"off");
return 1;
case SPEECH:
speech=!speech;
printf("MegaHAL speech is now %s.\n", speech?"on":"off");
return 1;
case HELP:
help();
return 1;
case VOICELIST:
listvoices();
return 1;
case VOICE:
changevoice(words, position);
return 1;
case BRAIN:
change_personality(words, position, &model);
make_greeting(greets);
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.
libmegahal.c view on Meta::CPAN
#ifdef __mac_os
bool initialize_speech(void)
{
bool speechExists = false;
long response;
OSErr err;
err = Gestalt(gestaltSpeechAttr, &response);
if(!err) {
if(response & (1L << gestaltSpeechMgrPresent)) {
speechExists = true;
}
}
return speechExists;
}
#endif
/*---------------------------------------------------------------------------*/
/*
* Function: changevoice
*
* Purpose: change voice of speech output.
*/
void changevoice(DICTIONARY* words, int position)
{
#ifdef __mac_os
register int i, index;
STRING word={ 1, "#" };
char buffer[80];
VoiceSpec voiceSpec;
VoiceDescription info;
short count, voiceCount;
unsigned char* temp;
OSErr err;
/*
* If there is less than 4 words, no voice specified.
*/
if(words->size<=4) return;
for(i=0; i<words->size-4; ++i)
if(wordcmp(word, words->entry[i])==0) {
err = CountVoices(&voiceCount);
if (!err && voiceCount) {
for (count = 1; count <= voiceCount; count++) {
err = GetIndVoice(count, &voiceSpec);
if (err) continue;
err = GetVoiceDescription(&voiceSpec, &info,
sizeof(VoiceDescription));
if (err) continue;
for (temp= info.name; *temp; temp++) {
if (*temp == ' ')
*temp = '_';
}
/*
* skip command and get voice name
*/
index = i + 3;
strcpy(buffer, words->entry[index].word);
c2pstr(buffer);
// compare ignoring case
if (EqualString((StringPtr)buffer, info.name, false, false)) {
if (gSpeechChannel) {
StopSpeech(gSpeechChannel);
DisposeSpeechChannel(gSpeechChannel);
gSpeechChannel = nil;
}
err = NewSpeechChannel(&voiceSpec, &gSpeechChannel);
if (!err) {
p2cstr((StringPtr)buffer);
printf("Now using %s voice\n", buffer);
c2pstr(buffer);
err = SpeakText(gSpeechChannel, &buffer[1], buffer[0]);
}
}
}
}
}
#endif
}
/*---------------------------------------------------------------------------*/
/*
* Function: listvoices
*
* Purpose: Display the names of voices for speech output.
*/
void listvoices(void)
{
#ifdef __mac_os
VoiceSpec voiceSpec;
VoiceDescription info;
short count, voiceCount;
unsigned char* temp;
OSErr err;
if(gSpeechExists) {
err = CountVoices(&voiceCount);
if (!err && voiceCount) {
for (count = 1; count <= voiceCount; count++) {
err = GetIndVoice(count, &voiceSpec);
if (err) continue;
err = GetVoiceDescription(&voiceSpec, &info,
sizeof(VoiceDescription));
if (err) continue;
p2cstr(info.name);
for (temp= info.name; *temp; temp++)
if (*temp == ' ')
*temp = '_';
printf("%s\n",info.name);
}
}
}
libmegahal.c view on Meta::CPAN
#endif
}
/*---------------------------------------------------------------------------*/
/*
* Function: Progress
*
* Purpose: Display a progress indicator as a percentage.
*/
bool progress(char *message, int done, int total)
{
static int last=0;
static bool first=FALSE;
/*
* We have already hit 100%, and a newline has been printed, so nothing
* needs to be done.
*/
if((done*100/total==100)&&(first==FALSE)) return(TRUE);
/*
* Nothing has changed since the last time this function was called,
* so do nothing, unless it's the first time!
*/
if(done*100/total==last) {
if((done==0)&&(first==FALSE)) {
// fprintf(stderr, "%s: %3d%%", message, done*100/total);
first=TRUE;
}
return(TRUE);
}
/*
* Erase what we printed last time, and print the new percentage.
*/
last=done*100/total;
//if(done>0) fprintf(stderr, "%c%c%c%c", 8, 8, 8, 8);
//fprintf(stderr, "%3d%%", done*100/total);
/*
* We have hit 100%, so reset static variables and print a newline.
*/
if(last==100) {
first=FALSE;
last=0;
//fprintf(stderr, "\n");
}
return(TRUE);
}
/*---------------------------------------------------------------------------*/
void help(void)
{
int j;
for(j=0; j<COMMAND_SIZE; ++j) {
printf("#%-7s: %s\n", command[j].word.word, command[j].helpstring);
}
}
/*---------------------------------------------------------------------------*/
void load_personality(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("load_personality","Unable to allocate filename");
/*
* Check to see if the brain exists
*/
if(strcmp(directory, DEFAULT)!=0) {
sprintf(filename, "%s%smegahal.brn", directory, SEP);
file=fopen(filename, "r");
if(file==NULL) {
sprintf(filename, "%s%smegahal.trn", directory, SEP);
file=fopen(filename, "r");
if(file==NULL) {
fprintf(stdout, "Unable to change MegaHAL personality to \"%s\".\n"
"Reverting to MegaHAL personality \"%s\".\n", directory, last);
free(directory);
directory=strdup(last);
return;
}
}
fclose(file);
fprintf(stdout, "Changing to MegaHAL personality \"%s\".\n", directory);
}
/*
* Free the current personality
*/
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
* Quick bug fix to get sunos version to work.
*
* Revision 1.21 1998/04/24 03:39:51 hutch
* Added the BRAIN command, to allow user to change MegaHAL personalities
* on the fly.
*
* Revision 1.20 1998/04/22 07:12:37 hutch
* A few small changes to get the DOS version to compile.
*
* Revision 1.19 1998/04/21 10:10:56 hutch
* Fixed a few little errors.
*
* Revision 1.18 1998/04/06 08:02:01 hutch
* Added debugging stuff, courtesy of Paul Baxter.
*
* Revision 1.17 1998/04/02 01:34:20 hutch
* Added the help function and fixed a few errors.
*
* Revision 1.16 1998/04/01 05:42:57 hutch
* Incorporated Mac code, including speech synthesis, and attempted
* to tidy up the code for multi-platform support.
*
* Revision 1.15 1998/03/27 03:43:15 hutch
* Added AMIGA specific changes, thanks to Dag Agren.
*
* Revision 1.14 1998/02/20 06:40:13 hutch
* Tidied up transcript file format.
*
* Revision 1.13 1998/02/20 06:26:19 hutch
* Fixed random number generator and Seed() function (thanks to Mark
* Tarrabain), removed redundant code left over from the Loebner entry,
* prettied things up a little and destroyed several causes of memory
* leakage (although probably not all).
*
* Revision 1.12 1998/02/04 02:55:11 hutch
* Fixed up memory allocation error which caused SunOS versions to crash.
*
* Revision 1.11 1998/01/22 03:16:30 hutch
* Fixed several memory leaks, and the frustrating bug in the
* Write_Input routine.
*
* Revision 1.10 1998/01/19 06:44:36 hutch
* Fixed MegaHAL to compile under Linux with a small patch credited
* to Joey Hess (joey@kitenet.net). MegaHAL may now be included as
* part of the Debian Linux distribution.
*
* Revision 1.9 1998/01/19 06:37:32 hutch
* Fixed a minor bug with end-of-sentence punctuation.
*
* Revision 1.8 1997/12/24 03:17:01 hutch
* More bug fixes, and hopefully the final contest version!
*
* Revision 1.7 1997/12/22 13:18:09 hutch
* A few more bug fixes, and non-repeating implemented.
*
* Revision 1.6 1997/12/22 04:27:04 hutch
* A few minor bug fixes.
*
* Revision 1.5 1997/12/15 04:35:59 hutch
* Final Loebner version!
*
* Revision 1.4 1997/12/11 05:45:29 hutch
* The almost finished version.
*
( run in 1.009 second using v1.01-cache-2.11-cpan-f56aa216473 )