Question

In: Computer Science

A sentence can end with full-stop, question mark, or an exclamation mark. Spaces precede and follow...

A sentence can end with full-stop, question mark, or an exclamation mark. Spaces precede and follow a word.

Write a Python program to perform the following tasks:

  • Receive a filename from the user, open a text file for reading, and read the file.
  • Count the sentences in the text file.
  • Count the number of words in the text file.
  • Print these two values with appropriate labels as a result of running the program

Solutions

Expert Solution

Code:

#ask the user to enter a filename
filename = input("Enter a filename: ")
#open the file name for reading
with open(filename, "r") as file:
#initialize variables
sentences = 0
words = 0
#iterate each line in the file object
for line in file:
sentences += line.count(".") #count the period symbols in line and add it to sentences
sentences += line.count("!") #count the exclamation symbols in line and add it to sentences
sentences += line.count("?") #count the question mark symbols in line and add it to sentences
words += len(line.split(" ")) #split the line with respect to spaces and add the lenght of the resulting list to words
print("Sentences = {}".format(sentences)) #print count of sentences
print("Words = {}".format(words)) #print count of words

Code in the image:

Output:

Contents of sample file: sun.txt

This is sentence one. This is sentence two.
How are you? Great!
Haha! I am fine.


Related Solutions

STOP AND READ THIS PART!! - please DO NOT answer the full question. PLEASE just explain...
STOP AND READ THIS PART!! - please DO NOT answer the full question. PLEASE just explain why you would use the "pooled variances" over the method that uses degrees of freedom. please explain the differences and when you would use one over the other READ ABOVE^^ -- A cigarette manufacturer is experimenting with two new filters for its cigarettes. Filter I is cheaper to make than Filter II and will be used unless there is statistical evidence that Filter II...
Other Searches. For each question, answer the prompt and provide at least one full sentence of...
Other Searches. For each question, answer the prompt and provide at least one full sentence of reasoning and justification. More than one sentence may be necessary. Q1) In what situation(s) is Breadth First Search identical to Uniform Cost Search?
A. answer the following question in full sentence J. Monodo and F. Jacob, constructed several mutant...
A. answer the following question in full sentence J. Monodo and F. Jacob, constructed several mutant strain to study lac operon. which binding domains were affected by lac- and lacIs, and what was the effect of those mutations? how did LacOc affect binding? B. choose the correct choice in the next 6 multiple-choice questions. 1.FGFR3 mutation causes Achondroplasia, the mutation results in.... a) expression of the gene at an inappropriate place. b) expression of a gene that is more efficient...
Question 24 Scenario: Mark can make three tables and one chair in a day, while John...
Question 24 Scenario: Mark can make three tables and one chair in a day, while John can make four tables and one chair in a day. Refer to the scenario above. Which of the following is true in this case? Mark should specialize in making chairs, because he has a lower opportunity cost of producing them. Mark should specialize in making chairs, because he has a greater opportunity cost of producing them. Mark should specialize in making tables, because he...
Use this information for Carmen Co. to answer the question that follow. Carmen Co. can further...
Use this information for Carmen Co. to answer the question that follow. Carmen Co. can further process Product J to produce Product D. Product J is currently selling for $21.85 per pound and costs $16.85 per pound to produce. Product D would sell for $41.40 per pound and would require an additional cost of $9.50 per pound to produce. What is the differential cost of producing Product D? a.$7.60 per pound b.$9.50 per pound c.$5.70 per pound d.$11.40 per pound...
In your own words and in two full paragraphs can you please answer the following question....
In your own words and in two full paragraphs can you please answer the following question. Can you please make sure it is two full paragraphs please. -why does sleep deprivation affect cognitive performance?
Question 1 You currently have $7,500 to invest. You can invest the full amount now for...
Question 1 You currently have $7,500 to invest. You can invest the full amount now for a period of 9 years at which time you want to have $15,000. Approximately what rate of return is needed to accomplish this investment goal? 8.01% 7.59% 9.65% 8.50% Question 2 If four years of college tuition cost $15,000 in 1990, what did it cost in 1965 if tuition increased at 7% per year between 1965 and 1990? $2,764 $1,405 $3,876 $8,141 Question 3...
Please answer all 3 parts so I can get full credit for this question A five-year...
Please answer all 3 parts so I can get full credit for this question A five-year annuity of ten $5,900 semiannual payments will begin 9 years from now, with the first payment coming 9.5 years from now. The discount rate is 8 percent compunded monthly. a. What is the value of this annuity five years from now? b. What is the value three years from now? c. What is the current value of the annuity? (Do not round intermediate calculations...
AP CHEM question! Can you please follow these steps for each: 1.) net ionic equation, 2.)...
AP CHEM question! Can you please follow these steps for each: 1.) net ionic equation, 2.) ice table, 3.) pH= pKa + log (CB/WA) A 35.0 mL sample of 0.150 M acetic acid (HC2H3O2) is titrated with 0.150 M NaOH solution. Calculate the pH after the following volumes of base have been added: A.) 0 mL B.) 17.5 mL C.) 34.5 mL D.) 35.0 mL E.) 35.5 mL F.) 50.0 mL
This question illustrates how bidding dishonestly can end up hurting the cheater. Four partners are dividing...
This question illustrates how bidding dishonestly can end up hurting the cheater. Four partners are dividing a million-dollar property using the lone-divider method. Using a map, Danny divides the property into four parcels s1, s2, s3, and s4. The following table shows the value of the four parcels in the eyes of each partner (in thousands of dollars): s1 s2 s3 s4 Danny $250 $250 $250 $250 Brianna $520 $170 $150 $160 Carlos $320 $350 $210 $120 Greedy $320 $300...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT