Python文本词频统计(对三国演义进行人物出场频率的统计)

jieba:优秀的中文分词第三方库
ThreeKingdoms.txt(三国演义.txt):https://python123.io/resources/pye/threekingdoms.txt

代码1:
# CalThreeKingdomsV1.py
import jieba
txt = open("ThreeKingdoms.txt", encoding="utf-8").read()    # 打开文件
words = jieba.lcut(txt)                                     # 分词
counts = {}                                                 # 建字典
for word in words:
    if len(word) == 1:
        continue
    else:
        counts[word] = counts.get(word, 0) + 1              # 判断名字是否在字典中存在,存在则值加1,否则值为1
items = list(counts.items())                                # 列表化
items.sort(key=lambda x: x[1], reverse=True)                 # 排序,默认从小到大,reverse反序输出
for i in range(20):                                         # 输出前20名的词
    word, count = items[i]
    print("{0:<10}{1:>5}".format(word, count))
结果:
曹操          953
孔明          836
将军          772
却说          656
玄德          585
关公          510
丞相          491
二人          469
不可          440
荆州          425
玄德曰         390
孔明曰         390
不能          384
如此          378
张飞          358
商议          344
如何          338
主公          331
军士          317
吕布          300

Process finished with exit code 0

很明显输出的结果中有些不是人名,需要去掉;还有一些名字指的是同一个人,需要合并,所以就有了

代码2:
# CalThreeKingdomsV2.py
import jieba
txt = open("ThreeKingdoms.txt", encoding="utf-8").read()
excludes = {"将军", "却说", "荆州", "二人", "不可", "不能", "如此", "商议", "如何", "军士", "主公", "左右", "军马",
            "次日", "引兵", "大喜"}     # 多次运行代码,找出那些不是人物名的词
words = jieba.lcut(txt)                               # 分词
counts = {}                                           # 建字典
for word in words:
    if len(word) == 1:
        continue
    elif word == "诸葛亮" or word == "孔明曰":        # 合并同一个人的名词
        r_word = "孔明"
    elif word == "关公" or word == "云长":
        r_word = "关羽"
    elif word == "玄德" or word == "玄德曰":
        r_word = "刘备"
    elif word == "孟德" or word == "丞相":           # 暂且先认为所有的“丞相”都是指“曹操”
        r_word = "曹操"
    else:
        r_word = word
    counts[r_word] = counts.get(r_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(8):                                 # 输出前8名
    word, count = items[i]
    print("{0:<10}{1:>5}".format(word, count))
结果:
曹操         1451
孔明         1383
刘备         1252
关羽          784
张飞          358
吕布          300
赵云          278
孙权          264

Process finished with exit code 0
  • 12
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
好的,我可以帮你用Python实现文本词频统计、Hamlet英文词频统计以及《三国演义人物出场统计。你可以先将文本文件保存到本地,并确保已经安装好Python环境。 下面是一些示例代码,你可以参考一下: ### 文本词频统计 ```python import collections # 读取文本文件 with open('your_file_path.txt', 'r') as f: text = f.read() # 清洗文本 text = text.lower() text = text.replace('\n', ' ') text = ''.join(e for e in text if e.isalnum() or e.isspace()) # 统计词频 word_counts = collections.Counter(text.split()) # 输出前10个高频词 print(word_counts.most_common(10)) ``` ### Hamlet英文词频统计 ```python import collections # 读取Hamlet文本文件 with open('hamlet.txt', 'r') as f: text = f.read() # 清洗文本 text = text.lower() text = text.replace('\n', ' ') text = ''.join(e for e in text if e.isalnum() or e.isspace()) # 统计词频 word_counts = collections.Counter(text.split()) # 输出前10个高频词 print(word_counts.most_common(10)) ``` ### 《三国演义人物出场统计 ```python import re import collections # 读取《三国演义文本文件 with open('three_kingdoms.txt', 'r', encoding='utf-8') as f: text = f.read() # 找到所有人物名字 names = re.findall('[\u4e00-\u9fa5]{2,4}(?:·[\u4e00-\u9fa5]{2,4})*', text) # 统计人物出现次数 name_counts = collections.Counter(names) # 输出前10个高频人物 print(name_counts.most_common(10)) ``` 这些示例代码可以帮助你快速进行文本词频统计、Hamlet英文词频统计以及《三国演义人物出场统计。你可以根据自己的需求进行调整和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只水熊虫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值