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
Svetradugi [14.3K]
3 years ago
5

Write a function wordcount() that takes the name of a text file as input and prints the number of occurrences of every word in t

he file. You function should be case-insensitive so 'Hello' and 'hello' are treated as the same word. You should ignore words of length 2 or less. The results printed will be ordered from the most frequent to the least frequent. Hint: dictionary and list. Test your implementation on file great_expectations.txt
Computers and Technology
1 answer:
artcher [175]3 years ago
4 0

Answer:

Explanation:

The following Python program uses a combination of dictionary, list, regex, and loops to accomplish what was requested. The function takes a file name as input, reads the file, and saves the individual words in a list. Then it loops through the list, adding each word into a dictionary with the number of times it appears. If the word is already in the dictionary it adds 1 to its count value. The program was tested with a file named great_expectations.txt and the output can be seen below.

import re

def wordCount(fileName):

   file = open(fileName, 'r')

   wordList = file.read().lower()

   wordList = re.split('\s', wordList)

   wordDict = {}

   for word in wordList:

       if word in wordDict:

           wordDict[word] = wordDict.get(word) + 1

       else:

           wordDict[word] = 1

   print(wordDict)

wordCount('great_expectations.txt')

You might be interested in
What allows a programmer to write code quickly and efficiently for an action that must be repeated?
Marta_Voda [28]
An literation :))!! i hope u get it right lol
6 0
2 years ago
Read 2 more answers
Task 3&amp;4, please <br> with the formula
Anastasy [175]
It’s 12 because 3 times 4 is twelve, hope i helped!
6 0
2 years ago
Does You tube have a You tube channel? and does Netflix have to pay for their own account???
defon

I don't understand the You  tube part, but uh yeah???

3 0
3 years ago
Read 2 more answers
The diagram shows the parts of a computer. In which part is data processing done?
notsponge [240]
  • control unit mainly processes tasks and most resources are managed here in the computer system.

  • central processing unit, ( CPU ) where most of the data processing is done and executes instructions.
3 0
2 years ago
_______self-esteem can be a factor for individuals who are dependent on alcohol or other drugs.
Tatiana [17]

low becuase you are loosing it

6 0
3 years ago
Other questions:
  • In the 1960s, techniques were developed that allowed individuals to fool the phone system into providing free access to long dis
    14·1 answer
  • Greg works for online games development company. He occasionally visits online literature sites and downloads e-books of his cho
    12·2 answers
  • You have just installed a new sound card in your system, and Windows says the card installed with no errors. When you plug up th
    5·1 answer
  • Express 0.0005 x 10-4 farads as picofarads
    5·2 answers
  • The purpose of this assignment is to determine a set of test cases knowing only the function’s specifications, reported hereaf
    12·1 answer
  • Consider the following method:
    14·1 answer
  • Examples of application software​
    7·2 answers
  • The most common clefs for high notes are what?
    6·2 answers
  • Translate the following pseudocode for randomly permuting the characters in a string into a C++ program.
    15·1 answer
  • Which of the following is the file type of Microsoft® Publisher files?
    12·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!