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重绘了颜色。

相关推荐
数据皮皮侠AI7 天前
顶刊同款!中国地级市风灾风险与损失数据集(2000-2022)|灾害 / 环境 / 经济研究必备
大数据·人工智能·笔记·能源·1024程序员节
Fab1an8 天前
Busqueda——Hack The Box 靶机
linux·服务器·学习·1024程序员节
技术专家8 天前
Stable Diffusion系列的详细讨论 / Detailed Discussion of the Stable Diffusion Series
人工智能·python·算法·推荐算法·1024程序员节
学传打活11 天前
古代汉语是源,现代汉语是流,源与流一脉相承。
微信公众平台·1024程序员节·汉字·中华文化
学传打活16 天前
【边打字.边学昆仑正义文化】_19_星际生命的生存状况(1)
微信公众平台·1024程序员节·汉字·昆仑正义文化
unable code23 天前
[HNCTF 2022 WEEK2]ez_ssrf
网络安全·web·ctf·1024程序员节
unable code24 天前
[NISACTF 2022]easyssrf
网络安全·web·ctf·1024程序员节
unable code25 天前
BUUCTF-[第二章 web进阶]SSRF Training
网络安全·web·ctf·1024程序员节
开开心心就好1 个月前
进程启动瞬间暂停工具,适合调试多开
linux·运维·安全·pdf·智能音箱·智能手表·1024程序员节
仰泳之鹅1 个月前
【51单片机】第一课:单片机简介与软件安装
单片机·嵌入式硬件·51单片机·1024程序员节