Skip to content

Instantly share code, notes, and snippets.

@rlieberman
Last active August 29, 2015 14:17
Show Gist options
  • Save rlieberman/a3347a425afdf75e0f6e to your computer and use it in GitHub Desktop.
Save rlieberman/a3347a425afdf75e0f6e to your computer and use it in GitHub Desktop.
A script to create visual poems in Python, made for RWET Midterm by Rebecca Lieberman.
#SHAPESHIFT: A Poetic Form by Rebecca Lieberman
#Reading and Writing Electronic Text Midterm Project
#Made at ITP, Spring 2015
import sys
import random
words_by_len = dict() #create an empty dictionary to store words by their length
#Create a dictionary where the key is the # of characters in the word and the value is a list of words
for line in sys.stdin: #load each line from standard in
line = line.strip() #strip the whitespace
words = line.split() #split each line into a list of words
for word in words:
word = word.strip('!.?,;:-') #NOT WORKING - strip the punctuation from each word
charcount = len(word) #assign the length of the word to a variable 'charcount'
if charcount not in words_by_len: #if that length is not yet in the dictionary as a key
words_by_len[charcount] = [] #add it to the dictionary as a key
if word not in words_by_len[charcount]: #if statement to get rid of duplicates
words_by_len[charcount].append(word) #words_by_len[charcount] evaluates to a list - add each word to that list
#-------------------------------------SHAPE 1: SAWTOOTH-----------------------------------
#-------------------------------------ONE WORD PER LINE-----------------------------------
def sawToothLeft(startCharCount, endCharCount):
for x in range(8):
for i in range(startCharCount,endCharCount):
print random.choice(words_by_len[i])
for i in reversed(range(startCharCount,endCharCount-1)):
print random.choice(words_by_len[i])
def sawToothRight(startCharCount, endCharCount):
for x in range(8):
for i in range(startCharCount,endCharCount):
print random.choice(words_by_len[i]).rjust(15)
for i in reversed(range(startCharCount,endCharCount-1)):
print random.choice(words_by_len[i]).rjust(15)
sawToothLeft(3,8)
sawToothRight(3,8)
#***NEED TO ADD SOME KIND OF CHECK: if endCharCount is greater than what's in words_by_len
#----------------------------------SHAPE 2: DIAMOND CHAIN---------------------------------
#---------------------MULTIPLE WORDS PER LINE-----LEFT OR RIGHT JUSTIFY-------------------
#-------------------------1 X CHAR WORD, 2 X CHAR WORD, 3 TX CHAR WORD--------------------
def diamondChain(diamondWidth, charCount):
for x in range(10):
for i in range(diamondWidth):
print " ".join(random.sample(words_by_len[charCount], i)).center(50)
for i in range(diamondWidth, 0, -1):
print " ".join(random.sample(words_by_len[charCount], i)).center(50)
diamondChain(3, 6)
#-----------------------------------SHAPE 3: HOURGLASS------------------------------------
#---------------------------------MULTIPLE WORDS PER LINE---------------------------------
def hourGlass(width, charCount):
for i in range(width, 0, -1):
print " ".join(random.sample(words_by_len[charCount], i)).center(50)
for i in range(width+1):
print " ".join(random.sample(words_by_len[charCount], i)).center(50)
hourGlass(10, 6)
#-----------------------------------SHAPE 4: CHECKERBOARD------------------------------------
#---------------------------------MULTIPLE WORDS PER LINE---------------------------------
def checkerBoard (charCount, spacing):
centerOffset = 0
for x in range(8):
for i in range(4):
print random.choice(words_by_len[charCount]).center(centerOffset)
centerOffset = centerOffset + spacing
for i in range(5):
checkerBoard(6, 20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment