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

相关推荐
甄超锋19 分钟前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
AntBlack1 小时前
不当韭菜V1.1 :增强能力 ,辅助构建自己的交易规则
后端·python·pyqt
杜子不疼.3 小时前
《Python学习之字典(一):基础操作与核心用法》
开发语言·python·学习
myzzb4 小时前
基于uiautomation的自动化流程RPA开源开发演示
运维·python·学习·算法·自动化·rpa
TLuoQiu4 小时前
小电视视频内容获取GUI工具
爬虫·python
我叫黑大帅4 小时前
【CustomTkinter】 python可以写前端?😆
后端·python
胡耀超4 小时前
DataOceanAI Dolphin(ffmpeg音频转化教程) 多语言(中国方言)语音识别系统部署与应用指南
python·深度学习·ffmpeg·音视频·语音识别·多模态·asr
java1234_小锋4 小时前
一周学会Matplotlib3 Python 数据可视化-绘制自相关图
开发语言·python·信息可视化·matplotlib·matplotlib3
前端市界6 小时前
前端视角: PyQt6+Vue3 跨界开发实战
前端·qt·pyqt
Juchecar6 小时前
分析:将现代开源浏览器的JavaScript引擎更换为Python的可行性与操作
前端·javascript·python