Last active
August 29, 2015 14:20
-
-
Save andrewheiss/482d6e481014bc0ce212 to your computer and use it in GitHub Desktop.
Markov chain lorem ipsum text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# Load libraries | |
import markovgen | |
import argparse | |
import re | |
# Get command line arguments | |
parser = argparse.ArgumentParser(description='Generate random text with Markov chains!') | |
parser.add_argument('corpus', type=argparse.FileType('r+'), | |
help='corpus of training text') | |
parser.add_argument('num_words', type=int, default=100, nargs='?', | |
help='the number of words to output') | |
args = parser.parse_args() | |
# Save arguments | |
corpus = args.corpus | |
num_words = args.num_words | |
# Generate Markov chain | |
markov = markovgen.Markov(corpus) | |
output = markov.generate_markov_text(num_words) | |
# Capitalize first letter of paragraph | |
# Previously used .capitalize(), but that lowercases everything else in the string | |
output = re.sub('([a-zA-Z])', lambda x: x.groups()[0].upper(), output, 1) | |
print(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment