Last active
November 9, 2016 01:57
-
-
Save MarkEEaton/29a312a804e13fcb415f to your computer and use it in GitHub Desktop.
Bot Day: Script 5
This file contains 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 python | |
# -*- coding: utf-8 -*- | |
# Original script (kept up to date): https://github.com/robincamille/bot-tutorial/blob/master/mashup_markov/markovmaker.py | |
# 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 | |
newtext.append(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 mybot2.py | |
# insert new filename | |
outfile.close() | |
original.close() | |
# Script modified from http://agiliq.com/blog/2009/06/generating-pseudo-random-text-with-markov-chains-u/ | |
# Original MarkovGen library from https://github.com/mattspitz/markovgen - 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