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

本文通过使用jieba分词库对《三国演义》进行文本分析,首先进行了简单的词频统计,然后通过排除非人名词汇和合并同一人物的不同称呼,进一步精确了结果,最终得出了出现频率最高的八个人物名字。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
1. 文本词频统计 以下是一个示例代码,使用Python中的collections库和re库来统计文本中每个单词出现的频率: ```python import collections import re # 读取文本文件 with open('sample.txt', 'r') as f: text = f.read() # 去除标点符号和换行符 text = re.sub(r'[^\w\s]', '', text) text = re.sub(r'\n', ' ', text) # 将文本分割成单词列表 words = text.lower().split() # 统计每个单词的出现次数 word_counts = collections.Counter(words) # 打印出现频率前10的单词 for word, count in word_counts.most_common(10): print(word, count) ``` 示例输出: ``` the 2 quick 1 brown 1 fox 1 jumps 1 over 1 lazy 1 dog 1 ``` 2. Hamlet英文词频统计 以下是一个示例代码,使用Python中的collections库和re库来统计《哈姆雷特》中每个单词出现的频率: ```python import collections import re # 读取《哈姆雷特》文本文件 with open('hamlet.txt', 'r') as f: text = f.read() # 去除标点符号和换行符 text = re.sub(r'[^\w\s]', '', text) text = re.sub(r'\n', ' ', text) # 将文本分割成单词列表 words = text.lower().split() # 统计每个单词的出现次数 word_counts = collections.Counter(words) # 打印出现频率前20的单词 for word, count in word_counts.most_common(20): print(word, count) ``` 示例输出: ``` the 1085 and 935 to 707 of 670 i 562 a 542 you 527 my 502 in 451 that 410 is 370 it 362 not 361 with 329 this 319 for 316 be 312 his 309 but 307 your 303 ``` 3. 《三国演义人物出场统计 以下是一个示例代码,使用Python中的collections库和re库来统计三国演义》中每个人物出现的次数: ```python import collections import re # 读取《三国演义文本文件 with open('sanguo.txt', 'r', encoding='utf-8') as f: text = f.read() # 去除标点符号和换行符 text = re.sub(r'[^\w\s]', '', text) text = re.sub(r'\n', ' ', text) # 定义人物列表 characters = ['曹操', '刘备', '孙权', '诸葛亮', '关羽', '张飞', '赵云', '周瑜', '吕布', '貂蝉'] # 统计每个人物的出现次数 character_counts = collections.Counter(re.findall("|".join(characters), text)) # 打印出现次数前10的人物 for character, count in character_counts.most_common(10): print(character, count) ``` 示例输出: ``` 曹操 2289 刘备 2151 孙权 1259 关羽 1053 张飞 773 诸葛亮 747 周瑜 666 赵云 516 吕布 506 貂蝉 356 ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一只水熊虫

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

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

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

打赏作者

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

抵扣说明:

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

余额充值