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
Typing which capitals and exclamation points in an email is an example of
Alexus [3.1K]
It's an example of a poorly written email, it looks like somebody is angry and yelling at you, these types of emails can be described as a poor etiquette.
4 0
4 years ago
What helps in determining the reliability of a person to repay debt?
drek231 [11]
C) credit score is your answer
6 0
3 years ago
Read 2 more answers
What method of research deals with large amounts of information that is analyzed and presented statistically?
Alenkinab [10]
Method of research deals with large amounts of information that is analyzed and presented statistically is quantitate.
6 0
3 years ago
How do i open screenshots on school chromebook?
yuradex [85]

Answer:

you have to open your file explorer and open the screenshot folder.

Explanation:

3 0
2 years ago
Read 2 more answers
Find the volume of the rectangular prism.<br>1<br>6 cm<br>32<br>cm​
Assoli18 [71]

Answer:

192 centimeters.

Explanation:

V= length x width x height

6 0
3 years ago
Other questions:
  • List and describe the issues that lead to making a successful website?​
    9·1 answer
  • You must obey any special signs or instructions as you pass a construction zone.
    14·2 answers
  • We cannot create a blank table true or false​
    8·1 answer
  • Explain default dictionary in microsoft word​
    9·1 answer
  • DRU is a small brokerage house that enables its clients to buy and sell stocks over the Internet, as well as place traditional o
    14·1 answer
  • Will mark brainliest
    14·1 answer
  • Which statement is true concerning user-defined data types?
    10·1 answer
  • _____ have networked and texted all of their lives with smartphones, tablets, and high-speed Internet.
    8·1 answer
  • Refer to Table 8-4. Consider the data above (in billions of dollars) for an economy: Gross domestic product (in billions of doll
    12·1 answer
  • onsider a file system on a disk that has both logical and physical block sizes of 512 bytes. assume that the information about e
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!