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...
Python: Write a program that asks the user for the name of a file. The program...
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
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 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...
Problem: Write a Python module (a text file containing valid Python code) named p5.py. This file...
Problem: Write a Python module (a text file containing valid Python code) named p5.py. This file must satisfy the following. Define a function named rinsert. This function will accept two arguments, the first a list of items to be sorted and the second an integer value in the range 0 to the length of the list, minus 1. This function shall insert the element corresponding to the second parameter into the presumably sorted list from position 0 to one less...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT