Advertisement
Guest User

BDA brain damage algrithum by BigSHiny Toys

a guest
Sep 3rd, 2012
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.25 KB | None | 0 0
  1. --[[
  2.     Brain Damage Algorithm BDA for short
  3.  
  4.     Emulates drunk or brain damaged persons speech patterns
  5.     Devloped By BigSHinyToys
  6.     http://www.computercraft.info/forums2/index.php?/topic/3546-fun-ai-learning-program-that-can-talk/
  7.     BUILD 4 with aoto learn
  8. ]]--
  9.  
  10. local winX,winY = term.getSize()
  11. local length = 8
  12. local tDictionary = {}
  13. local shado = {}
  14. local tArgs = {...}
  15.  
  16. local savefile = "words"
  17. local convofile = "convo"
  18.  
  19. local function saveTalk(sentence)
  20.     local f = fs.open(convofile, "a")
  21.     f.write(sentence.."\n")
  22.     f.close()
  23. end
  24.  
  25. local function save()
  26.     local f = fs.open(savefile, "w")
  27.     f.write(textutils.serialize({tDictionary, shado}))
  28.     f.close()
  29. end
  30.  
  31. if fs.exists(savefile) then
  32.     local f = fs.open(savefile, "r")
  33.     local save = textutils.unserialize(f.readAll())
  34.     f.close()
  35.     tDictionary = save[1]
  36.     shado = save[2]
  37. end
  38.  
  39. local function addThis(sLine)
  40.     local posX,posY = term.getCursorPos()
  41.     local scrX,scrY = term.getSize()
  42.     posX = 1
  43.     local tWords = {}
  44.     for match in string.gmatch(sLine, "[^ \t]+") do
  45.         table.insert( tWords, match )
  46.     end
  47.     for i = 1,#tWords do
  48.         --sleep(0.01)
  49.         if tDictionary[tWords[i]] then
  50.             table.insert(tDictionary[tWords[i]]["to"],tWords[i])
  51.         else
  52.             table.insert(shado,tWords[i])
  53.             tDictionary[tWords[i]] = {}
  54.             tDictionary[tWords[i]]["name"] = tWords[i]
  55.             tDictionary[tWords[i]]["to"] = {}
  56.         end
  57.         term.setCursorPos(posX,posY)
  58.         write(string.rep("=",math.floor((i/#tWords)*(scrX-1))))
  59.         term.setCursorPos(scrX/2,posY)
  60.         write(tostring(math.floor((i/#tWords)*100)).." %")
  61.     end
  62. end
  63.  
  64.  
  65. if tArgs[1] ~= nil and fs.exists(tArgs[1]) and fs.isDir(tArgs[1]) then
  66.     print("Folder Found")
  67.     local tFiles = fs.list(tArgs[1])
  68.     print(tostring(#tFiles).." "..type(tFiles))
  69.     local posX,posY = term.getCursorPos()
  70.     local scrX,scrY = term.getSize()
  71.     posX = 1
  72.     for i = 1,#tFiles do
  73.         term.setCursorPos(posX,posY)
  74.         write(string.rep("=",math.floor((i/#tFiles)*(scrX-1))))
  75.         term.setCursorPos(scrX/2,posY)
  76.         write(tostring(math.floor((i/#tFiles)*100)).." %")
  77.         print(" ")
  78.         sleep(0.01)
  79.         if not fs.isDir(tArgs[1]..[[\]]..tFiles[i]) then
  80.             local file = fs.open(tArgs[1]..[[\]]..tFiles[i],"r")
  81.             if file then
  82.                 local data = file:readAll()
  83.                 file:close()
  84.                 addThis(data)
  85.             else
  86.                 print("error")
  87.             end
  88.         end
  89.     end
  90.     save()
  91. elseif tArgs[1] ~= nil then
  92.     print(tostring(tArgs[1]).." is not a Folder")
  93.     os.pullEvent("key")
  94. end
  95.  
  96.  
  97.  
  98. local function dataAI()
  99.     local lastTimer = os.startTimer(math.random(1,120))
  100.     while true do
  101.         local event,arg1,arg2,arg3 = os.pullEvent()
  102.         if event == "new_line" or event == "timer" then
  103.             if event == "timer" then
  104.                 if arg1 == lastTimer then
  105.                     lastTimer = os.startTimer(math.random(1,120))
  106.                 end
  107.                 arg1 = ""
  108.             end
  109.             local sLine = arg1
  110.             local tWords = {}
  111.             for match in string.gmatch(sLine, "[^ \t]+") do
  112.                 table.insert( tWords, match )
  113.             end
  114.             for i = 1,#tWords do
  115.                 if tDictionary[tWords[i]] then
  116.                     table.insert(tDictionary[tWords[i]]["to"],tWords[i])
  117.                 else
  118.                     table.insert(shado,tWords[i])
  119.                     tDictionary[tWords[i]] = {}
  120.                     tDictionary[tWords[i]]["name"] = tWords[i]
  121.                     tDictionary[tWords[i]]["to"] = {}
  122.                 end
  123.             end
  124.             local test = ""
  125.             local last = nil
  126.             for i = 1,math.random(1,length) do
  127.                 if last then
  128.                     if #tDictionary[last]["to"] and #tDictionary[last]["to"] > 0 then
  129.                         sNext = tDictionary[last]["to"][math.random(1,#tDictionary[last]["to"])]
  130.                         if sNext ~= last then
  131.                             test = test..sNext.." "
  132.                             last = sNext
  133.                         else
  134.                             last = tDictionary[shado[math.random(1,#shado)]]["name"]
  135.                             test = test..last.." "
  136.                         end
  137.                     else
  138.                         last = tDictionary[shado[math.random(1,#shado)]]["name"]
  139.                         test = test..last.." "
  140.                     end
  141.                 else
  142.                     last = tDictionary[shado[math.random(1,#shado)]]["name"]
  143.                     test = test..last.." "
  144.                 end
  145.             end
  146.             term.scroll(1)
  147.             term.setCursorPos(1,winY - 1)
  148.             term.clearLine()
  149.             if arg1 ~= "" then
  150.                 save()
  151.             end
  152.             saveTalk("AI> "..test)
  153.             print("AI> "..test)
  154.         end
  155.     end
  156. end
  157. local function userInput()
  158.     while true do
  159.         term.setCursorPos(1,winY)
  160.         term.clearLine()
  161.         local input = read()
  162.         saveTalk(input)
  163.         os.queueEvent("new_line",input)
  164.     end
  165. end
  166. term.clear()
  167. term.setCursorPos(1,1)
  168. parallel.waitForAny(userInput,dataAI)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement