Update app.py
Browse files
app.py
CHANGED
@@ -18,50 +18,55 @@ def _get_rhyming_tail(phones, syllable_count=1):
|
|
18 |
return None # Not enough syllables for the rhyme
|
19 |
return phones[-syllable_count * 2:]
|
20 |
|
21 |
-
def
|
22 |
rhyming_tail = _get_rhyming_tail(phones, syllable_count)
|
23 |
if not rhyming_tail:
|
24 |
return []
|
25 |
|
26 |
rhyming_tail_str = " ".join(rhyming_tail)
|
27 |
-
|
|
|
|
|
|
|
28 |
|
29 |
-
def
|
|
|
|
|
|
|
30 |
rhyming_tail = _get_rhyming_tail(phones, syllable_count)
|
31 |
if not rhyming_tail:
|
32 |
return []
|
33 |
|
34 |
-
#
|
35 |
-
search_pattern = " ".join(phone[:-1] + "." for phone in rhyming_tail)
|
36 |
-
|
|
|
37 |
|
38 |
def find_rhymes_for_phrase(phrase, syllable_count=1):
|
39 |
-
# Split phrase into individual words
|
40 |
words = phrase.split()
|
41 |
rhyming_options = []
|
42 |
|
43 |
-
# Get rhymes for each word in the phrase
|
44 |
for word in words:
|
45 |
phones = get_phones(word)
|
46 |
if phones is None:
|
47 |
rhyming_options.append([f"{word} (Not recognized)"])
|
48 |
continue
|
49 |
|
50 |
-
#
|
51 |
-
|
52 |
-
soft_rhymes = get_soft_rhymes(phones, syllable_count)
|
53 |
|
54 |
-
#
|
55 |
-
|
56 |
-
|
57 |
-
rhyming_options.append([f"{word} (No rhymes found)"])
|
58 |
else:
|
59 |
-
|
|
|
|
|
|
|
|
|
60 |
|
61 |
-
# Generate all possible combinations of rhyming words
|
62 |
combined_results = list(itertools.product(*rhyming_options))
|
63 |
|
64 |
-
# Format combined results as rhyming lines
|
65 |
if combined_results:
|
66 |
result_str = f"Rhyming combinations for '{phrase}':\n"
|
67 |
result_str += "\n".join(" ".join(combination) for combination in combined_results)
|
|
|
18 |
return None # Not enough syllables for the rhyme
|
19 |
return phones[-syllable_count * 2:]
|
20 |
|
21 |
+
def get_exact_rhymes(phones, syllable_count=1):
|
22 |
rhyming_tail = _get_rhyming_tail(phones, syllable_count)
|
23 |
if not rhyming_tail:
|
24 |
return []
|
25 |
|
26 |
rhyming_tail_str = " ".join(rhyming_tail)
|
27 |
+
matches = pronouncing.search(rhyming_tail_str + "$")
|
28 |
+
|
29 |
+
exact_rhymes = [match for match in matches if rhyming_tail == _get_rhyming_tail(get_phones(match), syllable_count)]
|
30 |
+
return exact_rhymes
|
31 |
|
32 |
+
def get_loose_rhymes(phones, syllable_count=1):
|
33 |
+
"""
|
34 |
+
Fallback function to find words with a similar ending sound by allowing slight variations.
|
35 |
+
"""
|
36 |
rhyming_tail = _get_rhyming_tail(phones, syllable_count)
|
37 |
if not rhyming_tail:
|
38 |
return []
|
39 |
|
40 |
+
# Allow for near matches by replacing the last character in each phoneme with a wildcard
|
41 |
+
search_pattern = " ".join(phone[:-1] + "." for phone in rhyming_tail)
|
42 |
+
matches = pronouncing.search(search_pattern)
|
43 |
+
return matches
|
44 |
|
45 |
def find_rhymes_for_phrase(phrase, syllable_count=1):
|
|
|
46 |
words = phrase.split()
|
47 |
rhyming_options = []
|
48 |
|
|
|
49 |
for word in words:
|
50 |
phones = get_phones(word)
|
51 |
if phones is None:
|
52 |
rhyming_options.append([f"{word} (Not recognized)"])
|
53 |
continue
|
54 |
|
55 |
+
# Try to find exact rhymes first
|
56 |
+
exact_rhymes = get_exact_rhymes(phones, syllable_count)
|
|
|
57 |
|
58 |
+
# If no exact rhymes are found, fallback to looser rhymes
|
59 |
+
if exact_rhymes:
|
60 |
+
rhyming_options.append(exact_rhymes)
|
|
|
61 |
else:
|
62 |
+
loose_rhymes = get_loose_rhymes(phones, syllable_count)
|
63 |
+
if loose_rhymes:
|
64 |
+
rhyming_options.append(loose_rhymes)
|
65 |
+
else:
|
66 |
+
rhyming_options.append([f"{word} (No rhymes found)"])
|
67 |
|
|
|
68 |
combined_results = list(itertools.product(*rhyming_options))
|
69 |
|
|
|
70 |
if combined_results:
|
71 |
result_str = f"Rhyming combinations for '{phrase}':\n"
|
72 |
result_str += "\n".join(" ".join(combination) for combination in combined_results)
|