Did you know that you can navigate the posts by swiping left and right?

Markov News

18 Jan 2017 . category: projects

I’ve been dreaming about ideas for a simple but off-hand useful (and fun!) twitter bot, and the ridiculous #fakenews sensation became an opportunity. Many people who have taken an intro computer science course are familiar with markov chains. Without getting too technical, a markov chain is a process where the next outcome in the process, given the current state, is independent of past behavior, and where the possible outcomes are discrete known values. This algorithm turns out to be a great way to generate text that reads in a specific voice. (This isn’t a new trick - there are plenty of markov text generators around the web.)

For my part, I made the account @FakeNewsSEA which tweets out markov generated “headlines” - literally fake news.

To create a base of text to generate my headlines, I scrape actual headlines from RSS feeds provided by Seattle news outlets. This is super easy using the feedparser library. Before passing the text into a Markov generator class (like this one), I add markers to the start and end of each headline. Guaranteeing that the generated text starts with a word used to start a real headline helps the output read more realistically. End markers also help with this, as the more traditional method of trying to stop at a sentence ending doesn’t translate to headlines.

After generating a fake headline, I use tweepy to access the twitter API and post to the account. For fun, the bot also tweets a link to a real article that shares the same first word (at least) as the fake story. Everything lives in a single python script, and heroku scheduler kicks off a job once a day.

Overall I’m happy with the result, especially with how easy this was to put together, and I’m glad that hosting turned out to be simple (and free!). Tuning the markov generation was a little tricky, but once I added the start and end markers into the algorithm it became much more consistent and readable. Adding more news sources may help with variation, but I’d also like to keep the fake news both local and timely. If you haven’t already, be sure to follow @FakeNewsSEA for your daily dose of actually fake news.


Eric Jones builds software that helps people. If he's not at a keyborad, he's probably running through the woods with a map and compass or carrying groceries home on his bicycle.