Python中文分词、词频统计并制作词云图

中文分词、词频统计并制作词云图是统计数据常用的功能,这里用到了三个模块快速实现这个功能。

中文分词、词频统计

python 复制代码
import jieba
from collections import Counter

# 1. 读取文本内容并进行分词
with open('demo.txt', mode='r', encoding='gbk') as f:
    report = f.read()
words = jieba.cut(report)

# 2. 按指定长度提取词
report_words = []
for word in words:
    if len(word) >= 4:
        report_words.append(word)
print(report_words)

# 3. 统计高频词汇
result = Counter(report_words).most_common(50)
print(result)

上面代码用jieba模块进行分词,用collections进行词频统计。

jieba是一个优秀的第三方中文词库,用于中文分词。中文分词指的是将一个汉字序列切分成一个一个单独的词。jieba可以帮助你快速高效地完成中文分词,支持三种分词模式:精确模式、全模式和搜索引擎模式。

collections是Python标准库中的一个模块,提供了一些额外的容器类型,以提供Python标准内建容器dictlistsettuple的替代选择。这些容器类型包括namedtupledequeCounter等。

简单词云图

python 复制代码
import jieba.posseg as pseg
from collections import Counter
from wordcloud import WordCloud

# 1. 读取文本内容并进行分词
with open('demo.txt', mode='r', encoding='gbk') as f:
    report = f.read()
words = pseg.cut(report)

# 2. 按指定长度和词性提取词
report_words = []
for word, flag in words:
    if (len(word) >= 4) and ('n' in flag):
        report_words.append(word)
# print(report_words)

# 3. 统计高频词汇
result = Counter(report_words).most_common(50)
# print(result)

# 4. 绘制词云图
content = dict(result)
# print(content)
wc = WordCloud(font_path='PINGFANG MEDIUM.TTF', background_color='white', width=1000, height=600)
wc.generate_from_frequencies(content)
wc.to_file('词云图1.png')

这里用到了wordcloud模块来生成词云图。

按照图片绘制词云图

python 复制代码
import jieba.posseg as pseg
from collections import Counter
from PIL import Image
import numpy as np
from wordcloud import WordCloud

# 1. 读取文本内容并进行分词
with open('demo.txt', mode='r', encoding='gbk') as f:
    report = f.read()
words = pseg.cut(report)

# 2. 按指定长度和词性提取词
report_words = []
for word, flag in words:
    if (len(word) >= 4) and ('n' in flag):
        report_words.append(word)
# print(report_words)

# 3. 统计高频词汇
result = Counter(report_words).most_common(300)
# print(result)

# 4. 绘制词云图
mask_pic = Image.open('map.png')
mask_data = np.array(mask_pic)
print(mask_data)
content = dict(result)
wc = WordCloud(font_path='PINGFANG MEDIUM.TTF', background_color='white', mask=mask_data)
wc.generate_from_frequencies(content)
wc.to_file('词云图2.png')

这里给WordCloud加了mask遮罩参数。

按照图片绘制渐变词云图

python 复制代码
import jieba.posseg as pseg
from collections import Counter
from PIL import Image
import numpy as np
from wordcloud import WordCloud, ImageColorGenerator

# 1. 读取文本内容并进行分词
with open('demo.txt', mode='r', encoding='gbk') as f:
    report = f.read()
words = pseg.cut(report)

# 2. 按指定长度和词性提取词
report_words = []
for word, flag in words:
    if (len(word) >= 4) and ('n' in flag):
        report_words.append(word)
# print(report_words)

# 3. 统计高频词汇
result = Counter(report_words).most_common(300)
# print(result)

# 4. 绘制词云图
mask_pic = Image.open('map.png')
mask_data = np.array(mask_pic)
content = dict(result)
wc = WordCloud(font_path='PINGFANG MEDIUM.TTF', background_color='white', mask=mask_data)
wc.generate_from_frequencies(content)
mask_colors = ImageColorGenerator(mask_data)
wc.recolor(color_func=mask_colors)
wc.to_file('词云图3.png')

这里用recolor重绘了颜色。

相关推荐
开开心心就好4 天前
开源免费高速看图工具,支持漫画大图秒开
linux·运维·服务器·安全·ruby·symfony·1024程序员节
unable code7 天前
磁盘取证-Flying_High
网络安全·ctf·misc·1024程序员节·磁盘取证
unable code8 天前
磁盘取证-ColorfulDisk
网络安全·ctf·misc·1024程序员节·内存取证
unable code9 天前
磁盘取证-[第十章][10.1.2 磁盘取证方法]磁盘取证1
网络安全·ctf·misc·1024程序员节·内存取证
开开心心就好10 天前
免费抽奖工具支持批量导入+自定义主题
linux·运维·服务器·macos·pdf·phpstorm·1024程序员节
开开心心就好14 天前
卸载工具清理残留,检测垃圾颜色标识状态
linux·运维·服务器·python·安全·tornado·1024程序员节
子燕若水15 天前
Facebook reels 运营指南
1024程序员节
尘觉18 天前
创作 1024 天|把热爱写成长期主义
数据库·1024程序员节
写点什么呢19 天前
Word使用记录
word·1024程序员节
开开心心就好19 天前
内存清理工具点击清理,自动间隔自启
linux·运维·服务器·安全·硬件架构·材料工程·1024程序员节