当前位置: 首页 > web >正文

Python实例题:使用Python定制词云

目录

Python实例题

题目

代码实现

实现原理

文本处理:

词云生成:

可视化与输出:

关键代码解析

1. 文本预处理与分词

2. 词云生成

3. 图像颜色映射

使用说明

安装依赖:

基本用法:

使用自定义字体:

使用蒙版图像:

从图像获取颜色:

自定义参数:

扩展建议

增强功能:

性能优化:

应用扩展:

界面改进:

Python实例题

题目

使用Python定制词云

代码实现

import jieba
import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
import numpy as np
from PIL import Image
import argparse
import osclass CustomWordCloud:def __init__(self):"""初始化词云生成器"""self.stopwords = set(STOPWORDS)# 添加中文停用词self.chinese_stopwords = {'的', '了', '在', '是', '我', '有', '和', '就', '不', '人', '都', '一', '一个', '上', '也', '很', '到', '说', '要', '去', '你', '会', '着', '没有', '看', '好', '自己', '这'}self.stopwords.update(self.chinese_stopwords)# 默认字体路径(根据系统不同可能需要修改)self.font_path = Noneself._set_default_font()def _set_default_font(self):"""设置默认字体路径,根据不同操作系统自动选择"""if os.name == 'nt':  # Windows系统self.font_path = r"C:\Windows\Fonts\simhei.ttf"  # 黑体elif os.name == 'posix':  # Linux/Mac系统# 尝试常见的中文字体位置possible_fonts = ["/System/Library/Fonts/PingFang.ttc",  # macOS"/usr/share/fonts/truetype/wqy/wqy-microhei.ttc",  # Linux文泉驿微米黑"/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",  # Linux Noto字体]for font in possible_fonts:if os.path.exists(font):self.font_path = fontbreakdef set_font_path(self, font_path):"""设置自定义字体路径"""if os.path.exists(font_path):self.font_path = font_pathreturn Trueelse:print(f"错误:字体文件不存在 - {font_path}")return Falsedef add_stopwords(self, words):"""添加自定义停用词"""if isinstance(words, str):self.stopwords.add(words)elif isinstance(words, list):self.stopwords.update(words)def preprocess_text(self, text):"""预处理文本,包括分词和停用词过滤"""# 使用jieba进行中文分词words = jieba.cut(text)# 过滤停用词并连接processed_text = " ".join([word for word in words if word not in self.stopwords])return processed_textdef generate_from_text(self, text, mask=None, background_color='white', max_words=200, width=800, height=400, contour_width=0, contour_color='steelblue',colormap=None, prefer_horizontal=0.9):"""从文本生成词云Args:text: 输入文本mask: 蒙版图像(numpy数组)background_color: 背景颜色max_words: 最大词数width: 词云宽度height: 词云高度contour_width: 轮廓线宽度contour_color: 轮廓线颜色colormap: 颜色映射名称prefer_horizontal: 水平放置词的比例"""if not self.font_path:raise ValueError("未找到合适的字体,请使用set_font_path方法设置字体路径")# 预处理文本processed_text = self.preprocess_text(text)# 创建词云对象wc = WordCloud(font_path=self.font_path,background_color=background_color,max_words=max_words,mask=mask,contour_width=contour_width,contour_color=contour_color,colormap=colormap,prefer_horizontal=prefer_horizontal,width=width,height=height,stopwords=self.stopwords)# 生成词云wc.generate(processed_text)return wcdef generate_from_frequencies(self, frequencies, mask=None, background_color='white', width=800, height=400, contour_width=0, contour_color='steelblue', colormap=None):"""从词频字典生成词云Args:frequencies: 词频字典 {词: 频率}mask: 蒙版图像(numpy数组)background_color: 背景颜色width: 词云宽度height: 词云高度contour_width: 轮廓线宽度contour_color: 轮廓线颜色colormap: 颜色映射名称"""if not self.font_path:raise ValueError("未找到合适的字体,请使用set_font_path方法设置字体路径")# 创建词云对象wc = WordCloud(font_path=self.font_path,background_color=background_color,mask=mask,contour_width=contour_width,contour_color=contour_color,colormap=colormap,width=width,height=height,stopwords=self.stopwords)# 从词频生成词云wc.generate_from_frequencies(frequencies)return wcdef color_by_image(self, wc, image_path):"""根据图像颜色生成词云颜色Args:wc: 词云对象image_path: 图像路径Returns:颜色函数"""try:image = Image.open(image_path)image_array = np.array(image)image_colors = ImageColorGenerator(image_array)return wc.recolor(color_func=image_colors)except Exception as e:print(f"从图像获取颜色失败: {e}")return wcdef save_wordcloud(self, wc, output_path):"""保存词云图像"""try:wc.to_file(output_path)print(f"词云已保存到: {output_path}")except Exception as e:print(f"保存词云失败: {e}")def display_wordcloud(self, wc, title=None):"""显示词云图像"""plt.figure(figsize=(10, 8))plt.imshow(wc, interpolation="bilinear")if title:plt.title(title, fontsize=15)plt.axis("off")plt.tight_layout(pad=0)plt.show()def main():parser = argparse.ArgumentParser(description='定制词云生成器')parser.add_argument('--text', help='输入文本文件路径')parser.add_argument('--output', help='输出图像文件路径', default='wordcloud.png')parser.add_argument('--mask', help='蒙版图像路径')parser.add_argument('--font', help='字体文件路径')parser.add_argument('--background', help='背景颜色', default='white')parser.add_argument('--max_words', type=int, help='最大词数', default=200)parser.add_argument('--width', type=int, help='词云宽度', default=800)parser.add_argument('--height', type=int, help='词云高度', default=400)parser.add_argument('--contour_width', type=float, help='轮廓宽度', default=0)parser.add_argument('--contour_color', help='轮廓颜色', default='steelblue')parser.add_argument('--colormap', help='颜色映射名称')parser.add_argument('--color_by_image', help='根据图像着色')parser.add_argument('--stopwords', help='停用词文件路径')parser.add_argument('--title', help='显示的标题')args = parser.parse_args()# 创建词云生成器wc_generator = CustomWordCloud()# 设置自定义字体if args.font:wc_generator.set_font_path(args.font)# 添加自定义停用词if args.stopwords:try:with open(args.stopwords, 'r', encoding='utf-8') as f:custom_stopwords = [line.strip() for line in f.readlines()]wc_generator.add_stopwords(custom_stopwords)except Exception as e:print(f"加载停用词失败: {e}")# 加载文本if not args.text:print("请提供输入文本文件路径 (--text)")returntry:with open(args.text, 'r', encoding='utf-8') as f:text = f.read()except Exception as e:print(f"读取文本文件失败: {e}")return# 加载蒙版图像mask = Noneif args.mask:try:mask_image = Image.open(args.mask).convert("RGB")mask = np.array(mask_image)except Exception as e:print(f"加载蒙版图像失败: {e}")# 生成词云wc = wc_generator.generate_from_text(text,mask=mask,background_color=args.background,max_words=args.max_words,width=args.width,height=args.height,contour_width=args.contour_width,contour_color=args.contour_color,colormap=args.colormap)# 根据图像着色if args.color_by_image:wc = wc_generator.color_by_image(wc, args.color_by_image)# 保存词云wc_generator.save_wordcloud(wc, args.output)# 显示词云wc_generator.display_wordcloud(wc, args.title)if __name__ == "__main__":main()    

