Question: Modify the sentence - generator program of Case Study 5 - 1 ( in the file generator.py ) so that it inputs its vocabulary from
Modify the sentencegenerator program of Case Study in the file generator.py so that it inputs its vocabulary from a set of text files at startup. The filenames are nouns.txt verbs.txt articles.txt and prepositions.txtHint: Define a single new function, getWords. This function should expect a filename as an argument. The function should open an input file with this name, define a temporary list, read words from the file, and add them to the list. The function should then convert the list to a tuple and return this tuple. Call the function with an actual filename to initialize each of the four variables for the vocabulary.LO:
Please modify the program below:
# stats.py
import random def getwordsfilename:
Get words from a text file and return them as a tuple.
try:
with openfilenamer as file:
words wordstrip for word in file.readlines
return tuplewords
except FileNotFoundError:
printfFile filename not found."
return
def generatesentencenouns verbs, articles, prepositions:
Generate a random sentence based on the given vocabulary.
sentence join
random.choicearticles
random.choicenouns
random.choiceverbs
random.choicearticles
random.choicenouns
random.choiceprepositions
random.choicearticles
random.choicenouns
return sentence.capitalize
def main:
Main function to generate a random sentence.
# Get words from text files
nouns getwordsnounstxt
verbs getwordsverbstxt
articles getwordsarticlestxt
prepositions getwordsprepositionstxt
# Generate and print a random sentence
if not allnouns verbs, articles, prepositions:
printError: Unable to generate sentence. Missing vocabulary."
else:
sentence generatesentencenouns verbs, articles, prepositions
printRandom Sentence:", sentence
if namemain:
main
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
