14 Rhymer: Using regular expressions to create rhyming words

 

In the movie The Princess Bride, the characters Inigo and Fezzik have a rhyming game they like to play, especially when their cruel boss, Vizzini, yells at them:

Inigo: That Vizzini, he can fuss.

Fezzik: I think he likes to scream at us.

Inigo: Probably he means no harm.

Fezzik: He’s really very short on charm.

When I was writing the alternate.txt for chapter 7, I would come up with a word like “cyanide” and wonder what I could rhyme with that. Mentally I started with the first consonant sound of the alphabet and substituted “b” for “byanide,” skipped “c” because that’s already the first character, then “d” for “dyanide,” and so forth. This is effective but tedious, so I decided to write a program to do this for me, as one does.


This is basically another find-and-replace type of program, like swapping all the numbers in a string in chapter 4 or all the vowels in a string in chapter 8. We wrote those programs using very manual, imperative methods, like iterating through all the characters of a string, comparing them to some wanted value, and possibly returning a new value.

In the final solution for chapter 8, we briefly touched on “regular expressions” (also called “regexes”--pronounced with a soft “g” like in “George”), which give us a declarative way to describe patterns of text. The material here may seem a bit of a reach, but I really want to help you dig into regexes to see what they can do.

14.1 Writing rhymer.py

14.1.1 Breaking a word

14.1.2 Using regular expressions

14.1.3 Using capture groups

14.1.4 Truthiness

14.1.5 Creating the output

14.2 Solution

14.3 Discussion

14.3.1 Stemming a word

14.3.2 Formatting and commenting the regular expression

14.3.3 Using the stemmer() function outside your program

14.3.4  Creating rhyming strings

14.3.5  Writing stemmer() without regular expressions

14.4 Going further

sitemap