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
What does a for loop look for in the sequence of data? Check all that apply.
ValentinkaMS [17]

Answer:

All

Explanation:

Not sure on the context of this question, but a loop is generally used to iterate though every piece of data to perform some kind of operation, on everything in something like an array or string.

7 0
2 years ago
Read 2 more answers
Assume a future where security counter measures against DoS attacks are much more widely implemented than at present. In this fu
Amiraneli [1.4K]

Answer:

yes administrators have to be concerned

Explanation:

It is necessary for countermeasures to be taken against DoS attacks

The types of attacks that can still occur are:

If the network connection get to be overloaded,  there could be flash crown of this computer system and fraudulent activities maybe initiated. By flash crowd  we mean that the there is a great deal of traffic on the system and this could cause the network connection to be destroyed.

To reduce the impact of this kind of attacks,

1. there should be enough or excess network bandwidth  and distributed servers should be replicated if there is a possibility that network could get to be overloaded.

2. restriction is more commonly done on sporting sites. Restriction brings about an increase in implementation cost.

3. The impact of this type of attack can be reduced by stopping unwanted traffic throught the implementation of filters

4. Use strong antivirus on computer sytems that are used to connect to the internet

7 0
3 years ago
Suppose you are using a Mac to read your e-mail messages, and you receive an
Gekata [30.6K]

Answer:

D

Explanation:

4 0
3 years ago
Read 2 more answers
A certain manager makes the following statement "Our internet company is in business for the money, making profits, and making t
julia-pushkina [17]

Answer:

d. Stockholder theory

Explanation:

The theory of maximising profits

8 0
3 years ago
Which drugs are known to distort the perception of reality and thought processes
andrey2020 [161]
<span>The correct answer here is Hallucinogen.

Technically all drugs one way or another distort the perception of reality and thought processes but hallucinogens are especially potent in that regard. Hallucinogens are those agents that can cause, among other things, hallucinations and changes in consciousness, emotions and thoughts. LSD is maybe the most known representative of the hallucinogens. </span>
7 0
3 years ago
Other questions:
  • Which transformation(s) can be used to map ARST onto
    8·1 answer
  • Wordwrap forces you to stop typing words and press the enter key at the end of each line.
    12·1 answer
  • How many host ip addresses are available on a network with a subnet mask of 255.255.255.192?
    6·1 answer
  • Which of the following is a reason why the comments tool within PDF documents could be helpful? (Select all that apply.)
    5·1 answer
  • Which of the following could be defined as a general-purpose computing device that enables workers to create, manage, store, sea
    12·1 answer
  • Which type of computer operating system would be best for a large corporation?
    7·2 answers
  • An optimal solution to a linear programming problem MUST lie A. somewere on the line between two corner points. B. somewhere out
    12·1 answer
  • **NEED HELP??!! Computer Science Questions!! KNOWING GIMP!!!!
    7·1 answer
  • What is the simplest form of backing up data?
    10·1 answer
  • Budgeting for a Computer
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!