Advertisement
Guest User

AIML.lua

a guest
Jun 24th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.57 KB | None | 0 0
  1. aiml = {
  2.     players = {},
  3.     topics = {default = {}},
  4.     botData = {}
  5. }
  6. emote = ""
  7. defaultResponses = {
  8.     "Uhh..?",
  9.     "Umm..?",
  10.     "What..?",
  11.     "Huh..?",
  12.     "Uhh..?",
  13.     "Alright..?",
  14.     "Okay..?",
  15.     "What..?",
  16.     "Nani..?"
  17. }
  18.  
  19. screendata = ""
  20.  
  21.  
  22. aiml.botData["flower"] = "blue orchid"
  23. aiml.botData["butterfly"] = "the pretty yellow ones"
  24. aiml.botData["block"] = "pink wool"
  25. aiml.botData["item"] = "pink dye"
  26. aiml.botData["color"] = "pink"
  27. aiml.botData["creator"] = "wiggle1000"
  28.  
  29.  
  30. function tablelength(T)
  31.   local count = 0
  32.   for _ in pairs(T) do count = count + 1 end
  33.   return count
  34. end
  35.  
  36. function string.split(rawString, separator)
  37.     local splittedString = {}
  38.     for str in string.gmatch(rawString, "[^"..separator.."]+") do
  39.         table.insert(splittedString, str)
  40.     end
  41.     return splittedString
  42. end
  43.  
  44. function string.title(rawString)
  45.     if type(string.split) == "function" then
  46.         allWords = string.split(rawString, " ")
  47.         for index, word in pairs(allWords) do
  48.             allWords[index] = string.gsub(string.lower(word), "%a", string.upper, 1)
  49.         end
  50.         return table.concat(allWords, " ")
  51.     else
  52.         error("string.split function is needed")
  53.     end
  54. end
  55.  
  56. function string.trim(rawString, pattern)
  57.     rawString = string.gsub(rawString, "%s+", " ")
  58.     if string.sub(rawString, 0, 1) == " " then
  59.         rawString = string.sub(rawString, 2)
  60.     end
  61.     if string.sub(rawString, -1) == " " then
  62.         rawString = string.sub(rawString, 0, -2)
  63.     end
  64.     return string.gsub(rawString, pattern, "")
  65. end
  66.  
  67. function trim1(s)
  68.   return (s:gsub("^%s*(.-)%s*$", "%1"))
  69. end
  70.  
  71. function aiml.load(file)
  72.     local topics = aiml.topics
  73.     --for nodeData, topicData in string.gmatch(file, "<topic (.-)>(.-)</topic>") do
  74.         topicName = "default"--string.match(nodeData, "name=[\"\'](.-)[\"\']")
  75.         for i,v in pairs(aiml.retrieveCategorys(file)) do
  76.             topics[topicName][i] = v--aiml.retrieveCategorys(topicData)
  77.         end
  78.     --end
  79.     aiml.topics = topics
  80. end
  81.  
  82. function aiml.retrieveCategorys(xmlData)
  83.     local categorys = {}
  84.     local hasPattern = false
  85.     local hasTemplate = false
  86.     --print("A")
  87.     for nodeData in string.gmatch(xmlData, "<category>(.-)</category>") do
  88.     ---print("B")
  89.         local input, output
  90.         if string.find(nodeData, "<pattern>(.-)</pattern>") ~= nil then
  91.             ---print("C")
  92.             hasPattern = true
  93.             input = string.trim(string.match(nodeData, "<pattern>(.-)</pattern>"), "[\n\t]")
  94.         end
  95.         if string.find(nodeData, "<template>(.-)</template>") ~= nil then
  96.             ---print("D")
  97.             hasTemplate = true
  98.             output = string.trim(string.match(nodeData, "<template>(.-)</template>"), "[\n\t]")
  99.         end
  100.         if hasTemplate and hasPattern then
  101.             ---print(input.."->"..output..".   OK")
  102.             categorys[input] = output
  103.         end
  104.     end
  105.     --print(tablelength(categorys))
  106.     return categorys
  107. end
  108.  
  109. function aiml.parseTemplate(player, pattern, input, template, dat)
  110.     local topic = aiml.players[player].topic or "default"
  111.     --print(template)
  112.     if string.match(template, "<user/>") then
  113.         local srai = string.match(template, "<user/>")
  114.         template = string.gsub(template, "<user/>", "<get name=\"name\"/>-<get name=\"relation\"/>")
  115.     end
  116.     if string.match(template, "<DAT/>") then
  117.         local srai = string.match(template, "<DAT/>")
  118.         template = string.gsub(template, "<DAT/>", dat)
  119.     end
  120.     if string.match(template, "<surprise/>") then
  121.         local srai = string.match(template, "<surprise/>")
  122.         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> ")
  123.     end
  124.     if string.match(template, "<affirmative/>") then
  125.         local srai = string.match(template, "<affirmative/>")
  126.         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>")
  127.     end
  128.     while string.match(template, "<random>(.-)</random>") do
  129.         local randomTemplate = string.match(template, "<random>(.-)</random>")
  130.             --print(randomTemplate)
  131.         local randomTable = {}
  132.         for str in string.gmatch(randomTemplate, "<li>(.-)</li>") do
  133.             --print(str)
  134.             table.insert(randomTable, str)
  135.         end
  136.         template = string.gsub(template, "<random>(.-)</random>", randomTable[math.random(#randomTable)], 1)
  137.     end
  138.     if string.match(template, "<srai>(.-)</srai>") then
  139.         local srai = string.match(template, "<srai>(.-)</srai>")
  140.         local raisedTemplate = aiml.getResponse(player, srai)
  141.         template = string.gsub(template, "<srai>"..srai.."</srai>", raisedTemplate)
  142.     end
  143.     if string.match(template, "<emote>(.-)</emote>") then
  144.         emote = string.match(template, "<emote>(.-)</emote>")
  145.         template = string.gsub(template, "<emote>(.-)</emote>", "")
  146.     end
  147.     if string.match(template, "<star index=[\'\"](.-)[\'\"] />") then
  148.         local starIndex = string.match(template, "<star index=[\'\"](.-)[\'\"] />")
  149.         local patternIndexes = string.split(input, " ")
  150.         if starIndex ~= "all" then
  151.             starIndex = tonumber(starIndex) or 0
  152.             starIndex = string.title(patternIndexes[starIndex] or "")
  153.         else
  154.             starIndex = string.title(input)
  155.         end
  156.         template = string.gsub(template, "<star index=[\'\"](.-)[\'\"] />", starIndex)
  157.     end
  158.    
  159.     if string.match(template, "<set (.-)=[\'\"](.-)[\'\"]>(.-)</set>") then
  160.         local setType, setData, setValue = string.match(template, "<set (.-)=[\'\"](.-)[\'\"]>(.-)</set>")
  161.         if setType == "topic" then
  162.             if aiml.topics[setData] then
  163.                 aiml.players[player].topic = setData
  164.             end
  165.         elseif setType == "data" then
  166.             aiml.players[player].data[setData] = setValue
  167.             template = string.gsub(template, "<set (.-)=[\'\"](.-)[\'\"]>(.-)</set>", aiml.players[player].data[setData])
  168.         elseif setType == "datacopy" then
  169.             aiml.players[player].data[setData] = dat
  170.             template = string.gsub(template, "<set (.-)=[\'\"](.-)[\'\"]>(.-)</set>", aiml.players[player].data[setData])
  171.         end
  172.         template = string.gsub(template, "<set (.-)=[\'\"](.-)[\'\"]>(.-)</set>", "")
  173.     end
  174.     while string.match(template, "<get name=\"(.-)\"/>") do
  175.         local getValue = string.match(template, "<get name=\"(.-)\"/>")
  176.         if aiml.players[player].data[getValue] then
  177.             template = string.gsub(template, "<get name=\"(.-)\"/>", aiml.players[player].data[getValue],1)
  178.         else
  179.             template = string.gsub(template, "<get name=\"(.-)\"/>", "",1)
  180.         end
  181.     end
  182.     while string.match(template, "<getbot name=\"(.-)\"/>") do
  183.         local getValue = string.match(template, "<getbot name=\"(.-)\"/>")
  184.         if aiml.botData[getValue] then
  185.             template = string.gsub(template, "<getbot name=\"(.-)\"/>", aiml.botData[getValue],1)
  186.         else
  187.             template = string.gsub(template, "<getbot name=\"(.-)\"/>", "",1)
  188.         end
  189.     end
  190.     if string.match(template, "<condition data=[\'\"](.-)[\'\"] value=[\'\"](.-)[\'\"]>(.-)</condition>") then
  191.         local conditionKey, conditionValue, conditionText = string.match(template, "<condition data=[\'\"](.-)[\'\"] value=[\'\"](.-)[\'\"]>(.-)</condition>")
  192.         if aiml.players[player].data[conditionKey] then
  193.             if aiml.players[player].data[conditionKey] == conditionValue then
  194.                 template = string.gsub(template, "<condition data=[\'\"](.-)[\'\"] value=[\'\"](.-)[\'\"]>(.-)</condition>", conditionText)
  195.             else
  196.                 template = string.gsub(template, "<condition data=[\'\"](.-)[\'\"] value=[\'\"](.-)[\'\"]>(.-)</condition>", "")
  197.             end
  198.         else
  199.             template = string.gsub(template, "<condition data=[\'\"](.-)[\'\"] value=[\'\"](.-)[\'\"]>(.-)</condition>", "")
  200.         end
  201.     end
  202.     if string.match(template, "<think>(.-)</think>") then
  203.         local srai = string.match(template, "<think>(.-)</think>")
  204.         template = string.gsub(template, "<think>(.-)</think>", "")
  205.     end
  206.     if string.match(template, "<screendata>(.-)</screendata>") then
  207.         local srai = string.match(template, "<screendata>(.-)</screendata>")
  208.         screendata = srai
  209.         template = string.gsub(template, "<screendata>(.-)</screendata>", "")
  210.     end
  211.     return template
  212. end
  213.  
  214. function aiml.getResponse(player, input)
  215.     local topic = aiml.players[player].topic or "default"
  216.     local originalInput = string.gsub(input, "[^a-zA-Z0-9 -]", "")
  217.     input = string.gsub(string.upper(input), "[^a-zA-Z0-9 -]", "")
  218.     local hasStar = false
  219.     local dat = nil
  220.     if(aiml.topics[topic]==nil)then
  221.         topic = "default"
  222.     end
  223.     for pattern, template in pairs(aiml.topics[topic]) do
  224.         local pattern = pattern
  225.         if string.find(pattern, "*") then
  226.             pattern = string.gsub(pattern, "*", "(.-)")
  227.         end
  228.         if string.find(pattern, "<DAT>") then
  229.             local before = string.match(pattern, "(.-)<DAT>")
  230.             local after = string.match(pattern, "<DAT>(.-)")
  231.             local s = input
  232.             if(before~=nil)then
  233.                 s = string.gsub(input, before, "")
  234.             end
  235.             if(after~=nil)then
  236.                 s = string.gsub(s, after, "")
  237.             end
  238.             dat = trim1(string.lower(s))
  239.             pattern = string.gsub(pattern, "<DAT>", "(.-)")
  240.         end
  241.         if pattern ~= "(.-)" then
  242.             if input == pattern or string.match(input, pattern) then
  243.                 return aiml.parseTemplate(player, pattern, input, template, dat), false
  244.             end
  245.         else
  246.             hasStar = {player, pattern, input, template}
  247.         end
  248.     end
  249.     if hasStar then
  250.         return aiml.parseTemplate(table.unpack(hasStar)), false
  251.     end
  252.     return string.format(defaultResponses[math.random(1,#defaultResponses)], input), true
  253. end
  254.  
  255. local savePlayerData = function(player)
  256.     local dfile = "data/"..player
  257.     if(fs.exists(dfile))then
  258.         fs.delete(dfile)
  259.     end
  260.     local datfile = fs.open(dfile, "w")
  261.     datfile.write(textutils.serialize(aiml.players[player].data))
  262.     datfile.close()
  263. end
  264. local loadPlayerData = function(player)
  265.     local dfile = "data/"..player
  266.     if(fs.exists(dfile))then
  267.         local datfile = fs.open(dfile, "r")
  268.         local dat = textutils.unserialize(datfile.readAll())
  269.        
  270.         datfile.close()
  271.         return dat
  272.     else
  273.         local dat = {}
  274.         dat["name"] = player
  275.         dat["relation"] = "oniichan"
  276.         dat["botmaster"] = "wiggle1000"
  277.         dat["myname"] = "Base-chan"
  278.         return dat
  279.     end
  280. end
  281.  
  282. function correctString(str)
  283.     local tempwords = string.split(str," ")
  284.     local outstring = ""
  285.     for i,g in pairs(tempwords) do
  286.         local v = string.upper(g)
  287.         --print("|"..v.."|")
  288.         if(v == "I'M" or v == "IM")then
  289.             v = "I AM"
  290.         elseif(v == "THAT'S" or v == "THATS")then
  291.             v = "THAT IS"
  292.         elseif(v == "IT'S")then
  293.             v = "IT IS"
  294.         end
  295.         outstring = outstring..v.." "
  296.     end
  297.     return outstring
  298. end
  299.  
  300. function getResponseToPlayer(player, command)
  301.     emote = nil
  302.     if not aiml.players[player] then
  303.         aiml.players[player] = {topic = "default", data = loadPlayerData(player)}
  304.     end
  305.     --print(correctString(command))
  306.     local response, isDefault = aiml.getResponse(player, correctString(command))
  307.     savePlayerData(player)
  308.     return response, emote, isDefault
  309. end
  310.  
  311. function loadFile(fname)
  312.     print("LOADING: "..fname)
  313.     local file = fs.open("knowledge/"..fname, "r")
  314.     --print(file.readAll())
  315.     aiml.load(file.readAll())
  316.     file.close()
  317. end
  318.  
  319. function loadKnowledge()
  320.     for i,v in pairs(fs.list("knowledge")) do
  321.         loadFile(v)
  322.     end
  323.     print("TOTAL SAMPLES: "..tablelength(aiml.topics.default))
  324. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement