96.1k views
5 votes
Modify the sentence-generator program of Case Study 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.txt. (Hint: 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 ini- tialize each of the four variables for the vocabulary.)Case Study: Generating Sentences code:import randomarticles = ("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()

User Jeysson
by
4.4k points

2 Answers

3 votes

Final answer:

Modify the sentence-generator program by creating a function called 'getWords' that takes a filename, reads its contents, and returns a tuple. Use this function to initialize vocabulary variables from text files, thereby making the program dynamically use the words provided in the files.

Step-by-step explanation:

To modify the sentence-generator program so that it inputs its vocabulary from text files, we can create a new function called getWords to handle the file reading. The getWords function will take the filename as an argument, after which it reads the file line by line, adds each line to a list and then converts that list to a tuple which it returns. The returned tuple will be used to initialize the vocabulary variables in the program.

Updated Code Snippet:

import random
def getWords(filename):
with open(filename, 'r') as file:
words = [word.strip() for word in file.readlines()]
return tuple(words)
articles = getWords('articles.txt')
nouns = getWords('nouns.txt')
verbs = getWords('verbs.txt')
prepositions = getWords('prepositions.txt')
# ... rest of the program ...
With this modification, the vocabulary of the sentence-generator will be based on the words provided in the corresponding text files, making it dynamic and customizable.
User Alexis Wilke
by
3.6k points
2 votes

Final answer:

To modify the sentence-generator program, create a function named getWords that reads a list of words from a file and converts them to a tuple. The function is then used to initialize the vocabulary variables by reading from external text files.

Step-by-step explanation:

The task is to modify a sentence-generator program to read its vocabulary from external text files rather than having the words hardcoded into the program. This requires creating a new function, called getWords, which takes a filename as its argument. This function should open the corresponding file, read its contents into a list, convert that list to a tuple, and then return the tuple. To utilize this new function, you need to call getWords with each of the filenames ('nouns.txt', 'verbs.txt', 'articles.txt', and 'prepositions.txt') to initialize the respective variables in the program.

Modified Code with getWords Function:


import random

def getWords(filename):
with open(filename, 'r') as file:
words = file.read().splitlines()
return tuple(words)

articles = getWords('articles.txt')
nouns = getWords('nouns.txt')
verbs = getWords('verbs.txt')
prepositions = getWords('prepositions.txt')

# ... (rest of the code remains unchanged)

User Sashi
by
4.3k points