Python 词云生成器:使用 PyQt5 构建交互式界面

本项目使用 Python 和 PyQt5 构建一个交互式词云生成器,让用户可以轻松创建自定义的词云。

功能特点:

  • 选择 CSV 文件:用户可以选择本地 CSV 文件作为词云数据源。
  • 设置排除词:用户可以在文本框中输入需要排除的词语,这些词语不会出现在生成的词云中。
  • 生成词云图:点击“生成词云图”按钮,即可根据用户选择的 CSV 文件和排除词生成词云图像。

代码示例:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QPlainTextEdit, QPushButton, QLabel
from wordcloud import WordCloud
import pandas as pd
import matplotlib.pyplot as plt

class WordCloudApp(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle('WordCloud App')
        self.setGeometry(50, 50, 600, 400)

        self.file_path = ''
        self.exclude_words = ''

        self.initUI()

    def initUI(self):
        # 标签显示文件路径
        self.label_file = QLabel(self)
        self.label_file.setText('No file selected')
        self.label_file.move(20, 20)

        # 选择 CSV 文件按钮
        btn_file = QPushButton('Select CSV file', self)
        btn_file.move(20, 50)
        btn_file.clicked.connect(self.selectFile)

        # 文本框输入排除词
        self.exclude_textbox = QPlainTextEdit(self)
        self.exclude_textbox.insertPlainText('Enter exclude words here')
        self.exclude_textbox.move(20, 100)
        self.exclude_textbox.resize(200, 100)
        self.exclude_textbox.textChanged.connect(self.excludeTextChanged)

        # 生成词云图按钮
        btn_generate = QPushButton('Generate Wordcloud', self)
        btn_generate.move(20, 220)
        btn_generate.clicked.connect(self.generateWordcloud)

    def selectFile(self):
        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog
        file, _ = QFileDialog.getOpenFileName(self, 'Select CSV file', '', 'CSV Files (*.csv)', options=options)
        if file:
            self.file_path = file
            self.label_file.setText(file)

    def excludeTextChanged(self):
        self.exclude_words = self.exclude_textbox.toPlainText()
        if self.exclude_words == 'Enter exclude words here':
            self.exclude_words = ''

    def generateWordcloud(self):
        if self.file_path:
            data = pd.read_csv(self.file_path, header=None, encoding='utf-8')
            words = dict(zip(data[0], data[1]))
            exclude_words = self.exclude_words.split()
            wordcloud = WordCloud(font_path='simhei.ttf', background_color='white', width=1000, height=500, max_words=100, max_font_size=300, min_font_size=30, relative_scaling=0.5, prefer_horizontal=1.0, stopwords=exclude_words)
            wordcloud.generate_from_frequencies(words)
            plt.imshow(wordcloud, interpolation='bilinear')
            plt.axis('off')
            plt.show()
        else:
            self.label_file.setText('Please select a CSV file')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = WordCloudApp()
    window.show()
    sys.exit(app.exec_())

使用方法:

  1. 确保已安装必要的库:pip install pandas wordcloud matplotlib pyqt5

  2. 准备一个包含词语和词频的 CSV 文件,例如:

    词语, 频率
    Python, 10
    编程, 5
    数据分析, 3
    ... 
    
  3. 运行 Python 代码,即可打开词云生成器窗口。

  4. 点击“Select CSV file”按钮选择您的 CSV 文件。

  5. 在文本框中输入需要排除的词语,例如“的”,“是”等。

  6. 点击“Generate Wordcloud”按钮生成词云图像。

注意事项:

  • 代码中使用 simhei.ttf 字体,请确保您的系统中已安装该字体。
  • 您可以根据需要调整代码中的词云参数,例如颜色、大小、最大词数等。

总结:

通过使用 Python 和 PyQt5,我们成功构建了一个功能完善的词云生成器,为用户提供了更直观便捷的词云创建方式。

Python 词云生成器:使用 PyQt5 构建交互式界面

原文地址: https://www.cveoy.top/t/topic/nE9V 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录