接单日记(三)文本处理之词云生成

接单日记(三)文本处理之词云生成

此为一个实验报告,故遵守实验报告的格式。

一、 实验目的

  1. 熟悉Python第三方库python-docx、wordcloud、jieba库的安装和使用
  2. 熟悉使用pathlib来获取文件
  3. 熟悉运用Python封装的思想
  4. 熟悉使用join方法对字符串进行拼接操作
  5. 了解字符串的utf-8的编码格式

二、 实验内容

编写一个程序,提取词库里面的所有内容,对其进行分词操作,同时进行词频统计,停用词清洗的操作,最后输出图云到result.jpg中。

三、 程序及结果

1、 运行程序

from docx import Document
from pathlib import Path
from wordcloud import WordCloud
import jieba
font = Path(r"C:\Windows\Fonts\simfang.ttf")
word_dataset = Path("词库.docx")
stop_word = Path("stoplist.txt")
def get_stop_list(stop_word):
with open(stop_word, "r", encoding="utf-8") as f:
return set(f.read().split())
def handle_word_dataset(word_dataset):
str_ = ""
for j in Document(word_dataset).paragraphs:
str_ += j.text
return [w for w in jieba.cut(str_)]
wc = WordCloud(
font_path=str(font),
stopwords=get_stop_list(stop_word),
width=1920,
height=1080,
background_color="white",
max_words=1000,
).generate(" ".join(handle_word_dataset(word_dataset)))
wc.to_file(Path("result.jpg"))

2、 运行结果

posted @   Kenny_LZK  阅读(51)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
点击右上角即可分享
微信分享提示