Hey all. I’ve been banging my head against this one for a couple days now and am reaching out to the gang for some help.
There’s this cool thing called Markov generation, which can come up with some pretty cool randomly strung together text if you feed it a large enough “sample file”, it will spew out some pretty convincing sounding sentences.
I found a sample actually in LUA from the PIL official docs, but I can’t get it to work properly.
The original’s here: http://www.lua.org/pil/10.2.html
It’s pretty damned small and nice too.
So I converted it over to this ugly beast:
math.randomseed( os.time() )
ourTextTable = {}
function allwords ()
local path = system.pathForFile( "testtext.txt", system.Resources )
local fh = io.open( path )
for line in fh:lines() do
while line do -- repeat while there are lines
local s, e = string.find(line, "%w+", pos)
if s then -- found a word?
pos = e + 1 -- update next position
--print("Inserting "..string.sub(line, s, e))
table.insert(ourTextTable,string.sub(line, s, e))
else
line = io.read() -- word not found; try next line
pos = 1 -- restart from first position
end
end
end
fh:close()
end
function prefix (w1, w2)
return w1 .." ".. w2
end
allwords()
local statetab = {}
function insert (index, value)
if not statetab[index] then
statetab[index] = {n=0}
end
table.insert(statetab[index], value)
end
local N = 2
-- MAXGEN is the number of words you want to generate in your final sentence
local MAXGEN = 100
local NOWORD = "\n"
-- build table
-- build table
local w1, w2 = NOWORD, NOWORD
for i=1,#ourTextTable do
insert(prefix(w1, w2), ourTextTable[i])
w1 = w2; w2 = ourTextTable[i];
end
insert(prefix(w1, w2), NOWORD)
local myFinishedText = ""
-- generate text
w1 = NOWORD; w2 = NOWORD -- reinitialize
for i=1,MAXGEN do
local list = statetab[prefix(w1, w2)]
-- choose a random item from list
local r = math.random(table.getn(list))
local nextword = list[r]
myFinishedText = myFinishedText .." "..nextword
w1 = w2; w2 = nextword
end
print(myFinishedText)
You’ll need to plop in a text file named “testtext.txt”, and it’s supposed to go thru it and…well I explained above. I hate to pull a “solve my problem for me”, but what do I have to lose?
I started by yanking out the io.read stuff, converted it to a table, parsed the text, etc…and it’s KINDA working…but instead of doing it in chunks of every 1 word then finding the next logical word, it does it in a 5 word chunk, then another 5 word chunk, really wierd. I tried posting some Dark Tower text in there, and Roland kept doing the same 5 word chunks throughout the finished output.
I’d post some test text but I don’t want the post to get really huge.
I’d really, REALLY appreciate any help anyone can give me. I’m sure someone with the LUA skillz can bust it out, and I’m not averse to paypaling someone a six pack of their favorite beverages or a coffee! 
[import]uid: 11636 topic_id: 9609 reply_id: 309609[/import]