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
Which actions can be performed on tables in Word? Check all that apply.
tigry1 [53]
✓ adding rows
✓ adding columns
✓ deleting rows
✓ deleting columns
✓ changing column width
✓ aligning tables
✓ converting tables to text
✓ converting tables to graphs

Believe it or not, all of these actions are possible in MS Word.
3 0
4 years ago
Read 2 more answers
What should you consider when looking at houses to buy? A. Location B. Size and condition of the yard C. Size, features, and con
Margaret [11]

Answer:

A

Explanation:   Would you like to buy a house in the desert

8 0
3 years ago
Read 2 more answers
There was a software crisis in 1968, as result of programming abilities, due to the invention of integrated circuits (chips).A.
lorasvet [3.4K]

Answer:

The correct answer to the following question will be "False".

Explanation:

Integrated Circuits: An integrated circuit, is a compact chip capable of acting as an amplifier, oscillator, timer, microprocessor, or even memory device.

The past of computing clusters beginning in 1960 is marked by the transformation from the vacuum chamber to solid-state systems such as transistors and integrated circuit (IC) chips afterward. By 1959, discrete components were considered accurate and economical enough to render additional vacuum tube computers noncompetitive.

Therefore, the given statement is false.

8 0
3 years ago
The goal of an audio codec is
Sloan [31]

If you are looking for what is used for, an audio codec is used for the compression or decompression of digital audio data from a live stream media or an already stored data file.

If you are looking for the purpose, the purpose of using an audio codec is to effectively reduce the size of an audio file without affecting the quality of the sound.

I really hope that this helps you with your question.

7 0
4 years ago
Supervisor: You will need to consolidate your trouble tickets
liubo4ka [24]
Wheres the rest???????????????????
7 0
3 years ago
Other questions:
  • The move up only one line in Microsoft Word, use the following method: (a) press Ctrl + Home Keys (b) Press Home Keys (c) Press
    10·1 answer
  • identify three of the many shared ethical standards among businesses that are incorporated in codes of ethics
    6·2 answers
  • Assume that an array named a containing exactly 5 integers has been declared and initialized. Write a single statement that adds
    6·1 answer
  • What is the name of the computer through which e-mail messages are sent and received?
    12·1 answer
  • If a computer file will not open, what should you do? A. Make sure you have the correct software to open it. B. Change the name
    9·1 answer
  • Which of the following involves making a reasoned analysis of an opportunity, envisioning potential solutions, evaluating those
    5·1 answer
  • Question 21 pts How many lines should an email signature be? Group of answer choices "5 to 6" "7 to 8" "1 to 2" "3 to 4"
    10·1 answer
  • Why would a team choose to employ a zone defense over a person to person defense?
    8·1 answer
  • Which of the following events happened first
    15·2 answers
  • What data type is this statement defined as in Python?
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!