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]
3 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]3 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
Diferencia entre tipo de datos simple char y compuesto cadena​
fomenos
Man i do not speak spanish L
3 0
3 years ago
Jack has determined that a virus has infiltrated his computer and corrupted several of his data files. Which two utilities would
Romashka [77]
Antivirus programs to detect and eliminate viruses such as Malwarebytes, Stinger, etc.

A hard drive to backup all of his remaining files.

Corrupted files are not always possible to recover, but it's worth a try using a software such as Recuva.
7 0
3 years ago
Read 2 more answers
In Black friday 2019 how much percentage of a cut did asus or msi did on gaming laptops did they do 50% off or?
astraxan [27]

Answer:

yes they do 50% off

Explanation:

7 0
3 years ago
Given a following C program, where M and N are constant declared with #define long P[M][N]; long Q[N][M]; long sum_element(long
sladkih [1.3K]

Answer:

Check the explanation

Explanation:

The first, second and third instruction together set %rdx = 3*%rdi + %rsi. The fourth and fifth instruction set %rdi = %rdi+9*%rsi.

Now %rdi is used to address Q, while %rdx is used to address P. Note that P is addressed by column and then by row. Hence M = 3 . Also Q is addressed by row and then column, hence N = 9 .

3 0
4 years ago
Which of the following binary (base-2) numbers is LARGEST?
Lemur [1.5K]

Answer:

A. 11000000

Explanation:

A. 11000000 is 192

B. 01111111 is 127

C. 00000001 is 1

D. 10111111 is 191

Therefore, making A the largest.

7 0
3 years ago
Other questions:
  • Plz answer and dont put a random thing for the points
    11·1 answer
  • A public key is part of what security measure?
    7·1 answer
  • Why aren’t white Wall auto tires as wide as they used to be?or why do most cars not have them at all?
    15·1 answer
  • Write a SELECT statement that returns these column names and data from the Order_Items table:
    14·1 answer
  • Consider having three String variables a, b, and c. The statement c = a + b; also can be achieved by saying_____________.a. c =
    11·1 answer
  • What's it called when a program is expandable?
    10·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
  • Some students are studying the amount of pollution in a river They are using a computer to measure the pollution level using sen
    14·1 answer
  • What does a class do?
    10·1 answer
  • ____ takes care of the transport between machines. but it is unreliable, and there is no guarantee that any given packet will ar
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!