本文来源吾爱破解论坛
python统计三国演义人物出场次数[Python] 纯文本查看 复制代码
import jieba excludes = {"将军", "却说", "荆州", "二人", "不可", "不能", "如此", "商议", "如何", "主公", "军士", "左右", "军马", "引兵", "次日", "大喜", "天下", "东吴", "于是", "今日", "不敢", "魏兵","人马", "陛下", "一人", "不知", "汉中", "只见", "众将"} #排除 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[rword] = counts.get(rword, 0) + 1 #如果word在counts中,返回word对应的值,否则返回0 for word in excludes: del(counts[word]) items = list(counts.items()) items.sort(key=lambda x:x[1], reverse=True) fo = open("三国人物出场次数.txt", "a") for i in range(10): word, count=items[i] 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作图。
2019-10-05_192537.png (39.56 KB, 下载次数: 0)
下载附件 保存到相册
三国人物出场次数.png (27.98 KB, 下载次数: 1)
下载附件 保存到相册
版权声明:
本站所有资源均为站长或网友整理自互联网或站长购买自互联网,站长无法分辨资源版权出自何处,所以不承担任何版权以及其他问题带来的法律责任,如有侵权或者其他问题请联系站长删除!站长QQ754403226 谢谢。