Archive for January, 2014

What Would the Bhagavad Gita Say?

Note: I wrote this post in Ghost originally, check it out!

You are the evil

This year I’m in a Humanities class. To finalize the semester, each student presents a “Creative Project.” The project includes a 10-15 line recitation from one of the texts we’ve read, a supplement (art piece, sculpture, song, dance, etc.), and a 5-7 minutes talk.

I’m reciting a passage from the Bhagavad Gita and was at a loss of what to include as a supplement until I remembered What Would I Say.

What Would I Say is a project by HackPrinceton which creates a Markov chain from your Facebook statuses and uses it to generate sentences that sound like you. The results are “often hilarious (and sometimes nonsensical).” I decided that as my supplement I wanted to create a Markov chain of the Bhagavad Gita, the goal being to generate phrases that sound like they were pulled from the text but really make little to no sense. This ended up being easier than I expected. Steps are as follows.

  • Extract the text from the Bhagavad Gita into plaintext
    • I had a PDF copy so extracting just the text was tough, but after half an hour or so of regular expressions and manual editing, it was fine
  • Create a Markov chain from the result
    • I used codebox’s markov-chain project from GitHub
    • I initially only generated the chain with a depth of 3, but added chains of depth 2 and 4 later — depth 2 produces far more disjointed and ridiculous sentences, depth 4 produces almost verbatim sentences with occasional changes
  • Get a nice template
    • Shamelessly copied from What Would I Say
  • Programatically make random sentences available through an API
    • I modified the markov-chain project slightly and put it inside a Flask wrapper
  • Write some JavaScript to grab new results from the API when the big button is clicked

That’s it! The final result can be seen at and works pretty nicely. The Markov chain depth defaults to 3, but it’s worth playing with 2 and 4 as well.