(jlib.JLIB.load) ;; First we define the patterns and responses for the "doctor" program (define demo-patterns '( (("mom" "Mom" "dad" "Dad" "mother" "father" "Mother" "Father") ("Tell me more about your parents" "You seem a little uncomfortable when we discuss your parents. Why is that?" "Enough about your parents, lets talk about you.")) (("sad" "depressed" "tired" "anxious") ("Do you often feel down?" "Tell me about something positive that happened this week")) (("No" "no " "Not" " not " "n't") ("Try to be more positive." "Are you always this negative." "Hey. I'm here to help you, but I can't do it myself.")) (("You" "you ") ("We are here to talk about you, not me." "I am not the focus of this session." "Lets talk about you please.")) (("?") ("I'm the one who gets to ask the questions here." "What, so now you are the doctor who gets to ask questions." "I ask questions. You give answers. Got it?")) )) (define demo-default '("You seem to be avoiding any discussion of your parents" "Let's talk about your feelings." "Hmmmm. Please go on." "If you want to make progress, you have to open up!" "OK, tell me your deepest, darkest secret. Get it off your chest!" "I don't understand, could you elaborate?" "Please continue." "I see. Could you expand on that?")) ;; NOW We create the layout ;; create the main components (define computer (textfield "" 60 (TimesRomanPlain 18))) (define human (textfield "" 60 (TimesRomanPlain 18) (action (lambda (e) (writestring computer (make-response (readstring human) demo-patterns demo-default)) (writestring human ""))))) (define fontchoice (choice "12" "14" "16" "18" "20" "24" "30" (action (lambda(e) (.setFont human (CourierBold (readexpr fontchoice))) (.setFont computer (CourierBold (readexpr fontchoice))) (.invalidate human) (.invalidate computer))))) (define win (window "Cyberclinic")) ;; lay the components out into a window (define cyberGUI (col 'horizontal (color 220 220 255) (label "Dr. Silicon's Cyberclinic!" (HelveticaBold 24)) (border (west (label " user: " (CourierBold 18))) (center human)) (border (west (label "Dr. Silicon: " (CourierBold 18))) (center computer)) fontchoice (button "detach" (action(lambda(e) (.add win cyberGUI) (.pack win) (.show win)))) )) (define (init this) (.setLayout this (java.awt.GridLayout. 1 1)) (.add this cyberGUI) ) ;; Here are some helper procedures ;; generate a random integer between 0 and N-1, inclusive (define (rand N) (.intValue (Math.round (* (- N 1) (Math.random))))) ;; select a random element of the list L (define (select-rand L) (list-ref L (rand (length L)))) (define (contains word sentence) (> (.indexOf sentence word) -1)) (define (contains-one words sentence) (cond ((null? words) #f) ((contains (first words) sentence) #t) (else (contains-one (rest words) sentence)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Here is where we define the "logic" of the applet ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define (make-response sentence patterns default) (if (null? patterns) (select-rand default) (let ((keywords (first (first patterns))) (responses (second (first patterns)))) (if (contains-one keywords sentence) (select-rand responses) (make-response sentence (rest patterns) default)))))