JaeSwift commited on
Commit
b054a29
·
verified ·
1 Parent(s): bad4777

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -26
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import pronouncing
2
  import string
3
- import copy
4
  import gradio as gr
5
 
6
  CONSONANTS = set(string.ascii_uppercase) - set('AEIOU')
@@ -12,52 +12,67 @@ def get_phones(word):
12
  phones = phones_for_word[0].split()
13
  return phones
14
 
15
- def _get_rhyming_tail(phones, syllable_count=2):
16
  vowels = [phone for phone in phones if phone[0] in 'AEIOU']
17
  if len(vowels) < syllable_count:
18
- return None # Not enough syllables for a multisyllabic rhyme
19
  return phones[-syllable_count * 2:]
20
 
21
- def get_multisyllabic_rhymes(phones, syllable_count=2):
 
 
 
22
  rhyming_tail = _get_rhyming_tail(phones, syllable_count)
23
  if not rhyming_tail:
24
  return []
25
- rhyming_tail_str = " ".join(rhyming_tail)
26
- return pronouncing.search(rhyming_tail_str + "$")
27
-
28
- def find_rhymes_for_words(words, syllable_count=2):
29
- # Split words by spaces
30
- words = words.split()
31
- results = {}
32
 
 
 
 
 
 
 
 
 
 
 
33
  for word in words:
34
  phones = get_phones(word)
35
  if phones is None:
36
- results[word] = 'Not recognized'
37
  continue
38
 
39
- multisyllabic_rhymes = get_multisyllabic_rhymes(phones, syllable_count)
40
- results[word] = multisyllabic_rhymes if multisyllabic_rhymes else "No multisyllabic rhymes found"
41
-
42
- # Format results as a string
43
- result_str = ""
44
- for word, rhymes in results.items():
45
- if isinstance(rhymes, list):
46
- result_str += f"{word.capitalize()} ({syllable_count} syllables): " + ", ".join(rhymes) + "\n\n"
47
  else:
48
- result_str += f"{word.capitalize()}: {rhymes}\n\n"
49
-
50
- return result_str
 
 
 
 
 
 
 
 
 
51
 
52
  # Gradio interface
53
  iface = gr.Interface(
54
- fn=lambda words, syllables: find_rhymes_for_words(words, syllables),
55
  inputs=[
56
- gr.Textbox(label="Enter words (space-separated)"),
57
  gr.Slider(1, 3, step=1, label="Number of Syllables")
58
  ],
59
  outputs="text",
60
- description="Enter multiple words separated by spaces to find multisyllabic rhymes."
61
  )
62
 
63
  if __name__ == "__main__":
 
1
  import pronouncing
2
  import string
3
+ import itertools
4
  import gradio as gr
5
 
6
  CONSONANTS = set(string.ascii_uppercase) - set('AEIOU')
 
12
  phones = phones_for_word[0].split()
13
  return phones
14
 
15
+ def _get_rhyming_tail(phones, syllable_count=1):
16
  vowels = [phone for phone in phones if phone[0] in 'AEIOU']
17
  if len(vowels) < syllable_count:
18
+ return None # Not enough syllables for the rhyme
19
  return phones[-syllable_count * 2:]
20
 
21
+ def get_soft_rhymes(phones, syllable_count=1):
22
+ """
23
+ Find words that have a similar ending sound but don't match exactly.
24
+ """
25
  rhyming_tail = _get_rhyming_tail(phones, syllable_count)
26
  if not rhyming_tail:
27
  return []
 
 
 
 
 
 
 
28
 
29
+ # Generate a "soft rhyme" by allowing slight variations in the ending sounds
30
+ search_pattern = " ".join(phone[:-1] + "." for phone in rhyming_tail) # Replace last digit in each phoneme with "."
31
+ return pronouncing.search(search_pattern)
32
+
33
+ def find_rhymes_for_phrase(phrase, syllable_count=1):
34
+ # Split phrase into individual words
35
+ words = phrase.split()
36
+ rhyming_options = []
37
+
38
+ # Get rhymes for each word in the phrase
39
  for word in words:
40
  phones = get_phones(word)
41
  if phones is None:
42
+ rhyming_options.append([f"{word} (Not recognized)"])
43
  continue
44
 
45
+ # Get perfect and soft rhymes
46
+ perfect_rhymes = get_multisyllabic_rhymes(phones, syllable_count)
47
+ soft_rhymes = get_soft_rhymes(phones, syllable_count)
48
+
49
+ # Combine both types of rhymes
50
+ all_rhymes = set(perfect_rhymes + soft_rhymes)
51
+ if not all_rhymes:
52
+ rhyming_options.append([f"{word} (No rhymes found)"])
53
  else:
54
+ rhyming_options.append(list(all_rhymes))
55
+
56
+ # Generate all possible combinations of rhyming words
57
+ combined_results = list(itertools.product(*rhyming_options))
58
+
59
+ # Format combined results as a string
60
+ if combined_results:
61
+ result_str = f"Rhyming combinations for '{phrase}':\n"
62
+ result_str += "\n".join(" ".join(combination) for combination in combined_results)
63
+ return result_str
64
+ else:
65
+ return f"No rhyming combinations found for '{phrase}'"
66
 
67
  # Gradio interface
68
  iface = gr.Interface(
69
+ fn=lambda phrase, syllables: find_rhymes_for_phrase(phrase, syllables),
70
  inputs=[
71
+ gr.Textbox(label="Enter phrase (space-separated words)"),
72
  gr.Slider(1, 3, step=1, label="Number of Syllables")
73
  ],
74
  outputs="text",
75
+ description="Enter a phrase with any number of words to find multisyllabic rhyming combinations and similar sounds."
76
  )
77
 
78
  if __name__ == "__main__":