Freddy Speaks!

Try out the Gibberish Generator 3000!

Just a heads-up, this is going to be another very technical – but very fun! – post. Also, if you don’t know about Freddy the Pig, you will probably get a little weirded out.

Historic Seneca Falls

In October, Miranda and I got the opportunity to join the Friends of Freddy in Seneca Falls, New York, for their biennial convention for the bodacious bovine detective. It was a lot of fun! At the end of the weekend, one thing was clear: despite a contentious debate about the quality of the final entry (Freddy and Dragon,) all attendees would certainly welcome a new entry in the Freddy lexicon, if only Walter R. Brooks was still alive. Being a man with a certain set of skills, I thought – what can I do about this?

Step 1: Markov Chains

The idea of using Markov chains for generating strings of nonsensical text is not a new one.  My favorite application of this is the now-infamous @horse_ebooks Twitter account, with such memorable tweets as “Everything happens so much.” A more recent example is the chapter of a fictitious Harry Potter book entitled Harry Potter and the Portrait of What Looked Like a Large Pile of Ash.

I will admit that, unfortunately, I am still hazy on how Markov chains work in a mathematical sense. The best resource I’ve found so far is this Kahn Academy video on the subject. Fortunately, I don’t need to understand it, because I’m a programmer! And if there’s one thing we do best, it’s use a library (Markovite, in this case) and make it sound like we did all the hard work!

Step 2: Regular Expressions

Now we can talk about something that I am much more familiar with! In order to actually generate text with the Markovite library, I need to feed it a steady diet of free-range, grass-fed Freddy the Pig novels. I have about half of the ebooks so having the material isn’t an issue, but I’m worried that if I shove the whole book in, the output will be too generic to really be all that funny.

So, I had a thought: what if I parsed the text from the books and grouped each line of dialog with who said it, and feed each character’s dialogue into Markovite?

Using Ruby, that’s exactly what I did. You can see my annotated source code here, although I will say that it is not representative of code that I write at work (just in case there are any current or future employers looking at this!) Most importantly, it does the job! When a character’s name is given, all the books are parsed and all the dialog is extracted and attempted to be matched to the character. It is surprisingly successful – occasionally (if you know the books well!) you can tell there are mixups, but overall, it is very good.

Step 3: World Wide Webb

The rest of the process was creating a fairly run-of-the-mill web app. I chose Sinatra over Rails because I thought it would be simpler, for a simpler app.

I don’t plan on releasing the source code for this part (beyond what I linked to above) since it’s not really very interesting. But you can check out Freddy Speaks! yourself here:

Some possible lines of dialog on Brooks’ cutting room floor include:

“I think that is the time and another, I wish I’d stuck to detective work, and not use death ray stuff and the folks around town either.” -Freddy

“Ah, it’s the clippers for me. I just can’t do anything.” -Leo (the lion)

“All right, animals, I can’t remember when I begin to think up something.” -Mrs. Wiggins

Step 4: Profit!

Share your own! I included “share to Facebook” and “link to me” links on the bottom left of each page so memorable quotes can be shared with others instead of vanishing into to the ether.

I hope that others enjoy this fun project as much I have! As they say on the Bean farm…

…”good-bye,” I guess? I actually don’t think there is a customary Bean farm good-bye.

That’s correct, I AM wearing a shirt with Freddy on it!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.