Question

In: Computer Science

Python class Select a free ebook and download the plain text file (utf8). Write a program...

Python class

Select a free ebook and download the plain text file (utf8).

Write a program that does the following:

  • Read in your ebook (any book, any language)
  • Break the book into words (look at .split() for strings)
  • Make a dictionary of all words and how often they occur: for instance if the word'the' happened 2000 time and the word'a' happened 1940 times word_dict= {'the' : 2000, 'a' :1940}
  • Print the dictionary of word frequencies to a file (freqs.txt) with the most frequent at the top and least frequent at the bottom
  • No need to submit the book, I will run it on my own files.

Turn in a .py or Google Colab URL

Dictionaries are not easily sortable since they are not ordered like a list. To sort for this assignment use the following template.


import operator

sorted_dict = sorted(my_dict.items(),key=operator.itemgetter(1))

Solutions

Expert Solution

CODE:

RAW CODE:

import operator
file = open("file","r") # opening a file in read mode , replace file name with your own file
data = file.read().split() # reading all data and split the data into words using by split() function
dic = {} # empty dic
for word in data: # iterate each word in data
        if word not in dic: # if the word is not present in the dic than add new pair with (word,1) as key and value
                dic[word] = 1
        else: # if the word is already exists in the dic than increment the value of dic[word] 
                dic[word] = dic[word] + 1
sorted_dict = sorted(dic.items(),key=operator.itemgetter(1)) # sorting dictionary based on value
out = open("freqs.txt","w") # opening file in write mode
for item in sorted_dict: # iterate each item(tuple) in sorted_dict
        out.write(str(item[0]) + "\t:" + str(item[1]) + "\n") # write the item data(key and value) into txt file. key = item[0] , value = item[1]
# closing files
file.close()
out.close()

NOTE:
If You have any doubts feel free to comment in comment section.
DO VOTE(LIKE).


Related Solutions

Python class Select a free ebook on gutenberg . org and download the plain text file...
Python class Select a free ebook on gutenberg . org and download the plain text file (utf8). the ebook is (The Devil's Dictionary by Ambrose Bierce) Write a program that does the following: Read in your ebook (any book, any language) Break the book into words (look at .split() for strings) Make a dictionary of all words and how often they occur: for instance if the word'the'happened 2000 time and the word'a' happened 1940 times word_dict= {'the' : 2000, 'a'...
Write a Python program to implement Vignere Cipher. Take user input to get plain text and...
Write a Python program to implement Vignere Cipher. Take user input to get plain text and key. TRY TO MAKE IT AS EASY AS YOU CAN.
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
Write a python program that does the following: Prompt for a file name of text words....
Write a python program that does the following: Prompt for a file name of text words. Words can be on many lines with multiple words per line. Read the file and convert the words to a list. Call a function you created called list_to_once_words(), that takes a list as an argument and returns a list that contains only words that occurred once in the file. Print the results of the function with an appropriate description. Think about everything you must...
Design and write a python program that reads a file of text and stores each unique...
Design and write a python program that reads a file of text and stores each unique word in some node of binary search tree while maintaining a count of the number appearance of that word. The word is stored only one time; if it appears more than once, the count is increased. The program then prints out 1) the number of distinct words stored un the tree, Function name: nword 2) the longest word in the input, function name: longest...
Write a Python program that will process the text file, Gettysburg.txt, by calculating the total words...
Write a Python program that will process the text file, Gettysburg.txt, by calculating the total words and output the number of occurrences of each word in the file. The program needs to open the file and process each line. You need to add each word to the dictionary with a frequency of 1 or update the word’s count by 1. You need to print the output from high to low frequency. The program needs 4 functions. The first function is...
Write a java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
Write a Java program that will encode plain text into cipher text and decode cipher text...
Write a Java program that will encode plain text into cipher text and decode cipher text into plain text. Create following methods and add them to your class: • a method named encode(String p, int key) that takes a plain text p and encodes it to a cipher text by adding the key value to each alphabet character of plain text p using the ASCII chart. The method should return the encoded String. For example, if p = "attack at...
Write a python program to read from a file the names and grades of a class...
Write a python program to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a gradesInput() function that reads data from a file and stores it and returns it as a dictionary....
In this PYTHON 3 program assignment, you will find a text file named WorldSeries.txt. This file...
In this PYTHON 3 program assignment, you will find a text file named WorldSeries.txt. This file contains a chronological list of the World Series' winning teams from 1903 through 2018. The first line in the file is the name of the team that won in 1903, and the last line is the name of the team that won in 2018. (Note the World Series was not played in 1904 and 1994. There are entries in the file indicating this.) Write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT