Permalink
Cannot retrieve contributors at this time
Fetching contributors…
| #found here http://agiliq.com/blog/2009/06/generating-pseudo-random-text-with-markov-chains-u/ | |
| #11-2012 | |
| import random | |
| import inspect | |
| import re | |
| def lineno(): | |
| """Returns the current line number in our program.""" | |
| return str(inspect.currentframe().f_back.f_lineno) | |
| 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() | |
| print lineno()+" __init__" | |
| def file_to_words(self): | |
| self.open_file.seek(0) | |
| data = self.open_file.read() | |
| words = data.split() | |
| print lineno()+" file_t0_words" | |
| 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). | |
| """ | |
| a = None | |
| while a is False: | |
| if len(self.words) < 3: | |
| a = False | |
| print lineno()+" TRIPLES." | |
| 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] | |
| print lineno()+" DATABASE." | |
| def generate_markov_text(self, par_len): | |
| size = par_len | |
| print lineno()+" begin markovgen.generate_markov_text" | |
| 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 | |
| # print lineno()+" "+w1+" WTF!!!!!!!!!!!!!!!!!!" | |
| # print lineno()+" "+seed_word | |
| # print lineno()+" "+w2+" WTF!!!!!!!!!!!!!!!!!!" | |
| # print lineno()+" "+next_word | |
| gen_words = [] | |
| print lineno()+" GENERATE_MARKOV_TEXT" | |
| try: | |
| for i in xrange(size): | |
| gen_words.append(w1) | |
| w1, w2 = w2, random.choice(self.cache[(w1, w2)]) | |
| except KeyError: | |
| return None | |
| gen_words.append(w2) | |
| print lineno()+" GENERATE_MARKOV_TEXT AFTER for i in xrange." | |
| return ' '.join(gen_words) | |