summaryrefslogtreecommitdiffstats
path: root/zpu/roadshow/roadshow/games/eliza/parse.c
blob: aba20334e3856b3945d37ab9987fdfb40902a560 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/*
Copyright (C) 1988-2003 by Mohan Embar

http://www.thisiscool.com/
DISCLAIMER: This was written in 1988. I don't code like this anymore!

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later version. 

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details. 

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. 
*/

/* Program defined abstract data type called list.  A list contains
 * character strings each of which has a maximum length of 20 characters.
 *
 * Operations supported are:
 *
 *    
 */
typedef char WORD[40];
typedef WORD SENTENCE[200];

extern int numwords;
extern SENTENCE s;

/* Returns upper case value of c */
char upcase(c)
   char c;
{
   if (islower(c)) return toupper(c); else return c;
}

/* This function converts string1 into lowercase. */
void make_lower(string1)
   char *string1;
{
   char c;
   while (c = *string1) *string1++ = tolower(c);
}

/* Parses words in instring into WORD array s
 * Automatically translates n't to not, 're to are
 * Stores all letters in uppercase.  Sets numwords to number of tokens in
 * s.  Valid indices are from 1 .. numwords.
 */
void parse(instring)
   char *instring;
{
   char c; int i;
   int read_word = 0;
   int offset = -1;
   numwords = 0;
   while (c = *instring++) {
      switch(c) {
         case ' ' :
         case '\t' :
            read_word = 0;
            continue;
         case ',' :
         case '?' :
         case '.' :
         case ':' :
         case '"' :
            s[numwords++][++offset] = '\0';
            read_word = 0;
            s[numwords][offset = 0] = c;
            break;
         case '\'' :
            if ((s[numwords][offset] == 'N') && (upcase(*instring) == 'T')) {
               s[numwords++][offset]='\0';
               s[numwords][0] = 'N';
               s[numwords][offset = 1] = 'O';
               read_word = 1;
            }
            else if (upcase(*instring) == 'R') {
               s[numwords++][++offset] = '\0';
               s[numwords][offset = 0] = 'A';
               read_word = 1;
            }
            else if (upcase(*instring) == 'V') {
               s[numwords++][++offset] = '\0';
               s[numwords][offset = 0] = 'H';
               s[numwords][++offset] = 'A';
               read_word = 1;
            }
            else if (upcase(*instring) == 'M') {
               s[numwords++][++offset] = '\0';
               s[numwords][offset = 0] = 'A';
               read_word = 1;
            }
            else if (upcase(*instring) == 'L') {
               s[numwords++][++offset] = '\0';
               s[numwords][offset = 0] = 'W';
               s[numwords][++offset] = 'I';
               read_word = 1;
            }
            else if (upcase(*instring) == 'S') {
               s[numwords][offset+1] = '\0';
               if (!strcmp(s[numwords],"HE") || !strcmp(s[numwords],"SHE") 
                                             || !strcmp(s[numwords],"IT")) {
                  s[++numwords][offset = 0] = 'I';
                  read_word = 1;
               }
               else s[numwords][++offset] = '\'';
            }
            break;
         default :
            if (isalpha(c))
               if (read_word) {
                  s[numwords][++offset] = upcase(c);
               }
               else {
                  s[numwords++][++offset] = '\0';
                  ++read_word;
                  s[numwords][offset = 0] = upcase(c);
               }
            else {
               read_word = 0;
            }
      }  
   }
   s[numwords][++offset] = '\0';
   return;
}

/* Self-explanatory.  Used when computer spits back the sentence */
void agree()
{
   int i;
   for(i=1;i<=numwords;i++) {
      if (!strcmp(s[i],"I")) {
         strcpy(s[i],"YOU");
      }
      else if (!strcmp(s[i],"YOU")) {
         strcpy(s[i],"I");
      }
      else if (!strcmp(s[i],"YOUR")) {
         strcpy(s[i],"MY");
      }
      else if (!strcmp(s[i],"MY")) {
         strcpy(s[i],"YOUR");
      }
      else if (!strcmp(s[i],"YOU")) {
         strcpy(s[i],"ME");
      }
      else if (!strcmp(s[i],"ME")) {
         strcpy(s[i],"YOU");
      }
      else if (!strcmp(s[i],"MINE")) {
         strcpy(s[i],"YOURS");
      }
      else if (!strcmp(s[i],"YOURS")) {
         strcpy(s[i],"MINE");
      }
      else if (!strcmp(s[i],"WE")) {
         strcpy(s[i],"YOU");
      }
      else if (!strcmp(s[i],"YOURSELF")) {
         strcpy(s[i],"MYSELF");
      }
      else if (!strcmp(s[i],"MYSELF")) {
         strcpy(s[i],"YOURSELF");
      }
      else if (!strcmp(s[i],"OURSELVES")) {
         strcpy(s[i],"YOURSELVES");
      }
      else if (!strcmp(s[i],"OURS")) {
         strcpy(s[i],"YOURS");
      }
      else if (!strcmp(s[i],"OUR")) {
         strcpy(s[i],"YOUR");
      }
   }
   for (i=1;i<=numwords;i++) {
      if (!strcmp(s[i],"AM")) {
         strcpy(s[i],"ARE");
      }
      else if ( (!strcmp(s[i],"ARE")) &&
                (((i>0) && (!strcmp(s[i-1],"I"))) ||
                 ((i<numwords) && (!strcmp(s[i+1],"I"))) ) ) {
         strcpy(s[i],"AM");
      }
      else if ( (!strcmp(s[i],"WERE")) &&
                (((i>0) && (!strcmp(s[i-1],"I"))) ||
                 ((i<numwords) && (!strcmp(s[i+1],"I"))) ) ) {
         strcpy(s[i],"WAS");
      }
      else if ( (!strcmp(s[i],"WAS")) &&
                (((i>0) && (!strcmp(s[i-1],"YOU"))) ||
                 ((i<numwords) && (!strcmp(s[i+1],"YOU"))) ) ) {
         strcpy(s[i],"WERE");
      }
   }
}

/* Returns 1 if string1 matches first length(string1) characters of string2
 * and 0 if not.
 */
int matches(string1, string2)
   char *string1, *string2;
{
   int c;
   if (strlen(string1)>strlen(string2))
      return 0;
   else { /* length(string1)<=length(string2) */
      while (c = *string1++) {
         if (c != *string2++) return 0;
      }
      return 1;
   }   
}

/* Search WORD array s for search_string.  If exact = 1, enforce exact match.
 * Otherwise, return positive match if all characters of search_string match
 * the first length(search_string) characters of a WORD in s.  Assumes legal
 * values for exact are 0 or 1.  Returns index of match in s if match, 0 if
 * no match.
 */
int sword(s_string, exact)
   char *s_string;
   int exact;
{
   int i;
   for (i=1;i<=numwords;i++) {
      if (exact) {
         if (!strcmp(s_string,s[i])) return i;
      }
      else {
         if (matches(s_string,s[i])) return i;
      }
   }
   /* No match */
   return 0;
}

int bad_word()
{
   if (sword("\115\117\124\110\105\122\106\125\103\113",0))
      return 1;
   else if (sword("\106\125\103\113",0))
      return 1;
   else if (sword("\123\110\111\124",0))
      return 1;
   else if (sword("\101\123\123\110\117\114\105",1))
      return 1;
   else if (sword("\101\123\123",1))
      return 1;
   else return  0;
}

int naughty_word()
{
   if (sword("DAMN",0))
      return 1;
   else if (sword("STUPID",0))
      return 1;
   else if (sword("IDIOT",0))
      return 1;
   else if (sword("MORON",0))
      return 1;
   else if (sword("NUMBSKULL",0))
      return 1;
   else if (sword("IMBECILE",0))
      return 1;
   else if (sword("OBNOXIOUS",0))
      return 1;
   else return 0;
}

/* Return the index to a form of be or helping verb, if one exists
 * Otherwise, return 0.
 */
int find_helper()
{
   int x;
   if (x = sword("AM",1))
      return x;
   else if (x = sword("IS",1))
      return x;
   else if (x = sword("ARE",1))
      return x;
   else if (x = sword("WAS",1))
      return x;
   else if (x = sword("WERE",1))
      return x;
   else if (x = sword("WILL",1))
      return x;
   else if (x = sword("DO",1))
      return x;
   else if (x = sword("DID",1))
      return x;
   else if (x = sword("DOES",1))
      return x;
   else if (x = sword("HAVE",1))
      return x;
   else if (x = sword("HAD",1))
      return x;
   else if (x = sword("HAS",1))
      return x;
   else if (x = sword("SHALL",1))
      return x;
   else if (x = sword("SHOULD",1))
      return x;
   else if (x = sword("CAN",1))
      return x;
   else if (x = sword("COULD",1))
      return x;
   else if (x = sword("MAY",1))
      return x;
   else if (x = sword("MIGHT",1))
      return x;
   else return 0;
}

/* Returns 1 is string1 is pronoun.  0 if not. */
int is_sub_pronoun(string1)
   char *string1;
{
   if (!strcmp("I",string1))
      return 1;
   else if (!strcmp("YOU",string1))
      return 1;
   else if (!strcmp("WE",string1))
      return 1;
   else if (!strcmp("HE",string1))
      return 1;
   else if (!strcmp("SHE",string1))
      return 1;
   else if (!strcmp("IT",string1))
      return 1;
   else if (!strcmp("THEY",string1))
      return 1;
   else return 0;
}

int is_possesive(string1)
   char *string1;
{
   if (!strcmp("MY",string1))
      return 1;
   else if (!strcmp("YOUR",string1))
      return 1;
   else if (!strcmp("OUR",string1))
      return 1;
   else if (!strcmp("HIS",string1))
      return 1;
   else if (!strcmp("HER",string1))
      return 1;
   else if (!strcmp("ITS",string1))
      return 1;
   else if (!strcmp("THEIR",string1))
      return 1;
   else return 0;
}

int is_article(string1)
   char *string1;
{
   if (!strcmp("A",string1))
      return 1;
   else if (!strcmp("AN",string1))
      return 1;
   else if (!strcmp("THE",string1))
      return 1;
   else return 0;
}

/* Tries to find reference to a family member */
int family()
{
   int x;
   if (x = sword("MOTHER",1))
      return x;
   else if (x = sword("FATHER",1))
      return x;
   else if (x = sword("SISTER",1))
      return x;
   else if (x = sword("BROTHER",1))
      return x;
   else if (x = sword("DAD",1))
      return x;
   else if (x = sword("MOM",1))
      return x;
   else if (x = sword("UNCLE",1))
      return x;
   else if (x = sword("AUNT",1))
      return x;
   else if (x = sword("GRANDMOTHER",1))
      return x;
   else if (x = sword("GRANDFATHER",1))
      return x;
   else if (x = sword("COUSIN",1))
      return x;
   else if (x = sword("GRANDMA",1))
      return x;
   else if (x = sword("GRANDPA",1))
      return x;
   else return 0;
}

int i_am()
{
   int x, e=1;
   while (e) {
      for (x=e;x<=numwords;x++) if (!strcmp("I",s[x])) break;
      if (x >= numwords)
         return 0;
      else if (!strcmp("AM",s[x+1]))
         return x;
      else e = ++x;
   }
}

void get_til_stop(x,string1)
   int x;
   char *string1;
{
   char c, *temp;
   int e = 1;  /* Exit test */
   while (e) {
      if (x>numwords) {
         e--;
         *string1 = '\0';
      }
      else if (!isalpha(s[x][0])) {
         e--;
         *string1 = '\0';
      }
      else if (!strcmp("AND",s[x]) || !strcmp("OR",s[x])
                                   || !strcmp("BUT",s[x])) {
         e--;
         *string1 = '\0';
      }
      else {
         *string1++ = ' ';
         if (!strcmp("I",s[x]))
            *string1++ = 'I';
         else {
            temp = s[x];
            while (c = *temp++) *string1++ = tolower(c);
         }
         x++;
      }
   }
   *string1 = '\0';
}

int sad_word()
{
   int x;
   if (x = sword("DEPRESS",0))
      return x;
   else if (x = sword("UNHAPPY",1))
      return x;
   else if (x = sword("SAD",1))
      return x;
   else if (x = sword("MISERABLE",1))
      return x;
   else if (x = sword("AWFUL",1))
      return x;
   else if (x = sword("UPSET",1))
      return x;
   else if (x = sword("TERRIBLE",1))
      return x;
   else return 0;
}

int search_back_sub(x)
   int x;
{
   int y = --x;
   while (y) {
      if (is_possesive(s[y]) || is_sub_pronoun(s[y]) || is_article(s[y]))
         return y;
      else
         y--;
   }
   return y;
}

/* Returns 1 if string is a form of be or helping verb,
 * Otherwise, returns 0.
 */
int is_helper(string1)
   char *string1;
{
   if (!strcmp(string1,"AM"))
      return 1;
   else if (!strcmp(string1,"IS"))
      return 1;
   else if (!strcmp(string1,"ARE"))
      return 1;
   else if (!strcmp(string1,"WAS"))
      return 1;
   else if (!strcmp(string1,"WERE"))
      return 1;
   else if (!strcmp(string1,"WILL"))
      return 1;
   else if (!strcmp(string1,"DO"))
      return 1;
   else if (!strcmp(string1,"DID"))
      return 1;
   else if (!strcmp(string1,"DOES"))
      return 1;
   else if (!strcmp(string1,"HAVE"))
      return 1;
   else if (!strcmp(string1,"HAD"))
      return 1;
   else if (!strcmp(string1,"HAS"))
      return 1;
   else if (!strcmp(string1,"SHALL"))
      return 1;
   else if (!strcmp(string1,"SHOULD"))
      return 1;
   else if (!strcmp(string1,"CAN"))
      return 1;
   else if (!strcmp(string1,"COULD"))
      return 1;
   else if (!strcmp(string1,"MAY"))
      return 1;
   else if (!strcmp(string1,"MIGHT"))
      return 1;
   else if (matches(string1,"FEEL"))
      return 1;
   else return 0;
}

