Skip to content
A small program to detect gibberish using a Markov Chain
Find file
Latest commit b1c6b12 Aug 24, 2015 @rrenaud Merge pull request #3 from rtkrruvinskiy/license
Add MIT license
Failed to load latest commit information.
LICENSE Add MIT license Aug 24, 2015
README.rst cleaned up readme Jun 11, 2011
bad.txt first commit Jun 10, 2011
big.txt first commit Jun 10, 2011 fixed avg transiton and model loading bugs reported by Didier Stevens Mar 3, 2014



A sample program I wrote to detect gibberish. It uses a 2 character markov chain.

This is a nice (IMO) answer to this guys question on stackoverflow.


First train the model:


Then try it on some sample input


my name is rob and i like to hack True

is this thing working? True

i hope so True

t2 chhsdfitoixcv False

ytjkacvzw False

yutthasxcvqer False

seems okay True

yay! True

How it works

The markov chain first 'trains' or 'studies' a few MB of English text, recording how often characters appear next to each other. Eg, given the text "Rob likes hacking" it sees Ro, ob, o[space], [space]l, ... It just counts these pairs. After it has finished reading through the training data, it normalizes the counts. Then each character has a probability distribution of 27 followup character (26 letters + space) following the given initial.

So then given a string, it measures the probability of generating that string according to the summary by just multiplying out the probabilities of the adjacent pairs of characters in that string. EG, for that "Rob likes hacking" string, it would compute prob['r']['o'] * prob['o']['b'] * prob['b'][' '] ... This probability then measures the amount of 'surprise' assigned to this string according the data the model observed when training. If there is funny business with the input string, it will pass through some pairs with very low counts in the training phase, and hence have low probability/high surprise.

I then look at the amount of surprise per character for a few known good strings, and a few known bad strings, and pick a threshold between the most surprising good string and the least surprising bad string. Then I use that threshold whenever to classify any new piece of text.

Peter Norvig, the director of Research at Google, has this nice talk about "The unreasonable effectiveness of data" here, This insight is really not to try to do something complicated, just write a small program that utilizes a bunch of data and you can do cool things.

Something went wrong with that request. Please try again.