File size: 2,412 Bytes
446ed41
 
 
 
5180129
 
 
 
 
 
 
446ed41
 
 
 
 
5180129
 
 
 
 
 
 
 
 
446ed41
 
 
 
 
5180129
 
 
 
 
 
 
446ed41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from random import choice, sample
import pronouncing
import emoji
import nltk
from itertools import cycle
from nltk.corpus import wordnet

# Ensure NLTK resources are downloaded
nltk.download('wordnet')
nltk.download('averaged_perceptron_tagger')

# Function to add emojis to a line
def add_emojis(line, emojis):
    return ' '.join([emoji.emojize(f":{e}:") + word for word, e in zip(line.split(), cycle(emojis))])

# Function to ensure the rhyming words fit grammatically
def filter_rhymes(rhymes, pos_tag):
    filtered_rhymes = []
    for word in rhymes:
        word_pos = nltk.pos_tag([word])[0][1]
        if word_pos in pos_tag:
            filtered_rhymes.append(word)
    return filtered_rhymes

# Function to generate a line of the tongue twister
def generate_line(keyword, humor_words, complexity, emojis):
    rhymes = pronouncing.rhymes(keyword)
    if not rhymes:
        return "No rhymes found."
    
    # Identify part-of-speech for keyword
    keyword_pos = nltk.pos_tag([keyword])[0][1]
    pos_tags = [wordnet.synsets(word)[0].pos() for word in rhymes if wordnet.synsets(word)]
    filtered_rhymes = filter_rhymes(rhymes, pos_tags)
    
    rhyme_words = sample(filtered_rhymes, min(len(filtered_rhymes), complexity))
    line = f"{keyword.capitalize()} {choice(humor_words)} {' '.join(rhyme_words)},"
    return add_emojis(line, emojis)

# Function to generate a tongue twister
def generate_tongue_twister(complexity):
    keywords = ['duck', 'light', 'night', 'spike', 'fight', 'kite']
    humor_words = ['jumps over', 'shines bright', 'takes flight', 'ignites', 'bites tight', 'flies a kite']
    emoji_themes = [['duck', 'star', 'moon', 'zap', 'boxing_glove', 'kite']]
    twister = []
    for _ in range(4):
        for keyword, emoji_theme in zip(keywords, emoji_themes):
            twister.append(generate_line(keyword, humor_words, complexity, emoji_theme))
    return ' '.join(twister)

# Streamlit UI
st.title("🌟 The Ultimate Emoji-Rich Tongue Twister Generator 🌟")

complexity = st.slider("Select the complexity of the tongue twister", 1, 5, 2)

if st.button('Generate Tongue Twister'):
    twister = generate_tongue_twister(complexity)
    st.markdown(f"**Tongue Twister:**\n\n> {twister}")

st.markdown("""
*Note: This app generates humorous, emoji-rich, and rhyming tongue twisters. Adjust the complexity to change the length and challenge.*
""")