liveBook · Manning

Rhyming word

As explained in Tiny Python Projects: Learn coding and testing with puzzles and games

A rhyming word is a word whose ending sound matches the ending sound of another word.

A rhyming word shares an ending sound with another word. The Rhymer program models this by separating initial consonant sounds from the remaining word, then replacing those initial sounds with alternatives. section 14.0 section 14.1.1

The program handles rhyming candidates by:

  • Lowercasing the input and identifying its leading consonant sound, including clusters such as ch, sh, and thr. section 14.0 section 14.1.1
  • Replacing the initial consonants while preserving the remaining stem, then sorting the generated strings alphabetically. section 14.0
  • Handling vowel-starting words by prepending consonant sounds and rejecting inputs with no usable vowel-containing remainder. section 14.0 section 14.1.1

Generated candidates

Running the program with cake begins bake, blake, and brake, ends with xake, yake, and zake, and produces 56 candidates. Apple produces bapple, blapple, and brapple, with 57 candidates. section 14.0

In this chapter, we’re going to take a given word and create “words” that rhyme. For instance, the word “bake” rhymes with words like “cake,” “make,” and “thrake,” the last of which isn’t actually a dictionary word but just a new string I created by replacing the “b” in “bake” with “thr.”

The algorithm we’ll use will split a word into any initial consonants and the rest of the word, so “bake” is split into “b” and “ake.” We’ll replace the “b” with all the other consonants from the alphabet plus these consonant clusters:


Inspect the substitution model through the bake example and its initial-consonant replacement algorithm. section 14.0

How Tiny Python Projects: Learn coding and testing with puzzles and games covers it

How deep it goes
Defines a tested stemmer and generation rules, but defers dictionary validation to further exercises.
Examples
Demonstrates cake, chair, apple, uppercase GUITAR, and invalid RDNZL inputs through generated output and tests.
What you can do after
Builds a tested Python program that generates sorted consonant-substitution candidates from a word.
How it connects
Connects rhyming-word generation with regular expressions, list comprehensions, filter(), truthiness, and unit testing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Tiny Python Projects: Learn coding and testing with puzzles and games. The section links point to the parts of the book it draws on.