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
I want to make a if else statement where I want to make a message. If the input are letters (which it’s not supposed to) then I
MrRa [10]

Answer:scratch, im in it (22550077) AND ALSO IF MEANS LIKE IF YOU SAY HI THEN IT SAYS HI OR IF YOU SAY START GAME THE GAME STARTS

Explanation:

4 0
3 years ago
The Mark Table of Contents Entry option is used for marking nonheading text for the table of contents. the options are manually
enyata [817]

Answer:number 1

Explanation:asian culture

5 0
3 years ago
Read 2 more answers
In Python please:
g100num [7]

Answer:

def compute_pay(number_of_hours, rate_of_pay):

   if number_of_hours > 40:

       pay_for_week = (40*rate_of_pay)+((number_of_hours-40)*\

                                                (rate_of_pay+rate_of_pay*0.5))

   else:

       pay_for_week = number_of_hours*rate_of_pay

   if pay_for_week >= 375:

       print("Paying %d by direct deposit" % pay_for_week)

   else:

       print("Paying %d by mailed check" % pay_for_week)

Explanation:

  1. We define the computer pay function that receives the number of hours worked in a week and the rate of pay
  2. From the test cases we deduce that if a worker works more than 40 hours a week an extra payment is given, you can calculated it as follow: (40 * rate_of_pay) + ((number_of_hours - 40) * (rate_of_pay + rate_of_pay * 0.5))
  3. If a worker works less than 40 hours the payment is calculated as follow: pay_for_week = number_of_hours * rate_of_pay
  4. If the pay for week is equal or greater than 375 we print a payment by direct deposit otherwise we print payment by mailed check

3 0
3 years ago
python program A year with 366 days is called a leap year. Leap years are necessary to keep the calendar in sync with the sun, b
Liula [17]

Answer:

In Python:

year = int(input("Year: "))

if (year % 4) == 0:

  if (year % 100) == 0:

      if (year % 400) == 0:

          print("Leap year")

      else:

          print("Not a leap year")

  else:

      print("Leap year")

else:

  print("Not a leap year")

Explanation:

This line prompts user for year

year = int(input("Year: "))

This condition checks if input year is divisible by 4

if (year % 4) == 0:

If yes, this condition checks if input year is divisible by 100

  if (year % 100) == 0:

If yes, this condition checks if input year is divisible by 400

      if (year % 400) == 0:

If divisible by 4, 100 and 400, then it is a leap year

          print("Leap year")

      else:

If divisible by 4 and 100 but not 400, then it is not a leap year

          print("Not a leap year")

  else:

If divisible by 4 but not 100, then it is a leap year

      print("Leap year")

else:

If it is not divisible by, then it is not a leap year

  print("Not a leap year")

3 0
3 years ago
Employees of ABC company uploaded files on a shared server with unique file naming conventions. However, they faced problems whi
sesenic [268]

WHAT ARE THE ANSWERS OMG WHY DO PEOPLE DO THIS

5 0
3 years ago
Read 2 more answers
Other questions:
  • What was the name of the system developed by Frederick Winslow Taylor, in which mass production is tied to the cultivation of ma
    10·1 answer
  • Complete the sentences to describe file options categories
    14·2 answers
  • What role does the agenda play in a webinar??
    14·1 answer
  • IT organizations implement powerful information systems like ERP and SCM that provide centralized data repositories. In addition
    11·2 answers
  • Windows XPProfessional and Windows Vista Both have same devicedrivers.<br> True<br> False
    5·1 answer
  • Although architects frequently do most of their work in an office, what might motivate an architect to work from the jobsite?
    7·1 answer
  • Generally, websites ending in .edu, .gov, and .org are less likely to be biased, or to show preference toward a certain financia
    12·1 answer
  • Function of Ribbon in Access 2010​
    7·1 answer
  • A form of segmentation that is based on user usage rate, user status, purchase occasion, and benefits sought is _________.
    13·1 answer
  • What are different sheet icons
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!