Processing of text [duplicate]

0

I have the following scenario: I have a variable with a text inside. I separate this text by words inside an array , with this I need to validate how many times each word appears in the text. I managed to do it, but there is another detail, I need to run the entire list without going through a word that I have already got the count.

Ex: "In a nest of mafagafos there are seven mafagafinhos." When the mafagafa gafa, they gafam the seven mafagafinhos. "

In this case I can not pass the code without repeating the "mafagafinhos". What do I do?

    
asked by anonymous 10.06.2017 / 00:57

1 answer

1

An example word counter could be done as follows:

import sys
text = "Num ninho de mafagafos há sete mafagafinhos. Quando a mafagafa gafa, gafam os sete mafagafinhos."
wordcount={}
for word in text.split():
    if word not in wordcount:
        wordcount[word] = 1
    else:
        wordcount[word] += 1
for key in wordcount.keys():
    print("%s %s " %(key , wordcount[key]))
    
10.06.2017 / 01:08