实现原理

这个定制词云生成器基于以下核心技术实现:

  • 文本处理

    • 使用 jieba 库进行中文分词
    • 支持自定义停用词过滤
    • 文本预处理和词频统计
  • 词云生成

    • 使用 wordcloud 库创建词云
    • 支持自定义形状、颜色和字体
    • 支持从文本或词频生成词云
  • 可视化与输出

    • 使用 matplotlib 显示词云
    • 支持保存为图片文件
    • 支持从图像获取颜色映射

关键代码解析

1. 文本预处理与分词

def preprocess_text(self, text):# 使用jieba进行中文分词words = jieba.cut(text)# 过滤停用词并连接processed_text = " ".join([word for word in words if word not in self.stopwords])return processed_text

2. 词云生成

def generate_from_text(self, text, mask=None, background_color='white', max_words=200, width=800, height=400, contour_width=0, contour_color='steelblue',colormap=None, prefer_horizontal=0.9):if not self.font_path:raise ValueError("未找到合适的字体")processed_text = self.preprocess_text(text)wc = WordCloud(font_path=self.font_path,background_color=background_color,max_words=max_words,mask=mask,contour_width=contour_width,contour_color=contour_color,colormap=colormap,prefer_horizontal=prefer_horizontal,width=width,height=height,stopwords=self.stopwords)wc.generate(processed_text)return wc

3. 图像颜色映射

def color_by_image(self, wc, image_path):try:image = Image.open(image_path)image_array = np.array(image)image_colors = ImageColorGenerator(image_array)return wc.recolor(color_func=image_colors)except Exception as e:print(f"从图像获取颜色失败: {e}")return wc

使用说明

  • 安装依赖

pip install jieba wordcloud matplotlib pillow numpy
  • 基本用法

python custom_wordcloud.py --text sample.txt --output wordcloud.png
  • 使用自定义字体

python custom_wordcloud.py --text sample.txt --font "C:/Windows/Fonts/simhei.ttf"
  • 使用蒙版图像

python custom_wordcloud.py --text sample.txt --mask shape.png
  • 从图像获取颜色

python custom_wordcloud.py --text sample.txt --mask shape.png --color_by_image colors.png
  • 自定义参数

python custom_wordcloud.py --text sample.txt --background black --max_words 300 --colormap viridis

扩展建议

  • 增强功能

    • 添加更多文本处理选项(词性过滤、词干提取等)
    • 实现词云动画效果
    • 添加交互式词云可视化
    • 支持多语言混合处理
  • 性能优化

    • 优化大规模文本的处理速度
    • 添加并行处理支持
    • 实现增量更新词云
  • 应用扩展

    • 开发社交媒体内容词云分析工具
    • 实现文档关键词提取与可视化
    • 结合 NLP 技术进行情感分析词云
    • 开发批量生成词云的功能
  • 界面改进

    • 开发图形界面应用
    • 添加实时预览功能
    • 支持参数调整滑块
    • 导出多种格式的图像
http://www.xdnf.cn/news/9286.html

相关文章:

  • 基于深度学习的语音识别系统设计与实现
  • OpenCV CUDA模块图像处理------颜色空间处理之用于执行伽马校正(Gamma Correction)函数gammaCorrection()
  • Jenkins分配对应项目权限与用户管理
  • Linux中的常用命令
  • JSON全面解析
  • Qt基础终结篇:从文件操作到多线程异步UI,深度解析核心要点
  • -资产收集篇FridaHOOKXposed证书提取单向双向检验抓包
  • Logi鼠标切换桌面失效
  • ubuntu2x.xx网络不通如何解决
  • 《计算机组成原理》第 9 章 - 控制单元的功能
  • 光电赋能低空场景,灵途科技助力无人机持续升级
  • 红黑树,B树,二叉树之间的不同
  • 【监控】Prometheus中的告警机制介绍
  • 异常:UnsupportedOperationException: null
  • 【Java开发日记】说一说序列化与反序列化中存在的问题
  • 前端 reconnecting-websocket 包
  • 【AI工具应用】使用 trae 实现 word 转成 html
  • 实验设计与分析(第6版,Montgomery)第3章单因子实验:方差分析3.11思考题3.4 R语言解题
  • Python训练打卡Day35
  • vLLM 核心技术 PagedAttention 原理详解
  • SLOT:测试时样本专属语言模型优化,让大模型推理更精准!
  • 对于ARM开发各种手册的分类
  • 计算机网络-网络层
  • 赋能低空经济 | 奇妙智能已掌握AI+无人机智慧巡检技术
  • Linux `cd` 命令深度解析与高阶应用指南
  • JavaScript 性能优化按层次逐步分析
  • Autodesk Maya 2025.3新特性
  • 【HTML-13】HTML表格合并技术详解:打造专业数据展示
  • 力扣经典算法篇-13-接雨水(较难,动态规划,加法转减法优化,双指针法)
  • go tour泛型