用python實現(xiàn)詞云效果實例介紹
什么是詞云
詞云其實就是就是對網(wǎng)絡(luò)文本中出現(xiàn)頻率較高的〝關(guān)鍵詞〞予以視覺上的突出,形成〝關(guān)鍵詞云層〞或〝關(guān)鍵詞渲染〞從而過濾掉大量的文本信息
詞云也是數(shù)據(jù)可視化的一種形式。給出一段文本,根據(jù)關(guān)鍵詞的出現(xiàn)頻率而生成的一幅圖像,人們只要掃一眼就能夠明白其文章主旨。
一、特效預(yù)覽

詞云圖
二、程序原理
從給出的文本中,進行分詞處理,然后將每個詞出現(xiàn)的的頻率進行統(tǒng)計從給出的背景圖片上,讀出圖片信息將文本按照出現(xiàn)的頻率進行畫圖,出現(xiàn)頻率越高,字體設(shè)置越大

你聽懂了嗎
三、程序源碼
jieba模塊:用來進行分詞處理PIL模塊:用來進行圖片處理wordcloud模塊:用來進行生成詞云
#!/usr/bin/env python
# encoding: utf-8
import jieba
import numpy as np
import PIL.Image as Image
from wordcloud import WordCloud
class wordCloud:
'''
This is a main Class, the file contains all documents.
One document contains paragraphs that have several sentences
It loads the original file and converts the original file to new content
Then the new content will be saved by this class
'''
def __init__(self):
self.bg_img = 'assets/picture.jpeg'
self.word_path = 'assets/word.txt'
def hello(self):
'''
This is a welcome speech
:return: self
'''
print('*' * 50)
print(' ' * 20 + '詞云制作')
print(' ' * 5 + 'Author: autofelix Date: 2022-01-17 13:14')
print('*' * 50)
return self
def run(self):
'''
The program entry
'''
with open(self.word_path, 'r') as f:
word = f.read()
cut_word = ' '.join(jieba.cut(word))
color_mask = np.array(Image.open(self.bg_img))
word_cloud = WordCloud(
# 設(shè)置字體,不指定就會出現(xiàn)亂碼
font_path='/System/Library/Fonts/PingFang.ttc',
# 設(shè)置背景色
background_color='white',
# 詞云形狀
mask=color_mask,
# 允許最大詞匯
max_words=120,
# 最大號字體
max_font_size=2000
).generate(cut_word)
word_cloud.to_file('word_cloud.jpg')
im = word_cloud.to_image()
im.show()
if __name__ == '__main__':
wordCloud().hello().run()總結(jié)
到此這篇關(guān)于用python實現(xiàn)詞云效果實例介紹的文章就介紹到這了,更多相關(guān)python詞云內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實現(xiàn)獲取當(dāng)前日期的所屬信息
在Python中,處理日期和時間是一個常見的任務(wù),它涉及到許多方面,例如獲取日期的年、月、日、星期幾等等,本文將詳細介紹如何使用Python來獲取當(dāng)前日期的各種相關(guān)信息,需要的可以了解下2024-01-01
Python加密方法小結(jié)【md5,base64,sha1】
這篇文章主要介紹了Python加密方法,結(jié)合實例形式總結(jié)分析了md5,base64,sha1的簡單加密方法,需要的朋友可以參考下2017-07-07
使用Python保存網(wǎng)頁上的圖片或者保存頁面為截圖
這篇文章主要介紹了使用Python保存網(wǎng)頁上的圖片或者保存頁面為截圖的方法,保存網(wǎng)頁圖片主要用到urllib模塊,即簡單的爬蟲原理,需要的朋友可以參考下2016-03-03

