1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Mila [183]
3 years ago
15

Modify the sentence-generator program of Case Study so that it inputs its vocabulary from a set of text files at startup. The fi

lenames 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()

Computers and Technology
1 answer:
Snezhnost [94]3 years ago
6 0

Answer:

Go to explaination for the program code.

Explanation:

Before running the program you need to have these files created like below in your unix box.

Unix Terminal> cat nouns.txt

BOY

GIRL

BAT

BALL

Unix Terminal> cat articles.txt

A

THE

Unix Terminal> cat verbs.txt

HIT

SAW

LIKED

Unix Terminal> cat prepositions.txt

WITH

BY

Unix Terminal>

Code:

#!/usr/local/bin/python3

import random

def getWords(filename):

fp = open(filename)

temp_list = list()

for each_line in fp:

each_line = each_line.strip()

temp_list.append(each_line)

words = tuple(temp_list)

fp.close()

return words

articles = getWords('articles.txt')

nouns = getWords('nouns.txt')

verbs = getWords('verbs.txt')

prepositions = getWords('prepositions.txt')

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 number of sentences: '))

for count in range(number):

print(sentence())

if __name__=='__main__':

main()

kindly check attachment for code output and onscreen code.

You might be interested in
​What file system below does not support encryption, file based compression, and disk quotas, but does support extremely large v
Elena L [17]

Answer:

D.  ReFS

Explanation:

File system is simply a management system for files that controls how and where data are stored, where they can be located and how data can be accessed. It deals with data storage and retrieval.

Examples of file system are NTFS, FAT(e.g FAT 16 and FAT 32), ReFS.

ReFS, which stands for Resilient File System, is designed primarily to enhance scalability by allowing for the storage of extremely large amounts of data and efficiently manage the availability of the data. It is called "resilient" because it ensures the integrity of data by offering resilience to data corruption. It does not support transaction, encryption, file based compression, page file and disk quotas, to mention a few.

6 0
3 years ago
Keith would like to compare data he has collected from research. The data includes the electrical output
liberstina [14]

Answer: my test says line graph soooo….

Explanation: if its what the test says

4 0
2 years ago
4. What is an example of a Trans receiver? *<br> A. Pager<br> B. Wi-Fi<br> C. Telephone
Charra [1.4K]
I really think it A : pager
I hope this helps !!
8 0
3 years ago
When resizing images or objects in a presentation, why should a user not utilize the sizing handles in the middle of the sides
Shalnov [3]

When resizing an image or an object in a presentation, a user should not utilize the sizing handles in the middle of the sides or the bottom of the image. There's nothing wrong with resizing this way.

There's nothing wrong with resizing this way

<u>Explanation:</u>

When the sizing handles in the middle of the sides are used, the image stretches out reflecting an increase in the width maintaining There's nothing wrong with resizing this way.

On the other hand, while using the sizing handles at the bottom, the image increases in height whilst maintaining the same width again disturbing the aspect ratio.

5 0
3 years ago
Read 2 more answers
What are the values of the digital signal when sending in physical layer?
irina [24]

Answer:

A waveform that switches representing the two states of a Boolean value (0 and 1, or low and high, or false and true) is referred to as a digital signal or logic signal or binary signal when it is interpreted in terms of only two possible digits.

8 0
2 years ago
Other questions:
  • Convert 578.2 into hexadecimal​
    13·1 answer
  • ____ software is rights-protection software used to control the use of a work.
    9·1 answer
  • Fact about energy that will make a knex car move
    7·2 answers
  • Write a class named Accumulator containing: An instance variable named sum of type integer. A constructor that accepts an intege
    14·1 answer
  • The first idea for a communications network was called
    14·2 answers
  • What is the computer that is similar to a destop but smaller in size
    8·1 answer
  • Hello. I have an app which doesn't let me get in. It says: "wrong app info". I'm in a samsung device. How can I solve that probl
    13·2 answers
  • Please write a Java program to read movies.txt file. (It is in Modules under Chapter Code) Create your own movies.ser file from
    14·1 answer
  • Mingji is working to transfer photos from her digital camera to her computer. While
    11·1 answer
  • ANSWER QUICKLY!!!
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!