Update app.py
Browse files
app.py
CHANGED
@@ -9,19 +9,14 @@ def get_phones(word):
|
|
9 |
phones_for_word = pronouncing.phones_for_word(word)
|
10 |
if not phones_for_word:
|
11 |
return None
|
12 |
-
|
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:]
|
25 |
|
26 |
def get_exact_rhymes(phones):
|
27 |
rhyming_tail = _get_rhyming_tail(phones)
|
@@ -30,24 +25,17 @@ def get_exact_rhymes(phones):
|
|
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):
|
@@ -57,31 +45,29 @@ def find_rhymes_for_phrase(phrase):
|
|
57 |
for word in words:
|
58 |
phones = get_phones(word)
|
59 |
if phones is None:
|
60 |
-
rhyming_options.append([f"{word} (Not recognized)"])
|
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
|
81 |
|
82 |
# Function to update the notepad with a selected rhyme
|
83 |
def add_to_notepad(notepad, selected_rhyme):
|
84 |
-
return notepad + "
|
85 |
|
86 |
# Gradio Interface
|
87 |
with gr.Blocks() as demo:
|
@@ -97,10 +83,10 @@ with gr.Blocks() as demo:
|
|
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()
|
|
|
9 |
phones_for_word = pronouncing.phones_for_word(word)
|
10 |
if not phones_for_word:
|
11 |
return None
|
12 |
+
return phones_for_word[0].split()
|
|
|
13 |
|
14 |
def _get_rhyming_tail(phones):
|
|
|
|
|
|
|
|
|
15 |
vowels = [phone for phone in phones if phone[0] in 'AEIOU']
|
16 |
syllable_count = len(vowels)
|
17 |
if syllable_count < 1:
|
18 |
return None
|
19 |
+
return phones[-syllable_count * 2:]
|
20 |
|
21 |
def get_exact_rhymes(phones):
|
22 |
rhyming_tail = _get_rhyming_tail(phones)
|
|
|
25 |
|
26 |
rhyming_tail_str = " ".join(rhyming_tail)
|
27 |
matches = pronouncing.search(rhyming_tail_str + "$")
|
|
|
28 |
exact_rhymes = [match for match in matches if rhyming_tail == _get_rhyming_tail(get_phones(match))]
|
29 |
return exact_rhymes
|
30 |
|
31 |
def get_filtered_loose_rhymes(phones):
|
|
|
|
|
|
|
|
|
32 |
rhyming_tail = _get_rhyming_tail(phones)
|
33 |
if not rhyming_tail:
|
34 |
return []
|
35 |
|
36 |
search_pattern = " ".join(phone[:-1] + "." for phone in rhyming_tail)
|
37 |
matches = pronouncing.search(search_pattern)
|
38 |
+
filtered_rhymes = [match for match in matches if get_phones(match)]
|
|
|
|
|
39 |
return filtered_rhymes
|
40 |
|
41 |
def find_rhymes_for_phrase(phrase):
|
|
|
45 |
for word in words:
|
46 |
phones = get_phones(word)
|
47 |
if phones is None:
|
48 |
+
rhyming_options.append([[f"{word} (Not recognized)"]])
|
49 |
continue
|
50 |
|
|
|
51 |
exact_rhymes = get_exact_rhymes(phones)
|
52 |
|
|
|
53 |
if exact_rhymes:
|
54 |
+
rhyming_options.append([[rhyme] for rhyme in exact_rhymes])
|
55 |
else:
|
56 |
loose_rhymes = get_filtered_loose_rhymes(phones)
|
57 |
if loose_rhymes:
|
58 |
+
rhyming_options.append([[rhyme] for rhyme in loose_rhymes])
|
59 |
else:
|
60 |
+
rhyming_options.append([[f"{word} (No rhymes found)"]])
|
61 |
|
62 |
combined_results = list(itertools.product(*rhyming_options))
|
63 |
unique_results = set(" ".join(combination) for combination in combined_results)
|
64 |
|
65 |
+
# Return a list of unique rhyming combinations, each wrapped in a list for DataFrame compatibility
|
66 |
+
return [[rhyme] for rhyme in unique_results]
|
67 |
|
68 |
# Function to update the notepad with a selected rhyme
|
69 |
def add_to_notepad(notepad, selected_rhyme):
|
70 |
+
return notepad + "\n" + selected_rhyme
|
71 |
|
72 |
# Gradio Interface
|
73 |
with gr.Blocks() as demo:
|
|
|
83 |
# Button for generating rhymes
|
84 |
generate_btn = gr.Button("Generate Rhymes")
|
85 |
|
|
|
|
|
|
|
86 |
# Display rhyming results and update notepad on button click
|
87 |
generate_btn.click(find_rhymes_for_phrase, inputs=[phrase_input], outputs=rhyme_output)
|
88 |
+
|
89 |
+
# Functionality to select a rhyme and add it to the notepad
|
90 |
+
rhyme_output.select(add_to_notepad, inputs=[notepad, rhyme_output], outputs=notepad)
|
91 |
|
92 |
demo.launch()
|