1. 程式人生 > >Python之詞雲學習筆記

Python之詞雲學習筆記

需要的主要模組:jieba中文分詞 wordcloud構建詞雲

英文詞雲

from wordcloud import WordCloud

string = 'Importance of relative word frequencies for font-size. With relative_scaling=0, only word-ranks are considered. With relative_scaling=1, a word that is twice as frequent will have twice the size. If you want to consider the word frequencies and not only their rank, relative_scaling around .5 often looks good.'
font = r'C:\Windows\Fonts\FZSTK.TTF'

wc = WordCloud(font_path=font,
                background_color='white',
                width=1000,
                height=800,
               ).generate(string)
wc.to_file('ss.png')

中文詞雲

import jieba
from wordcloud import WordCloud
from PIL import Image
import numpy as np

font = r'hwkt.ttf'
content = (open('天龍八部精校版.txt','r',encoding='utf-8')).read()
cut = jieba.cut(content)
cut_content = ' '.join(cut)
img = Image.open('test.jpg')
img_array = np.array(img)

wc = WordCloud(
    font_path=font,
    background_color='white',
    mask=img_array
)
wc.generate_from_text(cut_content)
wc.to_file('new.png')