Advertisement
Guest User

Computer Craft BDA AI learnimg speatch by BigSHiny Toys

a guest
Aug 29th, 2012
1,585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.61 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.    
  8. ]]--
  9.  
  10. local winX,winY = term.getSize()
  11. local length = 8
  12. local tDictionary = {}
  13. local shado = {}
  14.  
  15. local savefile = "words"
  16. local convofile = "convo"
  17.  
  18. local function saveTalk(sentence)
  19.     local f = fs.open(convofile, "a")
  20.     f.write(sentence.."\n")
  21.     f.close()
  22. end
  23.  
  24. local function save()
  25.     local f = fs.open(savefile, "w")
  26.     f.write(textutils.serialize({tDictionary, shado}))
  27.     f.close()
  28. end
  29.  
  30. if fs.exists(savefile) then
  31.     local f = fs.open(savefile, "r")
  32.     local save = textutils.unserialize(f.readAll())
  33.     f.close()
  34.     tDictionary = save[1]
  35.     shado = save[2]
  36. end
  37.  
  38. local function dataAI()
  39.     local lastTimer = os.startTimer(math.random(1,120))
  40.     while true do
  41.         local event,arg1,arg2,arg3 = os.pullEvent()
  42.         if event == "new_line" or event == "timer" then
  43.             if event == "timer" then
  44.                 if arg1 == lastTimer then
  45.                     lastTimer = os.startTimer(math.random(1,120))
  46.                 end
  47.                 arg1 = ""
  48.             end
  49.             local sLine = arg1
  50.             local tWords = {}
  51.             for match in string.gmatch(sLine, "[^ \t]+") do
  52.                 table.insert( tWords, match )
  53.             end
  54.             for i = 1,#tWords do
  55.                 if tDictionary[tWords[i]] then
  56.                     table.insert(tDictionary[tWords[i]]["to"],tWords[i])
  57.                 else
  58.                     table.insert(shado,tWords[i])
  59.                     tDictionary[tWords[i]] = {}
  60.                     tDictionary[tWords[i]]["name"] = tWords[i]
  61.                     tDictionary[tWords[i]]["to"] = {}
  62.                 end
  63.             end
  64.             local test = ""
  65.             local last = nil
  66.             for i = 1,math.random(1,length) do
  67.                 if last then
  68.                     if #tDictionary[last]["to"] and #tDictionary[last]["to"] > 0 then
  69.                         sNext = tDictionary[last]["to"][math.random(1,#tDictionary[last]["to"])]
  70.                         if sNext ~= last then
  71.                             test = test..sNext.." "
  72.                             last = sNext
  73.                         else
  74.                             last = tDictionary[shado[math.random(1,#shado)]]["name"]
  75.                             test = test..last.." "
  76.                         end
  77.                     else
  78.                         last = tDictionary[shado[math.random(1,#shado)]]["name"]
  79.                         test = test..last.." "
  80.                     end
  81.                 else
  82.                     last = tDictionary[shado[math.random(1,#shado)]]["name"]
  83.                     test = test..last.." "
  84.                 end
  85.             end
  86.             term.scroll(1)
  87.             term.setCursorPos(1,winY - 1)
  88.             term.clearLine()
  89.             save()
  90.             saveTalk("AI> "..test)
  91.             print("AI> "..test)
  92.         end
  93.     end
  94. end
  95. local function userInput()
  96.     while true do
  97.         term.setCursorPos(1,winY)
  98.         term.clearLine()
  99.         local input = read()
  100.         saveTalk(input)
  101.         os.queueEvent("new_line",input)
  102.     end
  103. end
  104. term.clear()
  105. parallel.waitForAny(userInput,dataAI)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement