失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > jieba读取txt文档并进行分词 词频统计 输出词云图

jieba读取txt文档并进行分词 词频统计 输出词云图

时间:2020-11-27 17:22:19

相关推荐

jieba读取txt文档并进行分词 词频统计 输出词云图

代码实现

# 库的引用import jiebaimport matplotlib as mplimport matplotlib.pyplot as pltfrom wordcloud import WordCloud#定义一个空字符串final = ""#文件夹位置filename = r"D:\python\pra\推荐系统1-500.txt"#打开文件夹,读取内容,并进行分词with open(filename,'r',encoding = 'utf-8') as f:for line in f.readlines():word = jieba.cut(line)for i in word:final = final + i +" "

运行结果

# 图云打印word_pic = WordCloud(font_path = r'C:\Windows\Fonts\simkai.ttf',width = 2000,height = 1000).generate(final)plt.imshow(word_pic)#去掉坐标轴plt.axis('off')#保存图片到相应文件夹plt.savefig(r'D:\python\pra\6.png')

图云输出图

参考博客

[1]:/songrenqing/article/details/80541055

如果觉得《jieba读取txt文档并进行分词 词频统计 输出词云图》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。