;
;
; DOCTOR
;
; A simple chatbot modelled on the famous ELIZA program
; coded in Blitz Basic
;
Dim word$(PAIRS) ; Look-up table for pronouns and verbs
Dim conjugate$(PAIRS) ; Word to substitute for tense reversal
Dim key$(KEYWORDS) ; Look-up table for keywords and phrases
Dim index%(KEYWORDS)
Global nothing ; Flag to allow program to skip routines not needed
Global patient$ ; Patient's response
Global previous$ ; Patient's previous response
Global your$ ; Phrase following "my" in patient$
Global doctor$ ; Doctor's reply to patient
Global match% ; Index where match was found in keyword table
; Main program loop
Initialize()
While GetInput()
SwapWordForm()
ProcessKeywords()
RemoveMarkers()
GenerateReply()
Wend
End
Function Initialize()
Graphics 800, 600
AppTitle "Doctor"
SeedRnd MilliSecs()
; Load data into look-up tables
For i = 1 To PAIRS
Read word$(i)
Read conjugate$(i)
Next
For i = 1 To KEYWORDS
Read key$(i)
Read index(i)
Next
Print "PLEASE STATE YOUR PROBLEM."
End Function
Function GetInput()
nothing = True
patient$ = Upper$(Trim$(Input$()))
; Strip any trailing punctuation symbols
offset = Len(patient$)
While offset
char$ = Mid$(patient$, offset, 1)
If char >= "A" And char <= "Z" Then Exit
offset = offset - 1
Wend
patient$ = Left$(patient$, offset)
If patient$ = "" Then
Print "DO YOU HAVE A PROBLEM?"
Return True
Else If patient$ = previous$ Then
Print "PLEASE DON'T REPEAT YOURSELF!"
Return True
Else If patient$ = "BYE" Then
Return False
End If
; Valid response
previous$ = patient$
patient$ = " " + patient$ + " "
nothing = False
Return True
End Function
Function SwapWordForm()
If nothing Then Return
; Scan for pronouns or verbs to swap
For i = 1 To PAIRS
For offset = 1 To Len(patient$) - Len(word$(i)) + 1
If word$(i) = Mid$(patient$, offset, Len(word$(i))) Then
; Swap word for conjugate of opposite form
patient$ = Left$(patient$, offset-1) + conjugate$(i) + Mid$(patient$, offset + Len(word$(i)))
End If
Next
Next
End Function
Function ProcessKeywords()
If nothing Then Return
; Scan for keywords/phrases
For match = 1 To KEYWORDS
For offset = 1 To Len(patient$) - Len(key$(match)) + 1
If key$(match) = Mid$(patient$, offset, Len(key$(match))) Then
; keyword match found
doctor$ = Mid$(patient$, offset + Len(key$(match)), Len(patient$) - Len(Key$(match)))
If doctor$ = "" Then
Return
Else
doctor$ = Left$(doctor$, Len(doctor$) - 1)
Return
End If
End If
Next
Next
; No keywords found
If your$ = "" Then
choice = Rand(6) ; Non-committal responses only
Else
choice = Rand(11) ; Include reference to a previous statement saved in your$
End If
Select choice
; 1..6 Generate a suitably non-committal response
Case 1
Print "PLEASE GO ON."
Case 2
Print "PLEASE CONTINUE."
Case 3
Print "WHAT DOES THAT SUGGEST TO YOU?"
Case 4
Print "DO YOU FEEL STRONGLY ABOUT DISCUSSING THIS?"
Case 5
Print "I'M NOT SURE I UNDERSTAND YOU FULLY."
Case 6
Print "CAN YOU ELABORATE ON THAT?"
; 7..11 Refer back to a previous statement made by the patient
Case 7
Print "LET'S DISCUSS FURTHER WHY YOUR" + your$ + "."
Case 8
Print "EARLIER YOU SAID YOUR" + your$ + "."
Case 9
Print "DOES THAT HAVE ANYTHING TO DO WITH THE FACT THAT YOUR" + your$ + "?"
Case 10
Print "YOU MENTIONED BEFORE THAT YOUR" + your$ + "."
Print "LET'S DISCUSS THAT A BIT FURTHER."
Case 11
Print "WHAT MAKES YOU THINK YOUR" + your$ + "?"
End Select
nothing = True
End Function
Function RemoveMarkers()
If nothing Then Return
; The '*' and '@' symbols after certain words are markers to keep the
; program from getting confused between two uses of the same word.
For offset = 1 To Len(doctor$)
If Mid$(doctor$, offset, 1) = "*" Or Mid$(doctor$, offset, 1) = "@" Then
doctor$ = Left$(doctor$, offset - 1) + Mid$(doctor$, offset + 1, Len(doctor$) - offset)
End If
Next
End Function
Function GenerateReply()
If nothing Then Return
Select index(match)
Case 1
Print "DO COMPUTERS WORRY YOU?"
Case 2
Print "I'M NOT INTERESTED IN NAMES."
Case 3
Print "IN WHAT WAY?"
Case 4
Print "DO YOU OFTEN THINK OF" + doctor$ + "?"
Case 5
Print "DID YOU THINK I WOULD FORGET" + doctor$ + "?"
Case 6
Print "REALLY," + doctor$ + "?"
Case 7
Print "WHAT DOES THAT DREAM SUGGEST TO YOU?"
Case 8
Print "DO YOU THINK IT'S LIKELY THAT IF " + doctor$ + "?"
Case 9
Print "REALLY, " + key$(match) + "?"
Case 10
Print "WHAT IF YOU WERE" + doctor$ + "?"
Case 11
Print "WERE YOU REALLY?"
Case 12
Print "WOULD YOU LIKE TO BELIEVE I WAS" + doctor$ + "?"
Case 13
Print "TELL ME MORE ABOUT YOUR FAMILY."
Case 14
Print "YOUR" + doctor$ + "."
; Special processing if keyword is "my"
If Len(doctor$) > 11 Then
your$ = doctor$
End If
Case 15
Print "CAN YOU THINK OF A SPECIFIC EXAMPLE?"
Case 16
Print "WHY ARE YOU INTERESTED IN WHETHER OR NOT I'M" + doctor$ + "?"
Case 17
Print "DO YOU THINK I MIGHT NOT BE " + doctor$ + "?"
Case 18
Print "DO YOU BELIEVE YOU'RE" + doctor$ + "?"
Case 19
Print "IS THAT THE REAL REASON?"
Case 20
Print "YOU BELIEVE I CAN" + doctor$ + ", DON'T YOU?"
Case 21
Print "WHETHER OR NOT YOU CAN" + doctor$ + " DEPENDS ON YOU MORE THAN ON ME."
Case 22
Print "YOU SEEM QUITE POSITIVE."
Case 23
Print "I'M SORRY, I SPEAK ONLY ENGLISH."
Case 24
Print "HOW DO YOU DO."
Case 25
Print "WHY DO YOU ASK?"
Case 26
Print "WHAT MAKES YOU THINK I'M" + doctor$ + "?"
Case 27
Print "WE WERE DISCUSSING YOU, NOT ME."
Case 28
Print "YOU SEEM UNCERTAIN."
Case 29
Print "WHY ARE YOU CONCERNED OVER MY " + doctor$ + "?"
Case 30
Print "ARE YOU SAYING 'NO' JUST TO BE NEGATIVE?"
Case 31
Select Rand(4)
Case 1
Print "PLEASE DON'T APOLOGIZE."
Case 2
Print "APOLOGIES ARE NOT NECESSARY."
Case 3
Print "WHAT FEELINGS DO YOU HAVE WHEN YOU APOLOGIZE?"
Case 4
Print "YOU NEEDN'T FEEL THAT YOU HAVE TO APOLOGIZE."
End Select
Case 32
Print "DO YOU BELIEVE I DON'T" + doctor$ + "?"
Case 33
Print "DO YOU THINK YOU SHOULD BE ABLE TO" + doctor$ + "?"
Case 34
Print "WHAT WOULD IT MEAN IF YOU GOT" + doctor$ + "?"
Case 35
If Mid$(key$(match), 4, 1) = "@" Then doctor$ = Right$(key$(match), Len(key$(match)) - 9)
If Mid$(key$(match), 7, 1) = "*" Then doctor$ = Right$(key$(match), Len(key$(match)) - 7)
Print "I'M SORRY TO HEAR YOU ARE" + doctor$ + "."
Case 36
If Mid$(key$(match), 4, 1) = "@" Then doctor$ = Right$(key$(match), Len(key$(match)) - 9)
If Mid$(key$(match), 7, 1) = "*" Then doctor$ = Right$(key$(match), Len(key$(match)) - 7)
Print "HOW HAVE I HELPED YOU TO BE" + doctor$ + "?"
Case 37
Print "DO YOU REALLY THINK SO?"
Case 38
Print "IS IT BECAUSE YOU'RE" + doctor$ + " THAT YOU CAME TO ME?"
Case 39
Print "HOW DO YOU KNOW YOU CAN'T" + doctor$ + "?"
Case 40
Print "DON'T YOU REALLY" + doctor$ + "?"
Case 41
Print "TELL ME MORE ABOUT THESE FEELINGS."
Case 42
Print "DO YOU THINK THEY MIGHT NOT BE" + doctor$ + "?"
End Select
End Function
; Data for the word$-conjugate$ look-up table
Const PAIRS = 18
Data " MOM "," MOTHER "
Data " MUM "," MOTHER "
Data " MOMMY "," MOTHER "
Data " MUMMY "," MOTHER "
Data " DAD "," FATHER "
Data " DADDY "," FATHER "
Data " DREAMS "," DREAM "
Data " I "," YOU@ "
Data " YOU "," I "
Data " ME "," YOU "
Data " MY "," YOUR* "
Data " YOUR "," MY "
Data " MYSELF "," YOURSELF* "
Data " YOURSELF "," MYSELF "
Data " I'M "," YOU'RE* "
Data " YOU'RE "," I'M "
Data " AM "," ARE@ "
Data " WERE "," WAS "
; Data for the key$-index (keyword/phrase) look-up table
Const KEYWORDS = 91
Data "COMPUTER",1,"MACHINE",1,"PC",1,"LAPTOP",1
Data " NAME ",2
Data "ALIKE",3," LIKE ",3," SAME ",3,"I REMIND YOU OF",3
Data "YOU@ REMEMBER",4
Data "DO I REMEMBER",5
Data "YOU@ DREAMED",6
Data " DREAM ",7
Data " IF ",8
Data "EVERYBODY",9,"EVERYONE",9,"NOBODY",9,"NO ONE",9
Data "WAS YOU@",10
Data "YOU@ WAS",11
Data "WAS I",12
Data "YOUR* MOTHER",13,"YOUR* FATHER",13,"YOUR* SISTER",13,"YOUR* BROTHER",13
Data "YOUR* WIFE",13,"YOUR* HUSBAND",13,"YOUR* CHILDREN",13,"YOUR* KIDS",13
Data "YOUR*",14
Data "ALWAYS",15
Data "ARE I",16
Data "I ARE ",17
Data "ARE@ YOU@",18
Data "BECAUSE",19
Data "CAN I",20
Data "CAN YOU@",21
Data "CERTAINLY",22,"YES ",22,"YEAH",22,"YES,",22,"YEP",22
Data "DEUTSCH",23,"ESPANOL",23,"FRANCAIS",23,"ITALIANO",23
Data "HELLO",24,"HI ",24,"HI,",24
Data " HOW ",25,"WHAT ",25,"WHEN ",25
Data "I ARE",26,"I'M",26
Data "MAYBE",28,"PERHAPS",28
Data " MY ",29
Data " NO ",30
Data "SORRY",31
Data "WHY DON'T I",32
Data "WHY CAN'T YOU@",33
Data " I ",27
Data "YOU@ WANT",34,"YOU@ NEED",34
Data "YOU@ ARE@ SAD",35,"YOU'RE* SAD",35,"YOU@ ARE@ UNHAPPY",35,"YOU'RE* UNHAPPY",35
Data "YOU@ ARE@ DEPRESSED",35,"YOU'RE* DEPRESSED",35,"YOU@ ARE@ SICK",35,"YOU'RE* SICK",35
Data "YOU@ ARE@ HAPPY",36,"YOU'RE* HAPPY",36,"YOU@ ARE@ ELATED",36,"YOU'RE* ELATED",36
Data "YOU@ ARE@ GLAD",36,"YOU'RE* GLAD",36,"YOU@ ARE@ BETTER",36,"YOU'RE* BETTER",36
Data "YOU@ FEEL YOU@",37,"YOU@ THINK YOU@",37,"YOU@ BELIEVE YOU@",37,"YOU@ WISH YOU@",37
Data " YOU@ ARE@",38,"YOU'RE*",38
Data "YOU@ CAN'T",39,"YOU@ CANNOT",39
Data "YOU@ DON'T",40
Data "YOU@ FEEL",41
Data "THEY ARE",42