Question: Python 3 Coding Problem generate_rhyming_lines() generate 2 lines of randomly selectedwords. Each line must be composed of 5 words. The two lines mustrhyme with
Python 3 Coding Problem
generate_rhyming_lines() – generate 2 lines of randomly selectedwords. Each line must be composed of 5 words. The two lines mustrhyme with each other. There are no other requirements. Thefunction should return a list whose elements are the two randomrhyming lines constructed by your code.
Filler:
import nltk
import pronouncing
import random
my_corpus = []
for fid in nltk.corpus.shakespeare.fileids():
my_corpus += nltk.corpus.shakespeare.words(fid)
bigrams = nltk.bigrams(my_corpus)
cfd = nltk.ConditionalFreqDist(bigrams)
def random_word_generator(source = None, num = 1):
result = []
while source == None or notsource[0].isalpha():
source = random.choice(my_corpus)
word = source
result.append(word)
while len(result) < num:
if word in cfd:
init_list =list(cfd[word].keys())
choice_list = [x for x ininit_list if x[0].isalpha()]
if len(choice_list) >0:
newword =random.choice(choice_list)
result.append(newword)
word =newword
else:
word =None
newword =None
else:
newword = None
while newword == None ornot newword[0].isalpha():
newword =random.choice(my_corpus)
result.append(newword)
word = newword
return result
def count_syllables(word):
phones = pronouncing.phones_for_word(word)
count_list = [pronouncing.syllable_count(x) for x inphones]
if len(count_list) > 0:
result = max(count_list)
else:
result = 0
return result
def get_rhymes(word):
result = pronouncing.rhymes(word)
return result
def get_stresses(word):
result = pronouncing.stresses_for_word(word)
return result
Code:
def generate_rhyming_lines():
----------Fill in Code using Python 3----------------------
result1 = generate_rhyming_lines()
print(result1)
print()
Thanks!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
