代码如下:fromosimportpathfromwordcloudimportWordCloudimportmatplotlibmatplotlib.use('TkAgg')#获取目录地址,读取文本d=path.dirname(__file__)text=open(path.join(d,'haha.txt')).read()#生成词云图wordcloud=WordCloud().generate(text)#Pil方式显示生成的词云图image=wordcloud.to_image()image.show()fromosimportpathfromPILimportImageimportnumpyasnpimportmatplotlibmatplotlib.use('TkAgg')fromwordcloudimportWordCloud,STOPWORDSd=path.dirname(__file__)#读取整个文本。text=open(path.join(d,'haha.txt')).read()读取图片alice_mask=np.array(Image.open(path.join(d,"heart.png")))添加停用词stopwords=set(STOPWORDS)stopwords.add("HaHa")设置词云的一些属性wc=WordCloud(background_color="black",max_words=2000,mask=alice_mask,stopwords=stopwords)#生成词云外汇奖励活动http://www.fx61.com/activitieswc.generate(text)#pil方式显示生成的词云图(如果你没有matplotlib)image=wc.to_image()image.show()fromos从PILimportImage导入路径importnumpyasnpimportmatplotlibmatplotlib.use('TkAgg')importmatplotlib.pyplotaspltfromwordcloudimportWordCloud,STOPWORDSimportjiebad=path.dirname(__file__)##阅读全文。text=open(path.join(d,'Chinese.txt')).read()#printtext##读取图片alice_mask=np.array(Image.open(path.join(d,"dog.jpeg")))stopwords_path='stopwords.txt'#停用词列表my_words_list=['donothing']#添加新词到口吃词库#添加你自己的词库分词defadd_word(list):foritemsinlist:jieba.add_word(items)add_word(my_words_list)#结巴词wordlist=jieba.cut(text,cut_all=False)wl="".join(wordlist)#print(wl)#分词后输出txt#去除停用词mywordlist=[]f_stop=open(stopwords_path)try:f_stop_text=f_stop.read()f_stop_text=unicode(f_stop_text,'utf-8')#printf_stop_text最后:f_stop.close()f_stop_seg_list=f_stop_text.split('\n')formywordinwl.split(""):对于f_stop_seg_list中的停用词:if(myword.strip()==stopword)orlen(myword.strip())<=1:休息;else:mywordlist.append(myword)mywordlist="".join(mywordlist)#stopwords=set(STOPWORDS)#stopwords.add("thisway")#设置词云wc=WordCloud(background_color="black",#Set背景颜色mask=alice_mask,#设置背景图片max_words=2000,#设置最大显示字数#font_path="fangsong_GB2312.ttf",font_path="ChineseSongTypeface.ttf",max_font_size=50,#设置maximumfontsizerandom_state=30,#设置有多少种随机生成状态,即有多少种配色方案)#wc=WordCloud(background_color="black",max_words=2000,mask=alice_mask,font_path="ChineseSongTypeface.ttf",#stopwords=stopwords)myword=wc.generate(mywordlist)#生成词云#显示词云图plt.imshow(myword)plt.axis("off")plt.show()如何解决词云图模糊?默认参数图片分辨率低,设置scale参数,参数越大分辨率越高如何解决词云图片重复?默认的collocations=True,我们只是将它设置为False。具体原理好像是把相邻的两个词算作一个词。collocations=False#对于重复关键字的问题,我们可以使用collocations参数来解决其他Python显示中文字符串列表,直接输出会以unicode编码格式显示。导入json打印json.dumps(f_stop_seg_list,encoding="UTF-8",ensure_ascii=False)
