File size: 2,978 Bytes
e0171fd
 
b054a29
24d2316
e0171fd
24d2316
e0171fd
 
 
 
 
 
 
 
b054a29
c14d215
 
b054a29
c14d215
 
fa2e050
 
 
 
 
 
 
 
b054a29
c14d215
 
 
24d2316
b054a29
 
 
 
 
 
 
 
 
 
c14d215
 
 
b054a29
c14d215
 
b054a29
 
 
 
fa2e050
b054a29
 
 
c14d215
b054a29
 
 
 
 
fa2e050
b054a29
 
 
 
 
 
e0171fd
24d2316
c14d215
b054a29
c14d215
b054a29
bad4777
c14d215
 
b054a29
c14d215
e0171fd
24d2316
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import pronouncing
import string
import itertools
import gradio as gr

CONSONANTS = set(string.ascii_uppercase) - set('AEIOU')

def get_phones(word):
    phones_for_word = pronouncing.phones_for_word(word)
    if not phones_for_word:
        return None
    phones = phones_for_word[0].split()
    return phones

def _get_rhyming_tail(phones, syllable_count=1):
    vowels = [phone for phone in phones if phone[0] in 'AEIOU']
    if len(vowels) < syllable_count:
        return None  # Not enough syllables for the rhyme
    return phones[-syllable_count * 2:]

def get_multisyllabic_rhymes(phones, syllable_count=1):
    rhyming_tail = _get_rhyming_tail(phones, syllable_count)
    if not rhyming_tail:
        return []
    
    rhyming_tail_str = " ".join(rhyming_tail)
    return pronouncing.search(rhyming_tail_str + "$")

def get_soft_rhymes(phones, syllable_count=1):
    rhyming_tail = _get_rhyming_tail(phones, syllable_count)
    if not rhyming_tail:
        return []
    
    # Generate a "soft rhyme" by allowing slight variations in the ending sounds
    search_pattern = " ".join(phone[:-1] + "." for phone in rhyming_tail)  # Replace last digit in each phoneme with "."
    return pronouncing.search(search_pattern)

def find_rhymes_for_phrase(phrase, syllable_count=1):
    # Split phrase into individual words
    words = phrase.split()
    rhyming_options = []

    # Get rhymes for each word in the phrase
    for word in words:
        phones = get_phones(word)
        if phones is None:
            rhyming_options.append([f"{word} (Not recognized)"])
            continue
        
        # Get perfect and soft rhymes
        perfect_rhymes = get_multisyllabic_rhymes(phones, syllable_count)
        soft_rhymes = get_soft_rhymes(phones, syllable_count)
        
        # Combine both types of rhymes and add to options
        all_rhymes = set(perfect_rhymes + soft_rhymes)
        if not all_rhymes:
            rhyming_options.append([f"{word} (No rhymes found)"])
        else:
            rhyming_options.append(list(all_rhymes))
    
    # Generate all possible combinations of rhyming words
    combined_results = list(itertools.product(*rhyming_options))
    
    # Format combined results as rhyming lines
    if combined_results:
        result_str = f"Rhyming combinations for '{phrase}':\n"
        result_str += "\n".join(" ".join(combination) for combination in combined_results)
        return result_str
    else:
        return f"No rhyming combinations found for '{phrase}'"

# Gradio interface
iface = gr.Interface(
    fn=lambda phrase, syllables: find_rhymes_for_phrase(phrase, syllables), 
    inputs=[
        gr.Textbox(label="Enter phrase (space-separated words)"), 
        gr.Slider(1, 3, step=1, label="Number of Syllables")
    ], 
    outputs="text", 
    description="Enter a phrase with any number of words to find multisyllabic rhyming combinations and similar sounds."
)

if __name__ == "__main__":
    iface.launch()