Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Building a GUI-Based Word Cloud Generator in Python

Tech 1

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:

  1. Place a Chinese font file (e.g., FZYTK.TTF) in the wordcloud package directory
  2. 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

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.