将生成马尔可夫链的python代码翻译成lua



作为一个初学者项目,我尝试将此python代码转换为lua代码https://eli.thegreenplace.net/2018/elegant-python-code-for-a-markov-chain-text-generator/。我在正确翻译蟒蛇的"随机选择"方面有问题。马尔可夫矩阵本身应该起作用。我将感谢任何帮助,用这个lua代码生成一个马尔可夫链。这是马尔可夫阶3的马尔可夫矩阵的输出和输入"1111111111110000006000000000006600"。

600 0   1
111 1   12
111 0   1
060 0   1
660 0   1
006 6   1
006 0   1
100 0   1
000 6   2
000 0   11
110 0   1
066 0   1

有了这些信息,我需要计算下一步,但我不知道如何使用lua。对于序列600,0以100%紧随其后,对于序列111,1以12/13紧随其后,对于序列060,0以100%紧随其后,依此类推

这是我的lua代码:

math.randomseed(os.time()- os.clock() * 1000);
-- make dictionary;
function defaultdict(default_value_factory);
local t = {};
local metatable = {};
metatable.__index = function(t, key);
if not rawget(t, key) then;
rawset(t, key, default_value_factory(key));
end;
return rawget(t, key);
end;
return setmetatable(t, metatable);
end;
;
;
-- make markov matrix;
STATE_LEN = 3;
model = defaultdict(function() return {} end)
data = "1111111111111110000006000000000006600";
print("datasize: ", #data)
print('Learning model...')
for i = 1, (#data - STATE_LEN) do;
state = data:sub(i, i + STATE_LEN-1);
print("state: ", state)    
local next = data:sub(i + STATE_LEN, i + STATE_LEN);
print("next: ", next);
model[state][next] = (model[state][next] or 0)+1;
end;
;
;
-- make markov chain;
print('Sampling...');
;
-- make key list;
local keyset={};
local n=0;
for k,v in pairs(model) do;
n=n+1;
keyset[n]=k;
end;
-- make random key;
local randomKey = keyset[math.random(#keyset)];
print("RandomKey: ", randomKey)
state = randomKey;
;
-- make table from random key;
local str = state;
local stateTable = {};
for i = 1, #str do;
stateTable[i] = str:sub(i, i);
end;
;
out = stateTable;
print ("random key as table: ", table.unpack(stateTable));
;
-- make value list;
local valueset={};
local n=0;
for key, value in pairs(model) do;
for k, v in pairs(value) do;
n=n+1;
valueset[n]=v;
end;
end;
;
print("Keys: ", table.unpack(keyset));
print("Vals: ", table.unpack(valueset));
;
for key, value in pairs(model) do;
for k, v in pairs(value) do;
print(key, k, v);
end;
end;
;
;
-- make markov chain;
for i = 1, 400 do;
table.insert(out, #out + 1, math.random(10)); 
state = string.sub(state, 2, #state) .. out[#out];
end;
-- print markov chain;
print(table.concat(out));
;

我想我自己想好了。我不知道我是否应该删除我的问题,无论如何,这是代码和我的答案:

-- LUA MARKOV GENERATOR;
;
local markovOrder = 3;
local markovChainLength = 16;
local randomStart = 0;
local markovInputList = {1, 1, 2, 1 , 1 , 2, 2, 1, 2, 6, 6, 2, 1, 6, 6, 1, 2, 6, 6, 6, 1, 1, 6, 6, 1, 2, 2, 2, 1};
;
math.randomseed(os.time()- os.clock() * 1000);
;
print("LUA MARKOV GENERATOR");
print("Markov Order: ", math.floor(markovOrder));
;
-- make dictionary;
;
local function defaultdict(default_value_factory);
;
local t = {};
local metatable = {};
metatable.__index = function(t, key);
if not rawget(t, key) then;
rawset(t, key, default_value_factory(key));
end;
return rawget(t, key);
end;
return setmetatable(t, metatable);
end;
;
-- make markov matrix;
;
local model = defaultdict(function() return {} end);
local data = {};
for i = 1, #markovInputList do;
data[i] = markovInputList[i];
end;
print("Data Size: ", #markovInputList);
for i = 1, markovOrder do;
table.insert(data, data[i]);
end;
for i = 1, #data - markovOrder do;
local state = table.concat({table.unpack(data, i, i + markovOrder - 1)}, "-");
local next = table.unpack(data, i + markovOrder, i + markovOrder);
model[state][next] = (model[state][next] or 0)+1;
end;
;
-- make tables from dict;
;
local keyTbl = {};
local nexTbl = {};
local prbTbl = {};
for key, value in pairs(model) do;
for k, v in pairs(value) do;
table.insert(keyTbl, key);
table.insert(nexTbl, k);
table.insert(prbTbl, v);
end;
end;
;
print("Key: ", table.unpack(keyTbl));
print("Nex: ", table.unpack(nexTbl));
print("Prb: ", table.unpack(prbTbl));
;
print("Make a Markov Chain...");
;
-- make start key;
;
local startKey = {};
if randomStart == 1 then;
local randomKey = math.random(#keyTbl);
startKey = randomKey;
else;
startKey = 1;
end;
;
local markovString = keyTbl[startKey];
local out = {};
for match in string.gmatch(keyTbl[startKey], "[^-]+") do;
table.insert(out, match);
end;
;
-- make markov chain;
;
for i = 1, markovChainLength do;
;
-- weighted random choices;
;
local choices = {};
local weights = {};
for j = 1, #keyTbl do;
if markovString == keyTbl[j] then;
table.insert(choices, nexTbl[j]);
table.insert(weights, prbTbl[j]);
end;
end;
;
-- print ("choices:", table.unpack(choices));
-- print ("weights:", table.unpack(weights));
;
local totalWeight = 0;
for _, weight in pairs(weights) do;
totalWeight = totalWeight + weight;
end;
rand = math.random() * totalWeight;
local choice = nil;
for i, weight in pairs(weights) do;
if rand < weight then;
choice = choices[i];
break;
else;
rand = rand - weight;
end;
end;
;
table.insert(out, choice);
local lastStep = {table.unpack(out, #out - (markovOrder-1), #out)};
markovString = table.concat(lastStep, "-");
end;
;
print ("Markov Chain: ", table.unpack(out, markovOrder + 1, #out));
;

相关内容

  • 没有找到相关文章

最新更新