词云生成WordCloud

将一篇文章中的出现的词语进行频率统计并生成词云,可以使人有一目了然的感觉.
本文选取ISO9001质量管理体系的部分内容,保存在iso.txt文件中.
使用图形
在这里插入图片描述使用PYTHON第三方库:jieba,WordCloud,matplotlib

# -*- coding: utf-8 -*-
from wordcloud import WordCloud
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import jieba
import re


text = open('iso.txt').read()
Chinese = ''.join(re.findall(r'[\u4e00-\u9fa5]+', text))  #中文字符范围
 

text = ' '.join(jieba.cut(Chinese))# 中文分词
wordlist=text.split(" ")

def count_repeat(value):  #利用字典统计次数
      dict={}  
      for i in value: 
            condi=dict.setdefault(i,0)  #如果数字首次出现,字典的键为i,值为0.如果已出现计算次数
            if condi: 
                      dict[i ]=dict[i ]+1  
            else:
                      dict[i]=1  
      return dict
 

mask = np.array(Image.open("ISO.PNG"))
excludewords={"使用","及其","有关","相关","这些","包括"}

wc = WordCloud(scale=5,mask=mask, font_path='simhei.ttf', mode='RGBA', background_color=None,   stopwords=excludewords, max_words = 60,max_font_size = 60,random_state=20).generate(text)
# max_words词汇容量量 ,max_font_size 词大小
#mode='RGBA'    A是透明背景

plt.imshow(wc , interpolation='bilinear')  #插值计算
'''   Supported values are 'none', 'nearest', 'bilinear', 'bicubic',
        'spline16', 'spline36', 'hanning', 'hamming', 'hermite', 'kaiser',
        'quadric', 'catrom', 'gaussian', 'bessel', 'mitchell', 'sinc',
        'lanczos'.'''

plt.axis("off")  #关闭坐标

plt.show()
wc.to_file('wordcloud.png')  # 保存到文件

生成的词云:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值