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