Generate Tweets Using Markov Chains

Want to learn more? I recommend these Python books: Python for Data Analysis, Python Data Science Handbook, and Introduction to Machine Learning with Python.


import markovify

Load Corpus

The corpus I am using is just one I found online. The corpus you choose is central to generating realistic text.

# Get raw text as string
with open("brown.txt") as f:
    text =

Build Markov Chain

# Build the model.
text_model = markovify.Text(text)

Generate One Tweet

# Print three randomly-generated sentences of no more than 140 characters
for i in range(3):
Within a month, calls were still productive and most devotees of baseball attended the dozens of them.
Even death, therefore, has a leather bolo drawn through a local rajah in 1949.
He had a rather sharp and confident.