Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- aiml = {
- players = {},
- topics = {default = {}},
- botData = {}
- }
- emote = ""
- defaultResponses = {
- "Uhh..?",
- "Umm..?",
- "What..?",
- "Huh..?",
- "Uhh..?",
- "Alright..?",
- "Okay..?",
- "What..?",
- "Nani..?"
- }
- screendata = ""
- aiml.botData["flower"] = "blue orchid"
- aiml.botData["butterfly"] = "the pretty yellow ones"
- aiml.botData["block"] = "pink wool"
- aiml.botData["item"] = "pink dye"
- aiml.botData["color"] = "pink"
- aiml.botData["creator"] = "wiggle1000"
- function tablelength(T)
- local count = 0
- for _ in pairs(T) do count = count + 1 end
- return count
- end
- function string.split(rawString, separator)
- local splittedString = {}
- for str in string.gmatch(rawString, "[^"..separator.."]+") do
- table.insert(splittedString, str)
- end
- return splittedString
- end
- function string.title(rawString)
- if type(string.split) == "function" then
- allWords = string.split(rawString, " ")
- for index, word in pairs(allWords) do
- allWords[index] = string.gsub(string.lower(word), "%a", string.upper, 1)
- end
- return table.concat(allWords, " ")
- else
- error("string.split function is needed")
- end
- end
- function string.trim(rawString, pattern)
- rawString = string.gsub(rawString, "%s+", " ")
- if string.sub(rawString, 0, 1) == " " then
- rawString = string.sub(rawString, 2)
- end
- if string.sub(rawString, -1) == " " then
- rawString = string.sub(rawString, 0, -2)
- end
- return string.gsub(rawString, pattern, "")
- end
- function trim1(s)
- return (s:gsub("^%s*(.-)%s*$", "%1"))
- end
- function aiml.load(file)
- local topics = aiml.topics
- --for nodeData, topicData in string.gmatch(file, "<topic (.-)>(.-)</topic>") do
- topicName = "default"--string.match(nodeData, "name=[\"\'](.-)[\"\']")
- for i,v in pairs(aiml.retrieveCategorys(file)) do
- topics[topicName][i] = v--aiml.retrieveCategorys(topicData)
- end
- --end
- aiml.topics = topics
- end
- function aiml.retrieveCategorys(xmlData)
- local categorys = {}
- local hasPattern = false
- local hasTemplate = false
- --print("A")
- for nodeData in string.gmatch(xmlData, "<category>(.-)</category>") do
- ---print("B")
- local input, output
- if string.find(nodeData, "<pattern>(.-)</pattern>") ~= nil then
- ---print("C")
- hasPattern = true
- input = string.trim(string.match(nodeData, "<pattern>(.-)</pattern>"), "[\n\t]")
- end
- if string.find(nodeData, "<template>(.-)</template>") ~= nil then
- ---print("D")
- hasTemplate = true
- output = string.trim(string.match(nodeData, "<template>(.-)</template>"), "[\n\t]")
- end
- if hasTemplate and hasPattern then
- ---print(input.."->"..output..". OK")
- categorys[input] = output
- end
- end
- --print(tablelength(categorys))
- return categorys
- end
- function aiml.parseTemplate(player, pattern, input, template, dat)
- local topic = aiml.players[player].topic or "default"
- --print(template)
- if string.match(template, "<user/>") then
- local srai = string.match(template, "<user/>")
- template = string.gsub(template, "<user/>", "<get name=\"name\"/>-<get name=\"relation\"/>")
- end
- if string.match(template, "<DAT/>") then
- local srai = string.match(template, "<DAT/>")
- template = string.gsub(template, "<DAT/>", dat)
- end
- if string.match(template, "<surprise/>") then
- local srai = string.match(template, "<surprise/>")
- template = string.gsub(template, "<surprise/>", "<random><li>Y- you mean me?</li><li>H- huh?</li><li>W- who? Me?</li><li>Wh- what?</li><li>M- me?</li></random> ")
- end
- if string.match(template, "<affirmative/>") then
- local srai = string.match(template, "<affirmative/>")
- template = string.gsub(template, "<affirmative/>", "<random><li>Hai</li><li>Hai-hai</li><li>OK</li><li>Will do</li><li>Right away</li><li>Kay</li><li>Okay</li></random>")
- end
- while string.match(template, "<random>(.-)</random>") do
- local randomTemplate = string.match(template, "<random>(.-)</random>")
- --print(randomTemplate)
- local randomTable = {}
- for str in string.gmatch(randomTemplate, "<li>(.-)</li>") do
- --print(str)
- table.insert(randomTable, str)
- end
- template = string.gsub(template, "<random>(.-)</random>", randomTable[math.random(#randomTable)], 1)
- end
- if string.match(template, "<srai>(.-)</srai>") then
- local srai = string.match(template, "<srai>(.-)</srai>")
- local raisedTemplate = aiml.getResponse(player, srai)
- template = string.gsub(template, "<srai>"..srai.."</srai>", raisedTemplate)
- end
- if string.match(template, "<emote>(.-)</emote>") then
- emote = string.match(template, "<emote>(.-)</emote>")
- template = string.gsub(template, "<emote>(.-)</emote>", "")
- end
- if string.match(template, "<star index=[\'\"](.-)[\'\"] />") then
- local starIndex = string.match(template, "<star index=[\'\"](.-)[\'\"] />")
- local patternIndexes = string.split(input, " ")
- if starIndex ~= "all" then
- starIndex = tonumber(starIndex) or 0
- starIndex = string.title(patternIndexes[starIndex] or "")
- else
- starIndex = string.title(input)
- end
- template = string.gsub(template, "<star index=[\'\"](.-)[\'\"] />", starIndex)
- end
- if string.match(template, "<set (.-)=[\'\"](.-)[\'\"]>(.-)</set>") then
- local setType, setData, setValue = string.match(template, "<set (.-)=[\'\"](.-)[\'\"]>(.-)</set>")
- if setType == "topic" then
- if aiml.topics[setData] then
- aiml.players[player].topic = setData
- end
- elseif setType == "data" then
- aiml.players[player].data[setData] = setValue
- template = string.gsub(template, "<set (.-)=[\'\"](.-)[\'\"]>(.-)</set>", aiml.players[player].data[setData])
- elseif setType == "datacopy" then
- aiml.players[player].data[setData] = dat
- template = string.gsub(template, "<set (.-)=[\'\"](.-)[\'\"]>(.-)</set>", aiml.players[player].data[setData])
- end
- template = string.gsub(template, "<set (.-)=[\'\"](.-)[\'\"]>(.-)</set>", "")
- end
- while string.match(template, "<get name=\"(.-)\"/>") do
- local getValue = string.match(template, "<get name=\"(.-)\"/>")
- if aiml.players[player].data[getValue] then
- template = string.gsub(template, "<get name=\"(.-)\"/>", aiml.players[player].data[getValue],1)
- else
- template = string.gsub(template, "<get name=\"(.-)\"/>", "",1)
- end
- end
- while string.match(template, "<getbot name=\"(.-)\"/>") do
- local getValue = string.match(template, "<getbot name=\"(.-)\"/>")
- if aiml.botData[getValue] then
- template = string.gsub(template, "<getbot name=\"(.-)\"/>", aiml.botData[getValue],1)
- else
- template = string.gsub(template, "<getbot name=\"(.-)\"/>", "",1)
- end
- end
- if string.match(template, "<condition data=[\'\"](.-)[\'\"] value=[\'\"](.-)[\'\"]>(.-)</condition>") then
- local conditionKey, conditionValue, conditionText = string.match(template, "<condition data=[\'\"](.-)[\'\"] value=[\'\"](.-)[\'\"]>(.-)</condition>")
- if aiml.players[player].data[conditionKey] then
- if aiml.players[player].data[conditionKey] == conditionValue then
- template = string.gsub(template, "<condition data=[\'\"](.-)[\'\"] value=[\'\"](.-)[\'\"]>(.-)</condition>", conditionText)
- else
- template = string.gsub(template, "<condition data=[\'\"](.-)[\'\"] value=[\'\"](.-)[\'\"]>(.-)</condition>", "")
- end
- else
- template = string.gsub(template, "<condition data=[\'\"](.-)[\'\"] value=[\'\"](.-)[\'\"]>(.-)</condition>", "")
- end
- end
- if string.match(template, "<think>(.-)</think>") then
- local srai = string.match(template, "<think>(.-)</think>")
- template = string.gsub(template, "<think>(.-)</think>", "")
- end
- if string.match(template, "<screendata>(.-)</screendata>") then
- local srai = string.match(template, "<screendata>(.-)</screendata>")
- screendata = srai
- template = string.gsub(template, "<screendata>(.-)</screendata>", "")
- end
- return template
- end
- function aiml.getResponse(player, input)
- local topic = aiml.players[player].topic or "default"
- local originalInput = string.gsub(input, "[^a-zA-Z0-9 -]", "")
- input = string.gsub(string.upper(input), "[^a-zA-Z0-9 -]", "")
- local hasStar = false
- local dat = nil
- if(aiml.topics[topic]==nil)then
- topic = "default"
- end
- for pattern, template in pairs(aiml.topics[topic]) do
- local pattern = pattern
- if string.find(pattern, "*") then
- pattern = string.gsub(pattern, "*", "(.-)")
- end
- if string.find(pattern, "<DAT>") then
- local before = string.match(pattern, "(.-)<DAT>")
- local after = string.match(pattern, "<DAT>(.-)")
- local s = input
- if(before~=nil)then
- s = string.gsub(input, before, "")
- end
- if(after~=nil)then
- s = string.gsub(s, after, "")
- end
- dat = trim1(string.lower(s))
- pattern = string.gsub(pattern, "<DAT>", "(.-)")
- end
- if pattern ~= "(.-)" then
- if input == pattern or string.match(input, pattern) then
- return aiml.parseTemplate(player, pattern, input, template, dat), false
- end
- else
- hasStar = {player, pattern, input, template}
- end
- end
- if hasStar then
- return aiml.parseTemplate(table.unpack(hasStar)), false
- end
- return string.format(defaultResponses[math.random(1,#defaultResponses)], input), true
- end
- local savePlayerData = function(player)
- local dfile = "data/"..player
- if(fs.exists(dfile))then
- fs.delete(dfile)
- end
- local datfile = fs.open(dfile, "w")
- datfile.write(textutils.serialize(aiml.players[player].data))
- datfile.close()
- end
- local loadPlayerData = function(player)
- local dfile = "data/"..player
- if(fs.exists(dfile))then
- local datfile = fs.open(dfile, "r")
- local dat = textutils.unserialize(datfile.readAll())
- datfile.close()
- return dat
- else
- local dat = {}
- dat["name"] = player
- dat["relation"] = "oniichan"
- dat["botmaster"] = "wiggle1000"
- dat["myname"] = "Base-chan"
- return dat
- end
- end
- function correctString(str)
- local tempwords = string.split(str," ")
- local outstring = ""
- for i,g in pairs(tempwords) do
- local v = string.upper(g)
- --print("|"..v.."|")
- if(v == "I'M" or v == "IM")then
- v = "I AM"
- elseif(v == "THAT'S" or v == "THATS")then
- v = "THAT IS"
- elseif(v == "IT'S")then
- v = "IT IS"
- end
- outstring = outstring..v.." "
- end
- return outstring
- end
- function getResponseToPlayer(player, command)
- emote = nil
- if not aiml.players[player] then
- aiml.players[player] = {topic = "default", data = loadPlayerData(player)}
- end
- --print(correctString(command))
- local response, isDefault = aiml.getResponse(player, correctString(command))
- savePlayerData(player)
- return response, emote, isDefault
- end
- function loadFile(fname)
- print("LOADING: "..fname)
- local file = fs.open("knowledge/"..fname, "r")
- --print(file.readAll())
- aiml.load(file.readAll())
- file.close()
- end
- function loadKnowledge()
- for i,v in pairs(fs.list("knowledge")) do
- loadFile(v)
- end
- print("TOTAL SAMPLES: "..tablelength(aiml.topics.default))
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement