Python - Finding word frequencies of list of words in text file

后端 未结 4 1028
渐次进展
渐次进展 2020-12-01 12:46

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

4条回答
  •  情书的邮戳
    2020-12-01 13:23

    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
    }
    

提交回复
热议问题