Sorted Word frequency count using python

前端 未结 10 903
再見小時候
再見小時候 2020-11-28 06:00

I have to count the word frequency in a text using python. I thought of keeping words in a dictionary and having a count for each of these words.

Now if I have to so

10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 06:25

    I have just wrote a similar program, with the help of Stack Overflow guys:

    from string import punctuation
    from operator import itemgetter
    
    N = 100
    words = {}
    
    words_gen = (word.strip(punctuation).lower() for line in open("poi_run.txt")
                                                 for word in line.split())
    
    for word in words_gen:
        words[word] = words.get(word, 0) + 1
    
    top_words = sorted(words.items(), key=itemgetter(1), reverse=True)[:N]
    
    for word, frequency in top_words:
        print ("%s %d" % (word, frequency))
    

提交回复
热议问题