If you're feeling more aleatoric ambitions, try reading in multiple files at a time and mashing them together, leaving the outcome up to chance. You still need to use text documents, and in this example I am pretending to use texts from Whitman and Christina Rossetti, which are available in the public domain at Project Gutenberg. In actuality this program calls up my sample poems document and my dissertation, but you can use as many as you want. (A mashup of Whitman's Leaves of Grass + Poe's The Raven led to some interesting data about Whitman's poem.)

 

Exercise 1.2: Same as Exercise 1.0, but now use multiple texts (public domain, news, etc) along with your own. 


import random

words = list()

rossetti = open('C:\\Users\\lillian.bertram\\Documents\\personalsciencediss.txt').read()
whitman = open('C:\\Users\\lillian.bertram\\Documents\\samplepoems1.txt').read()

for item in whitman.split():
    words.append(item)

for item in rossetti.split():
    words.append(item)

for i in range(20):
    #num_words_this_line = (10)
    num_words_this_line = random.randrange(1, 15)
    words_this_line = random.sample(words, num_words_this_line)
    print (" ".join(words_this_line))