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

该实验报告介绍了使用Python的python-docx,wordcloud和jieba库进行词云生成的过程。首先,提取词库内容并进行分词,接着进行词频统计和停用词过滤,最后生成1920x1080的词云图片result.jpg。
摘要由CSDN通过智能技术生成

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

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

一、 实验目的

  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、 运行结果

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SteveKenny

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值