Wordcloud+PyQt5写个词云图生成器1.0

-------------词云图集合-------------

用Wordcloud+PyQt5写个词云图生成器1.0

WordCloud去掉停用词(fit_words+generate)的2种用法

通过词频来绘制词云图(jieba+WordCloud)

Python教程95:去掉停用词+词频统计+jieba.tokenize示例用法

将进酒---李白process_text词频统计,及词频可视化分析

使用wordcloud模块,绘制一个自定义的词云图形状

使用WordCloud模块中repeat参数,做一个关键字重复的词云图

关于词云图显示异常,出现乱码的解决办法

盘点WordCloud模块,词云图的相关知识点

Python源码05:使用Pyecharts画词云图图

这段代码是一个基于PyQt5和wordcloud库的词云图生成器GUI应用程序。它允许用户通过图形界面选择文本文件、停用词文件、背景颜色,并生成词云图。

python 复制代码
# -*- coding: utf-8 -*-
# @Author : 小红牛
# 微信公众号:wdPython
import re
import sys
from collections import Counter
import jieba
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QFileDialog, \
    QColorDialog
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from wordcloud import WordCloud


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.color_code = None
        self.stopwords = {','}
        self.initUI()

    def initUI(self):
        # 创建布局
        vbox = QVBoxLayout()
        # 横向排列的四个按钮
        button_list = ["选择文本", "选择停用词", "选择背景颜色", "导出词云图"]
        hbox1 = QHBoxLayout()
        for i in range(4):
            btn = QPushButton(button_list[i])
            if button_list[i] == "选择文本":
                btn.clicked.connect(self.openTextFile)
            elif button_list[i] == "选择停用词":
                btn.clicked.connect(self.openStopWordsFile)
            elif button_list[i] == "选择背景颜色":
                btn.setObjectName("选择背景颜色")
                btn.clicked.connect(self.selectBackgroundColor)
            else:
                btn.clicked.connect(self.save_wordcloud_image)
            hbox1.addWidget(btn)
        vbox.addLayout(hbox1)

        # connect绑定按钮事件
        btn1 = QPushButton('逐字解析词云图')
        btn1.clicked.connect(self.update_wordclouds)
        vbox.addWidget(btn1)

        btn2 = QPushButton('按词语解析词云图')
        btn2.clicked.connect(self.update_wordcloud)
        vbox.addWidget(btn2)

        # matplotlib绘制图到Canvas
        self.fig = Figure()
        self.axes = self.fig.add_subplot(111)
        self.canvas = FigureCanvas(self.fig)
        # 将画布添加到布局中
        vbox.addWidget(self.canvas)

        # 设置窗口属性
        self.setLayout(vbox)
        self.setWindowTitle('词云图生成器1.0------------微信公众号:wdPython')
        self.setGeometry(200, 100, 900, 640)
        self.show()

    def save_wordcloud_image(self):
        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog
        fileName, _ = QFileDialog.getSaveFileName(self, "保存词云图", "",
                                                  "PNG Files (*.png);;JPG Files (*.jpg);;All Files (*)",
                                                  options=options)
        if fileName:
            self.fig.savefig(fileName, dpi=100, bbox_inches='tight')
            print(f"词云图已保存为 {fileName}")

    # 逐字生成词云图
    def update_wordclouds(self):
        # 1.使用正则,只取中文的字符,过滤英文数字,各种标点符号等等
        new_text = re.findall('[\u4e00-\u9fff]+', self.text)
        new_text = ''.join(new_text)
        word_freq = Counter(new_text)
        print('2.过滤前的词频word_freq:'.center(50, '-'))
        print(word_freq)
        print(f'当前的停用词有:{self.stopwords}')

        # 2.过滤词频
        filtered_word_freq = {word: freq for word, freq in word_freq.items()
                              if word not in self.stopwords}
        print('3.过滤后的词频filtered_word_freq:'.center(50, '-'))
        print(filtered_word_freq)

        w = WordCloud(width=900, height=500, font_path='msyh.ttc',
                      background_color=self.color_code if self.color_code is not None else 'white')
        # 不能有换行符
        w.fit_words(filtered_word_freq)
        self.axes.clear()
        self.axes.imshow(w, interpolation='bilinear')
        self.axes.axis("off")
        self.canvas.draw()
        print('词云图加载完成!!')
        # 逐字生成词云图

    # 安词语生成词云图
    def update_wordcloud(self):
        # 1.使用正则,只取中文的字符,过滤英文数字,各种标点符号等等
        new_text = re.findall('[\u4e00-\u9fff]+', self.text)
        new_text = ''.join(new_text)
        # 使用jieba进行分词
        cut_word = jieba.cut(new_text, cut_all=False)
        word_freq = Counter(cut_word)
        print('2.过滤前的词频word_freq:'.center(50, '-'))
        print(word_freq)
        print(f'当前的停用词有:{self.stopwords}')

        # 2.过滤词频
        filtered_word_freq = {word: freq for word, freq in word_freq.items()
                              if word not in self.stopwords}
        print('3.过滤后的词频filtered_word_freq:'.center(50, '-'))
        print(filtered_word_freq)

        w = WordCloud(width=900, height=500, font_path='msyh.ttc',
                      background_color=self.color_code if self.color_code is not None else 'white')
        # 不能有换行符
        w.fit_words(filtered_word_freq)
        self.axes.clear()
        self.axes.imshow(w, interpolation='bilinear')
        self.axes.axis("off")
        self.canvas.draw()
        print('词云图加载完成!!')
    # 打开文件选择对话框,选择文本文件
    def openTextFile(self):
        self.file_path, _ = QFileDialog.getOpenFileName(self, "选择文本文件", "", "Text Files (*.txt)")
        if self.file_path:  # 确保文件路径不为空
            with open(self.file_path, 'r', encoding='utf-8') as file:
                # 读取文本文件内容
                self.text = file.read()
        else:
            print("未选择文件")

    # 打开文件选择对话框,选择停用词文件
    def openStopWordsFile(self):
        file_path, _ = QFileDialog.getOpenFileName(self, "选择停用词文件", "", "Text Files (*.txt)")
        # 这里添加处理文件路径的逻辑
        with open(file_path, 'r', encoding='utf-8') as f:
            self.stopwords = set(f.read().splitlines())

    # 打开颜色选择器,选择背景颜色
    def selectBackgroundColor(self):
        color = QColorDialog.getColor()
        if color.isValid():
            # 更新按钮文本为颜色代码
            self.color_code = color.name()
            button = self.findChild(QPushButton, "选择背景颜色")
            button.setText(self.color_code)
            # 更新按钮文本颜色
            button.setStyleSheet(f"color: {self.color_code};")
            # 这里添加使用所选颜色的逻辑


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

完毕!!感谢您的收看

----------★★历史博文集合★★----------

我的零基础Python教程,Python入门篇 进阶篇 视频教程 Py安装py项目 Python模块 Python爬虫 Json Xpath 正则表达式 Selenium Etree CssGui程序开发 Tkinter Pyqt5 列表元组字典数据可视化 matplotlib 词云图 Pyecharts 海龟画图 Pandas Bug处理 电脑小知识office自动化办公 编程工具 NumPy Pygame

相关推荐
凯子坚持 c3 分钟前
Qt常用控件指南(8)
开发语言·数据库·qt
疯狂踩坑人13 分钟前
【Python版 2026 从零学Langchain 1.x】(二)结构化输出和工具调用
后端·python·langchain
HDO清风28 分钟前
CASIA-HWDB2.x 数据集DGRL文件解析(python)
开发语言·人工智能·pytorch·python·目标检测·计算机视觉·restful
weixin_4997715531 分钟前
Python上下文管理器(with语句)的原理与实践
jvm·数据库·python
weixin_4521595533 分钟前
高级爬虫技巧:处理JavaScript渲染(Selenium)
jvm·数据库·python
多米Domi01140 分钟前
0x3f 第48天 面向实习的八股背诵第五天 + 堆一题 背了JUC的题,java.util.Concurrency
开发语言·数据结构·python·算法·leetcode·面试
深蓝海拓1 小时前
PySide6从0开始学习的笔记(二十六) 重写Qt窗口对象的事件(QEvent)处理方法
笔记·python·qt·学习·pyqt
纠结哥_Shrek1 小时前
外贸选品工程师的工作流程和方法论
python·机器学习
小汤圆不甜不要钱1 小时前
「Datawhale」RAG技术全栈指南 Task 5
python·llm·rag