0

Ye, but its required by the exercise call the function words(with the parameter filename).

0

Well, i just taught of doing this. The code it was developed here works fine, but it has one catch. It counts numbers. Where can i insert word.isalpha()?

from string import punctuation

def words(filename):
    count = {}
    for aline in filename:
        palavras = ''.join(c for c in aline.lower() if c not in punctuation)
        palavras = palavras.split()
        for word in palavras:
            if word in count:
                count[word]  +=1
            else:
                count[word] = 1
    return count



filename=open("texto.txt")
print(words(filename))
0

I tried and i think its good now.

from string import punctuation

def words(filename):
    count = {}
    for aline in filename:
        palavras = ''.join(c for c in aline.lower() if c not in punctuation if c.isalpha())
        palavras = palavras.split()
        for word in palavras:
            if word in count:
                count[word]  +=1
            else:
                count[word] = 1
    return count



filename=open("texto.txt")
print(words(filename))
0

No it is not: you should be able to call the function like

print(words('texto.txt'))

(and you left in the old condition which was less strict)

Edited by pyTony: n/a

0

Well can you give me an example how to do it? I now what you mean, i can't put it on Python language... Besides, realised that using

from string import punctuation
 
def words(filename):
    count = {}
    for aline in filename:
        palavras = ''.join(c for c in aline.lower() if c not in punctuation if c.isalpha())
        palavras = palavras.split()
        for word in palavras:
            if word in count:
                count[word]  +=1
            else:
                count[word] = 1
    return count
 
 
 
filename=open("texto.txt")
print(words(filename))

That doesn't count numbers has words anymore, BUT it returns a unsplitted string... What is wrong?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.