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'...
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 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.
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 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: There is a file called file 2. File2 is a txt file...
Write a python program: There is a file called file 2. File2 is a txt file and I have written the contents of file 2 below in the exact format it was in notepad. # This comment does not make sense # It is just to make it harder # The job description starts after this comment, notice that it has 4 lines. # This job description has 700150 hay system points\\ the incumbent will administer the spending of kindergarden...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT