-------------词云图集合-------------
用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应用程序。它允许用户通过图形界面选择文本文件、停用词文件、背景颜色,并生成词云图。
# -*- 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_())
完毕!!感谢您的收看
----------★★历史博文集合★★----------