awacke1 commited on
Commit
5180129
·
verified ·
1 Parent(s): ccb6caf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -1
app.py CHANGED
@@ -2,17 +2,39 @@ import streamlit as st
2
  from random import choice, sample
3
  import pronouncing
4
  import emoji
 
 
 
 
 
 
 
5
 
6
  # Function to add emojis to a line
7
  def add_emojis(line, emojis):
8
  return ' '.join([emoji.emojize(f":{e}:") + word for word, e in zip(line.split(), cycle(emojis))])
9
 
 
 
 
 
 
 
 
 
 
10
  # Function to generate a line of the tongue twister
11
  def generate_line(keyword, humor_words, complexity, emojis):
12
  rhymes = pronouncing.rhymes(keyword)
13
  if not rhymes:
14
  return "No rhymes found."
15
- rhyme_words = sample(rhymes, min(len(rhymes), complexity))
 
 
 
 
 
 
16
  line = f"{keyword.capitalize()} {choice(humor_words)} {' '.join(rhyme_words)},"
17
  return add_emojis(line, emojis)
18
 
 
2
  from random import choice, sample
3
  import pronouncing
4
  import emoji
5
+ import nltk
6
+ from itertools import cycle
7
+ from nltk.corpus import wordnet
8
+
9
+ # Ensure NLTK resources are downloaded
10
+ nltk.download('wordnet')
11
+ nltk.download('averaged_perceptron_tagger')
12
 
13
  # Function to add emojis to a line
14
  def add_emojis(line, emojis):
15
  return ' '.join([emoji.emojize(f":{e}:") + word for word, e in zip(line.split(), cycle(emojis))])
16
 
17
+ # Function to ensure the rhyming words fit grammatically
18
+ def filter_rhymes(rhymes, pos_tag):
19
+ filtered_rhymes = []
20
+ for word in rhymes:
21
+ word_pos = nltk.pos_tag([word])[0][1]
22
+ if word_pos in pos_tag:
23
+ filtered_rhymes.append(word)
24
+ return filtered_rhymes
25
+
26
  # Function to generate a line of the tongue twister
27
  def generate_line(keyword, humor_words, complexity, emojis):
28
  rhymes = pronouncing.rhymes(keyword)
29
  if not rhymes:
30
  return "No rhymes found."
31
+
32
+ # Identify part-of-speech for keyword
33
+ keyword_pos = nltk.pos_tag([keyword])[0][1]
34
+ pos_tags = [wordnet.synsets(word)[0].pos() for word in rhymes if wordnet.synsets(word)]
35
+ filtered_rhymes = filter_rhymes(rhymes, pos_tags)
36
+
37
+ rhyme_words = sample(filtered_rhymes, min(len(filtered_rhymes), complexity))
38
  line = f"{keyword.capitalize()} {choice(humor_words)} {' '.join(rhyme_words)},"
39
  return add_emojis(line, emojis)
40