I want to take every word from a text file, and count the word frequency in a dictionary.
Example: 'this is the textfile, and it is used to take words and count'
JavaScript
x
2
1
d = {'this': 1, 'is': 2, 'the': 1, }
2
I am not that far, but I just can’t see how to complete it. My code so far:
JavaScript
1
10
10
1
import sys
2
3
argv = sys.argv[1]
4
data = open(argv)
5
words = data.read()
6
data.close()
7
wordfreq = {}
8
for i in words:
9
#there should be a counter and somehow it must fill the dict.
10
Advertisement
Answer
If you don’t want to use collections.Counter, you can write your own function:
JavaScript
1
16
16
1
import sys
2
3
filename = sys.argv[1]
4
fp = open(filename)
5
data = fp.read()
6
words = data.split()
7
fp.close()
8
9
unwanted_chars = ".,-_ (and so on)"
10
wordfreq = {}
11
for raw_word in words:
12
word = raw_word.strip(unwanted_chars)
13
if word not in wordfreq:
14
wordfreq[word] = 0
15
wordfreq[word] += 1
16
for finer things, look at regular expressions.