50.5k views
0 votes
1. (4 points) Write a function, called word_frequency, which takes a file name as a formal parameter and find the occurrence frequency of each distinct word in the file. Consider to use the following algorithm: A. Prepare a dictionary to keep track of the count of each word. B. Open the file. C. For each line in the file do: C.A Break the line into words using spaces in between. C.B Clean each word: strip leading and trailing whitespaces, digits and punctuation characters and convert uppercase letters to lowercase. C.C Update word occurrence count in the word count dictionary. Each item in the dictionary has a word as key and the count of the word as value. D. Close the file. E. Return the dictionary. These are the symbols and digits to be stripped off from words: punctuation

User Usul
by
4.9k points

1 Answer

4 votes

Answer:

Check the explanation

Step-by-step explanation:

CODE

def total_word_count(words):

total = 0

#Loop through all words

for word in words:

#Find total

total = total + words[word]

return total

Kindly check the attached image below for the code output.

1. (4 points) Write a function, called word_frequency, which takes a file name as-example-1
User Ivan Wang
by
4.7k points