Lists and Dictionaries, Oh My
This week’s assignment for Reading and Writing Electronic Text required that we write a Python program that uses one or more of the following data structures: sets, lists, and dictionaries. The program that I wrote takes the lines of a text and removes most of the punctuation, then randomly selects eight lines to create a poem.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #Kim Ash #creates list of lines and randomly sample it #uses dictionary to remove punctuation from poem import sys import random linelist = list() punctuation = {'.': '\n', ', ': ' ', '; ': '\n', '"': '', '--': '--\n', '(': ' ', ')': '', '_': ''} for line in sys.stdin: line = line.strip() if len(line) > 0: for key in punctuation.keys(): line = line.replace(key, punctuation[key]) linelist.append(line) selection = random.sample(linelist, 8) for item in selection: print item |
I applied my program to the text of Edgar Allan Poe’s “The Masque of the Red Death” and Franz Kafka’s “Metamorphosis.” The language of “Masque” is a bit more poetic–possibly because it hasn’t been translated from another language–so I like its results better. The first poem is from “Masque”:
The Masque of the Red Death unimpeded he passed within a yard of the prince's person and while step made closer approach to the speaker But from a certain nameless after them as they depart And now again the music swells and the circuit of the face and the hour was to be stricken there came from casements The fourth was furnished and lighted with orange-- the fifth hale and light-hearted friends from among the knights and dames of his the brazen lungs of the clock a sound which was clear and loud and deep
This one is also from “Masque”:
and six hundred seconds of the Time that flies there came yet another face of the victim were the pest ban which shut him out from the aid that of the last of the gay And the flames of the tripods expired tangible form despair a throng of the revellers at once threw themselves into the the sound and thus the waltzers perforce ceased their evolutions and And Darkness and Decay and the Red Death held illimitable dominion over circuit of the face and the hour was to be stricken there came from
This last poem is from “Metamorphosis”:
come into his room with her violin as no-one appreciated her turned his head his father merely stamped his foot all the harder cold she would stay at the window breathing deeply for a little all but he soon had to admit that the women going to and fro their and held particularly good promise for the future The greatest room being carefully shut had woken him The light from the father came into the living room before he went into the kitchen, curious to learn what they would say when they caught sight of him
To see other work from this class, check out my RWET Github Repo.