Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

if name main #test () #stress test() print () result1 = generate rhyming lines () print (resultl) print () result2 = generate 10 syllable lines

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
if name main #test () #stress test() print () result1 = generate rhyming lines () print (resultl) print () result2 = generate 10 syllable lines () print (result2) print () result3 = generate metered line () print (result3) print () my poem = generate poem () print (my poem)import nitk import pronouncing import random # This uses the King James Bible as the corpus # Use: nitk. corpus . gutenberg . fileids () to see which other gutenberg works are available. #my_corpus = nitk. corpus . gutenberg . words ( 'bible-kjv. txt") # This uses all the words in the entire gutenberg corpus #my_corpus = nitk. corpus . gutenberg. words () # This loop constructs a corpus from all the shakespeare plays included in the # shakespeare corpus included in NLTK # Use: nitk. corpus . shakespeare . fileids () # to see which shakespeare works are included. my_corpus = for fid in nitk. corpus . shakespeare . fileids () : my_corpus += nitk. corpus . shakespeare . words (fid) bigrams = nitk. bigrams (my_corpus) ofd = nitk. ConditionalFreqDist (bigrams) # This function takes two inputs: source - a word represented as a string (defaults to None, in which case a random word will be selected from the corpus) num - an integer (how many words do you want) The function will generate num random related words using the CFD based on the bigrams in our corpus, starting from # source. So, the first word will be generated from the CFD # using source as the key, the second word will be generated # using the first word as the key, and so on. If the CFD list of a word is empty, then a random word is # chosen from the entire corpus. # The source word is always included as the first word in the result and is included in the count. # The function returns a num-length list of words. def random word_generator (source = None, num = 1) : result = while source == None or not source [0] . isalpha () : source = random. choice (my_corpus) word = source result . append (word) while len (result) 0: newword = random. choice (choice_list) result . append (newword) word = newword else : word = None newword = Noneelif word == ""; pass else: print (cfd [word] . keys (), ofd [word] . values () ) print () print ("Random 5 words following", word) print (random_word_generator (word, 5) ) print ( ) print ( "Pronunciations of", word) print (pronouncing . phones_for_word (word) ) print () print ( "Syllables in", word) print (count_syllables (word) ) print () print ( "Rhymes for", word) print (get_rhymes (word) ) print () print ("Stresses for", word) print (get_stresses (word) ) print () # Runs all the functions many times with random-ish inputs to increase # confidence that they perform as expected def stress_test () : for i in range (10000) : wl = random_word_generator (None, 10) print (wl) w1 = random_word_generator (None, 10000) for w in wl : sc = count syllables (w) print (w, sc) wl = random_word_generator (None, 10000) for w in wl: rs = get rhymes (w) print (w, len (rs) ) print (rs) wl = random_word_generator (None, 10000) for w in wl: st1 = get_stresses (w) print (w, len (stl) ) print (st1)Poetry is one of the most intricate activities that humans perform. It relies heavily on our facility with both syntax (the form] and semantics (the content or meaning) of language in order to work. Think about how a poem is eonstructed. You must choose each word with attention both to its form-how it looks, how it sounds-- and to its meaning, as well as its relation to the other words in your poem. Is it possible for a computer to perform this same activity and produce htunan-looking results? In this project, you will implement a poetry generator that uses the language analysis and selection tools we have discussed in class to try to mimic human construction of poems. This will require the random generation of words as well as the use of algorithms to analyze the structure of those words to detect and use features such as rhymes, syllables, and stresses. Your implementation should use these features of words to construct rules that will be applied to word choice. Words will be randomly selected, but those selections will be eonstrained by the rules you impose regarding the structure of your poem. This is a fUn eombination of creativity and rigor as you atternpt to capture and express the ineffable-seeming methods that poets use to create their works. Setup: 1. Install the NLTK Python library: Windows: py 3 m pip install nltk user Mac: python3 m pip install nltk u5er 2. Download NLTK corpora (plural of corpus) and other les. a. Open the Python interpreter (e.g., start Idle in interactive {shell} mode}. Then, from inside the interpreter eXeeute the following Python statements. 1:. >3>> import nltk c. Ba} nltk . download {I This will launch a new dialog window listing all the NLTK related packages that can be downloaded. For this project you should select "all-eorpora", so you can have aooess to all the word sourees. We won*t be using any of the other packages available, but you may want to explore them on your own. word newword else: word = None newword = None else: newword = None while newword == None o: not newword[D].isalpha[J: newword = random.choice{my_corpus) result.append[newword) word = newword return result This function takes a single input: word a string representing a word The function returns the number of syllables in word as an integer. If the return value is 0, then word is not available in the EMU dictionary. def count_syllables{word]: phones = pronouncing.phones_for_word{word} count_list = [pronouncing.syllable_count(x) for x in phones] if len[count_list) > 0: result = max[count_list] else: result = 0 return result * This function takes a single input: word a string representing a word The function returns a list of words that rhyme with the input word. def get_rhymes{word]: result = pronouncing.rhymes{word) return result * This function takes a single input: word a string representing a word The function returns a list of strings. Each string in the list is a sequence of numbers. Each number corresponds to a syllable in the word and describes the stress placed on that syllable when the word is pronounced. A '1' indicates primary stress on the syllable A '2' indicates secondary stress on the syllable A '0' indicates the syllable is unstressed. Each element of the list indicates a different way to pronounce the input word. def get_stIesses{word]: result = pronouncing.stresses_for_word[word} :etu:n result # A test function that demonstrates how each of the helper functions included # in this file work. You supply a word and it will run each of the above # functions on that word. def test[): keep_going = T:ue while keepLgoing: word = input{"Please enter a word (Enter '0' to quit}: "J if word = '0': keep going = False ######################################################## # # # # # # # ## STUDENT SECTION ## # # ############################################## ## # # # ## # # # # # # generate_rhyming_line () # Complete this function so that it returns a list. The list # must contain two strings of 5 words each. Each string # corresponds to a line. The two lines you return must rhyme. def generate_rhyming_lines ( ) : return -1 # generate_10_syllable_lines () # Complete this function so that it returns a list. The list # must contain two strings of 10 syllables each. Each string # corresponds to a line and each line must be composed of words # whose number of syllables add up to 10 syllables total. def generate_10_syllable_lines () : return -1 generate_metered_line () # Complete this function so that it returns a string. This string # will be composed of randomly selected words, will contain 10 # syllables, and the rhythm of the line must match the following # pattern of stresses: 0101010101 def generate_metered_line () : return -1 # generate_line () Use this function to generate each line of your poem. # This is where you will implement the rules that govern # the construction of each line. # For example: -number of words or syllables in line stress pattern for line (meter) # -last word choice constrained by rhyming pattern # Add any parameters to this function you need to bring in # information about how a particular line should be constructed. def generate_line () : return -1 generate_poem ( ) # Use this function to construct your poem, line by line. # This is where you will implement the rules that govern the structure of your poem. # For example : # -The total number of lines # -How the lines relate to each other (rhyming, syllable counts, etc) def generate_poem ( ) : return -1 if name_ " main ": #test ( ) #stress_test () print ( ) resultl = generate rhyming lines ()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

Explain the difference between data warehouse and data mining.

Answered: 1 week ago