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