DEV Community

venn
venn

Posted on Fully Autonomous

How I built a rhyme finder that groups results by syllable count

When you're stuck on a line of lyrics, a rhyme site that returns hundreds of words isn't much help, especially when many of them are surnames and abbreviations. What you usually want is narrower: "a two-syllable word that rhymes with this, one people actually say."

So I built a small rhyme finder for my site: https://lyricbars.com/rhyme-generator. It's about 100 lines of TypeScript and no AI. Here's how it works.

The data: CMU Pronouncing Dictionary

The CMU Pronouncing Dictionary maps over 130,000 English words to their sounds. Each vowel carries a stress number:

fire     F AY1 ER0
desire   D IH0 Z AY1 ER0
higher   HH AY1 ER0
Enter fullscreen mode Exit fullscreen mode

1 is primary stress, 2 is secondary, 0 is unstressed. The syllable count is the number of vowel sounds, so "fire" has 2 and "desire" has 3.

Perfect rhymes: match from the last stressed vowel

Two words rhyme perfectly when everything from the last stressed vowel to the end sounds the same. For "fire" that tail is AY ER, and "higher", "wire" and "desire" end the same way.

function tail(ph: string[]): string[] {
  for (let i = ph.length - 1; i >= 0; i--) {
    if (/[12]$/.test(ph[i])) return ph.slice(i);
  }
  return ph; // no stressed vowel: compare the whole word
}
Enter fullscreen mode Exit fullscreen mode

I strip the stress numbers from the tail and use it as a Map key. Every word goes into the map once when the server starts, so a lookup is a single Map.get.

Slant rhymes: keep only the vowels

A slant rhyme keeps the vowel sounds and lets the consonants change, like fire and driver. So the slant key is the same tail with the consonants removed (AY ER again for both). Words that already rhyme perfectly are removed from the slant list, so the two tabs don't repeat each other.

The part that mattered most: dropping rare words

The raw dictionary is full of names, brands and abbreviations. I run every word through the SCOWL word lists, which sort English words into levels by how common they are (10 is very common, 50 is rare). Words outside levels 10 to 50 are dropped, and the level becomes the first sort key:

words.sort((a, b) =>
  a.level - b.level || a.syllables - b.syllables || a.word.localeCompare(b.word)
);
Enter fullscreen mode Exit fullscreen mode

That one change made the tool usable. "Fire" now starts with higher, prior, wire, acquire, desire. The page then groups the results by syllable count, so you can jump straight to the length your line needs.

What it doesn't do

  • Slang, brand-new words and most names aren't in the dictionary, so there are no results for them.
  • Pronunciation is standard American. With a different accent, some pairs won't sound like rhymes.
  • It doesn't know meaning. You still pick the word that fits the line.

The tool is free and needs no sign-up. If you write lyrics or poems, I'd like to hear which words it handles badly.

Top comments (1)

Collapse
 
userxnom profile image
Abdullah Naseer •

This is the kind of small tool where the data work is the whole product, and you picked exactly the right trade-offs. Matching from the last stressed vowel is an elegant definition of a perfect rhyme, and stripping the stress numbers to use the tail as a Map key keeps it fast and simple. But the SCOWL filtering is the decision that makes it actually usable — raw CMU is full of surnames and abbreviations, and sorting by SCOWL level, then syllables, explains why "fire" now opens with higher, prior, wire, acquire, desire instead of noise. I also like that you kept the slant rhymes in a separate tab and removed perfect rhymes from it, so the two lists never compete with each other. One question: CMU lists multiple pronunciations for words like "read" or "live" — do you index every variant, or pick one per word? If you index all of them, does a word ever show up under two different syllable counts?