Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Format the essay file according to the rules described above and compute the required statis- tics. You should do this in three steps. For the

Format the essay file according to the rules described above and compute the required statis- tics. You should do this in three steps. For the purposes of the following discussion, assume that the essay is in a file called essay.txt.

1. First, remove all extra white space (extra spaces between words, extra spaces at the beginning of a line, and extra blank lines between paragraphs) from essay.txt and output the result into a file called essay neb.txt. Keep in mind that only extra white space is removed. In particular, paragraphs should still be separated by one blank line.

2. Next, adjust the length of the lines in essay neb.txt to 60 characters, and output the result into a file called essay final.txt.

3. Finally, count the number of (non-blank) lines, the number of words, and the average word length for the text in essay final.txt. Output the result, with the appropriate headings, into a file called essay stats.txt. The average word length is simply the sum of the word lengths divided by the number of words. Your program should contain the following functions:

A function called remove extra whitespaces with two parameters: infile, the name of the file to be read from and outfile, the name of the file to be written to. Both these parameters are strings as they are file names. This function should open and close files as necessary and carry out the task described in step (1) above.

A function called adjust linelength with two parameters: infile, the name of the file to be read from (this is the file from which all extra white spaces have been removed) and outfile, the name of the file to be written to. Both these parameters are strings as they are file names. This function should open and close files as necessary and carry out the task described in step (2) above. Keep in mind that words should not be broken across lines. This implies that in the output file, a newline character should appear after the last complete word that will fit in a 60-character line. This should be true for every line of a paragraph. Furthermore, each line should contain the maximum number of complete words that can fit in that line. Also make sure that consecutive paragraphs continue to be separated by a blank line.

2 A function called essay statistics with two parameters: infile, the name of the file to be read from (this is the file in which line lengths have been adjusted) and outfile, the name of the file to be written to. Both these parameters are strings as they are file names. This function should open and close files as necessary and carry out the task described in step (3) above.

A function called format essay without any parameters. This function should ask the user to enter the name of the file containing the essay, and then carry out steps (1)-(3) by calling functions remove extra whitespaces, adjust linelength, and essay statistics, respectively. After each of these function calls, the function should inform the user that the intermediate file has been created. For example, if the user enters essay.txt as the name of the file, then after calling remove extra whitespaces, inform the user that the file essay neb.txt has been created, and so on for the other functions as well. Ide- ally, you should use intermediate file names that are derived from the original file name. For example, if the user enters rain.txt as the name of the file containing the essay, then the files created by your program should be rain neb.txt, rain final.txt, and rain stats.txt.

Once again, so that I may run your program in the shell, include an appropriate call to the format essay function in the body of the following if statement: if __name__ == "__main__":

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional SQL Server 2000 Database Design

Authors: Louis Davidson

1st Edition

1861004761, 978-1861004765

More Books

Students also viewed these Databases questions

Question

2. Evaluate factors that influence employment opportunities.

Answered: 1 week ago