在Python上基于Markov链生成伪随机文本的教程
作者:goldensun 时间:2023-07-02 09:24:03
首先看一下来自Wolfram的定义
马尔可夫链是随机变量{X_t}的集合(t贯穿0,1,...),给定当前的状态,未来与过去条件独立。
Wikipedia的定义更清楚一点儿
...马尔可夫链是具有马尔可夫性质的随机过程...[这意味着]状态改变是概率性的,未来的状态仅仅依赖当前的状态。
马尔可夫链具有多种用途,现在让我看一下如何用它生产看起来像模像样的胡言乱语。
算法如下,
找一个作为语料库的文本,语料库用于选择接下来的转换。
从文本中两个连续的单词开始,最后的两个单词构成当前状态。
生成下一个单词的过程就是马尔可夫转换。为了生成下一个单词,首先查看语料库,查找这两个单词之后跟着的单词。从它们中随机选择一个。
重复2,直到生成的文本达到需要的大小。
代码如下
import random
class Markov(object):
def __init__(self, open_file):
self.cache = {}
self.open_file = open_file
self.words = self.file_to_words()
self.word_size = len(self.words)
self.database()
def file_to_words(self):
self.open_file.seek(0)
data = self.open_file.read()
words = data.split()
return words
def triples(self):
""" Generates triples from the given data string. So if our string were
"What a lovely day", we'd generate (What, a, lovely) and then
(a, lovely, day).
"""
if len(self.words) < 3:
return
for i in range(len(self.words) - 2):
yield (self.words[i], self.words[i+1], self.words[i+2])
def database(self):
for w1, w2, w3 in self.triples():
key = (w1, w2)
if key in self.cache:
self.cache[key].append(w3)
else:
self.cache[key] = [w3]
def generate_markov_text(self, size=25):
seed = random.randint(0, self.word_size-3)
seed_word, next_word = self.words[seed], self.words[seed+1]
w1, w2 = seed_word, next_word
gen_words = []
for i in xrange(size):
gen_words.append(w1)
w1, w2 = w2, random.choice(self.cache[(w1, w2)])
gen_words.append(w2)
return ' '.join(gen_words)
为了看到一个示例结果,我们从古腾堡计划中拿了沃德豪斯的《My man jeeves》作为文本,示例结果如下。
In [1]: file_ = open('/home/shabda/jeeves.txt')
In [2]: import markovgen
In [3]: markov = markovgen.Markov(file_)
In [4]: markov.generate_markov_text()
Out[4]: 'Can you put a few years of your twin-brother Alfred,
who was apt to rally round a bit. I should strongly advocate
the blue with milk'
[如果想执行这个例子,请下载jeeves.txt和markovgen.py
马尔可夫算法怎样呢?
最后两个单词是当前状态。
接下来的单词仅仅依赖最后两个单词,也就是当前状态。
接下来的单词是从语料库的统计模型中随机选择的。
这是一个示例文本。
"The quick brown fox jumps over the brown fox who is slow jumps over the brown fox who is dead."
这个文本对应的语料库像这样,
{('The', 'quick'): ['brown'],
('brown', 'fox'): ['jumps', 'who', 'who'],
('fox', 'jumps'): ['over'],
('fox', 'who'): ['is', 'is'],
('is', 'slow'): ['jumps'],
('jumps', 'over'): ['the', 'the'],
('over', 'the'): ['brown', 'brown'],
('quick', 'brown'): ['fox'],
('slow', 'jumps'): ['over'],
('the', 'brown'): ['fox', 'fox'],
('who', 'is'): ['slow', 'dead.']}
现在如果我们从"brown fox"开始,接下来的单词可以是"jumps"或者"who"。如果我们选择"jumps",然后当前的状态就变成了"fox jumps",再接下的单词就是"over",之后依此类推。
提示
我们选择的文本越大,每次转换的选择更多,生成的文本更好看。
状态可以设置为依赖一个单词、两个单词或者任意数量的单词。随着每个状态的单词数的增加,生成的文本更不随机。
不要去掉标点符号等。它们会使语料库更具代表性,随机文本更好看。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
人工智能学习pyTorch自建数据集及可视化结果实现过程
![](https://img.aspxhome.com/file/2023/5/75105_0s.png)
javascript让浏览器实现复读机的功能
IE下绝对定位的元素不能响应鼠标的bug修正
![](https://img.aspxhome.com/file/UploadPic/20089/10/20089101380482s.png)
Golang Http请求返回结果处理
SQL Server 数据库备份和还原认识和总结(二)
Python调用Prometheus监控数据并计算
![](https://img.aspxhome.com/file/2023/8/117338_0s.jpg)
PyTorch中常用的激活函数的方法示例
![](https://img.aspxhome.com/file/2023/5/85275_0s.png)
python模拟点击在ios中实现的实例讲解
Python 中迭代器与生成器实例详解
![](https://img.aspxhome.com/file/2023/0/92250_0s.png)
chatgpt 1020 错误码成功解决的三种方案(推荐)
![](https://img.aspxhome.com/file/2023/3/104963_0s.png)
Windows环境下实现批量执行Sql文件
![](https://img.aspxhome.com/file/2023/2/98062_0s.png)
python读取csv和txt数据转换成向量的实例
vue2 拖动排序 vuedraggable组件的实现
Mysql两表联合查询的四种情况总结
![](https://img.aspxhome.com/file/2023/4/109824_0s.png)
JavaScript中clientWidth,offsetWidth,scrollWidth的区别
Python WSGI的深入理解
Python 将 QQ 好友头像生成祝福语的实现代码
![](https://img.aspxhome.com/file/2023/2/104922_0s.png)
numpy中nan_to_num的具体使用
![](https://img.aspxhome.com/file/2023/5/128565_0s.png)
编写SQL需要注意的细节Checklist总结
python将ansible配置转为json格式实例代码
![](https://img.aspxhome.com/file/2023/3/118403_0s.png)