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
Juliette [100K]
2 years ago
12

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. Also, be sure to remove punctuation and digits.
>>>wordcount('frankenstein.txt')
artifice 1
resting 2
compact 1
service 3

Computers and Technology
1 answer:
o-na [289]2 years ago
4 0

Answer:

I am writing a Python program. Let me know if you want the program in some other programming language.        

import string  #to use string related functions

def wordcount(filename):  # function that takes a text file name as parameter and returns the number of occurrences of every word in file

   file = open(filename, "r")  # open the file in read mode

   wc = dict()  # creates a dictionary

   for sentence in file:  # loop through each line of the file

       sentence = sentence.strip()  #returns the text, removing empty spaces

       sentence=sentence.lower() #converts each line to lowercase to avoid case sensitivity

       sentence = sentence.translate(sentence.maketrans("", "", string.punctuation))  #removes punctuation from every line of the text file

       words = sentence.split(" ")  # split the lines into a list of words

       for word in words:  #loops through each word of the file

           if len(word)>2:  #checks if the length of the word is greater than 2

               if word in wc:  # if the word is already in dictionary

                   wc[word] = wc[word] + 1  #if the word is already present in dict wc then add 1 to the count of that word

               else:  #if the word is not already present

                   wc[word] = 1  # word is added to the wc dict and assign 1 to the count of that word                

   for w in list(wc.keys()):  #prints the list of words and their number of occurrences

       print(w, wc[w])  #prints word: occurrences in key:value format of dict        

wordcount("file.txt") #calls wordcount method and passes name of the file to that method

Explanation:

The program has a function wordcount that takes the name of a text file (filename) as parameter.

open() method is used to open the file in read mode. "r" represents the mode and it means read mode. Then a dictionary is created and named as wc. The first for loop, iterates through each line (sentence) of the text file. strip() method is used to remove extra empty spaces or new line character from each sentence of the file, then each sentence is converted to lower case using lower() method to avoid case sensitivity. Now the words "hello" and "Hello" are treated as the same word.

sentence = sentence.translate(sentence.maketrans("", "", string.punctuation))  statement uses two methods i.e. maketrans() and translate(). maketrans() specifies the punctuation characters that are to be deleted from the sentences and returns a translation table. translate() method uses the table that maketrans() returns in order to replace a character to its mapped character and returns the lines of text file after performing these translations.

Next the split() method is used to break these sentences into a list of words. Second for loop iterates through each word of the text file. As its given to ignore words of length 2 or less, so an IF statement is used to check if the length of word is greater than 2. If this statement evaluates to true then next statement: if word in wc:   is executed which checks if the word is already present in dictionary. If this statement evaluates to true then 1 is added to the count of that word. If the word is not already present  then the word is added to the wc dictionary and 1 s assigned to the count of that word.

Next the words along with their occurrences is printed. The program and its output are attached as screenshot. Since the frankenstein.txt' is not provided so I am using my own text file.

You might be interested in
Why is it important to cite your sources?
emmainna [20.7K]

Answer:

It gives credit to the original author and shows that you are not taking credit for someone

else's work

7 0
3 years ago
Read 2 more answers
NEED HELP FAST timed
Andrej [43]

ton

Answer:

piston - engine book

values- crackshaft rod

head- camshaft

3 0
2 years ago
To a traditional computer, one means
blagie [28]

a.on , off

In most computer processors, electron movement is controlled by tiny switches that turn this flow of electricity on and off...zero represents off and one represents on

4 0
3 years ago
Which of the following is an example of a runtime error?
Rina8888 [55]

Answer:

missing quotation marks around a string literal

6 0
2 years ago
Please answer this question​
RSB [31]

Answer:

what is the other language

is it Assamese clarify me in the comment section bro

4 0
2 years ago
Other questions:
  • Does the steelseries arctis 9x work with a pcie bluetooth card in windows
    13·1 answer
  • The ____________________ packet-filtering firewall allows only a particular packet with a particular source, destination, and po
    10·1 answer
  • Google Ads was designed to deliver three things to every advertiser: relevance, control, and results. It provides relevance by c
    11·1 answer
  • Why is it important to begin development of a software solution with a programming design in place? A programming design summari
    11·2 answers
  • Bulldog Holdings is a U.S.-based consumer electronics company. It owns smaller firms in Japan and Taiwan where most of its cell
    5·1 answer
  • A network administrator recently implemented two caching proxy servers on the network. how can the administrator best aggregate
    5·1 answer
  • In symmetric key cryptosystem, assume that Alice and Bob have set up a common key Kab. This key is only known to Alice and Bob.
    12·1 answer
  • You are a Data Scientist at Anthem Blue Cross Blue Shield. You want to check if a patient will develop diabetes. Please write th
    5·1 answer
  • Web résumés allow you to include extra graphics and images that you would not include in a traditional résumé. please select the
    13·1 answer
  • Which of the following best describes a hotspot as
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!