Question: Use the Design Recipe to write a function called word_frequency that consumes the name of a file (str) and produces a dictionary in which the

 Use the Design Recipe to write a function called word_frequency that

Use the Design Recipe to write a function called word_frequency that consumes the name of a file (str) and produces a dictionary in which the keys are the individual words found in the file and the values are the number of times each word appears For example, if the word "the appears 128 times, the dictionary would contain an element with 'the' as the key and 128 as the value The program may assume the file exists on disk and no error checking is needed. The file may be assumed to contain 1 word per line To test the given examples on your local computer, create a file named nums.txt and make sure its contents are 4 and create a file named chars.txt and make sure its contents are For example Test Result tilonamo "nums.txt" frequencies word frequency (ilename) rosult" for key in sorted (frequencies.keys()): rosult += str (koy) + " " + str(frouoncios[koy]) + " " print (result) filname "ma t t2.txt" froquencios = word-froquency (filonamo) result "" for key in sorted (frequencies.keys O): b 1 result += str (key) " " + + str(frequencies [key]) + " " print(result)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!