Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A Twitter bot making philosophical musings via a Markov Chain.
import glob
import markovify
import pickle
import tweepy
import settings_tweet as st
#Bot Creation
#Files consisted of five books downloaded fromthe Project Gutenberg website: 1) Beyong Good and Evil, 2) The Analysis of Mind,
#3) The Critique of Pure Reason, 4) The Prince, 5) The Republic. Cleaning of text done via https://github.com/catseye/Guten-gutter.
def read_files(files):
file_list = []
for file in files:
with open(file) as f:
file_list.append(f.read())
return file_list
files = glob.glob('./corpora_processed/*.txt')
corpora_files = read_files(files)
markov_models = [markovify.Text(corpus) for corpus in corpora_files]
full_philosophy_model = markovify.combine(markov_models)
with open('philosophy_bot.pkl', 'wb') as f:
pickle.dump(full_philosophy_model, f, protocol = 2)
#Bot Deployment
#Deployed on an AWS EC2 instance to run three times a day via a cronjob.
with open('philosophy_bot.pkl', 'rb') as f:
bot = pickle.load(f)
CONSUMER_KEY = st.CK
CONSUMER_SECRET = st.CS
ACCESS_KEY = st.AK
ACCESS_SECRET = st.AS
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
api.update_status(bot.make_short_sentence(140))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment