惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
IT之家
IT之家
博客园 - Franky
Stack Overflow Blog
Stack Overflow Blog
宝玉的分享
宝玉的分享
Recent Announcements
Recent Announcements
Engineering at Meta
Engineering at Meta
S
SegmentFault 最新的问题
V
Visual Studio Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Last Week in AI
Last Week in AI
H
Help Net Security
V
V2EX
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
博客园 - 叶小钗
J
Java Code Geeks
博客园 - 【当耐特】
月光博客
月光博客
爱范儿
爱范儿
人人都是产品经理
人人都是产品经理
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件

材料与逻辑

如何理解动力粘度η 铝合金牌号系列对比及选型指南 TPE选型攻略 一次搞懂PPAP:从提交到批准的完整指南 铝合金阳极氧化工艺详解 APQP 第五阶段:量产、反馈、评定与纠正 APQP 第四阶段:产品与过程验证 APQP 第三阶段:过程设计开发 控制计划(Control Plan)在制造业中的深度解析 控制计划(CP):制造业质量管理的核心支柱 OpenClaw 使用指南 PFMEA系统学习与制造业质量工具集成 第一性原理系统学习报告:从认知底层到创新实践 桌面端 EPUB 工具:Jane Reader及不同阅读器功能对比 APQP第二阶段(产品设计和开发)学习与实施指南 APQP第一阶段:顾客声音(VOC)向项目指标转化的标准化指南 制造业研发技术中的WBS(工作分解结构)应用 一个玩具车项目,讲清新版 APQP 怎么干 质量是设计出来的:APQP 核心框架与全流程详解 思维导图系统学习与职场应用指南 ISO 4892 塑料实验室光源暴露试验标准系统梳理与应用分析 高分子材料挤出成型工艺系统研发技术报告 制造业研发人员如何制定高效的项目开发计划:从材料开发到工艺落地的系统性指南 材料耐候性评价指南:色差值 ΔE 与抗老化等级的标准转换及年限推算 自动化工具:将 Markdown 文档转换为静态 PNG 思维导图脚本 溯因推理:原理、应用与智能演进研究 高分子加工工艺对比与选型参考 归纳推理:从经验观察到知识泛化的系统化研究 铝合金及其关键部件表面处理技术 通过 rclone 自动备份 Lsky Pro(兰空图床)数据至 OneDrive
自制多功能词云图生成程序——多文件分析、删除关键词等
ZhangYong · 2026-02-14 · via 材料与逻辑
1
pip3 install jieba wordcloud matplotlib
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110


import jieba
from collections import Counter
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import argparse
import os
import sys

def get_macos_font():
"""自动化获取 macOS 可用的中文字体路径"""
paths = [
"/System/Library/Fonts/STHeiti Light.ttc",
"/System/Library/Fonts/PingFang.ttc",
"/Library/Fonts/Arial Unicode.ttf",
"/System/Library/Fonts/Hiragino Sans GB.ttc"
]
for p in paths:
if os.path.exists(p):
return p
return None

def generate_combined_analysis(file_paths, font_path=None, stop_words_path="~/Project/Python_wordcloud/stopwords/scu_stopwords.txt", exclude_words=None, skip_top=0):
all_word_counts = Counter()
processed_files = 0


filter_set = set()
if stop_words_path and os.path.exists(stop_words_path):
with open(stop_words_path, 'r', encoding='utf-8') as f:
filter_set.update([line.strip() for line in f.readlines() if line.strip()])
if exclude_words:
filter_set.update(exclude_words)


for file_path in file_paths:
if not os.path.exists(file_path):
print(f"跳过不存在的文件: {file_path}")
continue
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
if not content.strip(): continue
words = jieba.cut(content)
filtered = [w for w in words if len(w) > 1 and w not in filter_set]
all_word_counts.update(filtered)
processed_files += 1
except Exception as e:
print(f"读取文件 {file_path} 出错: {e}")


if not all_word_counts:
print("错误:未提取到任何有效关键词,请检查输入文件内容或编码。")
return

if skip_top > 0:
top_n = [item[0] for item in all_word_counts.most_common(skip_top)]
print(f"已跳过最高频词: {top_n}")
for w in top_n:
del all_word_counts[w]

if not all_word_counts:
print("错误:跳过高频词后,数据为空。")
return


final_font = font_path
if not final_font or not os.path.exists(final_font):
if sys.platform == "darwin":
final_font = get_macos_font()
elif sys.platform == "win32":
final_font = "C:/Windows/Fonts/simhei.ttf"

if not final_font or not os.path.exists(final_font):
print("错误:未找到有效的字体文件,请通过 --font 参数手动指定。")
return


try:
print(f"正在使用字体: {final_font}")
wc = WordCloud(
font_path=final_font,
background_color='white',
width=1000,
height=800,
max_words=150
)
wc.generate_from_frequencies(all_word_counts)

output_name = "analysis_result.png"
wc.to_file(output_name)
print(f"成功!处理文件: {processed_files} | 结果保存至: {os.path.abspath(output_name)}")


print("最终分析高频词 (Top 20):", all_word_counts.most_common(20))

except Exception as e:
print(f"词云生成失败,具体原因: {e}")

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="多文件词频分析工具")
parser.add_argument("filenames", nargs='+', help="文件路径")
parser.add_argument("--font", help="手动指定字体路径")
parser.add_argument("--stop", help="停用词路径")
parser.add_argument("--exclude", "-e", nargs='*', help="排除特定词")
parser.add_argument("--skip_top", type=int, default=0, help="跳过前N个词")

args = parser.parse_args()
generate_combined_analysis(args.filenames, args.font, args.stop, args.exclude, args.skip_top)
1
alias get_wc="~/Project/Python_wordcloud/.venv/bin/python3 ~/Project/Python_wordcloud/get_word_cloud.py"
1
python3 get_word_cloud.py *song* --skip_top 8 -e tags null

该文章介绍了一个自主开发、面向中文文本的多功能词云图生成工具,核心目标是提升文本关键词可视化分析的实用性与灵活性。不同于基础词云脚本,该项目具备显著的工程化特征:

整体体现了“小而美”的数字人文工具理念:以轻量 Python 脚本为载体,融合自然语言处理(jieba 分词)、数据聚合(Counter)、可视化(WordCloud + Matplotlib)与系统交互(跨平台字体探测),在学术分析、内容运营、教学演示等场景中具备直接复用价值。