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
Digital cameras are less susceptible to UV light.Select one of the options below as your answer:A. TrueB. False
horrorfan [7]
<span>Digital cameras are less susceptible to UV light. TRUE. In fact, there is an existing photography called Ultraviolet Photography. It is a process of photography that records images by using the light from the ultraviolet spectrum. There are two possible ways to use UV in taking photographs: via reflected UV and UV induced fluorescence.</span>
4 0
3 years ago
Read 2 more answers
1.Know the BMI of the user. First, the user will input the height in meters and weight in kilograms. After inputting, the inputt
Verizon [17]

The program that checks the BMI of a patient is given below:

<h3>THE CODE</h3>

#include <iomanip>

#include <iostream>

#include <math.h>

using namespace std;

// C++ program to illustrate

// how to calculate BMI

float BMI(float height, float weight)

{

   float bmi = weight / pow(height, 2);

   return bmi;

}

int main()

{

   float height = 1.79832;

   float weight = 70;

   // Function call

  float bmi = BMI(height, weight);

   cout << "The BMI is " << setprecision(15) << bmi

        << " so ";

   // Conditions to find out BMI category

   if (bmi < 18.5)

       cout << "underweight";

   else if (bmi >= 18.5 && bmi < 24.9)

       cout << "Healthy";

   else if (bmi >= 24.9 && bmi < 30)

       cout << "overweight";

   else if (bmi >= 30)

       cout << "Suffering from Obesity";

   return 0;

}

// This code is contributed by aarohirai2616.

<h3>Output: </h3>

The BMI is 21.64532402096181 so Healthy

Read more about algorithms here:

brainly.com/question/24953880
#SPJ1

3 0
1 year ago
your computer running Windows 10 is doing some very strange things with operating system you are fairly certain it is not a hard
AlekseyPX
It might be a virus, when my computer starts going crazy i just bought a fixme stick and they work amazing or trying getting an expert to fix it if it worse
6 0
2 years ago
Read 2 more answers
What command would you use to copy the content of a file named report1 to another called report1uc. Convert the content of repor
Ray Of Light [21]

Answer:

Answered below.

Explanation:

This answer is applicable to Linux.

There are several commands for copying files in Linux. cp and rsync are the most widely used. cp though, is the command used to copy files and their contents.

To copy a file named report1.txt to another file named report1uc.txt in the current directory, the following command is run.

$ cp report1.txt report1uc.txt

You can change the case of the strings in the file by using the tr command.

tr [:lower:] [:upper:]

For example,

$ echo brainly | tr [:lower:]. [:upper:]

Result is BRAINLY.

7 0
3 years ago
Why would a store owner want to calculate a mean
chubhunter [2.5K]

Answer:

a store owner would calculate a mean to see the how much a person spends

Explanation:

4 0
2 years ago
Other questions:
  • The term ________ refers to the use of a single unifying device that handles media, Internet, entertainment, and telephone needs
    7·1 answer
  • When you take a multiple-choice test, you are relying on ________, a means of retrieving information out of your long-term memor
    8·1 answer
  • what is a massive online storage that allows for Access by any internet-connected device running web browser. use for Less priva
    6·1 answer
  • What is the keyboard shortcut to display the merge to printer dialog box?
    5·1 answer
  • Consider a satellite orbiting the earth. Its position above the earth is specified in polar coordinates. Find a model-view matri
    12·1 answer
  • Do you need internet to play xbox one
    14·1 answer
  • If I wished to insert a hyperlink from text in my document to my company website, what type of link would I insert?
    9·1 answer
  • Explain three specific &amp; major security threats to internet users. In each case, address whether or not the vulnerabilities
    15·1 answer
  • Which of the following is not the disadvantage of closed
    7·2 answers
  • ________ is interpreted. Group of answer choices A. Python B. C C. C D. Ada E. Pascal
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!