您好,登錄后才能下訂單哦!
這篇文章主要介紹Python如何實(shí)現(xiàn)用GUI設(shè)計(jì)有界面的詞云生成器,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
概述
Python 實(shí)現(xiàn)的、帶GUI界面的詞云生成器。 選擇文檔(中文、英文均可)即可生成詞云,支持自定義 停用詞詞典,支持自定義遮罩形狀。
詳細(xì)說明:
“詞云”就是數(shù)據(jù)可視化的一種形式,給出一段文本,根據(jù)文本中詞語的出現(xiàn)頻率而生成的一幅圖像,從而過濾掉大量的文本信息,人們只要掃一眼就能夠明白文章主旨,使得數(shù)據(jù)分析的結(jié)果更加直觀。
1.安裝必要的第三方庫:
pip install wordcloud pip install jieba pip install numpy pip install wxPython
安裝PIL,在以下地址下載PIL庫進(jìn)行安裝:
http://effbot.org/media/downloads/PIL-1.1.7.win32-py2.7.exe
(或在http://effbot.org/downloads/ 中找到與你操作系統(tǒng)及python版本相對(duì)應(yīng)
版本的PIL)
需要注意一點(diǎn),因?yàn)閣ordcloud自帶的字體文件不支持中文,為了讓wordcloud支持中文詞云的生成,安裝完wordcloud庫后需要hack一下,具體做法如下:
復(fù)制一個(gè)中文字體文件(在本項(xiàng)目中為方正姚體 FZYTK.TTF)到wordcloud安裝路徑下(如\Python27\Lib\site-packages\wordcloud),然后打開wordcloud庫中的wordcloud.py文檔,將其中的
“FONT_PATH = os.environ.get(‘FONT_PATH’, os.path.join(FILE, ‘DroidSansMono.ttf’))”
(本項(xiàng)目的附件中將附帶 FZYTK.TTF 字體文件)
改寫為
“FONT_PATH = os.environ.get(‘FONT_PATH’, os.path.join(FILE, ‘FZYTK.TTF’))”.
這樣wordcloud將會(huì)以”FZYTK.TTF”作為字體文件.
整體的項(xiàng)目結(jié)構(gòu)十分簡單,一共三個(gè)腳本文件,一個(gè)是GUI界面腳本(draw_gui.py),
一個(gè)是GUI菜單的輔助性腳本(utility_template.py),
一個(gè)是詞云生成器腳本(wordcloud_gen.py)。
如下:
以下是程序的實(shí)現(xiàn)思路,以及步驟,實(shí)現(xiàn)步驟里,附上了關(guān)鍵代碼,全部的代碼,請(qǐng)下載代碼后閱讀
在wordcloud_gen.py中導(dǎo)入相關(guān)的庫:
from os import path from PIL import Image import numpy as np import timefrom wordcloud import WordCloud, STOPWORDS import jieba
編寫wordcloud_gen.py中的相關(guān)函數(shù),
讀取傳入文檔:
def get_text(fn): text = open(fn).read() return text
中文分詞:
def get_text_cn(fn): t0 = get_text(fn) t0 = jieba.cut(t0,cut_all = False) text = ">
生成詞云:
def draw_wc(text,mask_path,stopwords): mask = np.array(Image.open(mask_path)) wc = WordCloud(max_words = 1000,mask = mask,stopwords = stopwords, margin = 0,random_state=1).generate(text) im = wc.to_image() im.show() fn = str(int(time.time()))+'.jpg' im.save(fn) return im
3.在draw_gui.py中編寫用戶界面:
導(dǎo)入相關(guān)的庫:
import wx import osfrom os import path from collections import namedtuple import wx.lib.rcsizer as rcs from wordcloud import STOPWORDS from utility_template import layout_template #自定義的庫 import wordcloud_gen as wcg #自定義的庫
編寫界面:
class MainWindow(wx.Frame): def __init__(self,parent,title): wx.Frame.__init__(self,parent,title=title,size=(600,-1)) Size = namedtuple("Size",['x','y']) s = Size(100,50) self.cn_text = None self.en_text = None cwd = os.getcwd() self.mask_path = path.join(path.abspath(cwd),'alice_mask.png') self.user_sw = STOPWORDS self.lt = layout_template() self.name = 'WordCloud draw' self.version = '0.2' self.des = '''Draw the word cloud.\n''' self.git_website = "https://github.com/WellenWoo/WordCloud_draw" self.copyright = "(C) 2018 All Right Reserved" """創(chuàng)建菜單欄""" filemenu = wx.Menu() menuExit = filemenu.Append(wx.ID_EXIT,"E&xit\tCtrl+Q","Tenminate the program") confmenu = wx.Menu() menuSw = confmenu.Append(wx.ID_ANY,"StopWords","Add user StopWrods dict") menuMask = confmenu.Append(wx.ID_ANY,"Mask","Set mask") helpmenu = wx.Menu () menuAbout = helpmenu.Append(wx.ID_ABOUT ,"&About","Information about this program") menuBar = wx.MenuBar () menuBar.Append(filemenu,"&File") menuBar.Append(confmenu,"&Configure") menuBar.Append(helpmenu,"&Help") self.SetMenuBar(menuBar) """創(chuàng)建輸入框""" self.in1 = wx.TextCtrl(self,-1,size = (2*s.x,s.y)) """創(chuàng)建按鈕""" b1 = wx.Button(self,-1,'text') b2 = wx.Button(self, -1, "run") """設(shè)置輸入框的提示信息""" self.in1.SetToolTipString('choose a text file') """界面布局""" self.sizer0 = rcs.RowColSizer() self.sizer0.Add(b1,row = 1,col = 1) self.sizer0.Add(self.in1,row = 1,col = 2) self.sizer0.Add(b2,row = 1,col = 3) """綁定回調(diào)函數(shù)""" self.Bind(wx.EVT_BUTTON, self.choose_cn, b1) self.Bind(wx.EVT_BUTTON, self.draw_cn, b2) '''菜單綁定函數(shù)''' self.Bind(wx.EVT_MENU,self.OnExit,menuExit) self.Bind(wx.EVT_MENU,self.OnAbout,menuAbout) self.Bind(wx.EVT_MENU,self.get_stopwords,menuSw) self.Bind(wx.EVT_MENU,self.get_mask,menuMask) self.SetSizer(self.sizer0) self.SetAutoLayout(1) self.sizer0.Fit(self) self.CreateStatusBar() self.Show(True)
界面如下:
編寫控件的回調(diào)函數(shù):
def choose_cn(self,evt): """Choose a Chinses text file""" self.cn_text = None self.cn_text = self.choose_file(txtformat) if self.cn_text is None: pass else: self.in1.Clear() self.in1.write(self.cn_text) def choose_file(self,wildcard): '''choose img''' dlg = wx.FileDialog( self, message="Choose a file", defaultDir=os.getcwd(), defaultFile="", wildcard=wildcard, style=wx.OPEN | wx.MULTIPLE | wx.CHANGE_DIR ) if dlg.ShowModal() == wx.ID_OK: paths = dlg.GetPaths() dlg.Destroy() return paths[0] else: return None def draw_cn(self,evt): if self.cn_text is None: self.raise_msg(u'plaese Choose a Chinses text file first.') return None else: text = wcg.get_text_cn(self.cn_text) wcg.draw_wc(text,self.mask_path,self.user_sw)
運(yùn)行效果如下:
以上是“Python如何實(shí)現(xiàn)用GUI設(shè)計(jì)有界面的詞云生成器”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。