Advertisement
Guest User

Program

a guest
Aug 9th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. words = {["English"] = {["verbs"] = {"created", "wake"}, ["persons"] = {"he", "she", "it", "they", "us", "I"}, ["adjetives"] = {"nice", "ugly", "big", "small"}, ["others"] = {}, ["nouns"] = {"Minecraft", "potato"}, ["grammar"] = {"persons is adjetives", "persons verbs nouns", "persons verbs the nouns", "Is persons adjetives?", "The adjetives persons verbs the adjetives nouns"}}}
  2. local lang = ""
  3. local yieldTime
  4. local ttdd = false
  5. function spairs(t, order)
  6.     -- collect the keys
  7.     local keys = {}
  8.     for k in pairs(t) do keys[#keys+1] = k end
  9.  
  10.     -- if order function given, sort by it by passing the table and keys a, b,
  11.     -- otherwise just sort the keys
  12.     if order then
  13.         table.sort(keys, function(a,b) return order(t, a, b) end)
  14.     else
  15.         table.sort(keys)
  16.     end
  17.  
  18.     -- return the iterator function
  19.     local i = 0
  20.     return function()
  21.         i = i + 1
  22.         if keys[i] then
  23.             return keys[i], t[keys[i]]
  24.         end
  25.     end
  26. end
  27. local function yield()
  28.     if yieldTime then
  29.         if os.clock() - yieldTime > 7 then
  30.             os.queueEvent("yielding")
  31.             os.pullEvent("yielding")
  32.             yieldTime = nil
  33.         end
  34.     else
  35.         yieldTime = os.clock() -- store the time
  36.     end
  37. end
  38. function split(str)
  39.      local div = " "
  40.     local pos,arr = 0,{}
  41.     -- for each divider found
  42.     for st,sp in function() return string.find(str,div,pos,true) end do
  43.        table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider
  44.         pos = sp + 1 -- Jump past current divider
  45.     end
  46.     table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider
  47.     return arr
  48. end
  49. function getType(str, mode)
  50.     mode = mode or false
  51.     for k, v in pairs(words[lang]) do
  52.         for k2, v2 in pairs(v) do
  53.             if v2==str then
  54.                 if mode then
  55.                     if k=="others" then return nil end
  56.                 end
  57.                 return k
  58.             end
  59.         end
  60.     end
  61. end
  62. function getType2(str, mode)
  63.     mode = mode or false
  64.     local temp2 = getType(str, mode)
  65.     if not temp2 then return str end
  66.     return temp
  67. end
  68. function exists(str, mode)
  69.     mode = mode or false
  70.     if not getType(str) then
  71.         return false
  72.     else
  73.         if mode then if getType(str)=="others" then return false end end
  74.         return true
  75.     end
  76. end
  77. local askb = false
  78. function adivine(str)
  79.     local is = ""
  80.     local word = ""
  81.     local temp = split(str)
  82.     local returnt = {}
  83.     local count = 0
  84.     for k, v in pairs(temp) do
  85.         local raf = getType2(v, true)
  86.         if raf~=v then
  87.             returnt[v] = getType(v)
  88.         else
  89.             if getType2(v)=="other" then
  90.                 returnt[v] = getType(v)
  91.             else
  92.                 if count==1 then
  93.                     askb = true
  94.                 end
  95.                 word = v
  96.                 count = 1
  97.             end
  98.         end
  99.     end
  100.     if askb then return end
  101.     local t = {}
  102.     for k, v in pairs(words[lang]["grammar"]) do
  103.         t[v] = {["count"] = 0, ["canBe"] = "not", ["number"] = 0}
  104.     end
  105.     for k, v in pairs(words[lang]["grammar"]) do
  106.         if t[v].count~=1 then t[v].number = t[v].number + 1 end
  107.         if v==word then
  108.             t[v].count = t[v].count+1
  109.         elseif v==returnt[v] then
  110.             if canBe=="not" or canBe == true then
  111.                 t[v].canBe = true
  112.             end
  113.         else
  114.             t[v].canBe = false
  115.         end
  116.     end
  117.     tbl = {["adjetives"] = 0, ["nouns"] = 0, ["verbs"] = 0, ["persons"] = 0}
  118.     for k, v in pairs(t) do
  119.         if v.CanBe and count==1 then
  120.             local temp = split(k)
  121.             for i=1, #temp do
  122.                 if i==v.number then
  123.                     tbl[temp[i]] = tbl[temp[i]] + 1
  124.                 end
  125.             end
  126.         end
  127.     end
  128.     local t2 = {}
  129.     for k,v in spairs(tbl, function(t,a,b) return t[b] < t[a] end) do
  130.         table.insert(t2, k)
  131.     end
  132.     if not t2[1] then
  133.         t2[1] = ask(word)
  134.         t2[1] = t2[1].."s"
  135.     end
  136.     returnt[word] = t2[1]
  137.     add(word, t2[1])
  138.     return returnt
  139. end
  140. function add(word, wordtype)
  141.     if wordtype=="adjetive" then
  142.         table.insert(words[lang].adjetives, word)
  143.     elseif wordtype=="person" then
  144.         table.insert(words[lang].persons, word)
  145.     elseif wordtype=="verb" then
  146.         table.insert(words[lang].verbs, word)
  147.     elseif wordtype=="noun" then
  148.         table.insert(words[lang].nouns, word)
  149.     elseif wordtype=="other" then
  150.         table.insert(words[lang].others, word)
  151.     end
  152. end
  153. function ask(word)
  154.     print("What is "..word.."?")
  155.     print("1- Adjetive")
  156.     print("2- Person")
  157.     print("3- Verb")
  158.     print("4- Noun")
  159.     print("5- Other")
  160.     local a = tonumber(read())
  161.     if a==1 then return word, "adjetive" end
  162.     if a==2 then return word, "person" end
  163.     if a==3 then return word, "verb" end
  164.     if a==4 then return word, "noun" end
  165.     if a==5 then return word, "other" end
  166. end
  167. local asked = false
  168. local askForEver = false
  169. function parseWord()
  170.     local t = {}
  171.     if not asked then
  172.         print("Do you want to try to adivine the type? Y/N")
  173.         local a = string.lower(read())
  174.         if a=="y" then ttdd = true end
  175.         if a=="n" then
  176.             ask = true
  177.             askForEver = true
  178.         end
  179.         asked = true
  180.     end
  181.     str = read()   
  182.     if ttdd then
  183.         t = adivine(str)
  184.     end
  185.     if askb then
  186.         local temp = split(str)
  187.         for k, v in pairs(temp) do
  188.             local raf = getType2(v, true)
  189.             if raf~=v then
  190.                 t[v] = getType(v)
  191.             else
  192.                 if getType2(v)=="other" then
  193.                     t[v] = getType(v)
  194.                 else
  195.                     v, typ = ask(v)
  196.                     add(v, typ)
  197.                     t[v] = getType(v)
  198.                 end
  199.             end
  200.         end
  201.         if not askForEver then
  202.             askb = false
  203.         end
  204.     end
  205.     --add the thing to generate
  206. end
  207. function generateNoun()
  208.     local random = math.random(1, #words[lang].nouns)
  209.     return words[lang].nouns[random]
  210. end
  211. function generateAdjetive()
  212.     local random = math.random(1, #words[lang].adjetives)
  213.     return words[lang].adjetives[random]
  214. end
  215. function generatePerson()
  216.     local random = math.random(1, #words[lang].persons)
  217.     return words[lang].persons[random]
  218. end
  219. function generateVerb()
  220.     local random = math.random(1, #words[lang].verbs)
  221.     return words[lang].verbs[random]
  222. end
  223. function generateGrammar()
  224.     local random = math.random(1, #words[lang]["grammar"])
  225.     return words[lang]["grammar"][random]
  226. end
  227. function generate(use)
  228.     local grammar = generateGrammar()
  229.     local temp = split(grammar)
  230.     local t = {}
  231.     t.adjetives = 0
  232.     t.nouns = 0
  233.     t.verbs = 0
  234.     t.persons = 0
  235.     for i=1, #temp do
  236.         local a = temp[i]
  237.         if a:find("adjetives") then t.adjetives = t.adjetives + 1 end
  238.         if a:find("nouns") then t.nouns = t.nouns + 1 end
  239.         if a:find("verbs") then t.verbs = t.verbs + 1 end
  240.         if a:find("persons") then t.persons = t.persons + 1 end
  241.     end
  242.     while t.adjetives ~= 0 do
  243.         grammar = string.gsub(grammar, "adjetives", generateAdjetive(), 1)
  244.         t.adjetives = t.adjetives - 1
  245.     end
  246.     while t.nouns ~= 0 do
  247.         grammar = string.gsub(grammar, "nouns", generateNoun(), 1)
  248.         t.nouns = t.nouns - 1
  249.     end
  250.     while t.verbs ~= 0 do
  251.         grammar = string.gsub(grammar, "verbs", generateVerb(), 1)
  252.         t.verbs = t.verbs - 1
  253.     end
  254.     while t.persons ~= 0 do
  255.         grammar = string.gsub(grammar, "persons", generatePerson(), 1)
  256.         t.persons = t.persons - 1
  257.     end
  258.     return grammar
  259. end
  260. function save()
  261.     file = fs.open(".usave", "w")
  262.     file.writeLine(textutils.serialize(words))
  263.     file.close()
  264. end
  265. function load()
  266.     if fs.exists(".usave") then
  267.         file = fs.open(".usave", "r")
  268.         words = textutils.unserialize(file.readLine())
  269.         file.close()
  270.     end
  271. end
  272. function menu()
  273.     load()
  274.     local t = {}
  275.     for k, v in pairs(words) do
  276.         table.insert(t, k)
  277.         print(#t, "- ", k)
  278.     end
  279.     local a = tonumber(read())
  280.     lang = t[a]
  281.     t = nil
  282.     term.clear()
  283.     term.setCursorPos(1,1)
  284.     print("1- Generate")
  285.     print("2- Parse")
  286.     print("3- Chat")
  287.     local a = tonumber(read())
  288.     if a==1 then
  289.         while true do
  290.             print(generate())
  291.             save()
  292.             yield()
  293.             os.pullEvent("key")
  294.         end
  295.     elseif a==2 then
  296.         while true do
  297.             parseWord()
  298.             save()
  299.             yield()
  300.             os.pullEvent("key")
  301.         end
  302.     elseif a==3 then
  303.         while true do
  304.             generate(parseWord())
  305.             save()
  306.             yield()
  307.             os.pullEvent("key")
  308.         end
  309.     end
  310. end
  311. menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement