python中jieba库筛选高频词语

安装jieba库
pip install jieba
如果失败提示权限问题输入:sudo su

#-*-coding:UTF-8-*-
import json
import  jieba

txt = open("data.txt", "r").read()
words = jieba.lcut(txt)     # 使用精确模式对文本进行分词
counts = {}     # 通过键值对的形式存储词语及其出现的次数

for word in words:
    if  len(word) == 1:    # 单个词语不计算在内
        continue
    else:
        counts[word] = counts.get(word, 0) + 1    # 遍历所有词语,每出现一次其对应的值加 1
        
items = list(counts.items())#将键值对转换成列表
items.sort(key=lambda x: x[1], reverse=True)    # 根据词语出现的次数进行从大到小排序
ite=json.dumps(items,ensure_ascii=False)
print ite
for i in range(15):
    word, count = items[i]
    print word, count

转载地址:https://www.cnblogs.com/wkfvawl/p/9487165.html

猜你喜欢

转载自blog.csdn.net/qq_40286424/article/details/85448750