Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make rhymes return rhymes for all pronunciations #46

Merged
merged 7 commits into from Sep 5, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions pronouncing/__init__.py
@@ -1,4 +1,5 @@
from __future__ import print_function
from itertools import chain
import re
from pkg_resources import resource_stream
import collections
Expand Down Expand Up @@ -217,8 +218,13 @@ def rhymes(word):
:returns: a list of rhyming words
"""
phones = phones_for_word(word)
if len(phones) > 0:
return [w for w in rhyme_lookup.get(rhyming_part(phones[0]), [])
if w != word]
combined_rhymes = []
if phones:
for element in phones:
combined_rhymes.append([w for w in rhyme_lookup.get(rhyming_part(
element), []) if w != word])
combined_rhymes = list(chain.from_iterable(combined_rhymes))
unique_combined_rhymes = sorted(set(combined_rhymes))
return unique_combined_rhymes
else:
return []
10 changes: 10 additions & 0 deletions tests/test_pronouncing.py
Expand Up @@ -65,6 +65,16 @@ def test_rhymes(self):
'obliquely', 'steakley', 'szekely', 'uniquely', 'weakley',
'weakly', 'weekley', 'weekly', 'yeakley']
self.assertEqual(expected, rhymes)
# ensure correct behavior for words that have multiple pronunciations
rhymes = pronouncing.rhymes("dove")
expected = [
"above", "belove", "boeve", "bove", "clove", "cove",
"deneuve", "drove", "glove", "gov", "gove", "grove",
"hove", "labauve", "labov", "labove", "love", "nov",
"o'glove", "of", "rove", "shove", "soave", "stove",
"strove", "thereof", "throve", "tov", "trove", "vanhove",
"wove"]
self.assertEqual(expected, rhymes)
hugovk marked this conversation as resolved.
Show resolved Hide resolved
# ensure correct behavior for words that don't rhyme
rhymes = pronouncing.rhymes("orange")
self.assertEqual([], rhymes)
Expand Down