Building a GUI-Based Word Cloud Generator in Python
Implementation Overview
This Python application creates word clouds with graphical user interface support. It processes both Chinese and English documents, allows custom stop word dictionaries, and suports shape masking.
Technical Requirements
Required libraries:
pip install wordcloud jieba numpy wxPython
For Chinese character support, modify the wordcloud librarys' font configuration:
- Place a Chinese font file (e.g., FZYTK.TTF) in the wordcloud package directory
- Edit wordcloud.py to reference the new font:
FONT_PATH = os.environ.get('FONT_PATH', os.path.join(FILE, 'FZYTK.TTF'))
Core Functionality
Text Processing
English text handling:
def get_text(fn):
return open(fn).read()
Chinese text segmentation:
def get_text_cn(fn):
import jieba
text = jieba.cut(open(fn).read(), cut_all=False)
return " ".join(text)
Word Cloud Generation
def generate_wordcloud(text, mask_path, stopwords):
import numpy as np
from PIL import Image
from wordcloud import WordCloud
mask = np.array(Image.open(mask_path))
wc = WordCloud(max_words=1000, mask=mask, stopwords=stopwords,
margin=0, random_state=1).generate(text)
return wc.to_image()
GUI Implementation
Main window structure:
class WordCloudGUI(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(600, 400))
# Initialize components
self.text_path = None
self.mask_path = 'alice_mask.png'
self.stopwords = set()
# Create UI elements
self.create_menu()
self.create_controls()
self.layout_interface()
File selection dialog:
def show_file_dialog(self, wildcard):
dialog = wx.FileDialog(
self, message="Select document",
defaultDir=os.getcwd(),
wildcard=wildcard,
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST
)
if dialog.ShowModal() == wx.ID_OK:
return dialog.GetPath()
return None