In [1]:
import wordcloud as wc
import jieba
import matplotlib.pyplot as plt
from scipy.misc import imread
%matplotlib inline
plt.rc('figure', figsize=(15, 15))
In [2]:
all_text = open(file='老九门.txt', encoding='utf-8').read()
In [3]:
all_text
Out[3]:
In [4]:
all_text = all_text.replace('\n', '' )
all_text = all_text.replace('\u3000', '')
In [6]:
seg_list = jieba.cut(all_text, cut_all=False)
words =' '
for seg in seg_list:
words = words + seg + ' '
print(words)
Out[6]:
In [7]:
jieba.load_userdict('dict.txt')
all_seg = jieba.cut(all_text, cut_all=False)
all_word =' '
for seg in all_seg:
all_word = all_word + seg + ' '
print(all_word)
In [8]:
# 引入字体
font=r"C:\WINDOWS\Fonts\simhei.ttf"
#读取背景图片,生成矩阵
color_mask = imread("love.jpg")
# 生成词云对象,设置参数
cloud = wc.WordCloud( font_path=font,#设置字体
background_color="black", #背景颜色
max_words=2000,# 词云显示的最大词数
mask=color_mask,#设置背景图片
max_font_size=100, #字体最大值
random_state=42)
# 绘制词云图
mywc = cloud.generate(all_word)
In [9]:
plt.imshow(mywc)
Out[9]:
In [10]:
# 把词云图保存到磁盘文件中
mywc.to_file('老九门wordcloud.png')
Out[10]: