Skip to content

Instantly share code, notes, and snippets.

@MarkEEaton MarkEEaton/
Last active Nov 9, 2016

What would you like to do?
Bot Day: Script 5
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Original script (kept up to date):
# Markov-chain text maker
# This script takes a .txt file and makes a mashup of it
# using a Markov chain: linking word phrases together
# from different spots in the text.
# For instance, if the text contained two lines,
# "she has a dog" and "my dog has a tail,"
# this might generate "my dog has a dog" and "she has a tail."
# Housekeeping
import markovgen, re, string
# Choose original file, new filename
original = open('twain.txt')
outfile = open('twain_markov.txt','w')
# Repeatable Markov'd text generator
newtext = []
mk = markovgen.Markov(original)
counter = 0
while counter < 10: # Change 10 to however many lines you want to generate
line = '\n' + mk.generate_markov_text()
#remove punctuation
exclude = ['"','(',')',';']
line = ''.join(ch for ch in line if ch not in exclude)
#make line lowercase, add period at end
line = line.lower() + "."
print line
counter = counter + 1
for aline in newtext:
outfile.write(aline) #makes text file line by line
# next steps if you want to tweet these lines:
# move the newly-made file into the mybot folder
# open
# insert new filename
# Script modified from
# Original MarkovGen library from - modified by RobinCamille to spit out smaller chunks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.