void getrange(y,x,string1)
   int y, x;
   char *string1;
{
   char c, *temp;
   while (y<=x) {
      *string1++ = ' ';
      if (!strcmp("I",s[y]))
         *string1++ = 'I';
      else {
         temp = s[y];
         while (c = *temp++) *string1++ = tolower(c);
      }
      y++;
   }
   *string1 = '\0';
}

/* Returns 1 if s[1] is a command.  0 if not. */
int is_command()
{
   if (!strcmp("GIVE",s[1]))
      return 1;
   else if (!strcmp("TELL",s[1]))
      return 1;
   else if (!strcmp("SHOW",s[1]))
      return 1;
   else if (!strcmp("EXPLAIN",s[1]))
      return 1;
   else return 0;
}

int four_ws()
{
   if (!strcmp(s[1],"WHO"))
      return 1;
   else if (!strcmp(s[1],"WHAT"))
      return 1;
   else if (!strcmp(s[1],"WHERE"))
      return 1;
   else if (!strcmp(s[1],"WHY"))
      return 1;
   else if (!strcmp(s[1],"WHEN"))
      return 1;
   else if (!strcmp(s[1],"HOW"))
      return 1;
   else return 0;
}

int vague_quest()
{
   return (four_ws() && (!is_helper(s[2]) || !is_sub_pronoun(s[3])));
}

int real_quest()
{
   return (four_ws() && is_helper(s[2]) && is_sub_pronoun(s[3]));
}

int sub_and_helper()
{
   int x;
   return ((x = find_helper()) && search_back_sub(x));
}

char *cnnv(string1)
   char *string1;
{
   if (!strcmp(string1," i")) {
      return " myself";
   }
   else if (!strcmp(string1," you")) {
      return " yourself";
   }
   else if (!strcmp(string1," we")) {
      return " ourselves";
   }
   else if (!strcmp(string1," he")) {
      return " him";
   }
   else if (!strcmp(string1," she")) {
      return " her";
   }
   else if (!strcmp(string1," it")) {
      return " it";
   }
   else if (!strcmp(string1," they")) {
      return " them";
   }
   if (!strcmp(string1,"i")) {
      return "myself";
   }
   else if (!strcmp(string1,"you")) {
      return "yourself";
   }
   else if (!strcmp(string1,"we")) {
      return "ourselves";
   }
   else if (!strcmp(string1,"he")) {
      return "him";
   }
   else if (!strcmp(string1,"she")) {
      return "her";
   }
   else if (!strcmp(string1,"it")) {
      return "it";
   }
   else if (!strcmp(string1,"they")) {
      return "them";
   }
   else return string1;
}

int is_be(string1)
   char *string1;
{
   if (!strcmp("am",string1))
      return 1;
   else if (!strcmp("is",string1))
      return 1;
   else if (!strcmp("are",string1))
      return 1;
   else if (!strcmp("was",string1))
      return 1;
   else if (!strcmp("were",string1))
      return 1;
   else return 0;
}

int can_spit_out()
{
   int x;
   for (x=1;x<=numwords;x++)
      if (is_possesive(s[x]) || is_sub_pronoun(s[x]) || is_article(s[x]))
         return x;
   return 0;
}

char *cnnv2(string1)
   char *string1;
{
   if (!strcmp(string1," i")) {
      return " me";
   }
   else if (!strcmp(string1," you")) {
      return " you";
   }
   else if (!strcmp(string1," we")) {
      return " us";
   }
   else if (!strcmp(string1," he")) {
      return " him";
   }
   else if (!strcmp(string1," she")) {
      return " her";
   }
   else if (!strcmp(string1," it")) {
      return " it";
   }
   else if (!strcmp(string1," they")) {
      return " them";
   }
   if (!strcmp(string1,"i")) {
      return "me";
   }
   else if (!strcmp(string1,"you")) {
      return "you";
   }
   else if (!strcmp(string1,"we")) {
      return "us";
   }
   else if (!strcmp(string1,"he")) {
      return "him";
   }
   else if (!strcmp(string1,"she")) {
      return "her";
   }
   else if (!strcmp(string1,"it")) {
      return "it";
   }
   else if (!strcmp(string1,"they")) {
      return "them";
   }
   else return string1;
}
OpenPOWER on IntegriCloud