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