Python WordCloud库与jieba分词生成词云图的完整指南

Python WordCloud库与jieba分词生成词云图的完整指南

关键技术点及代码示例

1. 安装必要的库

使用pip安装wordcloudjieba库:

bash 复制代码
pip install wordcloud
pip install jieba

2. jieba分词

精确模式
python 复制代码
import jieba

text = "Python是广泛使用的编程语言。它被用于网站开发、数据分析、人工智能等多个领域。"
seg_list = jieba.cut(text, cut_all=False)  # 精确模式
print("精确模式: " + "/ ".join(seg_list))
搜索引擎模式
python 复制代码
seg_list = jieba.cut_for_search(text)  # 搜索引擎模式
print("搜索引擎模式: " + "/ ".join(seg_list))

3. 去除停用词

创建一个停用词列表,并从分词结果中去除停用词:

python 复制代码
with open('stopwords.txt', 'r', encoding='utf-8') as f:
    stopwords = [line.strip() for line in f.readlines()]

words = [word for word in seg_list if word not in stopwords and len(word) > 1]

4. 统计词频

使用collections.Counter类统计词频:

python 复制代码
from collections import Counter

counter = Counter(words)
for word, count in counter.most_common(10):
    print(word, count)

5. 生成词云图

创建WordCloud对象并生成词云图:

python 复制代码
from wordcloud import WordCloud
import matplotlib.pyplot as plt

wordcloud = WordCloud(font_path='path_to_your_chinese_font.ttf',  # 指定中文字体路径
                      background_color='white').generate_from_frequencies(dict(counter))

plt.figure(figsize=(10, 5))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')  # 不显示坐标轴
plt.show()

6. 保存词云图

将生成的词云图保存为图片文件:

python 复制代码
wordcloud.to_file('wordcloud.png')

完整代码

结合以上关键技术点,以下是生成词云图的完整代码:

python 复制代码
import jieba
from collections import Counter
from wordcloud import WordCloud
import matplotlib.pyplot as plt

text = "Python是广泛使用的编程语言。它被用于网站开发、数据分析、人工智能等多个领域。"

# 使用jieba进行中文分词
seg_list = jieba.cut(text, cut_all=False)  # 精确模式

# 要有这个文件stopwords.txt   去除停用词
with open('stopwords.txt', 'r', encoding='utf-8') as f:
    stopwords = [line.strip() for line in f.readlines()]
words = [word for word in seg_list if word not in stopwords and len(word) > 1]

# 统计词频
counter = Counter(words)
# 打印词频最高的10个词
for word, count in counter.most_common(10):
    print(word, count)

# 生成词云图
wordcloud = WordCloud(font_path='C:/Windows/Fonts/simhei.ttf',  # 指定中文字体路径
                      background_color='white').generate_from_frequencies(dict(counter))

# 显示词云图
plt.figure(figsize=(10, 5))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')  # 不显示坐标轴
plt.show()

# 保存词云图
wordcloud.to_file('wordcloud.png')

注意事项

  • 确保stopwords.txt文件中包含了你想要去除的停用词,每行一个词。
  • font_path参数需要指向一个有效的中文字体文件路径,否则中文字符将无法正确显示。
  • path_to_your_chinese_font.ttf需要替换为你实际的中文字体文件路径。
  • stopwords.txtwordcloud.png是示例文件名,你可以根据需要修改它们。

通过上述代码,你可以实现从中文文本的分词到词云图的生成和保存的完整流程。这是一个非常实用的文本数据可视化工具,可以帮助你快速理解文本数据中的关键信息。

相关推荐
一只鹿鹿鹿8 小时前
智慧水利一体化建设方案
大数据·运维·开发语言·数据库·物联网
witAI8 小时前
**AI仿真人剧制作软件2025推荐,解锁沉浸式数字内容创作
人工智能·python
没有医保李先生9 小时前
字节对齐的总结
java·开发语言
Elastic 中国社区官方博客10 小时前
使用 Elastic 进行网络监控:统一网络可观测性
大数据·开发语言·网络·人工智能·elasticsearch·搜索引擎·全文检索
Codefengfeng10 小时前
Python Base环境中加包的方法
开发语言·python
清水白石00810 小时前
《Python 编程全景解析:从核心精要到测试替身(Test Doubles)五大武器的实战淬炼》
开发语言·python
如若12311 小时前
AutoDL云服务器 NVIDIA 570驱动 EGL渲染修复全记录
运维·服务器·python
甲枫叶11 小时前
【claude】Claude Code正式引入Git Worktree原生支持:Agent全面实现并行独立工作
java·人工智能·git·python·ai编程
六件套是我11 小时前
无法访问org.springframeword.beans.factory.annotation.Value
java·开发语言·spring boot
S-码农11 小时前
Linux ——条件变量
linux·开发语言