用多少眼泪才能让你相信 发表于 5 天前

Python作业4 文本词云统计,天生词云

编写程序,统计两会政府工作报告热词频率,并天生词云。
2025两会政府工作报告
import jieba
import wordcloud
from collections import Counter
import re

# 读取文件
with open("gov.txt", "r", encoding="gbk") as f:
    t = f.read()

# 分词处理
ls = jieba.lcut(t)

# 定义过滤函数
def is_valid_word(word):
    # 过滤条件:
    # 1. 长度至少为2个字符(过滤单字)
    # 2. 只包含中文(\u4e00-\u9fff)
    # 3. 不是停用词(可选)
    return (len(word) >= 2 and
            all('\u4e00' <= char <= '\u9fff' for char in word))

# 严格过滤
filtered_words =

# 统计词频
word_counts = Counter(filtered_words)

# 打印前20个高频词
print("=== 纯中文热词统计 ===")
for word, count in word_counts.most_common(20):
    print(f"{word}: {count}次")

# 生成词云
txt = " ".join(filtered_words)
w = wordcloud.WordCloud(
    font_path="msyh.ttc",
    width=1000,
    height=700,
    background_color="white",
    max_words=200# 限制词云显示的最大词数
)
w.generate(txt)
w.to_file("wordcloud.png") <img alt=""src="https://i-blog.csdnimg.cn/direct/1e5c4767793a43e28396a1390b9a6648.png"/>

<img alt=""src="https://i-blog.csdnimg.cn/direct/ce0386d394d74bb693ac8a1b52d49acc.png"/>

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Python作业4 文本词云统计,天生词云