Skip to content

Instantly share code, notes, and snippets.

@wezleysherman
Created July 31, 2020 05:12
Show Gist options
  • Save wezleysherman/8f3fe3cac7577855ad22fd9e023d15ed to your computer and use it in GitHub Desktop.
Save wezleysherman/8f3fe3cac7577855ad22fd9e023d15ed to your computer and use it in GitHub Desktop.
''' Rhyme density is calculated by taking the number of rhymed syllables and divide it by total number of syllables'''
def calc_rhyme_density(bars):
total_syllables = 0
rhymed_syllables = 0
for bar in bars:
for word in bar.split():
p = pronouncing.phones_for_word(word)
if len(p) == 0:
break
syllables = pronouncing.syllable_count(p[0])
total_syllables += syllables
has_rhyme = False
for rhyme in pronouncing.rhymes(word):
if has_rhyme:
break
for idx, r_bar in enumerate(bars):
if idx > 4:
break
if rhyme in r_bar:
rhymed_syllables += syllables
has_rhyme = True
break
return rhymed_syllables/total_syllables
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment