Python-jieba库的应用举例

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/AI_JOKER/article/details/89060050

三国演义人物出场词频的统计,本文以第一章内容为例
demo如下:

import jieba
txt=open("threekindoms.txt","r",encoding='utf-8').read()
excludes={"天下","黄巾","一名","姓名","叔父","三人","大喜","颍川","英雄","身长"}#去除频率较高的名词
words=jieba.lcut(txt)#将文件转换成列表类型
counts={}
for word in words:
    if len(word)==1:
        continue
    elif word=="玄德曰":
        reword="玄德"
    else:
        counts[word]=counts.get(word,0)+1
for word in excludes:
    del counts[word]
items=list(counts.items())#字典类型转换成列表
items.sort(key=lambda x:x[1],reverse=True)#排序
for i in range(15):  #对前十五位出现次数最多的单词进行打印
    word,count=items[i]
    print("{0:<10}{1:>5}".format(word,count))

猜你喜欢

转载自blog.csdn.net/AI_JOKER/article/details/89060050