Skip to content

Instantly share code, notes, and snippets.

@donkirkby
Forked from agiliq/gist:131679
Last active December 15, 2015 19:38
Show Gist options
  • Save donkirkby/5312490 to your computer and use it in GitHub Desktop.
Save donkirkby/5312490 to your computer and use it in GitHub Desktop.
Markov text generator, with bug fix to wrap around from end of source text to the start. Also add a method to generate complete sentences based on the location of periods in the source.
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 self.word_size < 3:
return
for i in range(self.word_size):
yield (self.words[i],
self.words[(i+1)%self.word_size],
self.words[(i+2)%self.word_size])
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)])
return ' '.join(gen_words)
def generate_markov_sentences(self, size=3):
seed = random.randint(0, self.word_size-3)
is_sentence_found = False
while not is_sentence_found:
seed_word = self.words[seed]
is_sentence_found = seed_word.endswith('.')
seed = (seed + 1) % (self.word_size-3)
seed_word, next_word = self.words[seed], self.words[seed+1]
sentence_count = 0
w1, w2 = seed_word, next_word
gen_words = []
while sentence_count < size:
gen_words.append(w1)
if w1.endswith('.'):
sentence_count += 1
w1, w2 = w2, random.choice(self.cache[(w1, w2)])
return ' '.join(gen_words)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment