用Python分析下王小波与李银河写情书最爱用哪些词

作家王小波其实也是我国最早期的程序员,突发奇想,王小波写情书最喜欢用哪些词呢?用Python词云分析下!

直接上代码吧,有注释很好理解。输出的图片设置的比较大,所以运行的比较慢,可以适当把图片尺寸改小点。

import jieba
from stylecloud import gen_stylecloud

def jieba_cloud(file_name, icon):
    with open(file_name, 'r', encoding='utf8') as f:
        word_list = jieba.cut(f.read())

        result = " ".join(word_list)    # 分词用  隔开

        # 设置停用词
        stopwords_file = open('stopwords.txt', 'r', encoding='utf-8')
        stopwords = [words.strip() for words in stopwords_file.readlines()]


        # 制作中文词云
        icon_name = " "
        if icon == "1":
            icon_name = "fas fa-thumbs-up"
        elif icon == "2":
            icon_name = "fas fa-heartbeat"
        elif icon == "3":
            icon_name = "fas fa-dog"
        elif icon == "4":
            icon_name = "fas fa-cat"
        elif icon == "5":
            icon_name = "fas fa-bug"
        elif icon == "6":
            icon_name = "fab fa-qq"
        pic = str(icon) + '.png'
        if icon_name is not None and len(icon_name) > 0:
            gen_stylecloud(text=result,
                           size=2048,  # stylecloud 的大小(长度和宽度)
                           icon_name=icon_name,
                           font_path='simsun.ttc',
                           max_font_size=400,  # stylecloud 中的最大字号
                           max_words=3000,  # stylecloud 可包含的最大单词数
                           custom_stopwords=stopwords,  #定制停用词列表
                           output_name=pic)
        else:
            gen_stylecloud(text=result, font_path='simsun.ttc', output_name=pic)
        return pic


# 主函数
if __name__ == '__main__':

    jieba_cloud("王小波与李银河书信集.txt", "1")
    jieba_cloud("王小波与李银河书信集.txt", "2")
    jieba_cloud("王小波与李银河书信集.txt", "3")
    jieba_cloud("王小波与李银河书信集.txt", "4")
    jieba_cloud("王小波与李银河书信集.txt", "5")
    jieba_cloud("王小波与李银河书信集.txt", "6")

炫酷的词云来了:


用Python分析下王小波与李银河写情书最爱用哪些词_第1张图片

你可能感兴趣的:(Python,列表,python,深度学习,人工智能,数据挖掘)