Playing with Python classes
For my Reading and Writing Electronic Text class I created a ‘SherbetGenerator’ class for my ‘Sherbet’ poetry stylings. It includes some new arguments for inserting certain line embellishments (“OOH, [line] YEAH!” and “uh huh, UH HUH!”) and an argument for the percent of lines to feature said embellishments. Check out the code.
class SherbetGenerator(object):
def __init__(self, oohYeah, uhHuh, chance):
self.oohYeah = oohYeah
self.uhHuh = uhHuh
self.chance = chance
def subst(self, source):
source = source.strip()
# Substitute for words ending certain ways.
source = re.sub(r'[a][d]\b', 'adness\n', source)
source = re.sub(r'[e][d]\b', 'eth', source)
source = re.sub(r'[y]\b', 'ybert', source)
return source
def randomAdd(self, source, chance):
tempRand = random.random()
tempRandTwo = random.random()
tempChance = tempRand * 100
tempChanceTwo = tempRandTwo * 100
if (tempChance < chance):
if (self.oohYeah == 1):
source = "Ooh, " + source + " YEAH!"
if (tempChanceTwo < chance):
if (self.uhHuh == 1):
source = source + " uh huh, UH HUH!"
return source
def searchNChop(self, source, counter):
if re.search(r'^\b[AEIOUTtaeiou]\w*', source):
source = source.replace(" there ", " O'er there ")
# Increase the counter
# Chop the line in half every other line
if counter % 2 == 0:
words = source.split(" ")
length = len(words)
# print length
# print length/2
source = " ".join(words[0:((length/2))])
secondLine = " ".join(words[length/2:])
source = self.randomAdd(source, self.chance)
secondLine = self.randomAdd(secondLine, self.chance)
print source
print secondLine
else:
source = self.randomAdd(source, self.chance)
print source
if __name__ == '__main__':
import sys
import re
import random
# Counter used to determine every other line
counter = 1
generator = SherbetGenerator(oohYeah = 1, uhHuh = 1, chance = 20)
for line in sys.stdin:
line = line.strip()
newSource = generator.subst(line)
generator.searchNChop(newSource, counter)
counter = counter + 1