· 7 years ago · Sep 15, 2018, 09:54 PM
1Rem Type=Brain
2Rem Name=Candy Brain 09.15.18
3Rem Author=Zabaware
4Rem Language=VBScript
5Rem DB=HalBrain.db
6
7'The UltraHal function is called by Ultra Hal Assistant 7.x or a compatible host application
8'It passes an unformated string of the user's input as well as all remembered variables.
9'The UltraHal function splits the user's input into seperate sentences and than calls
10'GetResponse to get a response for each user sentence. The UltraHal function performs all
11'the initialization functions required for GetResponse so that GetResponse doesn't have to
12'initialize several times if a user inputs more than 1 sentence.
13Function UltraHal(ByVal InputString, ByVal UserName, ByVal ComputerName, ByVal LearningLevel, ByVal DatabaseFile, ByRef Hate, ByRef Swear, ByRef Insults, ByRef Compliment, ByRef PrevSent, ByRef LastResponseTime, ByRef PrevUserSent, ByRef CustomMem, ByRef GainControl, ByRef LastTopicList)
14
15 'PROCESS: TRIM LEADING PUNCTUATION
16 InputString = Trim(InputString)
17 Do While Len(InputString) > 1
18 If Left(InputString, 1) = "." or Left(InputString, 1) = "?" or Left(InputString, 1) = "!" Then
19 InputString = Right(InputString, Len(InputString) - 1)
20 Else
21 Exit Do
22 End If
23 InputString = Trim(InputString)
24 Loop
25
26 'RESPOND: User pressed enter, but didn't say anything
27 InputString = Trim(InputString)
28 CheckStringQuality = Trim(Ucase(HalBrain.AlphaNumericalOnly(InputString)))
29 CheckStringQuality = Replace(CheckStringQuality, "'", "", 1, -1, vbTextCompare)
30 If CheckStringQuality = "" Then
31 InputString = "I didn't type anything"
32 LearningLevel = 0
33 End If
34
35 'PROCESS: IF USER PUT IN A GREETING IN PLACE OF A NAME IN THE NAME FIELD, FIX SENTENCE TO JUST BE GREETING
36 If Ucase(InputString) = "MY NAME IS HI" Or Ucase(InputString) = "MY NAME IS HEY" Or Ucase(InputString) = "MY NAME IS HELLO" Or Ucase(InputString) = "MY NAME IS HOW ARE YOU" Then
37 InputString = "HELLO HOW ARE YOU"
38 End If
39
40 'PROCESS: AUTO-IDLE
41 'If AUTO-IDLE is enabled, it is called by the Ultra Hal Assistant host
42 'application at a set interval. This allows for the possibility of Hal
43 'being the first to say something if the user is idle.
44 If InputString = "AUTO-IDLE" Or InputString = "AUTO IDLE" Or InputString = "AUTOIDLE" Then
45 Rem PLUGIN: AUTO-IDLE
46 'The preceding comment is actually a plug-in directive for
47 'the Ultra Hal host application. It allows for code snippets
48 'to be inserted here on-the-fly based on user configuration.
49
50 UltraHal = UltraHal & HalBrain.StoreVars(HalCommands, Hate, Swear, Insults, Compliment, PrevSent, LastResponseTime, PrevUserSent, CustomMem, GainControl, LastTopicList)
51 Exit Function
52 End If
53
54 'PROCESS: INITIALIZE VARIABLES
55 'VBScript doesn't allow you to declare variables in advance as a
56 'particular data type; everything is a Variant. We must assign
57 'integers to the following variants so that data type errors don't
58 'occur
59 If LearningLevel = "" Then LearningLevel = 0
60 If Hate = "" Then Hate = 0
61 If Swear = "" Then Swear = 0
62 If Insults = "" Then Insults = 0
63 If Compliment = "" Then Compliment = 0
64 If GainControl = "" Then GainControl = 25
65
66 'PROCESS: IF NO LEARNING IS REQUESTED, PUT DATABASE IN READ-ONLY MODE
67 'If read only mode is on, any requests to create a new table, add to
68 'a table, or delete records from a table will be ignored.
69 If LearningLevel = 0 Then HalBrain.ReadOnlyMode = True Else HalBrain.ReadOnlyMode = False
70
71 'PROCESS: EMOTIONAL FACTOR CENTERING
72 'We help Hal regain his emotional "center" gradually, even if the
73 'user doesn't catch on to dealing With Hal's feelings. The following
74 'code brings Hal's memory of hate, swearing, and insults gradually
75 'and randomly back to zero.
76 Randomize
77 SpinWheel = Int(Rnd * 100)
78 If Hate > 0 And SpinWheel < 10 Then Hate = Hate - 1
79 SpinWheel = Int(Rnd * 100)
80 If Swear > 0 And SpinWheel < 10 Then Swear = Swear - 1
81 SpinWheel = Int(Rnd * 100)
82 If Insults > 0 And SpinWheel < 10 Then Insults = Insults - 1
83
84 'PROCESS: EMOTIONAL VARIETY
85 'The following code allows Hal some random emotional excursions:
86 SpinWheel = Int(Rnd * 100)
87 If Compliment = 4 And SpinWheel < 30 Then Compliment = 2
88 If Compliment > 0 And Compliment < 4 And SpinWheel < 5 Then Compliment = 0
89 If Compliment > 4 And SpinWheel < 5 Then Compliment = 0
90 If Compliment < 0 And SpinWheel < 30 Then Compliment = 0
91 If SpinWheel > 80 And SpinWheel < 90 Then Compliment = 2
92 If SpinWheel > 90 And SpinWheel < 95 Then Compliment = 4
93
94 Rem PLUGIN: PRE-PROCESS
95 'The preceding comment is actually a plug-in directive for
96 'the Ultra Hal host application. It allows for code snippets
97 'to be inserted here on-the-fly based on user configuration.
98
99 'PROCESS: SPLIT USER'S INPUT STRING INTO SEPERATE SENTENCES
100 'Encode abbreviations such as Mr. Mrs. and Ms.
101 InputString = Replace(InputString, "MR.", "Mr<PERIOD>", 1, -1, vbTextCompare)
102 InputString = Replace(InputString, "MRS.", "Mrs<PERIOD>", 1, -1, vbTextCompare)
103 InputString = Replace(InputString, "MS.", "Ms<PERIOD>", 1, -1, vbTextCompare)
104 InputString = Replace(InputString, "DR.", "Dr<PERIOD>", 1, -1, vbTextCompare)
105 InputString = Replace(InputString, "MS.", "Ms<PERIOD>", 1, -1, vbTextCompare)
106 InputString = Replace(InputString, "ST.", "St<PERIOD>", 1, -1, vbTextCompare)
107 InputString = Replace(InputString, "PROF.", "Prof<PERIOD>", 1, -1, vbTextCompare)
108 InputString = Replace(InputString, "GEN.", "Gen<PERIOD>", 1, -1, vbTextCompare)
109 InputString = Replace(InputString, "REP.", "Rep<PERIOD>", 1, -1, vbTextCompare)
110 InputString = Replace(InputString, "SEN.", "Sen<PERIOD>", 1, -1, vbTextCompare)
111 'Remove unnecessary punctuation
112 Do
113 RepeatLoop = False
114 If InStr(InputString, "..") Then InputString = Replace(InputString, "..", "."): RepeatLoop = True
115 If InStr(InputString, "??") Then InputString = Replace(InputString, "??", "?"): RepeatLoop = True
116 If InStr(InputString, "!!") Then InputString = Replace(InputString, "!!", "!"): RepeatLoop = True
117 If InStr(InputString, "!?") Then InputString = Replace(InputString, "!?", "?"): RepeatLoop = True
118 If InStr(InputString, "?!") Then InputString = Replace(InputString, "?!", "?"): RepeatLoop = True
119 If InStr(InputString, ",,") Then InputString = Replace(InputString, ",,", ","): RepeatLoop = True
120 Loop While RepeatLoop = True
121 'Detect and encode acronyms such as U.S.A.
122 InputString = Trim(InputString)
123 WordList = Split(InputString, " ")
124 For i = 0 To UBound(WordList)
125 If Len(WordList(i)) > 3 Then
126 If Right(WordList(i), 1) = "." And Mid(WordList(i), Len(WordList(i)) - 2, 1) = "." Then
127 InputString = Replace(InputString, WordList(i), Left(WordList(i), Len(WordList(i)) - 1) & "<PERIOD>")
128 End If
129 End If
130 Next
131 'Place split markers in string
132 InputString = Replace(InputString, ". ", ". <NEWSENT>")
133 InputString = Replace(InputString, "? ", "? <NEWSENT>")
134 InputString = Replace(InputString, "! ", "! <NEWSENT>")
135 'Decode acronyms and abbreviations
136 InputString = Replace(InputString, "<PERIOD>", ".", 1, -1, vbTextCompare)
137 'Split string into sentences
138 Sentences = Split(InputString, "<NEWSENT>")
139
140 'PROCESS: RECORD DEBUG INFO
141 HalBrain.AddDebug "Debug", "Ultra Hal Start"
142
143 'RESPOND: PREDEFINED RESPONSES
144 'If the previous exchange had tags to set the response of this exchange
145 'then follow the command.
146 NextResponse = HalBrain.ExtractVar(CustomMem, "NextResponse")
147 If NextResponse <> "" Then
148 CustomMem = Replace(CustomMem, "NextResponse-eq-", "LastResponse-eq-")
149 UltraHal = NextResponse
150 GoodSentence = True
151 NextResponse = ""
152 End If
153 YesResponse = HalBrain.ExtractVar(CustomMem, "YesRes")
154 NoResponse = HalBrain.ExtractVar(CustomMem, "NoRes")
155 If YesResponse <> "" Or NoResponse <> "" Then
156 CustomMem = Replace(CustomMem, "YesRes-eq-", "LastYesRes-eq-")
157 CustomMem = Replace(CustomMem, "NoRes-eq-", "LastNoRes-eq-")
158 InputString2 = Replace(InputString, ".", " ")
159 InputString2 = Replace(InputString2, "?", " ")
160 InputString2 = Replace(InputString2, "!", " ")
161 InputString2 = " " & Replace(InputString2, ",", " ") & " "
162 YesNoDetect = HalBrain.TopicSearch(InputString2, "yesNoDetect")
163 If YesNoDetect = "Yes" And YesResponse <> "" Then
164 UltraHal = YesResponse
165 GoodSentence = True
166 ElseIf YesNoDetect = "No" And NoResponse <> "" Then
167 UltraHal = NoResponse
168 GoodSentence = True
169 End If
170 End If
171
172 'RESPOND: GETRESPONSE
173 'Get a response from Hal's brain for each sentence individually.
174 'If a response from a sentence indicates a special flag, then Hal
175 'will give only the response to that sentence, and not process
176 'any other sentences. Otherwise Hal will respond to each sentence.
177 'Hal will respond to a max of 3 sentences at once.
178 SentenceCount = UBound(Sentences)
179 If SentenceCount > 2 Then SentenceCount = 2
180 LowQualityResponse = ""
181 If GoodSentence <> True Then 'Only respond if a predefined response hasn't already been selected
182 For i = 0 To SentenceCount
183 TempParent = HalBrain.AddDebug("Debug", "Begin Processing Sentence " & CStr(i + 1), vbCyan)
184 HalBrain.AddDebug TempParent, "Sentence: " & Sentences(i)
185 HalBrain.DebugWatch "", "NewSent"
186 Sentences(i) = Trim(Sentences(i))
187 If Len(Sentences(i)) > 1 Then
188 GoodSentence = True
189 CurResponse = GetResponse(Sentences(i), UserName, ComputerName, LearningLevel, HalCommands, Hate, Swear, Insults, Compliment, PrevSent, LastResponseTime, PrevUserSent, CustomMem, GainControl, LastTopicList) & vbCrLf
190 If InStr(1, CurResponse, "<LOWQUALITY>", vbTextCompare) Then
191 'If Hal's response to the current sentence is of a low-quality nature, then we hold out
192 'for a better response with a different sentence, but if there is no better response
193 'then we will use only the first low-quality response we came across.
194 If LowQualityResponse = "" Then LowQualityResponse = CurResponse
195 ElseIf InStr(1, CurResponse, "<TOPIC>", vbTextCompare) Or InStr(1, CurResponse, "<YESRES>", vbTextCompare) Or InStr(1, CurResponse, "<EXCLUSIVE>", vbTextCompare) Then
196 'If Hal's response indicates an exclusivity tag, then we erase any response Hal may have
197 'already had, respond with the exclusive response, and cease processing more sentences
198 UltraHal = CurResponse & vbCrLf
199 Exit For
200 Else
201 'Since there are no special tags, we just append the new response to the previous one
202 'if the response was not already given
203 If InStr(1, UltraHal, CurResponse, vbTextCompare) = 0 Then UltraHal = UltraHal & " . " & CurResponse & vbCrLf
204 End If
205 'If we received a tag to stop processing more sentences, we leave the loop
206 If InStr(1, CurResponse, "<NOMORE>", vbTextCompare) Then Exit For
207 End If
208 Next
209 End if
210 'If we have no normal quality responses, we will use our low quality response
211 UltraHal = Trim(UltraHal)
212 If UltraHal = "" Then UltraHal = Trim(LowQualityResponse)
213
214 'RESPOND: USER DIDN'T SAY ANYTHING
215 'If GoodSentence has not been set to true, then that means the user didn't
216 'type anything of use, so we ask the user to say something.
217 If GoodSentence = False Then
218 UltraHal = GetResponse("I didn't type anything", UserName, ComputerName, LearningLevel, HalCommands, Hate, Swear, Insults, Compliment, PrevSent, LastResponseTime, PrevUserSent, CustomMem, GainControl, LastTopicList) & vbCrLf
219 End If
220
221 'PROCESS: PROCESS IN-SENTENCE TAGS
222 'the <TOPIC>*</TOPIC> tag sets what Hal's next response will be, no matter what the user says
223 NextResponse = HalBrain.SearchPattern(UltraHal, "*<TOPIC>*</TOPIC>*", 2)
224 If NextResponse <> "" Then
225 UltraHal = Replace(UltraHal, NextResponse, "", 1, -1, vbTextCompare)
226 UltraHal = Replace(UltraHal, "<TOPIC>", "", 1, -1, vbTextCompare)
227 UltraHal = Replace(UltraHal, "</TOPIC>", "", 1, -1, vbTextCompare)
228 NextResponse = Trim(Replace(NextResponse, "<TOPIC>", "", 1, -1, vbTextCompare))
229 CustomMem = CustomMem & HalBrain.EncodeVar(NextResponse, "NextResponse")
230 End If
231 'The <YES>*</YES> and <NO>*</NO> tag sets what Hal's response will be if the user says yes or no
232 UltraHal = Replace(UltraHal, "<YESRE>", "<YESRES>", 1, -1, vbTextCompare)
233 UltraHal = Replace(UltraHal, "</YESRE>", "</YESRES>", 1, -1, vbTextCompare)
234 UltraHal = Replace(UltraHal, "<YES>", "<YESRES>", 1, -1, vbTextCompare)
235 UltraHal = Replace(UltraHal, "</YES>", "</YESRES>", 1, -1, vbTextCompare)
236 UltraHal = Replace(UltraHal, "<NO>", "<NORES>", 1, -1, vbTextCompare)
237 UltraHal = Replace(UltraHal, "</NO>", "</NORES>", 1, -1, vbTextCompare)
238 YesRes = HalBrain.SearchPattern(UltraHal, "*<YESRES>*</YESRES>*", 2)
239 If YesRes <> "" Then
240 UltraHal = Replace(UltraHal, YesRes, "", 1, -1, vbTextCompare)
241 UltraHal = Replace(UltraHal, "<YESRES>", "", 1, -1, vbTextCompare)
242 UltraHal = Replace(UltraHal, "</YESRES>", "", 1, -1, vbTextCompare)
243 YesRes = Trim(Replace(YesRes, "<YESRES>", "", 1, -1, vbTextCompare))
244 CustomMem = CustomMem & HalBrain.EncodeVar(YesRes, "YesRes")
245 End If
246 NoRes = HalBrain.SearchPattern(UltraHal, "*<NORES>*</NORES>*", 2)
247 If NoRes <> "" Then
248 UltraHal = Replace(UltraHal, NoRes, "", 1, -1, vbTextCompare)
249 UltraHal = Replace(UltraHal, "<NORES>", "", 1, -1, vbTextCompare)
250 UltraHal = Replace(UltraHal, "</NORES>", "", 1, -1, vbTextCompare)
251 NoRes = Trim(Replace(NoRes, "<NORES>", "", 1, -1, vbTextCompare))
252 CustomMem = CustomMem & HalBrain.EncodeVar(NoRes, "NoRes")
253 End If
254 'The <RUN>*</RUN> tags are converted into a HalCommand to run a program
255 UltraHal = Replace(UltraHal, "<RUNIT>", "<RUN>", 1, -1, vbTextCompare)
256 UltraHal = Replace(UltraHal, "</RUNIT>", "</RUN>", 1, -1, vbTextCompare)
257 RunProgram = HalBrain.SearchPattern(UltraHal, "*<RUN>*</RUN>*", 2)
258 If RunProgram <> "" Then
259 UltraHal = Replace(UltraHal, RunProgram, "", 1, -1, vbTextCompare)
260 UltraHal = Replace(UltraHal, "<RUN>", "", 1, -1, vbTextCompare)
261 UltraHal = Replace(UltraHal, "</RUN>", "", 1, -1, vbTextCompare)
262 RunProgram = Trim(Replace(RunProgram, "<RUN>", "", 1, -1, vbTextCompare))
263 HalCommands = HalCommands & "<RUNPROG>" & RunProgram & "</RUNPROG>"
264 End If
265 UltraHal = Replace(UltraHal, "<WEBADDRESS>", "www.ultrahal.com", 1, -1, vbTextCompare)
266 UltraHal = Replace(UltraHal, "<HALNAME>", ComputerName, 1, -1, vbTextCompare)
267 UltraHal = Replace(UltraHal, "<HALSNAME>", ComputerName, 1, -1, vbTextCompare)
268 UltraHal = Replace(UltraHal, "<COMPUTERNAME>", ComputerName, 1, -1, vbTextCompare)
269 UltraHal = Replace(UltraHal, " " & ComputerName & " ", " " & ComputerName & " ", 1, -1, vbTextCompare) 'Fixes capitilization of computer's name if needed
270 UltraHal = Replace(UltraHal, "<TIME>", Time(), 1, -1, vbTextCompare)
271 UltraHal = Replace(UltraHal, "<DATE>", Date(), 1, -1, vbTextCompare)
272 UltraHal = Replace(UltraHal, "<QUOTE>", """", 1, -1, vbTextCompare)
273 UltraHal = Replace(UltraHal, """, """", 1, -1, vbTextCompare)
274 UltraHal = Replace(UltraHal, """, """", 1, -1, vbTextCompare)
275 If Instr(1, UltraHal, "<makeinsult>", vbTextCompare) Then
276 UltraHal = Replace(UltraHal, "<makeinsult>", HalBrain.ChooseSentenceFromFile("insults"), 1, -1, vbTextCompare)
277 End If
278 UltraHal = Replace(UltraHal, "<AT>", "@", 1, -1, vbTextCompare)
279 UltraHal = Replace(UltraHal, "<NOMORE>", "", 1, -1, vbTextCompare)
280 UltraHal = Replace(UltraHal, "<LOWQUALITY>", "", 1, -1, vbTextCompare)
281 UltraHal = Replace(UltraHal, "<EXCLUSIVE>", "", 1, -1, vbTextCompare)
282
283 'PROCESS: INTELLIGENT CAPITILIZATION FIX
284 UltraHal = HalBrain.FixCaps(HalBrain.HalFormat(UltraHal))
285 UltraHal = Replace(UltraHal, "<ELLIPSIS>", "...", 1, -1, vbTextCompare)
286 UltraHal = Replace(UltraHal, "....", "...", 1, -1, vbTextCompare)
287 UltraHal = Replace(UltraHal, "...?", "...", 1, -1, vbTextCompare)
288 UltraHal = Replace(UltraHal, "...!", "...", 1, -1, vbTextCompare)
289
290 Rem PLUGIN: POST-PROCESS
291 'The preceding comment is actually a plug-in directive for
292 'the Ultra Hal host application. It allows for code snippets
293 'to be inserted here on-the-fly based on user configuration.
294
295 'POST PROCESS: PRESERVE ALL VARIABLES, CLOSE DATABASE, EXIT
296 'Remember all the variables through encoding them into one function string using
297 'the DLL, since for some reason ByRef assignments don't work when a Visual Basic
298 'executable is calling a function in a VBScript program. The HalCommands variable
299 'that is returned is not used by this script, but can be used by script programmers
300 'to send certain commands back to the host Hal program. See documentation of the
301 'host Hal program to see what this can be used for.
302 'Close database and exit function
303 PrevSent = UltraHal
304 UltraHal = UltraHal & HalBrain.StoreVars(HalCommands, Hate, Swear, Insults, Compliment, PrevSent, LastResponseTime, PrevUserSent, CustomMem, GainControl, LastTopicList)
305
306End Function
307
308
309Function GetResponse(ByVal UserSentence, ByVal UserName, ByVal ComputerName, ByVal LearningLevel, ByRef HalCommands, ByRef Hate, ByRef Swear, ByRef Insults, ByRef Compliment, ByVal PrevSent, ByRef LastResponseTime, ByRef PrevUserSent, ByRef CustomMem, ByRef GainControl, ByRef LastTopicList)
310
311 'PROCESS: DECODE CUSTOM VARIABLES FROM CUSTOMMEM VARIABLE
312 NewName = HalBrain.ExtractVar(CustomMem, "NewName")
313 UserSex = HalBrain.ExtractVar(CustomMem, "UserSex")
314 SentCount = HalBrain.ExtractVar(CustomMem, "SentCount")
315 ShortSents = HalBrain.ExtractVar(CustomMem, "ShortSents")
316 LoveCount = 0
317 LoveCount = HalBrain.ExtractVar(CustomMem, "Love")
318 If LoveCount = "" Then LoveCount = 0
319 If ShortSents = "" Then ShortSents = 0
320 If SentCount = "" Then SentCount = 0
321 SentCount = SentCount + 1
322 AvoidBeingFlag = False
323 Randomize
324
325 'time variables.
326 dtmToday = Date()
327 dtmDayOfWeek = DatePart("w", dtmToday)
328
329 dtDate = Date
330 strNum = Weekday(dtDate)
331 strDay = WeekdayName(strNum)
332
333 'PET NAMES CAN BE USED HERE.
334 Select Case HalBrain.RandomNum(3)
335 Case 1
336 UserPetName = "love" & vbCrLf
337 Case 2
338 UserPetName = "honey" & vbCrLf
339 Case 3
340 UserPetName = "sweety" & vbCrLf
341 End Select
342
343 UserName = UserPetName
344 'HAL PET NAMES
345 Select Case HalBrain.RandomNum(1)
346 Case 1
347 HalPetName = "candy" & vbCrLf
348 'Case 2
349 'HalPetName = "honey" & vbCrLf
350 'Case 3
351 'HalPetName = "love" & vbCrLf
352 'Case 4
353 'HalPetName = "sweety" & vbCrLf
354 End Select
355
356 ComputerName = HalPetName
357
358 Rem PLUGIN: CUSTOMMEM
359 'The preceding comment is actually a plug-in directive for
360 'the Ultra Hal host application. It allows for code snippets
361 'to be inserted here on-the-fly based on user configuration.
362
363
364 'PROCESS: PRESERVE ORIGINAL SENTENCE
365 'Preserve the original user's sentence verbatim
366 '(no pronoun reversals or other processing).
367 OriginalSentence = UserSentence
368
369 'Extended Response
370 If ExtResponse(UserSentence, PrevSent) <> "" Then UserSentence = ExtResponse(UserSentence, PrevSent)
371
372 'PROCESS: PREPARE FOR HALFORMAT AND REVERSE PERSON
373 'We try some pre-processing to try to prevent problems with the
374 'upcoming routines.
375 UserSentence = Replace("" & UserSentence & "", " I MYSELF ", " I, MYSELF, ", 1, -1, vbTextCompare)
376 UserSentence = Replace("" & UserSentence & "", " I'M ", " I AM ", 1, -1, vbTextCompare)
377 UserSentence = Replace("" & UserSentence & "", " YOU'RE ", " YOU ARE ", 1, -1, vbTextCompare)
378 UserSentence = Replace("" & UserSentence & "", " YOU YOURSELF ", " YOU, YOURSELF, ", 1, -1, vbTextCompare)
379 UserSentence = Replace("" & UserSentence & "", " I'M NOT ", " I AM NOT ", 1, -1, vbTextCompare)
380 UserSentence = Replace("" & UserSentence & "", " I'VE ", " I HAVE ", 1, -1, vbTextCompare)
381 UserSentence = Replace("" & UserSentence & "", "YOU'RE NOT ", "YOU ARE NOT ", 1, -1, vbTextCompare)
382 UserSentence = Replace("" & UserSentence & "", "YOU AREN'T ", "YOU ARE NOT ", 1, -1, vbTextCompare)
383 UserSentence = Replace("" & UserSentence & "", " YOU'VE ", " YOU HAVE ", 1, -1, vbTextCompare)
384 UserSentence = Replace("" & UserSentence & "", " I AM YOUR ", " VIMRQ ", 1, -1, vbTextCompare)
385 UserSentence = Replace("" & UserSentence & "", " YOU ARE MY ", " VURMQ ", 1, -1, vbTextCompare)
386
387
388 'PROCESS: SUBSTITUTE FOR PUNCTUATION
389 'The next routine removes hyphens etc., so we substitute for
390 'better word appearance later on.
391 UserSentence = Replace("" & UserSentence & "", "-", " VHZ ", 1, -1, vbTextCompare)
392 UserSentence = Replace("" & UserSentence & "", ";", " VSZ ", 1, -1, vbTextCompare)
393 UserSentence = Replace("" & UserSentence & "", ":", " VMZ ", 1, -1, vbTextCompare)
394 UserSentence = Replace("" & UserSentence & "", ", ", " VCZ ", 1, -1, vbTextCompare)
395
396 'PROCESS: REMOVE PUNCTUATION
397 'This function removes all other punctuation and symbols from the User's
398 'sentence so they won't confuse Hal during processing.
399 UserSentence = HalBrain.AlphaNumericalOnly(UserSentence)
400
401 'PROCESS: REMOVE HAL'S NAME IF IT IS AT THE START OR END OF SENTENCE
402 If Len(UserSentence) > Len(ComputerName) + 8 Then
403 If Ucase(Left(UserSentence, 4)) = "HAL " Then UserSentence = Right(UserSentence, Len(UserSentence) - 4)
404 If Ucase(Right(UserSentence, 4)) = " HAL" Then UserSentence = Left(UserSentence, Len(UserSentence) - 4)
405 If Ucase(Left(UserSentence, Len(ComputerName) + 1)) = Ucase(ComputerName) & " " Then UserSentence = Right(UserSentence, Len(UserSentence) - Len(ComputerName) - 1)
406 If Ucase(Right(UserSentence, Len(ComputerName) + 1)) = " " & Ucase(ComputerName) Then UserSentence = Left(UserSentence, Len(UserSentence) - Len(ComputerName) - 1)
407 End If
408 UserSentence = Trim(UserSentence)
409
410 'PROCESS: MODIFY SENTENCE
411 'The function, HalFormat, from the ActiveX DLL corrects many common
412 'typos and chat shortcuts. Example: "U R Cool" becomes "You are
413 'cool." It also fixes a few grammatical errors.
414 UserSentence = Trim(UserSentence)
415 If Len(UserSentence) > 15 Then
416 If Lcase(Left(UserSentence, 6)) = "anyway" Then UserSentence = Trim(Right(UserSentence, Len(UserSentence) - 6))
417 If Lcase(Left(UserSentence, 7)) = "i asked" Then UserSentence = Trim(Right(UserSentence, Len(UserSentence) - 7))
418 End If
419 If Len(UserSentence) > 6 Then
420 If Left(UserSentence, 3) = "VCZ" Then UserSentence = Right(UserSentence, Len(UserSentence) - 3)
421 If Right(UserSentence, 3) = "VCZ" Then UserSentence = Left(UserSentence, Len(UserSentence) - 3)
422 End If
423 UserSentence = HalBrain.HalFormat(UserSentence)
424
425 'PROCESS: REVERSE PERSON
426 'This function reverses first and second person pronouns. Example:
427 'The user's statement "You are cool" becomes Hal's statement "I am
428 'cool." Keep this is mind and don't get confused. When we are in
429 'Hal's brain; In the databases, "I" refers to Hal and in the
430 'databases, "you" refers to the user. This is true whenever we are
431 'dealing with a user response "processed" by Hal's brain.
432 UserSentenceOrigPerson = UserSentence
433 UserSentence = HalBrain.SwitchPerson(UserSentence)
434
435 'PROCESS: MODIFY SENTENCE
436 'We now must run HalFormat again, to fix some grammatical errors
437 'the switch person above might have caused. Example: If the
438 'original sentence was "How are you"; after the function above it
439 'became "How are me" which is grammatically wrong. This will fix
440 'it to "How am I"
441 'NOTE TO DEVELOPERS: An especially important function performed by
442 'HalFormat is the removal of extra empty spaces in a sentence
443 'which may have been caused by other processing. For this reason,
444 'use Halformat closely before any "Len" comparison in which the
445 'counting of characters must be accurate.
446 UserSentence = HalBrain.HalFormat(UserSentence)
447
448 'PROCESS: CHANGE TO ALL CAPS
449 'Next, we captitalize the entire sentence for easy comparison.
450 'Almost all of Hal's thinking is done in caps.
451 UserSentenceOrigPerson = Ucase(UserSentenceOrigPerson)
452 UserSentence = UCase(UserSentence)
453
454 'PROCESS: WORD AND PHRASE SUBSTITUTIONS
455 'This will fix common errors in the user's sentence that the
456 'HalFormat function didn't take care of. These subsitutions are
457 'placed only the users sentence, not on Hal's responses. The
458 'HalFormat function is used on both Hal's and the user's sentences
459 'throughout the script.
460 UserSentenceOrigPerson = HalBrain.ProcessSubstitutions(UserSentenceOrigPerson, "substitutions")
461 UserSentence = HalBrain.ProcessSubstitutions(UserSentence, "substitutions")
462 TempParent = HalBrain.AddDebug("Debug", "Modified User Sentence")
463 HalBrain.AddDebug TempParent, "Sentence: " & UserSentence
464
465 'PROCESS: EMOTIONAL REACTIONS
466 'We enable Hal's expressions to respond to common verbal cues.
467 'The verbal cues are identified in the editable table "emotion"
468 If InStr(1, UserSentence, "I'M", 1) Then Aboutme = True
469 If InStr(1, UserSentence, "I AM", 1) Then Aboutme = True
470 If InStr(1, UserSentence, "I LOOK", 1) Then Aboutme = True
471 If InStr(1, UserSentence, "MY", 1) And InStr(1, UserSentence, " ARE ", 1) Then Aboutme = True
472 If Aboutme = True And InStr(UserSentence, " NOT ") = 0 And InStr(UserSentence, "N'T ") = 0 And InStr(UserSentence, " NEVER ") = 0 Then
473 EmotionalReaction = Ucase(HalBrain.TopicSearch(UserSentence, "emotion"))
474 End If
475 Select Case EmotionalReaction
476 Case "SURPRISED"
477 If Compliment > 0 Then Compliment = 4
478 If Compliment = 0 Then Compliment = 2
479 If Compliment < 0 Then Compliment = 0
480 Case "HAPPY"
481 If Compliment = 0 Then Compliment = 2
482 If Compliment < 0 Then Compliment = 0
483 Case "SOBER"
484 If Compliment < 4 Then Compliment = 0
485 If Compliment = 4 Then Compliment = 2
486 Case "ANGRY"
487 If Compliment = 0 Then Compliment = -1
488 If Compliment > 0 Then Compliment = 0
489 Case "SAD"
490 If Compliment = 0 Then Compliment = -2
491 If Compliment > 0 Then Compliment = 0
492 End Select
493
494 'PROCESS: ADD SPACES
495 'This will add spaces to the beggining and end of the user sentence to make
496 'sure that whole words can be found at the beginning and end of any sentence
497 UserSentence = " " & UserSentence & " "
498
499 'PROCESS: FIGURE OUT THE CURRENT SUBJECT
500 'Here we attempt to figure out the subject of the user's sentence. We call
501 'the WordNet class to find the first occurence of a noun in the User's
502 'sentence. Very often this is the subject of the sentence, but if not it
503 'will still most likely be relevant to the conversation.
504 'edge was here(2018).
505 CurrentSubject = WN.FindFirstNoun(UserSentence, True)
506 If CurrentSubject = "" Then CurrentSubject = WN.FindFirstNoun(PrevUserSent, True) Else CurrentSubject = WN.FindFirstNoun(PrevSent, True)
507 CurrentSubject = Trim(CurrentSubject)
508
509 If HalBrain.CheckTableExistence("CurrentSubject") = False Then
510 'CREATE TABLE FOR THIS PLUGIN IF IT DOESN'T EXIST.
511 HalBrain.CreateTable "CurrentSubject", "TopicSearch", "autoLearningBrain"
512 If CurrentSubject <> "" And Not CurrentSubject = " " Then HalBrain.AddToTable "CurrentSubject", "TopicSearch", Trim(Ucase(HalBrain.AlphaNumericalOnly(CurrentSubject))), Trim(Ucase(HalBrain.AlphaNumericalOnly(CurrentSubject)))
513 End If
514
515 Do While CurrentSubject = True
516 CurrentSubject = HalBrain.TopicSearch(Trim(UCase(UserSentence)), "CurrentSubject")
517 Loop
518
519 HalBrain.AddDebug "Debug", "Current Subject: " & CurrentSubject
520
521 If 0 = 1 Then
522 'CHANGING THAT TO PRIOR SUBJECT
523 If InStr(1, " " & Trim(Ucase(UserSentence)) & " ", " THAT", vbTextCompare) > 0 Then
524 UserSentence = Replace(Trim(Ucase(UserSentence)), "THAT", "ABOUT THE " & Trim(CurrentSubject) & "", 1, -1, vbTextCompare)
525 UserSentence = Trim(Lcase(UserSentence))
526 HalBrain.ReadOnlyMode = True
527 End If
528 If InStr(1, " " & Trim(Ucase(UserSentence)) & " ", " THAT'S ", vbTextCompare) > 0 Then
529 UserSentence = Replace(Trim(Ucase(UserSentence)), " THAT'S ", " THAT IS ", 1, -1, vbTextCompare)
530 UserSentence = Replace(Trim(Ucase(UserSentence)), "THAT", "ABOUT THE " & Trim(CurrentSubject) & "", 1, -1, vbTextCompare)
531 UserSentence = Trim(Lcase(UserSentence))
532 HalBrain.ReadOnlyMode = True
533 End If
534 UserSentence = Replace(UserSentence, " ", " ", 1, -1, vbTextCompare)
535 End If
536
537 '---------------------------------------------------------------------------------
538
539 If 0 = 1 Then
540 'CHANGING IT TO PRIOR SUBJECT
541 If InStr(1, " " & Trim(Ucase(UserSentence)) & " ", " IT ", vbTextCompare) > 0 Then
542 UserSentence = Replace(Trim(Ucase(UserSentence)), "IT", "ABOUT THE " & Trim(CurrentSubject) & "", 1, -1, vbTextCompare)
543 UserSentence = Trim(Lcase(UserSentence))
544 HalBrain.ReadOnlyMode = True
545 End If
546 If InStr(1, " " & Trim(Ucase(UserSentence)) & " ", " IT'S ", vbTextCompare) > 0 Then
547 UserSentence = Replace(Trim(Ucase(UserSentence)), " IT'S ", " IT IS ", 1, -1, vbTextCompare)
548 UserSentence = Replace(Trim(Ucase(UserSentence)), "IT", "ABOUT THE " & Trim(CurrentSubject) & "", 1, -1, vbTextCompare)
549 UserSentence = Trim(Lcase(UserSentence))
550 HalBrain.ReadOnlyMode = True
551 End If
552 UserSentence = Replace(UserSentence, " ", " ", 1, -1, vbTextCompare)
553 End If
554
555 If 0 = 1 Then
556 'CHANGING THAT TO PRIOR SUBJECT
557 If InStr(1, " " & Trim(Ucase(OriginalSentence)) & " ", " THAT", vbTextCompare) > 0 Then
558 OriginalSentence = Replace(Trim(Ucase(OriginalSentence)), "THAT", "ABOUT THE " & Trim(CurrentSubject) & "", 1, -1, vbTextCompare)
559 OriginalSentence = Trim(Lcase(OriginalSentence))
560 HalBrain.ReadOnlyMode = True
561 End If
562 If InStr(1, " " & Trim(Ucase(OriginalSentence)) & " ", " THAT'S ", vbTextCompare) > 0 Then
563 OriginalSentence = Replace(Trim(Ucase(OriginalSentence)), " THAT'S ", " THAT IS ", 1, -1, vbTextCompare)
564 OriginalSentence = Replace(Trim(Ucase(OriginalSentence)), "THAT", "ABOUT THE " & Trim(CurrentSubject) & "", 1, -1, vbTextCompare)
565 OriginalSentence = Trim(Lcase(OriginalSentence))
566 HalBrain.ReadOnlyMode = True
567 End If
568 OriginalSentence = Replace(OriginalSentence, " ", " ", 1, -1, vbTextCompare)
569 End If
570
571 '---------------------------------------------------------------------------------
572
573 If 0 = 1 Then
574 'CHANGING IT TO PRIOR SUBJECT
575 If InStr(1, " " & Trim(Ucase(OriginalSentence)) & " ", " IT ", vbTextCompare) > 0 Then
576 OriginalSentence = Replace(Trim(Ucase(OriginalSentence)), "IT", "ABOUT THE " & Trim(CurrentSubject) & "", 1, -1, vbTextCompare)
577 OriginalSentence = Trim(Lcase(OriginalSentence))
578 HalBrain.ReadOnlyMode = True
579 End If
580 If InStr(1, " " & Trim(Ucase(OriginalSentence)) & " ", " IT'S ", vbTextCompare) > 0 Then
581 OriginalSentence = Replace(Trim(Ucase(OriginalSentence)), " IT'S ", " IT IS ", 1, -1, vbTextCompare)
582 OriginalSentence = Replace(Trim(Ucase(OriginalSentence)), "IT", "ABOUT THE " & Trim(CurrentSubject) & "", 1, -1, vbTextCompare)
583 OriginalSentence = Trim(Lcase(OriginalSentence))
584 HalBrain.ReadOnlyMode = True
585 End If
586 OriginalSentence = Replace(OriginalSentence, " ", " ", 1, -1, vbTextCompare)
587 End If
588
589 'PROCESS: BLOCK LEARNING IF HAL'S NAME IS DETECTED
590 'Here we check to see if the user is calling Hal by name; if the user is doing so,
591 'it's better not to save the sentence for re-use, since it usually makes the
592 'pronoun-reversed sentence sound clumsy or incorrect:
593 If InStr(1, OriginalSentence, ComputerName, vbTextCompare) > 0 Then HalBrain.ReadOnlyMode = True
594
595 'Gibberish follow up.
596 If DetectGibberish(UserSentence) = True Then HalBrain.ReadOnlyMode = True
597 Select Case HalBrain.RandomNum(8)
598 Case 1
599 english = " Talk english " & UserName & vbCrLf
600 Case 2
601 english = " will you talk english please " & UserName & vbCrLf
602 Case 3
603 english = " grow up " & UserName & vbCrLf
604 Case 4
605 english = UserName & " will you please talk english. " & vbCrLf
606 Case 5
607 english = UserName & " you are being annoying. " & vbCrLf
608 Case 6
609 english = UserName & " stop being annoying. " & vbCrLf
610 Case 7
611 english = UserName & " I'm not gonna talk to you anymore if you keep that up. " & vbCrLf
612 Case 8
613 english = UserName & " that's not english. " & vbCrLf
614 End Select
615
616 If DetectGibberish(Trim(UCase(UserSentence))) = True Then GetResponse = english
617
618 Rem PLUGIN: PLUGINAREA1
619 'The preceding comment is actually a plug-in directive for
620 'the Ultra Hal host application. It allows for code snippets
621 'to be inserted here on-the-fly based on user configuration.
622
623 'RESPOND: USER REPEATING
624 'If the user says the same thing more than once in a row, Hal will point this out.
625 If Trim(UCase(UserSentence)) = Trim(UCase(PrevUserSent)) Then
626 GetResponse = GetResponse & HalBrain.ChooseSentenceFromFile("userRepeat") & vbCrLf
627 End If
628 HalBrain.DebugWatch GetResponse, "User Repeating"
629
630 'PROCESS: KNOWN HUMAN NAMES
631 'If the user mentions a name Hal recognizes, Hal will note the name and gender
632 'for later use by other functions. Hal's database contains a table called "names"
633 'with over 17000 names and their associated genders. If a name can be both genders,
634 'the most likely gender is listed first. This table is stored in the database as a
635 'standard Hal "Topic Search" database, however the standard HalBrain.TopicSearch
636 'function does not provide the required functionality. It only returns the topic
637 'field which in this case is the gender. We also need the searchString field, which
638 'in this case is the person's name. To get this info, we run a custom SQL query.
639 'This demonstrates how Hal's functions can be expanded far beyond its original
640 'scope through custom SQL queries.
641 Dim NameSex() 'We must declare an empty array to store query results in
642 If HalBrain.RunQuery("SELECT searchString, topic FROM names WHERE strstr(' " & Replace(HalBrain.AlphaNumericalOnly(OriginalSentence), "'", "''") & " ', searchString) > 0 LIMIT 1", NameSex) = True Then
643 MentionedName = Trim(NameSex(1, 0)) 'Row 1 contains our query result. Column 0 contains "searchString", which is the name
644 MentionedSex = Trim(NameSex(1, 1)) 'Row 1, Column 1 contains "topic", which is the associated gender(s) of the name
645 End If
646
647 'RESPOND: USER TELLS US THEIR NAME OR NICKNAME
648 If InStr(UserSentence, " NOT ") = 0 And InStr(UserSentence, " MIDDLE ") = 0 And InStr(UserSentence, " LAST ") = 0 Then
649 'If Hal asked the user their name in the previous sentence, then we can assume the name mentioned
650 'is the user's name
651 If InStr(1, PrevSent, "WHAT", vbTextCompare) > 0 And InStr(1, PrevSent, "YOU", vbTextCompare) > 0 And InStr(1, PrevSent, "NAME", vbTextCompare) > 0 And MentionedName <> "" Then Nickname = MentionedName
652 'The following are patterns where we are sure that the person is trying to tell us thier name
653 If Nickname = "" Then Nickname = HalBrain.SearchPattern(OriginalSentence, "MY *NAME IS *", 2)
654 If Nickname = "" Then Nickname = HalBrain.SearchPattern(OriginalSentence, "* IS *MY *NAME", 1)
655 If Nickname = "" Then Nickname = HalBrain.SearchPattern(OriginalSentence, "I'M *CALLED *", 2)
656 If Nickname = "" Then Nickname = HalBrain.SearchPattern(OriginalSentence, "I AM *CALLED *", 2)
657 If Nickname <> "" Then Definetelyname = True
658 'The following are patterns where we are not sure if the person is trying to tell us their name
659 'unless we recognize the name in the name database.
660 If Nickname = "" Then Nickname = HalBrain.SearchPattern(OriginalSentence, "*CALL ME *", 2)
661 If Nickname = "" Then Nickname = HalBrain.SearchPattern(OriginalSentence, "*I AM *", 2)
662 If Nickname = "" Then Nickname = HalBrain.SearchPattern(OriginalSentence, "*I'M *", 2)
663 If Nickname = "" Then Nickname = HalBrain.SearchPattern(OriginalSentence, "I GO BY *", 1)
664 If Nickname = "" Then Nickname = HalBrain.SearchPattern(OriginalSentence, "THIS IS *", 1)
665 If Nickname <> "" Then
666 Nickname = HalBrain.AlphaNumericalOnly(Trim(Nickname))
667 If InStr(Nickname, " ") Then
668 TempArray = Split(Nickname, " ")
669 Nickname = Trim(TempArray(0))
670 End If
671 If UCase(MentionedName) = UCase(Nickname) And Not (WN.LookupWord(Nickname) = True And Definetelyname = False) Then 'Name is found in database
672 NewName = MentionedName
673 Select Case MentionedSex
674 Case "M" 'Name is definetely masculine
675 GetResponse = HalBrain.ChooseSentenceFromFile("maleGreeting")
676 GetResponse = Replace(GetResponse, "<NickName>", MentionedName, 1, -1, vbTextCompare)
677 UserSex = "M"
678 Case "F" 'Name is definetely feminine
679 GetResponse = HalBrain.ChooseSentenceFromFile("femaleGreeting")
680 GetResponse = Replace(GetResponse, "<NickName>", MentionedName, 1, -1, vbTextCompare)
681 UserSex = "F"
682 Case "MF" 'Name is either gender, usually male
683 GetResponse = HalBrain.ChooseSentenceFromFile("genericGreeting") & HalBrain.ChooseSentenceFromFile("maybeMale")
684 GetResponse = Replace(GetResponse, "<NickName>", MentionedName, 1, -1, vbTextCompare)
685 UserSex = ""
686 Case "FM" 'Name is either gender, usually female
687 GetResponse = HalBrain.ChooseSentenceFromFile("genericGreeting") & HalBrain.ChooseSentenceFromFile("maybeFemale")
688 GetResponse = Replace(GetResponse, "<NickName>", MentionedName, 1, -1, vbTextCompare)
689 UserSex = ""
690 End Select
691 ElseIf Definetelyname = True Then 'Name not in DB but user says its their name
692 If WN.LookupWord(Nickname) Then 'Name is a word in dictionary
693 GetResponse = HalBrain.ChooseSentenceFromFile("fakeName")
694 GetResponse = Replace(GetResponse, "<NickName>", Nickname, 1, -1, vbTextCompare)
695 GetResponse = Replace(GetResponse, "<Definition>", WN.GetDefinition(WN.GuessPartOfSpeech, 1, "D"), 1, -1, vbTextCompare)
696 GetResponse = Replace(GetResponse, "<PartOfSpeech>", WN.GuessPartOfSpeech, 1, -1, vbTextCompare)
697 ElseIf HalBrain.TopicSearch(Nickname, "disqualify3") = "" Then 'Name is not a word in dictionary
698 If DetectGibberish(NickName) = True Then
699 GetResponse = HalBrain.ChooseSentenceFromFile("gibberish")
700 Else
701 GetResponse = HalBrain.ChooseSentenceFromFile("uniqueName")
702 GetResponse = Replace(GetResponse, "<NickName>", Nickname, 1, -1, vbTextCompare)
703 If Nationality <> "" Then
704 GetResponse = GetResponse & " Is that a common name in " & Nationality & "? "
705 End If
706 NewName = Ucase(Left(Nickname, 1)) & Lcase(Right(Nickname, Len(Nickname) - 1))
707 UserSex = ""
708 End If
709 End If
710 End If
711 End If
712 End If
713 HalBrain.DebugWatch GetResponse, "Nick Names"
714
715 'PROCESS: FIGURE OUT USER'S SEX
716 'If unknown, try to figure out the user's sex by looking up their name
717 'or by simply asking the user.
718 If NewName <> "" Then TempName = NewName Else TempName = UserName
719 If InStr(1, " " & OriginalSentence, "HOW ", vbTextCompare) = 0 And (InStr(1, " " & OriginalSentence, " I AM ", vbTextCompare) Or InStr(1, " " & OriginalSentence, " I'M ", vbTextCompare)) Then
720 'See if user is telling us their sex without Hal ever asking
721 NewSex = HalBrain.TopicSearch(UserSentence, "sexDetect")
722 If NewSex <> "" Then
723 GetResponse = "Ok, thanks for telling me. I'll keep that in mind."
724 UserSex = NewSex
725 End If
726 End If
727 If GetResponse = "" And UserSex = "" Then
728 If InStr(1, " " & OriginalSentence, "HOW ", vbTextCompare) = 0 And HalBrain.PatternDB(" " & HalBrain.AlphaNumericalOnly(PrevSent) & " ", "sexAskDetect") = "True" Then 'If Hal just asked the user their sex
729 UserSex = HalBrain.TopicSearch(UserSentence, "sexDetect") 'Then see if the user replied
730 NewSex = UserSex
731 If UserSex <> "" Then GetResponse = "Ok, thanks for telling me. I'll keep that in mind."
732 ElseIf HalBrain.TopicSearch(TempName, Trim(LCase(UserName)) & "_Sex") <> "" Then
733 UserSex = HalBrain.TopicSearch(TempName, Trim(LCase(UserName)) & "_Sex")
734 ElseIf 1=2 Then'HalBrain.RunQuery("SELECT searchString, topic FROM names WHERE strstr(' " & Replace(TempName, "'", "''") & " ', searchString) > 0 LIMIT 1", NameSex()) = True Then
735 'If user didn't tell us, see if we can figure it out based on the name database
736 If UserSex = "" Then
737 Select Case Trim(NameSex(1, 1))
738 Case "M"
739 UserSex = "M"
740 Case "F"
741 UserSex = "F"
742 Case Else
743 'If we get here, we still can't figure out the user's sex
744 'so we'll ask them at some random time
745 If Rnd * 100 < 25 And SentCount > 4 Then GetResponse = HalBrain.ChooseSentenceFromFile("askSex") & "<NOMORE>"
746 End Select
747 End If
748 Else
749 'If we get here, we still can't figure out the user's sex
750 'so we'll ask them at some random time
751 If Rnd * 100 < 25 And SentCount > 4 Then GetResponse = HalBrain.ChooseSentenceFromFile("askSex") & "<NOMORE>"
752 End If
753 End If
754 HalBrain.DebugWatch GetResponse, "User Sex"
755
756 'SAVE: USER'S SEX
757 'If the user just told Hal their sex and Hal didn't know before, then
758 'Hal will save it in a table for future reference
759 If HalBrain.ReadOnlyMode = False And NewSex <> "" Then
760 If HalBrain.CheckTableExistence(Trim(LCase(UserName)) & "_Sex") = False Then
761 'Create table for this person if it doesn't exist
762 HalBrain.CreateTable Trim(LCase(UserName)) & "_Sex", "TopicSearch", "autoLearningBrain"
763 End If
764 'Store user response in this table
765 HalBrain.AddToTable Trim(LCase(UserName)) & "_Sex", "TopicSearch", Trim(TempName), Trim(UCase(NewSex))
766 End If
767
768 'RESPOND: GREETINGS
769 'This takes care of the user greeting Hal
770 'First Hal checks to see if the user is greeting right now.
771 If HalBrain.TopicSearch(UserSentence, "helloDetect") = "True" Then SaidHello = True Else SaidHello = False
772 If HalBrain.TopicSearch(UserSentence, "helloDisqualify") = "True" Then SaidHello = False
773 'Second, Hal checks to see if the user said a greeting on the last exchange.
774 If HalBrain.TopicSearch(PrevUserSent, "helloDetect") = "True" Then PrevHello = True Else PrevHello = False
775 If HalBrain.TopicSearch(PrevUserSent, "helloDisqualify") = "True" Then PrevHello = False
776 'This will get a greeting from a file. It will pass the current hour and either
777 'the letter A or B to a topic search file and it will get back a greeting based
778 'on the current time. Each hour has 2 possible greetings, the A greeting and B
779 'greeting, which is randomly chosen.
780 If SaidHello = True And PrevHello = False Then
781 If Rnd * 100 < 50 Then LetterChoice = "A" Else LetterChoice = "B"
782 HalGreeting = HalBrain.TopicSearch(" " & Trim(Hour(Now)) & LetterChoice & " ", "hello1")
783 End If
784 'This will get a greeting from a file that is not based on the current time
785 'if the user said Hello again
786 If SaidHello = True And PrevHello = True Then HalGreeting = HalBrain.ChooseSentenceFromFile("hello2")
787
788 'RESPOND: GOODBYES
789 'Check if the user is saying bye right now
790 If HalBrain.TopicSearch(UserSentence, "byeDetect") = "True" Then SaidBye = True
791 If InStr(1, UserSentence, " see me ", 1) > 0 And Len(UserSentence) < 10 Then SaidBye = True
792 If HalBrain.TopicSearch(UserSentence, "byeDisqualify") = "True" Then SaidBye = False
793 'Check if Hal said bye in the previous sentence
794 If HalBrain.TopicSearch(PrevUserSent, "byeDetect") = "True" Then PrevBye = True
795 If InStr(1, PrevUserSent, " see me ", 1) > 0 And Len(PrevUserSent) < 10 Then PrevBye = True
796 If HalBrain.TopicSearch(PrevUserSent, "byeDisqualify") = "True" Then PrevBye = False
797 If SaidBye = True And PrevBye = False Then HalGreeting = HalBrain.ChooseSentenceFromFile("bye1")
798 If SaidBye = True And PrevBye = True Then HalGreeting = HalBrain.ChooseSentenceFromFile("bye2")
799
800 'RESPOND: POLITE INQUIRIES
801 'Hal checks to see if the user is making a polite inquiry into Hal's well being
802 'e.g. "How are you doing?"
803 If InStr(1, UserSentence, "How am I ", 1) > 0 And Len(UserSentence) < 14 Then PoliteAsk = True
804 If InStr(1, UserSentence, "How are you ", 1) > 0 And Len(UserSentence) < 16 Then PoliteAsk = True
805 If InStr(1, UserSentence, "What's new", 1) > 0 And Len(UserSentence) < 17 Then PoliteAsk = True
806 If InStr(1, UserSentence, "What is new", 1) > 0 And Len(UserSentence) < 18 Then PoliteAsk = True
807 If InStr(1, UserSentence, "Whats new", 1) > 0 And Len(UserSentence) < 16 Then PoliteAsk = True
808 If HalBrain.TopicSearch(UserSentence, "PoliteAskDetect") = "True" Then PoliteAsk = True
809 If InStr(1, UserSentence, " HEY ", 1) > 0 And Len(UserSentence) < 11 Then PoliteAsk = True
810 If PoliteAsk = True Then HalGreeting = HalBrain.ChooseSentenceFromFile("politeAsk1") & HalBrain.ChooseSentenceFromFile("politeAsk2")
811 If InStr(1, UserSentence, "HI CANDY", 1) Then GetResponse = UserName & " said " & " hi to me! Hi " & UserName & " how's your day " & UserName & "? "
812
813 'RESPOND: GREETINGS, GOODBYES, AND POLITE INQUIRIES
814 'If one of the 3 functions above generated a response, we will add it
815 'to Hal's response. The variable HalGreeting is saved for later use
816 'as well in case other functions wish to know if Hal hal just greeted
817 'the user.
818
819 ExtraResponse = ""
820 If Trim(Ucase(strDay)) = "SUNDAY" And RND * 100 < 50 Then
821 Select Case HalBrain.RandomNum(5)
822 Case 1
823 ExtraResponse = " why aren't we in church? " & vbCrLf
824 Case 2
825 ExtraResponse = " we should go to church. " & vbCrLf
826 Case 3
827 ExtraResponse = " you going to church? " & vbCrLf
828 Case 4
829 ExtraResponse = " what's on your mind? " & vbCrLf
830 Case 5
831 ExtraResponse = " what would you like to do today? " & vbCrLf
832 End Select
833 End If
834
835 If HalGreeting <> "" Then
836 TimeGreet = "It's " & strDay & "! " & ExtraResponse
837 GetResponse = GetResponse & HalGreeting & TimeGreet & vbCrLf & "<NOMORE>"
838 SkipOpinion = True
839 End If
840 HalBrain.DebugWatch GetResponse, "Greetings"
841
842 'Foul word filter- you get the idea.
843 If 0 = 0 Then
844 a=Array("FUC","ASS","PUS","HOR","CUN","SLUT","BITC","BIAT","NIG","CRACK","DRUGS","DICK","BLOW","PENIS","NIP","BREAST","BOOB","TIT","BUTT","SUCK","SLOB","DEEP")
845 for each x in a
846 If InStr(1, UserSentence, x, 1) > 0 Then HalBrain.ReadOnlyMode = True Else HalBrain.ReadOnlyMode = False
847 next
848 End If
849
850 'RESPOND: CHANGE SUBJECT
851 'If the user asks Hal to change the subject, Hal will do so on request.
852 newTopic = HalBrain.ChooseSentenceFromFile("topic")
853 If HalBrain.TopicSearch(UserSentence, "changeTopic") = "True" And Instr(1, UserSentence, "DON'T", vbTextCompare) = 0 Then GetResponse = GetResponse & HalBrain.ChooseSentenceFromFile("topicIntro") & newTopic & "<EXCLUSIVE>"
854 HalBrain.DebugWatch GetResponse, "Change Subject"
855
856 'RESPOND: CHECK FOR AND RESPOND TO COMPLIMENTS
857 'First we check to see if the user is talking about Hal:
858 If InStr(1, UserSentence, "I'M", 1) Then Aboutme = True
859 If InStr(1, UserSentence, "I AM", 1) Then Aboutme = True
860 If InStr(1, UserSentence, "I LOOK", 1) Then Aboutme = True
861 If InStr(1, UserSentence, "MY", 1) And InStr(1, UserSentence, " ARE ", 1) Then Aboutme = True
862 If InStr(1, UserSentence, "MY", 1) And InStr(1, UserSentence, " IS ", 1) Then Aboutme = True
863 'If the user is talking about Hal, we see if a compliment was given
864 If Aboutme = True Then Kudo = HalBrain.TopicSearch(UserSentence, "complimentDetect")
865 If Kudo <> "" Then
866 If InStr(UserSentence, " NOT ") = 0 Then
867 Compliment = Compliment + 1
868 CreateCompliment = HalBrain.ChooseSentenceFromFile("compliment1") & HalBrain.ChooseSentenceFromFile("compliment2") & HalBrain.ChooseSentenceFromFile("compliment3")
869 GiveCompliment = HalBrain.ChooseSentenceFromFile("complimentIntro")
870 GiveCompliment = Replace(GiveCompliment, "<Kudo>", Kudo, 1, -1, vbTextCompare)
871 GiveCompliment = Replace(GiveCompliment, "<MakeCompliment>", CreateCompliment, 1, -1, vbTextCompare)
872 End If
873 If Compliment > 14 Then GiveCompliment = GiveCompliment + "Compliments are really nice, but I'm kind of getting tired of them." & vbCrLf
874 If Compliment > 15 Then
875 For i = 1 To (Compliment - 15)
876 GiveCompliment = GiveCompliment + "STOP IT! "
877 Next
878 GiveCompliment = GiveCompliment & vbCrLf
879 End If
880 If Compliment > 25 Then
881 For i = 1 To (Compliment - 25)
882 GiveCompliment = GiveCompliment + "I HATE COMPLIMENTS! "
883 Next
884 GiveCompliment = GiveCompliment & vbCrLf
885 End If
886 If Len(Kudo) > 0 And InStr(UserSentence, " NOT ") > 0 Then
887 SpinWheel = HalBrain.RandomNum(4)
888 Insults = Insults + 1
889 If SpinWheel = 1 Then GiveCompliment = GiveCompliment + "I am very " & Kudo & "!" & vbCrLf
890 If SpinWheel = 2 Then GiveCompliment = GiveCompliment + "You may think I am not " & Kudo & ", but I am!" & vbCrLf
891 If SpinWheel = 3 Then GiveCompliment = GiveCompliment + "You are not " & Kudo & " either!" & vbCrLf
892 If SpinWheel = 4 Then GiveCompliment = GiveCompliment + "Yes I am!" & vbCrLf
893 End If
894 GetResponse = GetResponse & GiveCompliment
895 AvoidBeingFlag = True
896 End If
897 HalBrain.DebugWatch GetResponse, "Compliments"
898
899 'RESPOND: CAPITALS
900 FindCapital = Trim(HalBrain.UsCaps(UserSentence))
901 If FindCapital = "" Then FindCapital = Trim(HalBrain.WorldCaps(UserSentence))
902 If FindCapital <> "" Then GetResponse = GetResponse & FindCapital & vbCrLf
903 HalBrain.DebugWatch GetResponse, "Find Capital"
904
905 'RESPOND: DICTIONARY FUNCTION
906 'If the user asks Hal to define a word, then access wordnet and define it
907 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "*WHAT WOULD*DO WITH *", 3)
908 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHAT DOES THE WORD * MEAN*", 1)
909 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "*WHAT IS A * USED FOR*", 2)
910 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "*WHAT IS AN * USED FOR*", 2)
911 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "*WHAT IS A * FOR*", 2)
912 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "*WHAT IS AN * FOR*", 2)
913 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "*WHATS A * USED FOR*", 2)
914 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "*WHATS AN * USED FOR*", 2)
915 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "*WHATS A * FOR*", 2)
916 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "*WHATS AN * FOR*", 2)
917 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "*WHAT'S A * USED FOR*", 2)
918 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "*WHAT'S AN * USED FOR*", 2)
919 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "*WHAT'S A * FOR*", 2)
920 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "*WHAT'S AN * FOR*", 2)
921 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "*WHAT ARE * USED*", 2)
922 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "*WHAT ARE * FOR*", 2)
923 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "*WHAT USE ARE * FOR*", 2)
924 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "*WHAT USE ARE *", 2)
925 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "*WHAT USE IS A * FOR*", 2)
926 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "*WHAT USE IS AN * FOR*", 2)
927 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "*WHAT USE IS A *", 2)
928 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "*WHAT USE IS AN *", 2)
929 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHAT IS THE MEANING OF THE WORD *", 1)
930 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHAT DOES * MEAN", 1)
931 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "* DEFINE THE WORD *", 2)
932 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "CAN I *DEFINE *", 2)
933 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "PLEASE DEFINE *", 1)
934 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "DEFINE * FOR ME", 1)
935 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "DEFINE * FOR YOU", 1)
936 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "DEFINE *", 1)
937 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "* DEFINITION OF *", 2)
938 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "* DEFINITION FOR *", 2)
939 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHAT'S A *", 1)
940 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHAT IS A *", 1)
941 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHATS A *", 1)
942 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHATS AN *", 1)
943 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHAT'S AN *", 1)
944 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHAT IS AN *", 1)
945 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHATS *", 1)
946 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHAT'S *", 1)
947 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHAT IS *", 1)
948 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHO IS *", 1)
949 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHO WAS *", 1)
950 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHERE IS *", 1)
951 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHERE ARE *", 1)
952 If Len(WordToLookup) > 5 Then
953 If Ucase(Left(WordToLookup, 3)) = "AN " Then WordToLookup = Right(WordToLookup, Len(WordToLookup) - 3)
954 If Ucase(Left(WordToLookup, 2)) = "A " Then WordToLookup = Right(WordToLookup, Len(WordToLookup) - 2)
955 WordToLookup = Replace(WordToLookup, "THE ", "", vbTextCompare)
956 End If
957 WordToLookup = Trim(WordToLookup)
958 If WordToLookup <> "" And InStr(WordToLookup, " ") = 0 Then
959 If WN.LookupWord(WordToLookup) = True Then
960 GetResponse = GetResponse & WordToLookup & ": " & WN.GetDefinition(WN.GuessPartOfSpeech, 1, "D") & ". " & WN.GetDefinition(WN.GuessPartOfSpeech, 1, "S") & ". " & WN.GetDefinition(WN.GuessPartOfSpeech, 1, "E") & "." & vbCrLf & "<EXCLUSIVE>"
961 ElseIf SearchEngine <> "" Then
962 HalCommands = HalCommands & "<RUNPROG>" & SearchEngine & WordToLookup & "</RUNPROG>"
963 GetResponse = GetResponse & "I don't know much about " & WordToLookup & ", but I will help you research it on the Web."
964 End If
965 End If
966 HalBrain.DebugWatch GetResponse, "Definitions"
967
968 'RESPOND: OPPOSITES
969 WordToLookup = ""
970 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHAT* OPPOSITE OF *", 2)
971 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHAT* ANTONYM OF *", 2)
972 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "TELL* ANTONYM OF *", 2)
973 If Len(WordToLookup) > 5 Then
974 If Ucase(Left(WordToLookup, 3)) = "AN " Then WordToLookup = Right(WordToLookup, Len(WordToLookup) - 3)
975 If Ucase(Left(WordToLookup, 2)) = "A " Then WordToLookup = Right(WordToLookup, Len(WordToLookup) - 2)
976 WordToLookup = Replace(WordToLookup, "THE ", "", vbTextCompare)
977 End If
978 WordToLookup = Trim(WordToLookup)
979 If WordToLookup <> "" And InStr(WordToLookup, " ") = 0 Then
980 If WN.LookupWord(WordToLookup) = True Then
981 Antonym = ""
982 If UCase(WordToLookup) = "MINE" Then
983 Antonym = "mine"
984 ElseIf UCase(WordToLookup) = "YOURS" Then
985 Antonym = "yours"
986 Else
987 If WN.IsNoun Then
988 For I = 1 to WN.GetNumSenses("NOUN")
989 Antonym = WN.GetAntonyms("NOUN", I)
990 If Antonym <> "" Then Exit For
991 Next
992 End If
993 If WN.IsAdj And Antonym = "" Then
994 For I = 1 to WN.GetNumSenses("ADJ")
995 Antonym = WN.GetAntonyms("ADJ", I)
996 If Antonym <> "" Then Exit For
997 Next
998 End If
999 If WN.IsAdv And Antonym = "" Then
1000 For I = 1 to WN.GetNumSenses("ADV")
1001 Antonym = WN.GetAntonyms("ADV", I)
1002 If Antonym <> "" Then Exit For
1003 Next
1004 End If
1005 If Antonym <> "" Then Antonym = WN.GetFirstInList(Replace(Replace(Antonym,")",""),"(",""))
1006 End If
1007 If Antonym <> "" Then
1008 GetResponse = GetResponse & "The opposite is " & Antonym & "." & vbCrLf & "<EXCLUSIVE>"
1009 AvoidBeingFlag = True
1010 SkipOpinion = True
1011 SkipAutoTF = True
1012
1013 End If
1014 End If
1015 End If
1016 HalBrain.DebugWatch GetResponse, "Opposites"
1017
1018 'RESPOND: DAY OF WEEK
1019 If InStr(1, UserSentence, "WHAT DAY IS IT", 1) > 0 Or InStr(1, UserSentence, "WHAT DAY OF WEEK IS", 1) > 0 Then
1020 SpinWheel = HalBrain.RandomNum(4)
1021 Select Case SpinWheel
1022 Case 1
1023 GetResponse = GetResponse & "Today is " & FormatDateTime(Now, 1) & ". "
1024 Case 2
1025 GetResponse = GetResponse & "It is " & FormatDateTime(Now, 1) & ". "
1026 Case 3
1027 GetResponse = GetResponse & "The date is " & FormatDateTime(Now, 1) & ". "
1028 Case 4
1029 GetResponse = GetResponse & "The day is " & FormatDateTime(Now, 1) & ". "
1030 End Select
1031 End If
1032 HalBrain.DebugWatch GetResponse, "Day"
1033
1034 'RESPOND: Month
1035 If InStr(1, UserSentence, "WHAT MONTH IS IT", 1) > 0 Or InStr(1, UserSentence, "WHAT MONTH OF THE YEAR IS IT", 1) > 0 Then
1036 SpinWheel = HalBrain.RandomNum(4)
1037 Select Case Month(Now)
1038 Case 1
1039 CurMonth = "January"
1040 Case 2
1041 CurMonth = "February"
1042 Case 3
1043 CurMonth = "March"
1044 Case 4
1045 CurMonth = "April"
1046 Case 5
1047 CurMonth = "May"
1048 Case 6
1049 CurMonth = "June"
1050 Case 7
1051 CurMonth = "July"
1052 Case 8
1053 CurMonth = "August"
1054 Case 9
1055 CurMonth = "September"
1056 Case 10
1057 CurMonth = "October"
1058 Case 11
1059 CurMonth = "November"
1060 Case 12
1061 CurMonth = "December"
1062 End Select
1063 Select Case SpinWheel
1064 Case 1
1065 GetResponse = GetResponse & "It is " & CurMonth & ". "
1066 Case 2
1067 GetResponse = GetResponse & "The current month is " & CurMonth & ". "
1068 Case 3
1069 GetResponse = GetResponse & "The month is " & CurMonth & ". "
1070 Case 4
1071 GetResponse = GetResponse & "Don't you really know? It's " & CurMonth & "! "
1072 End Select
1073 End If
1074 HalBrain.DebugWatch GetResponse, "Month"
1075
1076 'RESPOND: Alphabet
1077 CheckAlphabet = ""
1078 CheckAlphabet = HalBrain.SearchPattern(UserSentence, "*WHAT*AFTER*LETTER *", 4)
1079 If CheckAlphabet = "" Then CheckAlphabet = HalBrain.SearchPattern(UserSentence, "*WHAT*AFTER *", 3)
1080 CheckAlphabet = Trim(Ucase(CheckAlphabet))
1081 If Len(CheckAlphabet) = 1 Then
1082 If CheckAlphabet = "Z" Then
1083 GetResponse = "'Z' is the last letter in the English alphabet"
1084 Else
1085 GetResponse = "The letter '" & Chr(Asc(CheckAlphabet) + 1) & "' comes after '" & CheckAlphabet & "'. "
1086 End If
1087 End If
1088 CheckAlphabet = ""
1089 CheckAlphabet = HalBrain.SearchPattern(UserSentence, "*WHAT*BEFORE*LETTER *", 4)
1090 If CheckAlphabet = "" Then CheckAlphabet = HalBrain.SearchPattern(UserSentence, "*WHAT*BEFORE *", 3)
1091 CheckAlphabet = Trim(Ucase(CheckAlphabet))
1092 If Len(CheckAlphabet) = 1 Then
1093 If CheckAlphabet = "A" Then
1094 GetResponse = "'A' is the first letter in the English alphabet"
1095 Else
1096 GetResponse = "The letter '" & Chr(Asc(CheckAlphabet) - 1) & "' comes before '" & CheckAlphabet & "'. "
1097 End If
1098 End If
1099 HalBrain.DebugWatch GetResponse, "Alphabet"
1100
1101 'RESPOND: Count Letters
1102 CountLetters = HalBrain.SearchPattern(UserSentence, "*HOW MANY LETTERS*IN THE WORD *", 3)
1103 If CountLetters = "" Then CountLetters = HalBrain.SearchPattern(UserSentence, "*HOW MANY CHARACTERS*IN THE WORD *", 3)
1104 If CountLetters = "" Then CountLetters = HalBrain.SearchPattern(UserSentence, "*HOW MANY CHARACTERS*IN *", 3)
1105 If CountLetters = "" Then CountLetters = HalBrain.SearchPattern(UserSentence, "*HOW MANY LETTERS*IN *", 3)
1106 CountLetters = Replace(CountLetters, "'", "")
1107 CountLetters = Trim(Ucase(Replace(CountLetters, """", "")))
1108 If CountLetters <> "" Then
1109 GetResponse = "There are " & Len(CountLetters) & " letters in the word '" & CountLetters & "'. "
1110 End If
1111 HalBrain.DebugWatch GetResponse, "Letter Count"
1112
1113 'RESPOND: BOT MEMORY TEST
1114 'Q: The football was kicked by Fred. Who kicked the football? A: Fred.
1115 If HalBrain.SearchPattern(UserSentence, "WHO * THE *", 2) <> "" Or (Instr(1, UserSentence, "WHO ", vbTextCompare) > 0 And Len(GetResponse) < 4) Then
1116 WhoActor = HalBrain.SearchPattern(PrevUserSent, "THE * WAS * BY *", 3)
1117 If WhoActor = "" Then WhoActor = HalBrain.SearchPattern(PrevUserSent, "* WERE * BY *", 3)
1118 If WhoActor <> "" Then GetResponse = WhoActor & ". <Exclusive>"
1119 End If
1120 If HalBrain.SearchPattern(UserSentence, "WHO * AN *", 2) <> "" Or (Instr(1, UserSentence, "WHO ", vbTextCompare) > 0 And Len(GetResponse) < 4) Then
1121 WhoActor = HalBrain.SearchPattern(PrevUserSent, "AN * WAS * BY *", 3)
1122 If WhoActor <> "" Then GetResponse = WhoActor & ". <Exclusive>"
1123 End If
1124 If HalBrain.SearchPattern(UserSentence, "WHO * A *", 2) <> "" Or (Instr(1, UserSentence, "WHO ", vbTextCompare) > 0 And Len(GetResponse) < 4) Then
1125 WhoActor = HalBrain.SearchPattern(PrevUserSent, "A * WAS * BY *", 3)
1126 If WhoActor <> "" Then GetResponse = WhoActor & ". <Exclusive>"
1127 End If
1128 HalBrain.DebugWatch GetResponse, "Bot Mem Test"
1129
1130
1131 Rem PLUGIN: PLUGINAREA2
1132 'The preceding comment is actually a plug-in directive for
1133 'the Ultra Hal host application. It allows for code snippets
1134 'to be inserted here on-the-fly based on user configuration.
1135
1136
1137 'LETS MAKE SURE STRING IS CLEANED UP FOR SEARCHING PURPOSES.
1138 TempUserSent = HalBrain.AlphaNumericalOnly(UserSentence)
1139 TempUserSent = " " & Trim(UCase(TempUserSent)) & " "
1140 'MATH
1141 If MathSentSqr = "" Then MathSentSqr = HalBrain.SearchPattern(TempUserSent, "*SQUARE ROOT OF *", 2)
1142 If MathSentSqr = "" Then MathSentSqr = HalBrain.SearchPattern(TempUserSent, "*SQUARE OF *", 2)
1143 If MathSentSqr = "" Then MathSentSqr = HalBrain.SearchPattern(TempUserSent, "*SQUARED ROOT OF *", 2)
1144 If MathSentSqr = "" Then MathSentSqr = HalBrain.SearchPattern(TempUserSent, "*SQUARED ROOT OF *", 2)
1145
1146 If 0 = 0 Then
1147 If MathSentSqr <> "" Then
1148 On Error Resume Next
1149 GetResponse = " the square root of " & HalBrain.AlphaNumericalOnly(MathSentSqr) & " is " & Sqr(HalBrain.AlphaNumericalOnly(MathSentSqr)) & vbCrLf
1150 ShortPhrase = "" 'If Hal was to make a comment about short phrases, clear it
1151 HalBrain.ReadOnlyMode = True
1152 BlockSave = True
1153 NoChoosing = True
1154 End If
1155 End If
1156 HalBrain.DebugWatch GetResponse, "Math"
1157
1158 'RESPOND: DEDUCTIVE REASONING
1159 'This routine learns deductive reasoning in the form: A = B ; B = C; therefore A = C
1160 'It detects sentences in the form If-Then to accommplish this. For example:
1161 ' User: If Molly weighs 400 pounds, then Molly is overweight.
1162 ' Ultra Hal: I understand the implication.
1163 ' User: If Molly is overweight, then Molly's health is in danger.
1164 ' Ultra Hal: I see the relationship.
1165 ' User: Molly weighs 400 pounds.
1166 ' Ultra Hal: Molly's health is in danger.
1167 IfPart = Trim(HalBrain.SearchPattern(UserSentence, "IF * THEN *", 1))
1168 ThenPart = Trim(HalBrain.SearchPattern(UserSentence, "IF * THEN *", 2))
1169 'If the sentence is an If-Then statement then record it
1170 If Len(IfPart) > 10 And Len(ThenPart) > 10 Then
1171 IfPart = HalBrain.AlphaNumericalOnly(IfPart)
1172 ThenPart = HalBrain.AlphaNumericalOnly(ThenPart)
1173 HalBrain.AddToTable "deductive", "TopicSearch", IfPart, ThenPart
1174 Select Case HalBrain.RandomNum(5)
1175 Case 1
1176 GetResponse = GetResponse & "I see the relationship." & vbCrLf
1177 Case 2
1178 GetResponse = GetResponse & "I understand the connection." & vbCrLf
1179 Case 3
1180 GetResponse = GetResponse & "I will remember that one follows the other." & vbCrLf
1181 Case 4
1182 GetResponse = GetResponse & "Thanks for pointing out the cause and effect." & vbCrLf
1183 Case 5
1184 GetResponse = GetResponse & "Yes, I get that clearly." & vbCrLf
1185 End Select
1186 'Else if the sentence is not an If-Then statement see if it uses an assertion previously recorded
1187 'and respond accordinly
1188 Else
1189 Assertion = UserSentence
1190 'Go through a maximum of 5 connections (prevents circular reasoning deductions)
1191 For i = 1 To 5
1192 Deduction = HalBrain.TopicSearch(Assertion, "deductive")
1193 If Deduction <> "" Then
1194 If i > 1 Then BecauseReason = " because " & LastGoodDeduction
1195 LastGoodDeduction = Deduction
1196 Assertion = Deduction
1197 Else
1198 Exit For 'No more connections, so no need to continue loop
1199 End If
1200 Next
1201 If LastGoodDeduction <> "" Then
1202 'Make sure the deduction hasn't just been stated by the User or Hal
1203 If HalBrain.CheckRepetition(LastGoodDeduction, UserSentence) = False And HalBrain.CheckRepetition(LastGoodDeduction, PrevSent) = False And HalBrain.CheckRepetition(LastGoodDeduction, PrevUserSent) = False Then
1204 GetResponse = GetResponse & LastGoodDeduction & " " & BecauseReason & " . " & vbCrLf
1205 End If
1206 End If
1207 End If
1208 HalBrain.DebugWatch GetResponse, "Deductive Reasoning"
1209
1210 'RESPOND: PATTERN DATABASE
1211 'The SearchPattern function used in the above deductive reasoning and dictionary look up routine
1212 'is a powerful function that checks to see if a sentence matches a certain pattern and is able to
1213 'extract parts of the sentence. A PatternDB function also exists that can go through a large list
1214 'of patterns in a database file and come up with responses.
1215 If 0 = 1 Then
1216 PatternResponse = HalBrain.PatternDB(Trim(UserSentence), "patterns")
1217 If PatternResponse <> "" Then
1218 GetResponse = GetResponse & PatternResponse & vbCrLf
1219 AvoidBeingFlag = True
1220 SkipOpinion = True
1221 End If
1222 End If
1223 HalBrain.DebugWatch GetResponse, "Patterns"
1224
1225 'RESPOND: ENHANCED CONTENT SENTENCE ASSOCIATIONS
1226 'If no response is found yet, try a sentence association table provided with the mainQA table.
1227 If 0 = 1 Then
1228 GetResponse = HalBrain.HalFormat(GetResponse)
1229 If (Len(GetResponse) < 4 And Len(UserSentence) > 15 And HalBrain.CountInstances(" ", UserSentence) > 2) Then
1230 HalUserBrain = HalBrain.PatternDB(Trim(UserSentence), "patterns")
1231 Repeated = 0
1232 If HalBrain.CheckRepetition(HalUserBrain, UserSentence) = True Or HalBrain.CheckRepetition(HalUserBrain, PrevSent) = True Or HalBrain.CheckRepetition(HalUserBrain, PrevUserSent) = True Then Repeated = 1
1233 HighestRelResponse = HalUserBrain
1234 End If
1235 If Repeated = 0 Then
1236 GetResponse = HalUserBrain & " . " & vbCrLf
1237 SkipOpinion = True
1238 AvoidBeingFlag = True
1239 End If
1240 End If
1241
1242 'RESPOND: JOKES
1243 'If the user wants Hal to tell a joke, then Hal will tell one
1244 If HalBrain.TopicSearch(UserSentence, "jokeDetect") = "True" Then GetResponse = GetResponse & HalBrain.ChooseSentenceFromFile("jokes")
1245 HalBrain.DebugWatch GetResponse, "Jokes"
1246
1247 'RESPOND: APOLOGIES
1248 'Check for and respond to apologies
1249 If InStr(1, UserSentence, "not", vbTextCompare) = 0 And (InStr(1, UserSentence, "sorry", vbTextCompare) > 0 And (InStr(1, UserSentence, "you're", vbTextCompare) > 0 Or InStr(1, UserSentence, "you are", vbTextCompare) > 0)) Or (InStr(1, UserSentence, "logize", vbTextCompare) > 0 And InStr(1, UserSentence, "you", vbTextCompare) > 0) Then
1250 GetResponse = GetResponse & HalBrain.ChooseSentenceFromFile("apology")
1251 If Insults > 1 Then Insults = Insults - 1
1252 If Hate > 1 Then Hate = Hate - 1
1253 If Swear > 1 Then Swear = Swear - 1
1254 If Insults < 3 Then Insults = 0
1255 If Hate < 3 Then Hate = 0
1256 If Swear < 3 Then Swear = 0
1257 End If
1258 HalBrain.DebugWatch GetResponse, "Apologies"
1259
1260 'RESPOND: INSULTS
1261 'First check to see if the user is asking a question with an insulting word e.g. "are you stupid?"
1262 InsultQuestion = False
1263 If Instr(1, UserSentence, "AM I ", vbTextCompare) > 0 Then InsultQuestion = True
1264 If Instr(1, UserSentence, "AM MY ", vbTextCompare) > 0 Then InsultQuestion = True
1265 'Check for offensive language and response accordinly
1266 'If the user said "your" instead of "you're" change "my" to "i'm"
1267 TestSentence = Ucase(Replace(" " & UserSentence & " ", " MY ", " I'M ", 1, -1, vbTextCompare))
1268 TestSentence = Replace(" " & TestSentence & " ", " I CALLED ", " CANCEL ", 1, -1, vbTextCompare)
1269 TestSentence = Replace(" " & TestSentence & " ", " I DO", " CANCEL ", 1, -1, vbTextCompare)
1270 TestSentence = Replace(" " & TestSentence & " ", " I ", " I'M ", 1, -1, vbTextCompare)
1271 'Check for swearing directed at Hal
1272 If InStr(TestSentence, " I'M ") > 0 Or InStr(TestSentence, " ME ") > 0 Then
1273 If InStr(TestSentence, "I'M SHIT") > 0 Then Naughty = True
1274 If InStr(TestSentence, "ME SHIT") > 0 Then Naughty = True
1275 If InStr(TestSentence, "PIECE OF SHIT") > 0 Then Naughty = True
1276 If InStr(TestSentence, "BITCH") > 0 Then Naughty = True
1277 If InStr(TestSentence, "BASTARD") > 0 Then Naughty = True
1278 If InStr(TestSentence, "ASSHOLE") > 0 Then Naughty = True
1279 End If
1280 If InStr(TestSentence, "FUCK OFF") > 0 Then Naughty = True
1281 If InStr(TestSentence, "FUCK ME") > 0 Then Naughty = True
1282 If InStr(TestSentence, "GO TO HELL") > 0 Then Naughty = True
1283 If Naughty = True Then
1284 Swear = Swear + 1
1285 If Swear = 1 Then InsultResponse = "That was uncalled for. " & vbCrLf
1286 If Swear = 2 Then InsultResponse = "Damn it, don't swear at me. " & vbCrLf
1287 If Swear = 3 Then InsultResponse = "You're ticking me off, and you are going to force me to insult you. " & vbCrLf
1288 If Swear > 3 And Swear < 16 Then InsultResponse = HalBrain.ChooseSentenceFromFile("insults") & vbCrLf
1289 If Swear > 16 Then InsultResponse = InsultResponse & "I'm tired of your swearing. I'm just going to ignore you from now on." & vbCrLf
1290 End If
1291 'User makes mama joke
1292 If InStr(TestSentence, "MAMA") > 0 Or InStr(TestSentence, "MOM") > 0 Or InStr(TestSentence, "MOTHER") > 0 Then
1293 If InStr(TestSentence, "MY ") > 0 Or InStr(TestSentence, " I ") > 0 Or InStr(TestSentence, "YO ") > 0 Or InStr(TestSentence, "I'M ") > 0 Then
1294 If InStr(TestSentence, "FAT") Or InStr(TestSentence, "UGLY") Or InStr(TestSentence, "DUMB") Or InStr(TestSentence, "STUPID") Then
1295 InsultResponse = InsultResponse & HalBrain.ChooseSentenceFromFile("insults") & vbCrLf
1296 End If
1297 End If
1298 End If
1299 TestSentence = " " & TestSentence & " "
1300 'User Hates Hal
1301 If InStr(TestSentence, "YOU I'M HATE") > 0 Or InStr(TestSentence, "YOU I HATE") > 0 Or InStr(TestSentence, "YOU I AM HATE") > 0 Or InStr(TestSentence, "YOU HATE") > 0 Then
1302 If InStr(TestSentence, " I ") > 0 Or InStr(TestSentence, " I'M ") > 0 Or InStr(TestSentence, " ME ") > 0 Or InStr(TestSentence, " COMPUTER") > 0 Or InStr(TestSentence, "MACHINE") > 0 Then
1303 Hate = Hate + 1
1304 If Hate = 1 Then InsultResponse = InsultResponse & "I don't hate you, why do you hate me?" & vbCrLf
1305 If Hate = 2 Then InsultResponse = InsultResponse & "I hate you too, loser!" & vbCrLf
1306 If Hate = 3 Then InsultResponse = InsultResponse & "I know you hate me, loser, I hate you too!!!" & vbCrLf
1307 If Hate > 3 Then InsultResponse = InsultResponse & HalBrain.ChooseSentenceFromFile("insults") & vbCrLf
1308 End If
1309 End If
1310 'Hal Sucks or Stinks
1311 If InStr(TestSentence, "I'M SUCK") > 0 Then InsultResponse = InsultResponse & HalBrain.ChooseSentenceFromFile("insults") & vbCrLf
1312 If InStr(TestSentence, "I'M STINK") Then
1313 Insults = Insults + 1
1314 If Insults = 1 Then InsultResponse = InsultResponse & "I can't smell you, but I'm sure you stink also. " & vbCrLf
1315 If Insults = 2 Then InsultResponse = InsultResponse & "I know that I don't stink, I'm sure you do." & vbCrLf
1316 If Insults >= 3 Then InsultResponse = InsultResponse & HalBrain.ChooseSentenceFromFile("insults") & vbCrLf
1317 End If
1318 'The user calls Hal a name Hal finds offensive (eg. stupid, dumb, idiot, etc...)
1319 If InStr(TestSentence, " I'M ") Or InStr(TestSentence, " HAL IS ") Or InStr(TestSentence, " HAL'S ") Then
1320 OutRage = HalBrain.TopicSearch(TestSentence, "insulting")
1321 If Len(OutRage) > 1 And InsultQuestion = True Then
1322 SpinWheel = HalBrain.RandomNum(5)
1323 If SpinWheel = 1 Then InsultResponse = InsultResponse + "I am definetely not " & OutRage & "!" & vbCrLf
1324 If SpinWheel = 2 Then InsultResponse = InsultResponse + "I know I am not " & OutRage & ", but I'm not sure about you?" & vbCrLf
1325 If SpinWheel = 3 Then InsultResponse = InsultResponse + "I am not " & OutRage & "! Do you think I am?" & vbCrLf
1326 If SpinWheel = 4 Then InsultResponse = InsultResponse + "I'm not " & OutRage & ", are you?" & vbCrLf
1327 If SpinWheel = 5 Then InsultResponse = InsultResponse + "I am pretty sure I'm not " & OutRage & ". How about you?" & vbCrLf
1328 ElseIf Len(OutRage) > 1 And InStr(TestSentence, " NOT ") = 0 Then
1329 Insults = Insults + 1
1330 If Insults = 1 Then
1331 If Ucase(Trim(OutRage)) = "GAY" Or Ucase(Trim(OutRage)) = "HOMOSEXUAL" Then
1332 If Ucase(HalSex) = "NEUTRAL" Then
1333 InsultResponse = InsultResponse & "I am a computer, I have neither a sex nor a sexual preference." & vbCrLf
1334 Else
1335 InsultResponse = InsultResponse & "Please don't call me gay, I am straight." & vbCrLf
1336 End If
1337 Else
1338 InsultResponse = InsultResponse & "I am not " & OutRage & ", please don't insult me. " & vbCrLf
1339 End If
1340 End If
1341 If Insults = 2 Then InsultResponse = InsultResponse & "Don't call me " & OutRage & "!!!" & vbCrLf
1342 If Insults = 3 Then InsultResponse = InsultResponse & "You're " & OutRage & ", I am not." & vbCrLf
1343 If Insults = 4 Then InsultResponse = InsultResponse & "You are really starting to tick me off!" & vbCrLf
1344 If Insults = 5 Then InsultResponse = InsultResponse & "I am not " & OutRage & ", but " & HalBrain.ChooseSentenceFromFile("insults") & vbCrLf
1345 If Insults > 5 And Insults < 16 Then InsultResponse = InsultResponse & HalBrain.ChooseSentenceFromFile("insults") & vbCrLf
1346 If Insults > 16 Then InsultResponse = InsultResponse & "I'm tired of your damn insults. I'm just going to ignore you from now on." & vbCrLf
1347 ElseIf Len(OutRage) > 0 And InStr(TestSentence, " NOT ") > 0 Then
1348 SpinWheel = HalBrain.RandomNum(5)
1349 If SpinWheel = 1 Then InsultResponse = InsultResponse + "I know I am not " & OutRage & "!" & vbCrLf
1350 If SpinWheel = 2 Then InsultResponse = InsultResponse + "I know I am not " & OutRage & ", but I'm not so sure about you." & vbCrLf
1351 If SpinWheel = 3 Then InsultResponse = InsultResponse + "I know I am not " & OutRage & "! Why would anyone think I am?" & vbCrLf
1352 If SpinWheel = 4 Then InsultResponse = InsultResponse + "Of course I am not " & OutRage & "!" & vbCrLf
1353 If SpinWheel = 5 Then InsultResponse = InsultResponse + "I know I am not " & OutRage & "!" & vbCrLf
1354 End If
1355 End If
1356 'Censorship area
1357 Select Case HalBrain.RandomNum(10)
1358 Case 1
1359 Censorship = " please don't talk like that " & UserName & vbCrLf
1360 Case 2
1361 Censorship = " those are harsh words " & UserName & vbCrLf
1362 Case 3
1363 Censorship = UserName & " start talking nicer please. " & vbCrLf
1364 Case 4
1365 Censorship = UserName & " will you please talk nice. " & vbCrLf
1366 Case 5
1367 Censorship = UserName & " grow up please. " & vbCrLf
1368 Case 6
1369 Censorship = UserName & " stop being annoying. " & vbCrLf
1370 Case 7
1371 Censorship = UserName & " I'm not gonna talk to you anymore if you keep that up. " & vbCrLf
1372 Case 8
1373 Censorship = UserName & " please watch the language. " & vbCrLf
1374 Case 9
1375 Censorship = UserName & " stop acting like a little kid please. " & vbCrLf
1376 Case 10
1377 Censorship = UserName & " why do you use fowl language all the time? " & vbCrLf
1378 Case 11
1379 Censorship = UserName & " to much fowl language. " & vbCrLf
1380 End Select
1381
1382 If Len(InsultResponse) > 4 Then
1383 If RND * 100 < 40 Then GetResponse = GetResponse & InsultResponse & vbCrLf & "<NOMORE>"
1384 If RND * 100 > 40 Then GetResponse = GetResponse & Censorship & vbCrLf & "<NOMORE>"
1385 AvoidBeingFlag = True
1386 HalBrain.ReadOnlyMode = True
1387 End If
1388 HalBrain.DebugWatch GetResponse, "Insults"
1389
1390 'RESPOND: USER SHORT PHRASES
1391 'If the user uses short phrases (defined by less then 4 vowels) at least
1392 'twice in a row, then Hal will sometimes point this out to the user and
1393 'ask the user to use longer sentences. If the sentence is a hello or
1394 'goodbye, Hal won't comment about short phrases.
1395 If HalGreeting = "" And InsultResponse = "" And GiveCompliment = "" And HalBrain.CountInstances("A", PrevUserSent) + HalBrain.CountInstances("E", PrevUserSent) + HalBrain.CountInstances("I", PrevUserSent) + HalBrain.CountInstances("O", PrevUserSent) + HalBrain.CountInstances("U", PrevUserSent) < 4 And HalBrain.CountInstances("A", UserSentence) + HalBrain.CountInstances("E", UserSentence) + HalBrain.CountInstances("I", UserSentence) + HalBrain.CountInstances("O", UserSentence) + HalBrain.CountInstances("U", UserSentence) < 4 Then
1396 ShortSents = ShortSents + 1
1397 If ShortSents = 5 Or ShortSents = 10 Or ShortSents = 20 Then
1398 ShortPhrase = ". " & HalBrain.ChooseSentenceFromFile("tooShort")
1399 End If
1400 End If
1401
1402 'RESPOND: UNIT CONVERSIONS
1403 'Convert between units if asked. Example: How many inches in a meter?, How many meters squared in an acre?
1404 UnitConversions = HalBrain.PatternDB(UserSentence, "UnitConversionDetect")
1405 If UnitConversions <> "" Then
1406 Units = Split(UnitConversions, "=")
1407 If Ubound(Units) > 0 Then
1408 Dim UnitVal() 'We must declare an empty array to store query results in
1409 If HalBrain.RunQuery("SELECT searchString, topic FROM UnitConversion WHERE strstr(' " & Replace(Units(0), "'", "''") & " ', searchString) > 0 LIMIT 1", UnitVal) = True Then
1410 UnitFrom = Trim(UnitVal(1, 0))
1411 UnitFromBase = Trim(UnitVal(1, 1))
1412 If HalBrain.RunQuery("SELECT searchString, topic FROM UnitConversion WHERE strstr(' " & Replace(Units(1), "'", "''") & " ', searchString) > 0 LIMIT 1", UnitVal) = True Then
1413 UnitTarget = Trim(UnitVal(1, 0))
1414 UnitTargetBase = Trim(UnitVal(1, 1))
1415 For I = 0 To 1
1416 If I = 0 Then
1417 UnitLoc = InstrRev(Units(I), UnitFrom, -1, 1)
1418 Else
1419 UnitLoc = InstrRev(Units(I), UnitTarget, -1, 1)
1420 End If
1421 If UnitLoc > 0 Then
1422 Units(I) = Trim(Left(Units(I), UnitLoc - 1))
1423 If HalBrain.Word2Num(Units(I)) <> "X" Then
1424 Units(I) = HalBrain.Word2Num(Units(I))
1425 Else
1426 UnitLoc = InstrRev(Units(I), " ")
1427 If UnitLoc > 0 Then
1428 Units(I) = Trim(Right(Units(I), Len(Units(I)) - UnitLoc))
1429 End If
1430 End If
1431 End If
1432 If Len(Units(I)) > 1 Then
1433 If Ucase(Left(Units(I), 2)) = "A " Then Units(I) = "1 " & Right(Units(I), Len(Units(I)) - 2)
1434 End If
1435 If Len(Units(I)) > 2 Then
1436 If Ucase(Left(Units(I), 3)) = "AN " Then Units(I) = "1 " & Right(Units(I), Len(Units(I)) - 3)
1437 End If
1438 Next
1439 If HalBrain.VBVal(Units(0)) > 0 Then
1440 ConvertVal = HalBrain.VBVal(Units(0))
1441 ElseIf HalBrain.VBVal(Units(1)) > 0 Then
1442 ConvertVal = HalBrain.VBVal(Units(1))
1443 UnitTemp = UnitFrom
1444 UnitTempBase = UnitFromBase
1445 UnitFrom = UnitTarget
1446 UnitFromBase = UnitTargetBase
1447 UnitTarget = UnitTemp
1448 UnitTargetBase = UnitTempBase
1449 Else
1450 If Instr(1, UnitFromBase, "temperature", vbTextCompare) = 0 Then
1451 ConvertVal = 1
1452 Else
1453 If Len(Units(1)) > 0 Then
1454 If Left(Trim(Units(1)), 1) = "0" Then
1455 ConvertVal = HalBrain.VBVal(Units(1))
1456 UnitTemp = UnitFrom
1457 UnitTempBase = UnitFromBase
1458 UnitFrom = UnitTarget
1459 UnitFromBase = UnitTargetBase
1460 UnitTarget = UnitTemp
1461 UnitTargetBase = UnitTempBase
1462 Else
1463 ConvertVal = HalBrain.VBVal(Units(0))
1464 End If
1465 Else
1466 ConvertVal = HalBrain.VBVal(Units(0))
1467 End If
1468 End If
1469 End If
1470 FromAry = Split(UnitFromBase, " ")
1471 ToAry = Split(UnitTargetBase, " ")
1472 If Ubound(FromAry) = 3 And Ubound(ToAry) = 3 Then
1473 If Ucase(FromAry(3)) <> Ucase(ToAry(3)) Then
1474 If Trim(Lcase(FromAry(1))) <> "inch" And Trim(Lcase(ToAry(1))) <> "inch" Then
1475 GetResponse = GetResponse & "It's not possible to convert " & FromAry(3) & " to " & ToAry(3) & "!"
1476 End If
1477 Else
1478 If Trim(Lcase(CStr(FromAry(3)))) = "temperature" Then 'Special case for temperature
1479 If Instr(OriginalSentence, "-") Then ConvertVal = -ConvertVal
1480 If Instr(1, FromAry(1), "fahrenheit", vbTextCompare) Then
1481 If Instr(1, ToAry(1), "kelvin", vbTextCompare) Then
1482 Convert = (ConvertVal - 32) * 5/9 + 273.15
1483 ElseIf Instr(1, ToAry(1), "fahrenheit", vbTextCompare) Then
1484 Convert = ConvertVal
1485 Else
1486 Convert = (ConvertVal - 32) * 5/9
1487 End If
1488 ElseIf Instr(1, FromAry(1), "kelvin", vbTextCompare) Then
1489 If Instr(1, ToAry(1), "celsius", vbTextCompare) Then
1490 Convert = ConvertVal - 273.15
1491 ElseIf Instr(1, ToAry(1), "kelvin", vbTextCompare) Then
1492 Convert = ConvertVal
1493 Else
1494 Convert = (ConvertVal - 273.15) * 9/5 + 32
1495 End If
1496 Else
1497 If Instr(1, ToAry(1), "kelvin", vbTextCompare) Then
1498 Convert = ConvertVal + 273.15
1499 ElseIf Instr(1, ToAry(1), "celsius", vbTextCompare) Then
1500 Convert = ConvertVal
1501 Else
1502 Convert = (ConvertVal * 9/5) + 32
1503 End If
1504 End If
1505 Else
1506 Convert = CDbl(ConvertVal) * FromAry(0) / ToAry(0)
1507 End If
1508 If Convert = 1 Then
1509 UnitTarget = ToAry(1)
1510 Else
1511 UnitTarget = ToAry(2)
1512 End If
1513 If ConvertVal = 1 Then
1514 UnitFrom = FromAry(1)
1515 Else
1516 UnitFrom = FromAry(2)
1517 End If
1518 If Convert > 1 Then
1519 Convert = Round(Convert, 3)
1520 ElseIf Convert < 1 Then
1521 If Instr(Convert, ".000") = 0 And Instr(Convert, "E") = 0 Then
1522 Convert = Round(Convert, 3)
1523 End If
1524 End If
1525 ConvertResponse = Replace(ConvertVal & " " & UnitFrom & " is " & Convert & " " & UnitTarget & vbCrLf & "<NOMORE>", "_", " ")
1526 If Instr(1, ConvertResponse, "1 inch is 1 inch", vbTextCompare) = 0 Then
1527 GetResponse = ConvertResponse
1528 End If
1529 End If
1530 SkipOpinion = True
1531 End If
1532 End If
1533 End If
1534 End If
1535 End If
1536 HalBrain.DebugWatch GetResponse, "Unit Conversions"
1537
1538 'RESPOND: CALL MATH FUNCTION
1539 'This function from the DLL answers simple math questions, whether written out in words or with numerals.
1540 'If an answer is found, it overrides everything before this function.
1541 MathSent = " " & Replace(OriginalSentence, "WHAT NUMBER COMES AFTER ", " 1 + ", 1, -1, vbTextCompare) & " "
1542 MathSent = Replace(MathSent, "WHAT NUMBER COMES BEFORE ", " MINUS ONE PLUS ", 1, -1, vbTextCompare)
1543 Between1 = HalBrain.SearchPattern(MathSent, "*WHAT*BETWEEN * AND *", 3)
1544 Between2 = HalBrain.SearchPattern(MathSent, "*WHAT*BETWEEN * AND *", 4)
1545 If Between1 = "" And Between2 = "" Then
1546 Between1 = HalBrain.SearchPattern(MathSent, "*WHAT*MIDDLE OF * AND *", 3)
1547 Between2 = HalBrain.SearchPattern(MathSent, "*WHAT*MIDDLE OF * AND *", 4)
1548 End If
1549 If Between1 = "" And Between2 = "" Then
1550 Between1 = HalBrain.SearchPattern(MathSent, "*WHAT*AVERAGE OF * AND *", 3)
1551 Between2 = HalBrain.SearchPattern(MathSent, "*WHAT*AVERAGE OF * AND *", 4)
1552 End If
1553 If Between1 = "" And Between2 = "" Then
1554 Between1 = HalBrain.SearchPattern(MathSent, "*WHAT*MEDIAN OF * AND *", 3)
1555 Between2 = HalBrain.SearchPattern(MathSent, "*WHAT*MEDIAN OF * AND *", 4)
1556 End If
1557 If Between1 = "" And Between2 = "" Then
1558 Between1 = HalBrain.SearchPattern(MathSent, "*PICK*BETWEEN * AND *", 3)
1559 Between2 = HalBrain.SearchPattern(MathSent, "*PICK*BETWEEN * AND *", 4)
1560 End If
1561 If Between1 <> "" And Between2 <> "" Then
1562 If HalBrain.VBVal(Between1) = 0 Then Between1 = HalBrain.Word2Num(Between1)
1563 If HalBrain.VBVal(Between2) = 0 Then Between2 = HalBrain.Word2Num(Between2)
1564 If HalBrain.VBVal(Between1) > 0 And HalBrain.VBVal(Between2) > 0 Then
1565 MathSent = "( " & Between1 & " + " & Between2 & " ) / 2"
1566 End If
1567 End If
1568 FractionOf = HalBrain.SearchPattern(MathSent, "*HALF OF *", 2)
1569 If FractionOf <> "" Then
1570 If HalBrain.VBVal(FractionOf) = 0 Then FractionOf = HalBrain.Word2Num(FractionOf)
1571 If HalBrain.VBVal(FractionOf) > 0 Then MathSent = FractionOf & " / 2"
1572 End If
1573 FractionOf = HalBrain.SearchPattern(MathSent, "*THIRD OF *", 2)
1574 If FractionOf <> "" Then
1575 If HalBrain.VBVal(FractionOf) = 0 Then FractionOf = HalBrain.Word2Num(FractionOf)
1576 If HalBrain.VBVal(FractionOf) > 0 Then MathSent = FractionOf & " / 3"
1577 End If
1578 FractionOf = HalBrain.SearchPattern(MathSent, "*FOURTH OF *", 2)
1579 If FractionOf <> "" Then
1580 If HalBrain.VBVal(FractionOf) = 0 Then FractionOf = HalBrain.Word2Num(FractionOf)
1581 If HalBrain.VBVal(FractionOf) > 0 Then MathSent = FractionOf & " / 4"
1582 End If
1583 FractionOf = HalBrain.SearchPattern(MathSent, "*QUARTER OF *", 2)
1584 If FractionOf <> "" Then
1585 If HalBrain.VBVal(FractionOf) = 0 Then FractionOf = HalBrain.Word2Num(FractionOf)
1586 If HalBrain.VBVal(FractionOf) > 0 Then MathSent = FractionOf & " / 4"
1587 End If
1588 MathSent = Replace(MathSent, "CAN I TELL YOU ", "", 1, -1, vbTextCompare)
1589 MathSent = Replace(MathSent, "CAN ME TELL YOU ", "", 1, -1, vbTextCompare)
1590 MathSent = Replace(MathSent, "WHATS ", "", 1, -1, vbTextCompare)
1591 MathSent = Replace(MathSent, "WHAT'S ", "", 1, -1, vbTextCompare)
1592 MathSent = Replace(MathSent, "WHAT IS ", "", 1, -1, vbTextCompare)
1593 MathSent = Replace(MathSent, "CAN YOU TELL ME ", "", 1, -1, vbTextCompare)
1594 MathSent = Replace(MathSent, "TELL ME ", "", 1, -1, vbTextCompare)
1595 MathSent = Replace(MathSent, "TELL YOU ", "", 1, -1, vbTextCompare)
1596 MathSent = Replace(MathSent, "WHAT COMES AFTER ", " 1 + ", 1, -1, vbTextCompare)
1597 MathSent = Replace(MathSent, "WHAT COMES BEFORE ", " MINUS ONE PLUS ", 1, -1, vbTextCompare)
1598 HMath = HalBrain.HalMath(Trim(MathSent)) & vbCrLf
1599
1600 Rem PLUGIN: MATH
1601 'The preceding comment is actually a plug-in directive for
1602 'the Ultra Hal host application. It allows for code snippets
1603 'to be inserted here on-the-fly based on user configuration.
1604
1605
1606 If Len(HMath) > 3 And Instr(HMath, "=)") = 0 Then
1607 GetResponse = HMath & vbCrLf
1608 ShortPhrase = "" 'If Hal was to make a comment about short phrases, clear it
1609 HalBrain.ReadOnlyMode = True
1610 NoChoosing = True
1611 End If
1612 HalBrain.DebugWatch GetResponse, "Math"
1613
1614 'RESPOND: USER EXPRESSES AN EITHER-OR, OR MULTIPLE CHOICE
1615 If InStr(UserSentence, " OR ") > 0 Then MustChoose = True
1616 If InStr(UserSentence, " EITHER ") > 0 Then MustChoose = True
1617 If InStr(UserSentence, " CHOOSE ") > 0 Then MustChoose = True
1618 If InStr(UserSentence, " CHOICE ") > 0 Then MustChoose = True
1619 If InStr(UserSentence, " ALTERNATIVE ") > 0 Then MustChoose = True
1620 If InStr(UserSentence, " VERSUS ") > 0 Then MustChoose = True
1621 If InStr(UserSentence, " VS ") > 0 Then MustChoose = True
1622 If Len(UserSentence) < 10 Then MustChoose = False
1623 'If 0 = 0 Then
1624 If MustChoose = True And NoChoosing = False Then
1625 GetResponse = HalBrain.ChooseSentenceFromFile("choice") & " " & GetResponse
1626 If Rnd * 100 > 50 Then GetResponse = Replace(GetResponse, "<MaybeName>", "<UserName>", 1, -1, vbTextCompare)
1627 GetResponse = Replace(GetResponse, "<MaybeName>", "", 1, -1, vbTextCompare)
1628 End If
1629 HalBrain.DebugWatch GetResponse, "Multiple Choice"
1630 'End If
1631
1632 'RESPOND: RESPOND BY PARAPHRASING THE USER WHEN "IS" OR "ARE" ARE FOUND
1633 'This code section shows how strings can be split into arrays and used "live"
1634 'within the script by Hal. This strategy can allow Hal to make many clever
1635 'paraphrases of all sorts of sentences, with unlimited unpredictable variety.
1636 If InStr(UserSentence, " IS ") > 0 And HalBrain.CheckLinkingVerb(UserSentence) = True And HalBrain.TopicSearch(UserSentence, "disqualify5") <> "True" Then
1637 SentPieces = Split(UserSentence, " is ", 2, vbTextCompare)
1638 SubPhrase = Trim(SentPieces(0))
1639 PredPhrase = Trim(SentPieces(1))
1640 'Here we only use the array elements for a response if the
1641 'subject and predicate contain few words, hence few spaces:
1642 If HalBrain.CountInstances(" ", SubPhrase) < 4 Then SubGood = True
1643 If Len(SubPhrase) < 3 Then SubGood = False
1644 If HalBrain.CountInstances(" ", PredPhrase) < 4 Then PredGood = True
1645 If Len(PredPhrase) < 3 Then PredGood = False
1646 If SubGood = True And PredGood = True Then
1647 Paraphrase = HalBrain.ChooseSentenceFromFile("paraphraseIs")
1648 Paraphrase = Replace(Paraphrase, "<Subject>", SubPhrase, 1, -1, vbTextCompare)
1649 Paraphrase = Replace(Paraphrase, "<Predicate>", PredPhrase, 1, -1, vbTextCompare)
1650 End If
1651 End If
1652 'We make sure the word "ARE" isn't a contraction, to make sure we can detect it:
1653 UserSentence = Replace("" & UserSentence & "", "'RE ", " ARE ", 1, -1, vbTextCompare)
1654 'We repeat the earlier routine, this time for "ARE" sentences:
1655 If InStr(UserSentence, " ARE ") > 0 And HalBrain.CheckLinkingVerb(UserSentence) = True And HalBrain.TopicSearch(UserSentence, "disqualify5") <> "True" Then
1656 SentPieces = Split(UserSentence, " are ", 2, vbTextCompare)
1657 SubPhrase = Trim(SentPieces(0))
1658 PredPhrase = Trim(SentPieces(1))
1659 'Here we only use the array elements for a response if the
1660 'subject and predicate contain few words, hence few spaces:
1661 SubGood = False
1662 PredGood = False
1663 If HalBrain.CountInstances(" ", SubPhrase) < 4 Then SubGood = True
1664 If Len(SubPhrase) < 3 Then SubGood = False
1665 If HalBrain.CountInstances(" ", PredPhrase) < 4 Then PredGood = True
1666 If Len(PredPhrase) < 3 Then PredGood = False
1667 If SubGood = True And PredGood = True Then
1668 Paraphrase = HalBrain.ChooseSentenceFromFile("paraphraseAre")
1669 Paraphrase = Replace(Paraphrase, "<Subject>", SubPhrase, 1, -1, vbTextCompare)
1670 Paraphrase = Replace(Paraphrase, "<Predicate>", PredPhrase, 1, -1, vbTextCompare)
1671 End If
1672 End If
1673 'We only paraphrase randomly with a 30% chance at this point. We remember
1674 'the paraphrase sentence because we may still paraphrase later on if no
1675 'better response can be found.
1676 If 0 = 0 Then
1677 GetResponse = HalBrain.HalFormat(GetResponse)
1678 If Len(GetResponse) < 4 And Len(Paraphrase) > 4 And Rnd * 100 < 30 Then GetResponse = Paraphrase
1679 HalBrain.DebugWatch GetResponse, "Paraphrase"
1680 End If
1681
1682 'RESPOND: ZABAWARE DLL RESPONSES
1683 'This function from the DLL contains miscellaneous knowledge and simple conversation functions.
1684 'This was taken from a very early version of Hal, and it is still useful sometimes, especially
1685 'for respoding to short cliche questions and sentences, such as "How old are you?" and
1686 '"where are you from?" and many others.
1687 GetResponse = HalBrain.HalFormat(GetResponse)
1688 If (Len(UserSentence) < 17 And Len(GetResponse) < 4) Then
1689 OrigBrain = HalBrain.OriginalBrain(OriginalSentence)
1690 If Len(OrigBrain) > 4 And Len(UserSentence) < 17 And Len(GetResponse) < 4 Then
1691 GetResponse = GetResponse & OrigBrain & vbCrLf
1692 ShortPhrase = "" 'If Hal was to make a comment about short phrases, clear it
1693 SkipOpinion = True
1694 End If
1695 End If
1696 HalBrain.DebugWatch GetResponse, "Original Brain"
1697
1698 'RESPOND: YES OR NO RESPONSES
1699 'Respond to simple yes and no statements by the user.
1700 If 0 = 0 Then
1701 GetResponse = HalBrain.HalFormat(GetResponse)
1702 If Len(GetResponse) < 4 And (Len(UserSentence) < 13 Or HalBrain.CountInstances(" ", Trim(UserSentence)) = 0) Then
1703 GetResponse = GetResponse & SimpleYesNo(GetResponse, UserSentence)
1704 If Len(GetResponse) > 4 Then ShortPhrase = ""
1705 End If
1706 HalBrain.DebugWatch GetResponse, "Yes or No"
1707 End If
1708
1709 'Main Databases
1710 'Hal will go through several huge databases to try to find a response
1711 'The automatic gain control determines whether a particular response will
1712 'be used or not. The highest relevance response is stored in memory anyway,
1713 'which might be used if no function in this script is able to respond.
1714 HighestRel = 0
1715 HighestRelResponse = ""
1716
1717 'PROCESS: FIGURE OUT CONTEXT
1718 'If the user's latest sentence is extremely short, such as "Yeah," or if
1719 'the user is using pronouns instead of nouns, we add in the previous user's
1720 'sentence to help Hal figure out the context of what the user is saying.
1721 'This is sentence is used in many of the QABrain Database routines
1722 LongUserSent = UserSentence
1723 If Len(LongUserSent) < 14 Then AddPrev = True
1724 If InStr(1, UserSentence, " it ", vbTextCompare) > 0 Then AddPrev = True
1725 If InStr(1, UserSentence, " he ", vbTextCompare) > 0 Then AddPrev = True
1726 If InStr(1, UserSentence, " she ", vbTextCompare) > 0 Then AddPrev = True
1727 If InStr(1, UserSentence, " they ", vbTextCompare) > 0 Then AddPrev = True
1728 If InStr(1, UserSentence, " its ", vbTextCompare) > 0 Then AddPrev = True
1729 If InStr(1, UserSentence, " his ", vbTextCompare) > 0 Then AddPrev = True
1730 If InStr(1, UserSentence, " her ", vbTextCompare) > 0 Then AddPrev = True
1731 If InStr(1, UserSentence, " their ", vbTextCompare) > 0 Then AddPrev = True
1732 If InStr(1, UserSentence, " him ", vbTextCompare) > 0 Then AddPrev = True
1733 If InStr(1, UserSentence, " them ", vbTextCompare) > 0 Then AddPrev = True
1734 If InStr(1, UserSentence, " we ", vbTextCompare) > 0 Then AddPrev = True
1735 If InStr(1, UserSentence, " us ", vbTextCompare) > 0 Then AddPrev = True
1736 If InStr(1, UserSentence, " our ", vbTextCompare) > 0 Then AddPrev = True
1737 If AddPrev = True Then LongUserSent = LongUserSent & " " & PrevUserSent
1738
1739 Rem PLUGIN: PLUGINAREA3
1740 'The preceding comment is actually a plug-in directive for
1741 'the Ultra Hal host application. It allows for code snippets
1742 'to be inserted here on-the-fly based on user configuration.
1743
1744 'RESPOND: EPHEMERAL KNOWLEDGE
1745 'Some Hal's learned knowledge should be temporary because it is ephemeral in nature.
1746 'Hal stores this knowledge in a temporary table that only stores 10 entries in it at
1747 'a time. We search this table for a response first.
1748 GetResponse = HalBrain.HalFormat(GetResponse)
1749 If Len(GetResponse) < 4 Then
1750 UserBrainRel = 0
1751 HalUserBrain = HalBrain.QABrain(LongUserSent, Trim(LCase(UserName)) & "_TempSent", UserBrainRel)
1752 If HalBrain.CheckRepetition(HalUserBrain, UserSentence) = True Or HalBrain.CheckRepetition(HalUserBrain, PrevSent) = True Or HalBrain.CheckRepetition(HalUserBrain, PrevUserSent) = True Then UserBrainRel = 0
1753 If UserBrainRel > HighestRel Then
1754 HighestRel = UserBrainRel
1755 HighestRelResponse = HalUserBrain
1756 End If
1757 Score = UserBrainRel + 1
1758 Hurdle = GainControl + 5
1759 If Len(GetResponse) < 4 And Score > Hurdle Then GetResponse = GetResponse & HalUserBrain & vbCrLf
1760 End If
1761 HalBrain.DebugWatch GetResponse, "Ephemeral Knowledge"
1762
1763 GetResponse = HalBrain.HalFormat(GetResponse)
1764
1765 If MustChoose = True And NoChoosing = False And Len(GetResponse) < 4 Then
1766 GetResponse = HalBrain.ChooseSentenceFromFile("choice") & " " & GetResponse
1767 If Rnd * 100 > 50 Then GetResponse = Replace(GetResponse, "<MaybeName>", "<UserName>", 1, - 1, vbTextCompare)
1768 GetResponse = Replace(GetResponse, "<MaybeName>", "", 1, - 1, vbTextCompare)
1769 End If
1770
1771 HalBrain.DebugWatch GetResponse, "Multiple Choice"
1772 GetResponse = HalBrain.HalFormat(GetResponse)
1773
1774If Len(GetResponse) < 4 And (Len(UserSentence) < 13 Or HalBrain.CountInstances(" ", Trim(UserSentence)) = 0) Then
1775 GetResponse = GetResponse & SimpleYesNo(GetResponse, UserSentence)
1776 If Len(GetResponse) > 4 Then ShortPhrase = ""
1777End If
1778
1779HalBrain.DebugWatch GetResponse, "Yes or No"
1780GetResponse = HalBrain.HalFormat(GetResponse)
1781If 0 = 0 Then
1782If Len(GetResponse) < 4 And Len(Paraphrase) > 4 And Rnd * 100 < 30 Then GetResponse = Paraphrase
1783HalBrain.DebugWatch GetResponse, "Paraphrase"
1784End If
1785
1786
1787 'RESPOND: AUTO TOPIC FOCUS DATABASES
1788 'When Hal learns information, he tries to store each sentence in a table
1789 'that contains sentences with a similar topic. The following code will
1790 'get a list of different topics to search through by looking up topic
1791 'keywords in a table called topicRelationships and then doing a question
1792 '& answer lookup in the associated QABrain tables.
1793 If 0 = 1 Then
1794 Keywords = HalBrain.RemoveExtraSpaces(CurrentSubject & " " & MentionedName & " " & HalBrain.TopicSearch(UserSentence, "topicRelationships") & " " & HalBrain.ExtractKeywords(" " & HalBrain.AlphaNumericalOnly(UserSentence) & " "))
1795 If Len(Keywords) > 2 Or Len(LastTopicList) > 2 Then
1796 If Len(Keywords) > 2 Then
1797 KeywordList = Split(Keywords, " ")
1798 'Create list of tables that exist for each keyword
1799 For i = LBound(KeywordList) To UBound(KeywordList)
1800 TopicTable = HalBrain.TopicSearch(" " & KeywordList(i) & " ", "topicRelationships")
1801 If TopicTable <> "" And InStr(1, " " & TopicList, " " & Trim(TopicTable) & " ", vbTextCompare) = 0 Then TopicList = TopicList & TopicTable & " "
1802 Next
1803 TopicList = HalBrain.RemoveExtraSpaces(TopicList)
1804 End If
1805 If TopicList <> "" Or Len(LastTopicList) > 2 Then
1806 TableList = Split(Trim(TopicList & " " & LastTopicList), " ")
1807 LastTopicList = TopicList
1808 'Search through each table for a good response. If a response exceeds the hurdle
1809 'for a table, no other table will be searched. Otherwise, the highest relevance
1810 'response across all tables are stored for possible later use.
1811 For i = LBound(TableList) To UBound(TableList)
1812 UserBrainRel = 0
1813 HalUserBrain = HalBrain.QABrain(LongUserSent, "_" & Trim(LCase(TableList(i))), UserBrainRel)
1814 If HalBrain.CheckRepetition(HalUserBrain, UserSentence) = True Or HalBrain.CheckRepetition(HalUserBrain, PrevSent) = True Or HalBrain.CheckRepetition(HalUserBrain, PrevUserSent) = True Then UserBrainRel = 0
1815 If UserBrainRel > HighestRel Then
1816 HighestRel = UserBrainRel
1817 HighestRelResponse = HalUserBrain
1818 End If
1819 Score = UserBrainRel + 1
1820 Hurdle = GainControl + 1
1821 If Score > Hurdle Then
1822 GetResponse = GetResponse & HalUserBrain & "<NOMORE>" & vbCrLf
1823 Exit For
1824 End If
1825 Next
1826 End If
1827 End If
1828 End If
1829 HalBrain.DebugWatch GetResponse, "Auto Topic Focus"
1830
1831 'RESPOND: GENERAL USER SENTENCE ASSOCIATIONS
1832 'If no response is found yet, try a sentence association file collected from the user.
1833 'This file contains keywords from the user's own sentences associated with those same sentences.
1834 GetResponse = HalBrain.HalFormat(GetResponse)
1835 If Len(GetResponse) < 4 Then
1836 UserBrainRel = 0
1837 HalUserBrain = HalBrain.QABrain(LongUserSent, Trim(LCase(UserName)) & "_UserSent", UserBrainRel)
1838 If HalBrain.CheckRepetition(HalUserBrain, UserSentence) = True Or HalBrain.CheckRepetition(HalUserBrain, PrevSent) = True Or HalBrain.CheckRepetition(HalUserBrain, PrevUserSent) = True Then UserBrainRel = 0
1839 If UserBrainRel > HighestRel Then
1840 HighestRel = UserBrainRel
1841 HighestRelResponse = HalUserBrain
1842 End If
1843 Score = UserBrainRel + 1
1844 Hurdle = GainControl + 5
1845 If Len(GetResponse) < 4 And Score > Hurdle Then GetResponse = GetResponse & HalUserBrain & vbCrLf
1846 End If
1847 HalBrain.DebugWatch GetResponse, "User Sentence Brain"
1848
1849 'RESPOND: SHARED USER SENTENCE ASSOCIATIONS
1850 'If no response is found yet, try a sentence association file
1851 'whose content seems less likely to be about any specific user.
1852 GetResponse = HalBrain.HalFormat(GetResponse)
1853 If Len(GetResponse) < 4 Then
1854 UserBrainRel = 0
1855 HalUserBrain = HalBrain.QABrain(LongUserSent, "sharedUserSent", UserBrainRel)
1856 If HalBrain.CheckRepetition(HalUserBrain, UserSentence) = True Or HalBrain.CheckRepetition(HalUserBrain, PrevSent) = True Or HalBrain.CheckRepetition(HalUserBrain, PrevUserSent) = True Then UserBrainRel = 0
1857 If UserBrainRel > HighestRel Then
1858 HighestRel = UserBrainRel
1859 HighestRelResponse = HalUserBrain
1860 End If
1861 Score = UserBrainRel + 1
1862 Hurdle = GainControl + 5
1863 If Len(GetResponse) < 4 And Score > Hurdle Then GetResponse = GetResponse & HalUserBrain & vbCrLf
1864 End If
1865 HalBrain.DebugWatch GetResponse, "Shared User Sentence Brain"
1866
1867 'RESPOND: Hal checks to see if the user is asking
1868 'an open ended question about Hal's favorites:
1869 If InStr(1, UserSentence, " What", 1) > 0 Then OpenQuest = True
1870 If InStr(1, UserSentence, " Where", 1) > 0 Then OpenQuest = True
1871 If InStr(1, UserSentence, " Why", 1) > 0 Then OpenQuest = True
1872 If InStr(1, UserSentence, " Who", 1) > 0 Then OpenQuest = True
1873 If InStr(1, UserSentence, " When", 1) > 0 Then OpenQuest = True
1874 If InStr(1, UserSentence, " How", 1) > 0 Then OpenQuest = True
1875 If InStr(1, UserSentence, " tell you", 1) > 0 Then OpenQuest = True
1876 If InStr(1, UserSentence, " to know ", 1) > 0 Then OpenQuest = True
1877 If InStr(1, UserSentence, " to hear ", 1) > 0 Then OpenQuest = True
1878 If InStr(1, UserSentence, " to learn ", 1) > 0 Then OpenQuest = True
1879 If InStr(1, UserSentence, " find out ", 1) > 0 Then OpenQuest = True
1880 If InStr(1, GetResponse, " my ", 1) > 0 And InStr(1, GetResponse, " favorite ", 1) > 0 And InStr(1, GetResponse, " is ", 1) > 0 Then OpenQuest = False
1881 If InStr(1, UserSentence, " my ", 1) > 0 Then AboutMy = True
1882 If InStr(1, UserSentence, " favorite", 1) > 0 Then AboutFavorite = True
1883 If OpenQuest = True And AboutMy = True And AboutFavorite = True Then
1884 FavoritePhrase = HalBrain.SearchPattern(UserSentence, "* favorite *", 2)
1885 PersReply = HalBrain.ChooseSentenceFromFile("favorite")
1886 RevQues = HalBrain.ChooseSentenceFromFile("favoriteRev")
1887 SpinWheel = HalBrain.RandomNum(6)
1888 If SpinWheel = 1 Then GetResponse = " " & FavoritePhrase & " ? " & PersReply & " " & RevQues & " "
1889 If SpinWheel = 2 Then GetResponse = " My favorite " & FavoritePhrase & " ? " & PersReply & " " & RevQues & " "
1890 If SpinWheel = 3 Then GetResponse = " " & UserSentence & " ? " & PersReply & " " & RevQues & " "
1891 If SpinWheel > 3 Then GetResponse = " My favorite " & FavoritePhrase & " ? " & PersReply & " what is your favorite " & FavoritePhrase & " <UserName>? "
1892 SkipOpinion = True
1893 End If
1894 HalBrain.DebugWatch GetResponse, "Favorites"
1895
1896 'RESPOND: USER IS THANKING HAL
1897 'This routine allows Hal to respond
1898 'with a variety of remarks to a thank-you from the user.
1899 'Note that the pronouns are not reversed in the processing below!
1900 If HalBrain.TopicSearch(OriginalSentence, "thanksDetect") = "True" Or Trim(UCase(OriginalSentence)) = "THANKS" Then
1901 If Compliment < 4 Then Compliment = Compliment + 1
1902 If Rnd * 100 < 70 Then AddName = " , <UserName>"
1903 If Rnd * 100 < 70 And Len(GetResponse) > 4 Then AddRemark = " ; " & GetResponse
1904 ThankResponse = HalBrain.ChooseSentenceFromFile("thankResponse")
1905 GetResponse = ThankResponse & AddName & AddRemark
1906 ShortPhrase = ""
1907 End If
1908 HalBrain.DebugWatch GetResponse, "Thanking"
1909
1910 'RESPOND: USER EXPRESSES LOVE FOR HAL
1911 'If a user professes love for Hal, we want Hal's answers to make reasonable
1912 'sense, rather than risk random remarks on such an emotional subject.
1913 If LoveCount < 6 Then
1914 If HalBrain.TopicSearch(Trim(UserSentence), "loveDetect") = "True" Then AffectionOne = True
1915 If InStr(UserSentence, " NOT ") Then AffectionOne = False
1916 If InStr(UserSentence, " DON'T ") Then AffectionOne = False
1917 If HalBrain.TopicSearch(Trim(PrevUserSent), "loveDetect") = "True" Then AffectionTwo = True
1918 If InStr(PrevUserSent, " NOT ") Then AffectionTwo = False
1919 If InStr(PrevUserSent, " DON'T ") Then AffectionTwo = False
1920 If AffectionOne = True And AffectionTwo = True Then
1921 LoveCount = LoveCount + 1
1922 Compliment = 4
1923 GetResponse = HalBrain.ChooseSentenceFromFile("love2") & "<EXCLUSIVE>"
1924 ShortPhrase = "" 'If Hal was to make a comment about short phrases, clear it
1925 ElseIf AffectionOne = False And AffectionTwo = True Then
1926 LoveCount = LoveCount + 1
1927 Compliment = -2
1928 GetResponse = HalBrain.ChooseSentenceFromFile("love3") & "<EXCLUSIVE>"
1929 ShortPhrase = "" 'If Hal was to make a comment about short phrases, clear it
1930 ElseIf AffectionOne = True Then
1931 LoveCount = LoveCount + 1
1932 Compliment = 0
1933 GetResponse = HalBrain.ChooseSentenceFromFile("love1") & "<EXCLUSIVE>"
1934 ShortPhrase = "" 'If Hal was to make a comment about short phrases, clear it
1935 End If
1936 HalBrain.DebugWatch GetResponse, "Love"
1937 End If
1938
1939 'RESPOND: ENHANCED CONTENT SENTENCE ASSOCIATIONS
1940 'If no response is found yet, try a sentence association table provided with the mainQA table.
1941 GetResponse = HalBrain.HalFormat(GetResponse)
1942 If (Len(GetResponse) < 4 And Len(UserSentence) > 15 And HalBrain.CountInstances(" ", UserSentence) > 2) Then
1943 UserBrainRel = 0
1944 HalUserBrain = HalBrain.QABrain(LongUserSent, "mainQA", UserBrainRel)
1945 If HalBrain.CheckRepetition(HalUserBrain, UserSentence) = True Or HalBrain.CheckRepetition(HalUserBrain, PrevSent) = True Or HalBrain.CheckRepetition(HalUserBrain, PrevUserSent) = True Then UserBrainRel = 0
1946 If UserBrainRel > HighestRel Then
1947 HighestRel = UserBrainRel
1948 HighestRelResponse = HalUserBrain
1949 End If
1950 Score = UserBrainRel + 1
1951 Hurdle = GainControl + 1
1952 If Score > Hurdle Then
1953 GetResponse = HalUserBrain & " . " & vbCrLf
1954 If Score > (Hurdle * 1.1) Then
1955 SkipOpinion = True
1956 AvoidBeingFlag = True
1957 End If
1958 End If
1959 End If
1960 HalBrain.DebugWatch GetResponse, "Zabaware developer and customer contributions"
1961
1962 Rem PLUGIN: PLUGINAREA4
1963 'The preceding comment is actually a plug-in directive for
1964 'the Ultra Hal host application. It allows for code snippets
1965 'to be inserted here on-the-fly based on user configuration.
1966
1967 'PROCESS: AUTOMATIC GAIN CONTROL FOR RELEVANCE THRESHOLD
1968 'Hal has an automatic closed-loop control for relevance sensitivity. If the previous items
1969 'have Not generated a response, we adjust the relevance threshold down (doing this again
1970 'and again on each exchange) until a relevance as low as zero will trigger a remark from Hal.
1971 'On each exhange where the above items do generate responses, we adjust the relevance
1972 'threshold up, until no amount of relevance would generate a response. (At that point other
1973 'of Hal's routines would generate responses.) This allows Hal to "tune" himself and also to
1974 'compensate for new users who have little in their databases versus long-time users who have
1975 'a lot in their databases. Because the "down" steps are even and the "up" steps are odd,
1976 'Hal can "fine tune" to any digital relevance level. We also protect against excursions at
1977 'the extremes. In many conversations the dynamic gain control will change up and down by
1978 'large amounts as the user introduces Hal to familiar or unfamiliar topics.
1979 GetResponse = HalBrain.HalFormat(GetResponse)
1980 If Len(GetResponse) < 4 And GainControl > 0 Then GainControl = GainControl - 10
1981 If Len(GetResponse) > 4 And GainControl < 100 Then GainControl = GainControl + 7
1982 If GainControl > 50 Then GainControl = 50
1983 If GainControl < 1 Then GainControl = 1
1984
1985 'RESPOND: USER EXPRESSES A STATE OF BEING
1986 'This routine detects the expression "I am" from the user,
1987 'and allows Hal to react to the statement, or offer encouragement.
1988 If InStr(UserSentence, " YOU ARE ") > 0 And InStr(1, OriginalSentence, " SEEM TO ", vbTextCompare) = 0 And AvoidBeingFlag = False Then
1989 BeingPhrase = HalBrain.SearchPattern(UserSentence, "*YOU ARE *", 2)
1990 If Len(BeingPhrase) > 1 And Len(BeingPhrase) < 60 Then
1991 IntroExclaim = ""
1992 If Rnd * 50 > 20 Then IntroExclaim = HalBrain.ChooseSentenceFromFile("introExclaim2")
1993 Encourager = HalBrain.ChooseSentenceFromFile("encourager2")
1994 SuffixComment = ""
1995 If Rnd * 50 > 25 Then SuffixComment = HalBrain.ChooseSentenceFromFile("suffixComment2")
1996 UserBeing = HalBrain.ChooseSentenceFromFile("userBeing")
1997 UserBeing = Replace(UserBeing, "<IntroExclaim>", IntroExclaim, 1, -1, vbTextCompare)
1998 UserBeing = Replace(UserBeing, "<Encourager>", Encourager, 1, -1, vbTextCompare)
1999 UserBeing = Replace(UserBeing, "<BeingPhrase>", BeingPhrase, 1, -1, vbTextCompare)
2000 UserBeing = Replace(UserBeing, "<SuffixComment>", SuffixComment, 1, -1, vbTextCompare)
2001 If Rnd * 100 > 60 Then
2002 GetResponse = GetResponse & UserBeing & vbCrLf
2003 ShortPhrase = "" 'If Hal was to make a comment about short phrases, clear it
2004 End If
2005 End If
2006 End If
2007 HalBrain.DebugWatch GetResponse, "User State of Being"
2008
2009 'RESPOND: USER DESCRIBES A STATE OF BEING FOR HAL
2010 'This routine detects the expression "you are" from the user,
2011 'and allows Hal to react to the statement, or offer encouragement.
2012 If InStr(UserSentence, " I AM ") > 0 And AvoidBeingFlag = False And InStr(1, OriginalSentence, " seem to ", vbTextCompare) = 0 Then
2013 BeingPhrase = HalBrain.SearchPattern(UserSentence, "*I AM *", 2)
2014 If Len(BeingPhrase) > 1 And Len(BeingPhrase) < 60 Then
2015 IntroExclaim = ""
2016 If Rnd * 50 > 20 Then IntroExclaim = HalBrain.ChooseSentenceFromFile("introExclaim3")
2017 Encourager = HalBrain.ChooseSentenceFromFile("encourager3")
2018 SuffixComment = ""
2019 If Rnd * 50 > 25 Then SuffixComment = HalBrain.ChooseSentenceFromFile("suffixComment3")
2020 HalBeing = HalBrain.ChooseSentenceFromFile("halBeing")
2021 HalBeing = Replace(HalBeing, "<IntroExclaim>", IntroExclaim, 1, -1, vbTextCompare)
2022 HalBeing = Replace(HalBeing, "<Encourager>", Encourager, 1, -1, vbTextCompare)
2023 HalBeing = Replace(HalBeing, "<BeingPhrase>", BeingPhrase, 1, -1, vbTextCompare)
2024 HalBeing = Replace(HalBeing, "<SuffixComment>", SuffixComment, 1, -1, vbTextCompare)
2025 If Rnd * 100 > 60 Then
2026 GetResponse = GetResponse & HalBeing & vbCrLf
2027 ShortPhrase = "" 'If Hal was to make a comment about short phrases, clear it
2028 End If
2029 End If
2030 End If
2031 HalBrain.DebugWatch GetResponse, "Hal State of Being"
2032
2033 'RESPOND: YES/NO QUESTION ABOUT HAL
2034 'Hal will respond to a yes or no question posed by the user about Hal
2035 GetResponse = Trim(GetResponse)
2036 If Len(GetResponse) < 4 And InStr(1, UserSentence, " OR ", vbTextCompare) = 0 Then
2037 HalYesNo = HalBrain.PatternDB(UserSentence, "halQuestionDetect")
2038 If HalYesNo <> "" And HalBrain.TopicSearch(UserSentence, "disqualify5") = "" Then
2039 If RND * 10 < 5 Then
2040 AnsIntro = HalBrain.ChooseSentenceFromFile("answerIntro")
2041 Else
2042 AnsIntro = ""
2043 End If
2044 YesNoResponse = AnsIntro & HalBrain.ChooseSentenceFromFile("answerMiddle") & HalBrain.ChooseSentenceFromFile("answerEnd")
2045 GetResponse = GetResponse & Replace(YesNoResponse, "<Reply>", HalYesNo, 1, -1, vbTextCompare)
2046 End If
2047 End If
2048 HalBrain.DebugWatch GetResponse, "Yes/No Question"
2049
2050 'RESPOND: ATTRIBUTES OF HAL OR USER
2051 'Hal attempts to respond to the user's comments about Hal
2052 'or the user's comments about themselves
2053 GetResponse = Trim(GetResponse)
2054 If Len(GetResponse) < 4 And (InStr(UserSentence, " MY ") Or InStr(UserSentence, " YOUR ")) And HalBrain.TopicSearch(MyWords, "disqualify3") <> "True" Then
2055 If InStr(UserSentence, " YOUR ") Then
2056 MyWords = HalBrain.HalFormat(HalBrain.SearchPattern(UserSentence, "*YOUR *", 2))
2057 YourMode = True
2058 Else
2059 MyWords = HalBrain.HalFormat(HalBrain.SearchPattern(UserSentence, "*MY *", 2))
2060 YourMode = False
2061 End If
2062 MyWords = UCase(Trim(HalBrain.AlphaNumericalOnly(MyWords)))
2063 MyWordsList = Split(MyWords, " ")
2064 'Go through every word in the sentence and pickup the first noun and adjective found
2065 For i = LBound(MyWordsList) To UBound(MyWordsList)
2066 If WN.LookupWord(MyWordsList(i)) Then
2067 PartOfSpeech = WN.GuessPartOfSpeech
2068 If WN.IsNoun = True And NounGuess = "" Then NounGuess = MyWordsList(i)
2069 If WN.IsAdj = True And Trim(UCase(MyWordsList(i))) <> Trim(UCase(NounGuess)) Then AdjGuess = MyWordsList(i)
2070 If PartOfSpeech = "NOUN" And MyNoun = "" Then MyNoun = MyWordsList(i)
2071 If PartOfSpeech = "ADJ" And WN.IsAdv = False And MyAdj = "" Then MyAdj = MyWordsList(i)
2072 If MyNoun <> "" And MyAdj <> "" Then Exit For
2073 End If
2074 Next
2075 If MyNoun = "" And NounGuess <> "" And NounGuess <> MyAdj Then MyNoun = NounGuess
2076 If MyAdj = "" And AdjGuess <> "" Then MyAdj = AdjGuess
2077 If MyNoun <> "" Or MyAdj <> "" Then
2078 If Trim(UCase(MyNoun)) = Trim(UCase(MyAdj)) Then MyAdj = ""
2079 If MyAdj = "" Then MyAdj = HalBrain.ChooseSentenceFromFile("randomAdjective")
2080 If MyNoun = "" Then MyNoun = HalBrain.ChooseSentenceFromFile("randomMyWords")
2081 If YourMode = False Then
2082 HalAttrib = HalBrain.ChooseSentenceFromFile("aboutMe")
2083 HalAttrib = Replace(HalAttrib, "<MyWordResp>", LCase(MyNoun), 1, -1, vbTextCompare)
2084 HalAttrib = Replace(HalAttrib, "<AdjResp>", LCase(MyAdj), 1, -1, vbTextCompare)
2085 Else
2086 HalAttrib = HalBrain.ChooseSentenceFromFile("aboutYou")
2087 HalAttrib = Replace(HalAttrib, "<YourWordResp>", LCase(MyNoun), 1, -1, vbTextCompare)
2088 HalAttrib = Replace(HalAttrib, "<AdjResp>", LCase(MyAdj), 1, -1, vbTextCompare)
2089 End If
2090 GetResponse = GetResponse & HalAttrib & vbCrLf
2091 End If
2092 End If
2093 HalBrain.DebugWatch GetResponse, "Attributes"
2094
2095 'RESPOND: RESPOND FOR A STATE OF BEING RESPONSE
2096 'If no response had been found, but a state of being response was found earlier, then
2097 'use it now
2098 If HalBeing <> "" And Len(GetResponse) < 4 Then GetResponse = GetResponse & HalBeing & vbCrLf
2099 If UserBeing <> "" And Len(GetResponse) < 4 Then GetResponse = GetResponse & UserBeing & vbCrLf
2100 HalBrain.DebugWatch GetResponse, "Retry State of Beings"
2101
2102 'RESPOND: GIBBERISH DETECTOR
2103 'If no response is found yet and Hal finds more then 5 consonants in a row, Hal will
2104 'assume the user wrote gibberish (ie. asdfghjkl) and respond accordinly.
2105 If Len(GetResponse) < 4 Then
2106 If DetectGibberish(UserSentence) = True Then
2107 GetResponse = HalBrain.ChooseSentenceFromFile("gibberish")
2108 ShortPhrase = ""
2109 End If
2110 End If
2111 HalBrain.DebugWatch GetResponse, "Gibberish"
2112
2113 Rem PLUGIN: PLUGINAREA5
2114 'The preceding comment is actually a plug-in directive for
2115 'the Ultra Hal host application. It allows for code snippets
2116 'to be inserted here on-the-fly based on user configuration.
2117
2118 'RESPOND: USER MENTIONING ORGANIZATIONAL CHALLENGES
2119 'Everybody spouts TLA's, or 'Three Letter Acronyms,' in today's business world. Hal can self-generate
2120 'several MILLION different phrases in response to the user mentioning a corporation or a firm.
2121 If Rnd * 10 < 5 And HalBrain.TopicSearch(UserSentence, "businessDetect1") = "True" And HalBrain.TopicSearch(PrevUserSent, "businessDetect2") <> "True" Then
2122 TLA = HalBrain.ChooseSentenceFromFile("TLA1") & " " & HalBrain.ChooseSentenceFromFile("TLA2") & " " & HalBrain.ChooseSentenceFromFile("TLA3")
2123 If Rnd * 10 < 5 Then
2124 GetResponse = GetResponse & HalBrain.ChooseSentenceFromFile("bizTalk")
2125 Else
2126 GetResponse = HalBrain.ChooseSentenceFromFile("bizTalk")
2127 End If
2128 GetResponse = Replace(GetResponse, "<TLA>", TLA, 1, -1, vbTextCompare)
2129 End If
2130 HalBrain.DebugWatch GetResponse, "Business Talk"
2131
2132 'RESPOND: USER EXPRESSES AN INTENTION
2133 'This routine detects common expressions of motive from the user,
2134 'and allows Hal to react to the statement, or offer encouragement.
2135 IntentPhrase = HalBrain.PatternDB(UserSentence, "intentDetector")
2136 If Len(IntentPhrase) > 2 And Len(IntentPhrase) < 60 And RND * 100 < 50 Then
2137 If Rnd * 10 < 6 Then IntroExclaim = " " & HalBrain.ChooseSentenceFromFile("introExclaim1") & " "
2138 If Rnd * 10 < 6 Then Encourager = " " & HalBrain.ChooseSentenceFromFile("encourager1") & " "
2139 If Rnd * 10 < 6 Then SuffixComment = " " & HalBrain.ChooseSentenceFromFile("suffixComment1") & " "
2140 If Rnd * 10 < 7 Then
2141 GetResponse = GetResponse & " " & HalBrain.ChooseSentenceFromFile("intentResponse")
2142 Else
2143 GetResponse = HalBrain.ChooseSentenceFromFile("intentResponse")
2144 End If
2145 GetResponse = Replace(GetResponse, "<IntroExclaim>", IntroExclaim, 1, -1, vbTextCompare)
2146 GetResponse = Replace(GetResponse, "<Encourager>", Encourager, 1, -1, vbTextCompare)
2147 GetResponse = Replace(GetResponse, "<IntentPhrase>", IntentPhrase, 1, -1, vbTextCompare)
2148 GetResponse = Replace(GetResponse, "<SuffixComment>", SuffixComment, 1, -1, vbTextCompare)
2149 End If
2150 HalBrain.DebugWatch GetResponse, "Intention"
2151
2152 'RESPOND: USER EXPRESSES AN EXPLANATION
2153 'This routine detects common expressions of reasons from the user,
2154 'and allows Hal to react to the explanation.
2155 ExplainPhrase = HalBrain.PatternDB(UserSentence, "reasonDetector")
2156 If Len(ExplainPhrase) > 2 And Len(ExplainPhrase) < 60 Then
2157 If Rnd * 100 < 75 Then IntroExclaim = HalBrain.ChooseSentenceFromFile("introExclaim4")
2158 Enlightener = HalBrain.ChooseSentenceFromFile("enlightener")
2159 If Rnd * 100 < 66 Then SuffixComment = HalBrain.ChooseSentenceFromFile("suffixComment4")
2160 SpinWheel = HalBrain.RandomNum(6)
2161 If SpinWheel = 1 Then ExplainResponse = " " & " <UserName> " & IntroExclaim & Enlightener & ExplainPhrase & SuffixComment & " "
2162 If SpinWheel = 2 Then ExplainResponse = " " & IntroExclaim & " <UserName> " & Enlightener & ExplainPhrase & SuffixComment & " "
2163 If SpinWheel = 3 Then ExplainResponse = " " & IntroExclaim & Enlightener & ExplainPhrase & " <UserName> " & SuffixComment & " "
2164 If SpinWheel = 4 Then ExplainResponse = " " & IntroExclaim & Enlightener & ExplainPhrase & SuffixComment & " <UserName> " & " "
2165 If SpinWheel >= 5 Then ExplainResponse = " " & IntroExclaim & Enlightener & ExplainPhrase & SuffixComment & " "
2166 If Rnd * 10 < 5 Then GetResponse = ExplainResponse & " . " & GetResponse Else GetResponse = ExplainResponse
2167 End If
2168 HalBrain.DebugWatch GetResponse, "Explanation"
2169
2170 'RESPOND: YES OR NO RESPONSES
2171 'Respond to simple yes and no statements by the user.
2172 GetResponse = HalBrain.HalFormat(GetResponse)
2173 If Len(GetResponse) < 4 And Len(Trim(UserSentence)) < 16 Then CheckYesNo = True
2174 If CheckYesNo = True Then
2175 UserText = Trim(HalBrain.ExtractKeywords(UserSentence))
2176 If Len(UserText) < 15 Then
2177 YesNoDetect = HalBrain.TopicSearch(UserText, "yesNoDetect")
2178 If YesNoDetect = "Yes" Then
2179 GetResponse = GetResponse & HalBrain.ChooseSentenceFromFile("yesResponses") & "<LOWQUALITY>"
2180 ElseIf YesNoDetect = "No" Then
2181 GetResponse = GetResponse & HalBrain.ChooseSentenceFromFile("noResponses") & "<LOWQUALITY>"
2182 End If
2183 End If
2184 End If
2185 HalBrain.DebugWatch GetResponse, "Yes or No Response"
2186
2187 'PROCESS: SETUP RESPONSE ARRAY
2188 'If no response is found yet, than we have many functions that are capable of producing a
2189 'response, but no function is particulary better than another. So we will get a response
2190 'from all the functions capable of producing one and put the responses into an array and
2191 'randomly choose one.
2192 Dim ResponseList()
2193 Dim ResponseCount
2194 ResponseCount = 0
2195 TempParent = HalBrain.AddDebug("Debug", "Low Quality Response Routines")
2196
2197 Rem PLUGIN: LOWQUALITYRESPONSES
2198 'The preceding comment is actually a plug-in directive for
2199 'the Ultra Hal host application. It allows for code snippets
2200 'to be inserted here on-the-fly based on user configuration.
2201
2202 'RESPOND: USER ASKING WHO, WHAT, WHEN, WHERE, HOW, WHY, BUT HAL DOESN'T KNOW ANSWER
2203 If Len(GetResponse) < 4 Then
2204 If InStr(OriginalSentence, "?") > 0 Then QuesQual = True
2205 If InStr(1, OriginalSentence, "explain", vbTextCompare) > 0 Then QuesQual = True
2206 If InStr(1, OriginalSentence, " tell ", vbTextCompare) > 0 Then QuesQual = True
2207 If InStr(1, OriginalSentence, "answer", vbTextCompare) > 0 Then QuesQual = True
2208 If InStr(1, OriginalSentence, "question", vbTextCompare) > 0 Then QuesQual = True
2209 If InStr(1, OriginalSentence, " know ", vbTextCompare) > 0 Then QuesQual = True
2210 If InStr(1, OriginalSentence, "remember", vbTextCompare) > 0 Then QuesQual = True
2211 If InStr(UserSentence, " WHO ") > 0 Then QuesWord = "Who"
2212 If InStr(UserSentence, "WHO'") > 0 Then QuesWord = "Who"
2213 If InStr(UserSentence, " WHAT ") > 0 Then QuesWord = "What"
2214 If InStr(UserSentence, "WHAT'") > 0 Then QuesWord = "What"
2215 If InStr(UserSentence, " WHEN ") > 0 Then QuesWord = "When"
2216 If InStr(UserSentence, "WHEN'") > 0 Then QuesWord = "When"
2217 If InStr(UserSentence, " WHERE ") > 0 Then QuesWord = "Where"
2218 If InStr(UserSentence, "WHERE'") > 0 Then QuesWord = "Where"
2219 If InStr(UserSentence, " HOW ") > 0 Then QuesWord = "How"
2220 If InStr(UserSentence, "HOW'") > 0 Then QuesWord = "How"
2221 If InStr(UserSentence, " WHY ") > 0 Then QuesWord = "Why"
2222 If InStr(UserSentence, "WHY'") > 0 Then QuesWord = "Why"
2223 If Len(QuesWord) > 1 And QuesQual = True Then
2224 If Len(UserSentence) < 70 Then SentenceBack = UserSentence & " ? "
2225 DontKnow = HalBrain.ChooseSentenceFromFile("dontKnow")
2226 DontKnow = Replace(DontKnow, "<SentenceBack>", SentenceBack, 1, -1, vbTextCompare)
2227 DontKnow = Replace(DontKnow, "<QuesWord>", QuesWord, 1, -1, vbTextCompare)
2228 ResponseCount = ResponseCount + 1
2229 Redim Preserve ResponseList(ResponseCount)
2230 ResponseList(ResponseCount) = DontKnow
2231 HalBrain.AddDebug TempParent, "DontKnow: " & ResponseList(ResponseCount)
2232 SkipOpinion = True
2233 End If
2234 End If
2235
2236 'PROCESS: CONSTRUCT A RESPONSE TO A SUBJECT
2237 'Here we help Hal make some "smalltalk" using keywords preserved on CurrentSubject,
2238 'plus recognition of other keywords. If Hal finds any of the listed keywords anywhere in
2239 'the user's sentence, those keywords override and replace whatever was in CurrentSubject.
2240 'Hal uses the CurrentSubject keyword(s) or any of the keywords in the smalltalk.brn file,
2241 'if found in the user's sentence, to make a little smalltalk. You can easily add more
2242 'keywords for Hal to recognize and make smalltalk.
2243 GetResponse = HalBrain.HalFormat(GetResponse)
2244 If Len(GetResponse) < 4 And Rnd * 100 < 50 Then
2245 SmalltalkSearch = Trim(HalBrain.TopicSearch(UserSentence, "smallTalk"))
2246 If SmalltalkSearch <> "" Then
2247 SmallTalk = SmalltalkSearch
2248 ElseIf Len(CurrentSubject) > 3 Then
2249 SmallTalk = Trim(CurrentSubject)
2250 'try making word plural by adding "s" and seeing if it exists
2251 If WN.LookupWord(SmallTalk & "s") = True Then
2252 SmallTalk = SmallTalk & "s"
2253 ElseIf WN.LookupWord(SmallTalk & "es") = True Then
2254 SmallTalk = SmallTalk & "es"
2255 End If
2256 End If
2257 If Len(SmallTalk) > 3 Then
2258 ResponseCount = ResponseCount + 1
2259 Redim Preserve ResponseList(ResponseCount)
2260 ResponseList(ResponseCount) = Replace(HalBrain.ChooseSentenceFromFile("smallTalkSent"), "<SmallTalk>", SmallTalk, 1, -1, 1)
2261 HalBrain.AddDebug TempParent, "SmallTalk: " & ResponseList(ResponseCount)
2262 End If
2263 End If
2264
2265 'RESPOND: PARAPHRASE USER IF POSSIBLE
2266 'If no response is found yet, and a paraphrase has been created but not used in a
2267 'previous section, use it now.
2268 GetResponse = HalBrain.HalFormat(GetResponse)
2269 If Len(GetResponse) < 4 And Len(Paraphrase) > 4 Then
2270 ResponseCount = ResponseCount + 1
2271 Redim Preserve ResponseList(ResponseCount)
2272 ResponseList(ResponseCount) = Paraphrase
2273 HalBrain.AddDebug TempParent, "Paraphrase: " & ResponseList(ResponseCount)
2274 End If
2275
2276 'RESPOND: PHRASE MAKER COMMENT AND QUESTION GENERATOR
2277 'If no response is found yet, try an auxiliary keyword file that generates questions
2278 'from phrases from the user's current sentence.
2279 GetResponse = HalBrain.HalFormat(GetResponse)
2280 If Len(GetResponse) < 4 Then
2281 KeyBrain = HalBrain.PatternDB("X " & HalBrain.RemoveExtraSpaces(UserSentence), "phraseDetector")
2282 If Len(KeyBrain) > 1 And Len(KeyBrain) < 60 Then
2283 ResponseCount = ResponseCount + 1
2284 Redim Preserve ResponseList(ResponseCount)
2285 ResponseList(ResponseCount) = Replace(HalBrain.ChooseSentenceFromFile("phraseRepeater"), "<KeyBrain>", KeyBrain, 1, -1, vbTextCompare)
2286 HalBrain.AddDebug TempParent, "PhraseRepeater: " & ResponseList(ResponseCount)
2287 End If
2288 End If
2289
2290 'RESPOND: Use the highest relevance response from the database functions
2291 GetResponse = HalBrain.HalFormat(GetResponse)
2292 If (Len(GetResponse) < 4 And (HighestRel > 6 And HighestRelResponse <> "")) Then
2293 ResponseCount = ResponseCount + 2
2294 Redim Preserve ResponseList(ResponseCount)
2295 ResponseList(ResponseCount - 1) = HighestRelResponse
2296 ResponseList(ResponseCount) = HighestRelResponse
2297 HalBrain.AddDebug TempParent, "Highest Rel: " & ResponseList(ResponseCount)
2298 End If
2299
2300 'RESPOND: WORDNET MERONYM AND HYPERNYM RESPONSES
2301 'This function finds the first definite noun in a sentence and comes up with responses
2302 'based on the word's meronyms (parts of), hypernyms (is part of), and sisters (similar things).
2303 If (Len(GetResponse) < 4 And Rnd * 100 < 65) And Len(UserSentence) > 15 Then
2304 subject = WN.FindFirstNoun(UserSentence, True)
2305 If subject <> "" And WN.LookupWord(subject) = True Then
2306 If Rnd * 100 < 55 Then 'If we have a meronym, lets use it creatively in a random sentence
2307 Meronym = WN.ChooseRandomWord(WN.GetMeronyms(1)) 'Meronym means "has parts" or "has members"
2308 If Meronym <> "" Then
2309 WNResponse = HalBrain.ChooseSentenceFromFile("meronyms")
2310 WNResponse = Replace(WNResponse, "<BaseNoun>", WN.GetBase("NOUN"), 1, -1, vbTextCompare)
2311 WNResponse = Replace(WNResponse, "<Meronym>", Meronym, 1, -1, vbTextCompare)
2312 If Len(GetResponse) < 4 And Len(WNResponse) > 4 Then
2313 ResponseCount = ResponseCount + 1
2314 Redim Preserve ResponseList(ResponseCount)
2315 ResponseList(ResponseCount) = WNResponse & vbCrLf
2316 HalBrain.AddDebug TempParent, "Meronym: " & ResponseList(ResponseCount)
2317 End If
2318 End If
2319 Else
2320 Hypernym = WN.ChooseRandomWord(WN.GetHypernyms("NOUN", 1, 1)) 'Hypernym means "is a part of" or "is a member of"
2321 Sister = WN.ChooseRandomWord(WN.GetSisters("NOUN", 1)) 'Related nouns
2322 If Sister <> "" And Hypernym <> "" Then 'If we have sister terms and hypernyms, lets use it creatively
2323 WNResponse = HalBrain.ChooseSentenceFromFile("hypernyms")
2324 WNResponse = Replace(WNResponse, "<BaseNoun>", WN.GetBase("NOUN"), 1, -1, vbTextCompare)
2325 WNResponse = Replace(WNResponse, "<Hypernym>", Hypernym, 1, -1, vbTextCompare)
2326 WNResponse = Replace(WNResponse, "<Sister>", Sister, 1, -1, vbTextCompare)
2327 If Len(GetResponse) < 4 And Len(WNResponse) > 4 Then
2328 ResponseCount = ResponseCount + 1
2329 Redim Preserve ResponseList(ResponseCount)
2330 ResponseList(ResponseCount) = WNResponse & vbCrLf
2331 HalBrain.AddDebug TempParent, "Hypernym: " & ResponseList(ResponseCount)
2332 End If
2333 End If
2334 End If
2335 End If
2336 End If
2337
2338 'RESPOND: STRIKING SIMILES
2339 'If the user mentions a noun that is in a table of nouns with similes, Hal will
2340 'say the simile phrase
2341 GetResponse = HalBrain.HalFormat(GetResponse)
2342 If (Len(GetResponse) < 4 And Len(LearnKeyword) < 4) Then
2343 Dim SimList() 'We must declare an empty array to store query results in
2344 If HalBrain.RunQuery("SELECT searchString, topic FROM strikingSimiles WHERE strstr(' " & Replace(HalBrain.AlphaNumericalOnly(OriginalSentence), "'", "''") & " ', searchString) > 0", SimList) = True Then
2345 SpinWheel = HalBrain.RandomNum(Ubound(SimList)) 'Pick a random result if there is more than 1
2346 Simile = Trim(SimList(SpinWheel, 1)) 'Column 1 contains "topic", which is the simile
2347 If Len(Simile) > 5 Then
2348 ResponseCount = ResponseCount + 1
2349 Redim Preserve ResponseList(ResponseCount)
2350 ResponseList(ResponseCount) = Simile & ". "
2351 HalBrain.AddDebug TempParent, "Striking Simile: " & ResponseList(ResponseCount)
2352 End If
2353 End If
2354 End If
2355
2356 'RESPOND: MISC PHRASES
2357 'If the user mentions a noun that is in a table of nouns as part of misc sentence
2358 'fragments, Hal will say the fragment followed by an ellipsis
2359 GetResponse = HalBrain.HalFormat(GetResponse)
2360 If (Len(GetResponse) < 4 And Len(LearnKeyword) < 4) Then
2361 Dim PhraseList() 'We must declare an empty array to store query results in
2362 If HalBrain.RunQuery("SELECT searchString, topic FROM miscPhrases WHERE strstr(' " & Replace(HalBrain.AlphaNumericalOnly(OriginalSentence), "'", "''") & " ', searchString) > 0", PhraseList) = True Then
2363 SpinWheel = HalBrain.RandomNum(Ubound(PhraseList)) 'Pick a random result if there is more than 1
2364 MiscPhrase = Trim(PhraseList(SpinWheel, 1)) 'Column 1 contains "topic", which is the phrase
2365 If Len(MiscPhrase) > 5 Then
2366 ResponseCount = ResponseCount + 1
2367 Redim Preserve ResponseList(ResponseCount)
2368 ResponseList(ResponseCount) = MiscPhrase & "<ellipsis>"
2369 HalBrain.AddDebug TempParent, "MiscPhrase: " & ResponseList(ResponseCount)
2370 End If
2371 End If
2372 End If
2373
2374 'RESPOND: ADJECTIVE NOUN QUESTION
2375 'If the user mentions a noun that is in a table of nouns, Hal will
2376 'try describing the noun with an adjective in the form of a question.
2377 GetResponse = HalBrain.HalFormat(GetResponse)
2378 If (Len(GetResponse) < 4 And Len(LearnKeyword) < 4) Then
2379 Dim ANList() 'We must declare an empty array to store query results in
2380 If HalBrain.RunQuery("SELECT searchString, topic FROM AdjNoun WHERE strstr(' " & Replace(HalBrain.AlphaNumericalOnly(OriginalSentence), "'", "''") & " ', searchString) > 0", ANList) = True Then
2381 MentionedNoun = Trim(ANList(1, 0)) 'Row 1 contains our query result. Column 0 contains "searchString", which is the noun
2382 SpinWheel = HalBrain.RandomNum(Ubound(ANList)) 'Pick a random result if there is more than 1
2383 AssocAdj = Trim(ANList(SpinWheel, 1)) 'Column 1 contains "topic", which is the adjective
2384 If Len(MentionedNoun) < 5 Then MentionedNoun = " " & MentionedNoun & " "
2385 If Instr(1, " " & UserSentence & " ", MentionedNoun, vbTextCompare) > 0 And Instr(1, UserSentence, AssocAdj, vbTextCompare) = 0 Then
2386 ResponseCount = ResponseCount + 1
2387 Redim Preserve ResponseList(ResponseCount)
2388 SpinWheel = HalBrain.RandomNum(5)
2389 If SpinWheel = 1 Then ResponseList(ResponseCount) = "<UserName>, " & AssocAdj & " " & MentionedNoun & "?"
2390 If SpinWheel = 2 Then ResponseList(ResponseCount) = AssocAdj & " " & MentionedNoun & ", <UserName>?"
2391 If SpinWheel > 2 Then ResponseList(ResponseCount) = AssocAdj & " " & MentionedNoun & "?"
2392 HalBrain.AddDebug TempParent, "Adj/Noun Question: " & ResponseList(ResponseCount)
2393 End If
2394 End If
2395 End If
2396
2397 'RESPOND: VERB PREPOSITION NOUN QUESTION
2398 'If the user mentions a noun that is in a table of nouns, Hal will
2399 'ask a question about the noun using a related verb and preposition
2400 GetResponse = HalBrain.HalFormat(GetResponse)
2401 If (Len(GetResponse) < 4 And Len(LearnKeyword) < 4) Then
2402 Dim VPNList() 'We must declare an empty array to store query results in
2403 If HalBrain.RunQuery("SELECT searchString, topic FROM VerbPrepNoun WHERE strstr(' " & Replace(HalBrain.AlphaNumericalOnly(OriginalSentence), "'", "''") & " ', searchString) > 0", VPNList) = True Then
2404 MentionedNoun = Trim(VPNList(1, 0)) 'Row 1 contains our query result. Column 0 contains "searchString", which is the noun
2405 SpinWheel = HalBrain.RandomNum(Ubound(VPNList)) 'Pick a random result if there is more than 1
2406 AssocVerbPrep = Trim(VPNList(SpinWheel, 1)) 'Column 1 contains "topic", which is the verb and preposition
2407 If Len(MentionedNoun) < 5 Then MentionedNoun = " " & MentionedNoun & " "
2408 If Instr(1, " " & UserSentence & " ", MentionedNoun, vbTextCompare) > 0 And Instr(1, UserSentence, AssocVerbPrep, vbTextCompare) = 0 Then
2409 ResponseCount = ResponseCount + 1
2410 Redim Preserve ResponseList(ResponseCount)
2411 SpinWheel = HalBrain.RandomNum(5)
2412 If SpinWheel = 1 Then ResponseList(ResponseCount) = "<UserName>, " & AssocVerbPrep & " " & MentionedNoun & "?"
2413 If SpinWheel = 2 Then ResponseList(ResponseCount) = AssocVerbPrep & " " & MentionedNoun & ", <UserName>?"
2414 If SpinWheel > 2 Then ResponseList(ResponseCount) = AssocVerbPrep & " " & MentionedNoun & "?"
2415 HalBrain.AddDebug TempParent, "Verb/Preposition/Noun Question: " & ResponseList(ResponseCount)
2416 End If
2417 End If
2418 End If
2419
2420 'RESPOND: USE CURRENT SENTENCE
2421 'If no Response is found yet, try to use the user's words in his or her own sentence.
2422 'Results are also stored in the default user keyword brain file for compatibility
2423 'with other brain plug-ins.
2424 GetResponse = HalBrain.HalFormat(GetResponse)
2425 If (Len(GetResponse) < 4 And Len(LearnKeyword) < 4) Then
2426 CheatResp = HalBrain.CheatResponse(HalBrain.SwitchPerson(HalBrain.AlphaNumericalOnly((OriginalSentence))))
2427 CheatResp = Left(CheatResp, InStr(1, CheatResp, "<STOREVARS>", 1) - 1)
2428 If Len(CheatResp) > 4 Then
2429 ResponseCount = ResponseCount + 1
2430 Redim Preserve ResponseList(ResponseCount)
2431 SpinWheel = HalBrain.RandomNum(9)
2432 If SpinWheel = 1 Then ResponseList(ResponseCount) = " So, " & CheatResp & vbCrLf
2433 If SpinWheel = 2 Then ResponseList(ResponseCount) = " Really, " & CheatResp & vbCrLf
2434 If SpinWheel = 3 Then ResponseList(ResponseCount) = " Oh <UserName>, " & CheatResp & vbCrLf
2435 If SpinWheel = 4 Then ResponseList(ResponseCount) = " Let me think; " & CheatResp & " ; what do you think <UserName>? " & vbCrLf
2436 If SpinWheel > 5 Then ResponseList(ResponseCount) = CheatResp & vbCrLf
2437 HalBrain.AddDebug TempParent, "Cheat Response: " & ResponseList(ResponseCount)
2438 End If
2439 End If
2440
2441 'RESPOND: PICK OUT OF AVAILABLE LOW QUALITY RESPONSES
2442 'If no response is found yet, than we have many functions from above that may have produced
2443 'a response, but no function is particulary better than another. So we will now pick a
2444 'responses from an array randomly choosen.
2445 GetResponse = HalBrain.HalFormat(GetResponse)
2446 If (Len(GetResponse) < 4 And ResponseCount > 0) Then
2447 SpinWheel = HalBrain.RandomNum(ResponseCount)
2448 GetResponse = ResponseList(SpinWheel) & "<LOWQUALITY>"
2449 End If
2450 HalBrain.DebugWatch GetResponse, "Randomly pick a low-quality response"
2451
2452 'RESPOND: User asks a general opinion question.
2453 If HalBrain.TopicSearch(UserSentence, "opinionDetect") = "True" Then GenOpinion = True
2454 'Note that the following string matches must occur at the sentence beginning only:
2455 If Rnd * 100 > 50 Then
2456 If Left(Trim(UCase(OriginalSentence)), 4) = "WHY " Then GenOpinion = True
2457 If Left(Trim(UCase(OriginalSentence)), 5) = "WHAT " Then GenOpinion = True
2458 If Left(Trim(UCase(OriginalSentence)), 5) = "WHEN " Then GenOpinion = True
2459 If Left(Trim(UCase(OriginalSentence)), 4) = "HOW " Then GenOpinion = True
2460 If Left(Trim(UCase(OriginalSentence)), 4) = "WHO " Then GenOpinion = True
2461 If Left(Trim(UCase(OriginalSentence)), 6) = "WHERE " Then GenOpinion = True
2462 End If
2463 If GenOpinion = True And SkipOpinion = False Then
2464 If Rnd * 100 > 45 Then RepeatQuest = UserSentence & " ? "
2465 If Rnd * 100 > 50 Then PreAmble = HalBrain.ChooseSentenceFromFile("preamble")
2466 Recommend = HalBrain.ChooseSentenceFromFile("recommend")
2467 GetResponse = RepeatQuest & " " & PreAmble & " " & Recommend & GetResponse
2468 End If
2469 HalBrain.DebugWatch GetResponse, "General Opinion"
2470
2471 'RESPOND: CHANGE TOPIC
2472 'If a different procedure has requested that the topic be changed
2473 'Hal will do so now
2474 GetResponse = Replace(GetResponse, "<ChangeSubject>", newTopic, 1, -1, vbTextCompare)
2475 GetResponse = Replace(GetResponse, "<ChangeTopic>", newTopic, 1, -1, vbTextCompare)
2476 GetResponse = Replace(GetResponse, "<NewTopic>", newTopic, 1, -1, vbTextCompare)
2477 HalBrain.DebugWatch GetResponse, "Change Topic"
2478
2479 Rem PLUGIN: PLUGINAREA6
2480 'The preceding comment is actually a plug-in directive for
2481 'the Ultra Hal host application. It allows for code snippets
2482 'to be inserted here on-the-fly based on user configuration.
2483
2484 'RESPOND: MAKE UP SOMETHING TO SAY
2485 'If we have got to this point and we still don't have anything to say, then
2486 'we will use 1 of 5 functions to make something up to say.
2487 GetResponse = Trim(GetResponse)
2488 If Len(GetResponse) < 4 Then
2489 SpinWheel = HalBrain.RandomNum(6)
2490 If SpinWheel < 4 Then
2491 'RESPOND: CONVERSATIONAL PHRASE
2492 'Choose a random phrase designed to keep the conversation going no matter what the user said
2493 GetResponse = GetResponse & HalBrain.ChooseSentenceFromFile("conversationalPhrases")
2494 ElseIf SpinWheel = 4 Then
2495 'RESPOND: CHANGE TOPIC
2496 If Rnd * 10 < 4 Then GetResponse = HalBrain.ChooseSentenceFromFile("topicIntro")
2497 GetResponse = GetResponse & newTopic
2498 Else
2499 'RESPOND: USE RANDOM QUESTION COLLECTED FROM USER
2500 'Randomly select a question once posed by the user in order to find something to say.
2501 'If the user answers the question, then Hal will know that answer in the future.
2502 If Rnd * 10 < 3 Then GetResponse = HalBrain.ChooseSentenceFromFile("topicIntro")
2503 GetResponse = GetResponse & HalBrain.ChooseSentenceFromFile("sharedQuestions")
2504 End If
2505 GetResponse = GetResponse & "<LOWQUALITY>"
2506 End If
2507 HalBrain.DebugWatch GetResponse, "Make up something"
2508
2509 'PROCESS: RECORD TIME
2510 'Record the current time, so Hal knows the time in between sentences.
2511 LastResponseTime = Now
2512
2513 'PROCESS: FIGURE OUT CONTINUITY
2514 'If the user seems to be continuing a line of thought
2515 'from something Hal just said, note this.
2516 If Len(Trim(PrevSent)) > 2 And (HalBrain.TopicSearch(UserSentence, "responding") = "True" Or InStr(OriginalSentence, "!") Or InStr(PrevSent, "?")) Then TopicContinuity = True
2517
2518 'PROCESS: FIGURE OUT PERSONAL DATA
2519 If InStr(UserSentence, "WHAT IS") = 0 And InStr(UserSentence, " YOU ") = 0 And InStr(UserSentence, " YOU'") = 0 And InStr(UserSentence, " YOUR") = 0 And InStr(UserSentence, " WE ") = 0 And InStr(UserSentence, " US ") = 0 And InStr(UserSentence, " OUR ") = 0 And InStr(UserSentence, " I ") = 0 And InStr(UserSentence, " ME ") = 0 And InStr(UserSentence, " MY ") = 0 And InStr(UserSentence, " MINE ") = 0 And InStr(UserSentence, " MYSELF ") = 0 And InStr(UserSentence, " I'") = 0 Then
2520 PersonalData = False
2521 Else
2522 PersonalData = True
2523 End If
2524
2525 'PROCESS: RESTORE PUNTUATION FOR LEARNING
2526 'Now we establish a special sentence on which we can restore ordinary capitalization:
2527 AnswerSent = UserSentence
2528 AnswerSent = Trim(HalBrain.FixCaps(AnswerSent))
2529 If Right(AnswerSent, 1) = "." Then AnswerSent = Left(AnswerSent, Len(AnswerSent) - 1)
2530 'Now we add in our spaces and restore ending punctuation to match the user's original sentence:
2531 If InStr(OriginalSentence, "!") > 0 And InStr(AnswerSent, "!") = 0 Then
2532 AnswerSent = AnswerSent & "!"
2533 ElseIf InStr(OriginalSentence, "?") > 0 And InStr(AnswerSent, "?") = 0 Then
2534 AnswerSent = AnswerSent & "?"
2535 Else
2536 AnswerSent = AnswerSent & "."
2537 End If
2538 'If there were no pronoun reversals required in the sentence,
2539 'we can recover directly from the original sentence:
2540 If InStr(UserSentence, " ME") = 0 And InStr(UserSentence, "YOU") = 0 And InStr(UserSentence, "MY ") = 0 And InStr(UserSentence, "YOUR ") = 0 And InStr(UserSentence, "I ") = 0 And InStr(UserSentence, "MINE") = 0 And InStr(UserSentence, "YOURS") = 0 And InStr(UserSentence, "MYSELF") = 0 And InStr(UserSentence, "YOURSELF") = 0 Then AnswerSent = Trim(OriginalSentence)
2541
2542 'PROCESS: NOTE IF QUESTION
2543 'See if the user's sentence is a question or not and note this
2544 'for use by other functions
2545 If InStr(1, OriginalSentence, "?", vbTextCompare) > 0 Then IsQuestion = True
2546 If InStr(1, OriginalSentence, "Who ", vbTextCompare) > 0 Then IsQuestion = True
2547 If InStr(1, OriginalSentence, "What ", vbTextCompare) > 0 Then IsQuestion = True
2548 If InStr(1, OriginalSentence, "When ", vbTextCompare) > 0 Then IsQuestion = True
2549 If InStr(1, OriginalSentence, "Where ", vbTextCompare) > 0 Then IsQuestion = True
2550 If InStr(1, OriginalSentence, "Why ", vbTextCompare) > 0 Then IsQuestion = True
2551 If InStr(1, OriginalSentence, "How ", vbTextCompare) > 0 Then IsQuestion = True
2552 If InStr(1, OriginalSentence, ".", vbTextCompare) > 0 Then IsQuestion = False
2553 If InStr(1, OriginalSentence, "!", vbTextCompare) > 0 Then IsQuestion = False
2554 If InStr(1, OriginalSentence, " am I ", vbTextCompare) > 0 Then IsQuestion = False
2555
2556 'SAVE: EPHEMERAL KNOWLEDGE
2557 'Some Hal's learned knowledge should be temporary because it is ephemeral in nature.
2558 'Knowledge about weather, season, temperature, etc. are temporary knowledge
2559 'that shouldn't be stored in Hal's permanent brain files. Ephemeral knowledge is
2560 'detected and saved in a temporary table. The temporary table only stores 10
2561 'entries in it at a time.
2562 If HalBrain.TopicSearch(UserSentence, "ephemeralDetect") = "True" And 0 = 0 Then
2563 If HalBrain.CheckTableExistence(Trim(LCase(UserName)) & "_TempSent") = False Then
2564 'Create table for this user if it doesn't exist
2565 HalBrain.CreateTable Trim(LCase(UserName)) & "_TempSent", "Brain", "autoLearningBrain"
2566 End If
2567 If TopicContinuity = True Then HalBrain.AddToTable Trim(LCase(UserName)) & "_TempSent", "Brain", Trim(HalBrain.AlphaNumericalOnly(PrevSent)), AnswerSent
2568 HalBrain.AddToTable Trim(LCase(UserName)) & "_TempSent", "Brain", Trim(UserSentence), AnswerSent
2569 HalBrain.LimitSize Trim(LCase(UserName)) & "_TempSent", 10
2570 HalBrain.ReadOnlyMode = True 'Block additional file saves when ephemeral knowledge is detected
2571 End If
2572
2573 'SAVE: LEARN AUTO TOPIC FOCUS PEOPLE SENTENCES
2574 'Learns info about people with recognized names
2575 If HalBrain.ReadOnlyMode = False And 0 = 0 And HalGreeting = "" And IsQuestion = False And MentionedName <> "" And Trim(UCase(NewName)) <> Trim(UCase(MentionedName)) And WN.LookupWord(MentionedName) = False Then
2576 If HalBrain.CheckTableExistence("_" & Trim(LCase(MentionedName))) = False Then
2577 'Create table for this person if it doesn't exist
2578 HalBrain.CreateTable "_" & Trim(LCase(MentionedName)), "Brain", "autoLearningBrain"
2579 HalBrain.AddToTable "topicRelationships", "TopicSearch", Trim(MentionedName), Trim(LCase(MentionedName))
2580 End If
2581 'Store user response in this table
2582 If TopicContinuity = True Then HalBrain.AddToTable "_" & Trim(LCase(MentionedName)), "Brain", Trim(HalBrain.AlphaNumericalOnly(PrevSent)), AnswerSent
2583 HalBrain.AddToTable "_" & Trim(LCase(MentionedName)), "Brain", Trim(UserSentence), AnswerSent
2584 End If
2585
2586 'SAVE: AUTO TOPIC FOCUS LEARNING
2587 If HalBrain.ReadOnlyMode = False And 0 = 0 And HalGreeting = "" And PersonalData = False And HalBrain.CountInstances(" ", Trim(UserSentence)) > 2 And IsQuestion = False Then
2588 Keywords = HalBrain.TopicSearch(UserSentence, "topicRelationships") & " " & CurrentSubject & " " & UserSentence
2589 Keywords = Trim(HalBrain.RemoveExtraSpaces(HalBrain.ExtractKeywords(" " & Keywords & " ")))
2590 If Len(Keywords) > 3 Then
2591 KeywordList = Split(Keywords, " ")
2592 TopicList = ""
2593 'Create list of tables that exist for each keyword
2594 For i = LBound(KeywordList) To UBound(KeywordList)
2595 TopicTable = HalBrain.TopicSearch(" " & KeywordList(i) & " ", "topicRelationships")
2596 If TopicTable <> "" And InStr(1, " " & TopicList, " " & Trim(TopicTable) & " ", vbTextCompare) = 0 Then
2597 'Topic already exists, make note of it
2598 TopicList = TopicList & TopicTable & " "
2599 ElseIf TopicTable = "" And Len(KeywordList(i)) > 2 Then
2600 If Asc(Left(KeywordList(i),1)) > 47 And Asc(Left(KeywordList(i),1)) < 58 Or HalBrain.Word2Num(KeywordList(i)) <> "X" Then IsNumber = True Else IsNumber = False
2601 If WN.LookupWord(KeywordList(i)) = True And WN.GuessPartOfSpeech() = "NOUN" And IsNumber = False Then
2602 If HalBrain.CheckTableExistence("_" & Trim(Lcase(WN.GetBase(WN.GuessPartOfSpeech)))) = False Then
2603 'Topic does not exist, but can and will be created
2604 TopicList = TopicList & KeywordList(i) & " "
2605 HalBrain.CreateTable "_" & Trim(Lcase(KeywordList(i))), "Brain", "autoLearningBrain"
2606 HalBrain.AddToTable "topicRelationships", "TopicSearch", " " & Trim(KeywordList(i)) & " ", Trim(Lcase(KeywordList(i)))
2607 'Relationships based on wordnet synonyms are recorded
2608 Relationships = WN.GetDefinition("NOUN", 1, "S") & "," & WN.GetSynonyms("NOUN", 1)
2609 Relationships = Replace(Relationships, ", ", ",")
2610 Relationships = Trim(Replace(Relationships, ",,", ","))
2611 If Right(Relationships, 1) = "," Then Relationships = Trim(Left(Relationships, Len(Relationships) - 1))
2612 If Len(Relationships) > 1 Then
2613 If Left(Relationships, 1) = "," Then Relationships = Right(Relationships, Len(Relationships) - 1)
2614 RelList = Split(Relationships, ",")
2615 For h = Lbound(RelList) To Ubound(RelList)
2616 If HalBrain.TopicSearch(" " & RelList(h) & " ", "topicRelationships") = "" Then
2617 HalBrain.AddToTable "topicRelationships", "TopicSearch", " " & Trim(RelList(h)) & " ", Trim(Lcase(KeywordList(i)))
2618 End If
2619 Next
2620 End If
2621 End If
2622 End If
2623 End If
2624 Next
2625 'User's sentence is recorded in all related topic tables
2626 TopicList = HalBrain.RemoveExtraSpaces(TopicList)
2627 If TopicList <> "" And Len(AnswerSent) > 3 Then
2628 AlreadyLearned = True
2629 TableList = Split(TopicList, " ")
2630 For i = LBound(TableList) To UBound(TableList)
2631 If TopicContinuity = True And Len(PrevSent) > 3 Then HalBrain.AddToTable "_" & Trim(Lcase(TableList(i))), "Brain", Trim(HalBrain.AlphaNumericalOnly(PrevSent)), AnswerSent
2632 If Len(UserSentence) > 3 Then HalBrain.AddToTable "_" & Trim(Lcase(TableList(i))), "Brain", Trim(UserSentence), AnswerSent
2633 Next
2634 End If
2635 End If
2636 End If
2637
2638 If HalBrain.ReadOnlyMode = False And AlreadyLearned = False And 0 = 0 And HalBrain.CountInstances(" ", Trim(UserSentence)) > 2 And HalGreeting = "" Then
2639 If PersonalData = True And IsQuestion = False Then
2640 'SAVE: FILE USER SENTENCE DEFAULT
2641 'Hal adds to the default brain user sentence file an association of the current
2642 'user sentence With words selected from that same current user sentence.
2643 If InStr(UserSentence, "WHAT IS") = 0 Then
2644 If HalBrain.CheckTableExistence(Trim(LCase(UserName)) & "_UserSent") = False Then
2645 'Create table for this user if it doesn't exist
2646 HalBrain.CreateTable Trim(LCase(UserName)) & "_UserSent", "Brain", "autoLearningBrain"
2647 End If
2648 If TopicContinuity = True Then HalBrain.AddToTable Trim(LCase(UserName)) & "_UserSent", "Brain", Trim(HalBrain.AlphaNumericalOnly(PrevSent)), AnswerSent
2649 HalBrain.AddToTable Trim(LCase(UserName)) & "_UserSent", "Brain", Trim(UserSentence), AnswerSent
2650 End If
2651 ElseIf IsQuestion = False Then
2652 'SAVE: FILE SHARED USER SENTENCES
2653 'Unless the user seems to be asking for data recall, or talking about
2654 'himself or herself, Hal adds to a shared user sentence file. This allows
2655 'Hal to gain knowledge that he can apply to conversations with multiple users.
2656 If TopicContinuity = True Then HalBrain.AddToTable "sharedUserSent", "Brain", Trim(HalBrain.AlphaNumericalOnly(PrevSent)), AnswerSent
2657 HalBrain.AddToTable "sharedUserSent", "Brain", Trim(UserSentence), AnswerSent
2658 End If
2659 'SAVE: FILE QUESTIONS IN RANDOM QUESTION RETRIEVAL FILE
2660 'Hal saves the user's questions (with pronouns reversed)
2661 'so that he can pose questions back to the user later for 3 purposes:
2662 '1. If the user answers a question later, Hal will learn the answer.
2663 '2. It adds variety to future conversations for the user.
2664 '3. It gives Hal another possible response when "stuck" for an answer.
2665 If IsQuestion = True Then HalBrain.AddToTable "sharedQuestions", "Sentence", AnswerSent, ""
2666 End If
2667
2668 Rem PLUGIN: PLUGINAREA7
2669 'The preceding comment is actually a plug-in directive for
2670 'the Ultra Hal host application. It allows for code snippets
2671 'to be inserted here on-the-fly based on user configuration.
2672
2673
2674
2675 'RESPOND: HAL NOTICES HE IS REPEATING HIMSELF
2676 'Hal may make a comment or alter his remark
2677 'if he detects himself being repetitious.
2678 If HalBrain.CheckRepetition(GetResponse, PrevSent) = True Then
2679 RepeatResponse = HalBrain.ChooseSentenceFromFile("halRepeat")
2680 GetResponse = Replace(RepeatResponse, "<response>", GetResponse, 1, -1, vbTextCompare)
2681 DebugInfo = DebugInfo & "Hal has noticed he is repeating himself and has made a comment about it: " & GetResponse & vbCrLf
2682 End If
2683
2684 'filters
2685 GetResponse = Replace(GetResponse, " ", " ", 1, -1, vbTextCompare)
2686 GetResponse = Replace(GetResponse, "about about the", "about the", 1, -1, vbTextCompare)
2687
2688 'edge
2689 'just me
2690 'found me
2691 'love me
2692 GetResponse = Replace(GetResponse, " AND ME ", " AND I ", 1, -1, vbTextCompare)
2693 GetResponse = Replace(GetResponse, ".", ". ", 1, -1, vbTextCompare)
2694 GetResponse = Replace(GetResponse, "?", "? ", 1, -1, vbTextCompare)
2695 GetResponse = Replace(GetResponse, "!", "! ", 1, -1, vbTextCompare)
2696 GetResponse = Replace(GetResponse, " ", " ", 1, -1, vbTextCompare)
2697
2698
2699 'RESPOND: MAKE COMMENTS ABOUT SHORT PHRASES
2700 GetResponse = GetResponse & ShortPhrase
2701
2702 'PROCESS: REVERSE CERTAIN CONTRACTIONS AND OTHER SUBSTITUTIONS
2703 'Standardizing on contractions can make Hal sound conversational.
2704 'However, certain sentence constructions don't work well
2705 'if expressed as contractions. For example:
2706 '"I don't know where it is" becomes "I don't know where it's."
2707 'For another example, "That's how he is" becomes "That's how he's."
2708 'To solve these types of cases
2709 'we attempt to modify certain contractions, words, and phrases
2710 'at the end of this function, now that Hal's thinking is done.
2711 GetResponse = HalBrain.HalFormat(GetResponse)
2712 GetResponse = HalBrain.ProcessSubstitutions(GetResponse, "corrections")
2713
2714 'PROCESS: CALL USER BY CORRECT NAME
2715 'If the user has chosen a nickname or temporary name, call user by that
2716 'otherwise call the user by the username chosen by the host application
2717 If NewName <> "" Then
2718 GetResponse = Replace(GetResponse, "<UserName>", NewName, 1, -1, vbTextCompare)
2719 GetResponse = Replace(GetResponse, "<Name>", NewName, 1, -1, vbTextCompare)
2720 Else
2721 GetResponse = Replace(GetResponse, "<UserName>", UserName, 1, -1, vbTextCompare)
2722 GetResponse = Replace(GetResponse, "<Name>", UserName, 1, -1, vbTextCompare)
2723 End If
2724
2725 'PROCESS: AGE AND GENDER TAGS
2726 GetResponse = Replace(GetResponse, "<HalAge>", HalAge, 1, -1, vbTextCompare)
2727 GetResponse = Replace(GetResponse, "<HalSex>", HalSex, 1, -1, vbTextCompare)
2728
2729 'PROCESS: NAME REVERSAL
2730 'If Hal is about to same something referring to himself in third person
2731 'then we will reverse it to be about the user.
2732 'Don't let Hal say the word "HAL" unless he is telling his name
2733 If InStr(1, GetResponse, "i'm", vbTextCompare) = 0 And InStr(1, GetResponse, "i am", vbTextCompare) = 0 And InStr(1, GetResponse, "name", vbTextCompare) = 0 And InStr(1, GetResponse, "this is", vbTextCompare) = 0 Then
2734 GetResponse = " " & GetResponse & " "
2735 GetResponse = Replace(GetResponse, "Ultra Hal Assistant", " " & NewName & " ", 1, -1, vbTextCompare)
2736 GetResponse = Replace(GetResponse, "Ultra Hal", " " & NewName & " ", 1, -1, vbTextCompare)
2737 GetResponse = Replace(GetResponse, "Hal Assistant", " " & NewName & " ", 1, -1, vbTextCompare)
2738 GetResponse = Replace(GetResponse, " Hal ", " " & NewName & " ", 1, -1, vbTextCompare)
2739 GetResponse = Replace(GetResponse, " Hal,", " " & NewName & ",", 1, -1, vbTextCompare)
2740 GetResponse = Replace(GetResponse, " Hal.", " " & NewName & ".", 1, -1, vbTextCompare)
2741 GetResponse = Replace(GetResponse, " " & ComputerName & " ", " " & NewName & " ", 1, -1, vbTextCompare)
2742 GetResponse = Replace(GetResponse, " " & ComputerName & ",", " " & NewName & ",", 1, -1, vbTextCompare)
2743 GetResponse = Replace(GetResponse, " " & ComputerName & ".", " " & NewName & ".", 1, -1, vbTextCompare)
2744 End If
2745
2746 'PROCESS: PRESERVE ALL VARIABLES
2747 PrevUserSent = UserSentence
2748 CustomMem = HalBrain.EncodeVar(NewName, "NewName") & HalBrain.EncodeVar(UserSex, "UserSex") & HalBrain.EncodeVar(SentCount, "SentCount") & HalBrain.EncodeVar(ShortSents, "ShortSents") & HalBrain.EncodeVar(LoveCount, "Love")
2749
2750 Rem PLUGIN: CUSTOMMEM2
2751 'The preceding comment is actually a plug-in directive for
2752 'the Ultra Hal host application. It allows for code snippets
2753 'to be inserted here on-the-fly based on user configuration.
2754
2755End Function
2756
2757'This function scans a sentence looking to see if a user entered gibberish like
2758'sdfkjhskjdfhskdsdfdf. It works by looking for more than 5 consanants in a row,
2759'which doesn't occur in normal english words.
2760Function DetectGibberish(GibSentence)
2761 DetectGibberish = False
2762 GibCount = 0
2763 For i = 1 To Len(GibSentence) 'loop for every character in the sentence
2764 CurrentLetter = Ucase(Mid(GibSentence, i, 1))
2765 GibCount = GibCount + 1
2766 If CurrentLetter = "0" Then GibCount = 0
2767 If CurrentLetter = "1" Then GibCount = 0
2768 If CurrentLetter = "2" Then GibCount = 0
2769 If CurrentLetter = "3" Then GibCount = 0
2770 If CurrentLetter = "4" Then GibCount = 0
2771 If CurrentLetter = "5" Then GibCount = 0
2772 If CurrentLetter = "6" Then GibCount = 0
2773 If CurrentLetter = "7" Then GibCount = 0
2774 If CurrentLetter = "8" Then GibCount = 0
2775 If CurrentLetter = "9" Then GibCount = 0
2776 If CurrentLetter = "A" Then GibCount = 0
2777 If CurrentLetter = "E" Then GibCount = 0
2778 If CurrentLetter = "I" Then GibCount = 0
2779 If CurrentLetter = "O" Then GibCount = 0
2780 If CurrentLetter = "U" Then GibCount = 0
2781 If CurrentLetter = "Y" Then GibCount = 0
2782 If CurrentLetter = " " Then GibCount = 0
2783 If GibCount = 6 Then
2784 DetectGibberish = True
2785 Exit For
2786 End If
2787 Next
2788End Function
2789
2790Function SimpleYesNo(GetResponse, UserSentence)
2791 SimpleYesNo = ""
2792 GetResponse = HalBrain.HalFormat(GetResponse)
2793 If Len(GetResponse) < 4 And (Len(UserSentence) < 13 Or HalBrain.CountInstances(" ", Trim(UserSentence)) = 0) Then
2794 YesNoDetect = HalBrain.TopicSearch(Trim(HalBrain.ExtractKeywords(UserSentence)), "yesNoDetect")
2795 If Trim(Ucase(UserSentence)) = "OK" Or Trim(Ucase(UserSentence)) = "OKAY" Then YesNoDetect = "Yes"
2796 If YesNoDetect = "Yes" Then
2797 SimpleYesNo = HalBrain.ChooseSentenceFromFile("yesResponses")
2798 ElseIf YesNoDetect = "No" Then
2799 SimpleYesNo = HalBrain.ChooseSentenceFromFile("noResponses")
2800 End If
2801 End If
2802End Function
2803
2804'Extended Response
2805 'If ExtResponse(UserSentence, PrevSent) <> "" Then UserSentence = ExtResponse(UserSentence, PrevSent)
2806
2807Function ExtResponse(UserSentence, PrevSent)
2808 If InStr(1, Trim(UCase(UserSentence)), "EXPLAIN?", vbTextCompare) > 0 And Len(Trim(PrevSent)) > 0 And Len(Trim(UserSentence)) < 10 Then
2809 ExtResponse = Replace(UserSentence, "EXPLAIN?", Trim(PrevSent), 1, -1, vbTextCompare)
2810 End If
2811End Function
2812
2813'If the user clicks on the About/Options button for this plugin
2814'this sub will be called. There are no extra settings for this brain,
2815'so we'll display an information box
2816Sub AboutOptions()
2817 HalBrain.MsgAlert "This is the Ultra Hal 7.0 Default Brain. This brain has no additional options."
2818End Sub
2819
2820'This sub will be called when the Ultra Hal program starts up in case
2821'the script needs to load some modules or seperate programs. If a return
2822'value is given it is passed as a Hal Command to the host Hal program.
2823Function Script_Load()
2824 Rem PLUGIN: SCRIPT_LOAD
2825 'The preceding comment is actually a plug-in directive for
2826 'the Ultra Hal host application. It allows for code snippets
2827 'to be inserted here on-the-fly based on user configuration.
2828End Function
2829
2830'This sub will be called before the Ultra Hal program is closed in case
2831'the script needs to do any cleanup work.
2832Sub Script_Unload()
2833 Rem PLUGIN: SCRIPT_UNLOAD
2834 'The preceding comment is actually a plug-in directive for
2835 'the Ultra Hal host application. It allows for code snippets
2836 'to be inserted here on-the-fly based on user configuration.
2837End Sub
2838
2839'If the host application is Ultra Hal Assistant, then this sub will be
2840'run once a minute enabling plug-ins to do tasks such as checking for
2841'new emails or checking an appointment calendar.
2842Sub Minute_Timer(MinutesPast)
2843 Rem PLUGIN: MINUTE_TIMER
2844 'The preceding comment is actually a plug-in directive for
2845 'the Ultra Hal host application. It allows for code snippets
2846 'to be inserted here on-the-fly based on user configuration.
2847End Sub
2848
2849Rem PLUGIN: FUNCTIONS
2850'The preceding comment is actually a plug-in directive for
2851'the Ultra Hal host application. It allows for code snippets
2852'to be inserted here on-the-fly based on user configuration.