1;;; doctor.el --- psychological help for frustrated users
2
3;; Copyright (C) 1985, 1987, 1994, 1996, 2000, 2001, 2002, 2003, 2004,
4;;   2005, 2006, 2007  Free Software Foundation, Inc.
5
6;; Maintainer: FSF
7;; Keywords: games
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING.  If not, write to the
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
25
26;;; Commentary:
27
28;; The single entry point `doctor', simulates a Rogerian analyst using
29;; phrase-production techniques similar to the classic ELIZA demonstration
30;; of pseudo-AI.
31
32;; This file was for a while censored by the Communications Decency Act.
33;; Some of its features were removed.  The law was promoted as a ban
34;; on pornography, but it bans far more than that.  The doctor program
35;; did not contain pornography, but part of it was prohibited
36;; nonetheless.
37
38;; The Supreme Court overturned the Communications Decency Act, but
39;; Congress is sure to look for some other way to try to end free speech.
40;; For information on US government censorship of the Internet, and
41;; what you can do to protect freedom of the press, see the web
42;; site http://www.vtw.org/
43;; See also the file etc/CENSORSHIP in the Emacs distribution
44;; for a discussion of why and how this file was censored, and the
45;; political implications of the issue.
46
47;;; Code:
48
49(defvar **mad**)        (defvar *debug*)      (defvar *print-space*)
50(defvar *print-upcase*) (defvar abuselst)     (defvar abusewords)
51(defvar account)        (defvar afraidof)     (defvar arerelated)
52(defvar areyou)         (defvar bak)          (defvar beclst)
53(defvar bother)         (defvar bye)          (defvar canyou)
54(defvar chatlst)        (defvar continue)     (defvar deathlst)
55(defvar describe)       (defvar drnk)         (defvar drugs)
56(defvar eliza-flag)     (defvar elizalst)     (defvar famlst)
57(defvar feared)         (defvar fears)        (defvar feelings-about)
58(defvar foullst)        (defvar found)        (defvar hello)
59(defvar history)        (defvar howareyoulst) (defvar howdyflag)
60(defvar huhlst)         (defvar ibelieve)     (defvar improve)
61(defvar inter)          (defvar isee)         (defvar isrelated)
62(defvar lincount)       (defvar longhuhlst)   (defvar lover)
63(defvar machlst)        (defvar mathlst)      (defvar maybe)
64(defvar moods)          (defvar neglst)       (defvar obj)
65(defvar object)         (defvar owner)        (defvar please)
66(defvar problems)       (defvar qlist)        (defvar random-adjective)
67(defvar relation)       (defvar remlst)       (defvar repetitive-shortness)
68(defvar replist)        (defvar rms-flag)     (defvar schoollst)
69(defvar sent)           (defvar sexlst)       (defvar shortbeclst)
70(defvar shortlst)       (defvar something)    (defvar sportslst)
71(defvar stallmanlst)    (defvar states)       (defvar subj)
72(defvar suicide-flag)   (defvar sure)         (defvar things)
73(defvar thlst)          (defvar toklst)       (defvar typos)
74(defvar verb)           (defvar want)         (defvar whatwhen)
75(defvar whereoutp)      (defvar whysay)       (defvar whywant)
76(defvar zippy-flag)     (defvar zippylst)
77
78(defun doc// (x) x)
79
80(defmacro doc$ (what)
81  "quoted arg form of doctor-$"
82  (list 'doctor-$ (list 'quote what)))
83
84(defun doctor-$ (what)
85  "Return the car of a list, rotating the list each time"
86  (let* ((vv (symbol-value what))
87	(first (car vv))
88	(ww (append (cdr vv) (list first))))
89    (set what ww)
90    first))
91
92(defvar doctor-mode-map
93  (let ((map (make-sparse-keymap)))
94    (define-key map "\n" 'doctor-read-print)
95    (define-key map "\r" 'doctor-ret-or-read)
96    map))
97
98(define-derived-mode doctor-mode text-mode "Doctor"
99  "Major mode for running the Doctor (Eliza) program.
100Like Text mode with Auto Fill mode
101except that RET when point is after a newline, or LFD at any time,
102reads the sentence before point, and prints the Doctor's answer."
103  (make-doctor-variables)
104  (turn-on-auto-fill)
105  (doctor-type '(i am the psychotherapist \.
106		 (doc$ please) (doc$ describe) your (doc$ problems) \.
107		 each time you are finished talking, type \R\E\T twice \.))
108  (insert "\n"))
109
110(defun make-doctor-variables ()
111  (make-local-variable 'typos)
112  (setq typos
113	(mapcar (function (lambda (x)
114			    (put (car x) 'doctor-correction  (cadr x))
115			    (put (cadr x) 'doctor-expansion (car (cddr x)))
116			    (car x)))
117		'((theyll they\'ll (they will))
118		  (theyre they\'re (they are))
119		  (hes he\'s (he is))
120		  (he7s he\'s (he is))
121		  (im i\'m (you are))
122		  (i7m i\'m (you are))
123		  (isa is\ a (is a))
124		  (thier their (their))
125		  (dont don\'t (do not))
126		  (don7t don\'t (do not))
127		  (you7re you\'re (i am))
128		  (you7ve you\'ve (i have))
129		  (you7ll you\'ll (i will)))))
130  (make-local-variable 'found)
131  (setq found nil)
132  (make-local-variable 'owner)
133  (setq owner nil)
134  (make-local-variable 'history)
135  (setq history nil)
136  (make-local-variable '*debug*)
137  (setq *debug* nil)
138  (make-local-variable 'inter)
139  (setq inter
140	'((well\,)
141	  (hmmm \.\.\.\ so\,)
142	  (so)
143	  (\.\.\.and)
144	  (then)))
145  (make-local-variable 'continue)
146  (setq continue
147	'((continue)
148	  (proceed)
149	  (go on)
150	  (keep going) ))
151  (make-local-variable 'relation)
152  (setq relation
153	'((your relationship with)
154	  (something you remember about)
155	  (your feelings toward)
156	  (some experiences you have had with)
157	  (how you feel about)))
158  (make-local-variable 'fears)
159  (setq fears '( ((doc$ whysay) you are (doc$ afraidof) (doc// feared) \?)
160		 (you seem terrified by (doc// feared) \.)
161		 (when did you first feel (doc$ afraidof) (doc// feared) \?) ))
162  (make-local-variable 'sure)
163  (setq sure '((sure)(positive)(certain)(absolutely sure)))
164  (make-local-variable 'afraidof)
165  (setq afraidof '( (afraid of) (frightened by) (scared of) ))
166  (make-local-variable 'areyou)
167  (setq areyou '( (are you)(have you been)(have you been) ))
168  (make-local-variable 'isrelated)
169  (setq isrelated '( (has something to do with)(is related to)
170		     (could be the reason for) (is caused by)(is because of)))
171  (make-local-variable 'arerelated)
172  (setq arerelated '((have something to do with)(are related to)
173		     (could have caused)(could be the reason for) (are caused by)
174		     (are because of)))
175  (make-local-variable 'moods)
176  (setq moods '( ((doc$ areyou)(doc// found) often \?)
177		 (what causes you to be (doc// found) \?)
178		 ((doc$ whysay) you are (doc// found) \?) ))
179  (make-local-variable 'maybe)
180  (setq maybe
181	'((maybe)
182	  (perhaps)
183	  (possibly)))
184  (make-local-variable 'whatwhen)
185  (setq whatwhen
186	'((what happened when)
187	  (what would happen if)))
188  (make-local-variable 'hello)
189  (setq hello
190	'((how do you do \?) (hello \.) (howdy!) (hello \.) (hi \.) (hi there \.)))
191  (make-local-variable 'drnk)
192  (setq drnk
193	'((do you drink a lot of (doc// found) \?)
194	  (do you get drunk often \?)
195	  ((doc$ describe) your drinking habits \.) ))
196  (make-local-variable 'drugs)
197  (setq drugs '( (do you use (doc// found) often \?)((doc$ areyou)
198						 addicted to (doc// found) \?)(do you realize that drugs can
199						 be very harmful \?)((doc$ maybe) you should try to quit using (doc// found)
200						 \.)))
201  (make-local-variable 'whywant)
202  (setq whywant '( ((doc$ whysay) (doc// subj) might (doc$ want) (doc// obj) \?)
203		   (how does it feel to want \?)
204		   (why should (doc// subj) get (doc// obj) \?)
205		   (when did (doc// subj) first (doc$ want) (doc// obj) \?)
206		   ((doc$ areyou) obsessed with (doc// obj) \?)
207		   (why should i give (doc// obj) to (doc// subj) \?)
208		   (have you ever gotten (doc// obj) \?) ))
209  (make-local-variable 'canyou)
210  (setq canyou '((of course i can \.)
211		 (why should i \?)
212		 (what makes you think i would even want to \?)
213		 (i am the doctor\, i can do anything i damn please \.)
214		 (not really\, it\'s not up to me \.)
215		 (depends\, how important is it \?)
216		 (i could\, but i don\'t think it would be a wise thing to do \.)
217		 (can you \?)
218		 (maybe i can\, maybe i can\'t \.\.\.)
219		 (i don\'t think i should do that \.)))
220  (make-local-variable 'want)
221  (setq want '( (want) (desire) (wish) (want) (hope) ))
222  (make-local-variable 'shortlst)
223  (setq shortlst
224	'((can you elaborate on that \?)
225	  ((doc$ please) continue \.)
226	  (go on\, don\'t be afraid \.)
227	  (i need a little more detail please \.)
228	  (you\'re being a bit brief\, (doc$ please) go into detail \.)
229	  (can you be more explicit \?)
230	  (and \?)
231	  ((doc$ please) go into more detail \?)
232	  (you aren\'t being very talkative today\!)
233	  (is that all there is to it \?)
234	  (why must you respond so briefly \?)))
235
236  (make-local-variable 'famlst)
237  (setq famlst
238	'((tell me (doc$ something) about (doc// owner) family \.)
239	  (you seem to dwell on (doc// owner) family \.)
240	  ((doc$ areyou) hung up on (doc// owner) family \?)))
241  (make-local-variable 'huhlst)
242  (setq huhlst
243	'(((doc$ whysay)(doc// sent) \?)
244	  (is it because of (doc$ things) that you say (doc// sent) \?) ))
245  (make-local-variable 'longhuhlst)
246  (setq longhuhlst
247	'(((doc$ whysay) that \?)
248	  (i don\'t understand \.)
249	  ((doc$ thlst))
250	  ((doc$ areyou) (doc$ afraidof) that \?)))
251  (make-local-variable 'feelings-about)
252  (setq feelings-about
253	'((feelings about)
254	  (apprehensions toward)
255	  (thoughts on)
256	  (emotions toward)))
257  (make-local-variable 'random-adjective)
258  (setq random-adjective
259	'((vivid)
260	  (emotionally stimulating)
261	  (exciting)
262	  (boring)
263	  (interesting)
264	  (recent)
265	  (random)   ;How can we omit this?
266	  (unusual)
267	  (shocking)
268	  (embarrassing)))
269  (make-local-variable 'whysay)
270  (setq whysay
271	'((why do you say)
272	  (what makes you believe)
273	  (are you sure that)
274	  (do you really think)
275	  (what makes you think) ))
276  (make-local-variable 'isee)
277  (setq isee
278	'((i see \.\.\.)
279	  (yes\,)
280	  (i understand \.)
281	  (oh \.) ))
282  (make-local-variable 'please)
283  (setq please
284	'((please\,)
285	  (i would appreciate it if you would)
286	  (perhaps you could)
287	  (please\,)
288	  (would you please)
289	  (why don\'t you)
290	  (could you)))
291  (make-local-variable 'bye)
292  (setq bye
293	'((my secretary will send you a bill \.)
294	  (bye bye \.)
295	  (see ya \.)
296	  (ok\, talk to you some other time \.)
297	  (talk to you later \.)
298	  (ok\, have fun \.)
299	  (ciao \.)))
300  (make-local-variable 'something)
301  (setq something
302	'((something)
303	  (more)
304	  (how you feel)))
305  (make-local-variable 'things)
306  (setq things
307	'(;(your interests in computers)   ;; let's make this less computer oriented
308	  ;(the machines you use)
309	  (your plans)
310	  ;(your use of computers)
311	  (your life)
312	  ;(other machines you use)
313	  (the people you hang around with)
314	  ;(computers you like)
315	  (problems at school)
316	  (any hobbies you have)
317	  ;(other computers you use)
318	  (your sex life)
319	  (hangups you have)
320	  (your inhibitions)
321	  (some problems in your childhood)
322	  ;(knowledge of computers)
323	  (some problems at home)))
324  (make-local-variable 'describe)
325  (setq describe
326	'((describe)
327	  (tell me about)
328	  (talk about)
329	  (discuss)
330	  (tell me more about)
331	  (elaborate on)))
332  (make-local-variable 'ibelieve)
333  (setq ibelieve
334	'((i believe) (i think) (i have a feeling) (it seems to me that)
335	  (it looks like)))
336  (make-local-variable 'problems)
337  (setq problems '( (problems)
338		    (inhibitions)
339		    (hangups)
340		    (difficulties)
341		    (anxieties)
342		    (frustrations) ))
343  (make-local-variable 'bother)
344  (setq bother
345	'((does it bother you that)
346	  (are you annoyed that)
347	  (did you ever regret)
348	  (are you sorry)
349	  (are you satisfied with the fact that)))
350  (make-local-variable 'machlst)
351  (setq machlst
352	'((you have your mind on (doc// found) \, it seems \.)
353	  (you think too much about  (doc// found) \.)
354	  (you should try taking your mind off of (doc// found)\.)
355	  (are you a computer hacker \?)))
356  (make-local-variable 'qlist)
357  (setq qlist
358	'((what do you think \?)
359	  (i\'ll ask the questions\, if you don\'t mind!)
360	  (i could ask the same thing myself \.)
361	  ((doc$ please) allow me to do the questioning \.)
362	  (i have asked myself that question many times \.)
363	  ((doc$ please) try to answer that question yourself \.)))
364  (make-local-variable 'foullst)
365  (setq foullst
366	'(((doc$ please) watch your tongue!)
367	  ((doc$ please) avoid such unwholesome thoughts \.)
368	  ((doc$ please) get your mind out of the gutter \.)
369	  (such lewdness is not appreciated \.)))
370  (make-local-variable 'deathlst)
371  (setq deathlst
372	'((this is not a healthy way of thinking \.)
373	  ((doc$ bother) you\, too\, may die someday \?)
374	  (i am worried by your obsession with this topic!)
375	  (did you watch a lot of crime and violence on television as a child \?))
376	)
377  (make-local-variable 'sexlst)
378  (setq sexlst
379	'(((doc$ areyou) (doc$ afraidof) sex \?)
380	  ((doc$ describe)(doc$ something) about your sexual history \.)
381	  ((doc$ please)(doc$ describe) your sex life \.\.\.)
382	  ((doc$ describe) your (doc$ feelings-about) your sexual partner \.)
383	  ((doc$ describe) your most (doc$ random-adjective) sexual experience \.)
384	  ((doc$ areyou) satisfied with (doc// lover) \.\.\. \?)))
385  (make-local-variable 'neglst)
386  (setq neglst
387	'((why not \?)
388	  ((doc$ bother) i ask that \?)
389	  (why not \?)
390	  (why not \?)
391	  (how come \?)
392	  ((doc$ bother) i ask that \?)))
393  (make-local-variable 'beclst)
394  (setq beclst '(
395		 (is it because (doc// sent) that you came to me \?)
396		 ((doc$ bother)(doc// sent) \?)
397		 (when did you first know that (doc// sent) \?)
398		 (is the fact that (doc// sent) the real reason \?)
399		 (does the fact that (doc// sent) explain anything else \?)
400		 ((doc$ areyou)(doc$ sure)(doc// sent) \? ) ))
401  (make-local-variable 'shortbeclst)
402  (setq shortbeclst '(
403		      ((doc$ bother) i ask you that \?)
404		      (that\'s not much of an answer!)
405		      ((doc$ inter) why won\'t you talk about it \?)
406		      (speak up!)
407		      ((doc$ areyou) (doc$ afraidof) talking about it \?)
408		      (don\'t be (doc$ afraidof) elaborating \.)
409		      ((doc$ please) go into more detail \.)))
410  (make-local-variable 'thlst)
411  (setq thlst '(
412		((doc$ maybe)(doc$ things)(doc$ arerelated) this \.)
413		(is it because of (doc$ things) that you are going through all this \?)
414		(how do you reconcile (doc$ things) \? )
415		((doc$ maybe) this (doc$ isrelated)(doc$ things) \?) ))
416  (make-local-variable 'remlst)
417  (setq remlst '( (earlier you said (doc$ history) \?)
418		  (you mentioned that (doc$ history) \?)
419		  ((doc$ whysay)(doc$ history) \? ) ))
420  (make-local-variable 'toklst)
421  (setq toklst
422	'((is this how you relax \?)
423	  (how long have you been smoking	grass \?)
424	  ((doc$ areyou) (doc$ afraidof) of being drawn to using harder stuff \?)))
425  (make-local-variable 'states)
426  (setq states
427	'((do you get (doc// found) often \?)
428	  (do you enjoy being (doc// found) \?)
429	  (what makes you (doc// found) \?)
430	  (how often (doc$ areyou)(doc// found) \?)
431	  (when were you last (doc// found) \?)))
432  (make-local-variable 'replist)
433  (setq replist
434	'((i . (you))
435	  (my . (your))
436	  (me . (you))
437	  (you . (me))
438	  (your . (my))
439	  (mine . (yours))
440	  (yours . (mine))
441	  (our . (your))
442	  (ours . (yours))
443	  (we . (you))
444	  (dunno . (do not know))
445;;	  (yes . ())
446	  (no\, . ())
447	  (yes\, . ())
448	  (ya . (i))
449	  (aint . (am not))
450	  (wanna . (want to))
451	  (gimme . (give me))
452	  (gotta . (have to))
453	  (gonna . (going to))
454	  (never . (not ever))
455	  (doesn\'t . (does not))
456	  (don\'t . (do not))
457	  (aren\'t . (are not))
458	  (isn\'t . (is not))
459	  (won\'t . (will not))
460	  (can\'t . (cannot))
461	  (haven\'t . (have not))
462	  (i\'m . (you are))
463	  (ourselves . (yourselves))
464	  (myself . (yourself))
465	  (yourself . (myself))
466	  (you\'re . (i am))
467	  (you\'ve . (i have))
468	  (i\'ve . (you have))
469	  (i\'ll . (you will))
470	  (you\'ll . (i shall))
471	  (i\'d . (you would))
472	  (you\'d . (i would))
473	  (here . (there))
474	  (please . ())
475	  (eh\, . ())
476	  (eh . ())
477	  (oh\, . ())
478	  (oh . ())
479	  (shouldn\'t . (should not))
480	  (wouldn\'t . (would not))
481	  (won\'t . (will not))
482	  (hasn\'t . (has not))))
483  (make-local-variable 'stallmanlst)
484  (setq stallmanlst '(
485		      ((doc$ describe) your (doc$ feelings-about) him \.)
486		      ((doc$ areyou) a friend of Stallman \?)
487		      ((doc$ bother) Stallman is (doc$ random-adjective) \?)
488		      ((doc$ ibelieve) you are (doc$ afraidof) him \.)))
489  (make-local-variable 'schoollst)
490  (setq schoollst '(
491		    ((doc$ describe) your (doc// found) \.)
492		    ((doc$ bother) your grades could (doc$ improve) \?)
493		    ((doc$ areyou) (doc$ afraidof) (doc// found) \?)
494		    ((doc$ maybe) this (doc$ isrelated) to your attitude \.)
495		    ((doc$ areyou) absent often \?)
496		    ((doc$ maybe) you should study (doc$ something) \.)))
497  (make-local-variable 'improve)
498  (setq improve '((improve) (be better) (be improved) (be higher)))
499  (make-local-variable 'elizalst)
500  (setq elizalst '(
501		   ((doc$ areyou) (doc$ sure) \?)
502		   ((doc$ ibelieve) you have (doc$ problems) with (doc// found) \.)
503		   ((doc$ whysay) (doc// sent) \?)))
504  (make-local-variable 'sportslst)
505  (setq sportslst '(
506		    (tell me (doc$ something) about (doc// found) \.)
507		    ((doc$ describe) (doc$ relation) (doc// found) \.)
508		    (do you find (doc// found) (doc$ random-adjective) \?)))
509  (make-local-variable 'mathlst)
510  (setq mathlst '(
511		  ((doc$ describe) (doc$ something) about math \.)
512		  ((doc$ maybe) your (doc$ problems) (doc$ arerelated) (doc// found) \.)
513		  (i don\'t know much (doc// found) \, but (doc$ continue)
514		     anyway \.)))
515  (make-local-variable 'zippylst)
516  (setq zippylst '(
517		   ((doc$ areyou) Zippy \?)
518		   ((doc$ ibelieve) you have some serious (doc$ problems) \.)
519		   ((doc$ bother) you are a pinhead \?)))
520  (make-local-variable 'chatlst)
521  (setq chatlst '(
522		  ((doc$ maybe) we could chat \.)
523		  ((doc$ please) (doc$ describe) (doc$ something) about chat mode \.)
524		  ((doc$ bother) our discussion is so (doc$ random-adjective) \?)))
525  (make-local-variable 'abuselst)
526  (setq abuselst '(
527		   ((doc$ please) try to be less abusive \.)
528		   ((doc$ describe) why you call me (doc// found) \.)
529		   (i\'ve had enough of you!)))
530  (make-local-variable 'abusewords)
531  (setq abusewords '(boring bozo clown clumsy cretin dumb dummy
532			    fool foolish gnerd gnurd idiot jerk
533			    lose loser louse lousy luse luser
534			    moron nerd nurd oaf oafish reek
535			    stink stupid tool toolish twit))
536  (make-local-variable 'howareyoulst)
537  (setq howareyoulst  '((how are you) (hows it going) (hows it going eh)
538			(how\'s it going) (how\'s it going eh) (how goes it)
539			(whats up) (whats new) (what\'s up) (what\'s new)
540			(howre you) (how\'re you) (how\'s everything)
541			(how is everything) (how do you do)
542			(how\'s it hanging) (que pasa)
543			(how are you doing) (what do you say)))
544  (make-local-variable 'whereoutp)
545  (setq whereoutp '( huh remem rthing ) )
546  (make-local-variable 'subj)
547  (setq subj nil)
548  (make-local-variable 'verb)
549  (setq verb nil)
550  (make-local-variable 'obj)
551  (setq obj nil)
552  (make-local-variable 'feared)
553  (setq feared nil)
554  (make-local-variable 'repetitive-shortness)
555  (setq repetitive-shortness '(0 . 0))
556  (make-local-variable '**mad**)
557  (setq **mad** nil)
558  (make-local-variable 'rms-flag)
559  (setq rms-flag nil)
560  (make-local-variable 'eliza-flag)
561  (setq eliza-flag nil)
562  (make-local-variable 'zippy-flag)
563  (setq zippy-flag nil)
564  (make-local-variable 'suicide-flag)
565  (setq suicide-flag nil)
566  (make-local-variable 'lover)
567  (setq lover '(your partner))
568  (make-local-variable 'bak)
569  (setq bak nil)
570  (make-local-variable 'lincount)
571  (setq lincount 0)
572  (make-local-variable '*print-upcase*)
573  (setq *print-upcase* nil)
574  (make-local-variable '*print-space*)
575  (setq *print-space* nil)
576  (make-local-variable 'howdyflag)
577  (setq howdyflag nil)
578  (make-local-variable 'object)
579  (setq object nil))
580
581;; Define equivalence classes of words that get treated alike.
582
583(defun doctor-meaning (x) (get x 'doctor-meaning))
584
585(defmacro doctor-put-meaning (symb val)
586    "Store the base meaning of a word on the property list."
587    (list 'put (list 'quote symb) ''doctor-meaning val))
588
589(doctor-put-meaning howdy 'howdy)
590(doctor-put-meaning hi 'howdy)
591(doctor-put-meaning greetings 'howdy)
592(doctor-put-meaning hello 'howdy)
593(doctor-put-meaning tops20 'mach)
594(doctor-put-meaning tops-20 'mach)
595(doctor-put-meaning tops 'mach)
596(doctor-put-meaning pdp11 'mach)
597(doctor-put-meaning computer 'mach)
598(doctor-put-meaning unix 'mach)
599(doctor-put-meaning machine 'mach)
600(doctor-put-meaning computers 'mach)
601(doctor-put-meaning machines 'mach)
602(doctor-put-meaning pdp11s 'mach)
603(doctor-put-meaning foo 'mach)
604(doctor-put-meaning foobar 'mach)
605(doctor-put-meaning multics 'mach)
606(doctor-put-meaning macsyma 'mach)
607(doctor-put-meaning teletype 'mach)
608(doctor-put-meaning la36 'mach)
609(doctor-put-meaning vt52 'mach)
610(doctor-put-meaning zork 'mach)
611(doctor-put-meaning trek 'mach)
612(doctor-put-meaning startrek 'mach)
613(doctor-put-meaning advent 'mach)
614(doctor-put-meaning pdp 'mach)
615(doctor-put-meaning dec 'mach)
616(doctor-put-meaning commodore 'mach)
617(doctor-put-meaning vic 'mach)
618(doctor-put-meaning bbs 'mach)
619(doctor-put-meaning modem 'mach)
620(doctor-put-meaning baud 'mach)
621(doctor-put-meaning macintosh 'mach)
622(doctor-put-meaning vax 'mach)
623(doctor-put-meaning vms 'mach)
624(doctor-put-meaning ibm 'mach)
625(doctor-put-meaning pc 'mach)
626(doctor-put-meaning bitching 'foul)
627(doctor-put-meaning shit 'foul)
628(doctor-put-meaning bastard 'foul)
629(doctor-put-meaning damn 'foul)
630(doctor-put-meaning damned 'foul)
631(doctor-put-meaning hell 'foul)
632(doctor-put-meaning suck 'foul)
633(doctor-put-meaning sucking 'foul)
634(doctor-put-meaning sux 'foul)
635(doctor-put-meaning ass 'foul)
636(doctor-put-meaning whore 'foul)
637(doctor-put-meaning bitch 'foul)
638(doctor-put-meaning asshole 'foul)
639(doctor-put-meaning shrink 'foul)
640(doctor-put-meaning pot 'toke)
641(doctor-put-meaning grass 'toke)
642(doctor-put-meaning weed 'toke)
643(doctor-put-meaning marijuana 'toke)
644(doctor-put-meaning acapulco 'toke)
645(doctor-put-meaning columbian 'toke)
646(doctor-put-meaning tokin 'toke)
647(doctor-put-meaning joint 'toke)
648(doctor-put-meaning toke 'toke)
649(doctor-put-meaning toking 'toke)
650(doctor-put-meaning tokin\' 'toke)
651(doctor-put-meaning toked 'toke)
652(doctor-put-meaning roach 'toke)
653(doctor-put-meaning pills 'drug)
654(doctor-put-meaning dope 'drug)
655(doctor-put-meaning acid 'drug)
656(doctor-put-meaning lsd 'drug)
657(doctor-put-meaning speed 'drug)
658(doctor-put-meaning heroin 'drug)
659(doctor-put-meaning hash 'drug)
660(doctor-put-meaning cocaine 'drug)
661(doctor-put-meaning uppers 'drug)
662(doctor-put-meaning downers 'drug)
663(doctor-put-meaning loves 'loves)
664(doctor-put-meaning love 'love)
665(doctor-put-meaning loved 'love)
666(doctor-put-meaning hates 'hates)
667(doctor-put-meaning dislikes 'hates)
668(doctor-put-meaning hate 'hate)
669(doctor-put-meaning hated 'hate)
670(doctor-put-meaning dislike 'hate)
671(doctor-put-meaning stoned 'state)
672(doctor-put-meaning drunk 'state)
673(doctor-put-meaning drunken 'state)
674(doctor-put-meaning high 'state)
675(doctor-put-meaning horny 'state)
676(doctor-put-meaning blasted 'state)
677(doctor-put-meaning happy 'state)
678(doctor-put-meaning paranoid 'state)
679(doctor-put-meaning wish 'desire)
680(doctor-put-meaning wishes 'desire)
681(doctor-put-meaning want 'desire)
682(doctor-put-meaning desire 'desire)
683(doctor-put-meaning like 'desire)
684(doctor-put-meaning hope 'desire)
685(doctor-put-meaning hopes 'desire)
686(doctor-put-meaning desires 'desire)
687(doctor-put-meaning wants 'desire)
688(doctor-put-meaning desires 'desire)
689(doctor-put-meaning likes 'desire)
690(doctor-put-meaning needs 'desire)
691(doctor-put-meaning need 'desire)
692(doctor-put-meaning frustrated 'mood)
693(doctor-put-meaning depressed 'mood)
694(doctor-put-meaning annoyed 'mood)
695(doctor-put-meaning upset 'mood)
696(doctor-put-meaning unhappy 'mood)
697(doctor-put-meaning excited 'mood)
698(doctor-put-meaning worried 'mood)
699(doctor-put-meaning lonely 'mood)
700(doctor-put-meaning angry 'mood)
701(doctor-put-meaning mad 'mood)
702(doctor-put-meaning pissed 'mood)
703(doctor-put-meaning jealous 'mood)
704(doctor-put-meaning afraid 'fear)
705(doctor-put-meaning terrified 'fear)
706(doctor-put-meaning fear 'fear)
707(doctor-put-meaning scared 'fear)
708(doctor-put-meaning frightened 'fear)
709(doctor-put-meaning virginity 'sexnoun)
710(doctor-put-meaning virgins 'sexnoun)
711(doctor-put-meaning virgin 'sexnoun)
712(doctor-put-meaning cock 'sexnoun)
713(doctor-put-meaning cocks 'sexnoun)
714(doctor-put-meaning dick 'sexnoun)
715(doctor-put-meaning dicks 'sexnoun)
716(doctor-put-meaning cunt 'sexnoun)
717(doctor-put-meaning cunts 'sexnoun)
718(doctor-put-meaning prostitute 'sexnoun)
719(doctor-put-meaning condom 'sexnoun)
720(doctor-put-meaning sex 'sexnoun)
721(doctor-put-meaning rapes 'sexnoun)
722(doctor-put-meaning wife 'family)
723(doctor-put-meaning family 'family)
724(doctor-put-meaning brothers 'family)
725(doctor-put-meaning sisters 'family)
726(doctor-put-meaning parent 'family)
727(doctor-put-meaning parents 'family)
728(doctor-put-meaning brother 'family)
729(doctor-put-meaning sister 'family)
730(doctor-put-meaning father 'family)
731(doctor-put-meaning mother 'family)
732(doctor-put-meaning husband 'family)
733(doctor-put-meaning siblings 'family)
734(doctor-put-meaning grandmother 'family)
735(doctor-put-meaning grandfather 'family)
736(doctor-put-meaning maternal 'family)
737(doctor-put-meaning paternal 'family)
738(doctor-put-meaning stab 'death)
739(doctor-put-meaning murder 'death)
740(doctor-put-meaning murders 'death)
741(doctor-put-meaning suicide 'death)
742(doctor-put-meaning suicides 'death)
743(doctor-put-meaning kill 'death)
744(doctor-put-meaning kills 'death)
745(doctor-put-meaning killing 'death)
746(doctor-put-meaning die 'death)
747(doctor-put-meaning dies 'death)
748(doctor-put-meaning died 'death)
749(doctor-put-meaning dead 'death)
750(doctor-put-meaning death 'death)
751(doctor-put-meaning deaths 'death)
752(doctor-put-meaning pain 'symptoms)
753(doctor-put-meaning ache 'symptoms)
754(doctor-put-meaning fever 'symptoms)
755(doctor-put-meaning sore 'symptoms)
756(doctor-put-meaning aching 'symptoms)
757(doctor-put-meaning stomachache 'symptoms)
758(doctor-put-meaning headache 'symptoms)
759(doctor-put-meaning hurts 'symptoms)
760(doctor-put-meaning disease 'symptoms)
761(doctor-put-meaning virus 'symptoms)
762(doctor-put-meaning vomit 'symptoms)
763(doctor-put-meaning vomiting 'symptoms)
764(doctor-put-meaning barf 'symptoms)
765(doctor-put-meaning toothache 'symptoms)
766(doctor-put-meaning hurt 'symptoms)
767(doctor-put-meaning rum 'alcohol)
768(doctor-put-meaning gin 'alcohol)
769(doctor-put-meaning vodka 'alcohol)
770(doctor-put-meaning alcohol 'alcohol)
771(doctor-put-meaning bourbon 'alcohol)
772(doctor-put-meaning beer 'alcohol)
773(doctor-put-meaning wine 'alcohol)
774(doctor-put-meaning whiskey 'alcohol)
775(doctor-put-meaning scotch 'alcohol)
776(doctor-put-meaning fuck 'sexverb)
777(doctor-put-meaning fucked 'sexverb)
778(doctor-put-meaning screw 'sexverb)
779(doctor-put-meaning screwing 'sexverb)
780(doctor-put-meaning fucking 'sexverb)
781(doctor-put-meaning rape 'sexverb)
782(doctor-put-meaning raped 'sexverb)
783(doctor-put-meaning kiss 'sexverb)
784(doctor-put-meaning kissing 'sexverb)
785(doctor-put-meaning kisses 'sexverb)
786(doctor-put-meaning screws 'sexverb)
787(doctor-put-meaning fucks 'sexverb)
788(doctor-put-meaning because 'conj)
789(doctor-put-meaning but 'conj)
790(doctor-put-meaning however 'conj)
791(doctor-put-meaning besides 'conj)
792(doctor-put-meaning anyway 'conj)
793(doctor-put-meaning that 'conj)
794(doctor-put-meaning except 'conj)
795(doctor-put-meaning why 'conj)
796(doctor-put-meaning how 'conj)
797(doctor-put-meaning until 'when)
798(doctor-put-meaning when 'when)
799(doctor-put-meaning whenever 'when)
800(doctor-put-meaning while 'when)
801(doctor-put-meaning since 'when)
802(doctor-put-meaning rms 'rms)
803(doctor-put-meaning stallman 'rms)
804(doctor-put-meaning school 'school)
805(doctor-put-meaning schools 'school)
806(doctor-put-meaning skool 'school)
807(doctor-put-meaning grade 'school)
808(doctor-put-meaning grades 'school)
809(doctor-put-meaning teacher 'school)
810(doctor-put-meaning teachers 'school)
811(doctor-put-meaning classes 'school)
812(doctor-put-meaning professor 'school)
813(doctor-put-meaning prof 'school)
814(doctor-put-meaning profs 'school)
815(doctor-put-meaning professors 'school)
816(doctor-put-meaning mit 'school)
817(doctor-put-meaning emacs 'eliza)
818(doctor-put-meaning eliza 'eliza)
819(doctor-put-meaning liza 'eliza)
820(doctor-put-meaning elisa 'eliza)
821(doctor-put-meaning weizenbaum 'eliza)
822(doctor-put-meaning doktor 'eliza)
823(doctor-put-meaning athletics 'sports)
824(doctor-put-meaning baseball 'sports)
825(doctor-put-meaning basketball 'sports)
826(doctor-put-meaning football 'sports)
827(doctor-put-meaning frisbee 'sports)
828(doctor-put-meaning gym 'sports)
829(doctor-put-meaning gymnastics 'sports)
830(doctor-put-meaning hockey 'sports)
831(doctor-put-meaning lacrosse 'sports)
832(doctor-put-meaning soccer 'sports)
833(doctor-put-meaning softball 'sports)
834(doctor-put-meaning sports 'sports)
835(doctor-put-meaning swimming 'sports)
836(doctor-put-meaning swim 'sports)
837(doctor-put-meaning tennis 'sports)
838(doctor-put-meaning volleyball 'sports)
839(doctor-put-meaning math 'math)
840(doctor-put-meaning mathematics 'math)
841(doctor-put-meaning mathematical 'math)
842(doctor-put-meaning theorem 'math)
843(doctor-put-meaning axiom 'math)
844(doctor-put-meaning lemma 'math)
845(doctor-put-meaning algebra 'math)
846(doctor-put-meaning algebraic 'math)
847(doctor-put-meaning trig 'math)
848(doctor-put-meaning trigonometry 'math)
849(doctor-put-meaning trigonometric 'math)
850(doctor-put-meaning geometry 'math)
851(doctor-put-meaning geometric 'math)
852(doctor-put-meaning calculus 'math)
853(doctor-put-meaning arithmetic 'math)
854(doctor-put-meaning zippy 'zippy)
855(doctor-put-meaning zippy 'zippy)
856(doctor-put-meaning pinhead 'zippy)
857(doctor-put-meaning chat 'chat)
858
859;;;###autoload
860(defun doctor ()
861  "Switch to *doctor* buffer and start giving psychotherapy."
862  (interactive)
863  (switch-to-buffer "*doctor*")
864  (doctor-mode))
865
866(defun doctor-ret-or-read (arg)
867  "Insert a newline if preceding character is not a newline.
868Otherwise call the Doctor to parse preceding sentence."
869  (interactive "*p")
870  (if (= (preceding-char) ?\n)
871      (doctor-read-print)
872    (newline arg)))
873
874(defun doctor-read-print nil
875  "top level loop"
876  (interactive)
877  (let ((sent (doctor-readin)))
878    (insert "\n")
879    (setq lincount (1+ lincount))
880    (doctor-doc sent)
881    (insert "\n")
882    (setq bak sent)))
883
884(defun doctor-readin nil
885  "Read a sentence.  Return it as a list of words."
886  (let (sentence)
887    (backward-sentence 1)
888    (while (not (eobp))
889      (setq sentence (append sentence (list (doctor-read-token)))))
890    sentence))
891
892(defun doctor-read-token ()
893  "read one word from buffer"
894  (prog1 (intern (downcase (buffer-substring (point)
895					     (progn
896					       (forward-word 1)
897					       (point)))))
898    (re-search-forward "\\Sw*")))
899
900;; Main processing function for sentences that have been read.
901
902(defun doctor-doc (sent)
903  (cond
904   ((equal sent '(foo))
905    (doctor-type '(bar! (doc$ please)(doc$ continue) \.)))
906   ((member sent howareyoulst)
907    (doctor-type '(i\'m ok \.  (doc$ describe) yourself \.)))
908   ((or (member sent '((good bye) (see you later) (i quit) (so long)
909		       (go away) (get lost)))
910	(memq (car sent)
911	      '(bye halt break quit done exit goodbye
912		    bye\, stop pause goodbye\, stop pause)))
913    (doctor-type (doc$ bye)))
914   ((and (eq (car sent) 'you)
915	 (memq (cadr sent) abusewords))
916    (setq found (cadr sent))
917    (doctor-type (doc$ abuselst)))
918   ((eq (car sent) 'whatmeans)
919    (doctor-def (cadr sent)))
920   ((equal sent '(parse))
921    (doctor-type (list  'subj '= subj ",  "
922			'verb '= verb "\n"
923			'object 'phrase '= obj ","
924			'noun 'form '=  object "\n"
925			'current 'keyword 'is found
926			", "
927			'most 'recent 'possessive
928			'is owner "\n"
929			'sentence 'used 'was
930			"..."
931			'(doc// bak))))
932   ((memq (car sent) '(are is do has have how when where who why))
933    (doctor-type (doc$ qlist)))
934   ;;   ((eq (car sent) 'forget)
935   ;;    (set (cadr sent) nil)
936   ;;    (doctor-type '((doc$ isee)(doc$ please)
937   ;;     (doc$ continue)\.)))
938   (t
939    (if (doctor-defq sent) (doctor-define sent found))
940    (if (> (length sent) 12)(setq sent (doctor-shorten sent)))
941    (setq sent (doctor-correct-spelling (doctor-replace sent replist)))
942    (cond ((and (not (memq 'me sent))(not (memq 'i sent))
943		(memq 'am sent))
944	   (setq sent (doctor-replace sent '((am . (are)))))))
945    (cond ((equal (car sent) 'yow) (doctor-zippy))
946	  ((< (length sent) 2)
947	   (cond ((eq (doctor-meaning (car sent)) 'howdy)
948		  (doctor-howdy))
949		 (t (doctor-short))))
950	  (t
951	   (if (memq 'am sent)
952	       (setq sent (doctor-replace sent '((me . (i))))))
953	   (setq sent (doctor-fixup sent))
954	   (if (and (eq (car sent) 'do) (eq (cadr sent) 'not))
955	       (cond ((zerop (random 3))
956		      (doctor-type '(are you (doc$ afraidof) that \?)))
957		     ((zerop (random 2))
958		      (doctor-type '(don\'t tell me what to do \. i am the
959					    doctor here!))
960		      (doctor-rthing))
961		     (t
962		      (doctor-type '((doc$ whysay) that i shouldn\'t
963				     (cddr sent)
964				     \?))))
965	     (doctor-go (doctor-wherego sent))))))))
966
967;; Things done to process sentences once read.
968
969(defun doctor-correct-spelling (sent)
970  "Correct the spelling and expand each word in sentence."
971  (if sent
972      (apply 'append (mapcar (lambda (word)
973				(if (memq word typos)
974				    (get (get word 'doctor-correction) 'doctor-expansion)
975				  (list word)))
976			     sent))))
977
978(defun doctor-shorten (sent)
979  "Make a sentence manageably short using a few hacks."
980  (let (foo
981	(retval sent)
982	(temp '(because but however besides anyway until
983		    while that except why how)))
984    (while temp
985	   (setq foo (memq (car temp) sent))
986	   (if (and foo
987		    (> (length foo) 3))
988	       (setq retval (doctor-fixup foo)
989		     temp nil)
990	       (setq temp (cdr temp))))
991    retval))
992
993(defun doctor-define (sent found)
994  (doctor-svo sent found 1 nil)
995  (and
996   (doctor-nounp subj)
997   (not (doctor-pronounp subj))
998   subj
999   (doctor-meaning object)
1000   (put subj 'doctor-meaning (doctor-meaning object))
1001   t))
1002
1003(defun doctor-defq (sent)
1004  "Set global var FOUND to first keyword found in sentence SENT."
1005  (setq found nil)
1006  (let ((temp '(means applies mean refers refer related
1007		      similar defined associated linked like same)))
1008    (while temp
1009	   (if (memq (car temp) sent)
1010	       (setq found (car temp)
1011		     temp nil)
1012	       (setq temp (cdr temp)))))
1013  found)
1014
1015(defun doctor-def (x)
1016  (progn
1017   (doctor-type (list 'the 'word x 'means (doctor-meaning x) 'to 'me))
1018   nil))
1019
1020(defun doctor-forget ()
1021  "Delete the last element of the history list."
1022  (setq history (reverse (cdr (reverse history)))))
1023
1024(defun doctor-query (x)
1025  "Prompt for a line of input from the minibuffer until a noun or verb is seen.
1026Put dialogue in buffer."
1027  (let (a
1028	(prompt (concat (doctor-make-string x)
1029			" what \?  "))
1030	retval)
1031    (while (not retval)
1032	   (while (not a)
1033	     (insert ?\n
1034		     prompt
1035		     (read-string prompt)
1036		     ?\n)
1037	     (setq a (doctor-readin)))
1038	   (while (and a (not retval))
1039		  (cond ((doctor-nounp (car a))
1040			 (setq retval (car a)))
1041			((doctor-verbp (car a))
1042			 (setq retval (doctor-build
1043				       (doctor-build x " ")
1044				       (car a))))
1045			((setq a (cdr a))))))
1046    retval))
1047
1048(defun doctor-subjsearch (sent key type)
1049  "Search for the subject of a sentence SENT, looking for the noun closest
1050to and preceding KEY by at least TYPE words.  Set global variable subj to
1051the subject noun, and return the portion of the sentence following it."
1052  (let ((i (- (length sent) (length (memq key sent)) type)))
1053    (while (and (> i -1) (not (doctor-nounp (nth i sent))))
1054      (setq i (1- i)))
1055    (cond ((> i -1)
1056	   (setq subj (nth i sent))
1057	   (nthcdr (1+ i) sent))
1058	  (t
1059	   (setq subj 'you)
1060	   nil))))
1061
1062(defun doctor-nounp (x)
1063  "Returns t if the symbol argument is a noun."
1064	(or (doctor-pronounp x)
1065	    (not (or (doctor-verbp x)
1066		     (equal x 'not)
1067		     (doctor-prepp x)
1068		     (doctor-modifierp x) )) ))
1069
1070(defun doctor-pronounp (x)
1071  "Returns t if the symbol argument is a pronoun."
1072  (memq x '(
1073	i me mine myself
1074	we us ours ourselves ourself
1075	you yours yourself yourselves
1076	he him himself she hers herself
1077	it that those this these things thing
1078	they them themselves theirs
1079	anybody everybody somebody
1080	anyone everyone someone
1081	anything something everything)))
1082
1083(dolist (x
1084         '(abort aborted aborts ask asked asks am
1085           applied applies apply are associate
1086           associated ate
1087           be became become becomes becoming
1088           been being believe believed believes
1089           bit bite bites bore bored bores boring bought buy buys buying
1090           call called calling calls came can caught catch come
1091           contract contracted contracts control controlled controls
1092           could croak croaks croaked cut cuts
1093           dare dared define defines dial dialed dials did die died dies
1094           dislike disliked
1095           dislikes do does drank drink drinks drinking
1096           drive drives driving drove dying
1097           eat eating eats expand expanded expands
1098           expect expected expects expel expels expelled
1099           explain explained explains
1100           fart farts feel feels felt fight fights find finds finding
1101           forget forgets forgot fought found
1102           fuck fucked fucking fucks
1103           gave get gets getting give gives go goes going gone got gotten
1104           had harm harms has hate hated hates have having
1105           hear heard hears hearing help helped helping helps
1106           hit hits hope hoped hopes hurt hurts
1107           implies imply is
1108           join joined joins jump jumped jumps
1109           keep keeping keeps kept
1110           kill killed killing kills kiss kissed kisses kissing
1111           knew know knows
1112           laid lay lays let lets lie lied lies like liked likes
1113           liking listen listens
1114           login look looked looking looks
1115           lose losing lost
1116           love loved loves loving
1117           luse lusing lust lusts
1118           made make makes making may mean means meant might
1119           move moved moves moving must
1120           need needed needs
1121           order ordered orders ought
1122           paid pay pays pick picked picking picks
1123           placed placing prefer prefers put puts
1124           ran rape raped rapes
1125           read reading reads recall receive received receives
1126           refer refered referred refers
1127           relate related relates remember remembered remembers
1128           romp romped romps run running runs
1129           said sang sat saw say says
1130           screw screwed screwing screws scrod see sees seem seemed
1131           seems seen sell selling sells
1132           send sendind sends sent shall shoot shot should
1133           sing sings sit sits sitting sold studied study
1134           take takes taking talk talked talking talks tell tells telling
1135           think thinks
1136           thought told took tooled touch touched touches touching
1137           transfer transferred transfers transmit transmits transmitted
1138           type types types typing
1139           walk walked walking walks want wanted wants was watch
1140           watched watching went were will wish would work worked works
1141           write writes writing wrote use used uses using))
1142  (put x 'doctor-sentence-type 'verb))
1143
1144(defun doctor-verbp (x) (if (symbolp x)
1145			    (eq (get x 'doctor-sentence-type) 'verb)))
1146
1147(defun doctor-plural (x)
1148  "Form the plural of the word argument."
1149  (let ((foo (doctor-make-string x)))
1150    (cond ((string-equal (substring foo -1) "s")
1151	   (cond ((string-equal (substring foo -2 -1) "s")
1152		  (intern (concat foo "es")))
1153		 (t x)))
1154	   ((string-equal (substring foo -1) "y")
1155	    (intern (concat (substring foo 0 -1)
1156			    "ies")))
1157	   (t (intern (concat foo "s"))))))
1158
1159(defun doctor-setprep (sent key)
1160  (let ((val)
1161	(foo (memq key sent)))
1162    (cond ((doctor-prepp (cadr foo))
1163	   (setq val (doctor-getnoun (cddr foo)))
1164	   (cond (val val)
1165		 (t 'something)))
1166	  ((doctor-articlep (cadr foo))
1167	   (setq val (doctor-getnoun (cddr foo)))
1168	   (cond (val (doctor-build (doctor-build (cadr foo) " ") val))
1169		 (t 'something)))
1170	  (t 'something))))
1171
1172(defun doctor-getnoun (x)
1173  (cond ((null x)(setq object 'something))
1174	((atom x)(setq object x))
1175	((eq (length x) 1)
1176	 (setq object (cond
1177		       ((doctor-nounp (setq object (car x))) object)
1178		       (t (doctor-query object)))))
1179	((eq (car x) 'to)
1180	 (doctor-build 'to\  (doctor-getnoun (cdr x))))
1181	((doctor-prepp (car x))
1182	 (doctor-getnoun (cdr x)))
1183	((not (doctor-nounp (car x)))
1184	 (doctor-build (doctor-build (cdr (assq (car x)
1185						(append
1186						 '((a . this)
1187						   (some . this)
1188						   (one . that))
1189						 (list
1190						  (cons
1191						   (car x) (car x))))))
1192				     " ")
1193		       (doctor-getnoun (cdr x))))
1194	(t (setq object (car x))
1195	   (doctor-build (doctor-build (car x) " ") (doctor-getnoun (cdr x))))
1196	))
1197
1198(defun doctor-modifierp (x)
1199  (or (doctor-adjectivep x)
1200      (doctor-adverbp x)
1201      (doctor-othermodifierp x)))
1202
1203(defun doctor-adjectivep (x)
1204  (or (numberp x)
1205      (doctor-nmbrp x)
1206      (doctor-articlep x)
1207      (doctor-colorp x)
1208      (doctor-sizep x)
1209      (doctor-possessivepronounp x)))
1210
1211(defun doctor-adverbp (xx)
1212  (let ((xxstr (doctor-make-string xx)))
1213    (and (>= (length xxstr) 2)
1214	 (string-equal (substring (doctor-make-string xx) -2) "ly"))))
1215
1216(defun doctor-articlep (x)
1217  (memq x '(the a an)))
1218
1219(defun doctor-nmbrp (x)
1220  (memq x '(one two three four five six seven eight nine ten
1221		eleven twelve thirteen fourteen fifteen
1222		sixteen seventeen eighteen nineteen
1223		twenty thirty forty fifty sixty seventy eighty ninety
1224		hundred thousand million billion
1225		half quarter
1226		first second third fourth fifth
1227		sixth seventh eighth ninth tenth)))
1228
1229(defun doctor-colorp (x)
1230  (memq x '(beige black blue brown crimson
1231		  gray grey green
1232		  orange pink purple red tan tawny
1233		  violet white yellow)))
1234
1235(defun doctor-sizep (x)
1236  (memq x '(big large tall fat wide thick
1237		small petite short thin skinny)))
1238
1239(defun doctor-possessivepronounp (x)
1240  (memq x '(my your his her our their)))
1241
1242(defun doctor-othermodifierp (x)
1243  (memq x '(all also always amusing any anyway associated awesome
1244		bad beautiful best better but certain clear
1245		ever every fantastic fun funny
1246		good great grody gross however if ignorant
1247		less linked losing lusing many more much
1248		never nice obnoxious often poor pretty real related rich
1249		similar some stupid super superb
1250		terrible terrific too total tubular ugly very)))
1251
1252(defun doctor-prepp (x)
1253  (memq x '(about above after around as at
1254		  before beneath behind beside between by
1255		  for from in inside into
1256		  like near next of on onto over
1257		  same through thru to toward towards
1258		  under underneath with without)))
1259
1260(defun doctor-remember (thing)
1261  (cond ((null history)
1262	 (setq history (list thing)))
1263	(t (setq history (append history (list thing))))))
1264
1265(defun doctor-type (x)
1266  (setq x (doctor-fix-2 x))
1267  (doctor-txtype (doctor-assm x)))
1268
1269(defun doctor-fixup (sent)
1270  (setq sent (append
1271	      (cdr
1272	       (assq (car sent)
1273		     (append
1274		      '((me  i)
1275			(him  he)
1276			(her  she)
1277			(them  they)
1278			(okay)
1279			(well)
1280			(sigh)
1281			(hmm)
1282			(hmmm)
1283			(hmmmm)
1284			(hmmmmm)
1285			(gee)
1286			(sure)
1287			(great)
1288			(oh)
1289			(fine)
1290			(ok)
1291			(no))
1292		      (list (list (car sent)
1293				  (car sent))))))
1294	      (cdr sent)))
1295  (doctor-fix-2 sent))
1296
1297(defun doctor-fix-2 (sent)
1298  (let ((foo sent))
1299    (while foo
1300      (if (and (eq (car foo) 'me)
1301	       (doctor-verbp (cadr foo)))
1302	  (rplaca foo 'i)
1303	(cond ((eq (car foo) 'you)
1304	       (cond ((memq (cadr foo) '(am be been is))
1305		      (rplaca (cdr foo) 'are))
1306		     ((memq (cadr foo) '(has))
1307		      (rplaca (cdr foo) 'have))
1308		     ((memq (cadr foo) '(was))
1309		      (rplaca (cdr foo) 'were))))
1310	      ((equal (car foo) 'i)
1311	       (cond ((memq (cadr foo) '(are is be been))
1312		      (rplaca (cdr foo) 'am))
1313		     ((memq (cadr foo) '(were))
1314		      (rplaca (cdr foo) 'was))
1315		     ((memq (cadr foo) '(has))
1316		      (rplaca (cdr foo) 'have))))
1317	      ((and (doctor-verbp (car foo))
1318		    (eq (cadr foo) 'i)
1319		    (not (doctor-verbp (car (cddr foo)))))
1320	       (rplaca (cdr foo) 'me))
1321	      ((and (eq (car foo) 'a)
1322		    (doctor-vowelp (string-to-char
1323				    (doctor-make-string (cadr foo)))))
1324	       (rplaca foo 'an))
1325	      ((and (eq (car foo) 'an)
1326		    (not (doctor-vowelp (string-to-char
1327					 (doctor-make-string (cadr foo))))))
1328	       (rplaca foo 'a)))
1329	(setq foo (cdr foo))))
1330    sent))
1331
1332(defun doctor-vowelp (x)
1333  (memq x '(?a ?e ?i ?o ?u)))
1334
1335(defun doctor-replace (sent rlist)
1336  "Replace any element of SENT that is the car of a replacement
1337element pair in RLIST."
1338  (apply 'append
1339	 (mapcar
1340	  (function
1341	   (lambda (x)
1342	     (cdr (or (assq x rlist)   ; either find a replacement
1343		      (list x x)))))   ; or fake an identity mapping
1344	  sent)))
1345
1346(defun doctor-wherego (sent)
1347  (cond ((null sent)(doc$ whereoutp))
1348	((null (doctor-meaning (car sent)))
1349	 (doctor-wherego (cond ((zerop (random 2))
1350				(reverse (cdr sent)))
1351			       (t (cdr sent)))))
1352	(t
1353	 (setq found (car sent))
1354	 (doctor-meaning (car sent)))))
1355
1356(defun doctor-svo (sent key type mem)
1357  "Find subject, verb and object in sentence SENT with focus on word KEY.
1358TYPE is number of words preceding KEY to start looking for subject.
1359MEM is t if results are to be put on Doctor's memory stack.
1360Return in the global variables SUBJ, VERB and OBJECT."
1361  (let ((foo (doctor-subjsearch sent key type)))
1362    (or foo
1363	(setq foo sent
1364	      mem nil))
1365    (while (and (null (doctor-verbp (car foo))) (cdr foo))
1366      (setq foo (cdr foo)))
1367    (setq verb (car foo))
1368    (setq obj (doctor-getnoun (cdr foo)))
1369    (cond ((eq object 'i)(setq object 'me))
1370	  ((eq subj 'me)(setq subj 'i)))
1371    (cond (mem (doctor-remember (list subj verb obj))))))
1372
1373(defun doctor-possess (sent key)
1374  "Set possessive in SENT for keyword KEY.
1375Hack on previous word, setting global variable OWNER to correct result."
1376  (let* ((i (- (length sent) (length (memq key sent)) 1))
1377	 (prev (if (< i 0) 'your
1378		 (nth i sent))))
1379    (setq owner (if (or (doctor-possessivepronounp prev)
1380			(string-equal "s"
1381				      (substring (doctor-make-string prev)
1382						 -1)))
1383		    prev
1384		  'your))))
1385
1386;; Output of replies.
1387
1388(defun doctor-txtype (ans)
1389  "Output to buffer a list of symbols or strings as a sentence."
1390  (setq *print-upcase* t *print-space* nil)
1391  (mapc 'doctor-type-symbol ans)
1392  (insert "\n"))
1393
1394(defun doctor-type-symbol (word)
1395  "Output a symbol to the buffer with some fancy case and spacing hacks."
1396  (setq word (doctor-make-string word))
1397  (if (string-equal word "i") (setq word "I"))
1398  (if *print-upcase*
1399      (progn
1400	(setq word (capitalize word))
1401	(if *print-space*
1402	    (insert " "))))
1403  (cond ((or (string-match "^[.,;:?! ]" word)
1404	     (not *print-space*))
1405	 (insert word))
1406	(t (insert ?\s word)))
1407  (and auto-fill-function
1408       (> (current-column) fill-column)
1409       (apply auto-fill-function nil))
1410  (setq *print-upcase* (string-match "[.?!]$" word)
1411	*print-space* t))
1412
1413(defun doctor-build (str1 str2)
1414  "Make a symbol out of the concatenation of the two non-list arguments."
1415  (cond ((null str1) str2)
1416	((null str2) str1)
1417	((and (atom str1)
1418	      (atom str2))
1419	 (intern (concat (doctor-make-string str1)
1420			 (doctor-make-string str2))))
1421	(t nil)))
1422
1423(defun doctor-make-string (obj)
1424  (cond ((stringp obj) obj)
1425	((symbolp obj) (symbol-name obj))
1426	((numberp obj) (int-to-string obj))
1427	(t "")))
1428
1429(defun doctor-concat (x y)
1430  "Like append, but force atomic arguments to be lists."
1431  (append
1432   (if (and x (atom x)) (list x) x)
1433   (if (and y (atom y)) (list y) y)))
1434
1435(defun doctor-assm (proto)
1436  (cond ((null proto) nil)
1437	((atom proto) (list proto))
1438	((atom (car proto))
1439	 (cons (car proto) (doctor-assm (cdr proto))))
1440	(t (doctor-concat (doctor-assm (eval (car proto))) (doctor-assm (cdr proto))))))
1441
1442;; Functions that handle specific words or meanings when found.
1443
1444(defun doctor-go (destination)
1445  "Call a `doctor-*' function."
1446  (funcall (intern (concat "doctor-" (doctor-make-string destination)))))
1447
1448(defun doctor-desire1 ()
1449  (doctor-go (doc$ whereoutp)))
1450
1451(defun doctor-huh ()
1452  (cond ((< (length sent) 9) (doctor-type (doc$ huhlst)))
1453	(t (doctor-type (doc$ longhuhlst)))))
1454
1455(defun doctor-rthing () (doctor-type (doc$ thlst)))
1456
1457(defun doctor-remem () (cond ((null history)(doctor-huh))
1458			     ((doctor-type (doc$ remlst)))))
1459
1460(defun doctor-howdy ()
1461  (cond ((not howdyflag)
1462	 (doctor-type '((doc$ hello) what brings you to see me \?))
1463	 (setq howdyflag t))
1464	(t
1465	 (doctor-type '((doc$ ibelieve) we\'ve introduced ourselves already \.))
1466	 (doctor-type '((doc$ please) (doc$ describe) (doc$ things) \.)))))
1467
1468(defun doctor-when ()
1469  (cond ((< (length (memq found sent)) 3)(doctor-short))
1470	(t
1471	 (setq sent (cdr (memq found sent)))
1472	 (setq sent (doctor-fixup sent))
1473	 (doctor-type '((doc$ whatwhen)(doc// sent) \?)))))
1474
1475(defun doctor-conj ()
1476  (cond ((< (length (memq found sent)) 4)(doctor-short))
1477	(t
1478	 (setq sent (cdr (memq found sent)))
1479	 (setq sent (doctor-fixup sent))
1480	 (cond ((eq (car sent) 'of)
1481		(doctor-type '(are you (doc$ sure) that is the real reason \?))
1482		(setq things (cons (cdr sent) things)))
1483	       (t
1484		(doctor-remember sent)
1485		(doctor-type (doc$ beclst)))))))
1486
1487(defun doctor-short ()
1488  (cond ((= (car repetitive-shortness) (1- lincount))
1489	 (rplacd repetitive-shortness
1490		 (1+ (cdr repetitive-shortness))))
1491	(t
1492	 (rplacd repetitive-shortness 1)))
1493  (rplaca repetitive-shortness lincount)
1494  (cond ((> (cdr repetitive-shortness) 6)
1495	 (cond ((not **mad**)
1496		(doctor-type '((doc$ areyou)
1497			       just trying to see what kind of things
1498			       i have in my vocabulary \? please try to
1499			       carry on a reasonable conversation!))
1500		(setq **mad** t))
1501	       (t
1502		(doctor-type '(i give up \. you need a lesson in creative
1503				 writing \.\.\.))
1504		)))
1505	(t
1506	 (cond ((equal sent (doctor-assm '(yes)))
1507		(doctor-type '((doc$ isee) (doc$ inter) (doc$ whysay) this is so \?)))
1508	       ((equal sent (doctor-assm '(because)))
1509		(doctor-type (doc$ shortbeclst)))
1510	       ((equal sent (doctor-assm '(no)))
1511		(doctor-type (doc$ neglst)))
1512	       (t (doctor-type (doc$ shortlst)))))))
1513
1514(defun doctor-alcohol () (doctor-type (doc$ drnk)))
1515
1516(defun doctor-desire ()
1517  (let ((foo (memq found sent)))
1518    (cond ((< (length foo) 2)
1519	   (doctor-go (doctor-build (doctor-meaning found) 1)))
1520	  ((memq (cadr foo) '(a an))
1521	   (rplacd foo (append '(to have) (cdr foo)))
1522	   (doctor-svo sent found 1 nil)
1523	   (doctor-remember (list subj 'would 'like obj))
1524	   (doctor-type (doc$ whywant)))
1525	  ((not (eq (cadr foo) 'to))
1526	   (doctor-go (doctor-build (doctor-meaning found) 1)))
1527	  (t
1528	   (doctor-svo sent found 1 nil)
1529	   (doctor-remember (list subj 'would 'like obj))
1530	   (doctor-type (doc$ whywant))))))
1531
1532(defun doctor-drug ()
1533  (doctor-type (doc$ drugs))
1534  (doctor-remember (list 'you 'used found)))
1535
1536(defun doctor-toke ()
1537  (doctor-type (doc$ toklst)))
1538
1539(defun doctor-state ()
1540  (doctor-type (doc$ states))(doctor-remember (list 'you 'were found)))
1541
1542(defun doctor-mood ()
1543  (doctor-type (doc$ moods))(doctor-remember (list 'you 'felt found)))
1544
1545(defun doctor-fear ()
1546  (setq feared (doctor-setprep sent found))
1547  (doctor-type (doc$ fears))
1548  (doctor-remember (list 'you 'were 'afraid 'of feared)))
1549
1550(defun doctor-hate ()
1551  (doctor-svo sent found 1 t)
1552  (cond ((memq 'not sent) (doctor-forget) (doctor-huh))
1553	((equal subj 'you)
1554	 (doctor-type '(why do you (doc// verb)(doc// obj) \?)))
1555	(t (doctor-type '((doc$ whysay)(list subj verb obj))))))
1556
1557(defun doctor-symptoms ()
1558  (doctor-type '((doc$ maybe) you should consult a medical doctor\;
1559		 i am a psychotherapist. \.)))
1560
1561(defun doctor-hates ()
1562  (doctor-svo sent found 1 t)
1563  (doctor-hates1))
1564
1565(defun doctor-hates1 ()
1566  (doctor-type '((doc$ whysay)(list subj verb obj) \?)))
1567
1568(defun doctor-loves ()
1569  (doctor-svo sent found 1 t)
1570  (doctor-qloves))
1571
1572(defun doctor-qloves ()
1573  (doctor-type '((doc$ bother)(list subj verb obj) \?)))
1574
1575(defun doctor-love ()
1576  (doctor-svo sent found 1 t)
1577  (cond ((memq 'not sent) (doctor-forget) (doctor-huh))
1578	((memq 'to sent) (doctor-hates1))
1579	(t
1580	 (cond ((equal object 'something)
1581		(setq object '(this person you love))))
1582	 (cond ((equal subj 'you)
1583		(setq lover obj)
1584		(cond ((equal lover '(this person you love))
1585		       (setq lover '(your partner))
1586		       (doctor-forget)
1587		       (doctor-type '(with whom are you in love \?)))
1588		      ((doctor-type '((doc$ please)
1589				      (doc$ describe)
1590				      (doc$ relation)
1591				      (doc// lover)
1592				      \.)))))
1593	       ((equal subj 'i)
1594		(doctor-txtype '(we were discussing you!)))
1595	       (t (doctor-forget)
1596		  (setq obj 'someone)
1597		  (setq verb (doctor-build verb 's))
1598		  (doctor-qloves))))))
1599
1600(defun doctor-mach ()
1601  (setq found (doctor-plural found))
1602  (doctor-type (doc$ machlst)))
1603
1604(defun doctor-sexnoun () (doctor-sexverb))
1605
1606(defun doctor-sexverb ()
1607  (if (or (memq 'me sent)(memq 'myself sent)(memq 'i sent))
1608      (doctor-foul)
1609    (doctor-type (doc$ sexlst))))
1610
1611(defun doctor-death ()
1612  (cond (suicide-flag (doctor-type (doc$ deathlst)))
1613	((or (equal found 'suicide)
1614             (and (or (equal found 'kill)
1615                      (equal found 'killing))
1616                  (memq 'yourself sent)))
1617	 (setq suicide-flag t)
1618	 (doctor-type '(If you are really suicidal, you might
1619			   want to contact the Samaritans via
1620			   E-mail: jo@samaritans.org or, at your option,
1621			   anonymous E-mail: samaritans@anon.twwells.com\ \.
1622                           or find a Befrienders crisis center at
1623			   http://www.befrienders.org/\ \.
1624			   (doc$ please) (doc$ continue) \.)))
1625	(t (doctor-type (doc$ deathlst)))))
1626
1627(defun doctor-foul ()
1628  (doctor-type (doc$ foullst)))
1629
1630(defun doctor-family ()
1631  (doctor-possess sent found)
1632  (doctor-type (doc$ famlst)))
1633
1634;; I did not add this -- rms.
1635;; But he might have removed it.  I put it back.  --roland
1636(defun doctor-rms ()
1637  (cond (rms-flag (doctor-type (doc$ stallmanlst)))
1638	(t (setq rms-flag t) (doctor-type '(do you know Stallman \?)))))
1639
1640(defun doctor-school nil (doctor-type (doc$ schoollst)))
1641
1642(defun doctor-eliza ()
1643  (cond (eliza-flag (doctor-type (doc$ elizalst)))
1644	(t (setq eliza-flag t)
1645	   (doctor-type '((doc// found) \? hah !
1646			  (doc$ please) (doc$ continue) \.)))))
1647
1648(defun doctor-sports ()  (doctor-type (doc$ sportslst)))
1649
1650(defun doctor-math () (doctor-type (doc$ mathlst)))
1651
1652(defun doctor-zippy ()
1653  (cond (zippy-flag (doctor-type (doc$ zippylst)))
1654	(t (setq zippy-flag t)
1655	   (doctor-type '(yow! are we interactive yet \?)))))
1656
1657
1658(defun doctor-chat () (doctor-type (doc$ chatlst)))
1659
1660(random t)
1661
1662(provide 'doctor)
1663
1664;; arch-tag: 579380f6-4902-4ea5-bccb-6339e30e1257
1665;;; doctor.el ends here
1666