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