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
I just downloaded this song and if any one wants to listen to it then go ahead and do that. It’s called Trap Music 2018 âa bass
xz_007 [3.2K]

Answer:

ok

Explanation:

4 0
3 years ago
Read 2 more answers
Write 'T' for true and 'F' for false statements.
asambeis [7]
1-F
2-T
3-F
4-F
5-F
6 T
7-T
3 0
2 years ago
Write a small program basic c++, that defines a negative integer (between ‐1 and ‐255), converts it to a positive value and then
ANEK [815]

Answer:

#include <iostream>

using namespace std;

int main() {

int a=-156;//negative integer between -1 and -255.

a*=-1;//multiplying a to -1 so that it can become positive.

cout<<a;//printing a.

return 0;

}

Explanation:

The above written program is in C++ and in the program an integer a is defined with a negative value in the program it is -156.Then to convert it to positive integer we have to multiply a to -1 after that printing the value of a on the screen.

4 0
3 years ago
To ensure proper seating of the valve, the valve seat must be
Marta_Voda [28]
The vaule must be set true not false
5 0
3 years ago
Fill in the blank: when you execute code in the source editor, the code automatically also appears in the___ a. Files tab b. Plo
trapecia [35]

When you execute the code in source editor, then the code automatically also appears in the <u>R console</u>. Hence, option 'C' is the right answer.

<h3>What is source editor?</h3>

A text editor programme called a source-code editor can be used to edit the source code of computer programmes. It could be a standalone application, one that is incorporated into an IDE, or a web browser.

A source-code editor is a fundamental piece of programming hardware because writing and editing source code is a fundamental task for programmers.

A few of the features that source-code editors have are brace matching functionality, autocomplete, syntax highlighting, and indentation. These features are designed to make typing source code simpler and faster. These editors also provide a simple way for users to start a compiler, interpreter, debuger, or other software-development-related programme.

Learn more about source code

brainly.com/question/29661963

#SPJ1

5 0
1 year ago
Other questions:
  • According to Mintzberg's classification of managerial roles, the role of a(n) ______ is to transmit information received from ou
    7·1 answer
  • Look up and list the number of a local taxi or car service in your community. Include the company name and telephone number.
    13·1 answer
  • From an IT perspective, which of the following best describes BI and BI apps?
    13·1 answer
  • The OSI model is currently the most widely implemented network model used to develop and build networks of any size, including t
    8·1 answer
  • Someone asks you for help with a computer that hangs at odd times. You turn it on and work for about 15 minutes, and then the co
    7·1 answer
  • What happens to a mechanical system when two opposing torques do not cancel out?
    7·1 answer
  • The use of desktop computer equipment and software to create high-quality documents such as newsletters, business cards, letterh
    14·1 answer
  • Que es tarjeta madre resumen porfa
    13·2 answers
  • Can someone give me an earsketch song of summer to turn in
    6·1 answer
  • Define foreign employment​
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!