python 生成詞云

2022-01-22 12:40:02 字數 1780 閱讀 1084

1、知識點

"""

wordcloud引數講解:

font_path表示用到字型的路徑

width和height表示畫布的寬和高

prefer_horizontal可以調整詞雲中字型水平和垂直的多少

mask即掩膜,產生詞云背景的區域

scale:計算和繪圖之間的縮放

min_font_size設定最小的字型大小

max_words設定字型的多少

stopwords設定禁用詞

background_color設定詞云的背景顏色

max_font_size設定字型的最大尺寸

mode設定字型的顏色 但設定為rgba時背景透明

relative_scaling設定有關字型大小的相對字頻率的重要性

regexp設定正規表示式

collocations 是否包含兩個詞的搭配

"""

2、**

#

coding = utf-8

import

matplotlib.pyplot as plt

from wordcloud import

wordcloud

import

jieba

import

osimport

numpy

import

pil.image as image

cur_path = os.path.dirname(__file__) #

當前檔案的路徑

print

(cur_path)

text_from_file_with_apath = open(os.path.join(cur_path, '

深淵主宰系統.txt

'),encoding='

utf-8

').read() #

讀取檔案

讀取填詞的模板

wordlist_after_jieba = jieba.cut(text_from_file_with_apath, cut_all=true) #

jieba分詞

wl_space_split = "

".join(wordlist_after_jieba)

#mask 表示將詞填充到的模板 font_path 表示填充的字型

my_wordcloud = wordcloud(background_color="

white

",mask=mask_pic, font_path=os.path.join(cur_path, '

竹繁.ttf

')).generate(wl_space_split)

#顯示詞云

plt.imshow(my_wordcloud)

plt.axis(

"off")

plt.show()

3、模板

Python 生成詞云

import matplotlib.pyplot as plt from wordcloud import wordcloud import jieba text from file with apath open python.txt encoding utf 8 read wordlist af...

python 生成詞云

coding utf 8 from wordcloud import wordcloud import matplotlib.pyplot as plt import jieba from pil import image import numpy as np 生成詞云 defcreate word...

Python 生成詞云

import matplotlib.pyplot as plt from wordcloud import wordcloud import jieba text from file with apath open python.txt encoding utf 8 read wordlist af...