代碼:
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import jieba
from scipy.misc import imread
import matplotlib
matplotlib.style.use('ggplot')
%matplotlib inline
#讀取中文文本文件鼻百,生成詞云圖虏杰。
text_from_file_with_apath =open(r'E:\img\wordcloud\boe.txt',encoding='utf-8').read()
wordlist_after_jieba =jieba.cut(text_from_file_with_apath, cut_all = True)
wl_space_split = "".join(wordlist_after_jieba)
#逐行讀取中文文本文件较曼,生成以上詞云詞頻統(tǒng)計(jì)列表,包含:關(guān)鍵字罗捎,該關(guān)鍵字出現(xiàn)的頻率毒坛。
word_list = []
key_list=[]
for line in
open(r'E:\img\wordcloud\boe.txt',encoding='utf-8'):#1.txt是需要分詞統(tǒng)計(jì)的文檔
item = line.strip('\n\r').split('\t') #制表格切分
tags = jieba.analyse.extract_tags(item[0]) #jieba分詞
for t in tags:
word_list.append(t)
word_dict= {}
orderList ={}
#print(word_list)
for item in word_list:
if item not in word_dict: #統(tǒng)計(jì)數(shù)量
word_dict[item] = 1
else:
word_dict[item] += 1
orderList=list(word_dict.values())
orderList.sort(reverse=True)
print(word_dict)
matplotlib.rcParams['figure.figsize'] =(20.0, 10.0)
color_mask =
imread("E:\img\wordcloud\zhanl.jpeg") #讀取背景圖片
my_wordcloud =WordCloud(font_path="E:\wuqiang\salestool\python\wordcloud\DroidSansFallbackFull.ttf"
, mask=color_mask,background_color="black", prefer_horizontal=1.0,width=1800,height=800, max_words=200).fit_words(word_dict)
#wordcloud = wordcloud
plt.imshow(my_wordcloud)
plt.axis("off")
plt.show()
結(jié)果: