import pandas as pd
import jieba
import jieba.analyse
filename = "E:\数据处理\隐患类型.txt"
#载入数据
df_data = pd.read_csv(filename, header=0, encoding='gbk', dtype=str)#DataFrame
#去重并转换为list
ls_data = df_data.drop_duplicates().values.tolist()#df_data.values为array类型
ls_data = ";".join([str(j) for i in ls_data for j in i])#将列表元素转换为一个字符串,供lcut函数使用
print(ls_data)
seg_list = jieba.lcut(ls_data, cut_all=False, HMM=True) # 精确切割模式(默认为精确模式)
seg = '/'.join(seg_list)#str类型
#关键词抽取
tags = jieba.analyse.extract_tags(seg, topK=300, withWeight=True, allowPOS=('ns', 'n', 'vn', 'v'),)
import pyecharts.options as opts
from pyecharts.charts import WordCloud
def wordcloud():
g = WordCloud()
g.add(series_name="热点分析", data_pair=tags, word_size_range=[6, 66])
g.set_global_opts(
title_opts=opts.TitleOpts(
title="高频词条", title_textstyle_opts=opts.TextStyleOpts(font_size=23)
),
tooltip_opts=opts.TooltipOpts(is_show=True),
)
#.render("basic_wordcloud.html")
return g
# 使用snapshot-selenium渲染成图片
from snapshot_selenium import snapshot
from pyecharts.render import make_snapshot
make_snapshot(snapshot, wordcloud().render(), "E:\数据处理\词云图2.png")# 保存为图片