AI-MegaHAL

 view release on metacpan or  search on metacpan

Makefile.PL  view on Meta::CPAN

# -*- mode: perl; c-basic-offset: 4; indent-tabs-mode: nil; -*-
use ExtUtils::MakeMaker;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
WriteMakefile1(
    META_MERGE => {
        resources => {
            repository => 'http://github.com/chorny/AI-MegaHAL',
            keywords => ['megahal','chat',],
        },
    },

MegaHAL.xs  view on Meta::CPAN


MODULE = AI::MegaHAL		PACKAGE = AI::MegaHAL
PROTOTYPES: DISABLE

double
constant(name,arg)
	char *		name
	int		arg

void
megahal_setnoprompt ()

void
megahal_setnowrap ()

void
megahal_setnobanner ()

void
megahal_seterrorfile(filename)
        char*   filename

void
megahal_setstatusfile(filename)
        char*   filename

void
megahal_initialize()

char*
megahal_initial_greeting()

int
megahal_command(input)

lib/AI/MegaHAL.pm  view on Meta::CPAN

require DynaLoader;
require Exporter;

use AutoLoader;
use Carp;

use strict;

use vars qw(@EXPORT @ISA $VERSION $AUTOLOAD);

@EXPORT = qw(megahal_setnoprompt
	     megahal_setnowrap
	     megahal_setnobanner
	     megahal_seterrorfile
	     megahal_setstatusfile
	     megahal_initialize
	     megahal_initial_greeting
	     megahal_command
	     megahal_do_reply
	     megahal_learn
	     megahal_output
	     megahal_input
	     megahal_cleanup);

@ISA = qw(Exporter DynaLoader);

lib/AI/MegaHAL.pm  view on Meta::CPAN

    # Make sure that we can find a brain or a training file somewhere
    # else die with an error.
    my $path = $args{'Path'} || ".";
    if(-e "$path/megahal.brn" || -e "$path/megahal.trn") {
	chdir($path) || die("Error: chdir: $!\n");
    } else {
	die("Error: unable to locate megahal.brn or megahal.trn\n");
    }

    # Set some of the options that may have been passed to us.
    megahal_setnobanner() if(! $args{'Banner'});
    megahal_setnowrap()   if(! $args{'Wrap'});
    megahal_setnoprompt() if(! $args{'Prompt'});

    # This flag indicates whether or not we should automatically save
    # our brain when the object goes out of scope.
    $self->{'AutoSave'} = $args{'AutoSave'};

    # Initialize ourselves.
    $self->_initialize();

    return $self;
}

libmegahal.c  view on Meta::CPAN

 *
 *		WWW:		http://megahal.sourceforge.net
 *
 *		Compilation Notes
 *		=================
 *
 *		When compiling, be sure to link with the maths library so that the
 *		log() function can be found.
 *
 *		On the Macintosh, add the library SpeechLib to your project.  It is
 *		very important that you set the attributes to Import Weak.  You can
 *		do this by selecting the lib and then use Project Inspector from the
 *		Window menu.
 *
 *		CREDITS
 *		=======
 *
 *		Amiga (AmigaOS)
 *		---------------
 *		Dag Agren (dagren@ra.abo.fi)
 *

libmegahal.c  view on Meta::CPAN

static void train(MODEL *, char *);
static void typein(char);
static void update_context(MODEL *, int);
static void update_model(MODEL *, int);
static bool warn(char *, char *, ...);
static int wordcmp(STRING, STRING);
static bool word_exists(DICTIONARY *, STRING);
static int rnd(int);


/* Function: setnoprompt

   Purpose: Set noprompt variable.

 */
void megahal_setnoprompt(void)
{
    noprompt = TRUE;
}

void megahal_setnowrap (void)
{
    nowrap = TRUE;
}
void megahal_setnobanner (void)
{
    nobanner = TRUE;
}

void megahal_seterrorfile(char *filename)
{
    errorfilename = filename;
}
void megahal_setstatusfile(char *filename)
{
    statusfilename = filename;
}

/*
   megahal_initialize --

   Initialize various brains and files.

   Results:

libmegahal.c  view on Meta::CPAN

     *		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;

libmegahal.c  view on Meta::CPAN


/*---------------------------------------------------------------------------*/

/*
 *    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;

libmegahal.c  view on Meta::CPAN

    /*
     *		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));

libmegahal.c  view on Meta::CPAN

char *generate_reply(MODEL *model, DICTIONARY *words)
{
    static DICTIONARY *dummy=NULL;
    DICTIONARY *replywords;
    DICTIONARY *keywords;
    float surprise;
    float max_surprise;
    char *output;
    static char *output_none=NULL;
    int count;
    int basetime;
    int timeout = TIMEOUT;

    /*
     *		Create an array of keywords from the words in the user's input
     */
    keywords=make_keywords(model, words);

    /*
     *		Make sure some sort of reply exists
     */

libmegahal.c  view on Meta::CPAN

    if(dummy == NULL) dummy = new_dictionary();
    replywords = reply(model, dummy);
    if(dissimilar(words, replywords) == TRUE) output = make_output(replywords);

    /*
     *		Loop for the specified waiting period, generating and evaluating
     *		replies
     */
    max_surprise=(float)-1.0;
    count=0;
    basetime=time(NULL);
