自学内容网 自学内容网

数据可视化第9天(利用wordcloud和jieba分析蝙蝠侠评论的关键字)

数据可以在这里下载
https://github.com/harkbox/DataAnalyseStudy

WordCloud

  • wordcloud可以很方便的生成词云图,方便的提供可视化
  • 可以直接使用pip install wordcloud进行安装
  • 如果使用的是Anaconda,可以使用conda install进行安装

下面看一个简单的例子

txt ="谜语 人家 里,他 把 布鲁斯·韦恩 的 照片 和 蝙蝠侠"

#font_path:如果含有中文,需要有中文字体的路径
#collocations=False,不包括重复的词
#max_words最多50个词
wordcloud=WordCloud(font_path='/System/Library/Fonts/Hiragino Sans GB.ttc',collocations=False,
                       width=800,height=600,max_words=50).generate(txt)
    
    
image=wordcloud.to_image()
image.show()

jieba

  • jieba是一个强大的中文分词库
  • 存在三种分词模式,全模式,精确模式,搜索模式
  • 使用jieba和wordcloud可以方便的实现对文章进行词云图分析
  • jieba使用cut切分文章
  • jieba.analyse.extract_tags()用来提取关键词

使用上述的两个库实现分析评论

import wordcloud
import jieba.analyse
filename='你的text文件名字'

with open(filename,encoding='utf-8') as f:
    data=f.read()
#topk=1000,提取最多1000个关键词,allowPOS='a',提取的都#是形容词   
seg_list=jieba.analyse.extract_tags(data,topK=1000,allowPOS=("a"))
#将提取的关键词列表变为字符串并且用空格隔开,方便进行词云图的绘制
text=' '.join(seg_list)
wordcloud=WordCloud(font_path='/System/Library/Fonts/Hiragino Sans GB.ttc',width=800,height=600,                   collocations=False,max_words=100,background_color='black').generate(text)

image=wordcloud.to_image()
image.show()

效果如下,显然评论大多数认为过于拖沓
在这里插入图片描述


原文地址:https://blog.csdn.net/qq_43702629/article/details/139020549

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!