Michael Spencer recently wrote a limerick in Python on the comp.lang.python newsgroup. I was challenged to write one in sonnet form, which I did in about an hour. I've used Shakespearean rhyme scheme, and the result is an actually useful program that gives the wordcount for some HTML input (usage: python wcsonnet.py filename). Only the alphanumerics in each line count towards the syllable total and metre:
from re import compile as regess
from re import sub as regsubstitute
doc = r'(?m)(<!DOCTYPE[\t\n\r ]+\S+[^\[]+?(\[[^\]]+?\])?\s*>)'
pin = regess(r'<\?(\S+)[\t\n\r ]+(([^\?]+|\?(?!>))*)\?>(?P<mute>)')
def stripPI(up): return pin.sub(' ', up)
def strid(down): return regess(doc).sub(' ', down)
def stric(sup): return regsubstitute('<!--(?:[^-]+|-(?!-))+-->', '', sup)
def strit(frown): return regsubstitute('<[^>]+>', '', frown)
def doTheThingWeHaveToDo(*perplexed):
import sys; text = open(sys.argv[1]).read()
WithThisWeProceed = stripPI(stric(text))
BazBarFoo = strit(strid(WithThisWeProceed))
print >> sys.stdout, len(str.split(BazBarFoo))
if __name__=="__main__": doTheThingWeHaveToDo()
Five hundred bonus points for anyone who can make an even more useful program using Petrarchan rhyme scheme.