Using Python, please write code that does the following:
I have already written the helper code, which is asfollowing:
Please try to keep the code as simple as possible andcover all the conditions specified. Thanks so much!
def mostCommonWords (filename, N): (20 points) Reads the file from filename in your function and returns a list of N most common words in the text file (i.e., N words with the highest frequency), sorted by the number of times they occured in the file (most common first). – Use wordFrequency() helper function to count the frequency of each word. – Print “[Error] The “<filename>” contains <x> unique words (you asked for <N>).” and return None if N is larger than the number of words in the file (substitute “<filename>”, <x>, <N> with the actual values). return “stub” def wordFrequency (filename): frequency = {} file = open(filename, ‘r’) for line in file.readlines(): for word in line.strip() .split(): if word not in frequency: frequency[word] = 0 frequency (word] += 1 “If word is in frequency, then increase the frequency number by l. file.close() return frequency Show transcribed image text def mostCommonWords (filename, N): (20 points) Reads the file from filename in your function and returns a list of N most common words in the text file (i.e., N words with the highest frequency), sorted by the number of times they occured in the file (most common first). – Use wordFrequency() helper function to count the frequency of each word. – Print “[Error] The “” contains unique words (you asked for ).” and return None if N is larger than the number of words in the file (substitute “”, , with the actual values). return “stub”
def wordFrequency (filename): frequency = {} file = open(filename, ‘r’) for line in file.readlines(): for word in line.strip() .split(): if word not in frequency: frequency[word] = 0 frequency (word] += 1 “If word is in frequency, then increase the frequency number by l. file.close() return frequency
Expert Answer
Answer to Using Python, please write code that does the following: I have already written the helper code, which is as following: …