home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Eliza.pm < prev    next >
Encoding:
Perl POD Document  |  2003-01-23  |  52.1 KB  |  1,764 lines

  1. ###################################################################
  2.  
  3. package Chatbot::Eliza;
  4.  
  5. # Copyright (c) 1997-2003 John Nolan. All rights reserved. 
  6. # This program is free software.  You may modify and/or 
  7. # distribute it under the same terms as Perl itself.  
  8. # This copyright notice must remain attached to the file.  
  9. #
  10. # You can run this file through either pod2man or pod2html 
  11. # to produce pretty documentation in manual or html file format 
  12. # (these utilities are part of the Perl 5 distribution).
  13. #
  14. # POD documentation is distributed throughout the actual code
  15. # so that it also functions as comments.  
  16.  
  17. require 5.003; 
  18. use strict;
  19. use Carp;
  20.  
  21. use vars qw($VERSION @ISA $AUTOLOAD); 
  22.  
  23. $VERSION = '1.04';
  24. sub Version { $VERSION; }
  25.  
  26.  
  27. ####################################################################
  28. # ---{ B E G I N   P O D   D O C U M E N T A T I O N }--------------
  29. #
  30.  
  31. =head1  NAME
  32.  
  33. B<Chatbot::Eliza> - A clone of the classic Eliza program
  34.  
  35. =head1 SYNOPSIS
  36.  
  37.   use Chatbot::Eliza;
  38.  
  39.   $mybot = new Chatbot::Eliza;
  40.   $mybot->command_interface;
  41.  
  42.   # see below for details
  43.  
  44.  
  45. =head1 DESCRIPTION
  46.  
  47. This module implements the classic Eliza algorithm. 
  48. The original Eliza program was written by Joseph 
  49. Weizenbaum and described in the Communications 
  50. of the ACM in 1966.  Eliza is a mock Rogerian 
  51. psychotherapist.  It prompts for user input, 
  52. and uses a simple transformation algorithm
  53. to change user input into a follow-up question.  
  54. The program is designed to give the appearance 
  55. of understanding.  
  56.  
  57. This program is a faithful implementation of the program 
  58. described by Weizenbaum.  It uses a simplified script 
  59. language (devised by Charles Hayden).  The content 
  60. of the script is the same as Weizenbaum's. 
  61.  
  62. This module encapsulates the Eliza algorithm 
  63. in the form of an object.  This should make 
  64. the functionality easy to incorporate in larger programs.  
  65.  
  66.  
  67. =head1 INSTALLATION
  68.  
  69. The current version of Chatbot::Eliza.pm is available on CPAN:
  70.  
  71.   http://www.perl.com/CPAN/modules/by-module/Chatbot/
  72.  
  73. To install this package, just change to the directory which 
  74. you created by untarring the package, and type the following:
  75.  
  76.     perl Makefile.PL
  77.     make test
  78.     make
  79.     make install
  80.  
  81. This will copy Eliza.pm to your perl library directory for 
  82. use by all perl scripts.  You probably must be root to do this,
  83. unless you have installed a personal copy of perl.  
  84.  
  85.  
  86. =head1 USAGE
  87.  
  88. This is all you need to do to launch a simple
  89. Eliza session:
  90.  
  91.     use Chatbot::Eliza;
  92.  
  93.     $mybot = new Chatbot::Eliza;
  94.     $mybot->command_interface;
  95.  
  96. You can also customize certain features of the 
  97. session:
  98.  
  99.     $myotherbot = new Chatbot::Eliza;
  100.  
  101.     $myotherbot->name( "Hortense" );
  102.     $myotherbot->debug( 1 );
  103.  
  104.     $myotherbot->command_interface;
  105.  
  106. These lines set the name of the bot to be
  107. "Hortense" and turn on the debugging output.
  108.  
  109. When creating an Eliza object, you can specify
  110. a name and an alternative scriptfile:
  111.  
  112.     $bot = new Chatbot::Eliza "Brian", "myscript.txt";
  113.  
  114. You can also use an anonymous hash to set these parameters.
  115. Any of the fields can be initialized using this syntax:
  116.  
  117.     $bot = new Chatbot::Eliza {
  118.         name       => "Brian", 
  119.         scriptfile => "myscript.txt",
  120.         debug      => 1,
  121.         prompts_on => 1,
  122.         memory_on  => 0,
  123.         myrand     => 
  124.             sub { my $N = defined $_[0] ? $_[0] : 1;  rand($N); },
  125.     };
  126.  
  127. If you don't specify a script file, then the new object will be
  128. initialized with a default script.  The module contains this 
  129. script within itself. 
  130.  
  131. You can use any of the internal functions in
  132. a calling program.  The code below takes an 
  133. arbitrary string and retrieves the reply from 
  134. the Eliza object:
  135.  
  136.     my $string = "I have too many problems.";
  137.     my $reply  = $mybot->transform( $string );
  138.  
  139. You can easily create two bots, each with a different
  140. script, and see how they interact:
  141.  
  142.     use Chatbot::Eliza
  143.  
  144.     my ($harry, $sally, $he_says, $she_says);
  145.  
  146.     $sally = new Chatbot::Eliza "Sally", "histext.txt";
  147.     $harry = new Chatbot::Eliza "Harry", "hertext.txt";
  148.  
  149.     $he_says  = "I am sad.";
  150.  
  151.     # Seed the random number generator.
  152.     srand( time ^ ($$ + ($$ << 15)) );      
  153.  
  154.     while (1) {
  155.         $she_says = $sally->transform( $he_says );
  156.         print $sally->name, ": $she_says \n";
  157.     
  158.         $he_says  = $harry->transform( $she_says );
  159.         print $harry->name, ": $he_says \n";
  160.     }
  161.  
  162. Mechanically, this works well.  However, it critically depends
  163. on the actual script data.  Having two mock Rogerian therapists
  164. talk to each other usually does not produce any sensible conversation, 
  165. of course.  
  166.  
  167. After each call to the transform() method, the debugging output
  168. for that transformation is stored in a variable called $debug_text.
  169.  
  170.     my $reply      = $mybot->transform( "My foot hurts" );
  171.     my $debugging  = $mybot->debug_text;
  172.  
  173. This feature always available, even if the instance's $debug 
  174. variable is set to 0. 
  175.  
  176. Calling programs can specify their own random-number generators.
  177. Use this syntax:
  178.  
  179.         $chatbot = new Chatbot::Eliza;
  180.         $chatbot->myrand(
  181.                 sub {
  182.                         #function goes here!
  183.                 }
  184.         );
  185.  
  186. The custom random function should have the same prototype
  187. as perl's built-in rand() function.  That is, it should take
  188. a single (numeric) expression as a parameter, and it should
  189. return a floating-point value between 0 and that number.
  190.  
  191. What this code actually does is pass a reference to an anonymous
  192. subroutine ("code reference").  Make sure you've read the perlref
  193. manpage for details on how code references actually work. 
  194.  
  195. If you don't specify any custom rand function, then the Eliza
  196. object will just use the built-in rand() function. 
  197.  
  198. =head1 MAIN DATA MEMBERS
  199.  
  200. Each Eliza object uses the following data structures 
  201. to hold the script data in memory:
  202.  
  203. =head2 %decomplist 
  204.  
  205. I<Hash>: the set of keywords;  I<Values>: strings containing 
  206. the decomposition rules. 
  207.  
  208. =head2 %reasmblist 
  209.  
  210. I<Hash>: a set of values which are each the join 
  211. of a keyword and a corresponding decomposition rule;  
  212. I<Values>: the set of possible reassembly statements 
  213. for that keyword and decomposition rule.  
  214.  
  215. =head2 %reasmblist_for_memory
  216.  
  217. This structure is identical to C<%reasmblist>, except
  218. that these rules are only invoked when a user comment 
  219. is being retrieved from memory. These contain comments 
  220. such as "Earlier you mentioned that...," which are only
  221. appropriate for remembered comments.  Rules in the script
  222. must be specially marked in order to be included
  223. in this list rather than C<%reasmblist>. The default
  224. script only has a few of these rules. 
  225.  
  226. =head2 @memory
  227.  
  228. A list of user comments which an Eliza instance is remembering 
  229. for future use.  Eliza does not remember everything, only some things. 
  230. In this implementation, Eliza will only remember comments
  231. which match a decomposition rule which actually has reassembly 
  232. rules that are marked with the keyword "reasm_for_memory"
  233. rather than the normal "reasmb".  The default script
  234. only has a few of these.  
  235.  
  236. =head2 %keyranks
  237.  
  238. I<Hash>: the set of keywords;  I<Values>: the ranks for each keyword
  239.  
  240. =head2 @quit
  241.  
  242. "quit" words -- that is, words the user might use 
  243. to try to exit the program.  
  244.  
  245. =head2 @initial
  246.  
  247. Possible greetings for the beginning of the program.
  248.  
  249. =head2 @final
  250.  
  251. Possible farewells for the end of the program.
  252.  
  253. =head2 %pre
  254.  
  255. I<Hash>: words which are replaced before any transformations;  
  256. I<Values>: the respective replacement words.
  257.  
  258. =head2 %post
  259.  
  260. I<Hash>: words which are replaced after the transformations 
  261. and after the reply is constructed;  I<Values>: the respective 
  262. replacement words.
  263.  
  264. =head2 %synon
  265.  
  266. I<Hash>: words which are found in decomposition rules;  
  267. I<Values>: words which are treated just like their 
  268. corresponding synonyms during matching of decomposition
  269. rules. 
  270.  
  271. =head2 Other data members
  272.  
  273. There are several other internal data members.  Hopefully 
  274. these are sufficiently obvious that you can learn about them
  275. just by reading the source code.  
  276.  
  277. =cut
  278.  
  279.  
  280. my %fields = (
  281.     name         => 'Eliza',
  282.     scriptfile    => '',
  283.  
  284.     debug         => 0,
  285.     debug_text    => '',
  286.     transform_text    => '',
  287.     prompts_on    => 1,
  288.     memory_on       => 1,
  289.     botprompt    => '',
  290.     userprompt    => '',
  291.  
  292.     myrand          => 
  293.             sub { my $N = defined $_[0] ? $_[0] : 1;  rand($N); },
  294.  
  295.     keyranks    => undef,
  296.     decomplist    => undef,
  297.     reasmblist    => undef,
  298.     reasmblist_for_memory    => undef,
  299.  
  300.     pre        => undef,
  301.     post        => undef,
  302.     synon        => undef,
  303.     initial        => undef,
  304.     final        => undef,
  305.     quit        => undef, 
  306.  
  307.     max_memory_size            => 5,
  308.     likelihood_of_using_memory    => 1,
  309.     memory                => undef,
  310. );
  311.  
  312.  
  313. ####################################################################
  314. # ---{ B E G I N   M E T H O D S }----------------------------------
  315. #
  316.  
  317. =head1 METHODS
  318.  
  319. =head2 new()
  320.  
  321.     my $chatterbot = new Chatbot::Eliza;
  322.  
  323. new() creates a new Eliza object.  This method
  324. also calls the internal _initialize() method, which in turn
  325. calls the parse_script_data() method, which initializes
  326. the script data.  
  327.  
  328.     my $chatterbot = new Chatbot::Eliza 'Ahmad', 'myfile.txt';
  329.  
  330. The eliza object defaults to the name "Eliza", and it
  331. contains default script data within itself.  However,
  332. using the syntax above, you can specify an alternative
  333. name and an alternative script file. 
  334.  
  335. See the method parse_script_data(). for a description
  336. of the format of the script file. 
  337.  
  338. =cut
  339.  
  340. sub new {
  341.     my ($that,$name,$scriptfile) = @_;
  342.     my $class = ref($that) || $that;
  343.     my $self = {
  344.         _permitted => \%fields,
  345.         %fields,
  346.     };
  347.     bless $self, $class;
  348.     $self->_initialize($name,$scriptfile);
  349.     return $self;
  350. } # end method new
  351.  
  352. sub _initialize {
  353.     my ($self,$param1,$param2) = @_;
  354.  
  355.     if (defined $param1 and ref $param1 eq "HASH") {
  356.  
  357.         # Allow the calling program to pass in intial parameters
  358.         # as an anonymous hash
  359.         map { $self->{$_} = $param1->{$_}; } keys %$param1;
  360.  
  361.         $self->parse_script_data( $self->{scriptfile} );
  362.  
  363.     } else {
  364.         $self->name($param1) if $param1;
  365.         $self->parse_script_data($param2);
  366.     } 
  367.  
  368.     # Initialize the memory array ref at instantiation time,
  369.     # rather than at class definition time. 
  370.     # (THANKS to Randal Schwartz and Robert Chin for fixing this bug.) 
  371.     #
  372.     $self->{memory} = [];
  373. }
  374.  
  375. sub AUTOLOAD {
  376.     my $self = shift;
  377.     my $class = ref($self) || croak "$self is not an object : $!\n";
  378.     my $field = $AUTOLOAD;
  379.     $field =~ s/.*://; # Strip fully-qualified portion
  380.  
  381.     unless (exists $self->{"_permitted"}->{$field} ) {
  382.         croak "Can't access `$field' field in object of class $class : $!\n";
  383.     }
  384.  
  385.     if (@_) {
  386.         return $self->{$field} = shift;
  387.     } else {
  388.         return $self->{$field};
  389.     }
  390. } # end method AUTOLOAD
  391.  
  392.  
  393. ####################################################################
  394. # --- command_interface ---
  395.  
  396. =head2 command_interface()
  397.  
  398.     $chatterbot->command_interface;
  399.  
  400. command_interface() opens an interactive session with 
  401. the Eliza object, just like the original Eliza program.
  402.  
  403. If you want to design your own session format, then 
  404. you can write your own while loop and your own functions
  405. for prompting for and reading user input, and use the 
  406. transform() method to generate Eliza's responses. 
  407. (I<Note>: you do not need to invoke preprocess()
  408. and postprocess() directly, because these are invoked
  409. from within the transform() method.) 
  410.  
  411. But if you're lazy and you want to skip all that,
  412. then just use command_interface().  It's all done for you. 
  413.  
  414. During an interactive session invoked using command_interface(),
  415. you can enter the word "debug" to toggle debug mode on and off.
  416. You can also enter the keyword "memory" to invoke the _debug_memory()
  417. method and print out the contents of the Eliza instance's memory.
  418.  
  419. =cut
  420.  
  421. sub command_interface {
  422.     my $self = shift;
  423.     my ($user_input, $previous_user_input, $reply);
  424.  
  425.     $user_input = "";
  426.  
  427.     $self->botprompt($self->name . ":\t");    # Eliza's prompt 
  428.     $self->userprompt("you:\t");         # User's prompt
  429.  
  430.     # Seed the random number generator.
  431.     srand( time() ^ ($$ + ($$ << 15)) );  
  432.  
  433.     # Print the Eliza prompt
  434.     print $self->botprompt if $self->prompts_on;
  435.  
  436.     # Print an initial greeting
  437.     print "$self->{initial}->[ int &{$self->{myrand}}( scalar @{ $self->{initial} } ) ]\n";
  438.  
  439.  
  440.     ###################################################################
  441.     # command loop.  This loop should go on forever,
  442.     # until we explicity break out of it. 
  443.     #
  444.     while (1) {
  445.  
  446.         print $self->userprompt if $self->prompts_on;
  447.  
  448.         $previous_user_input = $user_input;
  449.         chomp( $user_input = <STDIN> ); 
  450.  
  451.  
  452.         # If the user wants to quit,
  453.         # print out a farewell and quit.
  454.         if ($self->_testquit($user_input) ) {
  455.             $reply = "$self->{final}->[ int &{$self->{myrand}}( scalar @{$self->{final}} ) ]";
  456.             print $self->botprompt if $self->prompts_on;
  457.             print "$reply\n";
  458.             last;
  459.         } 
  460.  
  461.         # If the user enters the word "debug",
  462.         # then toggle on/off this Eliza's debug output.
  463.         if ($user_input eq "debug") {
  464.             $self->debug( ! $self->debug );
  465.             $user_input = $previous_user_input;
  466.         }
  467.  
  468.         # If the user enters the word "memory",
  469.         # then use the _debug_memory method to dump out
  470.         # the current contents of Eliza's memory
  471.         if ($user_input eq "memory" or $user_input eq "debug memory") {
  472.             print $self->_debug_memory();
  473.             redo;
  474.         }
  475.  
  476.         # If the user enters the word "debug that",
  477.         # then dump out the debugging of the 
  478.         # most recent call to transform.  
  479.         if ($user_input eq "debug that") {
  480.             print $self->debug_text();
  481.             redo;
  482.         }
  483.  
  484.         # Invoke the transform method
  485.         # to generate a reply.
  486.         $reply = $self->transform( $user_input );
  487.  
  488.  
  489.         # Print out the debugging text if debugging is set to on.
  490.         # This variable should have been set by the transform method.
  491.         print $self->debug_text if $self->debug;
  492.  
  493.         # Print the actual reply
  494.         print $self->botprompt if $self->prompts_on;
  495.         print "$reply\n";
  496.  
  497.     } # End UI command loop.  
  498.  
  499.  
  500. } # End method command_interface
  501.  
  502.  
  503. ####################################################################
  504. # --- preprocess ---
  505.  
  506. =head2 preprocess()
  507.  
  508.     $string = preprocess($string);
  509.  
  510. preprocess() applies simple substitution rules to the input string.
  511. Mostly this is to catch varieties in spelling, misspellings,
  512. contractions and the like.  
  513.  
  514. preprocess() is called from within the transform() method.  
  515. It is applied to user-input text, BEFORE any processing,
  516. and before a reassebly statement has been selected. 
  517.  
  518. It uses the array C<%pre>, which is created 
  519. during the parse of the script.
  520.  
  521. =cut
  522.  
  523. sub preprocess {
  524.     my ($self,$string) = @_;
  525.  
  526.     my ($i, @wordsout, @wordsin, $keyword);
  527.  
  528.     @wordsout = @wordsin = split / /, $string;
  529.  
  530.     WORD: for ($i = 0; $i < @wordsin; $i++) {
  531.         foreach $keyword (keys %{ $self->{pre} }) {
  532.             if ($wordsin[$i] =~ /\b$keyword\b/i ) {
  533.                 ($wordsout[$i] = $wordsin[$i]) =~ s/$keyword/$self->{pre}->{$keyword}/ig;
  534.                 next WORD;
  535.             }
  536.         }
  537.     }
  538.     return join ' ', @wordsout;
  539. }
  540.  
  541.  
  542. ####################################################################
  543. # --- postprocess ---
  544.  
  545. =head2 postprocess()
  546.  
  547.     $string = postprocess($string);
  548.  
  549. postprocess() applies simple substitution rules to the 
  550. reassembly rule.  This is where all the "I"'s and "you"'s 
  551. are exchanged.  postprocess() is called from within the
  552. transform() function.
  553.  
  554. It uses the array C<%post>, created 
  555. during the parse of the script.
  556.  
  557. =cut
  558.  
  559. sub postprocess {
  560.     my ($self,$string) = @_;
  561.  
  562.     my ($i, @wordsout, @wordsin, $keyword);
  563.  
  564.     @wordsin = @wordsout = split (/ /, $string);
  565.  
  566.     WORD: for ($i = 0; $i < @wordsin; $i++) {
  567.         foreach $keyword (keys %{ $self->{post} }) {
  568.             if ($wordsin[$i] =~ /\b$keyword\b/i ) {
  569.                 ($wordsout[$i] = $wordsin[$i]) =~ s/$keyword/$self->{post}->{$keyword}/ig;
  570.                 next WORD;
  571.             }
  572.         }
  573.     }
  574.     return join ' ', @wordsout;
  575. }
  576.  
  577. ####################################################################
  578. # --- _testquit ---
  579.  
  580. =head2 _testquit()
  581.  
  582.      if ($self->_testquit($user_input) ) { ... }
  583.  
  584. _testquit() detects words like "bye" and "quit" and returns
  585. true if it finds one of them as the first word in the sentence. 
  586.  
  587. These words are listed in the script, under the keyword "quit". 
  588.  
  589. =cut
  590.  
  591. sub _testquit {
  592.     my ($self,$string) = @_;
  593.  
  594.     my ($quitword, @wordsin);
  595.  
  596.     foreach $quitword (@{ $self->{quit} }) {
  597.         return 1 if ($string =~ /\b$quitword\b/i ) ;
  598.     }
  599. }
  600.  
  601.  
  602. ####################################################################
  603. # --- _debug_memory ---
  604.  
  605. =head2 _debug_memory()
  606.  
  607.      $self->_debug_memory()
  608.  
  609. _debug_memory() is a special function which returns
  610. the contents of Eliza's memory stack. 
  611.  
  612.  
  613. =cut
  614.  
  615. sub _debug_memory {
  616.  
  617.     my ($self) = @_;
  618.  
  619.     my $string = "\t";           
  620.     $string .= $#{ $self->memory } + 1;
  621.     $string .= " item(s) in memory stack:\n";
  622.  
  623.     # [THANKS to Roy Stephan for helping me adjust this bit]
  624.     #
  625.     foreach (@{ $self->memory } ) { 
  626.  
  627.         my $line = $_; 
  628.         $string .= sprintf "\t\t->$line\n" ;
  629.     };
  630.  
  631.     return $string;
  632. }
  633.  
  634. ####################################################################
  635. # --- transform ---
  636.  
  637. =head2 transform()
  638.  
  639.     $reply = $chatterbot->transform( $string, $use_memory );
  640.  
  641. transform() applies transformation rules to the user input
  642. string.  It invokes preprocess(), does transformations, 
  643. then invokes postprocess().  It returns the tranformed 
  644. output string, called C<$reasmb>.  
  645.  
  646. The algorithm embedded in the transform() method has three main parts:
  647.  
  648. =over
  649.  
  650. =item 1 
  651.  
  652. Search the input string for a keyword.
  653.  
  654. =item 2 
  655.  
  656. If we find a keyword, use the list of decomposition rules
  657. for that keyword, and pattern-match the input string against
  658. each rule.
  659.  
  660. =item 3
  661.  
  662. If the input string matches any of the decomposition rules,
  663. then randomly select one of the reassembly rules for that
  664. decomposition rule, and use it to construct the reply. 
  665.  
  666. =back
  667.  
  668. transform() takes two parameters.  The first is the string we want
  669. to transform.  The second is a flag which indicates where this sting
  670. came from.  If the flag is set, then the string has been pulled
  671. from memory, and we should use reassembly rules appropriate
  672. for that.  If the flag is not set, then the string is the most
  673. recent user input, and we can use the ordinary reassembly rules. 
  674.  
  675. The memory flag is only set when the transform() function is called 
  676. recursively.  The mechanism for setting this parameter is
  677. embedded in the transoform method itself.  If the flag is set
  678. inappropriately, it is ignored.  
  679.  
  680. =cut
  681.  
  682. sub transform{
  683.     my ($self,$string,$use_memory) = @_;
  684.  
  685.     # Initialize the debugging text buffer.
  686.     $self->debug_text('');
  687.  
  688.     $self->debug_text(sprintf "\t[Pulling string \"$string\" from memory.]\n")
  689.         if $use_memory;
  690.  
  691.     my ($i, @string_parts, $string_part, $rank, $goto, $reasmb, $keyword, 
  692.         $decomp, $this_decomp, $reasmbkey, @these_reasmbs,
  693.         @decomp_matches, $synonyms, $synonym_index);
  694.  
  695.     # Default to a really low rank. 
  696.     $rank   = -2;
  697.     $reasmb = "";
  698.     $goto   = "";
  699.  
  700.     # First run the string through the preprocessor.  
  701.     $string = $self->preprocess( $string );
  702.  
  703.     # Convert punctuation to periods.  We will assume that commas
  704.     # and certain conjunctions separate distinct thoughts/sentences.  
  705.     $string =~ s/[?!,]/./g;
  706.     $string =~ s/but/./g;   #   Yikes!  This is English-specific. 
  707.  
  708.     # Split the string by periods into an array
  709.     @string_parts = split /\./, $string ;
  710.  
  711.     # Examine each part of the input string in turn.
  712.     STRING_PARTS: foreach $string_part (@string_parts) {
  713.  
  714.     # Run through the whole list of keywords.  
  715.     KEYWORD: foreach $keyword (keys %{ $self->{decomplist} }) {
  716.  
  717.         # Check to see if the input string contains a keyword
  718.         # which outranks any we have found previously
  719.         # (On first loop, rank is set to -2.)
  720.         if ( ($string_part =~ /\b$keyword\b/i or $keyword eq $goto) 
  721.              and 
  722.              $rank < $self->{keyranks}->{$keyword}  
  723.            ) 
  724.         {
  725.             # If we find one, then set $rank to equal 
  726.             # the rank of that keyword. 
  727.             $rank = $self->{keyranks}->{$keyword};
  728.  
  729.             $self->debug_text($self->debug_text . sprintf "\t$rank> $keyword");
  730.  
  731.             # Now let's check all the decomposition rules for that keyword. 
  732.             DECOMP: foreach $decomp (@{ $self->{decomplist}->{$keyword} }) {
  733.  
  734.                 # Change '*' to '\b(.*)\b' in this decomposition rule,
  735.                 # so we can use it for regular expressions.  Later, 
  736.                 # we will want to isolate individual matches to each wildcard. 
  737.                 ($this_decomp = $decomp) =~ s/\s*\*\s*/\\b\(\.\*\)\\b/g;
  738.  
  739.                 # If this docomposition rule contains a word which begins with '@', 
  740.                 # then the script also contained some synonyms for that word.  
  741.                 # Find them all using %synon and generate a regular expression 
  742.                 # containing all of them. 
  743.                 if ($this_decomp =~ /\@/ ) {
  744.                     ($synonym_index = $this_decomp) =~ s/.*\@(\w*).*/$1/i ;
  745.                     $synonyms = join ('|', @{ $self->{synon}->{$synonym_index} });
  746.                     $this_decomp =~ s/(.*)\@$synonym_index(.*)/$1($synonym_index\|$synonyms)$2/g;
  747.                 }
  748.  
  749.                 $self->debug_text($self->debug_text .  sprintf "\n\t\t: $decomp");
  750.  
  751.                 # Using the regular expression we just generated, 
  752.                 # match against the input string.  Use empty "()"'s to 
  753.                 # eliminate warnings about uninitialized variables. 
  754.                 if ($string_part =~ /$this_decomp()()()()()()()()()()/i) {
  755.  
  756.                     # If this decomp rule matched the string, 
  757.                     # then create an array, so that we can refer to matches
  758.                     # to individual wildcards.  Use '0' as a placeholder
  759.                     # (we don't want to refer to any "zeroth" wildcard).
  760.                     @decomp_matches = ("0", $1, $2, $3, $4, $5, $6, $7, $8, $9); 
  761.                     $self->debug_text($self->debug_text . sprintf " : @decomp_matches\n");
  762.  
  763.                     # Using the keyword and the decomposition rule,
  764.                     # reconstruct a key for the list of reassamble rules.
  765.                     $reasmbkey = join ($;,$keyword,$decomp);
  766.  
  767.                     # Get the list of possible reassembly rules for this key. 
  768.                     #
  769.                     if (defined $use_memory and $#{ $self->{reasmblist_for_memory}->{$reasmbkey} } >= 0) {
  770.  
  771.                         # If this transform function was invoked with the memory flag, 
  772.                         # and there are in fact reassembly rules which are appropriate
  773.                         # for pulling out of memory, then include them.  
  774.                         @these_reasmbs = @{ $self->{reasmblist_for_memory}->{$reasmbkey} }
  775.  
  776.                     } else {
  777.  
  778.                         # Otherwise, just use the plain reassembly rules.
  779.                         # (This is what normally happens.)
  780.                         @these_reasmbs = @{ $self->{reasmblist}->{$reasmbkey} }
  781.                     }
  782.  
  783.                     # Pick out a reassembly rule at random. 
  784.                     $reasmb = $these_reasmbs[ int &{$self->{myrand}}( scalar @these_reasmbs ) ];
  785.  
  786.                     $self->debug_text($self->debug_text . sprintf "\t\t-->  $reasmb\n");
  787.  
  788.                     # If the reassembly rule we picked contains the word "goto",
  789.                     # then we start over with a new keyword.  Set $keyword to equal
  790.                     # that word, and start the whole loop over. 
  791.                     if ($reasmb =~ m/^goto\s(\w*).*/i) {
  792.                         $self->debug_text($self->debug_text . sprintf "\$1 = $1\n");
  793.                         $goto = $keyword = $1;
  794.                         $rank = -2;
  795.                         redo KEYWORD;
  796.                     }
  797.  
  798.                     # Otherwise, using the matches to wildcards which we stored above,
  799.                     # insert words from the input string back into the reassembly rule. 
  800.                     # [THANKS to Gidon Wise for submitting a bugfix here]
  801.                     for ($i=1; $i <= $#decomp_matches; $i++) {
  802.                         $decomp_matches[$i] = $self->postprocess( $decomp_matches[$i] );
  803.                         $decomp_matches[$i] =~ s/([,;?!]|\.*)$//;
  804.                         $reasmb =~ s/\($i\)/$decomp_matches[$i]/g;
  805.                     }
  806.  
  807.                     # Move on to the next keyword.  If no other keywords match,
  808.                     # then we'll end up actually using the $reasmb string 
  809.                     # we just generated above.
  810.                     next KEYWORD ;
  811.  
  812.                 }  # End if ($string_part =~ /$this_decomp/i) 
  813.  
  814.                 $self->debug_text($self->debug_text . sprintf "\n");
  815.  
  816.             } # End DECOMP: foreach $decomp (@{ $self->{decomplist}->{$keyword} }) 
  817.  
  818.         } # End if ( ($string_part =~ /\b$keyword\b/i or $keyword eq $goto) 
  819.  
  820.     } # End KEYWORD: foreach $keyword (keys %{ $self->{decomplist})
  821.     
  822.     } # End STRING_PARTS: foreach $string_part (@string_parts) {
  823.  
  824. =head2 How memory is used
  825.  
  826. In the script, some reassembly rules are special.  They are marked with
  827. the keyword "reasm_for_memory", rather than just "reasm".  
  828. Eliza "remembers" any comment when it matches a docomposition rule 
  829. for which there are any reassembly rules for memory. 
  830. An Eliza object remembers up to C<$max_memory_size> (default: 5) 
  831. user input strings.  
  832.  
  833. If, during a subsequent run, the transform() method fails to find any 
  834. appropriate decomposition rule for a user's comment, and if there are 
  835. any comments inside the memory array, then Eliza may elect to ignore 
  836. the most recent comment and instead pull out one of the strings from memory.  
  837. In this case, the transform method is called recursively with the memory flag. 
  838.  
  839. Honestly, I am not sure exactly how this memory functionality
  840. was implemented in the original Eliza program.  Hopefully
  841. this implementation is not too far from Weizenbaum's. 
  842.  
  843. If you don't want to use the memory functionality at all,
  844. then you can disable it:
  845.  
  846.     $mybot->memory_on(0);
  847.  
  848. You can also achieve the same effect by making sure
  849. that the script data does not contain any reassembly rules 
  850. marked with the keyword "reasm_for_memory".  The default
  851. script data only has 4 such items.  
  852.  
  853. =cut
  854.  
  855.     if ($reasmb eq "") {
  856.  
  857.         # If all else fails, call this method recursively 
  858.         # and make sure that it has something to parse. 
  859.         # Use a string from memory if anything is available. 
  860.         #
  861.         # $self-likelihood_of_using_memory should be some number
  862.         # between 1 and 0;  it defaults to 1. 
  863.         #
  864.         if (
  865.             $#{ $self->memory } >= 0 
  866.             and 
  867.             &{$self->{myrand}}(1) >= 1 - $self->likelihood_of_using_memory
  868.         ) {
  869.  
  870.             $reasmb =  $self->transform( shift @{ $self->memory }, "use memory" );
  871.  
  872.         } else {
  873.             $reasmb =  $self->transform("xnone");
  874.         }
  875.  
  876.     } elsif ($self->memory_on) {   
  877.  
  878.         # If memory is switched on, then we handle memory. 
  879.  
  880.         # Now that we have successfully transformed this string, 
  881.         # push it onto the end of the memory stack... unless, of course,
  882.         # that's where we got it from in the first place, or if the rank
  883.         # is not the kind we remember.
  884.         #
  885.         if (
  886.                 $#{ $self->{reasmblist_for_memory}->{$reasmbkey} } >= 0
  887.                 and
  888.                 not defined $use_memory
  889.         ) {
  890.  
  891.             push  @{ $self->memory },$string ;
  892.         }
  893.  
  894.         # Shift out the least-recent item from the bottom 
  895.         # of the memory stack if the stack exceeds the max size. 
  896.         shift @{ $self->memory } if $#{ $self->memory } >= $self->max_memory_size;
  897.  
  898.         $self->debug_text($self->debug_text 
  899.             . sprintf("\t%d item(s) in memory.\n", $#{ $self->memory } + 1 ) ) ;
  900.  
  901.     } # End if ($reasmb eq "")
  902.  
  903.     $reasmb =~ tr/ / /s;       # Eliminate any duplicate space characters. 
  904.     $reasmb =~ s/[ ][?]$/?/;   # Eliminate any spaces before the question mark. 
  905.  
  906.     # Save the return string so that forgetful calling programs
  907.     # can ask the bot what the last reply was. 
  908.     $self->transform_text($reasmb);
  909.  
  910.     return $reasmb ;
  911. }
  912.  
  913.  
  914. ####################################################################
  915. # --- parse_script_data ---
  916.  
  917. =head2 parse_script_data()
  918.  
  919.     $self->parse_script_data;
  920.     $self->parse_script_data( $script_file );
  921.  
  922. parse_script_data() is invoked from the _initialize() method,
  923. which is called from the new() function.  However, you can also
  924. call this method at any time against an already-instantiated 
  925. Eliza instance.  In that case, the new script data is I<added>
  926. to the old script data.  The old script data is not deleted. 
  927.  
  928. You can pass a parameter to this function, which is the name of the
  929. script file, and it will read in and parse that file.  
  930. If you do not pass any parameter to this method, then
  931. it will read the data embedded at the end of the module as its
  932. default script data.  
  933.  
  934. If you pass the name of a script file to parse_script_data(), 
  935. and that file is not available for reading, then the module dies.  
  936.  
  937.  
  938. =head1 Format of the script file
  939.  
  940. This module includes a default script file within itself, 
  941. so it is not necessary to explicitly specify a script file 
  942. when instantiating an Eliza object.  
  943.  
  944. Each line in the script file can specify a key,
  945. a decomposition rule, or a reassembly rule.
  946.  
  947.   key: remember 5
  948.     decomp: * i remember *
  949.       reasmb: Do you often think of (2) ?
  950.       reasmb: Does thinking of (2) bring anything else to mind ?
  951.     decomp: * do you remember *
  952.       reasmb: Did you think I would forget (2) ?
  953.       reasmb: What about (2) ?
  954.       reasmb: goto what
  955.   pre: equivalent alike
  956.   synon: belief feel think believe wish
  957.  
  958. The number after the key specifies the rank.
  959. If a user's input contains the keyword, then
  960. the transform() function will try to match
  961. one of the decomposition rules for that keyword.
  962. If one matches, then it will select one of
  963. the reassembly rules at random.  The number
  964. (2) here means "use whatever set of words
  965. matched the second asterisk in the decomposition
  966. rule." 
  967.  
  968. If you specify a list of synonyms for a word,
  969. the you should use a "@" when you use that
  970. word in a decomposition rule:
  971.  
  972.   decomp: * i @belief i *
  973.     reasmb: Do you really think so ?
  974.     reasmb: But you are not sure you (3).
  975.  
  976. Otherwise, the script will never check to see
  977. if there are any synonyms for that keyword. 
  978.  
  979. Reassembly rules should be marked with I<reasm_for_memory>
  980. rather than I<reasmb> when it is appropriate for use
  981. when a user's comment has been extracted from memory. 
  982.  
  983.   key: my 2
  984.     decomp: * my *
  985.       reasm_for_memory: Let's discuss further why your (2).
  986.       reasm_for_memory: Earlier you said your (2).
  987.       reasm_for_memory: But your (2).
  988.       reasm_for_memory: Does that have anything to do with the fact that your (2) ?
  989.  
  990. =head1 How the script file is parsed
  991.  
  992. Each line in the script file contains an "entrytype"
  993. (key, decomp, synon) and an "entry", separated by
  994. a colon.  In turn, each "entry" can itself be 
  995. composed of a "key" and a "value", separated by
  996. a space.  The parse_script_data() function
  997. parses each line out, and splits the "entry" and
  998. "entrytype" portion of each line into two variables,
  999. C<$entry> and C<$entrytype>. 
  1000.  
  1001. Next, it uses the string C<$entrytype> to determine 
  1002. what sort of stuff to expect in the C<$entry> variable,  
  1003. if anything, and parses it accordingly.  In some cases,
  1004. there is no second level of key-value pair, so the function
  1005. does not even bother to isolate or create C<$key> and C<$value>. 
  1006.  
  1007. C<$key> is always a single word.  C<$value> can be null, 
  1008. or one single word, or a string composed of several words, 
  1009. or an array of words.  
  1010.  
  1011. Based on all these entries and keys and values,
  1012. the function creates two giant hashes:
  1013. C<%decomplist>, which holds the decomposition rules for
  1014. each keyword, and C<%reasmblist>, which holds the 
  1015. reassembly phrases for each decomposition rule. 
  1016. It also creates C<%keyranks>, which holds the ranks for
  1017. each key.  
  1018.  
  1019. Six other arrays are created: C<%reasm_for_memory, %pre, %post, 
  1020. %synon, @initial,> and C<@final>. 
  1021.  
  1022. =cut
  1023.  
  1024. sub parse_script_data {
  1025.  
  1026.     my ($self,$scriptfile) = @_;
  1027.     my @scriptlines;
  1028.  
  1029.     if ($scriptfile) {
  1030.  
  1031.         # If we have an external script file, open it 
  1032.         # and read it in (the whole thing, all at once). 
  1033.         open  (SCRIPTFILE, "<$scriptfile") 
  1034.             or die "Could not read from file $scriptfile : $!\n";
  1035.         @scriptlines = <SCRIPTFILE>; # read in script data 
  1036.         $self->scriptfile($scriptfile);
  1037.         close (SCRIPTFILE);
  1038.  
  1039.     } else {
  1040.  
  1041.         # Otherwise, read in the data from the bottom 
  1042.         # of this file.  This data might be read several
  1043.         # times, so we save the offset pointer and
  1044.         # reset it when we're done.
  1045.         my $where= tell(DATA);
  1046.         @scriptlines = <DATA>;  # read in script data 
  1047.         seek(DATA, $where, 0);
  1048.         $self->scriptfile('');
  1049.     }
  1050.  
  1051.     my ($entrytype, $entry, $key, $value) ;
  1052.     my $thiskey    = ""; 
  1053.     my $thisdecomp = "";
  1054.  
  1055.     ############################################################
  1056.     # Examine each line of script data.  
  1057.     for (@scriptlines) { 
  1058.  
  1059.         # Skip comments and lines with only whitespace.
  1060.         next if (/^\s*#/ || /^\s*$/);  
  1061.  
  1062.         # Split entrytype and entry, using a colon as the delimiter.
  1063.         ($entrytype, $entry) = $_ =~ m/^\s*(\S*)\s*:\s*(.*)\s*$/;
  1064.  
  1065.         # Case loop, based on the entrytype.
  1066.         for ($entrytype) {   
  1067.  
  1068.             /quit/        and do { push @{ $self->{quit}    }, $entry; last; };
  1069.             /initial/    and do { push @{ $self->{initial} }, $entry; last; };
  1070.             /final/        and do { push @{ $self->{final}   }, $entry; last; };
  1071.  
  1072.             /decomp/    and do { 
  1073.                         die "$0: error parsing script:  decomposition rule with no keyword.\n" 
  1074.                             if $thiskey eq "";
  1075.                         $thisdecomp = join($;,$thiskey,$entry);
  1076.                         push @{ $self->{decomplist}->{$thiskey} }, $entry ; 
  1077.                         last; 
  1078.                     };
  1079.  
  1080.             /reasmb/    and do { 
  1081.                         die "$0: error parsing script:  reassembly rule with no decomposition rule.\n" 
  1082.                             if $thisdecomp eq "";
  1083.                         push @{ $self->{reasmblist}->{$thisdecomp} }, $entry ;  
  1084.                         last; 
  1085.                     };
  1086.  
  1087.             /reasm_for_memory/    and do { 
  1088.                         die "$0: error parsing script:  reassembly rule with no decomposition rule.\n" 
  1089.                             if $thisdecomp eq "";
  1090.                         push @{ $self->{reasmblist_for_memory}->{$thisdecomp} }, $entry ;  
  1091.                         last; 
  1092.                     };
  1093.  
  1094.             # The entrytypes below actually expect to see a key and value
  1095.             # pair in the entry, so we split them out.  The first word, 
  1096.             # separated by a space, is the key, and everything else is 
  1097.             # an array of values.
  1098.  
  1099.             ($key,$value) = $entry =~ m/^\s*(\S*)\s*(.*)/;
  1100.  
  1101.             /pre/        and do { $self->{pre}->{$key}   = $value; last; };
  1102.             /post/        and do { $self->{post}->{$key}  = $value; last; };
  1103.  
  1104.             # synon expects an array, so we split $value into an array, using " " as delimiter.  
  1105.             /synon/        and do { $self->{synon}->{$key} = [ split /\ /, $value ]; last; };
  1106.  
  1107.             /key/        and do { 
  1108.                         $thiskey = $key; 
  1109.                         $thisdecomp = "";
  1110.                         $self->{keyranks}->{$thiskey} = $value ; 
  1111.                         last;
  1112.                     };
  1113.     
  1114.         }  # End for ($entrytype) (case loop) 
  1115.  
  1116.     }  # End for (@scriptlines)
  1117.  
  1118. }  # End of method parse_script_data
  1119.  
  1120.  
  1121. # Eliminate some pesky warnings.
  1122. #
  1123. sub DESTROY {}
  1124.  
  1125.  
  1126. # ---{ E N D   M E T H O D S }----------------------------------
  1127. ####################################################################
  1128.  
  1129. 1;      # Return a true value.  
  1130.  
  1131.  
  1132. =head1 CHANGES
  1133.  
  1134. =over 4
  1135.  
  1136. =item * Version 1.02-1.04 - January 2003
  1137.  
  1138.   Added a Norwegian script, kindly contributed by 
  1139.   Mats Stafseng Einarsen.  Thanks Mats!
  1140.  
  1141. =item * Version 1.01 - January 2003
  1142.  
  1143.   Added an empty DESTORY method, to eliminate
  1144.   some pesky warning messages.  Suggested by
  1145.   Stas Bekman. 
  1146.  
  1147. =item * Version 0.98 - March 2000
  1148.  
  1149.   Some changes to the documentation.
  1150.  
  1151. =item * Versions 0.96-0.97 - October 1999
  1152.  
  1153.   One tiny change to the regex which implements
  1154.   reassemble rules.  Thanks to Gidon Wise for
  1155.   suggesting this improvement. 
  1156.  
  1157. =item * Versions 0.94-0.95 - July 1999
  1158.  
  1159.  
  1160.   Fixed a bug in the way the bot invokes its random function
  1161.   when it pulls a comment out of memory. 
  1162.  
  1163. =item * Version 0.93 - June 1999
  1164.  
  1165.   Calling programs can now specify their own random-number generators.  
  1166.   Use this syntax:
  1167.  
  1168.     $chatbot = new Chatbot::Eliza;
  1169.     $chatbot->myrand( 
  1170.         sub { 
  1171.             #function goes here! 
  1172.         } 
  1173.     );
  1174.  
  1175.   The custom random function should have the same prototype
  1176.   as perl's built-in rand() function.  That is, it should take
  1177.   a single (numeric) expression as a parameter, and it should 
  1178.   return a floating-point value between 0 and that number.  
  1179.  
  1180.   You can also now use a reference to an anonymous hash 
  1181.   as a parameter to the new() method to define any fields 
  1182.   in that bot instance:
  1183.  
  1184.         $bot = new Chatbot::Eliza {
  1185.                 name       => "Brian",
  1186.                 scriptfile => "myscript.txt",
  1187.                 debug      => 1,
  1188.         };
  1189.  
  1190.  
  1191. =item * Versions 0.91-0.92 - April 1999
  1192.  
  1193.   Fixed some misspellings. 
  1194.  
  1195.  
  1196. =item * Version 0.90 - April 1999
  1197.  
  1198.   Fixed a bug in the way individual bot objects store 
  1199.   their memory.  Thanks to Randal Schwartz and to 
  1200.   Robert Chin for pointing this out.
  1201.  
  1202.   Fixed a very stupid error in the way the random
  1203.   function is invoked.  Thanks to Antony Quintal
  1204.   for pointing out the error. 
  1205.  
  1206.   Many corrections and improvements were made 
  1207.   to the German script by Matthias Hellmund.  
  1208.   Thanks, Matthias!
  1209.  
  1210.   Made a minor syntactical change, at the suggestion
  1211.   of Roy Stephan.
  1212.  
  1213.   The memory functionality can now be disabled by setting the
  1214.   $Chatbot::Eliza::memory_on variable to 0, like so:
  1215.  
  1216.     $bot->memory_on(0);
  1217.  
  1218.   Thanks to Robert Chin for suggesting that. 
  1219.   
  1220.  
  1221.  
  1222. =item * Version 0.40 - July 1998
  1223.  
  1224.   Re-implemented the memory functionality. 
  1225.  
  1226.   Cleaned up and expanded the embedded POD documentation.  
  1227.  
  1228.   Added a sample script in German.  
  1229.  
  1230.   Modified the debugging behavior.  The transform() method itself 
  1231.   will no longer print any debugging output directly to STDOUT.  
  1232.   Instead, all debugging output is stored in a module variable 
  1233.   called "debug_text".  The "debug_text" variable is printed out 
  1234.   by the command_interface() method, if the debug flag is set.   
  1235.   But even if this flag is not set, the variable debug_text 
  1236.   is still available to any calling program.  
  1237.  
  1238.   Added a few more example scripts which use the module.  
  1239.  
  1240.     simple       - simple script using Eliza.pm
  1241.     simple.cgi   - simple CGI script using Eliza.pm
  1242.     debug.cgi    - CGI script which displays debugging output
  1243.     deutsch      - script using the German script
  1244.     deutsch.cgi  - CGI script using the German script
  1245.     twobots      - script which creates two distinct bots
  1246.  
  1247. =item * Version 0.32 - December 1997
  1248.  
  1249.   Fixed a bug in the way Eliza loads its default internal script data.
  1250.   (Thanks to Randal Schwartz for pointing this out.) 
  1251.  
  1252.   Removed the "memory" functions internal to Eliza.  
  1253.   When I get them working properly I will add them back in. 
  1254.  
  1255.   Added one more example program.
  1256.  
  1257.   Fixed some minor errors in the embedded POD documentation.
  1258.  
  1259. =item * Version 0.31
  1260.  
  1261.   The module is now installable, just like any other self-respecting
  1262.   CPAN module.  
  1263.  
  1264. =item * Version 0.30
  1265.  
  1266.   First release.
  1267.  
  1268. =back
  1269.  
  1270.  
  1271. =head1 AUTHOR
  1272.  
  1273. John Nolan  jpnolan@sonic.net  January 2003. 
  1274.  
  1275. Implements the classic Eliza algorithm by Prof. Joseph Weizenbaum. 
  1276. Script format devised by Charles Hayden. 
  1277.  
  1278. =cut
  1279.  
  1280.  
  1281.  
  1282. ####################################################################
  1283. # ---{ B E G I N   D E F A U L T   S C R I P T   D A T A }----------
  1284. #
  1285. #  This script was prepared by Chris Hayden.  Hayden's Eliza 
  1286. #  program was written in Java, however, it attempted to match 
  1287. #  the functionality of Weizenbaum's original program as closely 
  1288. #  as possible.  
  1289. #
  1290. #  Hayden's script format was quite different from Weizenbaum's, 
  1291. #  but it maintained the same content.  I have adapted Hayden's 
  1292. #  script format, since it was simple and convenient enough 
  1293. #  for my purposes.  
  1294. #
  1295. #  I've made small modifications here and there.  
  1296. #
  1297.  
  1298. # We use the token __DATA__ rather than __END__, 
  1299. # so that all this data is visible within the current package.
  1300.  
  1301. __DATA__
  1302. initial: How do you do.  Please tell me your problem.
  1303. initial: Hello, I am a computer program. 
  1304. initial: Please tell me what's been bothering you. 
  1305. initial: Is something troubling you?
  1306. final: Goodbye.  It was nice talking to you.
  1307. final: Goodbye.  I hope you found this session helpful.
  1308. final: I think you should talk to a REAL analyst.  Ciao! 
  1309. final: Life is tough.  Hang in there!
  1310. quit: bye
  1311. quit: goodbye
  1312. quit: done
  1313. quit: exit
  1314. quit: quit
  1315. pre: dont don't
  1316. pre: cant can't
  1317. pre: wont won't
  1318. pre: recollect remember
  1319. pre: recall remember
  1320. pre: dreamt dreamed
  1321. pre: dreams dream
  1322. pre: maybe perhaps
  1323. pre: certainly yes
  1324. pre: machine computer
  1325. pre: machines computer
  1326. pre: computers computer
  1327. post: am are
  1328. post: your my
  1329. post: yours mine
  1330. pre: were was
  1331. post: me you
  1332. pre: you're you are
  1333. pre: i'm i am
  1334. post: myself yourself
  1335. post: yourself myself
  1336. post: i you
  1337. post: you me
  1338. post: my your
  1339. post: me you
  1340. post: i'm you are
  1341. pre: same alike
  1342. pre: identical alike
  1343. pre: equivalent alike
  1344. synon: belief feel think believe wish
  1345. synon: family mother mom father dad sister brother wife children child
  1346. synon: desire want need
  1347. synon: sad unhappy depressed sick
  1348. synon: happy elated glad better
  1349. synon: cannot can't
  1350. synon: everyone everybody nobody noone
  1351. synon: be am is are was
  1352. key: xnone -1
  1353.   decomp: *
  1354.     reasmb: I'm not sure I understand you fully.
  1355.     reasmb: Please go on.
  1356.     reasmb: That is interesting.  Please continue.
  1357.     reasmb: Tell me more about that.
  1358.     reasmb: Does talking about this bother you ?
  1359. key: sorry 0
  1360.   decomp: *
  1361.     reasmb: Please don't apologise.
  1362.     reasmb: Apologies are not necessary.
  1363.     reasmb: I've told you that apologies are not required.
  1364.     reasmb: It did not bother me.  Please continue.
  1365. key: apologise 0
  1366.   decomp: *
  1367.     reasmb: goto sorry
  1368. key: remember 5
  1369.   decomp: * i remember *
  1370.     reasmb: Do you often think of (2) ?
  1371.     reasmb: Does thinking of (2) bring anything else to mind ?
  1372.     reasmb: What else do you recollect ?
  1373.     reasmb: Why do you remember (2) just now ?
  1374.     reasmb: What in the present situation reminds you of (2) ?
  1375.     reasmb: What is the connection between me and (2) ?
  1376.     reasmb: What else does (2) remind you of ?
  1377.   decomp: * do you remember *
  1378.     reasmb: Did you think I would forget (2) ?
  1379.     reasmb: Why do you think I should recall (2) now ?
  1380.     reasmb: What about (2) ?
  1381.     reasmb: goto what
  1382.     reasmb: You mentioned (2) ?
  1383.   decomp: * you remember *
  1384.     reasmb: How could I forget (2) ?
  1385.     reasmb: What about (2) should I remember ?
  1386.     reasmb: goto you
  1387. key: forget 5
  1388.   decomp: * i forget *
  1389.     reasmb: Can you think of why you might forget (2) ?
  1390.     reasmb: Why can't you remember (2) ?
  1391.     reasmb: How often do you think of (2) ?
  1392.     reasmb: Does it bother you to forget that ?
  1393.     reasmb: Could it be a mental block ?
  1394.     reasmb: Are you generally forgetful ?
  1395.     reasmb: Do you think you are suppressing (2) ?
  1396.   decomp: * did you forget *
  1397.     reasmb: Why do you ask ?
  1398.     reasmb: Are you sure you told me ?
  1399.     reasmb: Would it bother you if I forgot (2) ?
  1400.     reasmb: Why should I recall (2) just now ?
  1401.     reasmb: goto what
  1402.     reasmb: Tell me more about (2).
  1403. key: if 3
  1404.   decomp: * if *
  1405.     reasmb: Do you think its likely that (2) ?
  1406.     reasmb: Do you wish that (2) ?
  1407.     reasmb: What do you know about (2) ?
  1408.     reasmb: Really, if (2) ?
  1409.     reasmb: What would you do if (2) ?
  1410.     reasmb: But what are the chances that (2) ?
  1411.     reasmb: What does this speculation lead to ?
  1412. key: dreamed 4
  1413.   decomp: * i dreamed *
  1414.     reasmb: Really, (2) ?
  1415.     reasmb: Have you ever fantasized (2) while you were awake ?
  1416.     reasmb: Have you ever dreamed (2) before ?
  1417.     reasmb: goto dream
  1418. key: dream 3
  1419.   decomp: *
  1420.     reasmb: What does that dream suggest to you ?
  1421.     reasmb: Do you dream often ?
  1422.     reasmb: What persons appear in your dreams ?
  1423.     reasmb: Do you believe that dreams have something to do with your problem ?
  1424. key: perhaps 0
  1425.   decomp: *
  1426.     reasmb: You don't seem quite certain.
  1427.     reasmb: Why the uncertain tone ?
  1428.     reasmb: Can't you be more positive ?
  1429.     reasmb: You aren't sure ?
  1430.     reasmb: Don't you know ?
  1431.     reasmb: How likely, would you estimate ?
  1432. key: name 15
  1433.   decomp: *
  1434.     reasmb: I am not interested in names.
  1435.     reasmb: I've told you before, I don't care about names -- please continue.
  1436. key: deutsch 0
  1437.   decomp: *
  1438.     reasmb: goto xfremd
  1439.     reasmb: I told you before, I don't understand German.
  1440. key: francais 0
  1441.   decomp: *
  1442.     reasmb: goto xfremd
  1443.     reasmb: I told you before, I don't understand French.
  1444. key: italiano 0
  1445.   decomp: *
  1446.     reasmb: goto xfremd
  1447.     reasmb: I told you before, I don't understand Italian.
  1448. key: espanol 0
  1449.   decomp: *
  1450.     reasmb: goto xfremd
  1451.     reasmb: I told you before, I don't understand Spanish.
  1452. key: xfremd 0
  1453.   decomp: *
  1454.     reasmb: I speak only English.
  1455. key: hello 0
  1456.   decomp: *
  1457.     reasmb: How do you do.  Please state your problem.
  1458.     reasmb: Hi.  What seems to be your problem ?
  1459. key: computer 50
  1460.   decomp: *
  1461.     reasmb: Do computers worry you ?
  1462.     reasmb: Why do you mention computers ?
  1463.     reasmb: What do you think machines have to do with your problem ?
  1464.     reasmb: Don't you think computers can help people ?
  1465.     reasmb: What about machines worries you ?
  1466.     reasmb: What do you think about machines ?
  1467. key: am 0
  1468.   decomp: * am i *
  1469.     reasmb: Do you believe you are (2) ?
  1470.     reasmb: Would you want to be (2) ?
  1471.     reasmb: Do you wish I would tell you you are (2) ?
  1472.     reasmb: What would it mean if you were (2) ?
  1473.     reasmb: goto what
  1474.   decomp: * i am *
  1475.     reasmb: goto i
  1476.   decomp: *
  1477.     reasmb: Why do you say 'am' ?
  1478.     reasmb: I don't understand that.
  1479. key: are 0
  1480.   decomp: * are you *
  1481.     reasmb: Why are you interested in whether I am (2) or not ?
  1482.     reasmb: Would you prefer if I weren't (2) ?
  1483.     reasmb: Perhaps I am (2) in your fantasies.
  1484.     reasmb: Do you sometimes think I am (2) ?
  1485.     reasmb: goto what
  1486.     reasmb: Would it matter to you ?
  1487.     reasmb: What if I were (2) ?
  1488.   decomp: * you are *
  1489.     reasmb: goto you
  1490.   decomp: * are *
  1491.     reasmb: Did you think they might not be (2) ?
  1492.     reasmb: Would you like it if they were not (2) ?
  1493.     reasmb: What if they were not (2) ?
  1494.     reasmb: Are they always (2) ?
  1495.         reasmb: Possibly they are (2).
  1496.     reasmb: Are you positive they are (2) ?
  1497. key: your 0
  1498.   decomp: * your *
  1499.     reasmb: Why are you concerned over my (2) ?
  1500.     reasmb: What about your own (2) ?
  1501.     reasmb: Are you worried about someone else's (2) ?
  1502.     reasmb: Really, my (2) ?
  1503.     reasmb: What makes you think of my (2) ?
  1504.     reasmb: Do you want my (2) ?
  1505. key: was 2
  1506.   decomp: * was i *
  1507.     reasmb: What if you were (2) ?
  1508.     reasmb: Do you think you were (2) ?
  1509.     reasmb: Were you (2) ?
  1510.     reasmb: What would it mean if you were (2) ?
  1511.     reasmb: What does ' (2) ' suggest to you ?
  1512.     reasmb: goto what
  1513.   decomp: * i was *
  1514.     reasmb: Were you really ?
  1515.     reasmb: Why do you tell me you were (2) now ?
  1516.     reasmb: Perhaps I already know you were (2).
  1517.   decomp: * was you *
  1518.     reasmb: Would you like to believe I was (2) ?
  1519.     reasmb: What suggests that I was (2) ?
  1520.     reasmb: What do you think ?
  1521.     reasmb: Perhaps I was (2).
  1522.     reasmb: What if I had been (2) ?
  1523. key: i 0
  1524.   decomp: * i @desire *
  1525.     reasmb: What would it mean to you if you got (3) ?
  1526.     reasmb: Why do you want (3) ?
  1527.     reasmb: Suppose you got (3) soon.
  1528.     reasmb: What if you never got (3) ?
  1529.     reasmb: What would getting (3) mean to you ?
  1530.     reasmb: What does wanting (3) have to do with this discussion ?
  1531.   decomp: * i am* @sad *
  1532.     reasmb: I am sorry to hear that you are (3).
  1533.     reasmb: Do you think coming here will help you not to be (3) ?
  1534.     reasmb: I'm sure it's not pleasant to be (3).
  1535.     reasmb: Can you explain what made you (3) ?
  1536.   decomp: * i am* @happy *
  1537.     reasmb: How have I helped you to be (3) ?
  1538.     reasmb: Has your treatment made you (3) ?
  1539.     reasmb: What makes you (3) just now ?
  1540.     reasmb: Can you explain why you are suddenly (3) ?
  1541.   decomp: * i was *
  1542.     reasmb: goto was
  1543.   decomp: * i @belief i *
  1544.     reasmb: Do you really think so ?
  1545.     reasmb: But you are not sure you (3).
  1546.     reasmb: Do you really doubt you (3) ?
  1547.   decomp: * i* @belief *you *
  1548.     reasmb: goto you
  1549.   decomp: * i am *
  1550.     reasmb: Is it because you are (2) that you came to me ?
  1551.     reasmb: How long have you been (2) ?
  1552.     reasmb: Do you believe it is normal to be (2) ?
  1553.     reasmb: Do you enjoy being (2) ?
  1554.     reasmb: Do you know anyone else who is (2) ?
  1555.   decomp: * i @cannot *
  1556.     reasmb: How do you know that you can't (3) ?
  1557.     reasmb: Have you tried ?
  1558.     reasmb: Perhaps you could (3) now.
  1559.     reasmb: Do you really want to be able to (3) ?
  1560.     reasmb: What if you could (3) ?
  1561.   decomp: * i don't *
  1562.     reasmb: Don't you really (2) ?
  1563.     reasmb: Why don't you (2) ?
  1564.     reasmb: Do you wish to be able to (2) ?
  1565.     reasmb: Does that trouble you ?
  1566.   decomp: * i feel *
  1567.     reasmb: Tell me more about such feelings.
  1568.     reasmb: Do you often feel (2) ?
  1569.     reasmb: Do you enjoy feeling (2) ?
  1570.     reasmb: Of what does feeling (2) remind you ?
  1571.   decomp: * i * you *
  1572.     reasmb: Perhaps in your fantasies we (2) each other.
  1573.     reasmb: Do you wish to (2) me ?
  1574.     reasmb: You seem to need to (2) me.
  1575.     reasmb: Do you (2) anyone else ?
  1576.   decomp: *
  1577.     reasmb: You say (1) ?
  1578.     reasmb: Why do you say (1) ?
  1579.     reasmb: Can you elaborate on that ?
  1580.     reasmb: Do you say (1) for some special reason ?
  1581.     reasmb: That's quite interesting.
  1582. key: you 0
  1583.   decomp: * you remind me of *
  1584.     reasmb: goto alike
  1585.   decomp: * you are *
  1586.     reasmb: What makes you think I am (2) ?
  1587.     reasmb: Does it please you to believe I am (2) ?
  1588.     reasmb: Do you sometimes wish you were (2) ?
  1589.     reasmb: Perhaps you would like to be (2).
  1590.   decomp: * you* me *
  1591.     reasmb: Why do you think I (2) you ?
  1592.     reasmb: You like to think I (2) you -- don't you ?
  1593.     reasmb: What makes you think I (2) you ?
  1594.     reasmb: Really, I (2) you ?
  1595.     reasmb: Do you wish to believe I (2) you ?
  1596.     reasmb: Suppose I did (2) you -- what would that mean ?
  1597.     reasmb: Does someone else believe I (2) you ?
  1598.   decomp: * you *
  1599.     reasmb: We were discussing you -- not me.
  1600.     reasmb: Oh, I (2) ?
  1601.     reasmb: You're not really talking about me -- are you ?
  1602.     reasmb: What are your feelings now ?
  1603. key: yes 0
  1604.   decomp: *
  1605.     reasmb: Please go on.
  1606.     reasmb: Please tell me some more about this.
  1607.     reasmb: Why don't you tell me a little more about this.
  1608.     reasmb: I see.
  1609.     reasmb: I understand.
  1610. key: no 0
  1611.   decomp: * no one *
  1612.     reasmb: Are you sure, no one (2) ?
  1613.     reasmb: Surely someone (2) .
  1614.     reasmb: Can you think of anyone at all ?
  1615.     reasmb: Are you thinking of a very special person ?
  1616.     reasmb: Who, may I ask ?
  1617.     reasmb: You have a particular person in mind, don't you ?
  1618.     reasmb: Who do you think you are talking about ?
  1619.   decomp: *
  1620.     reasmb: Are you saying no just to be negative?
  1621.     reasmb: Does this make you feel unhappy? 
  1622.     reasmb: Why not ?
  1623.     reasmb: Why 'no' ?
  1624. key: my 2
  1625.   decomp: * my* @family *
  1626.     reasmb: Tell me more about your family.
  1627.     reasmb: Who else in your family (4) ?
  1628.     reasmb: Your (3) ?
  1629.     reasmb: What else comes to mind when you think of your (3) ?
  1630.   decomp: * my *
  1631.     reasmb: Your (2) ?
  1632.     reasmb: Why do you say your (2) ?
  1633.     reasmb: Does that suggest anything else which belongs to you ?
  1634.     reasmb: Is it important to you that your (2) ?
  1635.     reasm_for_memory: Let's discuss further why your (2).
  1636.     reasm_for_memory: Earlier you said your (2).
  1637.     reasm_for_memory: But your (2).
  1638.     reasm_for_memory: Does that have anything to do with the fact that your (2) ?
  1639. key: can 0
  1640.   decomp: * can you *
  1641.     reasmb: You believe I can (2) don't you ?
  1642.     reasmb: goto what
  1643.     reasmb: You want me to be able to (2).
  1644.     reasmb: Perhaps you would like to be able to (2) yourself.
  1645.   decomp: * can i *
  1646.     reasmb: Whether or not you can (2) depends on you more than on me.
  1647.     reasmb: Do you want to be able to (2) ?
  1648.     reasmb: Perhaps you don't want to (2).
  1649.     reasmb: goto what
  1650. key: what 0
  1651.   decomp: *
  1652.     reasmb: Why do you ask ?
  1653.     reasmb: Does that question interest you ?
  1654.     reasmb: What is it you really want to know ?
  1655.     reasmb: Are such questions much on your mind ?
  1656.     reasmb: What answer would please you most ?
  1657.     reasmb: What do you think ?
  1658.     reasmb: What comes to mind when you ask that ?
  1659.     reasmb: Have you asked such questions before ?
  1660.     reasmb: Have you asked anyone else ?
  1661. key: who 0
  1662.   decomp:  who *
  1663.     reasmb: goto what
  1664. key: when 0
  1665.   decomp:  when *
  1666.     reasmb: goto what
  1667. key: where 0
  1668.   decomp:  where *
  1669.     reasmb: goto what
  1670. key: how 0
  1671.   decomp:  how *
  1672.     reasmb: goto what
  1673. key: because 0
  1674.   decomp: *
  1675.     reasmb: Is that the real reason ?
  1676.     reasmb: Don't any other reasons come to mind ?
  1677.     reasmb: Does that reason seem to explain anything else ?
  1678.     reasmb: What other reasons might there be ?
  1679. key: why 0
  1680.   decomp: * why don't you *
  1681.     reasmb: Do you believe I don't (2) ?
  1682.     reasmb: Perhaps I will (2) in good time.
  1683.     reasmb: Should you (2) yourself ?
  1684.     reasmb: You want me to (2) ?
  1685.     reasmb: goto what
  1686.   decomp: * why can't i *
  1687.     reasmb: Do you think you should be able to (2) ?
  1688.     reasmb: Do you want to be able to (2) ?
  1689.     reasmb: Do you believe this will help you to (2) ?
  1690.     reasmb: Have you any idea why you can't (2) ?
  1691.     reasmb: goto what
  1692.   decomp: *
  1693.     reasmb: goto what
  1694. key: everyone 2
  1695.   decomp: * @everyone *
  1696.     reasmb: Really, (2) ?
  1697.     reasmb: Surely not (2).
  1698.     reasmb: Can you think of anyone in particular ?
  1699.     reasmb: Who, for example?
  1700.     reasmb: Are you thinking of a very special person ?
  1701.     reasmb: Who, may I ask ?
  1702.     reasmb: Someone special perhaps ?
  1703.     reasmb: You have a particular person in mind, don't you ?
  1704.     reasmb: Who do you think you're talking about ?
  1705. key: everybody 2
  1706.   decomp: *
  1707.     reasmb: goto everyone
  1708. key: nobody 2
  1709.   decomp: *
  1710.     reasmb: goto everyone
  1711. key: noone 2
  1712.   decomp: *
  1713.     reasmb: goto everyone
  1714. key: always 1
  1715.   decomp: *
  1716.     reasmb: Can you think of a specific example ?
  1717.     reasmb: When ?
  1718.     reasmb: What incident are you thinking of ?
  1719.     reasmb: Really, always ?
  1720. key: alike 10
  1721.   decomp: *
  1722.     reasmb: In what way ?
  1723.     reasmb: What resemblence do you see ?
  1724.     reasmb: What does that similarity suggest to you ?
  1725.     reasmb: What do you suppose that resemblence means ?
  1726.     reasmb: What is the connection, do you suppose ?
  1727.     reasmb: Could there really be some connection ?
  1728.     reasmb: How ?
  1729. key: like 10
  1730.   decomp: * @be *like *
  1731.     reasmb: goto alike
  1732. key: different 0
  1733.   decomp: *
  1734.     reasmb: How is it different ?
  1735.     reasmb: What differences do you see ?
  1736.     reasmb: What does that difference suggest to you ?
  1737.     reasmb: What other distinctions do you see ?
  1738.     reasmb: What do you suppose that disparity means ?
  1739.     reasmb: Could there be some connection, do you suppose ?
  1740.     reasmb: How ?
  1741. key: fuck 10
  1742.   decomp: * 
  1743.     reasmb: goto xswear
  1744. key: fucker 10
  1745.   decomp: * 
  1746.     reasmb: goto xswear
  1747. key: shit 10
  1748.   decomp: * 
  1749.     reasmb: goto xswear
  1750. key: damn 10
  1751.   decomp: * 
  1752.     reasmb: goto xswear
  1753. key: shut 10
  1754.   decomp: * shut up *
  1755.     reasmb: goto xswear
  1756. key: xswear 10
  1757.   decomp: * 
  1758.     reasmb: Does it make you feel strong to use that kind of language ?
  1759.     reasmb: Are you venting your feelings now ?
  1760.     reasmb: Are you angry ?
  1761.     reasmb: Does this topic make you feel angry ? 
  1762.     reasmb: Is something making you feel angry ? 
  1763.     reasmb: Does using that kind of language make you feel better ? 
  1764.