Python分词+云图

Python分词+云图_第1张图片

Python分词+云图_第2张图片

Python分词+云图_第3张图片

代码:

from wordcloud import WordCloud, ImageColorGenerator
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
import jieba
from scipy.misc import imread

with open('resourse//AI2017.txt', 'r', encoding="utf-8") as f:
    remain=f.read()

jieba.load_userdict('resourse//AI2017.txt')
seg_list=jieba.cut(remain,cut_all=False)

tf={}   #统计词典
for seg in seg_list:
    if seg in tf:   #如果该键在集合的对象中,则该键所属对象值加1
        tf[seg]+=1
    else:   #否则,生成新词的键值对,初始值为1
        tf[seg]=1

ci=list(tf.keys())  #将字典的键值转为列表
# with open('stopword.txt','r') as ft:
#     stopword=ft.read()
# seg in stopword or
for seg in ci:
    if tf[seg]<5 or len(seg)<2 or  '一' in seg:
        tf.pop(seg)

mask_img=np.array(Image.open("cloud.jpg"))
font=r'font/simfang.ttf'
wc=WordCloud(background_color="white",mask=mask_img, collocations=False, font_path=font, max_font_size=200, width=1600, margin=0).generate_from_frequencies(tf)
plt.imshow(wc)
plt.axis('off')
plt.show()

#基于彩色图像生成相应的颜色
image_colors=ImageColorGenerator(mask_img)
plt.imshow(wc.recolor(color_func=image_colors))
plt.axis('off')
plt.show()
wc.to_file('guo2.jpg')

 

你可能感兴趣的:(Python,jieba,worldcloud)