如何用python和jieba分词,统计词频?

2025-03-30 20:15:42
推荐回答(2个)
回答1:

 #! python3
# -*- coding: utf-8 -*-
import os, codecs
import jieba
from collections import Counter
 
def get_words(txt):
    seg_list = jieba.cut(txt)
    c = Counter()
    for x in seg_list:
        if len(x)>1 and x != '\r\n':
            c[x] += 1
    print('常用词频度统计结果')
    for (k,v) in c.most_common(100):
        print('%s%s %s  %d' % ('  '*(5-len(k)), k, '*'*int(v/3), v))
 
if __name__ == '__main__':
    with codecs.open('19d.txt', 'r', 'utf8') as f:
        txt = f.read()
    get_words(txt)

回答2:

https://github.com/williezh/