Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You have to write a Python program (3.5) which take two text files. Start with t

ID: 3807338 • Letter: Y

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 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 you’ll 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. I’ll 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.

Explanation / Answer

Hey, I am sorry. I am able to solve only a part of the question. I could only count the number of words for a particular file. Here's my code:

def word_count_dict(filename):
"""Returns a word/count dict for this filename."""
# Utility used by count() and Topcount().
word_count = {} # Map each word to its count
input_file = open(filename, 'r')
for line in input_file:
words = line.split()
for word in words:
word = word.lower()
# Special case if we're seeing this word for the first time.
if not word in word_count:
word_count[word] = 1
else:
word_count[word] = word_count[word] + 1
input_file.close() # Not strictly required, but good form.
return word_count


def print_words(filename):
"""Prints one per line '<word> <count>' sorted by word for the given file."""
word_count = word_count_dict(filename)
words = sorted(word_count.keys())
for word in words:
print(word, word_count[word])
  
def main():
filename = input("Enter the filename whose word frequency needs to be counted.")
print_words(filename)
  
filename1 = input("Enter other filename whose word frequency needs to be counted.")
print_words(filename1)
  
  
  
if __name__ == '__main__':
main()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote