Skip to content

Instantly share code, notes, and snippets.

@inmatarian
Last active May 12, 2017 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inmatarian/c604060ac4caed76edac36d72249d3d2 to your computer and use it in GitHub Desktop.
Save inmatarian/c604060ac4caed76edac36d72249d3d2 to your computer and use it in GitHub Desktop.
random text replacement generator
lua gen.lua --times=25 sample.txt
Your pack contains another rucksack containing a shard of bone, some lint and an old bronze ring.
Your bag contains a tin of tobacco, an old bronze ring and a tin of tobacco.
Your pack contains a tin of tobacco, some lint and another bag containing another pack containing a handkerchief.
Your knapsack contains another bag containing an old bronze ring, a tin of tobacco and a tin of tobacco.
Your pack contains an old silver ring, a tin of tobacco and a shard of bone.
Your purse contains another backpack containing a tin of tobacco, some lint and some lint.
Your bag contains some coins, another rucksack containing some coins and some coins.
Your bag contains a tin of tobacco, an old silver ring and an old silver ring.
Your bag contains another knapsack containing a handkerchief, another bag containing another pack containing some coins and a tin of tobacco.
Your bag contains an old bronze ring, a tin of tobacco and a tin of tobacco.
Your purse contains some coins, an old silver ring and a tin of tobacco.
Your purse contains a tin of tobacco, a tin of tobacco and a shard of bone.
Your pack contains some coins, some lint and an old silver ring.
Your pack contains another pack containing a tin of tobacco, a shard of bone and some coins.
Your purse contains a handkerchief, some coins and a shard of bone.
Your pack contains an old bronze ring, a tin of tobacco and some lint.
Your pack contains some coins, some lint and some coins.
Your bag contains a handkerchief, some lint and an old bronze ring.
Your purse contains a shard of bone, a handkerchief and an old silver ring.
Your bag contains another rucksack containing a tin of tobacco, a handkerchief and some coins.
Your knapsack contains a shard of bone, a handkerchief and some coins.
Your bag contains a tin of tobacco, an old bronze ring and another bag containing an old silver ring.
Your knapsack contains another purse containing another purse containing another knapsack containing some lint, a tin of tobacco and another pack containing an old bronze ring.
Your bag contains a tin of tobacco, an old bronze ring and another pack containing a shard of bone.
Your bag contains another backpack containing a tin of tobacco, a handkerchief and some coins.
math.randomseed(os.time())
math.random()
math.random()
math.random()
function createState()
local state = {
outputs = {},
sets = {},
mode = 'outputs',
}
return state
end
function preprocess(line, state)
line = line:match("^%s*(.*)%s*$")
local includeFile = string.match(line, "^#include \"([^\"]+)\"")
if includeFile then
parseFile(includeFile, state)
return ""
end
return line
end
function parseLine(line, state)
if line ~= nil and #line > 0 then
if line:sub(1,1)=='[' then
assert(line:sub(-1)==']', 'set declaration must end with right bracket')
state.mode = 'sets'
state.currSet = line:sub(2, -2)
state.sets[state.currSet] = state.sets[state.currSet] or {}
assert(#state.currSet>1, 'set declaration is empty')
else
if state.mode == 'outputs' then
table.insert(state.outputs, line)
else
table.insert(state.sets[state.currSet], line)
end
end
end
return state
end
function parseFile(filename, state)
for line in io.lines(filename) do
line = preprocess(line, state)
parseLine(line, state)
end
return state
end
function split(s, sep)
local o = {}
for k in string.gmatch(s, "[^|]+") do
o[#o+1] = k
end
return o
end
function pick(collection)
return collection[math.random(1, #collection)]
end
function resolveBrackets(line)
return line:gsub("{[^{}]-}", function(s)
return pick(split(s:sub(2, -2), '|'))
end)
end
function runLine(line, sets)
line = resolveBrackets(line)
return line:gsub("<.->", function(s)
return runLine(pick(sets[s:sub(2, -2)]), sets)
end)
end
function run(output, sets)
for i = 1, #output do
io.write(runLine(output[1], sets), '\n')
end
end
function options(...)
local opt = {}
for i = 1, select('#', ...) do
local arg = select(i, ...)
if arg:sub(1,1) == '-' then
local param, value = arg:match('%-%-?([^-=]+)=?(.*)')
if value == nil then value = true end
opt[param] = value
else
opt[#opt+1] = arg
end
end
return opt
end
function main(opt)
local times = tonumber(opt['times']) or 1
for i = 1, #opt do
local state = createState()
local output, sets = parseFile(opt[i], state)
for j = 1, times do
run(state.outputs, state.sets)
end
end
end
function globalerror(_, index) error("global access: "..index) end
setmetatable(_G, { __newindex = globalerror, __index = globalerror })
main(options(...))
Your <pack> contains <item>, <item> and <item>.
[item]
some coins
an old {silver|bronze} ring
a handkerchief
a shard of bone
some lint
a tin of tobacco
another <pack> containing <item>
[pack]
pack
#include "sample_packs.txt"
purse
backpack
bag
knapsack
rucksack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment