Update app.py
Browse files
app.py
CHANGED
@@ -12,37 +12,45 @@ def get_phones(word):
|
|
12 |
phones = phones_for_word[0].split()
|
13 |
return phones
|
14 |
|
15 |
-
def _get_rhyming_tail(phones
|
|
|
|
|
|
|
|
|
16 |
vowels = [phone for phone in phones if phone[0] in 'AEIOU']
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
20 |
|
21 |
-
def get_exact_rhymes(phones
|
22 |
-
rhyming_tail = _get_rhyming_tail(phones
|
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)
|
30 |
return exact_rhymes
|
31 |
|
32 |
-
def
|
33 |
"""
|
34 |
Fallback function to find words with a similar ending sound by allowing slight variations.
|
|
|
35 |
"""
|
36 |
-
rhyming_tail = _get_rhyming_tail(phones
|
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 |
-
|
|
|
|
|
|
|
|
|
46 |
words = phrase.split()
|
47 |
rhyming_options = []
|
48 |
|
@@ -53,42 +61,46 @@ def find_rhymes_for_phrase(phrase, syllable_count=1):
|
|
53 |
continue
|
54 |
|
55 |
# Try to find exact rhymes first
|
56 |
-
exact_rhymes = get_exact_rhymes(phones
|
57 |
|
58 |
-
# If no exact rhymes are found, fallback to
|
59 |
if exact_rhymes:
|
60 |
rhyming_options.append(exact_rhymes)
|
61 |
else:
|
62 |
-
loose_rhymes =
|
63 |
if loose_rhymes:
|
64 |
rhyming_options.append(loose_rhymes)
|
65 |
else:
|
66 |
rhyming_options.append([f"{word} (No rhymes found)"])
|
67 |
|
68 |
-
# Generate all possible combinations of rhyming words
|
69 |
combined_results = list(itertools.product(*rhyming_options))
|
70 |
-
|
71 |
-
# Remove duplicates
|
72 |
unique_results = set(" ".join(combination) for combination in combined_results)
|
73 |
|
74 |
-
#
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
return f"No rhyming combinations found for '{phrase}'"
|
81 |
|
82 |
-
# Gradio
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
-
|
94 |
-
iface.launch()
|
|
|
12 |
phones = phones_for_word[0].split()
|
13 |
return phones
|
14 |
|
15 |
+
def _get_rhyming_tail(phones):
|
16 |
+
"""
|
17 |
+
Automatically detect the syllable count based on vowel sounds,
|
18 |
+
then return the appropriate rhyming tail.
|
19 |
+
"""
|
20 |
vowels = [phone for phone in phones if phone[0] in 'AEIOU']
|
21 |
+
syllable_count = len(vowels)
|
22 |
+
if syllable_count < 1:
|
23 |
+
return None
|
24 |
+
return phones[-syllable_count * 2:] # Use the detected syllable count to get rhyming tail
|
25 |
|
26 |
+
def get_exact_rhymes(phones):
|
27 |
+
rhyming_tail = _get_rhyming_tail(phones)
|
28 |
if not rhyming_tail:
|
29 |
return []
|
30 |
|
31 |
rhyming_tail_str = " ".join(rhyming_tail)
|
32 |
matches = pronouncing.search(rhyming_tail_str + "$")
|
33 |
|
34 |
+
exact_rhymes = [match for match in matches if rhyming_tail == _get_rhyming_tail(get_phones(match))]
|
35 |
return exact_rhymes
|
36 |
|
37 |
+
def get_filtered_loose_rhymes(phones):
|
38 |
"""
|
39 |
Fallback function to find words with a similar ending sound by allowing slight variations.
|
40 |
+
Applies additional filtering to avoid unrelated results.
|
41 |
"""
|
42 |
+
rhyming_tail = _get_rhyming_tail(phones)
|
43 |
if not rhyming_tail:
|
44 |
return []
|
45 |
|
|
|
46 |
search_pattern = " ".join(phone[:-1] + "." for phone in rhyming_tail)
|
47 |
matches = pronouncing.search(search_pattern)
|
|
|
48 |
|
49 |
+
# Filter matches to ensure they closely resemble the original word's rhyme structure
|
50 |
+
filtered_rhymes = [match for match in matches if match != phones]
|
51 |
+
return filtered_rhymes
|
52 |
+
|
53 |
+
def find_rhymes_for_phrase(phrase):
|
54 |
words = phrase.split()
|
55 |
rhyming_options = []
|
56 |
|
|
|
61 |
continue
|
62 |
|
63 |
# Try to find exact rhymes first
|
64 |
+
exact_rhymes = get_exact_rhymes(phones)
|
65 |
|
66 |
+
# If no exact rhymes are found, fallback to filtered loose rhymes
|
67 |
if exact_rhymes:
|
68 |
rhyming_options.append(exact_rhymes)
|
69 |
else:
|
70 |
+
loose_rhymes = get_filtered_loose_rhymes(phones)
|
71 |
if loose_rhymes:
|
72 |
rhyming_options.append(loose_rhymes)
|
73 |
else:
|
74 |
rhyming_options.append([f"{word} (No rhymes found)"])
|
75 |
|
|
|
76 |
combined_results = list(itertools.product(*rhyming_options))
|
|
|
|
|
77 |
unique_results = set(" ".join(combination) for combination in combined_results)
|
78 |
|
79 |
+
# Return a list of unique rhyming combinations
|
80 |
+
return list(unique_results)
|
81 |
+
|
82 |
+
# Function to update the notepad with a selected rhyme
|
83 |
+
def add_to_notepad(notepad, selected_rhyme):
|
84 |
+
return notepad + " " + selected_rhyme
|
|
|
85 |
|
86 |
+
# Gradio Interface
|
87 |
+
with gr.Blocks() as demo:
|
88 |
+
gr.Markdown("## Interactive Rhyming Generator with Notepad")
|
89 |
+
|
90 |
+
# Inputs for the rhyming generator
|
91 |
+
phrase_input = gr.Textbox(label="Enter phrase (space-separated words)")
|
92 |
+
|
93 |
+
# Output areas
|
94 |
+
rhyme_output = gr.Dataframe(headers=["Rhyming Phrases"], interactive=False)
|
95 |
+
notepad = gr.Textbox(label="Notepad", lines=10, placeholder="Write your lyrics here...")
|
96 |
+
|
97 |
+
# Button for generating rhymes
|
98 |
+
generate_btn = gr.Button("Generate Rhymes")
|
99 |
+
|
100 |
+
# Functionality to select a rhyme and add it to the notepad
|
101 |
+
rhyme_output.change(add_to_notepad, inputs=[notepad, rhyme_output], outputs=notepad)
|
102 |
+
|
103 |
+
# Display rhyming results and update notepad on button click
|
104 |
+
generate_btn.click(find_rhymes_for_phrase, inputs=[phrase_input], outputs=rhyme_output)
|
105 |
|
106 |
+
demo.launch()
|
|