{
    "mode": "perldoc",
    "parameter": "Chatbot::Eliza",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Chatbot%3A%3AEliza/json",
    "generated": "2026-06-12T16:39:33Z",
    "synopsis": "use Chatbot::Eliza;\n$mybot = new Chatbot::Eliza;\n$mybot->commandinterface;\n# see below for details",
    "sections": {
        "NAME": {
            "content": "Chatbot::Eliza - A clone of the classic Eliza program\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Chatbot::Eliza;\n\n$mybot = new Chatbot::Eliza;\n$mybot->commandinterface;\n\n# see below for details\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This module implements the classic Eliza algorithm. The original Eliza program was written by\nJoseph Weizenbaum and described in the Communications of the ACM in 1966. Eliza is a mock\nRogerian psychotherapist. It prompts for user input, and uses a simple transformation algorithm\nto change user input into a follow-up question. The program is designed to give the appearance\nof understanding.\n\nThis program is a faithful implementation of the program described by Weizenbaum. It uses a\nsimplified script language (devised by Charles Hayden). The content of the script is the same as\nWeizenbaum's.\n\nThis module encapsulates the Eliza algorithm in the form of an object. This should make the\nfunctionality easy to incorporate in larger programs.\n",
            "subsections": []
        },
        "INSTALLATION": {
            "content": "The current version of Chatbot::Eliza.pm is available on CPAN:\n\nhttp://www.perl.com/CPAN/modules/by-module/Chatbot/\n\nTo install this package, just change to the directory which you created by untarring the\npackage, and type the following:\n\nperl Makefile.PL\nmake test\nmake\nmake install\n\nThis will copy Eliza.pm to your perl library directory for use by all perl scripts. You probably\nmust be root to do this, unless you have installed a personal copy of perl.\n",
            "subsections": []
        },
        "USAGE": {
            "content": "This is all you need to do to launch a simple Eliza session:\n\nuse Chatbot::Eliza;\n\n$mybot = new Chatbot::Eliza;\n$mybot->commandinterface;\n\nYou can also customize certain features of the session:\n\n$myotherbot = new Chatbot::Eliza;\n\n$myotherbot->name( \"Hortense\" );\n$myotherbot->debug( 1 );\n\n$myotherbot->commandinterface;\n\nThese lines set the name of the bot to be \"Hortense\" and turn on the debugging output.\n\nWhen creating an Eliza object, you can specify a name and an alternative scriptfile:\n\n$bot = new Chatbot::Eliza \"Brian\", \"myscript.txt\";\n\nYou can also use an anonymous hash to set these parameters. Any of the fields can be initialized\nusing this syntax:\n\n$bot = new Chatbot::Eliza {\nname       => \"Brian\",\nscriptfile => \"myscript.txt\",\ndebug      => 1,\npromptson => 1,\nmemoryon  => 0,\nmyrand     =>\nsub { my $N = defined $[0] ? $[0] : 1;  rand($N); },\n};\n\nIf you don't specify a script file, then the new object will be initialized with a default\nscript. The module contains this script within itself.\n\nYou can use any of the internal functions in a calling program. The code below takes an\narbitrary string and retrieves the reply from the Eliza object:\n\nmy $string = \"I have too many problems.\";\nmy $reply  = $mybot->transform( $string );\n\nYou can easily create two bots, each with a different script, and see how they interact:\n\nuse Chatbot::Eliza\n\nmy ($harry, $sally, $hesays, $shesays);\n\n$sally = new Chatbot::Eliza \"Sally\", \"histext.txt\";\n$harry = new Chatbot::Eliza \"Harry\", \"hertext.txt\";\n\n$hesays  = \"I am sad.\";\n\n# Seed the random number generator.\nsrand( time ^ ($$ + ($$ << 15)) );\n\nwhile (1) {\n$shesays = $sally->transform( $hesays );\nprint $sally->name, \": $shesays \\n\";\n\n$hesays  = $harry->transform( $shesays );\nprint $harry->name, \": $hesays \\n\";\n}\n\nMechanically, this works well. However, it critically depends on the actual script data. Having\ntwo mock Rogerian therapists talk to each other usually does not produce any sensible\nconversation, of course.\n\nAfter each call to the transform() method, the debugging output for that transformation is\nstored in a variable called $debugtext.\n\nmy $reply      = $mybot->transform( \"My foot hurts\" );\nmy $debugging  = $mybot->debugtext;\n\nThis feature always available, even if the instance's $debug variable is set to 0.\n\nCalling programs can specify their own random-number generators. Use this syntax:\n\n$chatbot = new Chatbot::Eliza;\n$chatbot->myrand(\nsub {\n#function goes here!\n}\n);\n\nThe custom random function should have the same prototype as perl's built-in rand() function.\nThat is, it should take a single (numeric) expression as a parameter, and it should return a\nfloating-point value between 0 and that number.\n\nWhat this code actually does is pass a reference to an anonymous subroutine (\"code reference\").\nMake sure you've read the perlref manpage for details on how code references actually work.\n\nIf you don't specify any custom rand function, then the Eliza object will just use the built-in",
            "subsections": [
                {
                    "name": "rand",
                    "content": ""
                }
            ]
        },
        "MAIN DATA MEMBERS": {
            "content": "Each Eliza object uses the following data structures to hold the script data in memory:\n\n%decomplist\n*Hash*: the set of keywords; *Values*: strings containing the decomposition rules.\n\n%reasmblist\n*Hash*: a set of values which are each the join of a keyword and a corresponding decomposition\nrule; *Values*: the set of possible reassembly statements for that keyword and decomposition\nrule.\n\n%reasmblistformemory\nThis structure is identical to %reasmblist, except that these rules are only invoked when a user\ncomment is being retrieved from memory. These contain comments such as \"Earlier you mentioned\nthat...,\" which are only appropriate for remembered comments. Rules in the script must be\nspecially marked in order to be included in this list rather than %reasmblist. The default\nscript only has a few of these rules.\n\n@memory\nA list of user comments which an Eliza instance is remembering for future use. Eliza does not\nremember everything, only some things. In this implementation, Eliza will only remember comments\nwhich match a decomposition rule which actually has reassembly rules that are marked with the\nkeyword \"reasmformemory\" rather than the normal \"reasmb\". The default script only has a few of\nthese.\n\n%keyranks\n*Hash*: the set of keywords; *Values*: the ranks for each keyword\n\n@quit\n\"quit\" words -- that is, words the user might use to try to exit the program.\n\n@initial\nPossible greetings for the beginning of the program.\n\n@final\nPossible farewells for the end of the program.\n\n%pre\n*Hash*: words which are replaced before any transformations; *Values*: the respective\nreplacement words.\n\n%post\n*Hash*: words which are replaced after the transformations and after the reply is constructed;\n*Values*: the respective replacement words.\n\n%synon\n*Hash*: words which are found in decomposition rules; *Values*: words which are treated just\nlike their corresponding synonyms during matching of decomposition rules.\n",
            "subsections": [
                {
                    "name": "Other data members",
                    "content": "There are several other internal data members. Hopefully these are sufficiently obvious that you\ncan learn about them just by reading the source code.\n"
                }
            ]
        },
        "METHODS": {
            "content": "new()\nmy $chatterbot = new Chatbot::Eliza;\n",
            "subsections": [
                {
                    "name": "new",
                    "content": "which in turn calls the parsescriptdata() method, which initializes the script data.\n\nmy $chatterbot = new Chatbot::Eliza 'Ahmad', 'myfile.txt';\n\nThe eliza object defaults to the name \"Eliza\", and it contains default script data within\nitself. However, using the syntax above, you can specify an alternative name and an alternative\nscript file.\n\nSee the method parsescriptdata(). for a description of the format of the script file.\n\ncommandinterface()\n$chatterbot->commandinterface;\n"
                },
                {
                    "name": "command_interface",
                    "content": "Eliza program.\n\nIf you want to design your own session format, then you can write your own while loop and your\nown functions for prompting for and reading user input, and use the transform() method to\ngenerate Eliza's responses. (*Note*: you do not need to invoke preprocess() and postprocess()\ndirectly, because these are invoked from within the transform() method.)\n\nBut if you're lazy and you want to skip all that, then just use commandinterface(). It's all\ndone for you.\n\nDuring an interactive session invoked using commandinterface(), you can enter the word \"debug\"\nto toggle debug mode on and off. You can also enter the keyword \"memory\" to invoke the\ndebugmemory() method and print out the contents of the Eliza instance's memory.\n\npreprocess()\n$string = preprocess($string);\n"
                },
                {
                    "name": "preprocess",
                    "content": "varieties in spelling, misspellings, contractions and the like.\n"
                },
                {
                    "name": "preprocess",
                    "content": "BEFORE any processing, and before a reassebly statement has been selected.\n\nIt uses the array %pre, which is created during the parse of the script.\n\npostprocess()\n$string = postprocess($string);\n"
                },
                {
                    "name": "postprocess",
                    "content": "\"I\"'s and \"you\"'s are exchanged. postprocess() is called from within the transform() function.\n\nIt uses the array %post, created during the parse of the script.\n\ntestquit()\nif ($self->testquit($userinput) ) { ... }\n\ntestquit() detects words like \"bye\" and \"quit\" and returns true if it finds one of them as the\nfirst word in the sentence.\n\nThese words are listed in the script, under the keyword \"quit\".\n\ndebugmemory()\n$self->debugmemory()\n\ndebugmemory() is a special function which returns the contents of Eliza's memory stack.\n\ntransform()\n$reply = $chatterbot->transform( $string, $usememory );\n"
                },
                {
                    "name": "transform",
                    "content": "transformations, then invokes postprocess(). It returns the transformed output string, called\n$reasmb.\n\nThe algorithm embedded in the transform() method has three main parts:\n\n1   Search the input string for a keyword.\n\n2   If we find a keyword, use the list of decomposition rules for that keyword, and\npattern-match the input string against each rule.\n\n3   If the input string matches any of the decomposition rules, then randomly select one of the\nreassembly rules for that decomposition rule, and use it to construct the reply.\n"
                },
                {
                    "name": "transform",
                    "content": "flag which indicates where this sting came from. If the flag is set, then the string has been\npulled from memory, and we should use reassembly rules appropriate for that. If the flag is not\nset, then the string is the most recent user input, and we can use the ordinary reassembly\nrules.\n\nThe memory flag is only set when the transform() function is called recursively. The mechanism\nfor setting this parameter is embedded in the transoform method itself. If the flag is set\ninappropriately, it is ignored.\n"
                },
                {
                    "name": "How memory is used",
                    "content": "In the script, some reassembly rules are special. They are marked with the keyword\n\"reasmformemory\", rather than just \"reasm\". Eliza \"remembers\" any comment when it matches a\ndocomposition rule for which there are any reassembly rules for memory. An Eliza object\nremembers up to $maxmemorysize (default: 5) user input strings.\n\nIf, during a subsequent run, the transform() method fails to find any appropriate decomposition\nrule for a user's comment, and if there are any comments inside the memory array, then Eliza may\nelect to ignore the most recent comment and instead pull out one of the strings from memory. In\nthis case, the transform method is called recursively with the memory flag.\n\nHonestly, I am not sure exactly how this memory functionality was implemented in the original\nEliza program. Hopefully this implementation is not too far from Weizenbaum's.\n\nIf you don't want to use the memory functionality at all, then you can disable it:\n\n$mybot->memoryon(0);\n\nYou can also achieve the same effect by making sure that the script data does not contain any\nreassembly rules marked with the keyword \"reasmformemory\". The default script data only has 4\nsuch items.\n\nparsescriptdata()\n$self->parsescriptdata;\n$self->parsescriptdata( $scriptfile );\n"
                },
                {
                    "name": "parse_script_data",
                    "content": "function. However, you can also call this method at any time against an already-instantiated\nEliza instance. In that case, the new script data is *added* to the old script data. The old\nscript data is not deleted.\n\nYou can pass a parameter to this function, which is the name of the script file, and it will\nread in and parse that file. If you do not pass any parameter to this method, then it will read\nthe data embedded at the end of the module as its default script data.\n\nIf you pass the name of a script file to parsescriptdata(), and that file is not available for\nreading, then the module dies.\n"
                }
            ]
        },
        "Format of the script file": {
            "content": "This module includes a default script file within itself, so it is not necessary to explicitly\nspecify a script file when instantiating an Eliza object.\n\nEach line in the script file can specify a key, a decomposition rule, or a reassembly rule.\n\nkey: remember 5\ndecomp: * i remember *\nreasmb: Do you often think of (2) ?\nreasmb: Does thinking of (2) bring anything else to mind ?\ndecomp: * do you remember *\nreasmb: Did you think I would forget (2) ?\nreasmb: What about (2) ?\nreasmb: goto what\npre: equivalent alike\nsynon: belief feel think believe wish\n\nThe number after the key specifies the rank. If a user's input contains the keyword, then the",
            "subsections": [
                {
                    "name": "transform",
                    "content": "matches, then it will select one of the reassembly rules at random. The number (2) here means\n\"use whatever set of words matched the second asterisk in the decomposition rule.\"\n\nIf you specify a list of synonyms for a word, the you should use a \"@\" when you use that word in\na decomposition rule:\n\ndecomp: * i @belief i *\nreasmb: Do you really think so ?\nreasmb: But you are not sure you (3).\n\nOtherwise, the script will never check to see if there are any synonyms for that keyword.\n\nReassembly rules should be marked with *reasmformemory* rather than *reasmb* when it is\nappropriate for use when a user's comment has been extracted from memory.\n\nkey: my 2\ndecomp: * my *\nreasmformemory: Let's discuss further why your (2).\nreasmformemory: Earlier you said your (2).\nreasmformemory: But your (2).\nreasmformemory: Does that have anything to do with the fact that your (2) ?\n"
                }
            ]
        },
        "How the script file is parsed": {
            "content": "Each line in the script file contains an \"entrytype\" (key, decomp, synon) and an \"entry\",\nseparated by a colon. In turn, each \"entry\" can itself be composed of a \"key\" and a \"value\",\nseparated by a space. The parsescriptdata() function parses each line out, and splits the\n\"entry\" and \"entrytype\" portion of each line into two variables, $entry and $entrytype.\n\nNext, it uses the string $entrytype to determine what sort of stuff to expect in the $entry\nvariable, if anything, and parses it accordingly. In some cases, there is no second level of\nkey-value pair, so the function does not even bother to isolate or create $key and $value.\n\n$key is always a single word. $value can be null, or one single word, or a string composed of\nseveral words, or an array of words.\n\nBased on all these entries and keys and values, the function creates two giant hashes:\n%decomplist, which holds the decomposition rules for each keyword, and %reasmblist, which holds\nthe reassembly phrases for each decomposition rule. It also creates %keyranks, which holds the\nranks for each key.\n\nSix other arrays are created: \"%reasmformemory, %pre, %post, %synon, @initial,\" and @final.\n",
            "subsections": []
        },
        "COPYRIGHT AND LICENSE": {
            "content": "This software is copyright (c) 2003 by John Nolan <jpnolan@sonic.net>.\n\nThis is free software; you can redistribute it and/or modify it under the same terms as the Perl\n5 programming language system itself.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "John Nolan jpnolan@sonic.net January 2003.\n\nImplements the classic Eliza algorithm by Prof. Joseph Weizenbaum. Script format devised by\nCharles Hayden.\n",
            "subsections": []
        }
    },
    "summary": "Chatbot::Eliza - A clone of the classic Eliza program",
    "flags": [],
    "examples": [],
    "see_also": []
}