Gibberish train/test
Project description
# pygibberish
Overview
========
A sample program I wrote to detect gibberish. It uses a N character markov chain.
http://en.wikipedia.org/wiki/Markov_chain
This is a nice (IMO) answer to this guys question on stackoverflow.
http://stackoverflow.com/questions/6297991/is-there-any-way-to-detect-strings-like-putjbtghguhjjjanika/6298040#comment-7360747
Usage
=====
import pygibberish
if __name_ == '__main__':
gib = pygibberish.Gibberish(3)
gib.train('pygibberish/train_data/en_big.txt', 'pygibberish/train_data/en_good.txt', 'pygibberish/train_data/en_bad.txt')
gib.save('en2.pki')
print gib.calc("asdfasdf")
or
import pygibberish
if __name_ == '__main__':
gib = pygibberish.Gibberish('en2.pki')
print gib.calc("asdfasdf")
How it works
============
The markov chain first 'trains' or 'studies' a few MB of English/Chinese 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 26 followup character 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']['l'] ... 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.
Overview
========
A sample program I wrote to detect gibberish. It uses a N character markov chain.
http://en.wikipedia.org/wiki/Markov_chain
This is a nice (IMO) answer to this guys question on stackoverflow.
http://stackoverflow.com/questions/6297991/is-there-any-way-to-detect-strings-like-putjbtghguhjjjanika/6298040#comment-7360747
Usage
=====
import pygibberish
if __name_ == '__main__':
gib = pygibberish.Gibberish(3)
gib.train('pygibberish/train_data/en_big.txt', 'pygibberish/train_data/en_good.txt', 'pygibberish/train_data/en_bad.txt')
gib.save('en2.pki')
print gib.calc("asdfasdf")
or
import pygibberish
if __name_ == '__main__':
gib = pygibberish.Gibberish('en2.pki')
print gib.calc("asdfasdf")
How it works
============
The markov chain first 'trains' or 'studies' a few MB of English/Chinese 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 26 followup character 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']['l'] ... 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.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pygibberish-0.0.6.tar.gz
(4.5 MB
view hashes)