将 Python 代码转换成 Lua 以生成马尔可夫链

作为一个初学者项目,我试图将此 Python 代码翻译成 Lua 代码 https://eli.thegreenplace.net/2018/elegant-python-code-for-a-markov-chain-text-generator/ 。我在将 Python 的 "random.choices" 翻译成 Lua 时遇到了问题。马尔可夫矩阵本身应该可以工作。对于任何能够帮助我使用此 Lua 代码生成马尔可夫链的帮助,我将不胜感激。 以下是马尔可夫矩阵对于马尔可夫序列 3 和输入 "1111111111111110000006000000000006600" 的输出。

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,百分之百的概率, 对于序列 111,后面跟随的是 1,概率为 12/13,后面跟随的是 0,概率为 1/13, 对于序列 060,后面跟随的是 0,百分之百的概率,等等。

以下是我的 Lua 代码:

math.randomseed(os.time()- os.clock() * 1000);
-- 制作字典;
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;
;
;
-- 制作马尔可夫矩阵;
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;
;
;
-- 制作马尔可夫链;
print('Sampling...');
;
-- 制作关键字列表;
local keyset={};
local n=0;
for k,v in pairs(model) do;
n=n+1;
keyset[n]=k;
end;
-- 制作随机关键字;
local randomKey = keyset[math.random(#keyset)];
print("RandomKey: ", randomKey)
state = randomKey;
;
-- 从随机关键字制作表格;
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));
;
-- 制作值列表;
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;
;
;
-- 制作马尔可夫链;
for i = 1, 400 do;
table.insert(out, #out + 1, math.random(10));
state = string.sub(state, 2, #state) .. out[#out];
end;
-- 输出马尔可夫链;
print(table.concat(out));
;
点赞
用户10792661
用户10792661

我认为我独自解决了问题。 我不知道是否应该删除我的问题,但是这里是代码和我的答案:

-- LUA MARKOV 生成器;
;
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 生成器");
print("Markov 阶数:", math.floor(markovOrder));
;
-- 制作字典;
;
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;
;
-- 制作马尔可夫矩阵;
;
local model = defaultdict(function() return {} end);
local data = {};
for i = 1, #markovInputList do;
data[i] = markovInputList[i];
end;
print("数据大小:", #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;
;
-- 从字典制成表格;
;
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("制作一个马尔可夫链...");
;
-- 制作起始点;
;
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;
;
-- 制作马尔可夫链;
;
for i = 1, markovChainLength do;
;
-- 加权随机选择;
;
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 ("马尔可夫链:", table.unpack(out, markovOrder + 1, #out));
;
2019-01-06 18:06:52