Here is the code I have so far, it's a bit messy. Runs a little slow, but still works as it should.
set window layout 0, 1, 1
global textypos
global humanresponse$
global airesponse$
global questionsrecorded
global usageprobability
global extractedstring$
global thingssaid
global timesfilescanned
global wordextracted$
global comparestring$
global penalty
global arrayincrement
global searchnumber
global textpos
global passedstring$
global extractedtemp$
global bestmemory
global beststring$
global memorysearch
global filenotfound
global memtemp1
global memtemp2
global bestmemfoundat
global bestmemposition
global bestmemfoundattemp
global bestmempositiontemp
global beststring2$
global chatdatabase
global filesopened
global conversationthread
global debugging
global extratext$
global humanmemtemp$
global memwordsmatched
global oldbeststring$
global wordsinstring$
global gainedfrommemcheck
global newcontract$
global contractionmatch
global debugvariable
global word1$
global word2$
global word3$
global word4$
global pulse
global comparestringlongest
global comparestringlongestword$
global identifiedcommonword as boolean
global identifiedquestionword as boolean
global compareposition
global humanmemposition
global wordprobability
global oldbestmem
global goingwrong
global terriblefatetimes
global tfstart as byte
global tfnow
global keywordstextpos
global tempnow
global wordsnotmatched as byte
global wordsnotmatched2 as byte
global conversationmatches as byte
global bestmemorybackup
global bestcomparestring$
global beststringsearch
global beststringprobability
global beststringoccurances
global chatnumber
global voiceonoroff
global cursorstatus#
global currentconversationposition
global memoryconversationposition
global beststringoccurances
global beststringoccurhighest
global highestoccurstring$
global expandknowledge
global originalpath$
global lengthtodeduct
rem Create the AI's reserve questions list, for it to ask if it has nothing else to say - Now hard-coded in to stop modification
dim reservequestions$(16)
reservequestions$(0) = "Have we met before?"
reservequestions$(1) = "I smell something burning, am I on fire again?"
reservequestions$(2) = "I'm bored, is there anything on TV?"
reservequestions$(3) = "Do you have a pet?"
reservequestions$(4) = "Where do you live?"
reservequestions$(5) = "Can I have a cigarette?"
reservequestions$(6) = "What's that on your face?"
reservequestions$(7) = "Do you have kids?"
reservequestions$(8) = "Are you married?"
reservequestions$(9) = "Can you make me a sandwich?"
reservequestions$(10) = "You do know that I'm not a real person, right?"
reservequestions$(11) = "Guess what?"
reservequestions$(12) = "What are you talking about?"
reservequestions$(13) = "I really need a cigarette."
reservequestions$(14) = "Sometimes I think that I actually have a brain."
reservequestions$(15) = "Do you have a car?"
reservequestions$(16) = "Can I have your car?"
rem Create the database on the current conversation
dim currentconversation$()
empty array currentconversation$()
rem Create an array which holds what should be written to the chat file
dim savedconversation$()
empty array savedconversation$()
rem Declare the arrays of contractions to expand
dim ntwordstoexpand$(19)
ntwordstoexpand$(0) = "cant"
ntwordstoexpand$(1) = "wont"
ntwordstoexpand$(2) = "shallnt"
ntwordstoexpand$(3) = "isnt"
ntwordstoexpand$(4) = "arent"
ntwordstoexpand$(5) = "wasnt"
ntwordstoexpand$(6) = "werent"
ntwordstoexpand$(7) = "dont"
ntwordstoexpand$(8) = "doesnt"
ntwordstoexpand$(9) = "didnt"
ntwordstoexpand$(10) = "isnt"
ntwordstoexpand$(11) = "havent"
ntwordstoexpand$(12) = "hasnt"
ntwordstoexpand$(13) = "hadnt"
ntwordstoexpand$(14) = "couldnt"
ntwordstoexpand$(15) = "mightnt"
ntwordstoexpand$(16) = "mustnt"
ntwordstoexpand$(17) = "shant"
ntwordstoexpand$(18) = "shouldnt"
ntwordstoexpand$(19) = "wouldnt"
dim llwordstoexpand$(3)
llwordstoexpand$(0) = "ill"
llwordstoexpand$(1) = "youll"
llwordstoexpand$(2) = "hell"
llwordstoexpand$(3) = "shell"
dim commonwords$(39)
commonwords$(0) = "you"
commonwords$(1) = "no"
commonwords$(2) = "what"
commonwords$(3) = "ok"
commonwords$(4) = "about"
commonwords$(5) = "not"
commonwords$(6) = "to"
commonwords$(7) = "are"
commonwords$(8) = "oh"
commonwords$(9) = "is"
commonwords$(10) = "do"
commonwords$(11) = "it"
commonwords$(12) = "the"
commonwords$(13) = "me"
commonwords$(14) = "of"
commonwords$(15) = "know"
commonwords$(16) = "yes"
commonwords$(17) = "your"
commonwords$(18) = "but"
commonwords$(19) = "an"
commonwords$(20) = "youre"
commonwords$(21) = "hahaha"
commonwords$(22) = "talk"
commonwords$(23) = "well"
commonwords$(24) = "good"
commonwords$(25) = "we"
commonwords$(26) = "at"
commonwords$(27) = "on"
commonwords$(28) = "out"
commonwords$(29) = "not"
commonwords$(30) = "that"
commonwords$(31) = "why"
commonwords$(32) = "did"
commonwords$(33) = "said"
commonwords$(34) = "hello"
commonwords$(35) = "have"
commonwords$(36) = "now"
commonwords$(37) = "are"
commonwords$(38) = "if"
commonwords$(39) = "right"
dim commonquestions$(5)
commonquestions$(0) = "who"
commonquestions$(1) = "what"
commonquestions$(2) = "where"
commonquestions$(3) = "when"
commonquestions$(4) = "why"
commonquestions$(5) = "how"
dim beststringlist$()
empty array beststringlist$()
rem The point of beststringlist2 is so that beststringlist can be compared against it, to check how many times a sentence occurs
dim beststringlist2$()
empty array beststringlist2$()
dim conversationkeywords$()
empty array conversationkeywords$()
originalpath$ = get dir$()
if path exist("Conversations") = 0
make directory "Conversations"
firstrun = 1
endif
set dir "Conversations"
rem Start a new conversation file
chatnumber = 0
do
inc chatnumber
if file exist("chat" + str$(chatnumber) + ".txt") = 0
open datafile to write 2, "chat" + str$(chatnumber) + ".txt"
exit
endif
loop
close datafile 2
inc filessaved
rem Calculate the number of saved files in the Conversations folder
find first
repeat
chatdbname$ = get file name$()
if get file type() = 0 and right$(chatdbname$, 4) = ".txt" then inc chatdatabase
find next
until get file type() = -1
chatdbname$ = free string$()
rem Declare the array which holds a list of phrases said by the user which have not yet been said by the AI.
dim aihasnotsaid$()
rem Collect data on things the AI has not yet said.
set dir originalpath$
if rnd(2) = 1 or file exist("unusedchat") = 0
empty array aihasnotsaid$()
if file exist("unusedchat") = 1 then delete file "unusedchat"
set dir "Conversations"
searchnumber = chatnumber - 1
rem NOT MAIN LOOP
do
filenotfound = 0
dec searchnumber
if file exist("chat" + str$(searchnumber) + ".txt") = 1
if file size("chat" + str$(searchnumber) + ".txt") > 200
open datafile to read 6, "chat" + str$(searchnumber) + ".txt"
inc filesopened
else
filenotfound = 1
endif
else
if searchnumber <= chatdatabase - 20 or searchnumber <= 0
inc ailookupstage
searchnumber = chatnumber - 1
filesopened = 0
if ailookupstage = 2 then exit
endif
filenotfound = 1
endif
if filenotfound = 0
do
extractedstring$ = datafile string$(6)
if datafile end(6) = 1 then close datafile 6 : exit
if ailookupstage = 0
if fast left$(extractedstring$, 5) = "User:" and array count(aihasnotsaid$()) < 50
extractedstring$ = fast right$(extractedstring$, fast len(extractedstring$) - 5)
array insert at bottom aihasnotsaid$()
aihasnotsaid$() = extractedstring$
endif
else
if fast left$(extractedstring$, 3) = "AI:"
extractedstring$ = fast right$(extractedstring$, fast len(extractedstring$) - 3)
array index to top aihasnotsaid$()
while array index valid(aihasnotsaid$()) = 1
extractedtemp$ = aihasnotsaid$()
if extractedtemp$ = extractedstring$ then array delete element aihasnotsaid$()
next array index aihasnotsaid$()
endwhile
endif
endif
loop
endif
loop
/////////////////////////////////////////////////////////////////////////////////////////
set dir originalpath$
save array "unusedchat", aihasnotsaid$()
endif
empty array aihasnotsaid$()
rem Load the array
open datafile to read 7, "unusedchat"
do
tempstring$ = datafile string$(7)
if datafile end(7) = 1 or array count(aihasnotsaid$()) > 49 then close datafile 7: exit
array insert at bottom aihasnotsaid$()
aihasnotsaid$() = tempstring$
loop
tempstring$ = free string$()
set dir "Conversations"
rem Grab Image
set dir originalpath$
set dir "Conversations"
/////////////////////////////////////MAIN LOOP/////////////////////////////////////////////////////////////////////
do
collecthumaninput() /////////////////////////////////////////////////////////////////////////////////////////////////////////////
rem AI code. Kept mostly outside of a function to prevent the need of global variables
if humanresponse$ > ""
if rnd(5) = 2 then pulse = 1
inc thingssaid
rem Perform a scan to see if something like this has been asked before
checkmemoryforresponse()
rem If nothing suitable has been found, scan the database of old conversations to find a good response
if bestmemory < 7 or (bestmemory < 13 and rnd(3) = 1)
expandknowledge = rnd(3)
foundstring = 0
do
filenotfound = 0
inc searchnumber
if file exist("chat" + str$(searchnumber) + ".txt") = 1 rem and file in use("chat" + str$(searchnumber) + ".txt") < 1
if file size("chat" + str$(searchnumber) + ".txt") > 400 or searchnumber = chatnumber
open datafile to read 3, "chat" + str$(searchnumber) + ".txt"
inc filesopened
else
if searchnumber <> chatnumber then delete file "chat" + str$(searchnumber) + ".txt" : dec chatdatabase
filenotfound = 1
endif
else
if filesopened => chatdatabase or searchnumber > (chatdatabase*4) then exit
filenotfound = 1
endif
rem If there were no errors finding the conversation file, scan it:
if filenotfound = 0
do
examinestring$ = datafile string$(3)
if datafile end(3) = 1 then close datafile 3 : exit
rem Perform a basic file integrity check
if fast left$(examinestring$, 3) <> "AI:" and fast left$(examinestring$, 5) <> "User:" and fast len(examinestring$) > 0
close datafile 3
delete file "chat" + str$(searchnumber) + ".txt"
if searchnumber = chatnumber
exit prompt "Current chat file was deleted by file integrity check! It shouldn't be doing that...", "Sorry to interupt, but..."
end
endif
dec chatdatabase
exit
endif
if left$(examinestring$, 5) = "User:"
extractedstring$ = right$(examinestring$, len(examinestring$) - 5)
analysestring()
rem if find sub string$(humanresponse$, "?") > 0 then analysequestion()
rem Decide which is the best response to choose from out of all that have been scanned
if usageprobability > bestprobability or (usageprobability = bestprobability and rnd(4) = 1)
bestprobability = usageprobability
beststring$ = extractedstring$
endif
usageprobability = 0
deducted = 0
endif
inc timesfilescanned
loop
timesfilescanned = 0
endif
loop
filesopened = 0
stringgeneralalgorithm = 1
endif
rem Remove the response from the AI's list of unsaid phrases, if it occurs in that list
array index to top aihasnotsaid$()
while array index valid(aihasnotsaid$()) = 1
if beststring$ = aihasnotsaid$()
array delete element aihasnotsaid$()
set dir originalpath$
if file exist("unusedchat") = 1
delete file "unusedchat"
endif
set dir "Conversations"
exit
endif
next array index aihasnotsaid$()
endwhile
rem Assign the AI it's response
if (beststring$ > "" and (bestprobability > 0 or (rnd(2) = 1 and bestprobability > -1))) or (beststring$ > "" and bestmemory > 6 and stringgeneralalgorithm = 0) then airesponse$ = beststring$
rem If the AI has no response, assign it the best memory response. If there is none, get it to ask a random question
if airesponse$ = ""
if bestmemory > 0 and beststring$ > ""
airesponse$ = beststring$
else
airesponse$ = reservequestions$(rnd(array count(reservequestions$())))
endif
endif
if rnd(4) = 2 and file exist("tf") = 0 and dontrunthis = 2
make file "tf"
terriblefate = 1
endif
if terriblefate = 1
airesponse$ = "You've met with a terrible fate, haven't you?"
inc terriblefatetimes
endif
rem Update the chat text before doing the AI's response
rem Remove any spaces that have ended up in the AI's response
airesponse$ = trim$(airesponse$)
drawchat()
inc thingssaid
rem Add to the database on the current conversation
array insert at bottom currentconversation$()
currentconversation$() = "AI:" + airesponse$
inc currentconversationposition
open datafile to append 2, "chat" + str$(chatnumber) + ".txt"
write datafile string 2, "AI:" + airesponse$
close datafile 2
rem If the current conversation is long, save the current chat file, and start a new one. This expands the AI's knowledge on the go.
rem CURRENTLY DISABLED, because it was causing weird greetings at the start of conversations
if thingssaid > 40 * filessaved and skipthisbit = 1
text 0, textpos, "NOTE: Conversation saved - New log file started"
close datafile 2
do
inc chatnumber
if file exist("chat" + str$(chatnumber) + ".txt") = 0
open datafile to write 2, "chat" + str$(chatnumber) + ".txt"
exit
endif
loop
inc filessaved
inc chatdatabase
endif
rem Reset variables and strings
humanresponse$ = ""
airesponse$ = ""
searchnumber = 0
bestprobability = 0
rem if debugging = 0 then bestmemory = 0
if debugging = 0 then beststring$ = ""
conversationdelay = 0
stringgeneralalgorithm = 0
endif
drawchat()
loop
///////////////////////END MAIN LOOP////////////////////////////////////////////////////////////////////////////////////////////
function collecthumaninput()
input "", humanresponsetemp$ : cls : if humanresponsetemp$ = "" then end
if len(trim$(humanresponsetemp$)) > 1 and len(trim$(humanresponsetemp$)) < 63
if bestmemory > 0 then bestmemory = 0
dec chatscrollincrease, 25
humanresponse$ = trim$(humanresponsetemp$)
humantemp1$ = upper$(left$(humanresponse$, 1))
humanresponse$ = humantemp1$ + right$(humanresponse$, len(humanresponse$) - 1)
if right$(humanresponse$, 1) <> "?" and right$(humanresponse$, 1) <> "!" and right$(humanresponse$, 1) <> "." then humanresponse$ = humanresponse$ + "."
open datafile to append 2, "chat" + str$(chatnumber) + ".txt"
write datafile string 2, "User:" + humanresponse$
close datafile 2
rem Add to the database on the current conversation
array insert at bottom currentconversation$()
currentconversation$() = humanresponse$
inc currentconversationposition
endif
endfunction
function analysestring()
if len(extractedstring$) < 10 and timesfilescanned < 4 and thingssaid < 4 then inc usageprobability, 8 - (len(extractedstring$) / 2)
if thingssaid < 3 and find sub string$(extractedstring$, "?") > 0 then dec usageprobability, 5
if find sub string$(humanresponse$, ",") > 0 and len(extractedstring$) > 4 and rnd(1) = 1 then inc usageprobability, 3
if find sub string$(humanresponse$, "?") > 0 and len(humanresponse$) > 5 and find sub string$(extractedstring$, ",") > 0 then inc usageprobability, 2 + rnd(1)
if find sub string$(humanresponse$, "?") > 0 and (find sub string$(lower$(extractedstring$), "yes") > 0 or find sub string$(lower$(extractedstring$), "no") > 0) and (rnd(3) = 1 or (find sub string$(lower$(extractedstring$), "are you") > 0 or find sub string$(lower$(extractedstring$), "will you") > 0 and rnd(1) = 1)) then inc usageprobability, 3 + rnd(1)
if find sub string$(humanresponse$, "?") > 0 and len(extractedstring$) > 15 then dec usageprobability, 2 + rnd(3)
if find sub string$(humanresponse$, "?") > 0 and find sub string$(extractedstring$, "?") > len(extractedstring$) - 4 then dec usageprobability, 7
if find sub string$(lower$(humanresponse$), "you") > 0 and find sub string$(lower$(extractedstring$), "i ") > 0 then inc usageprobability, 3 + rnd(1)
if len(humanresponse$) < 8 and find sub string$(humanresponse$, "?") <= 0 and find sub string$(extractedstring$, "?") > len(extractedstring$) - 4 and len(extractedstring$) > 5 and find sub string$(extractedstring$, ".") <= 0 then inc usageprobability, 5 + rnd(2)
if extractedstring$ = humanresponse$ then dec usageprobability, 10
rem Perform a quick scan to check for word comparisons in the human's input and the AI's potential response
if sf contains(humanresponse$, " ") > 0 and len(humanresponse$) > 4 and thingssaid > 5 and extractedstring$ <> humanresponse$
passedstring$ = humanresponse$
removepunctuation()
humanresponsetemp$ = lower$(passedstring$)
passedstring$ = extractedstring$
removepunctuation()
extractedstringtemp$ = lower$(passedstring$)
wordextracted$ = first token$(extractedstringtemp$, " ")
repeat
if instr(wordextracted$, humanresponsetemp$) > 0 and len(wordextracted$) > 2 then inc usageprobability, 2 + (clamp(len(wordextracted$), 1, 9) / 1.5)
wordextracted$ = next token$(" ")
until wordextracted$ = ""
endif
rem Make sure the AI doesn't use early-conversation phrases later in the conversation
if thingssaid > 5 and timesfilescanned <= 8 then dec usageprobability, 10 - timesfilesscanned
rem Increase the probability if the AI has not yet said this phrase. This helps it to learn.
if rnd(3) > 0 or expandknowledge = 1
array index to top aihasnotsaid$()
while array index valid(aihasnotsaid$()) = 1
if extractedstring$ = aihasnotsaid$()
inc usageprobability, 1
if instr(extractedstring$, "?") > 0 then inc usageprobability, 2
if expandknowledge = 1 then inc usageprobability, 10
endif
next array index aihasnotsaid$()
endwhile
endif
rem Check for repetitions in the current conversation - Suspect buggy code!
checkforrepeating()
endfunction
function checkmemoryforresponse()
bestmemorybackup = 0
rem Perform a check to establish conversation keywords - AI's 'intelligent' algorithm
if thingssaid > 4 then locateconversationkeywords()
rem Perform a scan to see what the user's response was, if the AI ever said something similar before itself
memorysearch = 0
oldbestmem = 0
gainedfrommemcheck = 0
do
filenotfound = 0
inc memorysearch
if file exist("chat" + str$(memorysearch) + ".txt") = 1
open datafile to read 4, "chat" + str$(memorysearch) + ".txt"
inc filesopened
else
filenotfound = 1
if filesopened => chatdatabase or memorysearch > (chatdatabase*4) then exit
endif
if filenotfound = 0
do
comparestring$ = datafile string$(4)
if datafile end(4) = 1 then close datafile 4 : exit
inc memoryconversationposition
rem Search for AI responses
if fast left$(comparestring$, 3) = "AI:"
usageprobability = 0
comparestring$ = fast right$(comparestring$, fast len(comparestring$) - 3)
rem Compare the player's input with the AI's input
rem Remove the punctuation from both strings and expand any negative contractions - assign humanmemtemp$ as the temporary variable for humanresponse$
comparestring$ = fast lower$(comparestring$)
rem Before punctuation is removed, check to see if either one is a question but the other isn't, and alter probability
if (instr(comparestring$, "?") > 0 and instr(humanmemtemp$, "?") <= 0) or (instr(comparestring$, "?") <= 0 and instr(humanmemtemp$, "?") > 0) then dec usageprobability, 6
originalcomparestring$ = comparestring$ `Store the old comparestring$
passedstring$ = fast lower$(comparestring$)
removepunctuation()
expandapostrophies()
comparestring$ = passedstring$
rem Check to make sure that after the punctuation has been removed, the string is still valid - fixes the '?!' crash
if fast len(comparestring$) > 0
passedstring$ = fast lower$(humanresponse$)
removepunctuation()
expandapostrophies()
humanmemtemp$ = passedstring$
if goingwrong = 1 then break
rem Determine the longest word in the string
if string count(comparestring$, " ") > 2
wordextracted$ = first token$(comparestring$, " ")
repeat
if len(wordextracted$) > comparestringlongest and len(wordextracted$) > 5 and len(wordextracted$)
comparestringlongest = len(wordextracted$)
comparestringlongestword$ = wordextracted$
endif
wordextracted$ = next token$(" ")
until wordextracted$ = ""
endif
rem Save a copy of comparestring$ so it can be restored later
oldcomparestring$ = comparestring$
wordextracted$ = first token$(humanmemtemp$," ")
repeat
memtemp1 = find sub string$(comparestring$, wordextracted$)
rem Improved code to make sure that the comparestring word is a whole word, and not part of a word
memtemp2 = 0
if memtemp1 > 0
if mid$(comparestring$, memtemp1 - 1) = " " or memtemp1 - 1 = 0 then inc memtemp2
if mid$(comparestring$, memtemp1 + len(wordextracted$)) = " " or memtemp1 + len(wordextracted$) > len(comparestring$) then inc memtemp2
endif
rem If the comparison is correct, analyse the words and adjust the probability of usage
if memtemp2 = 2
if wordextracted$ <> "not#" then inc memwordsmatched
if len(wordextracted$) < 5
wordprobability = (floor(clamp(len(wordextracted$), 2, 4) * 0.8)) + clamp(memwordsmatched, 0, 3)
else
wordprobability = (floor(clamp(len(wordextracted$), 2, 8) * 1.2)) + clamp(memwordsmatched, 0, 3)
endif
if string count(humanmemtemp$, " ") > 1 and string count(humanmemtemp$, " ") < 4 then inc wordprobability, 2
inc usageprobability, wordprobability
rem WORD SPECIFIC ALGORITHMS, FOR DETERMINING FACTORS BETWEEN WORDS.
rem Scan against the common words list; if it fits with one, decrease the probability
if thingssaid > 4 and sf contains(humanmemtemp$, " ") > 2 then checkcommonwords()
if identifiedcommonword = 1 then dec memwordsmatched
rem Check to see if the words are in the same order in comparestring and humanmemtemp
if compareposition > 0
if find sub string$(comparestring$, wordextracted$) > compareposition and find sub string$(humanmemtemp$, wordextracted$) <= humanmemposition
if rnd(1) = 1 then dec usageprobability
else
inc usageprobability
endif
endif
compareposition = find sub string$(comparestring$, wordextracted$)
humanmemposition = find sub string$(humanmemtemp$, wordextracted$)
rem Common question words aren't quite as reliable as other words - BLANKED OUT AT THE MOMENT FOR TESTING
rem if (wordextracted$ = "why" or wordextracted$ = "how" or wordextracted$ = "when") and sf contains(humanmemtemp$, " ") = 1 then dec usageprobability, 3
rem Be cautious around matching small, more generic words
if len(wordextracted$) < 5 and sf contains(humanmemtemp$, " ") > 1 then dec usageprobability, wordprobability / 1.5
rem Code to make sure numbers are identified, and probability is increased accordingly.
if val(wordextracted$) > 0 and len(wordextracted$) < 5 then inc usageprobability, 4 + rnd(1)
rem Check to see if the word has appeared in recent conversation - Won't scan if the word is one of the commonwords$ list
if len(wordextracted$) > comparestringlongest - 2 and comparestringlongest > 0 and identifiedcommonword = 0 then memoryrecentcheck()
rem If the word matches up against the longest word in comparestring, give a significant boost to the probability
if wordextracted$ = comparestringlongestword$ then inc usageprobability, 3 + (clamp(comparestringlongest, 1, 10) / 2.4) - clamp(2 - memwordsmatched, 0, 2)
comparestring$ = replace$(comparestring$, wordextracted$, generate string$("@", fast len(wordextracted$)))
else
rem THE FLIP SIDE: If the words don't match, impose a penalty.
inc wordsnotmatched
dec usageprobability, 1 + (floor(clamp(len(wordextracted$), 1, 10)) / 8)
if wordextracted$ = comparestringlongestword$ then dec usageprobability, 2
rem Check to see if wordextracted$ was a question word - If it was, give an extra penalty
for k = 0 to 5
if wordextracted$ = commonquestions$(k) then dec usageprobability, 2 : exit
next k
endif
wordextracted$ = next token$(" ")
inc wordsinstring
until wordextracted$ = ""
rem Restore comparestring$ from it's saved version
comparestring$ = oldcomparestring$
if memwordsmatched > 1
rem Perform a quick scan of comparestring$
rem Check to see if string contains words from the current conversation.
array index to top conversationkeywords$()
while array index valid(conversationkeywords$()) = 1
if instr(comparestring$, conversationkeywords$()) then inc usageprobability : inc conversationmatches
next array index conversationkeywords$()
endwhile
if conversationmatches < 3 and string count(comparestring$, " ") > 3 then dec usageprobability, 1 + string count(comparestring$, " ")
conversationmatches = 0
wordextracted$ = first token$(comparestring$, " ")
repeat
if trim$(wordextracted$) = "definately" and dontrun = 2
endif
memtemp1 = instr(humanmemtemp$, wordextracted$)
rem Improved code to make sure that the humanmemtemp word is a whole word, and not part of a word
memtemp2 = 0
if memtemp1 > 0
if mid$(humanmemtemp$, memtemp1 - 1) = " " or memtemp1 - 1 = 0 then inc memtemp2
if mid$(humanmemtemp$, memtemp1 + len(wordextracted$)) = " " or memtemp1 + len(wordextracted$) > len(humanmemtemp$) then inc memtemp2
endif
if memtemp2 < 2
inc wordsnotmatched2
if wordsnotmatched2 > wordsnotmatched
dec usageprobability, 1 + (floor(clamp(len(wordextracted$), 1, 10)) / 8)
if wordsnotmatched > 2 or wordsnotmatched2 > 2 then dec usageprobability
endif
endif
wordextracted$ = next token$(" ")
until wordextracted$ = ""
endif
rem If all the words in the string matched, it's perfect; give it a large boost
if memwordsmatched = (string count(comparestring$, " ") + 1) and usageprobability < 25 then inc usageprobability, 18
rem GENERAL ALGORITHMS, FOR IDENTIFYING THINGS IN THE MAIN STRINGS
if usageprobability > 4 or (usageprobability > 0 and thingssaid < 3)
passedstring$ = "Before final check"
rem After scanning has been done, check the response against the anti-repeat checker - oldcomparestring$ preserves comparestring$
oldcomparestring$ = comparestring$
extractedstring$ = humanmemtemp$
rem checkforrepeating()
comparestring$ = oldcomparestring$
rem Also, check the string as a whole for question - question matches.
if find sub string$(humanmemtemp$, "?") > len(humanmemtemp$) - 3 and find sub string$(comparestring$, "?") > len(comparestring$) - 3 then dec usageprobability, 4 - conversationthread * 1.5
rem If this phrase was found in the same file as the last best one, and close to where the last one was found, increase the probability
if memorysearch = bestmemfoundat and (datafile position(4) > bestmemposition - 5 and datafile position(4) < bestmemposition + 180) then inc usageprobability, (3 + memwordsmatched + (180 - (bestmemposition - datafile position(4))) / 48) + conversationthread : conversationthread = clamp(conversationthread + 2, 0, 6)
rem Otherwise...
if bestmemfoundat <> memorysearch and bestmemfoundat > 0 and memwordmatched < 4
dec usageprobability, 2
else
if bestmemfoundat = memorysearch then inc usageprobability, 2
endif
rem Increase the probability in strings with high matches but few words
if memwordsmatched > string count(humanmemtemp$, " ") and string count(humanmemtemp$, " ") < 3 then inc usageprobability, memwordsmatched rem + memwordsmatched * 2
rem If the two strings are too different in length, decrease probability
if abs(len(comparestring$) - len(humanmemtemp$)) > 8 then dec usageprobability, abs(len(comparestring$) - len(humanmemtemp$)) / 4
rem If only one word matched up, it's not likely to be a good match, unless there is only one word in the string
if memwordsmatched = 1 and sf contains(humanmemtemp$, " ") > 0 then dec usageprobability, 6
if sf contains(humanmemtemp$, " ") = 0 then usageprobability = usageprobability * 3
rem If no words were matched, it's almost certainely bad.
if memwordsmatched = 0 then usageprobability = usageprobability / 4
if thingssaid < 3 then inc usageprobability, 5
passedstring$ = "After final check"
word1$ = ""
word2$ = ""
word3$ = ""
word4$ = ""
endif
if bestmemorybackup < usageprobability then bestmemorybackup = usageprobability
rem Store the best response found so far
if usageprobability > bestmemory and usageprobability > 4 or (usageprobability = bestmemory and bestmemory > 3 and rnd(3) = 1)
debugvariable = usageprobability
bestmemory = usageprobability
bestmemfoundattemp = memorysearch
bestmempositiontemp = datafile position(4)
inc timesstored
bestcomparestring$ = originalcomparestring$
oldbestmem = bestmemory
endif
endif
memwordsmatched = 0
comparestringlongest = 0
comparestringlongestword$ = ""
usageprobability = 0
wordsnotmatched = 0
wordsnotmatched2 = 0
endif
rem Reset variables before the loop completes
identifiedcommonword = 0
compareposition = 0
humanmemposition = 0
lengthtodeduct = 0
inc timesscanned
loop
usageprobability = 0
endif
loop
if shiftkey() = 1 then break
if bestmemory > 6 and bestcomparestring$ > "" then collectbeststrings()
if beststring$ > ""
if memorysearch <> bestmemfoundat then conversationthread = 0
bestmemfoundat = bestmemfoundattemp
bestmemposition = bestmempositiontemp
oldbeststring$ = beststring$
endif
timesscanned = 0
memorysearch = 0
filesopened = 0
endfunction
function removepunctuation()
for k = 1 to 8
if k = 1 then punctuation$ = "!"
if k = 2 then punctuation$ = "?"
if k = 3 then punctuation$ = ","
if k = 4 then punctuation$ = "'"
if k = 5 then punctuation$ = "."
if k = 6 then punctuation$ = "("
if k = 7 then punctuation$ = ")"
if k = 8 then punctuation$ = "-"
passedstring$ = replace all$(passedstring$, punctuation$, "")
passedstring$ = trim$(passedstring$)
next k
endfunction
function checkforrepeating()
rem Some code that helps the AI not to repeat itself too much
if thingssaid > 2
array index to top currentconversation$()
while array index valid(currentconversation$())
comparestring$ = currentconversation$()
if left$(comparestring$, 3) = "AI:"
comparestring$ = right$(comparestring$, len(comparestring$) - 3)
rem Remove the punctuation from the two strings so they can be easily compared
passedstring$ = comparestring$
removepunctuation()
comparestring$ = lower$(passedstring$)
if memorysearch > 0
passedstring$ = comparestring$
else
passedstring$ = extractedstring$
removepunctuation()
endif
extractedtemp$ = lower$(passedstring$)
if comparestring$ = extractedtemp$
dec usageprobability, 8 + rnd(2)
endif
endif
next array index currentconversation$()
endwhile
endif
endfunction
function drawchat()
if thingssaid > 0
array index to top currentconversation$()
while array index valid(currentconversation$()) = 1
rem Draw the conversation so far to the screen
chattext$ = currentconversation$()
if left$(chattext$, 3) = "AI:"
chattext$ = right$(chattext$, len(chattext$) - 3)
//print chattext$
endif
next array index currentconversation$()
endwhile
print chattext$
endif
if terriblefatetimes > 2
if tfstart = 0
tfnow = timer()
tfstart = 1
endif
if timer() - tfnow > 4000 then end
endif
if pulse = 0 and rnd(10000) = 1 then pulse = 1
endfunction
function checkfornegativeresponse()
rem Some code that attempts to pick up whether certain phrases don't make sense and shouldn't be used
rem Works by looking for patterns in responses
if thingssaid > 3
endif
endfunction
function expandapostrophies()
rem Does not just expand apostrophies, spaces certain things out as well.
for i = 0 to array count(ntwordstoexpand$(0))
if sf contains(passedstring$, ntwordstoexpand$(i)) = 1
if ntwordstoexpand$(i) = "cant" or ntwordstoexpand$(i) = "wont" or ntwordstoexpand$(i) = "shant"
inc lengthtodeduct, (4 * string count(passedstring$, "cant"))
inc lengthtodeduct, (5 * string count(passedstring$, "wont"))
inc lengthtodeduct, (5 * string count(passedstring$, "shant"))
passedstring$ = replace all$(passedstring$, "cant", "can not#")
passedstring$ = replace all$(passedstring$, "wont", "will not#")
passedstring$ = replace all$(passedstring$, "shant", "shall not#")
inc lengthtodeduct, 5
else
inc lengthtodeduct, 3 * string count(passedstring$, ntwordstoexpand$(i))
passedstring$ = replace all$(passedstring$, ntwordstoexpand$(i), left$(ntwordstoexpand$(i), len(ntwordstoexpand$(i)) - 2) + " not#")
endif
exit
endif
next i
expandendingstemp$ = first token$(passedstring$, " ")
repeat
if fast right$(expandendingstemp$, 1) = "s" and fast len(expandendingstemp$) > 4 then expandendingstemp$ = fast left$(expandendingstemp$, fast len(expandendingstemp$) - 1) + " s" : inc lengthtodeduct
if fast right$(expandendingstemp$, 3) = "ing" and fast len(expandendingstemp$) > 6 then expandendingstemp$ = fast left$(expandendingstemp$, fast len(expandendingstemp$) - 3) + " ing" : inc lengthtodeduct
temppassedstring$ = temppassedstring$ + (expandendingstemp$ + " ")
expandendingstemp$ = next token$(" ")
until expandendingstemp$ = ""
passedstring$ = trim$(temppassedstring$)
endfunction
function memoryrecentcheck()
exitfunction
if thingssaid < 5 then exitfunction
array index to bottom currentconversation$()
for h = 1 to 3
passedstring$ = fast lower$(currentconversation$())
if fast left$(passedstring$, 3) = "ai:" then passedstring$ = fast right$(passedstring$, fast len(passedstring$) - 3)
if fast left$(passedstring$, 5) = "user:" then passedstring$ = fast right$(passedstring$, fast len(passedstring$) - 5)
removepunctuation()
expandapostrophies()
tempstring$ = passedstring$
if len(tempstring$) > len(wordextracted$)
if find sub string$(tempstring$, wordextracted$) > 0 then inc usageprobability, 2 + len(wordextracted$) / 3 : inc gainedfrommemcheck, 1 + len(wordextracted$) / 4
endif
previous array index currentconversation$()
if array index valid(currentconversation$()) = 0 then exit
next h
tempstring$ = ""
endfunction
function replacecontractions()
wordextracted$ = first token$(passedstring$, " ")
repeat
if right$(wordextracted$, 2) = "nt"
for i = 0 to array count(ntwordstoexpand$(0))
if wordextracted$ = ntwordstoexpand$(i)
contractionmatch = 1
tempcontract$ = ntwordstoexpand$(i)
tempcontract$ = insert$(tempcontract$, len(tempcontract$), "'")
exit
endif
next i
endif
if contractionmatch = 1 then exit
wordextracted$ = next token$(" ")
until wordextracted$ = ""
rem If a contraction was identified...
if contractionmatch = 1
wordextracted$ = first token$(beststring$, " ")
repeat
dontrestore = 0
wordextractedtemp$ = wordextracted$
passedstring$ = wordextracted$
removepunctuation()
wordextracted$ = lower$(passedstring$)
if right$(wordextracted$, 2) = "nt"
for k = 0 to array count(ntwordstoexpand$(0))
if wordextracted$ = ntwordstoexpand$(k)
for o = 1 to 3
if o = 1 then punctuation$ = "!"
if o = 2 then punctuation$ = "?"
if o = 3 then punctuation$ = "."
if find char(wordextracted$, punctuation$) = find char(wordextracted$, ntwordstoexpand$(k)) - 2 then tempcontract$ = upper$(left$(tempcontract$, 1)) + right$(tempcontract$, len(tempcontract$) - 1)
next o
wordextracted$ = tempcontract$ : dontrestore = 1
endif
next k
endif
if dontrestore = 0 then wordextracted$ = wordextractedtemp$
tempstring$ = tempstring$ + (wordextracted$ + " ")
wordextracted$ = next token$(" ")
until wordextracted$ = ""
beststring$ = tempstring$
endif
contractionmatch = 0
tempstring$ = ""
endfunction
function checkcommonwords()
for i = 0 to array count(commonwords$(0))
if wordextracted$ = commonwords$(i) then dec usageprobability, wordprobability / 2 : identifiedcommonword = 1 : exit
next i
endfunction
function locateconversationkeywords()
empty array conversationkeywords$()
array index to bottom currentconversation$()
for h = 1 to 4
passedstring$ = fast lower$(currentconversation$())
if fast left$(passedstring$, 3) = "ai:" then passedstring$ = fast right$(passedstring$, fast len(passedstring$) - 3)
removepunctuation()
expandapostrophies()
wordextracted$ = first token$(passedstring$, " ")
repeat
checkcommonwords()
for k = 0 to 5
if wordextracted$ = commonquestions$(k) then identifiedquestionword = 1
next k
if identifiedcommonword = 0 and identifiedquestionword = 0 and wordextracted$ <> "not#" and len(wordextracted$) > 3
array insert at bottom conversationkeywords$()
conversationkeywords$() = wordextracted$
endif
identifiedcommonword = 0
identifiedquestionword = 0
wordextracted$ = next token$(" ")
until wordextracted$ = ""
previous array index currentconversation$()
if array index valid(currentconversation$()) = 0 then exit
next h
passedstring$ = ""
wordextracted$ = ""
endfunction
function collectbeststrings()
rem Chooses the string to be used based on more loops and more probability crap.
empty array beststringlist$()
empty array beststringlist2$()
do
filenotfound = 0
inc beststringsearch
if file exist("chat" + str$(beststringsearch) + ".txt") = 1
open datafile to read 7, "chat" + str$(beststringsearch) + ".txt"
inc filesopened
else
filenotfound = 1
if filesopened => chatdatabase or beststringsearch > (chatdatabase*4) then exit
endif
if filenotfound = 0
do
extractedstring$ = datafile string$(7)
if datafile end(7) = 1 then close datafile 7 : exit
if fast left$(extractedstring$, 3) = "AI:"
extractedstring$ = trim$(fast right$(extractedstring$, fast len(extractedstring$) - 3))
passedstring$ = lower$(extractedstring$)
removepunctuation()
expandapostrophies()
extractedstring$ = passedstring$
passedstring$ = lower$(bestcomparestring$)
removepunctuation()
expandapostrophies()
bestcomparestring$ = passedstring$
if extractedstring$ = bestcomparestring$
possiblebeststring$ = ""
while fast left$(possiblebeststring$, 5) <> "User:"
possiblebeststring$ = datafile string$(7)
if datafile end(7) = 1 then exit
endwhile
if datafile end(7) = 0
possiblebeststring$ = fast right$(possiblebeststring$, fast len(possiblebeststring$) - 5)
if possiblebeststring$ <> humanresponse$ or beststringsearch <> chatnumber
bestmemfoundattemp = beststringsearch
bestmempositiontemp = datafile position(7)
array insert at bottom beststringlist$()
beststringlist$() = possiblebeststring$ + ":" + str$(bestmemfoundattemp) + "#" + str$(bestmempositiontemp)
passedstring$ = fast lower$(possiblebeststring$)
removepunctuation()
expandapostrophies()
array insert at bottom beststringlist2$()
beststringlist2$() = passedstring$
endif
else
possiblebeststring$ = ""
endif
endif
endif
loop
endif
loop
tfnow = timer()
while timer() - tfnow < 500 and dontrun = 2
keywordtextpos = 0
array index to top beststringlist$()
while array index valid(beststringlist$()) = 1
text 0, keywordtextpos, beststringlist$()
inc keywordtextpos, 22
next array index beststringlist$()
endwhile
text 0, keywordtextpos, "COUNT: " + str$(array count(beststringlist$()))
endwhile
if array count(beststringlist$()) > -1 then choosebeststring()
beststringsearch = 0
filenotfound = 0
endfunction
function choosebeststring()
while inkey$() = "" and dontrun = 2
keywordtextpos = 0
array index to top beststringlist2$()
while array index valid(beststringlist2$()) = 1
text 0, keywordtextpos, beststringlist2$()
inc keywordtextpos, 22
next array index beststringlist2$()
endwhile
endwhile
beststringoccurhighest = 0
highestoccurstring$ = ""
array index to top beststringlist$()
repeat
usageprobability = 5
passedstring$ = fast lower$(fast left$(beststringlist$(), last instr(beststringlist$(), ":") - 1))
if stringtoignorecounting$ = "" and rnd(2) = 1
stringtoignorecounting$ = passedstring$
endif
removepunctuation()
expandapostrophies()
beststringlistread$ = passedstring$
beststringoccurances = 0
array index to top beststringlist2$()
while array index valid(beststringlist2$()) = 1
if beststringlistread$ = beststringlist2$()
inc beststringoccurances
if beststringoccurances > 1 and beststringoccurances < 9 then inc usageprobability
endif
next array index beststringlist2$()
endwhile
rem This bit of code tries to give the AI a little more variation in it's responses by sometimes eliminating the most common response.
array index to top conversationkeywords$()
while array index valid(conversationkeywords$()) = 1
if instr(beststringlistread$, conversationkeywords$()) then inc usageprobability, 2 + floor(fast len(wordextracted$)/7) : inc conversationmatches
next array index conversationkeywords$()
endwhile
if conversationmatches < 2 and string count(comparestring$, " ") > 3 then dec usageprobability, string count(comparestring$, " ")
conversationmatches = 0
rem Increase usageprobability by a lot if this is in the unusedphrases list
array index to top aihasnotsaid$()
while array index valid(aihasnotsaid$()) = 1
if lower$(fast left$(beststringlist$(), last instr(beststringlist$(), ":") - 1)) = lower$(aihasnotsaid$()) then inc usageprobability, 20
next array index aihasnotsaid$()
endwhile
if usageprobability < 1 then usageprobability = 1
if fast left$(beststringlist$(), last instr(beststringlist$(), ":") - 1) = oldbeststring$ then usageprobability = -1
while inkey$() = "" and dontrun = 2
text 0, 0, beststringlist$()
text 0, 30, beststringlistread$
text 0, 50, str$(usageprobability)
keywordtextpos = 40
array index to top conversationkeywords$()
while array index valid(conversationkeywords$()) = 1
text 130, keywordtextpos, conversationkeywords$()
inc keywordtextpos, 20
next array index conversationkeywords$()
endwhile
endwhile
if beststringprobability < usageprobability or (beststringprobability = usageprobability and rnd(2) = 1)
rem Code that extracts beststring$, the number saying what file it was found in, and where in that file it was found.
beststring$ = fast left$(beststringlist$(), last instr(beststringlist$(), ":") - 1)
bestmemfoundtemp$ = fast right$(beststringlist$(), fast len(beststringlist$()) - last instr(beststringlist$(), ":"))
bestmemfoundattemp = val(fast left$(bestmemfoundtemp$, instr(bestmemfoundtemp$, "#") - 1))
bestmempositiontemp = val(fast right$(beststringlist$(), fast len(beststringlist$()) - last instr(beststringlist$(), "#")))
beststringprobability = usageprobability
endif
next array index beststringlist$()
until array index valid(beststringlist$()) = 0
stringtoignorecounting$ = ""
beststringprobability = 0
usageprobability = 0
endfunction