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]
4 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]4 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
Ultraportable computers will often use ___ technology exclusively, even though their storage capacity is lower than that of a tr
ki77a [65]

Answer:

SSD

Explanation:

8 0
2 years ago
what do you do if someone you really love reads your message and doesnt reply? do I text them again or just ignore it?
crimeas [40]
If you really love someone and you text them but they don't text you back. You let it go because they aren't interested. I am sorry for saying that, but it could also mean that they are doing something at the moment and can't text back. I say, give it a day and if they don't text back, don't bother texting back.

3 0
3 years ago
After making a PowerPoint presentation about a new line of clothing designs, Henri notices that he used the word “gorgeous” on n
sineoko [7]

The spell checker and thesaurus are not underneath the view tab, so 1 and 2 are wrong.

If you replace every instance of the word gorgeous, then you would not be adding variety. So d is incorrect.

Therefore:

The only correct answer is C, as it is correct navigation and it adds word variety.

8 0
3 years ago
List the steps to identify address or link window on a phone​
Law Incorporation [45]

Answer: this is how

Explanation: if you are on your phone and a link pops up if it is blue then it is able to be clicked but if it is not blue you can simply copy and paste the link into your web browser.

7 0
3 years ago
What is the difference between Data and information?​
Ksivusya [100]

Answer:

Data is a piece of an Information or a raw form of information while an Information is a processed Data.

Explanation:

An example of Data would be Alphabets of someone's name while the name would be an example of an Information.

On it's own, data makes no sense and needs to be processed to become Information which makes sense.

6 0
3 years ago
Other questions:
  • Assume you have functions f and g such that f(n) is O(g(n)). For each of the following statements, decide whether you think it i
    12·1 answer
  • Why are high-quality transformers wound with large diameter wire?
    8·1 answer
  • Editing tool for quickly making a repeated change throughout a document
    14·1 answer
  • Which of the following statements is true of satellite internet access?
    13·1 answer
  • Select the correct answer.
    9·2 answers
  • Being able to express your thoughts in an email is a primary technology skill. true or false.
    9·2 answers
  • Ba esti prost?<br> Ba esti nebun?<br> Ce ai?
    5·2 answers
  • What are some other features of sending attachments in Outlook 2016? Check all that apply.
    9·2 answers
  • How do I send the face to the back? Seriously, I can't find out how...
    13·1 answer
  • A variable is assigned a value on line 328 of a program. Which of the following must be true in order for the variable to work?
    12·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!