目录
- 马尔科夫链
Continue, come on.
马尔科夫链
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
local function allwords()
local line = io.read()
local pos = 1
return function()
while line do
local w,e = string.match(line, "(%w+[,;.:]?)()",pos)
if w then
pos = e
return w
else
line = io.read()
pos = 1
end
end
return nil
end
end
local function prefix(w1,w2)
return w1 .. " " .. w2
end
local statetab = {}
local function insert(prefix, value)
local list = statetab[prefix]
if list == nil then
statetab[prefix] = {value}
else
table.insert(list,value)
end
end
local MAXGEN = 200
local NOWORD = "\n"
--创建表
local file = io.open("essay")
io.input(file)
local w1, w2 = NOWORD,NOWORD
for nextword in allwords() do
insert(prefix(w1,w2),nextword)
w1 = w2
w2 = nextword
end
insert(prefix(w1,w2),NOWORD)
--生成文本
w1 = NOWORD; w2 = NOWORD
for i = 1, MAXGEN do
local temp = prefix(w1,w2)
local list = statetab[temp]
--从列表中随机选出一个元素
local r = math.random(#list)
local nextword = list[r]
if nextword == NOWORD then return end
io.write(nextword," ")
w1 = w2; w2 = nextword
end
上面的代码,allwords这个迭代器函数构造的还是十分的巧妙的,利用迭代器遍历文章中的每一个单词,然后将其存到一张表中,方便进行随机整合。