将 Python 代码转换成 Lua 以生成马尔可夫链
2019-1-6 2:27:13
收藏:0
阅读:106
评论:1
作为一个初学者项目,我试图将此 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));
;
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

我认为我独自解决了问题。 我不知道是否应该删除我的问题,但是这里是代码和我的答案:
-- 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)); ;