21年政府报告词云python

编程入门 行业动态 更新时间:2024-10-18 12:27:41

21年政府<a href=https://www.elefans.com/category/jswz/34/1770268.html style=报告词云python"/>

21年政府报告词云python

  • 实验内容

1、题目

21年政府报告词云

2、实验代码

  1. #20203101694 孙成  
  2. from wordcloud import WordCloud, ImageColorGenerator  
  3. from PIL import Image  
  4. import matplotlib.pyplot as plt  
  5. import jieba  
  6. import numpy as np  
  7.   
  8. def test_chinese_text():  
  9.     # 打开文本  
  10.     with open('2021政府工作报告.txt', encoding='utf-8') as f:  
  11.         text = f.read()  
  12.     print(text)  
  13.     # 排除不需要的  
  14.     counts = {}  
  15.     for word in text:  
  16.         if len(word) == 1:  # 排除单个字符的分词结果  
  17.             continue  
  18.         else:  
  19.             counts[word] = counts.get(word, 0) + 1  
  20.     # 生成对象  
  21.     wc = WordCloud(font_path='simhei.ttf', width=800, height=600, mode='RGBA', background_color=None).generate(text)  
  22.     print(wc)  
  23.     # 显示词云  
  24.     plt.imshow(wc, interpolation='bilinear')  
  25.     plt.axis('off')  
  26.     plt.show()  
  27.   
  28.     # 保存到文件  
  29.     wc.to_file('wordcloud.png')  # 生成图像是透明的  
  30.   
  31. def test_english_text():  
  32.     # 打开文件  
  33.     text = open('2021政府工作报告.txt').read()  
  34.   
  35.     # 生成对象  
  36.     wc = WordCloud().generate(text=text)  
  37.   
  38.     # 显示词云  
  39.     plt.imshow(wc, interpolation='bilinear')  
  40.     plt.axis('off')  
  41.     plt.show()  
  42.   
  43.     # 保存文件  
  44.     wc.to_file('wordcloud.png')  
  45.   
  46. def test_ch_text_cut():  
  47.     # 打开文本  
  48.     text = open('2021政府工作报告.txt', encoding='utf-8').read()  
  49.   
  50.     # 中文分词  
  51.     print(jieba.cut(text))  
  52.     text = ' '.join(jieba.cut(text))  
  53.     print(text)  
  54.   
  55.     # 生成对象  
  56.     wc = WordCloud(font_path='simhei.ttf', width=800, height=600, mode='RGBA', background_color=None).generate(text)  
  57.   
  58.     # 显示词云  
  59.     plt.imshow(wc, interpolation='bilinear')  
  60.     plt.axis('off')  
  61.     plt.show()  
  62.     # 保存到文件  
  63.     wc.to_file('wordcloud3.png')  
  64.   
  65. def test_word_cloud_pic():  
  66.     # 打开文本  
  67.     text = open('2021政府工作报告.txt', encoding='utf-8').read()  
  68.   
  69.     # 中文分词  
  70.     text = ' '.join(jieba.cut(text))  
  71.   
  72.    # 生成对象  
  73.     mask = np.array(Image.open("touxiang.JPG"))  
  74.     wc = WordCloud(mask=mask, font_path='simhei.ttf', mode='RGBA', background_color=None).generate(text)  
  75.   
  76.     # 从图片中生成颜色  
  77.     image_colors = ImageColorGenerator(mask)  
  78.     wc.recolor(color_func=image_colors)  
  79.     #显示原始图像  
  80.     im = Image.open('touxiang.JPG')  
  81.     im.show()  
  82.     # 显示词云  
  83.     plt.imshow(wc, interpolation='bilinear')  
  84.     plt.axis("off")  
  85.     plt.show()  
  86.   
  87.     # 保存到文件  
  88.     wc.to_file('wordcloud5.png')  
  89.   
  90.   
  91. if __name__ == '__main__':  
  92.     test_chinese_text()  
  93.     #test_english_text()  
  94.     #test_ch_text_cut()  
  95.     #test_word_cloud_pic()  

3、运行结果

 

 

 

二、实验报告

请同学们每次实验记得签到,把每次上机的内容保存好,将所有的实验内容压缩为:学号+姓名,统一发给班长,第16到17发送到:346697954@qq邮箱。

 

更多推荐

21年政府报告词云python

本文发布于:2024-02-24 14:49:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1695665.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:报告   政府   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!