前言
昨天看到了B站up主不高兴就喝水的视频里面用到词云来展示他粉丝的相关评论和弹幕,觉得很好玩,同时他也将代码记在了笔记里面供其他人实现。看到这里不来写篇文章记录一下俺的实现过程,也方便以后想用的时候就可以直接拿来用,文章的最后会放上up的链接,可以去看看这个up主的视频,我觉得蛮有意思的。
以下是本篇文章正文内容,完整的代码在后面给出。
一、词云是什么?
比较官方的解释就是:“词云”就是通过形成“关键词云层”或“关键词渲染”,对网络文本中出现频率较高的“关键词”的视觉上的突出。
然后效果图就是这样的:
二、使用步骤
1.引入库
代码如下:
import jieba
import collections
import re
from pyecharts.charts import WordCloud
from pyecharts.globals import SymbolType
from pyecharts import options as opts
from pyecharts.globals import ThemeType
导入库时可能会遇到的困难
- ERROR: Could not find a version that satisfies the requirement jieba (from versions: none) 并且打开错误详细信息你会看到以下内容 :WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
[解决办法] 这里放一个网上的链接:https://blog.csdn.net/gobitan/article/details/108230881
我自己的解决办法是:我电脑里面装了有多个python,然后在建项目的时候选择了如下:
后来报错之后,我选择了下面的Previously configured interpreter,然后在导入包就成功了。可能我的解决办法并不适用其它电脑,但是大家可以借鉴以下我的这个方法。另外可以在网上多找找其它的方法。
2.去除分词结果中的无用词汇
这里需要说明以下,该部分对读入了一个文件stop_words.txt,这个是停用词(后面在完整代码处我放一个下载链接)。停用词的作用就是不显示那行没有意义的词,比如“那么”、“如果”
代码如下:
def deal_txt(seg_list_exact):
result_list = []
with open('stop_words.txt', encoding='utf-8') as f:
con = f.readlines()
stop_words = set()
for i in con:
i = i.replace("\n", "") # 去掉读取每一行数据的\n
stop_words.add(i)
for word in seg_list_exact:
# 设置停用词并去除单个词
if word not in stop_words and len(word) > 1:
result_list.append(word)
return result_list
3.渲染词云
def render_cloud(word_counts_top100):
word1 = WordCloud(init_opts=opts.InitOpts(width='1350px', height='750px', theme=ThemeType.MACARONS))
word1.add('词频', data_pair=word_counts_top100,
word_size_range=[15, 108], textstyle_opts=opts.TextStyleOpts(font_family='cursive'),
shape=SymbolType.DIAMOND)
word1.set_global_opts(title_opts=opts.TitleOpts('云图'),
toolbox_opts=opts.ToolboxOpts(is_show=True, orient='vertical'),
tooltip_opts=opts.TooltipOpts(is_show=True, background_color='red', border_color='yellow'))
# 渲染在html页面上
word1.render("云图.html")
4.主函数
这里面需要注意的是,在主函数里面有一个open()读取文件,这里的文件名大家可以换成自己的其它的都可以。随便试试。
if __name__ == '__main__':
# 读取文件
with open('words.txt', encoding='utf-8') as f:
data = f.read()
# 文本预处理 去除一些无用的字符 只提取出中文出来
new_data = re.findall('[\u4e00-\u9fa5]+', data, re.S)
new_data = " ".join(new_data)
# jieba分词将整句切成分词
seg_list_exact = jieba.cut(new_data, cut_all=True)
# 去掉无用词汇
final_list = deal_txt(seg_list_exact)
# 筛选后统计
word_counts = collections.Counter(final_list)
# 获取前100最高频的词
word_counts_top100 = word_counts.most_common(100)
# 可以打印出来看看统计的词频
# print(word_counts_top100)
# 渲染词云
render_cloud(word_counts_top100)
5. 完整代码
程序中所需要的词云的词典以及停用词的链接:资料
import jieba
import collections
import re
from pyecharts.charts import WordCloud
from pyecharts.globals import SymbolType
from pyecharts import options as opts
from pyecharts.globals import ThemeType
# 去除分词结果中的无用词汇
def deal_txt(seg_list_exact):
result_list = []
with open('stop_words.txt', encoding='utf-8') as f:
con = f.readlines()
stop_words = set()
for i in con:
i = i.replace("\n", "") # 去掉读取每一行数据的\n
stop_words.add(i)
for word in seg_list_exact:
# 设置停用词并去除单个词
if word not in stop_words and len(word) > 1:
result_list.append(word)
return result_list
# 渲染词云
def render_cloud(word_counts_top100):
word1 = WordCloud(init_opts=opts.InitOpts(width='1350px', height='750px', theme=ThemeType.MACARONS))
word1.add('词频', data_pair=word_counts_top100,
word_size_range=[15, 108], textstyle_opts=opts.TextStyleOpts(font_family='cursive'),
shape=SymbolType.DIAMOND)
word1.set_global_opts(title_opts=opts.TitleOpts('云图'),
toolbox_opts=opts.ToolboxOpts(is_show=True, orient='vertical'),
tooltip_opts=opts.TooltipOpts(is_show=True, background_color='red', border_color='yellow'))
# 渲染在html页面上
word1.render("云图.html")
if __name__ == '__main__':
# 读取文件
with open('words.txt', encoding='utf-8') as f:
data = f.read()
# 文本预处理 去除一些无用的字符 只提取出中文出来
new_data = re.findall('[\u4e00-\u9fa5]+', data, re.S)
new_data = " ".join(new_data)
# jieba分词将整句切成分词
seg_list_exact = jieba.cut(new_data, cut_all=True)
# 去掉无用词汇
final_list = deal_txt(seg_list_exact)
# 筛选后统计
word_counts = collections.Counter(final_list)
# 获取前100最高频的词
word_counts_top100 = word_counts.most_common(100)
# 可以打印出来看看统计的词频
# print(word_counts_top100)
# 渲染词云
render_cloud(word_counts_top100)