local data = [[It first checks whether that prefix already has a list; if not, it creates a new one with the new value. Otherwise, it uses the predefined function table.insert to insert the new value at the end of the existing list. To build the statetab table, we keep two variables, w1 and w2, with the last two words read. For each prefix, we keep a list of all words that follow it. After building the table, the program starts to generate a text with MAXGEN words. First, it re-initializes variables w1 and w2. Then, for each prefix, it chooses randomly a next word from the list of valid next words, prints that word, and updates w1 and w2. Next we show the complete program. ]] local word_table = {} function insert (i, value) --If a entry does not exist, create it if not word_table[i] then word_table[i] = {} end --Insert the value into the table table.insert(word_table[i], value) end function find_next_word(data, pos) local s, e = string.find(data, "%w+", pos) if e == nil then return nil end local str = string.sub(data, s, e) pos = e + 1 return str, pos end function makePair(w1, w2) return w1 .. " " .. w2 end function push_to_table(w1, w2, str) -- Inserts pair into table and swaps -- w2 -> w1 and str -> w2 -- not necessary just clean insert(makePair(w1,w2), str) io.write("Added entry at " .. makePair(w1,w2) .. ": " .. str .. "\n"); return w2, str end function markov_2 (data) local w1 = "\n" local w2 = "\n" local pos = 0 local str = "" -- Create Initial Table From Block io.write("\n-------------\n Entry Log\n-------------\n\n") str, pos = find_next_word(data, pos); w1, w2 = push_to_table(w1, w2, str) while pos ~= nil do str, pos = find_next_word(data, pos) if not pos then break end w1, w2 = push_to_table(w1, w2, str) end --List Pairs with multiple choices io.write("\n--------------------\n Multiple choices\n--------------------\n\n") for k, v in pairs(word_table) do if table.getn(v) > 1 then io.write(k .. "\n") end end end markov_2(data);

2 Responses

Its a WIP
Now it works

Write a comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.