Question
use python to do The judges of an essay competition require all essays to be submitted electronically in a neatly formatted fashion. However, not all
use python to do The judges of an essay competition require all essays to be submitted electronically in a neatly formatted fashion. However, not all submissions follow the formatting rules, and so the judges would like a program that formats a file according to their formatting rules. The judges also require some statistics for each essay. In particular, they require a count of the number of non-blank lines, the number of words, and the average word length for each essay. In this problem, you are asked to write a program that does both things; that is, a program that formats the files, and then calculates the required statistics. For the purposes of the program and the discussion, a word is simply a consecutive sequence of non-whitespace characters surrounded by white space. By this definition, a period or a comma by itself, surrounded by white space, would be counted as a word, but we'll live with that. The formatting rules for each essay are as follows:
There should not be any blank spaces at the beginning of a line. Two or more blank spaces should not appear consecutively. That is, there should be only one blank space between consecutive words. Two or more blank lines should not appear consecutively. That is, there should be only one blank line between consecutive paragraphs. There should be at most 60 characters per line (including blank spaces). Also, words should not be broken across lines. This implies that in a paragraph, a newline character should appear after the last complete word that will fit in a 60-character line. You may assume that no single word is longer than 60 characters (seems like a reasonable assumption). 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. 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. 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. Ideally, you should use interme- diate 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 program should be "rain neb.txt", "rain final.txt", and "rain stats.txt". A sample set of files is shown below. Suppose essay.txt is the following file: Albuquerque is my turkey and he's feathered and he's fine, And he wobbles and he gobbles and he's absolutely mine He's the best pet you can get yet, better than a dog or cat, He's my albuquerque turkey and i'm awfully proud of that Albuquerque is my turkey and he's happy in his bed, 'Cause for our thanksgiving dinner we'll have sphagetti instead Then, the file essay neb.txt, with extra white spaces removed, should look like this: Albuquerque is my turkey and he's feathered and he's fine, And he wobbles and he gobbles and he's absolutely mine He's the best pet you can get yet, better than a dog or cat, He's my albuquerque turkey and i'm awfully proud of that Albuquerque is my turkey and he's happy in his bed, 'Cause for our thanksgiving dinner we'll have sphagetti instead The final file essay final.txt with line length adjusted to 60 should look like the following. Just for your information, the longest line (the first line of the second paragraph) is exactly 60 characters long. Note that no words are broken across lines. Albuquerque is my turkey and he's feathered and he's fine, And he wobbles and he gobbles and he's absolutely mine He's the best pet you can get yet, better than a dog or cat, He's my albuquerque turkey and i'm awfully proud of that Albuquerque is my turkey and he's happy in his bed, 'Cause for our thanksgiving dinner we'll have sphagetti instead
Finally, file essay stats.txt should look like this (average word length has been truncated): In the file essay.txt: Number of (non-blank) lines: 6 Number of words: 63 Average word length: 4 Here is a sample output from calling format essay(): Essay Formatting Helper Program ------------------------------- Enter the name (*.txt) of the file containing the essay: essay.txt Three files have been created: essay_neb.txt: the essay without extra whitespaces or blank lines essay_final.txt: the formatted essay essay_stats.txt: the essay statistics Two sample files, essay.txt and essay2.txt, are provided for you to test your implementation.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Heres an implementation of the program that formats an essay file according to the given rules and calculates the required statistics python def remov...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started