Skip to content

Instantly share code, notes, and snippets.

Created April 7, 2016 21:21
Show Gist options
  • Save bhtucker/5b29318af6cdb0064b7094270e43fad2 to your computer and use it in GitHub Desktop.
Save bhtucker/5b29318af6cdb0064b7094270e43fad2 to your computer and use it in GitHub Desktop.
Example Markov-chain-from-gutenberg-textfile implementation
# -*- coding: utf-8 -*-
An example markov generator that reads corpus one line at a time
and uses numpy for storing / drawing word likelihoods
Example source text:
Some fun example phrases:
'That election of the advocates have, until the sovereign grace,
and we honour their humour; who are compatible with active links
or mahometanism, who can commit.'
'We must comply with the sequestered cottage of the system of party,
intriguing for a charitable project gutenberg license included with
churchmen were present discussion.'
'To be misunderstood, the particular state of interminable forests.'
from collections import Counter, deque
def clean(token):
return ''.join(filter(lambda v: ord(v) < 180, token)).lower()
def tokenize(line):
return deque(map(clean, line.split()))
def generate_tokens(line_generator):
last_token = ''
for line in line_generator:
tokens = tokenize(line)
while tokens:
current_token = tokens.popleft()
yield (last_token, current_token)
last_token = current_token
def assemble_counts(token_generator):
bigram_counts = Counter()
word_counts = Counter()
start_words = set()
for token_pair in token_generator:
bigram_counts[token_pair] += 1
word_counts[token_pair[0]] += 1
if token_pair[0].endswith('.'):
return bigram_counts, word_counts, start_words
class MarkovGenerator(object):
def __init__(self, bigram_counts, word_counts, start_words):
self.bigram_counts = bigram_counts
self.word_counts = word_counts
self.word_list = word_counts.keys()
self.start_words = start_words
def draw(self):
tokens = []
word_idx = int(np.random.multinomial(
1, self.initial_state_prob_vec
word = self.word_list[word_idx]
while not word.endswith('.'):
word_idx = int(np.random.multinomial(
1, self.transition_matrix[word_idx, :]
word = self.word_list[word_idx]
return ' '.join(tokens).capitalize()
def _do_math(self):
self.total_start_words = float(sum([
for word, count in self.word_counts.iteritems()
if word in self.start_words]))
self.initial_state_prob_vec = np.array(
((self.word_counts[w] if w in self.start_words else 0.0)
/ self.total_start_words)
for w in self.word_list]
self.transition_matrix = np.vstack(
[self.bigram_counts[(w1, w2)] / float(self.word_counts[w1])
for w2 in self.word_list]
for w1 in self.word_list
def get_chain_from_file(filename):
with open(filename, 'r') as f:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment