Question: You have to write a Python program (3.5) which take two text files. Start with the wordfreq.py script . As presented, it only calculates the

You have to write a Python program (3.5) which take two text files. Start with the "wordfreq.py" script . As presented, it only calculates the frequency of each individual word in a text file, stored in a dictionary structure. Keep the input question that asks how many results you want displayed (n).

Note: Since youll be doing many of the same operations twice, you should revise the code to write some sensibly-designed reusable functions instead of simply duplicating each chunk of code and changing variable names.

Revise and enhance the program so that the program ALSO does these things:

1. Ask the user for a second text file to compare with the first one. Ill refer to the files A and B below.

2. Calculate the word frequency for text B in the same way it does for A.

3. For both files, compute and print out how many total words it contains and how many distinct words they contain.

4. For both files, print out the n most frequent individual words (sorted like the provided example already does), but also showing the percentage of the total words each represents in its file. This is simple to calculate, as: frequency_of_word / total_words * 100. Round that to 2 decimal places.

5. Last, as a simple comparison of the texts, your program should print all the words that occured more than once in text A but not at all in B and vice-versa.

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!