awacke1's picture
Update app.py
5180129 verified
raw
history blame
2.41 kB
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.*
""")