Menu

[Solved]Python 3 Modify Sentence Generator Program Case Study Inputs Vocabulary Set Text Files Sta Q37058005

Python 3

Modify the sentence-generator program of Case Study sothat it inputs its vocabulary from a set of text files at startup.The filenames are nouns.txt, verbs. txt, articles.txt, andprepositions.txt.

(HINT: Define a single new function, getWords.This function should expect a filename as an argument. The functionshould open an input file with this name, define a temporary list,read words from the file, and add them to the list. The functionshould then convert the list to a tuple and return this tuple. Callthe function with an actual filename to initialize each of the fourvariables for the vocabulary.)

Case Study: Generating Sentences code:

import random
articles = (“A”, “THE”)
nouns = (“BOY”, “GIRL”, “BAT”, “BALL”)
verbs = (“HIT”, “SAW”, “LIKED”)
prepositions = (“WITH”, “BY”)

def sentence():
return nounPhrase() + ” ” + verbPhrase()
def nounPhrase():
return random.choice(articles) + ” ” + random.choice(nouns)
def verbPhrase():
return random.choice(verbs) + ” ” + nounPhrase() + ” ” +
prepositionalPhrase()
def prepositionalPhrase():
return random.choice(prepositions) + ” ” + nounPhrase()
def main():
number = int(input(“Enter the number of sentences: “))
for count in range(number):
print(sentence())
if __name__ == “__main__”:
main()

Expert Answer


Code:

#!/usr/local/bin/python3

import random

def getWords(filename): . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *