Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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"}}}
- local lang = ""
- local yieldTime
- local ttdd = false
- function spairs(t, order)
- -- collect the keys
- local keys = {}
- for k in pairs(t) do keys[#keys+1] = k end
- -- if order function given, sort by it by passing the table and keys a, b,
- -- otherwise just sort the keys
- if order then
- table.sort(keys, function(a,b) return order(t, a, b) end)
- else
- table.sort(keys)
- end
- -- return the iterator function
- local i = 0
- return function()
- i = i + 1
- if keys[i] then
- return keys[i], t[keys[i]]
- end
- end
- end
- local function yield()
- if yieldTime then
- if os.clock() - yieldTime > 7 then
- os.queueEvent("yielding")
- os.pullEvent("yielding")
- yieldTime = nil
- end
- else
- yieldTime = os.clock() -- store the time
- end
- end
- function split(str)
- local div = " "
- local pos,arr = 0,{}
- -- for each divider found
- for st,sp in function() return string.find(str,div,pos,true) end do
- table.insert(arr,string.sub(str,pos,st-1)) -- Attach chars left of current divider
- pos = sp + 1 -- Jump past current divider
- end
- table.insert(arr,string.sub(str,pos)) -- Attach chars right of last divider
- return arr
- end
- function getType(str, mode)
- mode = mode or false
- for k, v in pairs(words[lang]) do
- for k2, v2 in pairs(v) do
- if v2==str then
- if mode then
- if k=="others" then return nil end
- end
- return k
- end
- end
- end
- end
- function getType2(str, mode)
- mode = mode or false
- local temp2 = getType(str, mode)
- if not temp2 then return str end
- return temp
- end
- function exists(str, mode)
- mode = mode or false
- if not getType(str) then
- return false
- else
- if mode then if getType(str)=="others" then return false end end
- return true
- end
- end
- local askb = false
- function adivine(str)
- local is = ""
- local word = ""
- local temp = split(str)
- local returnt = {}
- local count = 0
- for k, v in pairs(temp) do
- local raf = getType2(v, true)
- if raf~=v then
- returnt[v] = getType(v)
- else
- if getType2(v)=="other" then
- returnt[v] = getType(v)
- else
- if count==1 then
- askb = true
- end
- word = v
- count = 1
- end
- end
- end
- if askb then return end
- local t = {}
- for k, v in pairs(words[lang]["grammar"]) do
- t[v] = {["count"] = 0, ["canBe"] = "not", ["number"] = 0}
- end
- for k, v in pairs(words[lang]["grammar"]) do
- if t[v].count~=1 then t[v].number = t[v].number + 1 end
- if v==word then
- t[v].count = t[v].count+1
- elseif v==returnt[v] then
- if canBe=="not" or canBe == true then
- t[v].canBe = true
- end
- else
- t[v].canBe = false
- end
- end
- tbl = {["adjetives"] = 0, ["nouns"] = 0, ["verbs"] = 0, ["persons"] = 0}
- for k, v in pairs(t) do
- if v.CanBe and count==1 then
- local temp = split(k)
- for i=1, #temp do
- if i==v.number then
- tbl[temp[i]] = tbl[temp[i]] + 1
- end
- end
- end
- end
- local t2 = {}
- for k,v in spairs(tbl, function(t,a,b) return t[b] < t[a] end) do
- table.insert(t2, k)
- end
- if not t2[1] then
- t2[1] = ask(word)
- t2[1] = t2[1].."s"
- end
- returnt[word] = t2[1]
- add(word, t2[1])
- return returnt
- end
- function add(word, wordtype)
- if wordtype=="adjetive" then
- table.insert(words[lang].adjetives, word)
- elseif wordtype=="person" then
- table.insert(words[lang].persons, word)
- elseif wordtype=="verb" then
- table.insert(words[lang].verbs, word)
- elseif wordtype=="noun" then
- table.insert(words[lang].nouns, word)
- elseif wordtype=="other" then
- table.insert(words[lang].others, word)
- end
- end
- function ask(word)
- print("What is "..word.."?")
- print("1- Adjetive")
- print("2- Person")
- print("3- Verb")
- print("4- Noun")
- print("5- Other")
- local a = tonumber(read())
- if a==1 then return word, "adjetive" end
- if a==2 then return word, "person" end
- if a==3 then return word, "verb" end
- if a==4 then return word, "noun" end
- if a==5 then return word, "other" end
- end
- local asked = false
- local askForEver = false
- function parseWord()
- local t = {}
- if not asked then
- print("Do you want to try to adivine the type? Y/N")
- local a = string.lower(read())
- if a=="y" then ttdd = true end
- if a=="n" then
- ask = true
- askForEver = true
- end
- asked = true
- end
- str = read()
- if ttdd then
- t = adivine(str)
- end
- if askb then
- local temp = split(str)
- for k, v in pairs(temp) do
- local raf = getType2(v, true)
- if raf~=v then
- t[v] = getType(v)
- else
- if getType2(v)=="other" then
- t[v] = getType(v)
- else
- v, typ = ask(v)
- add(v, typ)
- t[v] = getType(v)
- end
- end
- end
- if not askForEver then
- askb = false
- end
- end
- --add the thing to generate
- end
- function generateNoun()
- local random = math.random(1, #words[lang].nouns)
- return words[lang].nouns[random]
- end
- function generateAdjetive()
- local random = math.random(1, #words[lang].adjetives)
- return words[lang].adjetives[random]
- end
- function generatePerson()
- local random = math.random(1, #words[lang].persons)
- return words[lang].persons[random]
- end
- function generateVerb()
- local random = math.random(1, #words[lang].verbs)
- return words[lang].verbs[random]
- end
- function generateGrammar()
- local random = math.random(1, #words[lang]["grammar"])
- return words[lang]["grammar"][random]
- end
- function generate(use)
- local grammar = generateGrammar()
- local temp = split(grammar)
- local t = {}
- t.adjetives = 0
- t.nouns = 0
- t.verbs = 0
- t.persons = 0
- for i=1, #temp do
- local a = temp[i]
- if a:find("adjetives") then t.adjetives = t.adjetives + 1 end
- if a:find("nouns") then t.nouns = t.nouns + 1 end
- if a:find("verbs") then t.verbs = t.verbs + 1 end
- if a:find("persons") then t.persons = t.persons + 1 end
- end
- while t.adjetives ~= 0 do
- grammar = string.gsub(grammar, "adjetives", generateAdjetive(), 1)
- t.adjetives = t.adjetives - 1
- end
- while t.nouns ~= 0 do
- grammar = string.gsub(grammar, "nouns", generateNoun(), 1)
- t.nouns = t.nouns - 1
- end
- while t.verbs ~= 0 do
- grammar = string.gsub(grammar, "verbs", generateVerb(), 1)
- t.verbs = t.verbs - 1
- end
- while t.persons ~= 0 do
- grammar = string.gsub(grammar, "persons", generatePerson(), 1)
- t.persons = t.persons - 1
- end
- return grammar
- end
- function save()
- file = fs.open(".usave", "w")
- file.writeLine(textutils.serialize(words))
- file.close()
- end
- function load()
- if fs.exists(".usave") then
- file = fs.open(".usave", "r")
- words = textutils.unserialize(file.readLine())
- file.close()
- end
- end
- function menu()
- load()
- local t = {}
- for k, v in pairs(words) do
- table.insert(t, k)
- print(#t, "- ", k)
- end
- local a = tonumber(read())
- lang = t[a]
- t = nil
- term.clear()
- term.setCursorPos(1,1)
- print("1- Generate")
- print("2- Parse")
- print("3- Chat")
- local a = tonumber(read())
- if a==1 then
- while true do
- print(generate())
- save()
- yield()
- os.pullEvent("key")
- end
- elseif a==2 then
- while true do
- parseWord()
- save()
- yield()
- os.pullEvent("key")
- end
- elseif a==3 then
- while true do
- generate(parseWord())
- save()
- yield()
- os.pullEvent("key")
- end
- end
- end
- menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement