首页
搜索 搜索
快讯 >

接单日记(三)文本处理之词云生成 环球新资讯

2023-05-10 12:15:53 博客园


(资料图)

目录
  • 接单日记(三)文本处理之词云生成
    • 一、 实验目的
    • 二、 实验内容
    • 三、 程序及结果
      • 1、 运行程序
      • 2、 运行结果

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

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

一、 实验目的

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

二、 实验内容

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

三、 程序及结果

1、 运行程序

from docx import Documentfrom pathlib import Pathfrom wordcloud import WordCloudimport jiebafont = 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、 运行结果