zoukankan      html  css  js  c++  java
  • python 词云

    # 导入扩展库

    import re # 正则表达式库
    import collections # 词频统计库
    import numpy as np # numpy数据处理库
    import jieba # 结巴分词
    import wordcloud # 词云展示库
    from PIL import Image # 图像处理库
    import matplotlib.pyplot as plt # 图像展示库


    fs = open("sanguo.txt","r",encoding="utf-8") #三国的txt,可以网上下载
    txt = fs.read()
    fs.close()

    rt = jieba.lcut(txt) # 将段落的句子切割成一个个的词语
    rt_txt = " ".join(rt) 词语之间使用空格进行拼接
    mask = np.array(Image.open('time2.jpg')) # 定义词云图片背景
    wc = wordcloud.WordCloud(
    font_path='/home/syroot/zst_directory/font/msyh.ttf', # 设置字体格式
    width=1200,
    height=800,
    mask=mask, # 设置背景图
    max_words=200, # 最多显示词数
    max_font_size=200 # 字体最大值
    ).generate(rt_txt)
    image_colors = wordcloud.ImageColorGenerator(mask) # 从背景图建立颜色方案
    wc.recolor(color_func=image_colors) # 将词云颜色设置为背景图方案
    plt.imshow(wc,interpolation="bilinear") # 显示词云
    plt.axis('off') # 关闭坐标轴
  • 相关阅读:
    2.6
    zuoye
    练习1
    练习
    练习
    4
    3
    2
    1
    1.3
  • 原文地址:https://www.cnblogs.com/zst-blogs/p/12147998.html
Copyright © 2011-2022 走看看