[ create a new paste ] login | about

Link: http://codepad.org/755W5lZu    [ raw code | fork ]

ninwa - Python, pasted on Oct 6:
#!/usr/bin/python
# mixism.py - accepts a filename as a parameter and uses the files contents to 
#             create psudo-readable text by examining the files contents and
#             specifcally noting each word (as defined by characters separated
#             by white-space) and noting every word to ever come after it. After
#             such observations, the words are then presented in a sequence that
#             - while natural on an individual level - will not be entirely nat-
#             ural on a whole sum.
#
#             The output is intended to be comical and by no means a serious at-
#             tempt at manufacturing realistic sounding writing.

import sys
import random

def generate_chain_dict(filename):
	words = {}
	try:
		f = open(filename)
		prv_word = None

		for line in f:
			for word in line.split():
				if prv_word == None:
					prv_word = word
					continue
 
				if not prv_word in words:
					words[prv_word] = []

				words[prv_word].append(word)
				prv_word = word
	except IOError:
		print 'Error: unable to open file', filename

	return words

def generate_paragraph(d, wordsleft=-1):
	if len(d) == 0:
		return

	if wordsleft == None:
		wordsleft = -1

	random.seed(None)
	bootstrap = random.randrange(0,len(d))
	nextword = d.keys()[bootstrap]
	text = ''

	while len(d) > 0 and wordsleft != 0:
		text += ' ' + nextword

		if nextword in d and len(d[nextword]) > 0:
			# choose a word from the list at random.
			lastword = nextword			
			nextword = random.randrange(0, len(d[lastword]))
			nextword = d[lastword][nextword]
			d[lastword].remove(nextword)
		else:
			if nextword in d:
				del d[nextword]
			if not len(d) == 0:
				bootstrap = random.randrange(0,len(d))
				nextword = d.keys()[bootstrap]
		wordsleft -= 1

	return text

if __name__ == "__main__":
	if len(sys.argv) == 2:
		chain_dict = generate_chain_dict(sys.argv[1]) 
		text = generate_paragraph(chain_dict, 30) # generate 30 words
		print text

	else:
		print 'Usage: mixism.py filename'


Create a new paste based on this one


Comments: