词云生成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') # 保存到文件
生成的词云:
版权声明:本文为weixin_42109635原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
THE END