运用结巴库分析三国演义的人物出场次数
Posted hayhong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了运用结巴库分析三国演义的人物出场次数相关的知识,希望对你有一定的参考价值。
import jieba txt = open("D:三国演义.txt", "r", encoding=‘ANSI‘).read() words = jieba.lcut(txt) # 使用精确模式对文本进行分词 counts = {} # 通过键值对的形式存储词语及其出现的次数 for word in words: if len(word) == 1: # 单个词语不计算在内 continue elif word == "诸葛亮" or word == "孔明曰": #把相同的人合在一起 rword = "孔明" elif word == "关公" or word == "云长": rword = "关羽" elif word == "玄德" or word == "玄德曰": rword = "刘备" elif word == "孟德" or word == "丞相": rword = "曹操" else: counts[word] = counts.get(word, 0) + 1 # 遍历所有词语,每出现一次其对应的值加 1 excludes = {"将军","却说","荆州","二人","不可","不能","如此","商议","如何","主公","军士"} #找出不是人物的词语 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:<5}{1:>5}".format(word, count))
以上是关于运用结巴库分析三国演义的人物出场次数的主要内容,如果未能解决你的问题,请参考以下文章