python统计三国演义人物出场次数
python统计三国演义人物出场次数import jiebaexcludes = {"将军", "却说", "荆州", "二人", "不可", "不能", "如此", "商议", "如何", "主公",
"军士", "左右", "军马", "引兵", "次日", "大喜", "天下", "东吴", "于是", "今日",
"不敢", "魏兵","人马", "陛下", "一人", "不知", "汉中", "只见", "众将"}#排除
txt = open("三国演义.txt", "r", encoding="utf-8").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 = "曹操"
elif word == "公瑾" or word == "周都督":
rword = "周瑜"
else:
rword = word
counts = counts.get(rword, 0) + 1
#如果word在counts中,返回word对应的值,否则返回0
for word in excludes:
del(counts)
items = list(counts.items())
items.sort(key=lambda x:x, reverse=True)
fo = open("三国人物出场次数.txt", "a")
for i in range(10):
word, count=items
word = str(word)
count = str(count)
fo.write(word)
fo.write(' ')
fo.write(count)
fo.write('\n')
print (word, count)
fo.close()
运行后,程序会自动生成 ”三国人物出场次数.txt“ 的文件,这里面就是词频分析后的文档。
把文档导入Excel中(用Excel打开txt文件,选择用","进行分割,这样就能把词语和词频分到2列),然后手动剔除掉无用的助词(在Excel中操作很容易,而且快速)。在接着把同类词归类,比如”孔明“、”诸葛亮“指的都是同一个人,”操“,”曹操“,”丞相“也都一样。
最后,可以把你想要的数据提取出来,用Excel作图。
yaerhuo 发表于 2019-10-5 20:40
好巧,今年二级Python题库有考三国演义的,考曹操出现次数,还考了别的词语统计
几月份的? 词语统计啊Σ(っ °Д °;)っ 好巧,今年二级Python题库有考三国演义的,考曹操出现次数,还考了别的词语统计 jieba库还是棒棒的,让我查询到cut_all和lcut的模式 哇!mark一下,回头自己练习练习,感觉好有趣! 这不是中国大学mooc上的案例吗 这个很有意义 厉害了。。 厉害,膜拜:lol 高手!高手,高高手!