词云图
https://www.jianshu.com/p/b09359ee0596#comments
import pickle
from os import path
import jieba
import os
import imageio
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
# with open('text.txt', 'r', encoding='utf8') as fin:
# text = fin.read()
d = path.dirname(__file__) if "__file__" in locals() else os.getcwd()
print(d)
text = open(path.join(d, 'CalltoArms.txt'),encoding='utf8').read()
# text = ''
# with open('CalltoArms.txt', 'r', encoding='utf8') as fin:
# for line in fin.readlines():
# line = line.strip('\n')
# # sep’.join(seq)以sep作为分隔符,将seq所有的元素合并成一个新的字符串
# text += ' '.join(jieba.cut(line))
backgroud_Image = imageio.imread('3.png')
print('加载图片成功!')
'''设置词云样式'''
wc = WordCloud(
background_color='white',# 设置背景颜色
mask=backgroud_Image,# 设置背景图片
font_path='222.ttf', # 若是有中文的话,这句代码必须添加,不然会出现方框,不出现汉字
max_words=2000, # 设置最大现实的字数
stopwords=STOPWORDS,# 设置停用词
max_font_size=150,# 设置字体最大值
random_state=30 # 设置有多少种随机生成状态,即有多少种配色方案
)
wc.generate_from_text(text)
print('开始加载文本')
#改变字体颜色
img_colors = ImageColorGenerator(backgroud_Image)
#字体颜色为背景图片的颜色
# wc.recolor(color_func=img_colors)
# 显示词云图
plt.imshow(wc)
# 是否显示x轴、y轴下标
plt.axis('off')
plt.show()
# 获得模块所在的路径的
d = path.dirname(__file__)
# os.path.join(): 将多个路径组合后返回
wc.to_file(path.join(d, "h11.jpg"))
print('生成词云成功!')
# coding=utf-8
import pandas as pd
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
import imageio
import jieba
# 创建停用词列表
def stopwordslist():
stopwords = [line.strip() for line in open('stopwords.txt', encoding='UTF-8').readlines()]
return stopwords
stopwords = stopwordslist()
fd = pd.read_excel("./141.xlsx")
print(fd.head(2))
fd1 = fd['10、销售管理机器人的发展离不开您的帮助,请您提出宝贵意见。']
# 去停顿词
ls = []
for i in fd1:
seg_list = jieba.cut(str(i), cut_all=False)
for j in seg_list:
if j in stopwords:
continue
else:
ls.append(j)
# 词频统计
frequencies = {}
for i in ls:
if i in frequencies.keys():
frequencies[i] = frequencies[i]+1
else:
frequencies[i] = 1
print(frequencies)
with open("text.txt", "w", encoding='utf-8') as f:
str = '\n'
f.write(str.join(ls))
# 读取一个txt文件
text = open('text.txt', 'r', encoding='utf-8').read()
# 读入背景图片
bg_pic = imageio.imread('4.jpg')
# 生成词云
# wordcloud = WordCloud(font_path='222.ttf', mask=bg_pic, background_color='white', scale=1.5).generate(text) # 根据文本生成词云
wordcloud = WordCloud(font_path='222.ttf', mask=bg_pic, background_color='white', scale=1.5).fit_words(frequencies) # 根据频率生成词云
# 文件类型为字典
image_colors = ImageColorGenerator(bg_pic)
# 显示词云图片
# plt.rcParams['font.sans-serif'] = ['SimHei']
# plt.rcParams['axes.unicode_minus'] = False
plt.imshow(wordcloud)
plt.axis('off')
plt.show()
# 保存图片
wordcloud.to_file('test1.jpg')
版权声明:本文为m0_43609475原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。