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

相关推荐
linuxxx1101 天前
django测试缓存命令的解读
python·缓存·django
毕设源码-邱学长1 天前
【开题答辩全过程】以 基于Python的Bilibili平台数据分析与可视化实现为例,包含答辩的问题和答案
开发语言·python·数据分析
千疑千寻~1 天前
【Qt】QT的程序打包
开发语言·qt
咚咚王者1 天前
人工智能之编程进阶 Python高级:第十一章 过渡项目
开发语言·人工智能·python
莫小墨1 天前
基于TCP/IP和UDP组播的Qt网络直播间项目
网络·qt·tcp/ip·udp
A尘埃1 天前
大模型应用python+Java后端+Vue前端的整合
java·前端·python
A尘埃1 天前
LLM大模型评估攻略
开发语言·python
一晌小贪欢1 天前
【Python办公】处理 CSV和Excel 文件操作指南
开发语言·python·excel·excel操作·python办公·csv操作
檀越剑指大厂1 天前
【Python系列】fastapi和flask中的阻塞问题
python·flask·fastapi