In: Computer Science
In Python write a program that prompts the user for a file name, make sure the file exists and if it does reads through the file, count the number of times each word appears and then output the word count in a sorted order from high to low.
The program should:
the - 7
in - 6
to - 5
and - 4
of - 4
Remember:
NYT2.txt file below:
Fact-Checking Trump’s Orlando Rally: Russia, the Wall and Tax Cuts President Trump delivered remarks in Florida in a formal start to his re-election effort. Deutsche Bank Faces Criminal Investigation for Potential Money-Laundering Lapses Federal authorities are focused on whether the bank complied with anti-money-laundering laws, including in its review of transactions linked to Jared Kushner. Five NY1 Anchorwomen Sue Cable Channel for Age and Gender Discrimination The women, including Roma Torre, say their careers were derailed after Charter Communications bought New York’s hometown news station in 2016. Hypersonic Missiles Are Unstoppable. And They’re Starting a New Global Arms Race. The new weapons — which could travel at more than 15 times the speed of sound with terrifying accuracy — threaten to change the nature of warfare. Nxivm’s Keith Raniere Convicted in Trial Exposing Sex Cult’s Inner Workings Mr. Raniere set up a harem of sexual “slaves” who were branded with his initials and kept in line by blackmail. Jamal Khashoggi Was My Fiancé. His Killers Are Roaming Free. Washington hasn’t done enough to bring the murdered Saudi columnist’s killers to justice.
Hi,
The following is the code to print the frequency of words in the file.
Please ensure you create the nyt2.txt (or similar) file in current folder or any other folder and provide as input to the program.
#File: filewordcount.py
while True:
try:
#Get the name of the file to parse from the user
file_name = input("Enter the file name: ")
#Open the file and hold reference to the file
file = open(file_name)
#Dictionary to hold the count of each word in the file
word_count_dict = {}
#Read the file and spli the line into words
words = file.read().split()
#iterate each word in the list
for word in words:
#Check if the word is already in the dictionary, else
initialize
if word.lower() not in word_count_dict:
word_count_dict[word.lower()] = 1
else:
#If word already exists in the dictionary, increment the
counter
word_count_dict[word.lower()] += 1
#Sort the dictionary based on the values in descending order
sorted_word_count_dict = [(key, word_count_dict[key]) for key in
sorted(word_count_dict, key=word_count_dict.get, reverse=True)]
#Get the max frequency from the sorted list
max_count = sorted_word_count_dict[0][1]
#Iterate from the max counter value to 1
for v in range(max_count, 0, -1):
#Find the words that have the same value as "v"
keys_same_value = [key for(key, value) in sorted_word_count_dict if
value == v]
#If more than one word has same frequency, sort the words and
display them
if len(keys_same_value) > 1:
sorted_keys_same_value = sorted(keys_same_value)
for i in range(len(sorted_keys_same_value)):
print(sorted_keys_same_value[i] + " : " + str(v))
else:
#If there is only one word with same frequency, just display
it
print( keys_same_value[0] + " : " + str(v))
break
except Exception as ex:
print("Error: Unable to read the file. Try again")
print(ex)
### Output