# This is file wannabe.py.txt import random # some tools to make things random import sys def build_dict(r, context_length): '''Return a dictionary where the keys are tuples of length context_length containing words in reader r and the value for a key is the list of words that were found to follow the key.''' # word_dict: tuple of context_length words -> list of words that follow tuple # algorithm: # context = a tuple of context_length ''s # word_dict = {} # for each line in text or webpage r: # split the line into a list of words # for word in word_dict: # add word to the dict entry for context # update context to the next triple in r # word_dict = {} # The dictionary of words context = ('',) for i in range(context_length-1): context = context + ('',) for line in r: word_list = line.split() for word in word_list: if context in word_dict: word_dict[context].append(word) else: word_dict[context] = [word] context = context[1:] + (word, ) # Take care of the special case of the very last tuple # only appearing once - at the end of the file if not context in word_dict: word_dict[context] = [""] return word_dict def write_epic(word_dict, num_words, context_length): '''Based on the word_dict dictionary, produce an epic of num_words words.''' epic = '' context = ('',) for i in range(context_length-1): context = context + ('',) for i in range(num_words): values = word_dict[context] # get words that follow current context random.shuffle(values) word = values[0] epic += word + " " context = context[1:] + (word, ) # next prefix return epic def main(): # Change the prefix_length to get more strict with what follows words. prefix_length = 2 m = build_dict(open('midsummernight.txt'), prefix_length) f = build_dict(open('fairytale.txt'), prefix_length) a = build_dict(open('alice.txt'), prefix_length) p = build_dict(open('prideandprejudice.txt'), prefix_length) #n = build_dict(open(sys.argv[1]), prefix_length) print ( write_epic(m, 50, prefix_length)) print ("") print (write_epic(f, 50, prefix_length)) print ("") print (write_epic(a, 50, prefix_length)) print ("") print (write_epic(p, 50, prefix_length)) print ("") #print (write_epic(n, 50, prefix_length)) main()