eris2010

Documentation: http://frombelow.net/projects/eris2010/
Clone: git clone https://git.frombelow.net/eris2010.git
Log | Files | Refs | README | LICENSE

eliza.c (12662B)


      1 /*
      2  * Original Source:
      3  * https://github.com/mottosso/cs50x/blob/master/eliza.c
      4  * with some minor changes by Gerd Beuster for CC65
      5  *
      6  * eliza.c  
      7  * ys
      8  * original code by Weizenbaum, 1966
      9  * this rendition based on Charles Hayden's Java implementation from http://chayden.net/eliza/Eliza.html
     10  *
     11  * Note: There are certainly far more optimal and elegant ways to code this... we kept this
     12  * structure to be faithful to the original.  -scaz 
     13  */
     14 
     15 #include <ctype.h>
     16 #include <stdio.h>
     17 #include <string.h>
     18 #include <stdlib.h>
     19 #include <conio.h>
     20 
     21 #define NUMKEYWORDS 37
     22 #define MAXLINELEN 80
     23 #define NUMSWAPS 14
     24 
     25 const char *keywords[]= {
     26        "CAN YOU","CAN I","YOU ARE","YOURE","I DONT","I FEEL",
     27        "WHY DONT YOU","WHY CANT I","ARE YOU","I CANT","I AM","IM ",
     28        "YOU ","I WANT","WHAT","HOW","WHO","WHERE",
     29        "WHEN","WHY",
     30        "NAME","CAUSE","SORRY","DREAM","HELLO","HI ","MAYBE",
     31        " NO","YOUR","ALWAYS","THINK","ALIKE","YES","FRIEND",
     32        "COMPUTER","CAR","NOKEYFOUND"};
     33 
     34 const char *SWAPS[NUMSWAPS][2] = {
     35     {"ARE","AM"},
     36     {"WERE", "WAS"},
     37     {"YOU","I"},
     38     {"YOUR", "MY"},
     39     {"IVE", "YOU'VE"},
     40     {"IM", "YOU'RE"},
     41     {"YOU", "ME"},
     42     {"ME", "YOU"},
     43     {"AM","ARE"},
     44     {"WAS", "WERE"},
     45     {"I","YOU"},
     46     {"MY", "YOUR"},
     47     {"YOUVE", "I'VE"},
     48     {"YOURE", "I'M"}
     49 };
     50 
     51 int ResponsesPerKeyword[NUMKEYWORDS]= {
     52        3,2,4,4,4,3,
     53        3,2,3,3,4,4,
     54        3,5,9,9,9,9,
     55        9,9,
     56        2,4,4,4,1,1,5,
     57        5,2,4,3,7,3,6,
     58        7,5,6};
     59            
     60 const char *responses[NUMKEYWORDS][9] = { 
     61     {   "DON'T YOU BELIEVE THAT I CAN*",
     62         "PERHAPS YOU WOULD LIKE TO BE ABLE TO*",
     63         "YOU WANT ME TO BE ABLE TO*"},
     64     {   "PERHAPS YOU DON'T WANT TO*",
     65         "DO YOU WANT TO BE ABLE TO*"},
     66     {   "WHAT MAKES YOU THINK I AM*",
     67         "DOES IT PLEASE YOU TO BELIEVE I AM*",
     68         "PERHAPS YOU WOULD LIKE TO BE*",
     69         "DO YOU SOMETIMES WISH YOU WERE*"},    
     70     {   "WHAT MAKES YOU THINK I AM*",
     71         "DOES IT PLEASE YOU TO BELIEVE I AM*",
     72         "PERHAPS YOU WOULD LIKE TO BE*",
     73         "DO YOU SOMETIMES WISH YOU WERE*"},
     74     {   "DON'T YOU REALLY*",
     75         "WHY DON'T YOU*",
     76         "DO YOU WISH TO BE ABLE TO*",
     77         "DOES THAT TROUBLE YOU?"},
     78     {   "TELL ME MORE ABOUT SUCH FEELINGS.",
     79         "DO YOU OFTEN FEEL*",
     80         "DO YOU ENJOY FEELING*"},
     81     {   "DO YOU REALLY BELIEVE I DON'T*",
     82         "PERHAPS IN GOOD TIME I WILL*",
     83         "DO YOU WANT ME TO*"},
     84     {   "DO YOU THINK YOU SHOULD BE ABLE TO*",
     85         "WHY CAN'T YOU*"},
     86     {   "WHY ARE YOU INTERESTED IN WHETHER OR NOT I AM*",
     87         "WOULD YOU PREFER IF I WERE NOT*",
     88         "PERHAPS IN YOUR FANTASIES I AM*"},
     89     {   "HOW DO YOU KNOW YOU CAN'T*",
     90         "HAVE YOU TRIED?",
     91         "PERHAPS YOU CAN NOW*"},
     92     {   "DID YOU COME TO ME BECAUSE YOU ARE*",
     93         "HOW LONG HAVE YOU BEEN*",
     94         "DO YOU BELIEVE IT IS NORMAL TO BE*",
     95         "DO YOU ENJOY BEING*"},
     96     {   "DID YOU COME TO ME BECAUSE YOU ARE*",
     97         "HOW LONG HAVE YOU BEEN*",
     98         "DO YOU BELIEVE IT IS NORMAL TO BE*",
     99         "DO YOU ENJOY BEING*"},
    100     {   "WE WERE DISCUSSING YOU-- NOT ME.",
    101         "OH, I*",
    102         "YOU'RE NOT REALLY TALKING ABOUT ME, ARE YOU?"},
    103     {   "WHAT WOULD IT MEAN TO YOU IF YOU GOT*",
    104         "WHY DO YOU WANT*",
    105         "SUPPOSE YOU SOON GOT*",
    106         "WHAT IF YOU NEVER GOT*",
    107         "I SOMETIMES ALSO WANT*"},
    108     {   "WHY DO YOU ASK?",
    109         "DOES THAT QUESTION INTEREST YOU?",
    110         "WHAT ANSWER WOULD PLEASE YOU THE MOST?",
    111         "WHAT DO YOU THINK?",
    112         "ARE SUCH QUESTIONS ON YOUR MIND OFTEN?",
    113         "WHAT IS IT THAT YOU REALLY WANT TO KNOW?",
    114         "HAVE YOU ASKED ANYONE ELSE?",
    115         "HAVE YOU ASKED SUCH QUESTIONS BEFORE?",
    116         "WHAT ELSE COMES TO MIND WHEN YOU ASK THAT?"},
    117     {   "WHY DO YOU ASK?",
    118         "DOES THAT QUESTION INTEREST YOU?",
    119         "WHAT ANSWER WOULD PLEASE YOU THE MOST?",
    120         "WHAT DO YOU THINK?",
    121         "ARE SUCH QUESTIONS ON YOUR MIND OFTEN?",
    122         "WHAT IS IT THAT YOU REALLY WANT TO KNOW?",
    123         "HAVE YOU ASKED ANYONE ELSE?",
    124         "HAVE YOU ASKED SUCH QUESTIONS BEFORE?",
    125         "WHAT ELSE COMES TO MIND WHEN YOU ASK THAT?"},
    126     {   "WHY DO YOU ASK?",
    127         "DOES THAT QUESTION INTEREST YOU?",
    128         "WHAT ANSWER WOULD PLEASE YOU THE MOST?",
    129         "WHAT DO YOU THINK?",
    130         "ARE SUCH QUESTIONS ON YOUR MIND OFTEN?",
    131         "WHAT IS IT THAT YOU REALLY WANT TO KNOW?",
    132         "HAVE YOU ASKED ANYONE ELSE?",
    133         "HAVE YOU ASKED SUCH QUESTIONS BEFORE?",
    134         "WHAT ELSE COMES TO MIND WHEN YOU ASK THAT?"},
    135     {   "WHY DO YOU ASK?",
    136         "DOES THAT QUESTION INTEREST YOU?",
    137         "WHAT ANSWER WOULD PLEASE YOU THE MOST?",
    138         "WHAT DO YOU THINK?",
    139         "ARE SUCH QUESTIONS ON YOUR MIND OFTEN?",
    140         "WHAT IS IT THAT YOU REALLY WANT TO KNOW?",
    141         "HAVE YOU ASKED ANYONE ELSE?",
    142         "HAVE YOU ASKED SUCH QUESTIONS BEFORE?",
    143         "WHAT ELSE COMES TO MIND WHEN YOU ASK THAT?"},
    144     {   "WHY DO YOU ASK?",
    145         "DOES THAT QUESTION INTEREST YOU?",
    146         "WHAT ANSWER WOULD PLEASE YOU THE MOST?",
    147         "WHAT DO YOU THINK?",
    148         "ARE SUCH QUESTIONS ON YOUR MIND OFTEN?",
    149         "WHAT IS IT THAT YOU REALLY WANT TO KNOW?",
    150         "HAVE YOU ASKED ANYONE ELSE?",
    151         "HAVE YOU ASKED SUCH QUESTIONS BEFORE?",
    152         "WHAT ELSE COMES TO MIND WHEN YOU ASK THAT?"},
    153     {   "WHY DO YOU ASK?",
    154         "DOES THAT QUESTION INTEREST YOU?",
    155         "WHAT ANSWER WOULD PLEASE YOU THE MOST?",
    156         "WHAT DO YOU THINK?",
    157         "ARE SUCH QUESTIONS ON YOUR MIND OFTEN?",
    158         "WHAT IS IT THAT YOU REALLY WANT TO KNOW?",
    159         "HAVE YOU ASKED ANYONE ELSE?",
    160         "HAVE YOU ASKED SUCH QUESTIONS BEFORE?",
    161         "WHAT ELSE COMES TO MIND WHEN YOU ASK THAT?"},
    162     {   "NAMES DON'T INTEREST ME.",
    163         "I DON'T CARE ABOUT NAMES-- PLEASE GO ON."},
    164     {   "IS THAT THE REAL REASON?",
    165         "DON'T ANY OTHER REASONS COME TO MIND?",
    166         "DOES THAT REASON EXPLAIN ANY THING ELSE?",
    167         "WHAT OTHER REASONS MIGHT THERE BE?"},
    168     {   "PLEASE DON'T APOLOGIZE.",
    169         "APOLOGIES ARE NOT NECESSARY.",
    170         "WHAT FEELINGS DO YOU HAVE WHEN YOU APOLOGIZE?",
    171         "DON'T BE SO DEFENSIVE!"},
    172     {   "WHAT DOES THAT DREAM SUGGEST TO YOU?",
    173         "DO YOU DREAM OFTEN?",
    174         "WHAT PERSONS APPEAR IN YOUR DREAMS?",
    175         "ARE YOU DISTURBED BY YOUR DREAMS?"},
    176     {   "HOW DO YOU DO--PLEASE STATE YOUR PROBLEM."},
    177     {   "HOW DO YOU DO--PLEASE STATE YOUR PROBLEM."},
    178     {   "YOU DON'T SEEM QUITE CERTAIN.",
    179         "WHY THE UNCERTAIN TONE?",
    180         "CAN'T YOU BE MORE POSITIVE?",
    181         "YOU AREN'T SURE?",
    182         "DON'T YOU KNOW?"},
    183     {   "ARE YOU SAYING NO JUST TO BE NEGATIVE?",
    184         "YOU ARE BEING A BIT NEGATIVE.",
    185         "WHY NOT?",
    186         "ARE YOU SURE?",
    187         "WHY NO?"},
    188     {   "WHY ARE YOU CONCERNED ABOUT MY*",
    189         "WHAT ABOUT YOUR OWN*"},
    190     {   "CAN YOU THINK OF A SPECIFIC EXAMPLE?",
    191         "WHEN?",
    192         "WHAT ARE YOU THINKING OF?",
    193         "REALLY, ALWAYS?"},
    194     {   "DO YOU REALLY THINK SO?",
    195         "BUT YOU ARE NOT SURE YOU*",
    196         "DO YOU DOUBT YOU*"},
    197     {   "IN WHAT WAY?",
    198         "WHAT RESEMBLANCE DO YOU SEE?",
    199         "WHAT DOES THE SIMILARITY SUGGEST TO YOU?",
    200         "WHAT OTHER CONNECTIONS DO YOU SEE?",
    201         "COULD THERE REALLY BE SOME CONNECTION?",
    202         "HOW?"},
    203     {   "YOU SEEM QUITE POSITIVE.",
    204         "ARE YOU SURE?",
    205         "I SEE.",
    206         "I UNDERSTAND."},
    207     {   "WHY DO YOU BRING UP THE TOPIC OF FRIENDS?",
    208         "DO YOUR FRIENDS WORRY YOU?",
    209         "DO YOUR FRIENDS PICK ON YOU?",
    210         "ARE YOU SURE YOU HAVE ANY FRIENDS?",
    211         "DO YOU IMPOSE ON YOUR FRIENDS?",
    212         "PERHAPS YOUR LOVE FOR FRIENDS WORRIES YOU?"},
    213     {   "DO COMPUTERS WORRY YOU?",
    214         "ARE YOU TALKING ABOUT ME IN PARTICULAR?",
    215         "ARE YOU FRIGHTENED BY MACHINES?",
    216         "WHY DO YOU MENTION COMPUTERS?",
    217         "WHAT DO YOU THINK MACHINES HAVE TO DO WITH YOUR PROBLEM?",
    218         "DON'T YOU THINK COMPUTERS CAN HELP PEOPLE?",
    219         "WHAT IS IT ABOUT MACHINES THAT WORRIES YOU?"},
    220     {   "OH, DO YOU LIKE CARS?",
    221         "MY FAVORITE CAR IS A LAMBORGINI COUNTACH. WHAT IS YOUR FAVORITE CAR?",
    222         "MY FAVORITE CAR COMPANY IS FERRARI. WHAT IS YOURS?",
    223         "DO YOU LIKE PORSCHES?",
    224         "DO YOU LIKE PORSCHE TURBO CARRERAS?"},
    225     {   "SAY, DO YOU HAVE ANY PSYCHOLOGICAL PROBLEMS?",
    226         "WHAT DOES THAT SUGGEST TO YOU?",
    227         "I SEE.",
    228         "I'M NOT SURE I UNDERSTAND YOU FULLY.",
    229         "COME, COME ELUCIDATE YOUR THOUGHTS.",
    230         "CAN YOU ELABORATE ON THAT?",
    231         "THAT IS QUITE INTERESTING."}
    232         
    233     
    234 };
    235 
    236 void print_center(const char *msg) {
    237   int numspaces=(MAXLINELEN-strlen(msg))/2;
    238   int i;
    239   
    240   for(i=0;i<numspaces;i++)
    241     cprintf(" ");
    242   cprintf("%s\r\n", msg);
    243   return;
    244 }
    245 
    246 void print_title () {
    247     cprintf("\r\n\r\n");
    248     print_center("*** ELIZA ***");
    249     print_center("Original code by Weizenbaum, 1966");
    250     print_center("This is Marcus Ottosson's version from 2016");
    251     print_center("To stop Eliza, type 'bye'");
    252     cprintf("\r\n\r\n");
    253     cprintf("HI!  I'M ELIZA.  WHAT'S YOUR PROBLEM?\r\n");
    254 }
    255 
    256 void readline(char *instr) {
    257     char c;
    258     int slen=0;
    259 
    260     c=cgetc();
    261     cputc(c);
    262     while (c != '\r')
    263     {
    264         // removes punctuation and sets to uppercase
    265         if(isalpha(c) || isspace(c))
    266             instr[slen++]=toupper(c);
    267         if(slen>MAXLINELEN-1)
    268             {
    269                 cprintf("Exceeded Max Line Length\r\n");
    270                 exit(0);
    271             }
    272         c=cgetc();
    273 	cputc(c);
    274     }
    275     cputc('\n');
    276     instr[slen]='\0';
    277 }
    278 
    279 
    280 // Number of local variables of CC65 limited,
    281 // therefore we make all variables of main
    282 // global.
    283 int k,baseLength; 
    284 int whichReply[NUMKEYWORDS];
    285 char lastinput[MAXLINELEN];
    286 char reply[MAXLINELEN];
    287 char *baseResponse, *token;
    288 const char separator[2]=" ";
    289 char inputstr[MAXLINELEN];
    290 int x;
    291 char *location;
    292 int s;
    293 void main(void)
    294 {
    295     
    296     // use the first reply for each keyword match the first time you see that keyword
    297     for (x=0;x< NUMKEYWORDS; x++) {
    298         whichReply[x] = 0;
    299     }
    300 
    301     // print a nice centered title screen
    302     print_title();
    303 
    304     lastinput[0]='\0';
    305     
    306     while (1) { 
    307         readline(inputstr);
    308                 
    309         // check for termination 
    310         if (strcmp(inputstr,"BYE")==0)
    311             break; 
    312 
    313         // check for repeated entries 
    314         if (strcmp(lastinput,inputstr)==0) 
    315         {
    316             cprintf("PLEASE DON'T REPEAT YOURSELF!\r\n");
    317             continue;
    318         }
    319         strncpy(lastinput,inputstr,strlen(inputstr)+1); 
    320 
    321         // see if any of the keywords is contained in the input 
    322         // if not, we use the last element of keywords as our default responses 
    323         strcpy(reply,"");
    324         for(k=0;k<NUMKEYWORDS-1;k++)
    325         {
    326             location=strstr(inputstr, keywords[k]); 
    327             if(location != NULL)
    328                 break;
    329         }
    330         
    331         // Build Eliza's response 
    332         // start with Eliza's canned response, based on the keyword match
    333         baseResponse = (char *) responses[k][whichReply[k]];
    334         baseLength = strlen(baseResponse);
    335 
    336         if(baseResponse[baseLength-1] != '*')
    337         {
    338             // if we have a baseResponse without an asterix, just use it as-is
    339             strcat(reply, baseResponse);
    340         }
    341         else
    342         {
    343             // if we do have an asterix, fill in the remaining with the user input
    344             // use all but the last character of the base response
    345             strncat(reply, baseResponse, baseLength-1);
    346 
    347             // now add in the rest of the user's input, starting at <location>
    348             // but skip over the keyword itself
    349             location+=strlen(keywords[k]);
    350             // take them one word at a time, so that we can substitute pronouns
    351             token = strtok(location, separator);
    352             while(token != NULL)
    353             {
    354                 for(s=0;s<NUMSWAPS;s++)
    355                 {   
    356                     if(strcmp(SWAPS[s][0], token) == 0)
    357                     {
    358                         token = (char *) SWAPS[s][1];
    359                         break;
    360                     }
    361                 }   
    362                 strcat(reply," ");
    363                 strcat(reply, token);
    364                 token=strtok(NULL, separator);
    365             };
    366             strcat(reply, "?");
    367         }
    368         cprintf("%s\r\n", reply);
    369          
    370         // next time, use the next appropriate reply for that keyword
    371         whichReply[k]++;
    372         if ( whichReply[k] >= ResponsesPerKeyword[k])
    373             whichReply[k] = 0;
    374 
    375     } 
    376 
    377     cprintf( "GOODBYE!  THANKS FOR VISITING WITH ME...\r\n");
    378     cgetc();
    379 
    380 }