当前位置:首页 > 资讯 > 正文

re、词云

re、词云

from jieba.analyse import set_stop_words,extract_tags
from wordcloud import WordCloud,STOPWORDS as sw
import numpy as np
from PIL import Image
 
#①结巴分词提取高频中文词:
stopWords='D:/中文停用词表.txt'   #1个过滤词占1行
txtFile='F:/New Download/example.txt'
with open(txtFile) as f:
    sentence=f.read()
set_stop_words(stopWords)   #结巴分词の过滤:自定义中文
#words=extract_tags(sentence,50)
#text=' '.join(words)
#词云网站多数有个先后足矣,wc.generate_from_frequencies({*})等还需提供词频
words=extract_tags(sentence,topK=50,withWeight=True)
frequencies={word[0]:int(word[1]*1000) for word in words}
 
#②{词:词频,}数据导入词云:
backImg='F:/New Download/background.png'
bg=np.array(Image.open(backImg))    #bg=scipy.misc.imread(backImg)
wc=WordCloud('simhei.ttf',mask=bg,max_font_size=81) #背景是ndarray对象
#wc.stopwords=sw|set(open(stopWords).readlines())   #词云の过滤:内置英文及自定义中文
#wc.generate_from_frequencies({词1:词频1,}),wc.generate('空格分隔的各词')
wc.generate_from_frequencies(frequencies)   #wc.generate(text)
 
#③展示图:法1のImage库用图片路径str,法2のplt库用WordCloud对象
saveImg='F:/New Download/result.jpg'
 
wc.to_file(saveImg)
Image.open(saveImg).show()
 
#import matplotlib.pyplot as plt
#plt.imshow(wc)
#plt.axis('off')
#plt.savefig(saveImg,dpi=240,bbox_inches='tight')
#plt.show()
******************分割线*******************
词云网站https://wor删dart.com/create(加载完要等几秒)的用法:
 
左侧的:WORDSのImport→保持顺序贴入各词(勾上俩Remove,若有词频且以 分割则勾上CSV)→SHPAGES选个图→FONTSのAdd font(如选个本机的雅黑字体,网站提供的那些都不支持中文)→LAYOUT设字体倾斜→STYLEのCustom设字体五颜六色
→右上的Visualize
→右顶的DOWNLOAD(chrome设为内置下载)。