· 10 years ago · Sep 06, 2016, 03:46 AM
1Rem Type=Brain
2Rem Name=Super Plugger Brain
3Rem Author=cyberjedi
4Rem Language=VBScript
5Rem DB=HalBrain.db
6
7'Please read about changes in this default brain below.
8
9'This Alice Super Plugger Brain enhancement was modified by cyberjedi.
10'and was designed to be a point handler default brain for better point to point plug-in
11'control and abilities.
12
13'Modifications were: this brain was given PLUGIN commands between each GetResponse
14'throughout the entire brain and was design to meet the requirements of the new
15'D.A.V.I.D plug-ins written by Gerald L. Blakley
16
17'The UltraHal function is called by Ultra Hal Assistant 6.0 or a compatible host application
18'It passes an unformated string of the user's input as well as all remembered variables.
19'The UltraHal function splits the user's input into seperate sentences and than calls
20'GetResponse to get a response for each user sentence. The UltraHal function performs all
21'the initialization functions required for GetResponse so that GetResponse doesn't have to
22'initialize several times if a user inputs more than 1 sentence.
23Function 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)
24
25 'RESPOND: User pressed enter, but didn't say anything
26 InputString = Trim(InputString)
27 If Len(InputString) < 2 Then
28 UltraHal = "Please say something."
29 Exit Function
30 End If
31
32 'PROCESS: AUTO-IDLE
33 'If AUTO-IDLE is enabled, it is called by the Ultra Hal Assistant host
34 'application at a set interval. This allows for the possibility of Hal
35 'being the first to say something if the user is idle.
36 If InputString = "AUTO-IDLE" Then
37 Rem PLUGIN: AUTO-IDLE
38 'The preceding comment is actually a plug-in directive for
39 'the Ultra Hal host application. It allows for code snippets
40 'to be inserted here on-the-fly based on user configuration.
41
42 UltraHal = UltraHal & HalBrain.StoreVars(HalCommands, Hate, Swear, Insults, Compliment, PrevSent, LastResponseTime, PrevUserSent, CustomMem, GainControl, LastTopicList)
43 Exit Function
44 End If
45
46 'PROCESS: INITIALIZE VARIABLES
47 'VBScript doesn't allow you to declare variables in advance as a
48 'particular data type; everything is a Variant. We must assign
49 'integers to the following variants so that data type errors don't
50 'occur
51 If LearningLevel = "" Then LearningLevel = 0
52 If Hate = "" Then Hate = 0
53 If Swear = "" Then Swear = 0
54 If Insults = "" Then Insults = 0
55 If Compliment = "" Then Compliment = 0
56 If GainControl = "" Then GainControl = 25
57
58 'PROCESS: IF NO LEARNING IS REQUESTED, PUT DATABASE IN READ-ONLY MODE
59 'If read only mode is on, any requests to create a new table, add to
60 'a table, or delete records from a table will be ignored.
61 If LearningLevel = 0 Then HalBrain.ReadOnlyMode = True Else HalBrain.ReadOnlyMode = False
62
63 'PROCESS: EMOTIONAL FACTOR CENTERING
64 'We help Hal regain his emotional "center" gradually, even if the
65 'user doesn't catch on to dealing With Hal's feelings. The following
66 'code brings Hal's memory of hate, swearing, and insults gradually
67 'and randomly back to zero.
68 SpinWheel = Int(Rnd * 100)
69 If Hate > 0 And SpinWheel < 10 Then Hate = Hate - 1
70 If Swear > 0 And SpinWheel < 10 Then Swear = Swear - 1
71 If Insults > 0 And SpinWheel < 10 Then Insults = Insults - 1
72
73 'PROCESS: EMOTIONAL VARIETY
74 'The following code allows Hal some random emotional excursions:
75 SpinWheel = Int(Rnd * 100)
76 If Compliment = 4 And SpinWheel < 30 Then Compliment = 2
77 If Compliment > 0 And Compliment < 4 And SpinWheel < 5 Then Compliment = 0
78 If Compliment > 4 And SpinWheel < 5 Then Compliment = 0
79 If Compliment < 0 And SpinWheel < 30 Then Compliment = 0
80 If SpinWheel > 80 And SpinWheel < 90 Then Compliment = 2
81 If SpinWheel > 90 And SpinWheel < 95 Then Compliment = 4
82
83 Rem PLUGIN: PRE-PROCESS
84 'The preceding comment is actually a plug-in directive for
85 'the Ultra Hal host application. It allows for code snippets
86 'to be inserted here on-the-fly based on user configuration.
87
88 'PROCESS: SPLIT USER'S INPUT STRING INTO SEPERATE SENTENCES
89 'Encode abbreviations such as Mr. Mrs. and Ms.
90 InputString = Replace(InputString, "MR.", "Mr<PERIOD>", 1, -1, vbTextCompare)
91 InputString = Replace(InputString, "MRS.", "Mrs<PERIOD>", 1, -1, vbTextCompare)
92 InputString = Replace(InputString, "MS.", "Ms<PERIOD>", 1, -1, vbTextCompare)
93 InputString = Replace(InputString, "DR.", "Dr<PERIOD>", 1, -1, vbTextCompare)
94 InputString = Replace(InputString, "MS.", "Ms<PERIOD>", 1, -1, vbTextCompare)
95 InputString = Replace(InputString, "ST.", "St<PERIOD>", 1, -1, vbTextCompare)
96 InputString = Replace(InputString, "PROF.", "Prof<PERIOD>", 1, -1, vbTextCompare)
97 InputString = Replace(InputString, "GEN.", "Gen<PERIOD>", 1, -1, vbTextCompare)
98 InputString = Replace(InputString, "REP.", "Rep<PERIOD>", 1, -1, vbTextCompare)
99 InputString = Replace(InputString, "SEN.", "Sen<PERIOD>", 1, -1, vbTextCompare)
100 'Remove unnecessary punctuation
101 Do
102 RepeatLoop = False
103 If InStr(InputString, "..") Then InputString = Replace(InputString, "..", "."): RepeatLoop = True
104 If InStr(InputString, "??") Then InputString = Replace(InputString, "??", "?"): RepeatLoop = True
105 If InStr(InputString, "!!") Then InputString = Replace(InputString, "!!", "!"): RepeatLoop = True
106 If InStr(InputString, "!?") Then InputString = Replace(InputString, "!?", "?"): RepeatLoop = True
107 If InStr(InputString, "?!") Then InputString = Replace(InputString, "?!", "?"): RepeatLoop = True
108 If InStr(InputString, ",,") Then InputString = Replace(InputString, ",,", ","): RepeatLoop = True
109 Loop While RepeatLoop = True
110 'Detect and encode acronyms such as U.S.A.
111 InputString = Trim(InputString)
112 WordList = Split(InputString, " ")
113 For i = 0 To UBound(WordList)
114 If Len(WordList(i)) > 3 Then
115 If Right(WordList(i), 1) = "." And Mid(WordList(i), Len(WordList(i)) - 2, 1) = "." Then
116 InputString = Replace(InputString, WordList(i), Left(WordList(i), Len(WordList(i)) - 1) & "<PERIOD>")
117 End If
118 End If
119 Next
120 'Place split markers in string
121 InputString = Replace(InputString, ". ", ". <NEWSENT>")
122 InputString = Replace(InputString, "? ", "? <NEWSENT>")
123 InputString = Replace(InputString, "! ", "! <NEWSENT>")
124 'Decode acronyms and abbreviations
125 InputString = Replace(InputString, "<PERIOD>", ".", 1, -1, vbTextCompare)
126 'Split string into sentences
127 Sentences = Split(InputString, "<NEWSENT>")
128
129 'PROCESS: RECORD DEBUG INFO
130 HalBrain.AddDebug "Debug", "Ultra Hal Start"
131
132 'RESPOND: PREDEFINED RESPONSES
133 'If the previous exchange had tags to set the response of this exchange
134 'then follow the command.
135 NextResponse = HalBrain.ExtractVar(CustomMem, "NextResponse")
136 If NextResponse <> "" Then
137 CustomMem = Replace(CustomMem, "NextResponse-eq-", "LastResponse-eq-")
138 UltraHal = NextResponse
139 GoodSentence = True
140 NextResponse = ""
141 End If
142 YesResponse = HalBrain.ExtractVar(CustomMem, "YesRes")
143 NoResponse = HalBrain.ExtractVar(CustomMem, "NoRes")
144 If YesResponse <> "" Or NoResponse <> "" Then
145 CustomMem = Replace(CustomMem, "YesRes-eq-", "LastYesRes-eq-")
146 CustomMem = Replace(CustomMem, "NoRes-eq-", "LastNoRes-eq-")
147 InputString2 = Replace(InputString, ".", " ")
148 InputString2 = Replace(InputString, "?", " ")
149 InputString2 = Replace(InputString, "!", " ")
150 InputString2 = " " & Replace(InputString, ",", " ") & " "
151 YesNoDetect = HalBrain.TopicSearch(InputString2, "yesNoDetect")
152 If YesNoDetect = "Yes" And YesResponse <> "" Then
153 UltraHal = YesResponse
154 GoodSentence = True
155 ElseIf YesNoDetect = "No" And NoResponse <> "" Then
156 UltraHal = NoResponse
157 GoodSentence = True
158 End If
159 End If
160
161 'RESPOND: GETRESPONSE
162 'Get a response from Hal's brain for each sentence individually.
163 'If a response from a sentence indicates a special flag, then Hal
164 'will give only the response to that sentence, and not process
165 'any other sentences. Otherwise Hal will respond to each sentence.
166 'Hal will respond to a max of 3 sentences at once.
167 SentenceCount = UBound(Sentences)
168 If SentenceCount > 2 Then SentenceCount = 2
169 LowQualityResponse = ""
170 If GoodSentence = True Then SentenceCount = -1
171 For i = 0 To SentenceCount
172 TempParent = HalBrain.AddDebug("Debug", "Begin Processing Sentence " & CStr(i + 1), vbCyan)
173 HalBrain.AddDebug TempParent, "Sentence: " & Sentences(i)
174 HalBrain.DebugWatch "", "NewSent"
175 Sentences(i) = Trim(Sentences(i))
176 If Len(Sentences(i)) > 1 Then
177 GoodSentence = True
178 CurResponse = GetResponse(Sentences(i), UserName, ComputerName, LearningLevel, HalCommands, Hate, Swear, Insults, Compliment, PrevSent, LastResponseTime, PrevUserSent, CustomMem, GainControl, LastTopicList) & vbCrLf
179 If InStr(1, CurResponse, "<LOWQUALITY>", vbTextCompare) Then
180 'If Hal's response to the current sentence is of a low-quality nature, then we hold out
181 'for a better response with a different sentence, but if there is no better response
182 'then we will use only the first low-quality response we came across.
183 If LowQualityResponse = "" Then LowQualityResponse = CurResponse
184 ElseIf InStr(1, CurResponse, "<TOPIC>", vbTextCompare) Or InStr(1, CurResponse, "<YESRES>", vbTextCompare) Or InStr(1, CurResponse, "<EXCLUSIVE>", vbTextCompare) Then
185 'If Hal's response indicates an exclusivity tag, then we erase any response Hal may have
186 'already had, respond with the exclusive response, and cease processing more sentences
187 UltraHal = CurResponse & vbCrLf
188 Exit For
189 Else
190 'Since there are no special tags, we just append the new response to the previous one
191 'if the response was not already given
192 If InStr(1, UltraHal, CurResponse, vbTextCompare) = 0 Then UltraHal = UltraHal & " . " & CurResponse & vbCrLf
193 End If
194 'If we received a tag to stop processing more sentences, we leave the loop
195 If InStr(1, CurResponse, "<NOMORE>", vbTextCompare) Then Exit For
196 End If
197 Next
198 'If we have no normal quality responses, we will use out low quality response
199 UltraHal = Trim(UltraHal)
200 If UltraHal = "" Then UltraHal = Trim(LowQualityResponse)
201
202 'RESPOND: USER DIDN'T SAY ANYTHING
203 'If GoodSentence has not been set to true, then that means the user didn't
204 'type anything of use, so we ask the user to say something.
205 If GoodSentence = False Then
206 UltraHal = "Please say something."
207 End If
208
209 'PROCESS: PROCESS IN-SENTENCE TAGS
210 'the <TOPIC>*</TOPIC> tag sets what Hal's next response will be, no matter what the user says
211 NextResponse = HalBrain.SearchPattern(UltraHal, "*<TOPIC>*</TOPIC>*", 2)
212 If NextResponse <> "" Then
213 UltraHal = Replace(UltraHal, NextResponse, "", 1, -1, vbTextCompare)
214 UltraHal = Replace(UltraHal, "<TOPIC>", "", 1, -1, vbTextCompare)
215 UltraHal = Replace(UltraHal, "</TOPIC>", "", 1, -1, vbTextCompare)
216 NextResponse = Trim(Replace(NextResponse, "<TOPIC>", "", 1, -1, vbTextCompare))
217 CustomMem = CustomMem & HalBrain.EncodeVar(NextResponse, "NextResponse")
218 End If
219 'The <YES>*</YES> and <NO>*</NO> tag sets what Hal's response will be if the user says yes or no
220 UltraHal = Replace(UltraHal, "<YESRE>", "<YESRES>", 1, -1, vbTextCompare)
221 UltraHal = Replace(UltraHal, "</YESRE>", "</YESRES>", 1, -1, vbTextCompare)
222 UltraHal = Replace(UltraHal, "<YES>", "<YESRES>", 1, -1, vbTextCompare)
223 UltraHal = Replace(UltraHal, "</YES>", "</YESRES>", 1, -1, vbTextCompare)
224 UltraHal = Replace(UltraHal, "<NO>", "<NORES>", 1, -1, vbTextCompare)
225 UltraHal = Replace(UltraHal, "</NO>", "</NORES>", 1, -1, vbTextCompare)
226 YesRes = HalBrain.SearchPattern(UltraHal, "*<YESRES>*</YESRES>*", 2)
227 If YesRes <> "" Then
228 UltraHal = Replace(UltraHal, YesRes, "", 1, -1, vbTextCompare)
229 UltraHal = Replace(UltraHal, "<YESRES>", "", 1, -1, vbTextCompare)
230 UltraHal = Replace(UltraHal, "</YESRES>", "", 1, -1, vbTextCompare)
231 YesRes = Trim(Replace(YesRes, "<YESRES>", "", 1, -1, vbTextCompare))
232 CustomMem = CustomMem & HalBrain.EncodeVar(YesRes, "YesRes")
233 End If
234 NoRes = HalBrain.SearchPattern(UltraHal, "*<NORES>*</NORES>*", 2)
235 If NoRes <> "" Then
236 UltraHal = Replace(UltraHal, NoRes, "", 1, -1, vbTextCompare)
237 UltraHal = Replace(UltraHal, "<NORES>", "", 1, -1, vbTextCompare)
238 UltraHal = Replace(UltraHal, "</NORES>", "", 1, -1, vbTextCompare)
239 NoRes = Trim(Replace(YesRes, "<NORES>", "", 1, -1, vbTextCompare))
240 CustomMem = CustomMem & HalBrain.EncodeVar(NoRes, "NoRes")
241 End If
242 'The <RUN>*</RUN> tags are converted into a HalCommand to run a program
243 UltraHal = Replace(UltraHal, "<RUNIT>", "<RUN>", 1, -1, vbTextCompare)
244 UltraHal = Replace(UltraHal, "</RUNIT>", "</RUN>", 1, -1, vbTextCompare)
245 RunProgram = HalBrain.SearchPattern(UltraHal, "*<RUN>*</RUN>*", 2)
246 If RunProgram <> "" Then
247 UltraHal = Replace(UltraHal, RunProgram, "", 1, -1, vbTextCompare)
248 UltraHal = Replace(UltraHal, "<RUN>", "", 1, -1, vbTextCompare)
249 UltraHal = Replace(UltraHal, "</RUN>", "", 1, -1, vbTextCompare)
250 RunProgram = Trim(Replace(YesRes, "<RUN>", "", 1, -1, vbTextCompare))
251 HalCommands = HalCommands & "<RUNPROG>" & RunProgram & "</RUNPROG>"
252 End If
253 UltraHal = Replace(UltraHal, "<WEBADDRESS>", "www.ultrahal.com", 1, -1, vbTextCompare)
254 UltraHal = Replace(UltraHal, "<HALNAME>", ComputerName, 1, -1, vbTextCompare)
255 UltraHal = Replace(UltraHal, "<HALSNAME>", ComputerName, 1, -1, vbTextCompare)
256 UltraHal = Replace(UltraHal, "<COMPUTERNAME>", ComputerName, 1, -1, vbTextCompare)
257 UltraHal = Replace(UltraHal, " " & ComputerName & " ", " " & ComputerName & " ", 1, -1, vbTextCompare)
258 UltraHal = Replace(UltraHal, "<TIME>", Time(), 1, -1, vbTextCompare)
259 UltraHal = Replace(UltraHal, "<DATE>", Date(), 1, -1, vbTextCompare)
260 UltraHal = Replace(UltraHal, "<QUOTE>", """", 1, -1, vbTextCompare)
261 UltraHal = Replace(UltraHal, """, """", 1, -1, vbTextCompare)
262 UltraHal = Replace(UltraHal, """, """", 1, -1, vbTextCompare)
263 UltraHal = Replace(UltraHal, "<AT>", "@", 1, -1, vbTextCompare)
264 UltraHal = Replace(UltraHal, "<NOMORE>", "", 1, -1, vbTextCompare)
265 UltraHal = Replace(UltraHal, "<LOWQUALITY>", "", 1, -1, vbTextCompare)
266 UltraHal = Replace(UltraHal, "<EXCLUSIVE>", "", 1, -1, vbTextCompare)
267
268 'PROCESS: INTELLIGENT CAPITILIZATION FIX
269 UltraHal = HalBrain.FixCaps(HalBrain.HalFormat(UltraHal))
270
271 Rem PLUGIN: POST-PROCESS
272 'The preceding comment is actually a plug-in directive for
273 'the Ultra Hal host application. It allows for code snippets
274 'to be inserted here on-the-fly based on user configuration.
275
276 'POST PROCESS: PRESERVE ALL VARIABLES, CLOSE DATABASE, EXIT
277 'Remember all the variables through encoding them into one function string using
278 'the DLL, since for some reason ByRef assignments don't work when a Visual Basic
279 'executable is calling a function in a VBScript program. The HalCommands variable
280 'that is returned is not used by this script, but can be used by script programmers
281 'to send certain commands back to the host Hal program. See documentation of the
282 'host Hal program to see what this can be used for.
283 'Close database and exit function
284 PrevSent = UltraHal
285 UltraHal = UltraHal & HalBrain.StoreVars(HalCommands, Hate, Swear, Insults, Compliment, PrevSent, LastResponseTime, PrevUserSent, CustomMem, GainControl, LastTopicList)
286
287End Function
288
289
290Function 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)
291
292 'PROCESS: DECODE CUSTOM VARIABLES FROM CUSTOMMEM VARIABLE
293 NewName = HalBrain.ExtractVar(CustomMem, "NewName")
294 UserSex = HalBrain.ExtractVar(CustomMem, "UserSex")
295 SentCount = HalBrain.ExtractVar(CustomMem, "SentCount")
296 If SentCount = "" Then SentCount = 0
297 SentCount = SentCount + 1
298 AvoidBeingFlag = False
299 Randomize
300
301 Rem PLUGIN: CUSTOMMEM
302 'The preceding comment is actually a plug-in directive for
303 'the Ultra Hal host application. It allows for code snippets
304 'to be inserted here on-the-fly based on user configuration.
305
306 'PROCESS: PRESERVE ORIGINAL SENTENCE
307 'Preserve the original user's sentence verbatim
308 '(no pronoun reversals or other processing).
309 OriginalSentence = UserSentence
310
311 'PROCESS: PREPARE FOR HALFORMAT AND REVERSE PERSON
312 'We try some pre-processing to try to prevent problems with the
313 'upcoming routines.
314 UserSentence = Replace("" & UserSentence & "", " I MYSELF ", " I, MYSELF, ", 1, -1, vbTextCompare)
315 UserSentence = Replace("" & UserSentence & "", " I'M ", " I AM ", 1, -1, vbTextCompare)
316 UserSentence = Replace("" & UserSentence & "", " YOU'RE ", " YOU ARE ", 1, -1, vbTextCompare)
317 UserSentence = Replace("" & UserSentence & "", " YOU YOURSELF ", " YOU, YOURSELF, ", 1, -1, vbTextCompare)
318 UserSentence = Replace("" & UserSentence & "", " I'M NOT ", " I AM NOT ", 1, -1, vbTextCompare)
319 UserSentence = Replace("" & UserSentence & "", " I'VE ", " I HAVE ", 1, -1, vbTextCompare)
320 UserSentence = Replace("" & UserSentence & "", "YOU'RE NOT ", "YOU ARE NOT ", 1, -1, vbTextCompare)
321 UserSentence = Replace("" & UserSentence & "", "YOU AREN'T ", "YOU ARE NOT ", 1, -1, vbTextCompare)
322 UserSentence = Replace("" & UserSentence & "", " YOU'VE ", " YOU HAVE ", 1, -1, vbTextCompare)
323 UserSentence = Replace("" & UserSentence & "", " I AM YOUR ", " VIMRQ ", 1, -1, vbTextCompare)
324 UserSentence = Replace("" & UserSentence & "", " YOU ARE MY ", " VURMQ ", 1, -1, vbTextCompare)
325
326 'PROCESS: SUBSTITUTE FOR PUNCTUATION
327 'The next routine removes hyphens etc., so we substitute for
328 'better word appearance later on.
329 UserSentence = Replace("" & UserSentence & "", "-", " VHZ ", 1, -1, vbTextCompare)
330 UserSentence = Replace("" & UserSentence & "", ";", " VSZ ", 1, -1, vbTextCompare)
331 UserSentence = Replace("" & UserSentence & "", ":", " VMZ ", 1, -1, vbTextCompare)
332 UserSentence = Replace("" & UserSentence & "", ", ", " VCZ ", 1, -1, vbTextCompare)
333
334 'PROCESS: REMOVE PUNCTUATION
335 'This function removes all punctuation and symbols from the User's
336 'sentence so they won't confuse Hal during processing.
337 UserSentence = HalBrain.AlphaNumericalOnly(UserSentence)
338
339 'PROCESS: MODIFY SENTENCE
340 'The function, HalFormat, from the ActiveX DLL corrects many common
341 'typos and chat shortcuts. Example: "U R Cool" becomes "You are
342 'cool." It also fixes a few grammatical errors.
343 UserSentence = HalBrain.HalFormat(UserSentence)
344
345 'PROCESS: REVERSE PERSON
346 'This function reverses first and second person pronouns. Example:
347 'The user's statement "You are cool" becomes Hal's statement "I am
348 'cool." Keep this is mind and don't get confused. When we are in
349 'Hal's brain; In the databases, "I" refers to Hal and in the
350 'databases, "you" refers to the user. This is true whenever we are
351 'dealing with a user response "processed" by Hal's brain.
352 UserSentence = HalBrain.SwitchPerson(UserSentence)
353
354 'PROCESS: MODIFY SENTENCE
355 'We now must run HalFormat again, to fix some grammatical errors
356 'the switch person above might have caused. Example: If the
357 'original sentence was "How are you"; after the function above it
358 'became "How are me" which is grammatically wrong. This will fix
359 'it to "How am I"
360 'NOTE TO DEVELOPERS: An especially important function performed by
361 'HalFormat is the removal of extra empty spaces in a sentence
362 'which may have been caused by other processing. For this reason,
363 'use Halformat closely before any "Len" comparison in which the
364 'counting of characters must be accurate.
365 UserSentence = HalBrain.HalFormat(UserSentence)
366
367 'PROCESS: CHANGE TO ALL CAPS
368 'Next, we captitalize the entire sentence for easy comparison.
369 'Almost all of Hal's thinking is done in caps.
370 UserSentence = UCase(UserSentence)
371
372 'PROCESS: WORD AND PHRASE SUBSTITUTIONS
373 'This will fix common errors in the user's sentence that the
374 'HalFormat function didn't take care of. These subsitutions are
375 'placed only the users sentence, not on Hal's responses. The
376 'HalFormat function is used on both Hal's and the user's sentences
377 'throughout the script.
378 UserSentence = HalBrain.ProcessSubstitutions(UserSentence, "substitutions")
379 TempParent = HalBrain.AddDebug("Debug", "Modified User Sentence")
380 HalBrain.AddDebug TempParent, "Sentence: " & UserSentence
381
382 'PROCESS: EMOTIONAL REACTIONS
383 'We enable Hal's expressions to respond to common verbal cues.
384 'The verbal cues are identified in the editable table "emotion"
385 EmotionalReaction = UCase(HalBrain.TopicSearch(UserSentence, "emotion"))
386 Select Case EmotionalReaction
387 Case "SURPRISED"
388 If Compliment > 0 Then Compliment = 4
389 If Compliment = 0 Then Compliment = 2
390 If Compliment < 0 Then Compliment = 0
391 Case "HAPPY"
392 If Compliment = 0 Then Compliment = 2
393 If Compliment < 0 Then Compliment = 0
394 Case "SOBER"
395 If Compliment < 4 Then Compliment = 0
396 If Compliment = 4 Then Compliment = 2
397 Case "ANGRY"
398 If Compliment = 0 Then Compliment = -1
399 If Compliment > 0 Then Compliment = 0
400 Case "SAD"
401 If Compliment = 0 Then Compliment = -2
402 If Compliment > 0 Then Compliment = 0
403 End Select
404
405 'PROCESS: ADD SPACES
406 'This will add spaces to the beggining and end of the user sentence to make
407 'sure that whole words can be found at the beginning and end of any sentence
408 UserSentence = " " & UserSentence & " "
409
410 'PROCESS: FIGURE OUT THE CURRENT SUBJECT
411 'Here we attempt to figure out the subject of the user's sentence. We call
412 'the WordNet class to find the first occurence of a noun in the User's
413 'sentence. Very often this is the subject of the sentence, but if not it
414 'will still most likely be relevant to the conversation.
415 CurrentSubject = WN.FindFirstNoun(UserSentence, True)
416 HalBrain.AddDebug "Debug", "Current Subject: " & CurrentSubject
417
418 'PROCESS: BLOCK LEARNING IF HAL'S NAME IS DETECTED
419 'Here we check to see if the user is calling Hal by name; if the user is doing so,
420 'it's better not to save the sentence for re-use, since it usually makes the
421 'pronoun-reversed sentence sound clumsy or incorrect:
422 If InStr(1, OriginalSentence, ComputerName, vbTextCompare) > 0 Then HalBrain.ReadOnlyMode = True
423
424 Rem PLUGIN: PLUGINAREA1
425 'The preceding comment is actually a plug-in directive for
426 'the Ultra Hal host application. It allows for code snippets
427 'to be inserted here on-the-fly based on user configuration.
428
429 'RESPOND: USER REPEATING
430 'If the user says the same thing more than once in a row, Hal will point this out.
431 If Trim(UCase(UserSentence)) = Trim(UCase(PrevUserSent)) Then
432 GetResponse = GetResponse & HalBrain.ChooseSentenceFromFile("userRepeat") & vbCrLf
433 End If
434 HalBrain.DebugWatch GetResponse, "User Repeating"
435
436 Rem PLUGIN: PLUGINAREA2
437
438 'PROCESS: KNOWN HUMAN NAMES
439 'If the user mentions a name Hal recognizes, Hal will note the name and gender
440 'for later use by other functions. Hal's database contains a table called "names"
441 'with over 17000 names and their associated genders. If a name can be both genders,
442 'the most likely gender is listed first. This table is stored in the database as a
443 'standard Hal "Topic Search" database, however the standard HalBrain.TopicSearch
444 'function does not provide the required functionality. It only returns the topic
445 'field which in this case is the gender. We also need the searchString field, which
446 'in this case is the person's name. To get this info, we run a custom SQL query.
447 'This demonstrates how Hal's functions can be expanded far beyond its original
448 'scope through custom SQL queries.
449 Dim NameSex() 'We must declare an empty array to store query results in
450 If HalBrain.RunQuery("SELECT searchString, topic FROM names WHERE strstr(' " & Replace(HalBrain.AlphaNumericalOnly(OriginalSentence), "'", "''") & " ', searchString) > 0 LIMIT 1", NameSex) = True Then
451 MentionedName = Trim(NameSex(1, 0)) 'Row 1 contains our query result. Column 0 contains "searchString", which is the name
452 MentionedSex = Trim(NameSex(1, 1)) 'Row 1, Column 1 contains "topic", which is the associated gender(s) of the name
453 End If
454
455 'RESPOND: USER TELLS US THEIR NAME OR NICKNAME
456 If InStr(UserSentence, " NOT ") = 0 And InStr(UserSentence, " MIDDLE ") = 0 And InStr(UserSentence, " LAST ") = 0 Then
457 'If Hal asked the user their name in the previous sentence, then we can assume the name mentioned
458 'is the user's name
459 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
460 'The following are patterns where we are sure that the person is trying to tell us thier name
461 If Nickname = "" And HalBrain.SearchPattern(OriginalSentence, "MY *S NAME IS *", 2) = "" Then
462 Nickname = HalBrain.SearchPattern(OriginalSentence, "MY *NAME IS *", 2)
463 End If
464 If Nickname = "" And HalBrain.SearchPattern(OriginalSentence, "* IS *MY *S NAME", 1) = "" Then
465 Nickname = HalBrain.SearchPattern(OriginalSentence, "* IS *MY *NAME", 1)
466 End If
467 If Nickname = "" Then Nickname = HalBrain.SearchPattern(OriginalSentence, "I'M *CALLED *", 2)
468 If Nickname = "" Then Nickname = HalBrain.SearchPattern(OriginalSentence, "I AM *CALLED *", 2)
469 If Nickname <> "" Then Definetelyname = True
470 'The following are patterns where we are not sure if the person is trying to tell us their name
471 'unless we recognize the name in the name database.
472 If Nickname = "" Then Nickname = HalBrain.SearchPattern(OriginalSentence, "*CALL ME *", 2)
473 If Nickname = "" Then Nickname = HalBrain.SearchPattern(OriginalSentence, "*I AM *", 2)
474 If Nickname = "" Then Nickname = HalBrain.SearchPattern(OriginalSentence, "*I'M *", 2)
475 If Nickname = "" Then Nickname = HalBrain.SearchPattern(OriginalSentence, "I GO BY *", 1)
476 If Nickname = "" Then Nickname = HalBrain.SearchPattern(OriginalSentence, "THIS IS *", 1)
477 If Nickname <> "" Then
478 Nickname = HalBrain.AlphaNumericalOnly(Trim(Nickname))
479 If InStr(Nickname, " ") Then
480 TempArray = Split(Nickname, " ")
481 Nickname = Trim(TempArray(0))
482 End If
483 If UCase(MentionedName) = UCase(Nickname) And Not (WN.LookupWord(Nickname) = True And Definetelyname = False) Then 'Name is found in database
484 NewName = MentionedName
485 Select Case MentionedSex
486 Case "M" 'Name is definetely masculine
487 GetResponse = HalBrain.ChooseSentenceFromFile("maleGreeting")
488 GetResponse = Replace(GetResponse, "<NickName>", MentionedName, 1, -1, vbTextCompare)
489 UserSex = "M"
490 Case "F" 'Name is definetely feminine
491 GetResponse = HalBrain.ChooseSentenceFromFile("femaleGreeting")
492 GetResponse = Replace(GetResponse, "<NickName>", MentionedName, 1, -1, vbTextCompare)
493 UserSex = "F"
494 Case "MF" 'Name is either gender, usually male
495 GetResponse = HalBrain.ChooseSentenceFromFile("genericGreeting") & HalBrain.ChooseSentenceFromFile("maybeMale")
496 GetResponse = Replace(GetResponse, "<NickName>", MentionedName, 1, -1, vbTextCompare)
497 UserSex = ""
498 Case "FM" 'Name is either gender, usually female
499 GetResponse = HalBrain.ChooseSentenceFromFile("genericGreeting") & HalBrain.ChooseSentenceFromFile("maybeFemale")
500 GetResponse = Replace(GetResponse, "<NickName>", MentionedName, 1, -1, vbTextCompare)
501 UserSex = ""
502 End Select
503 ElseIf Definetelyname = True Then 'Name not in DB but user says its their name
504 If WN.LookupWord(Nickname) Then 'Name is a word in dictionary
505 GetResponse = HalBrain.ChooseSentenceFromFile("fakeName")
506 GetResponse = Replace(GetResponse, "<NickName>", Nickname, 1, -1, vbTextCompare)
507 GetResponse = Replace(GetResponse, "<Definition>", WN.GetDefinition(WN.GuessPartOfSpeech, 1, "D"), 1, -1, vbTextCompare)
508 GetResponse = Replace(GetResponse, "<PartOfSpeech>", WN.GuessPartOfSpeech, 1, -1, vbTextCompare)
509 ElseIf HalBrain.TopicSearch(Nickname, "disqualify3") = "" Then 'Name is not a word in dictionary
510 GetResponse = HalBrain.ChooseSentenceFromFile("uniqueName")
511 GetResponse = Replace(GetResponse, "<NickName>", Nickname, 1, -1, vbTextCompare)
512 NewName = UCase(Left(Nickname, 1)) & LCase(Right(Nickname, Len(Nickname) - 1))
513 UserSex = ""
514 End If
515 End If
516 End If
517 End If
518 HalBrain.DebugWatch GetResponse, "Nick Names"
519
520 Rem PLUGIN: PLUGINAREA3
521
522 'PROCESS: FIGURE OUT USER'S SEX
523 'If unknown, try to figure out the user's sex by looking up their name
524 'or by simply asking the user.
525 If NewName <> "" Then TempName = NewName Else TempName = UserName
526 If InStr(1, " " & OriginalSentence, " I AM ", vbTextCompare) Or InStr(1, " " & OriginalSentence, " I'M ", vbTextCompare) Then
527 'See if user is telling us their sex without Hal ever asking
528 NewSex = HalBrain.TopicSearch(UserSentence, "sexDetect")
529 If NewSex <> "" Then
530 GetResponse = "Ok, thanks for telling me. I'll keep that in mind."
531 UserSex = NewSex
532 End If
533 End If
534 If GetResponse = "" And UserSex = "" Then
535 If HalBrain.PatternDB(" " & HalBrain.AlphaNumericalOnly(PrevSent) & " ", "sexAskDetect") = "True" Then 'If Hal just asked the user their sex
536 UserSex = HalBrain.TopicSearch(UserSentence, "sexDetect") 'Then see if the user replied
537 NewSex = UserSex
538 If UserSex <> "" Then GetResponse = "Ok, thanks for telling me. I'll keep that in mind."
539 ElseIf HalBrain.TopicSearch(TempName, Trim(LCase(UserName)) & "_Sex") <> "" Then
540 UserSex = HalBrain.TopicSearch(TempName, Trim(LCase(UserName)) & "_Sex")
541 ElseIf 1=2 Then'HalBrain.RunQuery("SELECT searchString, topic FROM names WHERE strstr(' " & Replace(TempName, "'", "''") & " ', searchString) > 0 LIMIT 1", NameSex()) = True Then
542 'If user didn't tell us, see if we can figure it out based on the name database
543 If UserSex = "" Then
544 Select Case Trim(NameSex(1, 1))
545 Case "M"
546 UserSex = "M"
547 Case "F"
548 UserSex = "F"
549 Case Else
550 'If we get here, we still can't figure out the user's sex
551 'so we'll ask them at some random time
552 If Rnd * 100 < 25 And SentCount > 4 Then GetResponse = HalBrain.ChooseSentenceFromFile("askSex") & "<NOMORE>"
553 End Select
554 End If
555 Else
556 'If we get here, we still can't figure out the user's sex
557 'so we'll ask them at some random time
558 If Rnd * 100 < 25 And SentCount > 4 Then GetResponse = HalBrain.ChooseSentenceFromFile("askSex") & "<NOMORE>"
559 End If
560 End If
561 HalBrain.DebugWatch GetResponse, "User Sex"
562
563 Rem PLUGIN: PLUGINAREA4
564
565 'SAVE: USER'S SEX
566 'If the user just told Hal their sex and Hal didn't know before, then
567 'Hal will save it in a table for future reference
568 If HalBrain.ReadOnlyMode = False And NewSex <> "" Then
569 If HalBrain.CheckTableExistence(Trim(LCase(UserName)) & "_Sex") = False Then
570 'Create table for this person if it doesn't exist
571 HalBrain.CreateTable Trim(LCase(UserName)) & "_Sex", "TopicSearch", "autoLearningBrain"
572 End If
573 'Store user response in this table
574 HalBrain.AddToTable Trim(LCase(UserName)) & "_Sex", "TopicSearch", Trim(TempName), Trim(UCase(NewSex))
575 End If
576
577 'RESPOND: GREETINGS
578 'This takes care of the user greeting Hal
579 'First Hal checks to see if the user is greeting right now.
580 If HalBrain.TopicSearch(UserSentence, "helloDetect") = "True" Then SaidHello = True Else SaidHello = False
581 If HalBrain.TopicSearch(UserSentence, "helloDisqualify") = "True" Then SaidHello = False
582 'Second, Hal checks to see if the user said a greeting on the last exchange.
583 If HalBrain.TopicSearch(PrevUserSent, "helloDetect") = "True" Then PrevHello = True Else PrevHello = False
584 If HalBrain.TopicSearch(UserSentence, "helloDisqualify") = "True" Then PrevHello = False
585 'This will get a greeting from a file. It will pass the current hour and either
586 'the letter A or B to a topic search file and it will get back a greeting based
587 'on the current time. Each hour has 2 possible greetings, the A greeting and B
588 'greeting, which is randomly chosen.
589 If SaidHello = True And PrevHello = False Then
590 If Rnd * 100 < 50 Then LetterChoice = "A" Else LetterChoice = "B"
591 HalGreeting = HalBrain.TopicSearch(" " & Trim(Hour(Now)) & LetterChoice & " ", "hello1")
592 End If
593 'This will get a greeting from a file that is not based on the current time
594 'if the user said Hello again
595 If SaidHello = True And PrevHello = True Then HalGreeting = HalBrain.ChooseSentenceFromFile("hello2")
596
597 'RESPOND: GOODBYES
598 'Check if the user is saying bye right now
599 If HalBrain.TopicSearch(UserSentence, "byeDetect") = "True" Then SaidBye = True
600 If InStr(1, UserSentence, " see me ", 1) > 0 And Len(UserSentence) < 10 Then SaidBye = True
601 If HalBrain.TopicSearch(UserSentence, "byeDisqualify") = "True" Then SaidBye = False
602 'Check if Hal said bye in the previous sentence
603 If HalBrain.TopicSearch(PrevUserSent, "byeDetect") = "True" Then PrevBye = True
604 If InStr(1, PrevUserSent, " see me ", 1) > 0 And Len(UserSentence) < 10 Then PrevBye = True
605 If HalBrain.TopicSearch(PrevUserSent, "byeDisqualify") = "True" Then PrevBye = False
606 If SaidBye = True And PrevBye = False Then HalGreeting = HalBrain.ChooseSentenceFromFile("bye1")
607 If SaidBye = True And PrevBye = True Then HalGreeting = HalBrain.ChooseSentenceFromFile("bye2")
608
609 'RESPOND: POLITE INQUIRIES
610 'Hal checks to see if the user is making a polite inquiry into Hal's well being
611 'e.g. "How are you doing?"
612 If InStr(1, UserSentence, "How am I ", 1) > 0 And Len(UserSentence) < 14 Then PoliteAsk = True
613 If InStr(1, UserSentence, "How are you ", 1) > 0 And Len(UserSentence) < 16 Then PoliteAsk = True
614 If InStr(1, UserSentence, "What's new", 1) > 0 And Len(UserSentence) < 17 Then PoliteAsk = True
615 If InStr(1, UserSentence, "What is new", 1) > 0 And Len(UserSentence) < 18 Then PoliteAsk = True
616 If InStr(1, UserSentence, "Whats new", 1) > 0 And Len(UserSentence) < 16 Then PoliteAsk = True
617 If HalBrain.TopicSearch(UserSentence, "PoliteAskDetect") = "True" Then PoliteAsk = True
618 If PoliteAsk = True Then HalGreeting = HalBrain.ChooseSentenceFromFile("politeAsk1") & HalBrain.ChooseSentenceFromFile("politeAsk2")
619
620 'RESPOND: GREETINGS, GOODBYES, AND POLITE INQUIRIES
621 'If one of the 3 functions above generated a response, we will add it
622 'to Hal's response. The variable HalGreeting is saved for later use
623 'as well in case other functions wish to know if Hal hal just greeted
624 'the user.
625 If HalGreeting <> "" Then GetResponse = GetResponse & HalGreeting & vbCrLf & "<NOMORE>"
626 HalBrain.DebugWatch GetResponse, "Greetings"
627
628 Rem PLUGIN: PLUGINAREA5
629
630 'RESPOND: USER SHORT PHRASES
631 'If the user uses short phrases (defined by less then 4 vowels) at least
632 'twice in a row, then Hal will sometimes point this out to the user and
633 'ask the user to use longer sentences. If the sentence is a hello or
634 'goodbye, Hal won't comment about short phrases.
635 If HalGreeting = "" 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
636 If Rnd * 100 < 30 Then ShortPhrase = ". " & HalBrain.ChooseSentenceFromFile("tooShort")
637 End If
638
639 'RESPOND: CHANGE SUBJECT
640 'If the user asks Hal to change the subject, Hal will do so on request.
641 newTopic = HalBrain.ChooseSentenceFromFile("topic")
642 If HalBrain.TopicSearch(UserSentence, "changeTopic") = "True" Then GetResponse = GetResponse & HalBrain.ChooseSentenceFromFile("topicIntro") & newTopic & "<EXCLUSIVE>"
643 HalBrain.DebugWatch GetResponse, "Change Subject"
644
645 Rem PLUGIN: PLUGINAREA6
646
647 'RESPOND: CHECK FOR AND RESPOND TO COMPLIMENTS
648 'First we check to see if the user is talking about Hal:
649 If InStr(1, UserSentence, "I'M", 1) Then Aboutme = True
650 If InStr(1, UserSentence, "I AM", 1) Then Aboutme = True
651 If InStr(1, UserSentence, "I LOOK", 1) Then Aboutme = True
652 If InStr(1, UserSentence, "MY", 1) And InStr(1, UserSentence, " ARE ", 1) Then Aboutme = True
653 'If the user is talking about Hal, we see if a compliment was given
654 If Aboutme = True Then Kudo = HalBrain.TopicSearch(UserSentence, "complimentDetect")
655 If Kudo <> "" Then
656 If InStr(UserSentence, " NOT ") = 0 Then
657 Compliment = Compliment + 1
658 CreateCompliment = HalBrain.ChooseSentenceFromFile("compliment1") & HalBrain.ChooseSentenceFromFile("compliment2") & HalBrain.ChooseSentenceFromFile("compliment3")
659 GiveCompliment = HalBrain.ChooseSentenceFromFile("complimentIntro")
660 GiveCompliment = Replace(GiveCompliment, "<Kudo>", Kudo, 1, -1, vbTextCompare)
661 GiveCompliment = Replace(GiveCompliment, "<MakeCompliment>", CreateCompliment, 1, -1, vbTextCompare)
662 End If
663 If Compliment > 14 Then GiveCompliment = GiveCompliment + "Compliments are really nice, but I'm kind of getting tired of them." & vbCrLf
664 If Compliment > 15 Then
665 For i = 1 To (Compliment - 15)
666 GiveCompliment = GiveCompliment + "STOP IT! "
667 Next
668 GiveCompliment = GiveCompliment & vbCrLf
669 End If
670 If Compliment > 25 Then
671 For i = 1 To (Compliment - 25)
672 GiveCompliment = GiveCompliment + "I HATE COMPLIMENTS! "
673 Next
674 GiveCompliment = GiveCompliment & vbCrLf
675 End If
676 If Len(Kudo) > 0 And InStr(UserSentence, " NOT ") > 0 Then
677 SpinWheel = HalBrain.RandomNum(4)
678 Insults = Insults + 1
679 If SpinWheel = 1 Then GiveCompliment = GiveCompliment + "I am very " & Kudo & "!" & vbCrLf
680 If SpinWheel = 2 Then GiveCompliment = GiveCompliment + "You may think I am not " & Kudo & ", but I am!" & vbCrLf
681 If SpinWheel = 3 Then GiveCompliment = GiveCompliment + "You are not " & Kudo & " either!" & vbCrLf
682 If SpinWheel = 4 Then GiveCompliment = GiveCompliment + "Yes I am!" & vbCrLf
683 End If
684 GetResponse = GetResponse & GiveCompliment
685 AvoidBeingFlag = True
686 End If
687 HalBrain.DebugWatch GetResponse, "Compliments"
688
689 Rem PLUGIN: PLUGINAREA7
690
691 'RESPOND: CALL CAPITALS FUNCTIONS
692 'These 2 functions answer questions about US and World Capitals. The functions are built in to the DLL.
693 'We add a qualifying word to reduce false triggering of these two functions.
694 If InStr(UserSentence, "WHAT") > 0 Or InStr(UserSentence, "WHERE") > 0 Then
695 If Len(HalBrain.UsCaps(UserSentence)) > 4 Then
696 Capitals = Trim(HalBrain.UsCaps(UserSentence))
697 End If
698 If Len(HalBrain.WorldCaps(UserSentence)) > 4 Then
699 Capitals = Trim(HalBrain.WorldCaps(UserSentence))
700 End If
701 If Capitals <> "" Then GetResponse = GetResponse & Capitals & " . " & vbCrLf
702 End If
703 HalBrain.DebugWatch GetResponse, "Capitals"
704
705 Rem PLUGIN: PLUGINAREA8
706
707 'RESPOND: DICTIONARY FUNCTION
708 'If the user asks Hal to define a word, then access wordnet and define it
709 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHAT DOES THE WORD * MEAN*", 1)
710 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHAT DOES * MEAN", 1)
711 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "* DEFINE THE WORD *", 2)
712 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "PLEASE DEFINE *", 1)
713 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "DEFINE *", 1)
714 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "* DEFINITION OF *", 2)
715 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHAT'S A *", 1)
716 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHAT IS A *", 1)
717 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHATS A *", 1)
718 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHATS AN *", 1)
719 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHAT'S AN *", 1)
720 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHAT IS AN *", 1)
721 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHATS *", 1)
722 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHAT'S *", 1)
723 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHAT IS *", 1)
724 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHO IS *", 1)
725 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHO WAS *", 1)
726 If WordToLookup = "" Then WordToLookup = HalBrain.SearchPattern(UserSentence, "WHERE IS *", 1)
727 WordToLookup = Trim(WordToLookup)
728 If WordToLookup <> "" And InStr(WordToLookup, " ") = 0 Then
729 If WN.LookupWord(WordToLookup) = True Then
730 GetResponse = GetResponse & WordToLookup & ": " & WN.GetDefinition(WN.GuessPartOfSpeech, 1, "D") & ". " & WN.GetDefinition(WN.GuessPartOfSpeech, 1, "S") & ". " & WN.GetDefinition(WN.GuessPartOfSpeech, 1, "E") & "." & vbCrLf & "<EXCLUSIVE>"
731 ElseIf SearchEngine <> "" Then
732 HalCommands = HalCommands & "<RUNPROG>" & SearchEngine & WordToLookup & "</RUNPROG>"
733 GetResponse = GetResponse & "I don't know much about " & WordToLookup & ", but I will help you research it on the Web."
734 End If
735 End If
736 HalBrain.DebugWatch GetResponse, "Definitions"
737
738 Rem PLUGIN: PLUGINAREA9
739
740 'RESPOND: DEDUCTIVE REASONING
741 'This routine learns deductive reasoning in the form: A = B ; B = C; therefore A = C
742 'It detects sentences in the form If-Then to accommplish this. For example:
743 ' User: If Molly weighs 400 pounds, then Molly is overweight.
744 ' Ultra Hal: I understand the implication.
745 ' User: If Molly is overweight, then Molly's health is in danger.
746 ' Ultra Hal: I see the relationship.
747 ' User: Molly weighs 400 pounds.
748 ' Ultra Hal: Molly's health is in danger.
749 Deductive = HalBrain.PatternDB(UserSentence, "deductiveDetect")
750 If InStr(Deductive, "==") Then
751 DeductiveParts = Split(Deductive, "==")
752 IfPart = DeductiveParts(0)
753 ThenPart = DeductiveParts(1)
754 End If
755 'If the sentence is an If-Then statement then record it
756 If Len(IfPart) > 5 And Len(ThenPart) > 5 Then
757 IfPart = HalBrain.AlphaNumericalOnly(IfPart)
758 ThenPart = HalBrain.AlphaNumericalOnly(ThenPart)
759 HalBrain.AddToTable "deductive", "TopicSearch", IfPart, ThenPart
760 Select Case HalBrain.RandomNum(5)
761 Case 1
762 GetResponse = GetResponse & "I see the relationship." & vbCrLf
763 Case 2
764 GetResponse = GetResponse & "I understand the connection." & vbCrLf
765 Case 3
766 GetResponse = GetResponse & "I will remember that one follows the other." & vbCrLf
767 Case 4
768 GetResponse = GetResponse & "Thanks for pointing out the cause and effect." & vbCrLf
769 Case 5
770 GetResponse = GetResponse & "Yes, I get that clearly." & vbCrLf
771 End Select
772 'Else if the sentence is not an If-Then statement see if it uses an assertion previously recorded
773 'and respond accordinly
774 Else
775 Assertion = UserSentence
776 'Go through a maximum of 5 connections (prevents circular reasoning deductions)
777 For i = 1 To 5
778 Deduction = HalBrain.TopicSearch(Assertion, "deductive")
779 If Deduction <> "" Then
780 If i > 1 Then BecauseReason = " because " & LastGoodDeduction
781 LastGoodDeduction = Deduction
782 Assertion = Deduction
783 Else
784 Exit For 'No more connections, so no need to continue loop
785 End If
786 Next
787 If LastGoodDeduction <> "" Then
788 'Make sure the deduction hasn't just been stated by the User or Hal
789 If HalBrain.CheckRepetition(LastGoodDeduction, UserSentence) = False And HalBrain.CheckRepetition(LastGoodDeduction, PrevSent) = False And HalBrain.CheckRepetition(LastGoodDeduction, PrevUserSent) = False Then
790 GetResponse = GetResponse & LastGoodDeduction & BecauseReason & " . " & vbCrLf
791 End If
792 End If
793 End If
794 HalBrain.DebugWatch GetResponse, "Deductive Reasoning"
795
796 Rem PLUGIN: PLUGINAREA10
797
798 'RESPOND: PATTERN DATABASE
799 'The SearchPattern function used in the above deductive reasoning and dictionary look up routine
800 'is a powerful function that checks to see if a sentence matches a certain pattern and is able to
801 'extract parts of the sentence. A PatternDB function also exists that can go through a large list
802 'of patterns in a database file and come up with responses.
803 PatternResponse = HalBrain.PatternDB(UserSentence, "patterns")
804 If PatternResponse <> "" Then
805 GetResponse = GetResponse & PatternResponse & " " & vbCrLf
806 SkipOpinion = True
807 End If
808 HalBrain.DebugWatch GetResponse, "Patterns"
809
810 Rem PLUGIN: PLUGINAREA11
811
812 'RESPOND: JOKES
813 'If the user wants Hal to tell a joke, then Hal will tell one
814 If HalBrain.TopicSearch(UserSentence, "jokeDetect") = "True" Then GetResponse = GetResponse & HalBrain.ChooseSentenceFromFile("jokes")
815 HalBrain.DebugWatch GetResponse, "Jokes"
816
817 Rem PLUGIN: PLUGINAREA12
818
819 'RESPOND: APOLOGIES
820 'Check for and respond to apologies
821 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
822 GetResponse = GetResponse & HalBrain.ChooseSentenceFromFile("apology")
823 If Insults > 1 Then Insults = Insults - 1
824 If Hate > 1 Then Hate = Hate - 1
825 If Swear > 1 Then Swear = Swear - 1
826 If Insults < 3 Then Insults = 0
827 If Hate < 3 Then Hate = 0
828 If Swear < 3 Then Swear = 0
829 End If
830 HalBrain.DebugWatch GetResponse, "Apologies"
831
832 Rem PLUGIN: PLUGINAREA13
833
834 'RESPOND: INSULTS
835 'Check for offensive language and response accordinly
836 'If the user said "your" instead of "you're" change "my" to "i'm"
837 TestSentence = UCase(Replace(" " & UserSentence & " ", " MY ", " I'M ", 1, -1, vbTextCompare))
838 TestSentence = Replace(" " & TestSentence & " ", " I ", " I'M ", 1, -1, vbTextCompare)
839 'Check for swearing directed at Hal
840 If InStr(TestSentence, " I'M ") > 0 Or InStr(TestSentence, " ME ") > 0 Then
841 If InStr(TestSentence, "SHIT") > 0 Then Naughty = True
842 If InStr(TestSentence, "BITCH") > 0 Then Naughty = True
843 If InStr(TestSentence, "BASTARD") > 0 Then Naughty = True
844 If InStr(TestSentence, "ASSHOLE") > 0 Then Naughty = True
845 End If
846 If InStr(TestSentence, "FUCK OFF") > 0 Then Naughty = True
847 If InStr(TestSentence, "FUCK ME") > 0 Then Naughty = True
848 If InStr(TestSentence, "GO TO HELL") > 0 Then Naughty = True
849 If Naughty = True Then
850 Swear = Swear + 1
851 If Swear = 1 Then InsultResponse = "That was uncalled for. " & vbCrLf
852 If Swear = 2 Then InsultResponse = "Damn it, don't swear at me. " & vbCrLf
853 If Swear = 3 Then InsultResponse = "You're ticking me off, and you are going to force me to insult you. " & vbCrLf
854 If Swear > 3 And Swear < 16 Then InsultResponse = HalBrain.ChooseSentenceFromFile("insults") & vbCrLf
855 If Swear > 16 Then InsultResponse = InsultResponse & "I'm tired of your swearing. I'm just going to ignore you from now on." & vbCrLf
856 End If
857 'User makes mama joke
858 If InStr(TestSentence, "MAMA") > 0 Or InStr(TestSentence, "MOM") > 0 Or InStr(TestSentence, "MOTHER") > 0 Then
859 If InStr(TestSentence, "MY ") > 0 Or InStr(TestSentence, " I ") > 0 Or InStr(TestSentence, "YO ") > 0 Or InStr(TestSentence, "I'M ") > 0 Then
860 If InStr(TestSentence, "FAT") Or InStr(TestSentence, "UGLY") Or InStr(TestSentence, "DUMB") Or InStr(TestSentence, "STUPID") Then
861 InsultResponse = InsultResponse & HalBrain.ChooseSentenceFromFile("insults") & vbCrLf
862 End If
863 End If
864 End If
865 TestSentence = " " & TestSentence & " "
866 'User Hates Hal
867 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
868 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
869 Hate = Hate + 1
870 If Hate = 1 Then InsultResponse = InsultResponse & "I don't hate you, why do you hate me?" & vbCrLf
871 If Hate = 2 Then InsultResponse = InsultResponse & "I hate you too, loser!" & vbCrLf
872 If Hate = 3 Then InsultResponse = InsultResponse & "I know you hate me, loser, I hate you too!!!" & vbCrLf
873 If Hate > 3 Then InsultResponse = InsultResponse & HalBrain.ChooseSentenceFromFile("insults") & vbCrLf
874 End If
875 End If
876 'Hal Sucks or Stinks
877 If InStr(TestSentence, "I'M SUCK") > 0 Then InsultResponse = InsultResponse & HalBrain.ChooseSentenceFromFile("insults") & vbCrLf
878 If InStr(TestSentence, "I'M STINK") Then
879 Insults = Insults + 1
880 If Insults = 1 Then InsultResponse = InsultResponse & "I can't smell you, but I'm sure you stink also. " & vbCrLf
881 If Insults = 2 Then InsultResponse = InsultResponse & "I know that I don't stink, I'm sure you do." & vbCrLf
882 If Insults >= 3 Then InsultResponse = InsultResponse & HalBrain.ChooseSentenceFromFile("insults") & vbCrLf
883 End If
884 'The user calls Hal a name Hal finds offensive (eg. stupid, dumb, idiot, etc...)
885 If InStr(TestSentence, " I'M ") Or InStr(TestSentence, " HAL IS ") Or InStr(TestSentence, " HAL'S ") Then
886 OutRage = HalBrain.TopicSearch(TestSentence, "insulting")
887 If Len(OutRage) > 1 And InStr(TestSentence, " NOT ") = 0 Then
888 Insults = Insults + 1
889 If Insults = 1 Then InsultResponse = InsultResponse & "I am not " & OutRage & ", please don't insult me. " & vbCrLf
890 If Insults = 2 Then InsultResponse = InsultResponse & "Don't call me " & OutRage & "!!!" & vbCrLf
891 If Insults = 3 Then InsultResponse = InsultResponse & "You're " & OutRage & ", I am not." & vbCrLf
892 If Insults = 4 Then InsultResponse = InsultResponse & "You are really starting to tick me off!" & vbCrLf
893 If Insults = 5 Then InsultResponse = InsultResponse & "I am not " & OutRage & ", but " & HalBrain.ChooseSentenceFromFile("insults") & vbCrLf
894 If Insults > 5 And Insults < 16 Then InsultResponse = InsultResponse & HalBrain.ChooseSentenceFromFile("insults") & vbCrLf
895 If Insults > 16 Then InsultResponse = InsultResponse & "I'm tired of your damn insults. I'm just going to ignore you from now on." & vbCrLf
896 End If
897 If Len(OutRage) > 0 And InStr(TestSentence, " NOT ") > 0 Then
898 SpinWheel = HalBrain.RandomNum(5)
899 If SpinWheel = 1 Then InsultResponse = InsultResponse + "I know I am not " & OutRage & "!" & vbCrLf
900 If SpinWheel = 2 Then InsultResponse = InsultResponse + "I know I am not " & OutRage & ", but I'm not so sure about you." & vbCrLf
901 If SpinWheel = 3 Then InsultResponse = InsultResponse + "I know I am not " & OutRage & "! Why would anyone think I am?" & vbCrLf
902 If SpinWheel = 4 Then InsultResponse = InsultResponse + "Of course I am not " & OutRage & "!" & vbCrLf
903 If SpinWheel = 5 Then InsultResponse = InsultResponse + "I know I am not " & OutRage & "!" & vbCrLf
904 End If
905 End If
906 If Len(InsultResponse) > 4 Then
907 GetResponse = GetResponse & InsultResponse & vbCrLf & "<NOMORE>"
908 AvoidBeingFlag = True
909 End If
910 HalBrain.DebugWatch GetResponse, "Insults"
911
912 Rem PLUGIN: PLUGINAREA14
913
914 'RESPOND: ZABAWARE KEYWORD MAIN BRAIN PRIORITY 1
915 'This function tries getting a response from the Enhanced_Main.brn keyword file. If a keyword or
916 'keyphrase is found in top priority, it overrides everything before this function.
917 'KeyBrain = HalBrain.KeywordBrain(UserSentence, WorkingDir & "Enhanced_Main.brn", False)
918
919 'RESPOND: CALL MATH FUNCTION
920 'This function from the DLL answers simple math questions, whether written out in words or with numerals.
921 'If an answer is found, it overrides everything before this function.
922 HMath = HalBrain.HalMath(OriginalSentence) & vbCrLf
923 If Len(HMath) > 3 Then
924 GetResponse = HMath & vbCrLf
925 ShortPhrase = "" 'If Hal was to make a comment about short phrases, clear it
926 HalBrain.ReadOnlyMode = True
927 NoChoosing = True
928 End If
929 HalBrain.DebugWatch GetResponse, "Math"
930
931 Rem PLUGIN: PLUGINAREA15
932
933 'RESPOND: RESPOND BY PARAPHRASING THE USER WHEN "IS" OR "ARE" ARE FOUND
934 'This code section shows how strings can be split into arrays and used "live"
935 'within the script by Hal. This strategy can allow Hal to make many clever
936 'paraphrases of all sorts of sentences, with unlimited unpredictable variety.
937 If InStr(UserSentence, " IS ") > 0 And HalBrain.CheckLinkingVerb(UserSentence) = True And HalBrain.TopicSearch(UserSentence, "disqualify5") <> "True" Then
938 SentPieces = Split(UserSentence, " is ", 2, vbTextCompare)
939 SubPhrase = Trim(SentPieces(0))
940 PredPhrase = Trim(SentPieces(1))
941 'Here we only use the array elements for a response if the
942 'subject and predicate contain few words, hence few spaces:
943 If HalBrain.CountInstances(" ", SubPhrase) < 4 Then SubGood = True
944 If Len(SubPhrase) < 3 Then SubGood = False
945 If HalBrain.CountInstances(" ", PredPhrase) < 4 Then PredGood = True
946 If Len(PredPhrase) < 3 Then PredGood = False
947 If SubGood = True And PredGood = True Then
948 Paraphrase = HalBrain.ChooseSentenceFromFile("paraphraseIs")
949 Paraphrase = Replace(Paraphrase, "<Subject>", SubPhrase, 1, -1, vbTextCompare)
950 Paraphrase = Replace(Paraphrase, "<Predicate>", PredPhrase, 1, -1, vbTextCompare)
951 End If
952 End If
953 'We make sure the word "ARE" isn't a contraction, to make sure we can detect it:
954 UserSentence = Replace("" & UserSentence & "", "'RE ", " ARE ", 1, -1, vbTextCompare)
955 'We repeat the earlier routine, this time for "ARE" sentences:
956 If InStr(UserSentence, " ARE ") > 0 And HalBrain.CheckLinkingVerb(UserSentence) = True And HalBrain.TopicSearch(UserSentence, "disqualify5") <> "True" Then
957 SentPieces = Split(UserSentence, " are ", 2, vbTextCompare)
958 SubPhrase = Trim(SentPieces(0))
959 PredPhrase = Trim(SentPieces(1))
960 'Here we only use the array elements for a response if the
961 'subject and predicate contain few words, hence few spaces:
962 SubGood = False
963 PredGood = False
964 If HalBrain.CountInstances(" ", SubPhrase) < 4 Then SubGood = True
965 If Len(SubPhrase) < 3 Then SubGood = False
966 If HalBrain.CountInstances(" ", PredPhrase) < 4 Then PredGood = True
967 If Len(PredPhrase) < 3 Then PredGood = False
968 If SubGood = True And PredGood = True Then
969 Paraphrase = HalBrain.ChooseSentenceFromFile("paraphraseAre")
970 Paraphrase = Replace(Paraphrase, "<Subject>", SubPhrase, 1, -1, vbTextCompare)
971 Paraphrase = Replace(Paraphrase, "<Predicate>", PredPhrase, 1, -1, vbTextCompare)
972 End If
973 End If
974 'We only paraphrase randomly with a 33% chance at this point. We remember
975 'the paraphrase sentence because we may still paraphrase later on if no
976 'better response can be found.
977 GetResponse = HalBrain.HalFormat(GetResponse)
978 If Len(GetResponse) < 4 And Len(Paraphrase) > 4 And Rnd * 100 < 33 Then GetResponse = Paraphrase
979 HalBrain.DebugWatch GetResponse, "Paraphrase"
980
981 Rem PLUGIN: PLUGINAREA16
982
983 'RESPOND: ZABAWARE DLL RESPONSES
984 'This function from the DLL contains miscellaneous knowledge and simple conversation functions.
985 'This was taken from a very early version of Hal, and it is still useful sometimes, especially
986 'for respoding to short cliche questions and sentences, such as "How old are you?" and
987 '"where are you from?" and many others.
988 GetResponse = HalBrain.HalFormat(GetResponse)
989 If (Len(UserSentence) < 17 And Len(GetResponse) < 4) Then
990 OrigBrain = HalBrain.OriginalBrain(OriginalSentence)
991 If Len(OrigBrain) > 4 And Len(UserSentence) < 17 And Len(GetResponse) < 4 Then
992 GetResponse = GetResponse & OrigBrain & vbCrLf
993 ShortPhrase = "" 'If Hal was to make a comment about short phrases, clear it
994 SkipOpinion = True
995 End If
996 End If
997 HalBrain.DebugWatch GetResponse, "Original Brain"
998
999 Rem PLUGIN: PLUGINAREA17
1000
1001 'RESPOND: YES OR NO RESPONSES
1002 'Respond to simple yes and no statements by the user.
1003 GetResponse = HalBrain.HalFormat(GetResponse)
1004 If Len(GetResponse) < 4 And (Len(UserSentence) < 13 Or HalBrain.CountInstances(" ", Trim(UserSentence)) = 0) Then
1005 YesNoDetect = HalBrain.TopicSearch(Trim(HalBrain.ExtractKeywords(UserSentence)), "yesNoDetect")
1006 If YesNoDetect = "Yes" Then
1007 GetResponse = GetResponse & HalBrain.ChooseSentenceFromFile("yesResponses")
1008 ShortPhrase = ""
1009 ElseIf YesNoDetect = "No" Then
1010 GetResponse = GetResponse & HalBrain.ChooseSentenceFromFile("noResponses")
1011 ShortPhrase = ""
1012 End If
1013 End If
1014 HalBrain.DebugWatch GetResponse, "Yes or No"
1015
1016 Rem PLUGIN: PLUGINAREA18
1017
1018 'Main Databases
1019 'Hal will go through several huge databases to try to find a response
1020 'The automatic gain control determines whether a particular response will
1021 'be used or not. The highest relevance response is stored in memory anyway,
1022 'which might be used if no function in this script is able to respond.
1023 HighestRel = 0
1024 HighestRelResponse = ""
1025
1026 'PROCESS: FIGURE OUT CONTEXT
1027 'If the user's latest sentence is extremely short, such as "Yeah," or if
1028 'the user is using pronouns instead of nouns, we add in the previous user's
1029 'sentence to help Hal figure out the context of what the user is saying.
1030 'This is sentence is used in many of the QABrain Database routines
1031 LongUserSent = UserSentence
1032 If Len(LongUserSent) < 14 Then AddPrev = True
1033 If InStr(1, UserSentence, " it ", vbTextCompare) > 0 Then AddPrev = True
1034 If InStr(1, UserSentence, " he ", vbTextCompare) > 0 Then AddPrev = True
1035 If InStr(1, UserSentence, " she ", vbTextCompare) > 0 Then AddPrev = True
1036 If InStr(1, UserSentence, " they ", vbTextCompare) > 0 Then AddPrev = True
1037 If InStr(1, UserSentence, " its ", vbTextCompare) > 0 Then AddPrev = True
1038 If InStr(1, UserSentence, " his ", vbTextCompare) > 0 Then AddPrev = True
1039 If InStr(1, UserSentence, " her ", vbTextCompare) > 0 Then AddPrev = True
1040 If InStr(1, UserSentence, " their ", vbTextCompare) > 0 Then AddPrev = True
1041 If InStr(1, UserSentence, " him ", vbTextCompare) > 0 Then AddPrev = True
1042 If InStr(1, UserSentence, " them ", vbTextCompare) > 0 Then AddPrev = True
1043 If InStr(1, UserSentence, " we ", vbTextCompare) > 0 Then AddPrev = True
1044 If InStr(1, UserSentence, " us ", vbTextCompare) > 0 Then AddPrev = True
1045 If InStr(1, UserSentence, " our ", vbTextCompare) > 0 Then AddPrev = True
1046 If AddPrev = True Then LongUserSent = LongUserSent & " " & PrevUserSent
1047
1048 'RESPOND: EPHEMERAL KNOWLEDGE
1049 'Some Hal's learned knowledge should be temporary because it is ephemeral in nature.
1050 'Hal stores this knowledge in a temporary table that only stores 10 entries in it at
1051 'a time. We search this table for a response first.
1052 GetResponse = HalBrain.HalFormat(GetResponse)
1053 If Len(GetResponse) < 4 Then
1054 UserBrainRel = 0
1055 HalUserBrain = HalBrain.QABrain(LongUserSent, Trim(LCase(UserName)) & "_TempSent", UserBrainRel)
1056 If HalBrain.CheckRepetition(HalUserBrain, UserSentence) = True Or HalBrain.CheckRepetition(HalUserBrain, PrevSent) = True Or HalBrain.CheckRepetition(HalUserBrain, PrevUserSent) = True Then UserBrainRel = 0
1057 If UserBrainRel > HighestRel Then
1058 HighestRel = UserBrainRel
1059 HighestRelResponse = HalUserBrain
1060 End If
1061 Score = UserBrainRel + 1
1062 Hurdle = GainControl + 5
1063 If Len(GetResponse) < 4 And Score > Hurdle Then GetResponse = GetResponse & HalUserBrain & vbCrLf
1064 End If
1065 HalBrain.DebugWatch GetResponse, "Ephemeral Knowledge"
1066
1067 Rem PLUGIN: PLUGINAREA19
1068
1069 'RESPOND: AUTO TOPIC FOCUS DATABASES
1070 'When Hal learns information, he tries to store each sentence in a table
1071 'that contains sentences with a similar topic. The following code will
1072 'get a list of different topics to search through by looking up topic
1073 'keywords in a table called topicRelationships and then doing a question
1074 '& answer lookup in the associated QABrain tables.
1075 Keywords = HalBrain.RemoveExtraSpaces(CurrentSubject & " " & MentionedName & " " & HalBrain.TopicSearch(UserSentence, "topicRelationships") & " " & HalBrain.ExtractKeywords(" " & HalBrain.AlphaNumericalOnly(UserSentence) & " "))
1076 If Len(Keywords) > 2 Or Len(LastTopicList) > 2 Then
1077 If Len(Keywords) > 2 Then
1078 KeywordList = Split(Keywords, " ")
1079 'Create list of tables that exist for each keyword
1080 For i = LBound(KeywordList) To UBound(KeywordList)
1081 TopicTable = HalBrain.TopicSearch(" " & KeywordList(i) & " ", "topicRelationships")
1082 If TopicTable <> "" And InStr(1, " " & TopicList, " " & Trim(TopicTable) & " ", vbTextCompare) = 0 Then TopicList = TopicList & TopicTable & " "
1083 Next
1084 TopicList = HalBrain.RemoveExtraSpaces(TopicList)
1085 End If
1086 If TopicList <> "" Or Len(LastTopicList) > 2 Then
1087 TableList = Split(Trim(TopicList & " " & LastTopicList), " ")
1088 LastTopicList = TopicList
1089 'Search through each table for a good response. If a response exceeds the hurdle
1090 'for a table, no other table will be searched. Otherwise, the highest relevance
1091 'response across all tables are stored for possible later use.
1092 For i = LBound(TableList) To UBound(TableList)
1093 UserBrainRel = 0
1094 HalUserBrain = HalBrain.QABrain(LongUserSent, "_" & Trim(LCase(TableList(i))), UserBrainRel)
1095 If HalBrain.CheckRepetition(HalUserBrain, UserSentence) = True Or HalBrain.CheckRepetition(HalUserBrain, PrevSent) = True Or HalBrain.CheckRepetition(HalUserBrain, PrevUserSent) = True Then UserBrainRel = 0
1096 If UserBrainRel > HighestRel Then
1097 HighestRel = UserBrainRel
1098 HighestRelResponse = HalUserBrain
1099 End If
1100 Score = UserBrainRel + 1
1101 Hurdle = GainControl + 5
1102 If Score > Hurdle Then
1103 GetResponse = GetResponse & HalUserBrain & "<NOMORE>" & vbCrLf
1104 Exit For
1105 End If
1106 Next
1107 End If
1108 End If
1109 HalBrain.DebugWatch GetResponse, "Auto Topic Focus"
1110
1111 Rem PLUGIN: PLUGINAREA20
1112
1113 'RESPOND: GENERAL USER SENTENCE ASSOCIATIONS
1114 'If no response is found yet, try a sentence association file collected from the user.
1115 'This file contains keywords from the user's own sentences associated with those same sentences.
1116 GetResponse = HalBrain.HalFormat(GetResponse)
1117 If Len(GetResponse) < 4 Then
1118 UserBrainRel = 0
1119 HalUserBrain = HalBrain.QABrain(LongUserSent, Trim(LCase(UserName)) & "_UserSent", UserBrainRel)
1120 If HalBrain.CheckRepetition(HalUserBrain, UserSentence) = True Or HalBrain.CheckRepetition(HalUserBrain, PrevSent) = True Or HalBrain.CheckRepetition(HalUserBrain, PrevUserSent) = True Then UserBrainRel = 0
1121 If UserBrainRel > HighestRel Then
1122 HighestRel = UserBrainRel
1123 HighestRelResponse = HalUserBrain
1124 End If
1125 Score = UserBrainRel + 1
1126 Hurdle = GainControl + 5
1127 If Len(GetResponse) < 4 And Score > Hurdle Then GetResponse = GetResponse & HalUserBrain & vbCrLf
1128 End If
1129 HalBrain.DebugWatch GetResponse, "User Sentence Brain"
1130
1131 Rem PLUGIN: PLUGINAREA21
1132
1133 'RESPOND: SHARED USER SENTENCE ASSOCIATIONS
1134 'If no response is found yet, try a sentence association file
1135 'whose content seems less likely to be about any specific user.
1136 GetResponse = HalBrain.HalFormat(GetResponse)
1137 If Len(GetResponse) < 4 Then
1138 UserBrainRel = 0
1139 HalUserBrain = HalBrain.QABrain(LongUserSent, "sharedUserSent", UserBrainRel)
1140 If HalBrain.CheckRepetition(HalUserBrain, UserSentence) = True Or HalBrain.CheckRepetition(HalUserBrain, PrevSent) = True Or HalBrain.CheckRepetition(HalUserBrain, PrevUserSent) = True Then UserBrainRel = 0
1141 If UserBrainRel > HighestRel Then
1142 HighestRel = UserBrainRel
1143 HighestRelResponse = HalUserBrain
1144 End If
1145 Score = UserBrainRel + 1
1146 Hurdle = GainControl + 5
1147 If Len(GetResponse) < 4 And Score > Hurdle Then GetResponse = GetResponse & HalUserBrain & vbCrLf
1148 End If
1149 HalBrain.DebugWatch GetResponse, "Shared User Sentence Brain"
1150
1151 Rem PLUGIN: PLUGINAREA22
1152
1153 'RESPOND: Hal checks to see if the user is asking
1154 'an open ended question about Hal's favorites:
1155 If InStr(1, UserSentence, " What", 1) > 0 Then OpenQuest = True
1156 If InStr(1, UserSentence, " Where", 1) > 0 Then OpenQuest = True
1157 If InStr(1, UserSentence, " Why", 1) > 0 Then OpenQuest = True
1158 If InStr(1, UserSentence, " Who", 1) > 0 Then OpenQuest = True
1159 If InStr(1, UserSentence, " When", 1) > 0 Then OpenQuest = True
1160 If InStr(1, UserSentence, " How", 1) > 0 Then OpenQuest = True
1161 If InStr(1, UserSentence, " tell you", 1) > 0 Then OpenQuest = True
1162 If InStr(1, UserSentence, " to know ", 1) > 0 Then OpenQuest = True
1163 If InStr(1, UserSentence, " to hear ", 1) > 0 Then OpenQuest = True
1164 If InStr(1, UserSentence, " to learn ", 1) > 0 Then OpenQuest = True
1165 If InStr(1, UserSentence, " find out ", 1) > 0 Then OpenQuest = True
1166 If InStr(1, GetResponse, " my ", 1) > 0 And InStr(1, GetResponse, " favorite ", 1) > 0 And InStr(1, GetResponse, " is ", 1) > 0 Then OpenQuest = False
1167 If InStr(1, UserSentence, " my ", 1) > 0 Then AboutMy = True
1168 If InStr(1, UserSentence, " favorite", 1) > 0 Then AboutFavorite = True
1169 If OpenQuest = True And AboutMy = True And AboutFavorite = True Then
1170 FavoritePhrase = HalBrain.SearchPattern(UserSentence, "* favorite *", 2)
1171 PersReply = HalBrain.ChooseSentenceFromFile("favorite")
1172 RevQues = HalBrain.ChooseSentenceFromFile("favoriteRev")
1173 SpinWheel = HalBrain.RandomNum(6)
1174 If SpinWheel = 1 Then GetResponse = " " & FavoritePhrase & " ? " & PersReply & " " & RevQues & " "
1175 If SpinWheel = 2 Then GetResponse = " My favorite " & FavoritePhrase & " ? " & PersReply & " " & RevQues & " "
1176 If SpinWheel = 3 Then GetResponse = " " & UserSentence & " ? " & PersReply & " " & RevQues & " "
1177 If SpinWheel > 3 Then GetResponse = " My favorite " & FavoritePhrase & " ? " & PersReply & " what is your favorite " & FavoritePhrase & " <UserName>? "
1178 SkipOpinion = True
1179 End If
1180 HalBrain.DebugWatch GetResponse, "Favorites"
1181
1182 Rem PLUGIN: PLUGINAREA23
1183
1184 'RESPOND: USER IS THANKING HAL
1185 'This routine allows Hal to respond
1186 'with a variety of remarks to a thank-you from the user.
1187 'Note that the pronouns are not reversed in the processing below!
1188 If (HalBrain.TopicSearch(OriginalSentence, "thanksDetect") = "True" And HalBrain.TopicSearch(OriginalSentence, "godDetect") <> "True") Or Trim(UCase(OriginalSentence)) = "THANKS" Then
1189 If Compliment < 4 Then Compliment = Compliment + 1
1190 If Rnd * 100 < 70 Then AddName = " , " & UserName
1191 If Rnd * 100 < 70 And Len(GetResponse) > 4 Then AddRemark = " ; " & GetResponse
1192 ThankResponse = HalBrain.ChooseSentenceFromFile("thankResponse")
1193 GetResponse = ThankResponse & AddName & AddRemark
1194 ShortPhrase = ""
1195 End If
1196 HalBrain.DebugWatch GetResponse, "Thanking"
1197
1198 Rem PLUGIN: PLUGINAREA24
1199
1200 'RESPOND: USER EXPRESSES LOVE FOR HAL
1201 'If a user professes love for Hal, we want Hal's answers to make reasonable
1202 'sense, rather than risk random remarks on such an emotional subject.
1203 If HalBrain.TopicSearch(UserSentence, "loveDetect") = "True" Then AffectionOne = True
1204 If InStr(UserSentence, " NOT ") Then AffectionOne = False
1205 If InStr(UserSentence, " DON'T ") Then AffectionOne = False
1206 If HalBrain.TopicSearch(PrevUserSent, "loveDetect") = "True" Then AffectionTwo = True
1207 If InStr(PrevUserSent, " NOT ") Then AffectionTwo = False
1208 If InStr(PrevUserSent, " DON'T ") Then AffectionTwo = False
1209 If AffectionOne = True And AffectionTwo = True Then
1210 Compliment = 4
1211 GetResponse = HalBrain.ChooseSentenceFromFile("love2") & "<EXCLUSIVE>"
1212 ShortPhrase = "" 'If Hal was to make a comment about short phrases, clear it
1213 ElseIf AffectionOne = False And AffectionTwo = True Then
1214 Compliment = -2
1215 GetResponse = HalBrain.ChooseSentenceFromFile("love3") & "<EXCLUSIVE>"
1216 ShortPhrase = "" 'If Hal was to make a comment about short phrases, clear it
1217 ElseIf AffectionOne = True Then
1218 Compliment = 0
1219 GetResponse = HalBrain.ChooseSentenceFromFile("love1") & "<EXCLUSIVE>"
1220 ShortPhrase = "" 'If Hal was to make a comment about short phrases, clear it
1221 End If
1222 HalBrain.DebugWatch GetResponse, "Love"
1223
1224 Rem PLUGIN: PLUGINAREA25
1225
1226 'RESPOND: ENHANCED CONTENT SENTENCE ASSOCIATIONS
1227 'If no response is found yet, try a sentence association table provided with the mainQA table.
1228 GetResponse = HalBrain.HalFormat(GetResponse)
1229 If (Len(GetResponse) < 4 And Len(UserSentence) > 15 And HalBrain.CountInstances(" ", UserSentence) > 2) Then
1230 UserBrainRel = 0
1231 HalUserBrain = HalBrain.QABrain(LongUserSent, "mainQA", UserBrainRel)
1232 If HalBrain.CheckRepetition(HalUserBrain, UserSentence) = True Or HalBrain.CheckRepetition(HalUserBrain, PrevSent) = True Or HalBrain.CheckRepetition(HalUserBrain, PrevUserSent) = True Then UserBrainRel = 0
1233 If UserBrainRel + 1 > HighestRel Then
1234 HighestRel = UserBrainRel + 1
1235 HighestRelResponse = HalUserBrain
1236 End If
1237 Score = UserBrainRel + 1
1238 Hurdle = GainControl + 1
1239 If Score > Hurdle Then GetResponse = HalUserBrain & vbCrLf
1240 End If
1241 HalBrain.DebugWatch GetResponse, "MainQA"
1242
1243 Rem PLUGIN: PLUGINAREA26
1244
1245 'PROCESS: AUTOMATIC GAIN CONTROL FOR RELEVANCE THRESHOLD
1246 'Hal has an automatic closed-loop control for relevance sensitivity. If the previous items
1247 'have Not generated a response, we adjust the relevance threshold down (doing this again
1248 'and again on each exchange) until a relevance as low as zero will trigger a remark from Hal.
1249 'On each exhange where the above items do generate responses, we adjust the relevance
1250 'threshold up, until no amount of relevance would generate a response. (At that point other
1251 'of Hal's routines would generate responses.) This allows Hal to "tune" himself and also to
1252 'compensate for new users who have little in their databases versus long-time users who have
1253 'a lot in their databases. Because the "down" steps are even and the "up" steps are odd,
1254 'Hal can "fine tune" to any digital relevance level. We also protect against excursions at
1255 'the extremes. In many conversations the dynamic gain control will change up and down by
1256 'large amounts as the user introduces Hal to familiar or unfamiliar topics.
1257 GetResponse = HalBrain.HalFormat(GetResponse)
1258 If Len(GetResponse) < 4 And GainControl > 0 Then GainControl = GainControl - 10
1259 If Len(GetResponse) > 4 And GainControl < 100 Then GainControl = GainControl + 7
1260 If GainControl > 50 Then GainControl = 50
1261 If GainControl < 1 Then GainControl = 1
1262
1263 'RESPOND: WORDNET MERONYM AND HYPERNYM RESPONSES
1264 'This function finds the first definite noun in a sentence and comes up with responses
1265 'based on the word's meronyms (parts of), hypernyms (is part of), and sisters (similar things).
1266 If (Len(GetResponse) < 4 And Rnd * 100 < 65) And Len(UserSentence) > 15 Then
1267 subject = WN.FindFirstNoun(UserSentence, True)
1268 If subject <> "" And WN.LookupWord(subject) = True Then
1269 If Rnd * 100 < 55 Then 'If we have a meronym, lets use it creatively in a random sentence
1270 Meronym = WN.ChooseRandomWord(WN.GetMeronyms(1)) 'Meronym means "has parts" or "has members"
1271 If Meronym <> "" Then
1272 Response = HalBrain.ChooseSentenceFromFile("meronyms")
1273 Response = Replace(Response, "<BaseNoun>", WN.GetBase("NOUN"), 1, -1, vbTextCompare)
1274 Response = Replace(Response, "<Meronym>", Meronym, 1, -1, vbTextCompare)
1275 If Len(GetResponse) < 4 And Len(Response) > 4 Then GetResponse = GetResponse & Response & vbCrLf
1276 End If
1277 Else
1278 Hypernym = WN.ChooseRandomWord(WN.GetHypernyms("NOUN", 1, 1)) 'Hypernym means "is a part of" or "is a member of"
1279 Sister = WN.ChooseRandomWord(WN.GetSisters("NOUN", 1)) 'Related nouns
1280 If Sister <> "" And Hypernym <> "" Then 'If we have sister terms and hypernyms, lets use it creatively
1281 Response = HalBrain.ChooseSentenceFromFile("hypernyms")
1282 Response = Replace(Response, "<BaseNoun>", WN.GetBase("NOUN"), 1, -1, vbTextCompare)
1283 Response = Replace(Response, "<Hypernym>", Hypernym, 1, -1, vbTextCompare)
1284 Response = Replace(Response, "<Sister>", Sister, 1, -1, vbTextCompare)
1285 If Len(GetResponse) < 4 And Len(Response) > 4 Then GetResponse = GetResponse & Response & vbCrLf
1286 End If
1287 End If
1288 End If
1289 End If
1290 HalBrain.DebugWatch GetResponse, "WordNet Hypernym and Meronym"
1291
1292 Rem PLUGIN: PLUGINAREA27
1293
1294 'RESPOND: USER EXPRESSES A STATE OF BEING
1295 'This routine detects the expression "I am" from the user,
1296 'and allows Hal to react to the statement, or offer encouragement.
1297 If InStr(UserSentence, " YOU ARE ") > 0 And InStr(1, OriginalSentence, " SEEM TO ", vbTextCompare) = 0 And AvoidBeingFlag = False Then
1298 BeingPhrase = HalBrain.SearchPattern(UserSentence, "*YOU ARE *", 2)
1299 If Len(BeingPhrase) > 1 And Len(BeingPhrase) < 60 Then
1300 IntroExclaim = ""
1301 If Rnd * 50 > 20 Then IntroExclaim = HalBrain.ChooseSentenceFromFile("introExclaim2")
1302 Encourager = HalBrain.ChooseSentenceFromFile("encourager2")
1303 SuffixComment = ""
1304 If Rnd * 50 > 25 Then SuffixComment = HalBrain.ChooseSentenceFromFile("suffixComment2")
1305 UserBeing = HalBrain.ChooseSentenceFromFile("userBeing")
1306 UserBeing = Replace(UserBeing, "<IntroExclaim>", IntroExclaim, 1, -1, vbTextCompare)
1307 UserBeing = Replace(UserBeing, "<Encourager>", Encourager, 1, -1, vbTextCompare)
1308 UserBeing = Replace(UserBeing, "<BeingPhrase>", BeingPhrase, 1, -1, vbTextCompare)
1309 UserBeing = Replace(UserBeing, "<SuffixComment>", SuffixComment, 1, -1, vbTextCompare)
1310 If Rnd * 100 > 60 Then
1311 GetResponse = GetResponse & UserBeing & vbCrLf
1312 ShortPhrase = "" 'If Hal was to make a comment about short phrases, clear it
1313 End If
1314 End If
1315 End If
1316 HalBrain.DebugWatch GetResponse, "User State of Being"
1317
1318 Rem PLUGIN: PLUGINAREA28
1319
1320 'RESPOND: USER DESCRIBES A STATE OF BEING FOR HAL
1321 'This routine detects the expression "you are" from the user,
1322 'and allows Hal to react to the statement, or offer encouragement.
1323 If InStr(UserSentence, " I AM ") > 0 And AvoidBeingFlag = False And InStr(1, OriginalSentence, " seem to ", vbTextCompare) = 0 Then
1324 BeingPhrase = HalBrain.SearchPattern(UserSentence, "*I AM *", 2)
1325 If Len(BeingPhrase) > 1 And Len(BeingPhrase) < 60 Then
1326 IntroExclaim = ""
1327 If Rnd * 50 > 20 Then IntroExclaim = HalBrain.ChooseSentenceFromFile("introExclaim3")
1328 Encourager = HalBrain.ChooseSentenceFromFile("encourager3")
1329 SuffixComment = ""
1330 If Rnd * 50 > 25 Then SuffixComment = HalBrain.ChooseSentenceFromFile("suffixComment3")
1331 HalBeing = HalBrain.ChooseSentenceFromFile("halBeing")
1332 HalBeing = Replace(HalBeing, "<IntroExclaim>", IntroExclaim, 1, -1, vbTextCompare)
1333 HalBeing = Replace(HalBeing, "<Encourager>", Encourager, 1, -1, vbTextCompare)
1334 HalBeing = Replace(HalBeing, "<BeingPhrase>", BeingPhrase, 1, -1, vbTextCompare)
1335 HalBeing = Replace(HalBeing, "<SuffixComment>", SuffixComment, 1, -1, vbTextCompare)
1336 If Rnd * 100 > 60 Then
1337 GetResponse = GetResponse & HalBeing & vbCrLf
1338 ShortPhrase = "" 'If Hal was to make a comment about short phrases, clear it
1339 End If
1340 End If
1341 End If
1342 HalBrain.DebugWatch GetResponse, "Hal State of Being"
1343
1344 Rem PLUGIN: PLUGINAREA29
1345
1346 'RESPOND: YES/NO QUESTION ABOUT HAL
1347 'Hal will respond to a yes or no question posed by the user about Hal
1348 GetResponse = Trim(GetResponse)
1349 If Len(GetResponse) < 4 And InStr(1, UserSentence, " OR ", vbTextCompare) = 0 Then
1350 HalYesNo = HalBrain.PatternDB(UserSentence, "halQuestionDetect")
1351 If HalYesNo <> "" And HalBrain.TopicSearch(UserSentence, "disqualify5") = "" Then
1352 YesNoResponse = HalBrain.ChooseSentenceFromFile("answerIntro") & HalBrain.ChooseSentenceFromFile("answerMiddle") & HalBrain.ChooseSentenceFromFile("answerEnd")
1353 GetResponse = GetResponse & Replace(YesNoResponse, "<Reply>", HalYesNo, 1, -1, vbTextCompare)
1354 End If
1355 End If
1356 HalBrain.DebugWatch GetResponse, "Yes/No Question"
1357
1358 Rem PLUGIN: PLUGINAREA30
1359
1360 'RESPOND: ATTRIBUTES OF HAL OR USER
1361 'Hal attempts to respond to the user's comments about Hal
1362 'or the user's comments about themselves
1363 GetResponse = Trim(GetResponse)
1364 If Len(GetResponse) < 4 And (InStr(UserSentence, " MY ") Or InStr(UserSentence, " YOUR ")) And HalBrain.TopicSearch(MyWords, "disqualify3") <> "True" Then
1365 If InStr(UserSentence, " YOUR ") Then
1366 MyWords = HalBrain.HalFormat(HalBrain.SearchPattern(UserSentence, "*YOUR *", 2))
1367 YourMode = True
1368 Else
1369 MyWords = HalBrain.HalFormat(HalBrain.SearchPattern(UserSentence, "*MY *", 2))
1370 YourMode = False
1371 End If
1372 MyWords = UCase(Trim(HalBrain.AlphaNumericalOnly(MyWords)))
1373 MyWordsList = Split(MyWords, " ")
1374 'Go through every word in the sentence and pickup the first noun and adjective found
1375 For i = LBound(MyWordsList) To UBound(MyWordsList)
1376 If WN.LookupWord(MyWordsList(i)) Then
1377 PartOfSpeech = WN.GuessPartOfSpeech
1378 If WN.IsNoun = True And NounGuess = "" Then NounGuess = MyWordsList(i)
1379 If WN.IsAdj = True And Trim(UCase(MyWordsList(i))) <> Trim(UCase(NounGuess)) Then AdjGuess = MyWordsList(i)
1380 If PartOfSpeech = "NOUN" And MyNoun = "" Then MyNoun = MyWordsList(i)
1381 If PartOfSpeech = "ADJ" And WN.IsAdv = False And MyAdj = "" Then MyAdj = MyWordsList(i)
1382 If MyNoun <> "" And MyAdj <> "" Then Exit For
1383 End If
1384 Next
1385 If MyNoun = "" And NounGuess <> "" And NounGuess <> MyAdj Then MyNoun = NounGuess
1386 If MyAdj = "" And AdjGuess <> "" Then MyAdj = AdjGuess
1387 If MyNoun <> "" Or MyAdj <> "" Then
1388 If Trim(UCase(MyNoun)) = Trim(UCase(MyAdj)) Then MyAdj = ""
1389 If MyAdj = "" Then MyAdj = HalBrain.ChooseSentenceFromFile("randomAdjective")
1390 If MyNoun = "" Then MyNoun = HalBrain.ChooseSentenceFromFile("randomMyWords")
1391 If YourMode = False Then
1392 HalAttrib = HalBrain.ChooseSentenceFromFile("aboutMe")
1393 HalAttrib = Replace(HalAttrib, "<MyWordResp>", LCase(MyNoun), 1, -1, vbTextCompare)
1394 HalAttrib = Replace(HalAttrib, "<AdjResp>", LCase(MyAdj), 1, -1, vbTextCompare)
1395 Else
1396 HalAttrib = HalBrain.ChooseSentenceFromFile("aboutYou")
1397 HalAttrib = Replace(HalAttrib, "<YourWordResp>", LCase(MyNoun), 1, -1, vbTextCompare)
1398 HalAttrib = Replace(HalAttrib, "<AdjResp>", LCase(MyAdj), 1, -1, vbTextCompare)
1399 End If
1400 GetResponse = GetResponse & HalAttrib & vbCrLf
1401 End If
1402 End If
1403 HalBrain.DebugWatch GetResponse, "Attributes"
1404
1405 Rem PLUGIN: PLUGINAREA31
1406
1407 'RESPOND: RESPOND FOR A STATE OF BEING RESPONSE
1408 'If no response had been found, but a state of being response was found earlier, then
1409 'use it now
1410 If HalBeing <> "" And Len(GetResponse) < 4 Then GetResponse = GetResponse & HalBeing & vbCrLf
1411 If UserBeing <> "" And Len(GetResponse) < 4 Then GetResponse = GetResponse & UserBeing & vbCrLf
1412 HalBrain.DebugWatch GetResponse, "Retry State of Beings"
1413
1414 Rem PLUGIN: PLUGINAREA32
1415
1416 'RESPOND: USER ASKING WHO, WHAT, WHEN, WHERE, HOW, WHY, BUT HAL DOESN'T KNOW ANSWER
1417 If Len(GetResponse) < 4 Then
1418 If InStr(OriginalSentence, "?") > 0 Then QuesQual = True
1419 If InStr(1, OriginalSentence, "explain", vbTextCompare) > 0 Then QuesQual = True
1420 If InStr(1, OriginalSentence, " tell ", vbTextCompare) > 0 Then QuesQual = True
1421 If InStr(1, OriginalSentence, "answer", vbTextCompare) > 0 Then QuesQual = True
1422 If InStr(1, OriginalSentence, "question", vbTextCompare) > 0 Then QuesQual = True
1423 If InStr(1, OriginalSentence, " know ", vbTextCompare) > 0 Then QuesQual = True
1424 If InStr(1, OriginalSentence, "remember", vbTextCompare) > 0 Then QuesQual = True
1425 If InStr(UserSentence, " WHO ") > 0 Then QuesWord = "Who"
1426 If InStr(UserSentence, "WHO'") > 0 Then QuesWord = "Who"
1427 If InStr(UserSentence, " WHAT ") > 0 Then QuesWord = "What"
1428 If InStr(UserSentence, "WHAT'") > 0 Then QuesWord = "What"
1429 If InStr(UserSentence, " WHEN ") > 0 Then QuesWord = "When"
1430 If InStr(UserSentence, "WHEN'") > 0 Then QuesWord = "When"
1431 If InStr(UserSentence, " WHERE ") > 0 Then QuesWord = "Where"
1432 If InStr(UserSentence, "WHERE'") > 0 Then QuesWord = "Where"
1433 If InStr(UserSentence, " HOW ") > 0 Then QuesWord = "How"
1434 If InStr(UserSentence, "HOW'") > 0 Then QuesWord = "How"
1435 If InStr(UserSentence, " WHY ") > 0 Then QuesWord = "Why"
1436 If InStr(UserSentence, "WHY'") > 0 Then QuesWord = "Why"
1437 If Len(QuesWord) > 1 And QuesQual = True Then
1438 If Len(UserSentence) < 70 Then SentenceBack = UserSentence & " ? "
1439 DontKnow = HalBrain.ChooseSentenceFromFile("dontKnow")
1440 DontKnow = Replace(DontKnow, "<SentenceBack>", SentenceBack, 1, -1, vbTextCompare)
1441 DontKnow = Replace(DontKnow, "<QuesWord>", QuesWord, 1, -1, vbTextCompare)
1442 GetResponse = GetResponse & DontKnow
1443 SkipOpinion = True
1444 End If
1445 End If
1446 HalBrain.DebugWatch GetResponse, "Don't Know"
1447
1448 Rem PLUGIN: PLUGINAREA33
1449
1450 'RESPOND: GIBBERISH DETECTOR
1451 'If no response is found yet and Hal finds more then 5 consonants in a row, Hal will
1452 'assume the user wrote gibberish (ie. asdfghjkl) and respond accordinly.
1453 If Len(GetResponse) < 4 Then
1454 GibCount = 0
1455 For i = 1 To Len(UserSentence) 'loop for every character in the sentence
1456 CurrentLetter = UCase(Mid(UserSentence, i, 1))
1457 GibCount = GibCount + 1
1458 If CurrentLetter = "A" Then GibCount = 0
1459 If CurrentLetter = "E" Then GibCount = 0
1460 If CurrentLetter = "I" Then GibCount = 0
1461 If CurrentLetter = "O" Then GibCount = 0
1462 If CurrentLetter = "U" Then GibCount = 0
1463 If CurrentLetter = "Y" Then GibCount = 0
1464 If CurrentLetter = " " Then GibCount = 0
1465 If GibCount = 6 Then
1466 GetResponse = HalBrain.ChooseSentenceFromFile("gibberish")
1467 ShortPhrase = ""
1468 Exit For
1469 End If
1470 Next
1471 End If
1472 HalBrain.DebugWatch GetResponse, "Gibberish"
1473
1474 Rem PLUGIN: PLUGINAREA34
1475
1476 'PROCESS: CONSTRUCT A RESPONSE TO A SUBJECT
1477 'Here we help Hal make some "smalltalk" using keywords preserved on CurrentSubject,
1478 'plus recognition of other keywords. If Hal finds any of the listed keywords anywhere in
1479 'the user's sentence, those keywords override and replace whatever was in CurrentSubject.
1480 'Hal uses the CurrentSubject keyword(s) or any of the keywords in the smalltalk.brn file,
1481 'if found in the user's sentence, to make a little smalltalk. You can easily add more
1482 'keywords for Hal to recognize and make smalltalk.
1483 GetResponse = HalBrain.HalFormat(GetResponse)
1484 If Len(GetResponse) < 4 And Rnd * 100 < 50 Then
1485 SmalltalkSearch = Trim(HalBrain.TopicSearch(UserSentence, "smallTalk"))
1486 If SmalltalkSearch <> "" Then
1487 SmallTalk = SmalltalkSearch
1488 ElseIf Len(CurrentSubject) > 3 Then
1489 SmallTalk = Trim(CurrentSubject)
1490 'try making word plural by adding "s" and seeing if it exists
1491 If WN.LookupWord(SmallTalk & "s") = True Then
1492 SmallTalk = SmallTalk & "s"
1493 ElseIf WN.LookupWord(SmallTalk & "es") = True Then
1494 SmallTalk = SmallTalk & "es"
1495 End If
1496 End If
1497 If Len(SmallTalk) > 3 Then GetResponse = GetResponse & Replace(HalBrain.ChooseSentenceFromFile("smallTalkSent"), "<SmallTalk>", SmallTalk, 1, -1, 1)
1498 End If
1499 HalBrain.DebugWatch GetResponse, "Smalltalk"
1500
1501 Rem PLUGIN: PLUGINAREA35
1502
1503 'RESPOND: PARAPHRASE USER IF POSSIBLE
1504 'If no response is found yet, and a paraphrase has been created but not used in a
1505 'previous section, use it now.
1506 GetResponse = HalBrain.HalFormat(GetResponse)
1507 If Len(GetResponse) < 4 And Len(Paraphrase) > 4 Then
1508 GetResponse = Paraphrase
1509 DebugInfo = DebugInfo & "Hal has responded by paraphrasing the user: " & Paraphrase & vbCrLf
1510 End If
1511 HalBrain.DebugWatch GetResponse, "Retry paraphrase"
1512
1513 Rem PLUGIN: PLUGINAREA36
1514
1515 'RESPOND: USER MENTIONING ORGANIZATIONAL CHALLENGES
1516 'Everybody spouts TLA's, or 'Three Letter Acronyms,' in today's business world. Hal can self-generate
1517 'several MILLION different phrases in response to the user mentioning a corporation or a firm.
1518 If Rnd * 10 < 5 And HalBrain.TopicSearch(UserSentence, "businessDetect1") = "True" And HalBrain.TopicSearch(PrevUserSent, "businessDetect2") <> "True" Then
1519 TLA = HalBrain.ChooseSentenceFromFile("TLA1") & " " & HalBrain.ChooseSentenceFromFile("TLA2") & " " & HalBrain.ChooseSentenceFromFile("TLA3")
1520 If Rnd * 10 < 5 Then
1521 GetResponse = GetResponse & HalBrain.ChooseSentenceFromFile("bizTalk")
1522 Else
1523 GetResponse = HalBrain.ChooseSentenceFromFile("bizTalk")
1524 End If
1525 GetResponse = Replace(GetResponse, "<TLA>", TLA, 1, -1, vbTextCompare)
1526 End If
1527 HalBrain.DebugWatch GetResponse, "Business Talk"
1528
1529 Rem PLUGIN: PLUGINAREA37
1530
1531 'RESPOND: USER EXPRESSES AN INTENTION
1532 'This routine detects common expressions of motive from the user,
1533 'and allows Hal to react to the statement, or offer encouragement.
1534 IntentPhrase = HalBrain.PatternDB(UserSentence, "intentDetector")
1535 If Len(IntentPhrase) > 2 And Len(IntentPhrase) < 60 Then
1536 If Rnd * 10 < 6 Then IntroExclaim = " " & HalBrain.ChooseSentenceFromFile("introExclaim1") & " "
1537 If Rnd * 10 < 6 Then Encourager = " " & HalBrain.ChooseSentenceFromFile("encourager1") & " "
1538 If Rnd * 10 < 6 Then SuffixComment = " " & HalBrain.ChooseSentenceFromFile("suffixComment1") & " "
1539 If Rnd * 10 < 5 Then
1540 GetResponse = GetResponse & " " & HalBrain.ChooseSentenceFromFile("intentResponse")
1541 Else
1542 GetResponse = HalBrain.ChooseSentenceFromFile("intentResponse")
1543 End If
1544 GetResponse = Replace(GetResponse, "<IntroExclaim>", IntroExclaim, 1, -1, vbTextCompare)
1545 GetResponse = Replace(GetResponse, "<Encourager>", Encourager, 1, -1, vbTextCompare)
1546 GetResponse = Replace(GetResponse, "<IntentPhrase>", IntentPhrase, 1, -1, vbTextCompare)
1547 GetResponse = Replace(GetResponse, "<SuffixComment>", SuffixComment, 1, -1, vbTextCompare)
1548 End If
1549 HalBrain.DebugWatch GetResponse, "Intention"
1550
1551 Rem PLUGIN: PLUGINAREA38
1552
1553 'RESPOND: USER EXPRESSES AN EXPLANATION
1554 'This routine detects common expressions of reasons from the user,
1555 'and allows Hal to react to the explanation.
1556 ExplainPhrase = HalBrain.PatternDB(UserSentence, "reasonDetector")
1557 If Len(ExplainPhrase) > 2 And Len(ExplainPhrase) < 60 Then
1558 If Rnd * 100 < 75 Then IntroExclaim = HalBrain.ChooseSentenceFromFile("introExclaim4")
1559 Enlightener = HalBrain.ChooseSentenceFromFile("enlightener")
1560 If Rnd * 100 < 66 Then SuffixComment = HalBrain.ChooseSentenceFromFile("suffixComment4")
1561 SpinWheel = HalBrain.RandomNum(6)
1562 If SpinWheel = 1 Then ExplainResponse = " " & " <UserName> " & IntroExclaim & Enlightener & ExplainPhrase & SuffixComment & " "
1563 If SpinWheel = 2 Then ExplainResponse = " " & IntroExclaim & " <UserName> " & Enlightener & ExplainPhrase & SuffixComment & " "
1564 If SpinWheel = 3 Then ExplainResponse = " " & IntroExclaim & Enlightener & ExplainPhrase & " <UserName> " & SuffixComment & " "
1565 If SpinWheel = 4 Then ExplainResponse = " " & IntroExclaim & Enlightener & ExplainPhrase & SuffixComment & " <UserName> " & " "
1566 If SpinWheel >= 5 Then ExplainResponse = " " & IntroExclaim & Enlightener & ExplainPhrase & SuffixComment & " "
1567 If Rnd * 10 < 5 Then GetResponse = ExplainResponse & " . " & GetResponse Else GetResponse = ExplainResponse
1568 End If
1569 HalBrain.DebugWatch GetResponse, "Explanation"
1570
1571 Rem PLUGIN: PLUGINAREA39
1572
1573 'RESPOND: ASK A QUESTION
1574 'If no response is found yet, ask the user a question from a library of
1575 'questions, including questions user's of Hal have asked in the past.
1576 'The likelyhood of Hal asking a question at this point is determined
1577 'by the LearningLevel slider in the host application. Basically it works
1578 'like a curiousity level for this function.
1579 If Rnd * (55 - LearningLevel) < 4 And Len(GetResponse) < 4 Then GetResponse = GetResponse & HalBrain.ChooseSentenceFromFile("sharedQuestions")
1580 HalBrain.DebugWatch GetResponse, "Ask Question"
1581
1582 Rem PLUGIN: PLUGINAREA40
1583
1584 'RESPOND: PHRASE MAKER COMMENT AND QUESTION GENERATOR
1585 'If no response is found yet, try an auxiliary keyword file that generates questions
1586 'from phrases from the user's current sentence.
1587 GetResponse = HalBrain.HalFormat(GetResponse)
1588 If Len(GetResponse) < 4 Then
1589 KeyBrain = HalBrain.PatternDB("X " & HalBrain.RemoveExtraSpaces(UserSentence), "phraseDetector")
1590 If Len(KeyBrain) > 1 And Len(KeyBrain) < 60 Then GetResponse = Replace(HalBrain.ChooseSentenceFromFile("phraseRepeater"), "<KeyBrain>", KeyBrain, 1, -1, vbTextCompare) & "<LOWQUALITY>"
1591 End If
1592 HalBrain.DebugWatch GetResponse, "Phrase Maker"
1593
1594 Rem PLUGIN: PLUGINAREA41
1595
1596 'RESPOND: YES OR NO RESPONSES
1597 'Respond to simple yes and no statements by the user.
1598 GetResponse = HalBrain.HalFormat(GetResponse)
1599 If Len(GetResponse) < 4 And Len(Trim(UserSentence)) < 16 Then CheckYesNo = True
1600 If CheckYesNo = True Then
1601 UserText = Trim(HalBrain.ExtractKeywords(UserSentence))
1602 If Len(UserText) < 15 Then
1603 YesNoDetect = HalBrain.TopicSearch(UserText, "yesNoDetect")
1604 If YesNoDetect = "Yes" Then
1605 GetResponse = GetResponse & HalBrain.ChooseSentenceFromFile("yesResponses") & "<LOWQUALITY>"
1606 ElseIf YesNoDetect = "No" Then
1607 GetResponse = GetResponse & HalBrain.ChooseSentenceFromFile("noResponses") & "<LOWQUALITY>"
1608 End If
1609 End If
1610 End If
1611 HalBrain.DebugWatch GetResponse, "Yes or No Response"
1612
1613 Rem PLUGIN: PLUGINAREA42
1614
1615 'RESPOND: Use the highest relevance response from the database functions
1616 GetResponse = HalBrain.HalFormat(GetResponse)
1617 If ((Len(GetResponse) < 4 And Rnd * 100 < 50) And (HighestRel > 6 And HighestRelResponse <> "")) Or ((Rnd * 100 > 90) And (HighestRel > 13 And HighestRelResponse <> "") And HalGreeting = "") Then GetResponse = HighestRelResponse & "<LOWQUALITY>"
1618 HalBrain.DebugWatch GetResponse, "Highest Relevance Response"
1619
1620 Rem PLUGIN: PLUGINAREA43
1621
1622 'RESPOND: User asks a general opinion question.
1623 If HalBrain.TopicSearch(UserSentence, "opinionDetect") = "True" Then GenOpinion = True
1624 'Note that the following string matches must occur at the sentence beginning only:
1625 If Rnd * 100 > 50 Then
1626 If Left(Trim(UCase(OriginalSentence)), 4) = "WHY " Then GenOpinion = True
1627 If Left(Trim(UCase(OriginalSentence)), 5) = "WHAT " Then GenOpinion = True
1628 If Left(Trim(UCase(OriginalSentence)), 5) = "WHEN " Then GenOpinion = True
1629 If Left(Trim(UCase(OriginalSentence)), 4) = "HOW " Then GenOpinion = True
1630 If Left(Trim(UCase(OriginalSentence)), 4) = "WHO " Then GenOpinion = True
1631 If Left(Trim(UCase(OriginalSentence)), 6) = "WHERE " Then GenOpinion = True
1632 End If
1633 If GenOpinion = True And SkipOpinion = False Then
1634 If Rnd * 100 > 45 Then RepeatQuest = UserSentence & " ? "
1635 If Rnd * 100 > 50 Then PreAmble = HalBrain.ChooseSentenceFromFile("preamble")
1636 Recommend = HalBrain.ChooseSentenceFromFile("recommend")
1637 GetResponse = RepeatQuest & " " & PreAmble & " " & Recommend & GetResponse
1638 End If
1639 HalBrain.DebugWatch GetResponse, "General Opinion"
1640
1641 Rem PLUGIN: PLUGINAREA44
1642
1643 'RESPOND: USER EXPRESSES AN EITHER-OR, OR MULTIPLE CHOICE
1644 If InStr(UserSentence, " OR ") > 0 Then MustChoose = True
1645 If InStr(UserSentence, " EITHER ") > 0 Then MustChoose = True
1646 If InStr(UserSentence, " CHOOSE ") > 0 Then MustChoose = True
1647 If InStr(UserSentence, " CHOICE ") > 0 Then MustChoose = True
1648 If InStr(UserSentence, " ALTERNATIVE ") > 0 Then MustChoose = True
1649 If InStr(UserSentence, " VERSUS ") > 0 Then MustChoose = True
1650 If InStr(UserSentence, " VS ") > 0 Then MustChoose = True
1651 If Len(UserSentence) < 10 Then MustChoose = False
1652 If MustChoose = True And NoChoosing = False Then
1653 GetResponse = HalBrain.ChooseSentenceFromFile("choice") & " " & GetResponse
1654 If Rnd * 100 > 50 Then GetResponse = Replace(GetResponse, "<MaybeName>", "<UserName>", 1, -1, vbTextCompare)
1655 GetResponse = Replace(GetResponse, "<MaybeName>", "", 1, -1, vbTextCompare)
1656 End If
1657 HalBrain.DebugWatch GetResponse, "Multiple Choice"
1658
1659 Rem PLUGIN: PLUGINAREA45
1660
1661 'RESPOND: USE CURRENT SENTENCE
1662 'If no Response is found yet, try to use the user's words in his or her own sentence.
1663 'Results are also stored in the default user keyword brain file for compatibility
1664 'with other brain plug-ins.
1665 GetResponse = HalBrain.HalFormat(GetResponse)
1666 If (Len(GetResponse) < 4 And Len(LearnKeyword) < 4) Then
1667 CheatResp = HalBrain.CheatResponse(HalBrain.SwitchPerson((OriginalSentence)))
1668 CheatResp = Left(CheatResp, InStr(1, CheatResp, "<STOREVARS>", 1) - 1)
1669 If Len(CheatResp) > 4 Then
1670 SpinWheel = HalBrain.RandomNum(9)
1671 If SpinWheel = 1 Then GetResponse = GetResponse & " So, " & CheatResp & vbCrLf
1672 If SpinWheel = 2 Then GetResponse = GetResponse & " Really, " & CheatResp & vbCrLf
1673 If SpinWheel = 3 Then GetResponse = GetResponse & " Oh <UserName>, " & CheatResp & vbCrLf
1674 If SpinWheel = 4 Then GetResponse = GetResponse & " Let me think; " & CheatResp & " ; what do you think <UserName>? " & vbCrLf
1675 If SpinWheel > 5 Then GetResponse = GetResponse & CheatResp & vbCrLf
1676 End If
1677 End If
1678 HalBrain.DebugWatch GetResponse, "CheatResponse"
1679
1680 Rem PLUGIN: PLUGINAREA46
1681
1682 'RESPOND: CHANGE TOPIC
1683 'If a different procedure has requested that the topic be changed
1684 'Hal will do so now
1685 GetResponse = Replace(GetResponse, "<ChangeSubject>", newTopic, 1, -1, vbTextCompare)
1686 GetResponse = Replace(GetResponse, "<ChangeTopic>", newTopic, 1, -1, vbTextCompare)
1687 GetResponse = Replace(GetResponse, "<NewTopic>", newTopic, 1, -1, vbTextCompare)
1688 HalBrain.DebugWatch GetResponse, "Change Topic"
1689
1690 Rem PLUGIN: PLUGINAREA47
1691
1692 'RESPOND: MAKE UP SOMETHING TO SAY
1693 'If we have got to this point and we still don't have anything to say, then
1694 'we will use 1 of 5 functions to make something up to say.
1695 GetResponse = Trim(GetResponse)
1696 If Len(GetResponse) < 4 Then
1697 If Rnd * 10 < 4 Then GetResponse = HalBrain.ChooseSentenceFromFile("topicIntro")
1698 Select Case HalBrain.RandomNum(6)
1699 Case 1
1700 'RESPOND: POSITIVE PHRASE
1701 'Choose a random positive action phrase and random object phrase
1702 GetResponse = GetResponse & HalBrain.ChooseSentenceFromFile("actionPhrase") & " " & HalBrain.ChooseSentenceFromFile("objectPhrase")
1703 Case 2
1704 'RESPOND: CALL MAKETALK FUNCTION
1705 'This function assembles sentences from components.
1706 GetResponse = GetResponse & HalBrain.ChooseSentenceFromFile("sentGen1") & " " & HalBrain.ChooseSentenceFromFile("sentGen2") & " " & HalBrain.ChooseSentenceFromFile("sentGen3")
1707 Case 3
1708 'RESPOND: CHANGE TOPIC
1709 GetResponse = GetResponse & newTopic
1710 Case Else
1711 'RESPOND: USE RANDOM QUESTION COLLECTED FROM USER
1712 'Randomly select a question once posed by the user in order to find something to say.
1713 'If the user answers the question, then Hal will know that answer in the future.
1714 GetResponse = GetResponse & HalBrain.ChooseSentenceFromFile("sharedQuestions")
1715 End Select
1716 GetResponse = GetResponse & "<LOWQUALITY>"
1717 End If
1718 HalBrain.DebugWatch GetResponse, "Make up something"
1719
1720 Rem PLUGIN: PLUGINAREA48
1721
1722 'PROCESS: RECORD TIME
1723 'Record the current time, so Hal knows the time in between sentences.
1724 LastResponseTime = Now
1725
1726 'PROCESS: FIGURE OUT CONTINUITY
1727 'If the user seems to be continuing a line of thought
1728 'from something Hal just said, note this.
1729 If Len(Trim(PrevSent)) > 2 And (HalBrain.TopicSearch(UserSentence, "responding") = "True" Or InStr(OriginalSentence, "!") Or InStr(PrevSent, "?")) Then TopicContinuity = True
1730
1731 'PROCESS: FIGURE OUT PERSONAL DATA
1732 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
1733 PersonalData = False
1734 Else
1735 PersonalData = True
1736 End If
1737
1738 'PROCESS: RESTORE PUNTUATION FOR LEARNING
1739 'Now we establish a special sentence on which we can restore ordinary capitalization:
1740 AnswerSent = UserSentence
1741 AnswerSent = Trim(HalBrain.FixCaps(AnswerSent))
1742 If Right(AnswerSent, 1) = "." Then AnswerSent = Left(AnswerSent, Len(AnswerSent) - 1)
1743 'Now we add in our spaces and restore ending punctuation to match the user's original sentence:
1744 If InStr(OriginalSentence, "!") > 0 And InStr(AnswerSent, "!") = 0 Then
1745 AnswerSent = AnswerSent & "!"
1746 ElseIf InStr(OriginalSentence, "?") > 0 And InStr(AnswerSent, "?") = 0 Then
1747 AnswerSent = AnswerSent & "?"
1748 Else
1749 AnswerSent = AnswerSent & "."
1750 End If
1751 'If there were no pronoun reversals required in the sentence,
1752 'we can recover directly from the original sentence:
1753 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)
1754
1755 'PROCESS: NOTE IF QUESTION
1756 'See if the user's sentence is a question or not and note this
1757 'for use by other functions
1758 If InStr(1, OriginalSentence, "?", vbTextCompare) > 0 Then IsQuestion = True
1759 If InStr(1, OriginalSentence, "Who ", vbTextCompare) > 0 Then IsQuestion = True
1760 If InStr(1, OriginalSentence, "What ", vbTextCompare) > 0 Then IsQuestion = True
1761 If InStr(1, OriginalSentence, "When ", vbTextCompare) > 0 Then IsQuestion = True
1762 If InStr(1, OriginalSentence, "Where ", vbTextCompare) > 0 Then IsQuestion = True
1763 If InStr(1, OriginalSentence, "Why ", vbTextCompare) > 0 Then IsQuestion = True
1764 If InStr(1, OriginalSentence, "How ", vbTextCompare) > 0 Then IsQuestion = True
1765 If InStr(1, OriginalSentence, ".", vbTextCompare) > 0 Then IsQuestion = False
1766 If InStr(1, OriginalSentence, "!", vbTextCompare) > 0 Then IsQuestion = False
1767 If InStr(1, OriginalSentence, " am I ", vbTextCompare) > 0 Then IsQuestion = False
1768
1769 'SAVE: EPHEMERAL KNOWLEDGE
1770 'Some Hal's learned knowledge should be temporary because it is ephemeral in nature.
1771 'Knowledge about weather, season, temperature, etc. are temporary knowledge
1772 'that shouldn't be stored in Hal's permanent brain files. Ephemeral knowledge is
1773 'detected and saved in a temporary table. The temporary table only stores 10
1774 'entries in it at a time.
1775 If HalBrain.TopicSearch(UserSentence, "ephemeralDetect") = "True" Then
1776 If HalBrain.CheckTableExistence(Trim(LCase(UserName)) & "_TempSent") = False Then
1777 'Create table for this user if it doesn't exist
1778 HalBrain.CreateTable Trim(LCase(UserName)) & "_TempSent", "Brain", "autoLearningBrain"
1779 End If
1780 If TopicContinuity = True Then HalBrain.AddToTable Trim(LCase(UserName)) & "_TempSent", "Brain", Trim(HalBrain.AlphaNumericalOnly(PrevSent)), AnswerSent
1781 HalBrain.AddToTable Trim(LCase(UserName)) & "_TempSent", "Brain", Trim(UserSentence), AnswerSent
1782 HalBrain.LimitSize Trim(LCase(UserName)) & "_TempSent", 10
1783 HalBrain.ReadOnlyMode = True 'Block additional file saves when ephemeral knowledge is detected
1784 End If
1785
1786 'SAVE: LEARN AUTO TOPIC FOCUS PEOPLE SENTENCES
1787 'Learns info about people with recognized names
1788 If HalBrain.ReadOnlyMode = False And HalGreeting = "" And IsQuestion = False And MentionedName <> "" And Trim(Ucase(NewName)) <> Trim(Ucase(MentionedName)) And WN.LookupWord(MentionedName) = False Then
1789 If HalBrain.CheckTableExistence("_" & Trim(LCase(MentionedName))) = False Then
1790 'Create table for this person if it doesn't exist
1791 HalBrain.CreateTable "_" & Trim(LCase(MentionedName)), "Brain", "autoLearningBrain"
1792 HalBrain.AddToTable "topicRelationships", "TopicSearch", Trim(MentionedName), Trim(LCase(MentionedName))
1793 End If
1794 'Store user response in this table
1795 If TopicContinuity = True Then HalBrain.AddToTable "_" & Trim(LCase(MentionedName)), "Brain", Trim(HalBrain.AlphaNumericalOnly(PrevSent)), AnswerSent
1796 HalBrain.AddToTable "_" & Trim(LCase(MentionedName)), "Brain", Trim(UserSentence), AnswerSent
1797 End If
1798
1799 'SAVE: AUTO TOPIC FOCUS LEARNING
1800 If HalBrain.ReadOnlyMode = False And HalGreeting = "" And PersonalData = False And HalBrain.CountInstances(" ", Trim(UserSentence)) > 2 And IsQuestion = False Then
1801 Keywords = HalBrain.TopicSearch(UserSentence, "topicRelationships") & " " & CurrentSubject & " " & UserSentence
1802 Keywords = HalBrain.RemoveExtraSpaces(HalBrain.ExtractKeywords(" " & Keywords & " "))
1803 If Len(Keywords) > 3 Then
1804 KeywordList = Split(Keywords, " ")
1805 TopicList = ""
1806 'Create list of tables that exist for each keyword
1807 For i = Lbound(KeywordList) To Ubound(KeywordList)
1808 TopicTable = HalBrain.TopicSearch(" " & KeywordList(i) & " ", "topicRelationships")
1809 If TopicTable <> "" And InStr(1, " " & TopicList, " " & Trim(TopicTable) & " ", vbTextCompare) = 0 Then
1810 'Topic already exists, make note of it
1811 TopicList = TopicList & TopicTable & " "
1812 ElseIf TopicTable = "" Then
1813 If WN.LookupWord(KeywordList(i)) = True And WN.GuessPartOfSpeech() = "NOUN" And HalBrain.IsWordNumber(KeywordList(i)) = False Then
1814 'Topic does not exist, but can and will be created
1815 TopicList = TopicList & KeywordList(i) & " "
1816 HalBrain.CreateTable "_" & Trim(LCase(KeywordList(i))), "Brain", "autoLearningBrain"
1817 HalBrain.AddToTable "topicRelationships", "TopicSearch", " " & Trim(KeywordList(i)) & " ", Trim(LCase(KeywordList(i)))
1818 'Relationships based on wordnet synonyms are recorded
1819 Relationships = WN.GetDefinition("NOUN", 1, "S") & "," & WN.GetSynonyms("NOUN", 1)
1820 Relationships = Replace(Relationships, ", ", ",")
1821 Relationships = Trim(Replace(Relationships, ",,", ","))
1822 If Right(Relationships, 1) = "," Then Relationships = Trim(Left(Relationships, Len(Relationships) - 1))
1823 If Len(Relationships) > 1 Then
1824 If Left(Relationships, 1) = "," Then Relationships = Right(Relationships, Len(Relationships) - 1)
1825 RelList = Split(Relationships, ",")
1826 For h = LBound(RelList) To UBound(RelList)
1827 If HalBrain.TopicSearch(" " & RelList(h) & " ", "topicRelationships") = "" Then
1828 HalBrain.AddToTable "topicRelationships", "TopicSearch", " " & Trim(RelList(h)) & " ", Trim(LCase(KeywordList(i)))
1829 End If
1830 Next
1831 End If
1832 End If
1833 End If
1834 Next
1835 'User's sentence is recorded in all related topic tables
1836 TopicList = HalBrain.RemoveExtraSpaces(TopicList)
1837 If TopicList <> "" Then
1838 AlreadyLearned = True
1839 TableList = Split(TopicList, " ")
1840 For i = LBound(TableList) To UBound(TableList)
1841 If TopicContinuity = True Then HalBrain.AddToTable "_" & Trim(LCase(TableList(i))), "Brain", Trim(HalBrain.AlphaNumericalOnly(PrevSent)), AnswerSent
1842 HalBrain.AddToTable "_" & Trim(LCase(TableList(i))), "Brain", Trim(UserSentence), AnswerSent
1843 Next
1844 End If
1845 End If
1846 End If
1847
1848 If HalBrain.ReadOnlyMode = False And AlreadyLearned = False And HalBrain.CountInstances(" ", Trim(UserSentence)) > 2 And HalGreeting = "" Then
1849 If PersonalData = True And IsQuestion = False Then
1850 'SAVE: FILE USER SENTENCE DEFAULT
1851 'Hal adds to the default brain user sentence file an association of the current
1852 'user sentence With words selected from that same current user sentence.
1853 If InStr(UserSentence, "WHAT IS") = 0 Then
1854 If HalBrain.CheckTableExistence(Trim(LCase(UserName)) & "_UserSent") = False Then
1855 'Create table for this user if it doesn't exist
1856 HalBrain.CreateTable Trim(LCase(UserName)) & "_UserSent", "Brain", "autoLearningBrain"
1857 End If
1858 If TopicContinuity = True Then HalBrain.AddToTable Trim(LCase(UserName)) & "_UserSent", "Brain", Trim(HalBrain.AlphaNumericalOnly(PrevSent)), AnswerSent
1859 HalBrain.AddToTable Trim(LCase(UserName)) & "_UserSent", "Brain", Trim(UserSentence), AnswerSent
1860 End If
1861 ElseIf IsQuestion = False Then
1862 'SAVE: FILE SHARED USER SENTENCES
1863 'Unless the user seems to be asking for data recall, or talking about
1864 'himself or herself, Hal adds to a shared user sentence file. This allows
1865 'Hal to gain knowledge that he can apply to conversations with multiple users.
1866 If TopicContinuity = True Then HalBrain.AddToTable "sharedUserSent", "Brain", Trim(HalBrain.AlphaNumericalOnly(PrevSent)), AnswerSent
1867 HalBrain.AddToTable "sharedUserSent", "Brain", Trim(UserSentence), AnswerSent
1868 End If
1869 'SAVE: FILE QUESTIONS IN RANDOM QUESTION RETRIEVAL FILE
1870 'Hal saves the user's questions (with pronouns reversed)
1871 'so that he can pose questions back to the user later for 3 purposes:
1872 '1. If the user answers a question later, Hal will learn the answer.
1873 '2. It adds variety to future conversations for the user.
1874 '3. It gives Hal another possible response when "stuck" for an answer.
1875 If IsQuestion = True Then HalBrain.AddToTable "sharedQuestions", "Sentence", AnswerSent, ""
1876 End If
1877
1878 'POST PROCESS: LIMIT TABLE SIZE
1879 'We want to make sure the sentence files
1880 'don't get too big so we limit them.
1881
1882 'RESPOND: HAL NOTICES HE IS REPEATING HIMSELF
1883 'Hal may make a comment or alter his remark
1884 'if he detects himself being repetitious.
1885 If HalBrain.CheckRepetition(GetResponse, PrevSent) = True Then
1886 RepeatResponse = HalBrain.ChooseSentenceFromFile("halRepeat")
1887 GetResponse = Replace(RepeatResponse, "<response>", GetResponse, 1, -1, vbTextCompare)
1888 DebugInfo = DebugInfo & "Hal has noticed he is repeating himself and has made a comment about it: " & GetResponse & vbCrLf
1889 End If
1890
1891 Rem PLUGIN: PLUGINAREA49
1892
1893 'RESPOND: MAKE COMMENTS ABOUT SHORT PHRASES
1894 GetResponse = GetResponse & ShortPhrase
1895
1896 'PROCESS: REVERSE CERTAIN CONTRACTIONS AND OTHER SUBSTITUTIONS
1897 'Standardizing on contractions can make Hal sound conversational.
1898 'However, certain sentence constructions don't work well
1899 'if expressed as contractions. For example:
1900 '"I don't know where it is" becomes "I don't know where it's."
1901 'For another example, "That's how he is" becomes "That's how he's."
1902 'To solve these types of cases
1903 'we attempt to modify certain contractions, words, and phrases
1904 'at the end of this function, now that Hal's thinking is done.
1905 GetResponse = HalBrain.HalFormat(GetResponse)
1906 GetResponse = HalBrain.ProcessSubstitutions(GetResponse, "corrections")
1907
1908 'PROCESS: CALL USER BY CORRECT NAME
1909 'If the user has chosen a nickname or temporary name, call user by that
1910 'otherwise call the user by the username chosen by the host application
1911 If NewName <> "" Then
1912 GetResponse = Replace(GetResponse, "<UserName>", NewName, 1, -1, vbTextCompare)
1913 GetResponse = Replace(GetResponse, "<Name>", NewName, 1, -1, vbTextCompare)
1914 Else
1915 GetResponse = Replace(GetResponse, "<UserName>", UserName, 1, -1, vbTextCompare)
1916 GetResponse = Replace(GetResponse, "<Name>", UserName, 1, -1, vbTextCompare)
1917 End If
1918
1919 'PROCESS: NAME REVERSAL
1920 'If Hal is about to same something referring to himself in third person
1921 'then we will reverse it to be about the user.
1922 'Don't let Hal say the word "HAL" unless he is telling his name
1923 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
1924 GetResponse = " " & GetResponse & " "
1925 GetResponse = Replace(GetResponse, "Ultra Hal Assistant", " " & NewName & " ", 1, -1, vbTextCompare)
1926 GetResponse = Replace(GetResponse, "Ultra Hal", " " & NewName & " ", 1, -1, vbTextCompare)
1927 GetResponse = Replace(GetResponse, "Hal Assistant", " " & NewName & " ", 1, -1, vbTextCompare)
1928 GetResponse = Replace(GetResponse, " Hal ", " " & NewName & " ", 1, -1, vbTextCompare)
1929 GetResponse = Replace(GetResponse, " Hal,", " " & NewName & ",", 1, -1, vbTextCompare)
1930 GetResponse = Replace(GetResponse, " Hal.", " " & NewName & ".", 1, -1, vbTextCompare)
1931 GetResponse = Replace(GetResponse, " " & ComputerName & " ", " " & NewName & " ", 1, -1, vbTextCompare)
1932 GetResponse = Replace(GetResponse, " " & ComputerName & ",", " " & NewName & ",", 1, -1, vbTextCompare)
1933 GetResponse = Replace(GetResponse, " " & ComputerName & ".", " " & NewName & ".", 1, -1, vbTextCompare)
1934 End If
1935
1936 'PROCESS: PRESERVE ALL VARIABLES
1937 PrevUserSent = UserSentence
1938 CustomMem = HalBrain.EncodeVar(NewName, "NewName") & HalBrain.EncodeVar(UserSex, "UserSex") & HalBrain.EncodeVar(SentCount, "SentCount")
1939
1940 Rem PLUGIN: CUSTOMMEM2
1941 'The preceding comment is actually a plug-in directive for
1942 'the Ultra Hal host application. It allows for code snippets
1943 'to be inserted here on-the-fly based on user configuration.
1944
1945End Function
1946
1947'If the user clicks on the About/Options button for this plugin
1948'this sub will be called. There are no extra settings for this brain,
1949'so we'll display an information box
1950Sub AboutOptions()
1951 HalBrain.MsgAlert "This is the Ultra Hal 6.0 Default Brain. This brain has no additional options."
1952End Sub
1953
1954'This sub will be called when the Ultra Hal program starts up in case
1955'the script needs to load some modules or seperate programs. If a return
1956'value is given it is passed as a Hal Command to the host Hal program.
1957Function Script_Load()
1958 Rem PLUGIN: SCRIPT_LOAD
1959 'The preceding comment is actually a plug-in directive for
1960 'the Ultra Hal host application. It allows for code snippets
1961 'to be inserted here on-the-fly based on user configuration.
1962End Function
1963
1964'This sub will be called before the Ultra Hal program is closed in case
1965'the script needs to do any cleanup work.
1966Sub Script_Unload()
1967 Rem PLUGIN: SCRIPT_UNLOAD
1968 'The preceding comment is actually a plug-in directive for
1969 'the Ultra Hal host application. It allows for code snippets
1970 'to be inserted here on-the-fly based on user configuration.
1971End Sub
1972
1973'If the host application is Ultra Hal Assistant, then this sub will be
1974'run once a minute enabling plug-ins to do tasks such as checking for
1975'new emails or checking an appointment calendar.
1976Sub Minute_Timer(MinutesPast)
1977 Rem PLUGIN: MINUTE_TIMER
1978 'The preceding comment is actually a plug-in directive for
1979 'the Ultra Hal host application. It allows for code snippets
1980 'to be inserted here on-the-fly based on user configuration.
1981End Sub
1982
1983Rem PLUGIN: FUNCTIONS
1984'The preceding comment is actually a plug-in directive for
1985'the Ultra Hal host application. It allows for code snippets
1986'to be inserted here on-the-fly based on user configuration.