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
The difference between Return key and word Wrap
saveliy_v [14]
The enter key is used when we want to go directly to the next line while writing whereas through word wrap the cursor automatically shifts to the next line when the word limit for a particular line is exceeded
3 0
2 years ago
By default, PowerPoint ends a slide show with a black slide. <br> a. True<br> b. False
Dmitry [639]
True . Because Press " ESC " To Exit Off Of Slide . 
5 0
3 years ago
Read 2 more answers
Choose the type of collection created with each assignment statement
Leviafan [203]

Answer:

dictionary

tuple

list

Explanation:

4 0
2 years ago
TCO 10) Voice packets could experience a significant _____ delay in routers and switches when sharing a network with data traffi
bonufazy [111]

This delay in routers is what we called Packetization delay or also called Accumulation delay. Packetization delay is the time required for the information to pass on the wires. The data rate of the links that passes thru the wires cause the delay.

8 0
2 years ago
Write a program that prints the U.S. presidential election years from 1792 to present day, knowing that such elections occur eve
Naddika [18.5K]
Year = 1972
current year = 2021

while year <= current year:
print (year)
year = year + 4
8 0
2 years ago
Other questions:
  • What do you click to create a new presentation in Normal view? A. Section B. Blank Presentation C. Layout D. New Slide
    14·2 answers
  • Typically, a dvd has how many times more capacity than a cd?
    5·1 answer
  • In modern computer memory, each location is normally composed of one byte.
    7·1 answer
  • The simplest method to copy information is to first select the information you want to copy, and then use the Copy button and th
    11·1 answer
  • Where is permanent data in the computer stored whenever gym starts his laptop He sees some commands in numbers appearing on the
    12·1 answer
  • Edie wants to visit her university's website. What software application should she use?
    9·2 answers
  • Explain how the CPU processes data instructions.
    6·1 answer
  • PLZ HELP ASAP
    13·1 answer
  • Privacy, anonymity, and freedom of expression are all interrelated.
    12·1 answer
  • Ginny faced an application error while executing the recorder in opera. Which web browser is generally recommended to use with r
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!