JaeSwift commited on
Commit
6b49f37
·
verified ·
1 Parent(s): fa2e050

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -18
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 get_multisyllabic_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
- return pronouncing.search(rhyming_tail_str + "$")
 
 
 
28
 
29
- def get_soft_rhymes(phones, syllable_count=1):
 
 
 
30
  rhyming_tail = _get_rhyming_tail(phones, syllable_count)
31
  if not rhyming_tail:
32
  return []
33
 
34
- # Generate a "soft rhyme" by allowing slight variations in the ending sounds
35
- search_pattern = " ".join(phone[:-1] + "." for phone in rhyming_tail) # Replace last digit in each phoneme with "."
36
- return pronouncing.search(search_pattern)
 
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
- # Get perfect and soft rhymes
51
- perfect_rhymes = get_multisyllabic_rhymes(phones, syllable_count)
52
- soft_rhymes = get_soft_rhymes(phones, syllable_count)
53
 
54
- # Combine both types of rhymes and add to options
55
- all_rhymes = set(perfect_rhymes + soft_rhymes)
56
- if not all_rhymes:
57
- rhyming_options.append([f"{word} (No rhymes found)"])
58
  else:
59
- rhyming_options.append(list(all_rhymes))
 
 
 
 
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)