Question

In: Computer Science

Write a program that prompts the user for a file name, make sure the file exists...

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:

  • Display a message stating its goal
  • Prompt the user to enter a file name
  • Check that the file can be opened and if not ask the user to try again (hint: use the try/except structure)
  • Count the number of times each word appears in the file, regardless if in lowercase or uppercase (hint: use dictionaries and the lower() function)
  • Display the word count in order from high to low
  • Bonus: if a few words have the same count, sort the display in an alphabetic order
  • For example, for the attached file NYT2.txt, the top five words in the output should be

the - 7

in - 6

to - 5

and - 4

of - 4

Using Python as program language

Solutions

Expert Solution

Code:-

import re #importing library for splitting purpose
print("Printing the words in descending order based on frequency of the word in the file") #printing
while(True): #loop until user enters valid file name
filename=input("Enter filename: ") #Asking user to enter file name and taking filename from user
try:
file=open(filename,"r") #opening file in read mode
break; #if no exception
except: #if invalid filename
print("Invalid Filename Enter Again") #Asking user enter again
words=re.split('; |, |\*|\n|\s', file.read().lower()) #making all words to lowercase letters and splitting the text by(; , \n \s)
file.close() #closing the file
#here I want to sort the list for words in alphabetical order
words.sort() #sort the words list
frequency={} #declaring frequency dictionary
for i in words: #traverse through all the words in words list
if i not in frequency: #if word is not present in dictionary
frequency[i]= 0 #initiate the value with 0
frequency[i]+=1 #increment the count value
dictlist=list(frequency.items()) #here make dictionary to list for sorting purpose
for i in range(len(dictlist)-1, -1, -1): #bubble sort to sort the list using values
for j in range(i):
if dictlist[j][1] < dictlist[j+1][1]:
dictlist[j], dictlist[j+1] = dictlist[j+1], dictlist[j] #swapping
frequency=dict(dictlist) #make updatedlist to dictionary
for i in frequency:
print(i,"-",frequency[i]) #printing key along with value

Code Screenshot:-

OUTPUT:-


Related Solutions

C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
Part A In PyCharm, write a program that prompts the user for their name and age....
Part A In PyCharm, write a program that prompts the user for their name and age. Your program should then tell the user the year they were born. Here is a sample execution of the program with the user input in bold: What is your name? Amanda How old are you? 15 Hello Amanda! You were born in 2005. Write the program. Format your code using best practices. Refer to the zyBooks style guide, if needed, to use proper naming...
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
Write a program that asks the user for a file name. The file contains a series...
Write a program that asks the user for a file name. The file contains a series of scores(integers), each written on a separate line. The program should read the contents of the file into an array and then display the following content: 1) The scores in rows of 10 scores and in sorted in descending order. 2) The lowest score in the array 3) The highest score in the array 4) The total number of scores in the array 5)...
Write a JAVA program that prompts the user to enter a single name. Use a for...
Write a JAVA program that prompts the user to enter a single name. Use a for loop to determine if the name entered by the user contains at least 1 uppercase and 3 lowercase letters. If the name meets this policy, output that the name has been accepted. Otherwise, output that the name is invalid.
Tail of a File, C++ Program. write a program that asks the user for the name...
Tail of a File, C++ Program. write a program that asks the user for the name of a text file. The program should display the last 10 lines, or all lines if less than 10. The program should do this using seekg Here is what I have so far. #include<iostream> #include<fstream> #include<string> using namespace std; class File { private:    fstream file;    string name; public:    int countlines();    void printlines(); }; int File::countlines() {    int total =...
Write a Java program that allows the user to specify a file name on the command...
Write a Java program that allows the user to specify a file name on the command line and prints the number of characters, words, lines, average number of words per line, and average number of characters per word in that file. If the user does not specify any file name, then prompt the user for the name.
jgrasp environment, java write a complete program that prompts the user to enter their first name,...
jgrasp environment, java write a complete program that prompts the user to enter their first name, middle name, and last name (separately). print out thier name and initials, exactly as shown: your name is: John Paul Chavez your initials are: J. P. C. use string method chartAt() to extract the first (zero-th) character from a name(the name is a string type): username.charAt(0). thank you.
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT