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
Which of the following is the most effective password?
zzz [600]
I would have to say that the M3(ury would be the MOST effective, because it has more capitals, numbers, and symbols, which would make it harder to hack into.
5 0
3 years ago
Read 2 more answers
explain the impact of effectively understanding the various formatting options available in the word processing software applica
Vera_Pavlovna [14]

The formatting tool is very important. When one effectively understands the various formatting options available in the word processing software application, one can be able to;

  • Make more accessible options for readers through creating and use of headings, highlighting key words or ideas etc.

  • Formatting any document helps one to have a  presentable and professional document.

  • It makes the document easier and a lot interesting to read.

  • It helps in Proper punctuation marks and spelling usefulness.

<h3>What is formatting in MS Word?</h3>

Formatting text is simply known as the act of controlling how one wants a particular text to appears in your document. This includes the control of the size, color, etc.

Learn more about word processing software  from

brainly.com/question/1022352

8 0
1 year ago
when connecting to a wireless local area network (wlan), what is the most important fact about the service set identifier (ssid)
iragen [17]

The most important fact about the service set identifier (SSID) is that all the end users and all access points (AP) must be configured with matching SSIDs.

<h3>What is SSID?</h3>

SSID is an acronym for service set identifier and it can be defined as the generic name given to a wireless local area network (WLAN) connection.

In Computer networking, the most important fact about the service set identifier (SSID) is that all the end users and all access points (AP) must be configured with matching SSIDs.

Read more on wireless local area network here: brainly.com/question/13697151

#SPJ11

4 0
1 year ago
There has been something I had to ask but I have to type this so it goes through: creeper?
VMariaS [17]

Answer:

<u><em>awwwwwwwwwwwwwwwwwww man</em></u>

Explanation:

So we back in the mine

Got our pickaxe swinging from side to side

Side-side to side

This task, a grueling one

Hope to find some diamonds tonight, night, night

Diamonds tonight

Heads up

You hear a sound, turn around and look up

Total shock fills your body

Oh, no, it's you again

I can never forget those eyes, eyes, eyes

Eyes-eye-eyes

'Cause, baby, tonight

The creeper's tryna steal all our stuff again

'Cause, baby, tonight

You grab your pick, shovel, and bolt again (Bolt again-gain)

And run, run until it's done, done

Until the sun comes up in the morn'

'Cause, baby, tonight

The creeper's tryna steal all our stuff again (Stuff again-gain)

Just when you think you're safe

Overhear some hissing from right behind

Right-right behind

That's a nice life you have

Shame it's gotta end at this time, time, time

Time-time-time-time

[Pre-Chorus: TryHardNinja]

Blows up

Then your health bar drops and you could use a one-up

Get inside, don't be tardy

So, now you're stuck in there

Half a heart is left, but don't die, die, die

Die-die-die

'Cause, baby, tonight

The creeper's tryna steal all our stuff again

'Cause, baby, tonight

You grab your pick, shovel, and bolt again (Bolt again-gain)

And run, run until it's done, done

Until the sun comes up in the morn'

'Cause, baby, tonight

The creeper's tryna steal all our stuff again

(Creepers, you're mine, haha)

Dig up diamonds and craft those diamonds

And make some armor, get it, baby

Go and forge that like you so MLG pro

The sword's made of diamonds, so come at me, bro, huh

Training in your room under the torchlight

Hone that form to get you ready for the big fight

Every single day and the whole night

Creeper's out prowlin', hoo, alright

Look at me, look at you

Take my revenge, that's what I'm gonna do

I'm a warrior, baby, what else is new?

And my blade's gonna tear through you, bring it

'Cause, baby, tonight

The creeper's tryna steal all our stuff again

(Gather your stuff, yeah, let's take back the world)

Yeah, baby, tonight (Haha)

Grab your sword, armor and go (It's on)

Take your revenge (Woo), oh-oh, oh-oh

So fight, fight, like it's the last, last night

Of your life, life, show them your bite (Woo)

'Cause, baby, tonight

The creeper's tryna steal all our stuff again

'Cause, baby, tonight

You grab your pick, shovel and bolt again (Bolt again-gain, woo)

And run, run until it's done, done

Until the sun comes up in the morn'

'Cause, baby, tonight (Come on, swing your sword up high)

The creeper's tryna steal all our stuff again (Come on, jab your sword down low)

(Woo)

3 0
3 years ago
To light up an LED, you need to connect the cathode leg to one of the Digital Pins and connect the Anode leg to the ……………. on th
Elenna [48]

Answer bobobobobobobobo

4 0
2 years ago
Other questions:
  • How do type declaration statements for simple variables affect the readability of a language, considering that some languages do
    10·1 answer
  • Omar wants to add transitions to his presentation, so he clicks the Transitions tab. Which tasks can he now complete? Check all
    10·1 answer
  • If you want to present slides to fellow students or co-workers,which productivity software should you use to create them?
    8·2 answers
  • Why did artists use pinhole cameras during the renaissance?
    8·1 answer
  • Why coaxil cable called coxial cable ?
    15·2 answers
  • What is computer ? What is computer program? What is hard disk.
    12·2 answers
  • What does the binary odometer show about representing large numbers​
    15·1 answer
  • What time is spellrd the same forwards and backwards​
    11·1 answer
  • 100+20000000 please give answer and win iPhone 11 pro​
    15·1 answer
  • How to run angular project from github.
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!