You have to write a Python program (3.5) which take two text files and compare t
ID: 3864092 • Letter: Y
Question
You have to write a Python program (3.5) which take two text files and compare them. 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
From collections import deafultdict
From collections import counter
Import string
Wordcount = defaultdict (int)
File1=open ('name of text file','r')
File2=open ('name of text file2','r')
Words =[word for line in file1 for word in line.split ()]
Words2=[word for line in file2 for word in line.split ()]
Print "total no.of words",len (Words)
Print "total no. Of words",len (words2)
C=Counter(Words)
For word,count in c.most_common():
Print word,count
C2=Counter (words2)
For word,count in c2.most_common():
Print word,count
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.