Advertisement
Guest User

NitrogenFingers Computer Craft grammar AI speatch using BDA

a guest
Aug 29th, 2012
589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.77 KB | None | 0 0
  1. --[[
  2.     BDA: Brain Damaged Algorithm
  3.     A natural language parsing and attempting to creating program
  4.    
  5.     Written by: BigSHinyToys
  6.     Modified by: NitrogenFingers 28/8/2012 (12:20am)
  7. --]]
  8.  
  9. local winX, winY = term.getSize()
  10. --Add your stop word lists here- they can be common subjects, participles, or any other grammatical
  11. --feature you'd like to capture. Denote it with a single, lowercase character- this indicates it is a
  12. --terminating symbol
  13. local stopWords = {
  14.     ["s"] = {"I", "you", "he", "she", "they", "we"};
  15.     ["v"] = {"is", "are", "am", "can be", "like", "hate", "often", "will", "must", "should", "might"};
  16.     ["a"] = {"and", "or", ", also", ", but", ", however", "like"};
  17. }
  18. --This list is of words that aren't stop words- they'll be added as you type them
  19. local vocab = {}
  20. local vcLength = 0
  21.  
  22. local savefile = "Grammerwords"
  23. local convofile = "Grammerconvo"
  24.  
  25. local function saveTalk(sentence)
  26.     local f = fs.open(convofile, "a")
  27.     f.write(sentence.."\n")
  28.     f.close()
  29. end
  30.  
  31. local function save()
  32.     local f = fs.open(savefile, "w")
  33.     f.write(textutils.serialize(vocab))
  34.     f.close()
  35. end
  36.  
  37. if fs.exists(savefile) then
  38.     local f = fs.open(savefile, "r")
  39.     local save = textutils.unserialize(f.readAll())
  40.     f.close()
  41.     vocab = save
  42. end
  43.  
  44. --Here you should have tables with capital letters, your non-terminating characters, that include
  45. --all possible rules for your grammar. These tokens will in turn be selected and randomly replaced
  46. --with one of the contents of the table.
  47. --Note: AVOID INFINITE LOOPS: make sure every possible grammar has a terminating-only character string.
  48. local grammar = {
  49.     ["S"] = {"svW"},
  50.     ["W"] = {"waW", "wasW", "w"}
  51. }
  52.  
  53. function readWords()
  54.     while true do
  55.         local arg1, arg2 = os.pullEvent("new_line")
  56.        
  57.         if arg1 == "new_line" then
  58.             local lastMatch = ""
  59.             for match in string.gmatch(arg2, "[^ \t]+") do
  60.                 local stop = false
  61.                 for _,list in pairs(stopWords) do
  62.                     for _,stopword in pairs(list) do
  63.                         --If you want to be supersmart you can try to detect stop words and add them
  64.                         --to your list here- try looking for "ly" for adverbs, "ing" for present tense verbs
  65.                         --etc.
  66.                         if match == stopword then
  67.                             stop = true
  68.                             break
  69.                         end
  70.                     end
  71.                 end
  72.                
  73.                 if not stop then
  74.                     --We handle linkages here
  75.                     if not vocab[match] then
  76.                         --I must admit I'm unsure how to get just the key in a table (there must be a way)
  77.                         --Anyway I just include the word itself as the first value in the value table. Cheap and dusty.
  78.                         vocab[match] = {match, ""}
  79.                         --And of course because it's a string indexed value now it's not included in the array, or the length!
  80.                         vcLength = vcLength+1
  81.                     end
  82.                    
  83.                     if lastMatch ~= "" then
  84.                         table.insert(vocab[lastMatch], match)
  85.                     end
  86.                    
  87.                     lastMatch = match
  88.                 else
  89.                     lastMatch = ""
  90.                 end
  91.                
  92.             end
  93.         end
  94.        
  95.         local newGrammar = "S"
  96.         local found = false
  97.         --This randomly creates your grammar
  98.         repeat
  99.             found = false
  100.             for k,v in pairs(grammar) do
  101.                 local f = string.find(newGrammar, k)
  102.                 if f then
  103.                     newGrammar = string.gsub(newGrammar, k, v[math.random(1, #v)], 1)
  104.                     found = true
  105.                 end
  106.             end
  107.         until not found
  108.        
  109.         --This then randomly replaces your grammar with actual words
  110.         local newSentence = ""
  111.         for i=1,#newGrammar do
  112.             local token = string.sub(newGrammar, i, i)
  113.             if token == "w" then
  114.                 --This is horribly messy- will fix when less tired!
  115.                 local chosenWord = math.random(1,vcLength)
  116.                 local i=1
  117.                 for k,_ in pairs(vocab) do
  118.                     if i==chosenWord then
  119.                         chosenWord = k
  120.                         break
  121.                     end
  122.                     i=i+1
  123.                 end
  124.                
  125.                 local chosenLink = vocab[chosenWord][math.random(2,#vocab[chosenWord])]
  126.                 if chosenLink ~= "" then chosenLink = chosenLink.." " end
  127.                
  128.                 --Again cheap and dusty here, the extra space will make sentences look a bit ugly. Requires a little tweaking, but I'm to tired to fix right now.
  129.                 newSentence=newSentence..vocab[chosenWord][1].." "..chosenLink
  130.             else
  131.                 local newstop = stopWords[token][math.random(1,#stopWords[token])]
  132.                 --Cheap and dusty (lots of it in this program!) to get rid of extra space
  133.                 if string.sub(newstop, 1, 1)=="," then newSentence = string.sub(newSentence, 1, #newSentence-1) end
  134.                 newSentence=newSentence..newstop.." "
  135.             end
  136.         end
  137.         term.scroll(1)
  138.         term.setCursorPos(1,winY - 1)
  139.         term.clearLine()
  140.         save()
  141.         local talk = "AI> "..string.sub(newSentence, 1, #newSentence-1).."."
  142.         saveTalk(talk)
  143.         print(talk)
  144.     end
  145. end
  146.  
  147. function getInput()
  148.     while true do
  149.         term.setCursorPos(1,winY)
  150.         term.clearLine()
  151.         local input = read()
  152.         saveTalk(input)
  153.         os.queueEvent("new_line",input)
  154.     end
  155. end
  156.  
  157. term.clear()
  158. parallel.waitForAny(getInput, readWords)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement