1. 程式人生 > 實用技巧 >詞雲 wordcloud

詞雲 wordcloud

import jieba  # 分詞
import numpy as np  # 矩陣運算
import pymysql
from PIL import Image  # 圖片處理
from matplotlib import pyplot as plt  # 繪圖,資料視覺化
from wordcloud import WordCloud  # 詞雲

conn = pymysql.connect(host="localhost", port=3306, user="pig", passwd="123456", db="runoob", charset='utf8')

cursor = conn.cursor()
cursor.execute('select `inq` from `t_movie`')
data = cursor.fetchall()
text = ' '.join([_[0] for _ in data])
cursor.close()
conn.close()

fenci_text = jieba.cut(text, cut_all=False)
# words = []
# for t in fenci_text:
#     if t not in words:
#         words.append(t)

print(text)
print('-' * 30)
show_text = ' '.join(fenci_text)
print(show_text)
img = Image.open(r'./static/img/123.jpg')
img_array = np.array(img)

wc = WordCloud(background_color='white',  # 設定背景色
               mask=img_array,  # 設定背景圖片
               font_path=r'C:\Windows\Fonts\FZSTK.TTF',  # 字型,微軟雅黑一直找不到
               collocations=False,  # 避免重複單詞
               max_words=1000,  # 詞雲顯示的最大詞數
               max_font_size=500,  # 字型最大值
               min_font_size=20,  # 字型最小值
               )
wc.generate_from_text(show_text)

# 繪製圖片
fig = plt.figure(1)
plt.imshow(wc)
plt.axis('off')  # 隱藏座標軸

plt.show()
wc.to_file('ttt.png')  #儲存圖片