Sinisterly
[Source] Glibberish-O-Mat - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: Python (https://sinister.li/Forum-Python)
+--- Thread: [Source] Glibberish-O-Mat (/Thread-Source-Glibberish-O-Mat)



[Source] Glibberish-O-Mat - Deque - 04-03-2013

Have you ever wondered how spam bots create their spam messages?

This is probably how: They use Markov-Chains.
Given one word as input, a Markov Chain will put out another word. The output word is defined by a certain probability. It might be word A with probability of 20% or word B with probability of 80%. This word output is again used as input to get the next word, thus forming a chain of words.

This is my Glibberish-O-Mat that uses this principle. It takes as input a text file (you can use one from Project Gutenberg to get a reasonably long text: http://www.gutenberg.org/ebooks/search/?sort_order=downloads ), analyses the probabilities of the words following other words and then produces a text with the given amount of words using these probabilities.

Usage: glibberish.py <filename> <wordstogenerate>

Example usage: python glibberish.py pg28203.txt 100

Example output:

[Image: 7823rgf9.png]

Python source (Python 2.7):

Code:
import sys import operator import random markov_chain = {} #adds the specified word to the markov chain def add_to_chain(lastword, word): if not markov_chain.has_key(lastword): markov_chain[lastword] = {} if not markov_chain[lastword].has_key(word): markov_chain[lastword][word] = 1 else: markov_chain[lastword][word] += 1 #builds up the markov chain using the specified file def build_chain_from(filename): file = open(filename, 'r') lastword = "first" for word in words(file): add_to_chain(lastword, word) lastword = word #iterates over words in a file def words(file): for line in file: for word in line.split(): yield word #returns a random word with the probability based on the given lastword def get_rand_word(lastword): chain = markov_chain[lastword] total = sum(chain.itervalues()) randval = random.randint(1, total) for key in chain: randval -= chain[key] if randval <= 0: return key return "" #generates a text with the given amount of words def generate_text(amount): lastword = "first" word = get_rand_word(lastword) for i in range(0, amount): word = word + " " + get_rand_word(lastword) return word def print_title(): print(''' _____ _ _ _ _ _ _ | __ \| |(_)| | | | (_) | | | | \/| | _ | |__ | |__ ___ _ __ _ ___ | |__ | | __ | || || '_ \ | '_ \ / _ \| '__|| |/ __|| '_ \ | |_\ \| || || |_) || |_) || __/| | | |\__ \| | | | \____/|_||_||_.__/ |_.__/ \___||_| |_||___/|_| |_| _____ ___ ___ _ | _ | | \/ | | | ______ | | | | ______ | . . | __ _ | |_ |______|| | | ||______|| |\/| | / _` || __| \ \_/ / | | | || (_| || |_ \___/ \_| |_/ \__,_| \__| ''') def main(): print_title() if len(sys.argv) != 3 or not sys.argv[2].isdigit(): print "usage: markov.py <filename> <wordstogenerate>" return filename = sys.argv[1] amount = int(sys.argv[2]) build_chain_from(filename) print generate_text(amount) print main()



RE: [Source] Glibberish-O-Mat - H4R0015K - 04-03-2013

Gutenberg website link is not added properly.


RE: [Source] Glibberish-O-Mat - Deque - 04-03-2013

(04-03-2013, 03:49 PM)H4R0015K Wrote: Gutenberg website link is not added properly.

Thanks for the hint. It should work now.


RE: [Source] Glibberish-O-Mat - Ex094 - 04-03-2013

Nice one Deque, Added the link to the download page so that you can view the text in HTML format (View offered by the site) and copy paste it to the program Smile


RE: [Source] Glibberish-O-Mat - ArkPhaze - 04-04-2013

Not bad, Smile I always knew that there was some formula and algorithm being used for spam bots I have seen in the past. Although probability chose the words, the probability that it would fool me appeared to have failed in the past lol.


RE: [Source] Glibberish-O-Mat - Deque - 04-04-2013

It is not to fool people but to fool anti-spam software.
Or in this case you might just use it to create some funny poems: Wink

Quote:is sound glow despair.
inspired appeared.
the despair.
time sound sensation



RE: [Source] Glibberish-O-Mat - ArkPhaze - 04-04-2013

The only anti-spam i've ever used was a list of known spammer IP's disabled and banned from registration, among a few other registration security features. Implementing anything past that, can lead to unwanted actions sometimes, as not everybody has a certain level of quality in their posts, and not all are good English posters also, so I've never taken the risk to analyze others posts.

I have seen some good ones though that have been autogenerated! Perhaps better than some human posters which is the sad part.