Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Create a python program that randomly generates a western haiku from text read from a file. The program will generate a haiku that follows three
Create a python program that randomly generates a western haiku from text read from a file. The program will generate a haiku that follows three rules:
each haiku will be three lines total
lines will be either or words in length. Line length will be randomly selected by the program.
the first and third lines will be the same length.
Your program will randomly select or contiguous word phrases from the text provided. Include punctuation. Complete the functions below in order to create a Western Haiku.
Implement a function getText that takes fileName as a parameter. You may assume that the file is not empty. getText will read a file to a String variable and then convert it to a list of strings. Each item in the list will be a word from the file. getText will return a list of all the words. NOTE: do not worry about punctuation. You will need to remove any
characters from the text before splitting it into a list. Create a text file called sample.txt Save several lines of text in this file. The information below shows how you would call the function getText and what it would display:
Implement a function randomPhrase that takes a filename and an integer, length. The function will randomly generates a phrase of consecutive words punctuation intact determined by length. Your function may use the gettext function to obtain the list of strings. Your function will return a string of the length sent as a parameter.
Implement a function getHK which takes a filename as a parameter. This function will generate a random integer of or to determine the length of Lines and If lines and are length three, then line will be length four. If lines and are length four, then line will be length three.
use this code:
import random
import time
def getTextfname:
infile openfnamer
text infile.read
printtext
print
textList text.split
printtextList
infile.close
return textList
def randomPhrasefname length:
wordList getTextfname
spot random.randrange lenwordList
line
#your code here
return line
def getHKfname:
line random.randrange
ifline:
line
else:
line
#your code here
printHK
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started