火龙信奥
  • 首页
  • 课程
  • 题库
  • 打卡
    • 代码对战
    • 快速对战
  • 题单
  • 团队
  • 荣誉墙
  • 商城
  • 登录 / 注册

jieba 分词并制作词云

作者: 作者的头像   huolong , 时间:2023-10-25 12:13:59 , 所有人可见, 阅读  4


import jieba
from wordcloud import WordCloud
import matplotlib.pyplot as plt

txt = open("三体节选.txt",'r',encoding='utf-8').read()
words =jieba.lcut(txt) #使用 jieba 库函数分词

counts={}
for word in words:
    if len(word) == 1: #排除长度为 1 的字符分词结果
        continue
    else:
        counts[word]=counts.get(word,0)+1 #新词需要先新建,所以用 get 方法

items = list(counts.items()) #将字典中的键值对转为元组
items.sort(key=lambda x:x[1],reverse=True)#按照统计结果降序排序

ciyun =[]
for i in range(50):
    word,count = items[i]
    print(word,count)
    ciyun.append(word)

text_cut =''.join(ciyun) #转为字符串,并用空格分隔
wordscloud =WordCloud(background_color='white',font_path ='汉仪乐喵体.ttf',width=1000,height=1000,margin=2).generate(text_cut)
wordscloud.to_file("词云.png")
plt.imshow(wordscloud)
plt.axis('off') #关闭坐标轴
plt.show()


—— 本文来自火龙信奥(义乌睿码科技):义乌青少年信息学奥赛与编程教育平台,专注 CSP-J/S、NOIP、GESP 竞赛培训,线上线下融合教学,助力编程升学。网址:hlcoding.com

©2026加盟我们 | 关于我们 | ACM课程 | 常见问题 | 成果墙 | 评测记录 | 浙ICP备2021013995号
在线画图 | OI WIki | 打字练习
火龙信奥
请输入登录信息


请完成安全验证
验证码底图 滑块
向右拖动滑块完成验证
请输入用户名 / 绑定的手机号码



请输入注册信息(手机号验证码注册)





验证码5分钟有效,60秒内不可重复获取,每日最多3次

微信登录

微信登录二维码

正在生成二维码...

账号已过期,请续期。
去续期

绑定手机号

📱

为了更好地保护您的账号安全,享受完整的平台服务

请您尽快绑定手机号码