I am trying to speed up my project to count word frequencies. I have 360+ text files, and I need to get the total number of words and the number of times each word from anot
A simple functional code to count word frequencies in a text file:
{
import string
def process_file(filename):
hist = dict()
f = open(filename,'rb')
for line in f:
process_line(line,hist)
return hist
def process_line(line,hist):
line = line.replace('-','.')
for word in line.split():
word = word.strip(string.punctuation + string.whitespace)
word.lower()
hist[word] = hist.get(word,0)+1
hist = process_file(filename)
print hist
}