1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Vinil7 [7]
3 years ago
10

Write a program that reads a book (a file in text format), breaks each line into words, strips whitespace and punctuation from t

he words, and converts them to lowercase. Count the total number of the words, the number of times each word is used, and print out the top 10 most frequently used words.

Computers and Technology
1 answer:
diamong [38]3 years ago
4 0

Answer:

Following are the code to this question:

file= open('book.txt') #open file

li= {} #define an empty list

for lines in file: # use for loop Split file data into words

   d= lines.lower().strip(' !?').split() #define variable d that Add it to map

   for val in d: #define loop to store data  

       if val not in li: #check value is not in list

           li[val] = 0 #define list and assign value in 0

       li[val] = li[val] + 1 #Sort the book data and add its value  

m = sorted(li.items(),key = lambda x : -x[1]) #sorted value into the m variable  

print(m[:10]) #print value

Output:

please find the attachment.

Explanation:

In the given python code first, we open the file "book.txt", in next line, an empty list is defined, that uses the for loop which can be described as follows:

  • In the for loop is used, that reads the file data, and defines a variable "d", that stores the values into the map.
  • In the next line another loop is used, that check file values, if values are the same type so, it adds values and writes it.  
  • In the last line, m variable is used, that sorts the values and use the slicing to print its value.

You might be interested in
Does anyone have any social media message me
Mademuasel [1]
What's your username on them?
5 0
3 years ago
The italic button is located on the
SashulF [63]
It is located on Mini Tool Bar 
6 0
3 years ago
You must have an active ____ to test external links.
charle [14.2K]
Network & internet connection
4 0
3 years ago
Which of the following is an advantage of using variables?
GaryK [48]

Answer:

D. It makes it less likely that an error will be introduced into the code.

Explanation:

7 0
3 years ago
How many nibbles make one kilobyte​
Kazeer [188]
2000 nibbles I think correct me if I’m wrong
5 0
3 years ago
Read 2 more answers
Other questions:
  • Which of the following is used to encrypt web application data?
    11·1 answer
  • Universal Containers is implementing a community of High-Volume Community users. Community users should be able to see records a
    12·1 answer
  • You want to join your computer to a homegroup but you don't see any homegroups on your home network
    11·1 answer
  • What error occurs when you perform an illegal math operation such as divide by zero
    13·1 answer
  • .in the array based list implementation of the ADT what is the worst case time efficiency of the remove method?
    6·1 answer
  • Answer the following questions: • What is the source of the user’s request? Can a technical solution solve his problem? Perhaps
    10·1 answer
  • To ensure that comments identify the initials of the person making changes, you would need to _____.
    5·2 answers
  • Supp guees how your dayyyyyyyyyyyy
    10·2 answers
  • Which of these is a type of social engineering attack?
    10·1 answer
  • Differenciate between foreign key and primary key in database.
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!