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

相关推荐
deephub2 分钟前
Pydantic v2 入门教程:模型、字段、验证器
python·pydantic
LadenKiller4 分钟前
期货量化成交回报漏记:天勤 get_trade 与 trade_records 对账
python
小短腿的代码世界5 分钟前
Qt Quick 3D场景导入与渲染架构深度解析:从USD到PBR材质的完整管线
qt·3d·架构
小短腿的代码世界9 分钟前
Qt文本布局引擎深度解析:从QTextDocument排版到渲染的完整架构
开发语言·qt·架构
MemoriKu10 分钟前
Flutter 相册 APP 视频模态稳定化实战:从远端重构冲突到真机 Smoke Test
人工智能·python·flutter·机器学习·重构·音视频·新人首发
月疯11 分钟前
torch:view和reshape的区别
pytorch·python·深度学习
小短腿的代码世界16 分钟前
Qt Firebase集成深度解析:移动与嵌入式云后端解决方案
开发语言·qt
AC赳赳老秦20 分钟前
OpenClaw + 华为云自动化:批量管理云资源、生成月度云账单分析与成本优化报告
java·开发语言·javascript·人工智能·python·mysql·openclaw
极光代码工作室20 分钟前
基于数据分析的电影票房预测系统
大数据·python·数据分析·spark·数据可视化
量化君也26 分钟前
桥水基金全天候策略拆解,构建中国ETF躺平版策略
大数据·人工智能·python·算法·金融·业界资讯