Advertisement
Guest User

Untitled

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