Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a Python program that does all the following steps: Read a file located in the same directory as the program named: cs521_5_5_input.txt o
Write a Python program that does all the following steps: Read a file located in the same directory as the program named: cs521_5_5_input.txt o Print a clear error message and end the program if the file is missing o The program must not crash because the file doesn't exist . Your program will have four main objectives: o Determine the file's length in words o Count the number of unique words in the file o Return the word(s) that is/are most frequent o Return the word(s) that is/are least frequent To accomplish these four objectives, you will create the following user-defined functions: make_word_list() make_unique() Has a single parameter, a file object Iterate through the file and extract every word from the file Strip from each word any punctuation Returns a list object of the words . Has a single parameter, a list object of all words in the file (created via the make_word_list() function) find most frequent() . From the list object, create a list of only unique words Returns a list object of the unique words Has a single parameter, a list object of all words in the file (created via the make_word_list() function) From the list object, count the occurrence of each word and create a list object that stores the word(s) that appear(s) the most often Returns the list object of the most frequent word(s) find_least frequent() Has a single parameter, a list object of all words in the file (created via the make_word_list() function) From the list object, count the occurrence of each word and create a list object that stores the word(s) that appear(s) the most often Returns the list object of the least frequent word(s) Sample Output: The file has 50 words in total. The file has 27 unique words in total. The least frequent word (s): ['circumstance', 'world'] The most frequent word (s): ['my', 'a', 'the', 'to', 'yes', 'no', 'he', 'she', 'it', 'there'] Notes: You may NOT use a dictionary object to solve this problem. You should consider words as non-case sensitive. Meaning, the word Hello is the same as "hello". Account for this accordingly as you determine unique words, most and least frequent words somewhere in your function(s). Do not hardcode your string slices. Remember to properly close all files.
Step by Step Solution
★★★★★
3.34 Rating (148 Votes )
There are 3 Steps involved in it
Step: 1
import os def makewordlistfileobject Extracts every word from the file object stripping any punctuation Args fileobject A file object Returns A list o...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