Nursery Rhyme
[cc lang=”python”]
from BeautifulSoup import BeautifulSoup
import urllib
import sys
import random
words = set()
wordSet = set()
rhyme = dict()
rhymeList = list()
url = “littlemuffet.html”
changer = sys.argv[1]
name = sys.argv[2]
data = urllib.urlopen(url).read()
soup = BeautifulSoup(data)
genderID1 = “Mr.”
genderID2 = “his”
#parses speech
for line in sys.stdin:
line = line.strip()
line_words = line.split(” “)
for word in line_words:
words.add(word)
for word in words:
wordLength = len(word)
if (“.”) in word:
wordSet.add(word[:(wordLength-1)])
elif (“,”) in word:
wordSet.add(word[:(wordLength-1)])
elif (“;”) in word:
wordSet.add(word[:(wordLength-1)])
elif (“?”) in word:
wordSet.add(word[:(wordLength-1)])
elif (“:”) in word:
wordSet.add(word[:(wordLength-1)])
else:
wordSet.add(word)
for word in wordSet:
wordLength = len(word)
lastLetter = word[wordLength-2:wordLength]
if lastLetter in rhyme.keys():
rhyme[lastLetter].append(word)
else:
rhyme[lastLetter]=[word]
if “true” in changer:
gender1 = soup.findAll(‘span’, { “id” : “1” }, { “gender” : changer })
gender2 = soup.findAll(‘span’, { “id” : “4” }, { “gender” : changer })
for i in range(0,len(gender1)):
gender1[i].replaceWith(genderID1)
for i in range(0,len(gender2)):
gender2[i].contents[0].replaceWith(genderID2)
else:
gender1 = soup.findAll(‘span’, { “id” : “1” }, { “gender” : changer })
for i in range(0,len(gender1)):
origWord = gender1[i].contents[0]
gender1[i].replaceWith(origWord)
namer = soup.findAll(‘span’, { “id” : “2” }, { “namer” : “1” })
for i in range(0,len(namer)):
namer[i].contents[0].replaceWith(name)
#rhyme 1
rhyme1 = soup.findAll(‘span’,{ “rhyme” : “1” })
baseRhyme1 = str(rhyme1[0].contents[0])
baseRhyme1Last = baseRhyme1[len(baseRhyme1)-2:len(baseRhyme1)]
rhyme1Search = rhyme[baseRhyme1Last]
rhyme1Word = random.choice(rhyme1Search)
rhyme1[0].replaceWith(baseRhyme1)
rhyme1[1].replaceWith(rhyme1Word)
rhyme1[2].replaceWith(baseRhyme1)
#rhyme 1
rhyme2 = soup.findAll(‘span’,{ “rhyme” : “2” })
letters = random.choice(rhyme.keys())
randomList = rhyme[letters]
rhyme2Word1 = random.choice(randomList)
rhyme2Word2 = random.choice(randomList)
rhyme2[0].replaceWith(rhyme2Word1)
rhyme2[1].replaceWith(rhyme2Word2)
#rhyme 3
rhyme3 = soup.findAll(‘span’,{ “rhyme” : “3” })
baseRhyme3 = str(rhyme3[0].contents[0])
baseRhyme3Last = baseRhyme3[len(baseRhyme3)-2:len(baseRhyme3)]
rhyme3Search = rhyme[baseRhyme3Last]
rhyme3Word = random.choice(rhyme3Search)
rhyme3[0].replaceWith(baseRhyme3)
rhyme3[1].replaceWith(rhyme3Word)
rhyme3[2].replaceWith(baseRhyme3)
print soup
[/cc]
[cc lang=”html”]
Little MissMuffet, sat on a tuffet
Eating her curds and whey
Along came a spider
Who sat down beside her
And frightened MissMuffetaway
[/cc]
Political Nursery Rhyme from Zeven Rodriguez on Vimeo.