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