失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Python统计小说中出场人物词频

Python统计小说中出场人物词频

时间:2020-10-31 20:28:55

相关推荐

Python统计小说中出场人物词频

# 选择喜欢的小说,统计出场人物词频排名

import jieba

excludes = {"人马","都督","后主","军马","主公", "孔明曰","左右","东吴",\

"于是","知道","众将","大喜","二人", "玄德曰","天下",\

"军士","引兵","陛下","次日", "丞相","如此","商议","魏兵",\

"只见","今日","却说","不是", "将军","不可","不能","荆州",\

"不知","这个","如何","一人","汉中","蜀兵","不敢","大叫",}

f = open("三国演义.txt", "r")

txt = f.read()

f.close()

words = jieba.lcut(txt)

counts = {}

for word in words:

if len(word) == 1: #排除单个字符的分词结果

continue

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))

如果觉得《Python统计小说中出场人物词频》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。