Question

In: Computer Science

Write a python program that calculates the average number of words per sentence in the input...

Write a python program that calculates the average number of words per sentence in the input text file. every sentence ends with a period (when, in reality, sentences can end with !, ", ?, etc.) and the average number of sentences per paragraph, where a paragraph is any number of sentences followed by a blank line or by the end of the text.

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

#code

#reading name of file from user
file=input("Enter input file name: ")
#opening file, assuming file exists
file=open(file)
#declaring number of words, sentences and paragraphs to 0
words=0
sentences=0
paragraphs=0
#reading all lines from file to a list
lines=file.readlines()
#closing file
file.close()
#looping through each index in lines list
for i in range(len(lines)):
    #removing the newline char from end of current line
   
line=lines[i].strip()
    #checking if line is empty (blank line)
   
if line=='':
        #incrementing number of paragraphs
       
paragraphs+=1
    else:
        #splitting the line into a list of words by space or tab
       
words_in_line=line.split()
        #looping through each word in resultant list
       
for w in words_in_line:
            #incrementing words count
           
words+=1
            #if word ends with a period, incrementing sentences
           
if w.endswith('.'):
                sentences+=1
        #if this is the last line and line is not empty (end of text),
        #incrementing paragraphs
       
if i==len(lines)-1:
            paragraphs+=1

#at the end, if any field is 0, displaying that the file is empty
if paragraphs==0 or words==0 or sentences==0:
    print("Empty file!")
else:
    #otherwise finding the average words per sentence
   
avg_words_per_sentence=words/sentences
    #and average sentences per paragraph
   
avg_sentences_per_paragraph=sentences/paragraphs
    #displaying results
   
print("Average number of words per sentence:",avg_words_per_sentence)
    print("Average number of sentences per paragraph:", avg_sentences_per_paragraph)

#output

#screenshot of inp.txt file used in this program


Related Solutions

Write a program in python that corrects misspelled words. The input for the program is either...
Write a program in python that corrects misspelled words. The input for the program is either a string or a list of strings. The output should be a string or a list depending on the input and should have the spellings corrected. The speller should be able to correct at least the words listed below. You are free to use any data structures we have covered so far including lists and dictionaries. Your program should be in autocorrect.py. Here is...
c++ 9.39 LAB: Replacement words Write a program that replaces words in a sentence. The input...
c++ 9.39 LAB: Replacement words Write a program that replaces words in a sentence. The input begins with an integer indicating the number of word replacement pairs (original and replacement) that follow. The next line of input begins with an integer indicating the number of words in the sentence that follows. Any word on the original list is replaced. Ex: If the input is: 3 automobile car manufacturer maker children kids 15 The automobile manufacturer recommends car seats for children...
Write a program that calculates the average of upto 100 English distances input by the user....
Write a program that calculates the average of upto 100 English distances input by the user. Create an array of objects of the Distance class, as in the ENGLARAY example in this chapter. To calculate the average, you can borrow the add_dist() member function from the ENGLCON example in Chapter 6. You’ll also need a member function that divides a Distance value by an integer. Here’s one possibility: void Distance::div_dist(Distance d2, int divisor) { float fltfeet = d2.feet + d2.inches/12.0;...
Write a C program that calculates the average grade for a specified number of students from...
Write a C program that calculates the average grade for a specified number of students from each student's test1 and test2 grades. The program must first ask the user how many students there are. Then, for each student, the program will ask the user for the test1 grade (grade #1) and test2 grade (grade #2). The program should be able to handle up to 100 students, each with 2 grades (test1 and test2). Use a two-dimensional float array to store...
Write a C program that calculates the average grade for a specified number of students from...
Write a C program that calculates the average grade for a specified number of students from each student's termtest and final grade. The program must first ask the user how many students there are. Then, for each student, the program will ask the user for the termtest grade (grade #1) and final grade (grade #2). The program should be able to handle up to 100 students, each with 2 grades (termtest and final). Use a two dimensional float array to...
Write a java program that read a line of input as a sentence and display: ...
Write a java program that read a line of input as a sentence and display:  Only the uppercase letters in the sentence.  The sentence, with all lowercase vowels (i.e. “a”, “e”, “i”, “o”, and “u”) replaced by a strike symbol “*”.
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x)...
Write a C program to find out the number of words in an input text file...
Write a C program to find out the number of words in an input text file (in.txt). Also, make a copy of the input file. Solve in C programming.
Write a program (O(n), where n is the number of words) that takes as input a...
Write a program (O(n), where n is the number of words) that takes as input a set of words and returns groups of anagrams for those words. Complete your code here Do not change anything in the test file. CPP File: #include #include #include #include using namespace std; vector> findAnagrams(const vector& dict); vector> findAnagrams(const vector& dict) { // Your code here... } Test File: #include #include #include #include using namespace std; vector> findAnagrams(const vector& dict); int main() { vector word_list...
Write a program in python that counts the number of words in President Abraham Lincoln’s Gettysburg...
Write a program in python that counts the number of words in President Abraham Lincoln’s Gettysburg Address from the file provided. Replace all instances of "nation" with country, and all instances of "we" with "Americans". Write the revised address to a new file its for a txt file
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT