PYTHON 词云

时间:2023-03-09 19:28:28
PYTHON 词云
 #!/usr/bin/env python
# -*- coding:utf-8 -*-
import matplotlib.pyplot as plt
from wordcloud import WordCloud,STOPWORDS,ImageColorGenerator
import jieba
from scipy.misc import imread
from os import path def word_clould(file_path,stopwords_path,font_path,save_pic_name):
text_from_file_with_apath = open(file_path,encoding='utf-8').read() wordlist_after_jieba = jieba.cut(text_from_file_with_apath)
stop_words = [line.strip() for line in open(stopwords_path,encoding='utf-8')]
wordlist_jieba=[word for word in wordlist_after_jieba if word not in stop_words]
wl_space_split = " ".join(wordlist_jieba)
# 读取mask/color图片
d = path.dirname(__file__)
#nana_coloring = imread(path.join(d,bgpicture_path)) my_wordcloud = WordCloud(#background_color="white",
width=1200,height=800,
# max_font_size=30,
#max_words=5000,
margin = 5,
#mask = nana_coloring,
random_state = 30,
#stopwords = STOPWORDS,
font_path=font_path).generate(wl_space_split) #image_colors = ImageColorGenerator(nana_coloring) # recolor wordcloud and show
#my_wordcloud.recolor(color_func=image_colors)
plt.imshow(my_wordcloud)
plt.axis("off")
plt.show() my_wordcloud.to_file(path.join(d, save_pic_name)) file_path='小米6发布会.txt'
stopwords_path='E:\\stopwords.txt'
#bgpicture_path="E:\\wb.jpg"
font_path='E:\\SIMHEI.TTF'
save_pic_name="cloudimg1.png"
word_clould(file_path,stopwords_path,font_path,save_pic_name)