/*     progress("Generating reply", 0, 1);  */
    do {
	replywords=reply(model, keywords);
	surprise=evaluate_reply(model, keywords, replywords);
	++count;
	if((surprise>max_surprise)&&(dissimilar(words, replywords)==TRUE)) {
	    max_surprise=surprise;
	    output=make_output(replywords);
	}
/*  	progress(NULL, (time(NULL)-basetime),timeout); */
    } while((time(NULL)-basetime)<timeout);
    progress(NULL, 1, 1);

    /*
     *		Return the best answer we generated
     */
    return(output);
}

/*---------------------------------------------------------------------------*/

libmegahal.c  view on Meta::CPAN


    /*
     *    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);
}

libmegahal.c  view on Meta::CPAN

	} 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';
    }

megahal.h  view on Meta::CPAN

#ifndef MEGAHAL_H
#define MEGAHAL_H

void  megahal_setnoprompt ();
void  megahal_setnowrap ();
void  megahal_setnobanner ();
void  megahal_seterrorfile(char *filename);
void  megahal_setstatusfile(char *filename);
void  megahal_initialize();
char* megahal_initial_greeting();
int   megahal_command(char *input);
char* megahal_do_reply(char *input, int log);
void  megahal_learn(char *input, int log);
void  megahal_output(char *output);
char* megahal_input(char *prompt);
void  megahal_cleanup();

#endif

megahal.trn  view on Meta::CPAN

A rolling stone gathers no moss.
Time flies like an arrow.  Fruit flies like a banana.
Colourless green ideas sleep furiously.
I think, therefore I am.
The best way to remember your wife's birthday is to forget it once.
The most beautiful thing we can experience is the mysterious.
I never forget a face, but in your case I'll make an exception.
The older you get, the more you like to tell it like it used to be.
Comedy is simply a funny way of being serious.
If at first you don't succeed, don't take any more stupid chances.
To be upset about what you don't have is to waste what you do have.
Imagination is more important than knowledge.
Laugh, and the world laughs with you.  Cry, and I'll laugh anyway!
She sells sea shells by the sea shore.
My hovercraft is full of eels.
The meaning of life, the universe, and everything is 42.
#
#	Non-Sequiturs
#
I have absolutely no idea about that.  I really wish I had, though!
You know, I don't know what the hell you're babbling on about.

megahal.trn  view on Meta::CPAN

#
Abstract means theoretical rather than practical.
An accent is a local mode of pronunciation in speech.
An accordion is a small portable musical instrument with a keyboard and bellows.
An acid is a chemical compound that reacts with metals to form salts by releasing hydrogen.
An acronym is a word formed from the initials of other words.
An adhesive is a sticky substance.
An adult is a fully grown being.
An aircraft is a flying machine, a vessel which flies through the air rather than floats on water or travels along a road or rail.
Ale is an alcoholic drink made from malt and hops.
An algorithm is a set of rules.
An alphabet is an ordered series of letters used in language.
An atom is the smallest quantity of a chemical element which can enter into combination or take part in a chemical reaction.
Beer is a drink of fermented hops, malt and barley.
A bicycle is a two wheeled vehicle.
Cement is a mixture of chalk and clay used for building.
Chocolate is a confectionery made from cocoa beans.
A coma is a state of deep unconsciousness.
A compiler is a computer program that translates high level language code into machine language code.
A computer is a programmable electronic device.
Cooking is the art of preparing food for the table by subjecting it to heat in various ways.

megahal.trn  view on Meta::CPAN

A bard was a Celtic poet.
Beethoven was a German composer.
Benjamin Franklin was an American statesman and scientist.
Bruce Lee was a Chinese actor and expert in Kung Fu who popularised the martial arts in the west.
Bushrangers were Australian highwaymen, formerly escaped convicts.
Captain James Cook was an English sailor and explorer.
Charles Babbage was a British mathematician.  He designed an analytical engine which was the forerunner of the modern computer.
Charles Robert Darwin was an English naturalist.  He published his theory of evolution in a book entitled The Origin of Species.
A cretin is someone who suffers from the disease cretinism.
The druids were ancient Celtic priests.  Their group still exists today in secret, despite the existence of charlatan groups claiming to be druids.
Euclid was a Greek mathematician.  His book the Elements of Geometry set down how geometry was to be taught for the next 2000 years.
Galileo was an Italian scientist.  He discovered the ring of Saturn, Jupiter's 4 major satellites and the sun's spots.
George Orwell was an English writer.  He wrote Nineteen Eighty Four and Animal Farm.
Henry Ford was the founder of the Ford motor car company and the pioneer of the cheap motor car.
Homer was an ancient Greek poet.
James Watt was a Scottish inventor.  He invented the modern steam-engine.
Johann Sebastian Bach was a German composer.
Leonardo da Vinci was an Italian artist and scientist.  He recorded scientific studies in unpublished note books.
Neil Armstrong was the first man to step onto the moon.
Orville Wright was an American pioneer of flying.  Together with his brother he made the first controlled flight of an aeroplane.
Plato was an ancient Greek philosopher.



( run in 1.558 second using v1.01-cache-2.11-cpan-49f99fa48dc )