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
Which element of the security policy framework requires approval from upper management and applies to the entire organization?A.
Sav [38]

Answer: A) Policy

Explanation:

Security policy framework is the regulations that are links the information security of an organization with the professionals and business .It maintains the responsibilities and documentation with security .

  • Policy is the document containing expectation,importance, requirement and scope of an organization about security .This security plan is high level document that need to be approved by higher management for implementation in company.
  • Other options are incorrect because standard principles, guidelines in form of regulation and procedure is the manner that is not required for to be permitted by higher management for being implemented on the business organization.
  • Thus the correct option is option(A).
8 0
3 years ago
Which symbol is at the beginning and end of a multiline comment block? &&& """ %%% ###
yawa3891 [41]

Answer:

#

Explanation:

I have notes on it we learned it in 8th

7 0
3 years ago
Do you get notified if someone follows you on Spotify like your email or do you just have to find out on your own
pav-90 [236]
Answer: Spotify does not have a feature to notify you of new followers.
8 0
2 years ago
1. What is the relationship between liquidity and the interest rates?
Alenkasestr [34]
Liquidity Effect. When the Fed pursues a tight monetary policy, it takes money out of the system by selling Treasury securities and raising the reserve requirement at banks. This raises interest rates because the demand for credit is so high that lenders price their loans higher to take advantage of the demand.
7 0
2 years ago
2. You turn on your Windows 7 computer and see the system display POST messages. Then
Anton [14]
The monitor would definitely be the problem in this scenario.
7 0
3 years ago
Other questions:
  • For local travel addresses and street names should be
    14·1 answer
  • An investigator obtains consent and HIPAA authorization from subjects to review their medical records and HIV status. He plans t
    8·1 answer
  • How can a wiki contribute to an academic paper?
    9·2 answers
  • An example of creative curating is when a:
    9·1 answer
  • What is data Communications​
    11·2 answers
  • This is a bit confusing to me. Could someone please help me! I have to use CSS to Design a Web Page. I will mark Brainliest!
    8·1 answer
  • What are some ways you can give staying off your phone a "boost" and make it easier to do?
    9·1 answer
  • Why do computers use binary code?
    12·2 answers
  • It's important to understand that even information systems that do not use computers
    14·1 answer
  • Can someone reply me
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!