Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A text file, stored in the working directory, consists of sentences. A sentence consists of words, possibly directly followed by a comma, except for the

A text file, stored in the working directory, consists of sentences. A sentence consists of words, possibly directly followed by a comma, except for the last word which is directly followed by a full stop. Words are separated by spaces.

 

Execution example:

   >>> statistics('text_file_1.txt')
   There are 2 sentencee(s).
   The shortest sentence has 31 worrd(s).
   The longest sentence has 34 worrd(s).
   The shortest word has 1 character(s).
   The longest word has 9 character(s).
   >>> statistics('text_file_2.txt')
   There are 4 sentencee(s).
   The shortest sentence has 6 worrd(s).
   The longest sentence has 34 worrd(s).
   The shortest word has 1 character(s).
   The longest word has 12 character(s).
   >>> statistics('text_file_3.txt')
   There are 1 sentencee(s).
   The shortest sentence has 30 worrd(s).
   The longest sentence has 30 worrd(s).
   The shortest word has 1 character(s).
   The longest word has 12 character(s).
   

code:


   def statistics(filename):

   nb_of_sentences = None
   length_of_shortest_word = None
   length_of_longest_word = None
   min_nb_of_words_in_sentences = None
   max_nb_of_words_in_sentences = None
   
   with open(filename) as file:
       data = file.read()
   
   sentences = data.split('.')
   sentences = [i for i in sentences if i != '']
   nb_of_sentences = len(sentences)

   line = [sentence.strip().split(' ') for sentence in sentences.split(',')]

       # REPLACE pass ABOVE WITH YOUR CODE
   
   print(data)

   print('There are', nb_of_sentences, 'sentence(s).')
   print('The shortest sentence has', min_nb_of_words_in_sentences, 'word(s).')
   print('The longest sentence has', max_nb_of_words_in_sentences, 'word(s).')
   print('The shortest word has', length_of_shortest_word, 'character(s).')
   print('The longest word has', length_of_longest_word, 'character(s).')

 

Step by Step Solution

3.51 Rating (148 Votes )

There are 3 Steps involved in it

Step: 1

To solve this problem you can use the following approach 1 Read the contents of the text file into a ... 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

Artificial Intelligence Structures And Strategies For Complex Problem Solving

Authors: George Luger

6th Edition

0321545893, 9780321545893

More Books

Students also viewed these Programming questions