使用python相关技术实现对一本中文小说(自选)进行词频分析,显示小说中出现率前50的中文词组,并用matplotlib画图展示

本文介绍了一种使用Python的jieba分词库和Matplotlib库进行中文文本词频统计的方法,并展示了如何绘制前50个高频词组的频率分布图。

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


import matplotlib.pyplot as plt
import jieba
from pylab import mpl
#解决matplotlib框中显示中文失败的问题
mpl.rcParams['font.sans-serif'] = ['SimHei']

with open("all.txt", encoding="utf-8") as f:
    contents = f.read()
words = jieba.lcut(contents)
#存储词组出现的次数
counts = {}
for word in words:
    if len(word) == 1:
        continue
    else:
        counts[word] = counts.get(word, 0) + 1


items = list(counts.items())
#对词组进行排序并提取前50个词组
items.sort(key=lambda x: x[1], reverse=True)
names, dicts = [], []
for i in range(50):
    word, count = items[i]
    txt = "{0:<5}{1:>5}".format(word, count)
    names.append(word)
    dicts.append(count)

#用matplotlib画图展示
plt.figure()
plt.plot(names,dicts)
plt.xlabel('前50的中文词组')
plt.ylabel('中文词组出现频率')
plt.title("《我是传奇BOSS》文章中出现频率前50的中文词组")
plt.show()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值