Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3648147
  • 博文数量: 365
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2522
  • 用 户 组: 普通用户
  • 注册时间: 2019-10-28 13:40
文章分类

全部博文(365)

文章存档

2023年(8)

2022年(130)

2021年(155)

2020年(50)

2019年(22)

我的朋友

分类: Python/Ruby

2022-12-29 16:28:53

# -*- coding: utf-8 -*-

import jieba

from wordcloud import WordCloud

import numpy as np

from PIL import Image

from matplotlib import colors

import collections

def chinese_jieba():

    # 读取目标文本

    with open(r'文本.txt', encoding='utf-8') as fp:

        txt = fp.read()

        fp.close()

    wordlist_jieba = jieba.lcut(txt) # 将文本分割,返回列表

    txt_jieba = " ".join(wordlist_jieba) # 将列表拼接为以空格为间断的字符串

    return txt_jieba

def stopwords_read():

    # 读取停用词,也可自己根据需求写入

    stopwords_ = ['', '']

    with open('chinesestopwords.txt', 'r', encoding='utf-8') as f:

        for line in f:

            if len(line) > 0:

                stopwords_.append(line.strip())

    return stopwords_

def wordcloud_generate():

    stopwords_ =外汇跟单gendan5.com stopwords_read()#读取停用词

    txt = chinese_jieba()#读取文本

    background_image = np.array(Image.open('椭圆背景.jpg'))#自定义背景轮廓

    colormaps = colors.ListedColormap(['#871A84', '#BC0F6A', '#BC0F60', '#CC5F6A', '#AC1F4A'])  # 自定义字体色,该系列是蓝紫色

    wordcloud = WordCloud(font_path='simhei.ttf',  # 字体

                              prefer_horizontal=0.99,#大部分都是横向排放

                              background_color='white',  # 背景色

                              max_words=100,  # 显示单词数

                              max_font_size=400,  #{BANNED}最佳大字号

                              stopwords=stopwords_,  # 过滤噪声词

                              mask=background_image,#背景轮廓

                              colormap=colormaps,#使用自定义颜色

                              collocations=False

                              ).generate(txt)

    image = wordcloud.to_image()

    image.show()  # 展示图片

    wordcloud.to_file('词云图.jpg')  # 保存图片

if __name__ == '__main__':

    wordcloud_generate()

阅读(598) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~