借助collections模块实现词频及常用词频度统计。
#!/usr/bin/python3
# _*_ coding:utf-8 _*_
import sys
import jieba
from collections import Counter
with open("./downfile/file7.txt", 'r') as f:
text = f.read()
f.close()
print(text)
txt = jieba.lcut(text)
c = Counter()
for w in txt:
if len(w) > 1 and w != '\r\n':
c[w] = c[w] + 1
for key in c:
print(key, c[key])
print('常用词频度统计结果:')
for (k, v) in c.most_common(5):
print(k, v)