【Python小随笔】自定义词云图形

 
from wordcloud import ImageColorGenerator
from matplotlib.image import imread
import matplotlib.pyplot as plt

# 输出分词组合
ciyun_str = ciyun_word_str(ciyun_raw)

back_img = imread("logo.jpg")
img_colors = ImageColorGenerator(back_img)
word_cloud = WordCloud(font_path="msyh.ttc",
                           background_color="white",
                           max_words=1000,
                           max_font_size=100,
                           width=1920,
                           mask=back_img,
                           height=1080).generate(ciyun_str)
# word_cloud.recolor(color_func=img_colors)  # 替换默认的字体颜色
plt.figure()  # 创建一个图形实例
plt.imshow(word_cloud, interpolation='bilinear')
plt.axis("off")  # 不显示坐标尺寸
plt.show()

 


# 输出分词

def ciyun_word_str(text):
    """
        :param text:  文本
        :return: A,B,C,D,E,F
    """
    res_str = ""
    # 文本预处理
    remove_words = [u'', u'', u'', u'', u'随着', u'对于', u'', u'', u'', u'', u'', u' ', u'', u'', u'', u'',
                    u'通常', u'如果', u'我们', u'需要',u'' ,u'',u'',u'非常',u'',u'',u'',u'']  # 自定义去除词库
    seg_list_exact = SnowNLP(text).words  # 每一个数组评论分词
    for word in seg_list_exact:  # 循环读出每个分词
        if word not in remove_words:  # 如果不在去除词库中
            res_str = res_str + word + ","
    return res_str

 

 

posted @ 2022-05-07 11:44  PythonNew_Mr.Wang  Views(106)  Comments(0)    收藏  举报