Answered step by step
Verified Expert Solution
Link Copied!

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 3 or 4 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 3 or 4 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 3 or 4 to determine the length of Lines 1 and 3. If lines 1 and 3 are length three, then line 2 will be length four. If lines 1 and 3 are length four, then line 2 will be length three.
use this code:
import random
import time
def getText(fname):
infile = open(fname,'r')
text = infile.read()
print(text)
print()
textList = text.split()
print(textList)
infile.close()
return textList
def randomPhrase(fname, length):
wordList = getText(fname)
spot = random.randrange(0, len(wordList))
line=""
#your code here
return (line)
def getHK(fname):
line1_3= random.randrange(3,4)
if(line1_3==3):
line2=4
else:
line2=3
#your code here
print(HK)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

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

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions