Rem Type=Brain Rem Name=Ultra Hal 6.1 Default Brain Rem Author=Zabaware, Inc. Rem Language=VBScript Rem DB=HalBrain.db 'The UltraHal function is called by Ultra Hal Assistant 6.x or a compatible host application 'It passes an unformated string of the user's input as well as all remembered variables. 'The UltraHal function splits the user's input into seperate sentences and than calls 'GetResponse to get a response for each user sentence. The UltraHal function performs all 'the initialization functions required for GetResponse so that GetResponse doesn't have to 'initialize several times if a user inputs more than 1 sentence. 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) 'RESPOND: User pressed enter, but didn't say anything InputString = Trim(InputString) If Len(InputString) < 2 Then UltraHal = "Please say something." Exit Function End If 'PROCESS: AUTO-IDLE 'If AUTO-IDLE is enabled, it is called by the Ultra Hal Assistant host 'application at a set interval. This allows for the possibility of Hal 'being the first to say something if the user is idle. If InputString = "AUTO-IDLE" Or InputString = "AUTO IDLE" Or InputString = "AUTOIDLE" Then Rem PLUGIN: AUTO-IDLE 'The preceding comment is actually a plug-in directive for 'the Ultra Hal host application. It allows for code snippets 'to be inserted here on-the-fly based on user configuration. UltraHal = UltraHal & HalBrain.StoreVars(HalCommands, Hate, Swear, Insults, Compliment, PrevSent, LastResponseTime, PrevUserSent, CustomMem, GainControl, LastTopicList) Exit Function End If 'PROCESS: INITIALIZE VARIABLES 'VBScript doesn't allow you to declare variables in advance as a 'particular data type; everything is a Variant. We must assign 'integers to the following variants so that data type errors don't 'occur If LearningLevel = "" Then LearningLevel = 0 If Hate = "" Then Hate = 0 If Swear = "" Then Swear = 0 If Insults = "" Then Insults = 0 If Compliment = "" Then Compliment = 0 If GainControl = "" Then GainControl = 25 'PROCESS: IF NO LEARNING IS REQUESTED, PUT DATABASE IN READ-ONLY MODE 'If read only mode is on, any requests to create a new table, add to 'a table, or delete records from a table will be ignored. If LearningLevel = 0 Then HalBrain.ReadOnlyMode = True Else HalBrain.ReadOnlyMode = False 'PROCESS: EMOTIONAL FACTOR CENTERING 'We help Hal regain his emotional "center" gradually, even if the 'user doesn't catch on to dealing With Hal's feelings. The following 'code brings Hal's memory of hate, swearing, and insults gradually 'and randomly back to zero. Randomize SpinWheel = Int(Rnd * 100) If Hate > 0 And SpinWheel < 10 Then Hate = Hate - 1 SpinWheel = Int(Rnd * 100) If Swear > 0 And SpinWheel < 10 Then Swear = Swear - 1 SpinWheel = Int(Rnd * 100) If Insults > 0 And SpinWheel < 10 Then Insults = Insults - 1 'PROCESS: EMOTIONAL VARIETY 'The following code allows Hal some random emotional excursions: SpinWheel = Int(Rnd * 100) If Compliment = 4 And SpinWheel < 30 Then Compliment = 2 If Compliment > 0 And Compliment < 4 And SpinWheel < 5 Then Compliment = 0 If Compliment > 4 And SpinWheel < 5 Then Compliment = 0 If Compliment < 0 And SpinWheel < 30 Then Compliment = 0 If SpinWheel > 80 And SpinWheel < 90 Then Compliment = 2 If SpinWheel > 90 And SpinWheel < 95 Then Compliment = 4 Rem PLUGIN: PRE-PROCESS 'The preceding comment is actually a plug-in directive for 'the Ultra Hal host application. It allows for code snippets 'to be inserted here on-the-fly based on user configuration. 'PROCESS: SPLIT USER'S INPUT STRING INTO SEPERATE SENTENCES 'Encode abbreviations such as Mr. Mrs. and Ms. InputString = Replace(InputString, "MR.", "Mr", 1, -1, vbTextCompare) InputString = Replace(InputString, "MRS.", "Mrs", 1, -1, vbTextCompare) InputString = Replace(InputString, "MS.", "Ms", 1, -1, vbTextCompare) InputString = Replace(InputString, "DR.", "Dr", 1, -1, vbTextCompare) InputString = Replace(InputString, "MS.", "Ms", 1, -1, vbTextCompare) InputString = Replace(InputString, "ST.", "St", 1, -1, vbTextCompare) InputString = Replace(InputString, "PROF.", "Prof", 1, -1, vbTextCompare) InputString = Replace(InputString, "GEN.", "Gen", 1, -1, vbTextCompare) InputString = Replace(InputString, "REP.", "Rep", 1, -1, vbTextCompare) InputString = Replace(InputString, "SEN.", "Sen", 1, -1, vbTextCompare) 'Remove unnecessary punctuation Do RepeatLoop = False If InStr(InputString, "..") Then InputString = Replace(InputString, "..", "."): RepeatLoop = True If InStr(InputString, "??") Then InputString = Replace(InputString, "??", "?"): RepeatLoop = True If InStr(InputString, "!!") Then InputString = Replace(InputString, "!!", "!"): RepeatLoop = True If InStr(InputString, "!?") Then InputString = Replace(InputString, "!?", "?"): RepeatLoop = True If InStr(InputString, "?!") Then InputString = Replace(InputString, "?!", "?"): RepeatLoop = True If InStr(InputString, ",,") Then InputString = Replace(InputString, ",,", ","): RepeatLoop = True Loop While RepeatLoop = True 'Detect and encode acronyms such as U.S.A. InputString = Trim(InputString) WordList = Split(InputString, " ") For i = 0 To UBound(WordList) If Len(WordList(i)) > 3 Then If Right(WordList(i), 1) = "." And Mid(WordList(i), Len(WordList(i)) - 2, 1) = "." Then InputString = Replace(InputString, WordList(i), Left(WordList(i), Len(WordList(i)) - 1) & "") End If End If Next 'Place split markers in string InputString = Replace(InputString, ". ", ". ") InputString = Replace(InputString, "? ", "? ") InputString = Replace(InputString, "! ", "! ") 'Decode acronyms and abbreviations InputString = Replace(InputString, "", ".", 1, -1, vbTextCompare) 'Split string into sentences Sentences = Split(InputString, "") 'PROCESS: RECORD DEBUG INFO HalBrain.AddDebug "Debug", "Ultra Hal Start" 'RESPOND: PREDEFINED RESPONSES 'If the previous exchange had tags to set the response of this exchange 'then follow the command. NextResponse = HalBrain.ExtractVar(CustomMem, "NextResponse") If NextResponse <> "" Then CustomMem = Replace(CustomMem, "NextResponse-eq-", "LastResponse-eq-") UltraHal = NextResponse GoodSentence = True NextResponse = "" End If YesResponse = HalBrain.ExtractVar(CustomMem, "YesRes") NoResponse = HalBrain.ExtractVar(CustomMem, "NoRes") If YesResponse <> "" Or NoResponse <> "" Then CustomMem = Replace(CustomMem, "YesRes-eq-", "LastYesRes-eq-") CustomMem = Replace(CustomMem, "NoRes-eq-", "LastNoRes-eq-") InputString2 = Replace(InputString, ".", " ") InputString2 = Replace(InputString2, "?", " ") InputString2 = Replace(InputString2, "!", " ") InputString2 = " " & Replace(InputString2, ",", " ") & " " YesNoDetect = HalBrain.TopicSearch(InputString2, "") If YesNoDetect = "Yes" And YesResponse <> "" Then UltraHal = YesResponse GoodSentence = True ElseIf YesNoDetect = "No" And NoResponse <> "" Then UltraHal = NoResponse GoodSentence = True End If End If 'RESPOND: GETRESPONSE 'Get a response from Hal's brain for each sentence individually. 'If a response from a sentence indicates a special flag, then Hal 'will give only the response to that sentence, and not process 'any other sentences. Otherwise Hal will respond to each sentence. 'Hal will respond to a max of 3 sentences at once. SentenceCount = UBound(Sentences) If SentenceCount > 2 Then SentenceCount = 2 LowQualityResponse = "" If GoodSentence <> True Then 'Only respond if a predefined response hasn't already been selected For i = 0 To SentenceCount TempParent = HalBrain.AddDebug("Debug", "Begin Processing Sentence " & CStr(i + 1), vbCyan) HalBrain.AddDebug TempParent, "Sentence: " & Sentences(i) HalBrain.DebugWatch "", "NewSent" Sentences(i) = Trim(Sentences(i)) If Len(Sentences(i)) > 1 Then GoodSentence = True CurResponse = GetResponse(Sentences(i), UserName, ComputerName, LearningLevel, HalCommands, Hate, Swear, Insults, Compliment, PrevSent, LastResponseTime, PrevUserSent, CustomMem, GainControl, LastTopicList) & vbCrLf If InStr(1, CurResponse, "", vbTextCompare) Then 'If Hal's response to the current sentence is of a low-quality nature, then we hold out 'for a better response with a different sentence, but if there is no better response 'then we will use only the first low-quality response we came across. If LowQualityResponse = "" Then LowQualityResponse = CurResponse ElseIf InStr(1, CurResponse, "", vbTextCompare) Or InStr(1, CurResponse, "", vbTextCompare) Or InStr(1, CurResponse, "", vbTextCompare) Then 'If Hal's response indicates an exclusivity tag, then we erase any response Hal may have 'already had, respond with the exclusive response, and cease processing more sentences UltraHal = CurResponse & vbCrLf Exit For Else 'Since there are no special tags, we just append the new response to the previous one 'if the response was not already given If InStr(1, UltraHal, CurResponse, vbTextCompare) = 0 Then UltraHal = UltraHal & " . " & CurResponse & vbCrLf End If 'If we received a tag to stop processing more sentences, we leave the loop If InStr(1, CurResponse, "", vbTextCompare) Then Exit For End If Next End if 'If we have no normal quality responses, we will use our low quality response UltraHal = Trim(UltraHal) If UltraHal = "" Then UltraHal = Trim(LowQualityResponse) 'RESPOND: USER DIDN'T SAY ANYTHING 'If GoodSentence has not been set to true, then that means the user didn't 'type anything of use, so we ask the user to say something. If GoodSentence = False Then UltraHal = "Please say something." Exit Function End If 'PROCESS: PROCESS IN-SENTENCE TAGS 'the * tag sets what Hal's next response will be, no matter what the user says NextResponse = HalBrain.SearchPattern(UltraHal, "***", 2) If NextResponse <> "" Then UltraHal = Replace(UltraHal, NextResponse, "", 1, -1, vbTextCompare) UltraHal = Replace(UltraHal, "", "", 1, -1, vbTextCompare) UltraHal = Replace(UltraHal, "", "", 1, -1, vbTextCompare) NextResponse = Trim(Replace(NextResponse, "", "", 1, -1, vbTextCompare)) CustomMem = CustomMem & HalBrain.EncodeVar(NextResponse, "NextResponse") End If 'The * and * tag sets what Hal's response will be if the user says yes or no UltraHal = Replace(UltraHal, "", "", 1, -1, vbTextCompare) UltraHal = Replace(UltraHal, "", "", 1, -1, vbTextCompare) UltraHal = Replace(UltraHal, "", "", 1, -1, vbTextCompare) UltraHal = Replace(UltraHal, "", "", 1, -1, vbTextCompare) UltraHal = Replace(UltraHal, "", "", 1, -1, vbTextCompare) UltraHal = Replace(UltraHal, "", "", 1, -1, vbTextCompare) YesRes = HalBrain.SearchPattern(UltraHal, "***", 2) If YesRes <> "" Then UltraHal = Replace(UltraHal, YesRes, "", 1, -1, vbTextCompare) UltraHal = Replace(UltraHal, "", "", 1, -1, vbTextCompare) UltraHal = Replace(UltraHal, "", "", 1, -1, vbTextCompare) YesRes = Trim(Replace(YesRes, "", "", 1, -1, vbTextCompare)) CustomMem = CustomMem & HalBrain.EncodeVar(YesRes, "YesRes") End If NoRes = HalBrain.SearchPattern(UltraHal, "***", 2) If NoRes <> "" Then UltraHal = Replace(UltraHal, NoRes, "", 1, -1, vbTextCompare) UltraHal = Replace(UltraHal, "", "", 1, -1, vbTextCompare) UltraHal = Replace(UltraHal, "", "", 1, -1, vbTextCompare) NoRes = Trim(Replace(NoRes, "", "", 1, -1, vbTextCompare)) CustomMem = CustomMem & HalBrain.EncodeVar(NoRes, "NoRes") End If 'The * tags are converted into a HalCommand to run a program UltraHal = Replace(UltraHal, "", "", 1, -1, vbTextCompare) UltraHal = Replace(UltraHal, "", "", 1, -1, vbTextCompare) RunProgram = HalBrain.SearchPattern(UltraHal, "***", 2) If RunProgram <> "" Then UltraHal = Replace(UltraHal, RunProgram, "", 1, -1, vbTextCompare) UltraHal = Replace(UltraHal, "", "", 1, -1, vbTextCompare) UltraHal = Replace(UltraHal, "", "", 1, -1, vbTextCompare) RunProgram = Trim(Replace(RunProgram, "", "", 1, -1, vbTextCompare)) HalCommands = HalCommands & "" & RunProgram & "" End If UltraHal = Replace(UltraHal, "", "www.ultrahal.com", 1, -1, vbTextCompare) UltraHal = Replace(UltraHal, "", ComputerName, 1, -1, vbTextCompare) UltraHal = Replace(UltraHal, "", ComputerName, 1, -1, vbTextCompare) UltraHal = Replace(UltraHal, "", ComputerName, 1, -1, vbTextCompare) UltraHal = Replace(UltraHal, " " & ComputerName & " ", " " & ComputerName & " ", 1, -1, vbTextCompare) 'Fixes capitilization of computer's name if needed UltraHal = Replace(UltraHal, "