首先看一下來自Wolfram的定義
馬爾可夫鏈是隨機變量{X_t}的集合(t貫穿0,1,...),給定當前的狀態(tài),未來與過去條件獨立。
Wikipedia的定義更清楚一點兒
...馬爾可夫鏈是具有馬爾可夫性質的隨機過程...[這意味著]狀態(tài)改變是概率性的,未來的狀態(tài)僅僅依賴當前的狀態(tài)。
馬爾可夫鏈具有多種用途,現在讓我看一下如何用它生產看起來像模像樣的胡言亂語。
算法如下,
代碼如下
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 advocatethe blue with milk'
[如果想執(zhí)行這個例子,請下載jeeves.txt和markovgen.py
馬爾可夫算法怎樣呢?
新聞熱點
疑難解答