This week in Reading and Writing Electronic Text, we learned object oriented Python. I rewrote my midterm using object oriented code, basically just making an AMachine object (assonance or alliteration machine), which basically used the same code. I had to change the scope of thePoem variable to get it all out to terminal, and I changed some of the letters to get a slightly different output from a section of The Devil Tree.
And the code:
# midterm object oriented class AMachine(object): def __init__(self): pass def makePoem(self, txt_lines): key = "" tempKey = "" firstWord = 0 keySet = 0 tempKeySet = 0 wordSet = 0 notWord = 0 thePoem = "" # break through lines for line in txt_lines: line.strip() #split words words = line.split(" ") #go through words #print words for word in words: #go through letters tempKeySet = keySet for letter in word: if re.search(r"[aioult]", letter.lower()): if notWord == 0: if keySet == 0: # find the first vowel tempKey = letter.lower() keySet = 1 wordSet = 1 else: if letter.lower() == tempKey: keySet = 0 wordSet = 1 else: wordSet = 0 notWord = 1 tempKey = key keySet = tempKeySet #print "*"+key, keySet # else: # print letter if wordSet == 1 and notWord == 0: #print word key = tempKey #print key wordSet = 0 thePoem += " " + word if firstWord == 0: firstWord = 1 else: notWord = 0 wordSet = 0 if firstWord == 0: keySet = 0 firstWord = 1 return thePoem if __name__ == '__main__': import sys args = sys.argv import re # take in a file txt_file = args # read file txt = open(txt_file, 'r') txt_lines = txt.readlines() amachine = AMachine() newPoem = amachine.makePoem(txt_lines) print newPoem