栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

词云图python代码_词云图Python?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

词云图python代码_词云图Python?

一共三个文件

Hlm.pystop_words.txt红楼梦.txt

(tips: txt文件已放在文章最后)

废话不多说,直接上效果图和代码:

    效果图Hlm.py 代码部分:
import matplotlib.pyplot as plt
import jieba
import wordcloud
import matplotlib

matplotlib.rcParams['font.sans-serif'] = ['simple'] #设置绘图字体
def wordFreq(filepath,text,topn):
    #jieba分词库分词
    words = jieba.lcut(text.strip())
    counts = {}
    #列表生成式获取停用词
    stopwords = [line.strip() for line in open('stop_words.txt', 'r', encoding='utf-8').readlines()] 
    word_clear = [] #用于生成词云的词语列表,避免重复分词,节约运行时间
    
    #统计词频
    for word in words:
        if(len(word) == 1):
            continue
        elif word not in stopwords:
            if word == "凤姐儿":
                word = "凤姐"
            elif word == "林黛玉" or word == "林妹妹" or word == "黛玉笑":
                word == "黛玉"
            elif word == "宝二爷":
                word == "宝玉"
            elif word == "袭人道":
                word == "袭人"
            word_clear.append(word)
            counts[word] = counts.get(word, 0) + 1

    items = list(counts.items())   
    items.sort(key = lambda x:x[1], reverse=True)
    for i in range(topn):
        word, count = items[i]
        print(f"{word}:{count}")
    return word_clear

def gen_cloudword(txt):
    wcloud=wordcloud.WordCloud(font_path = r'C:WindowsFontssimhei.ttf', width=1000,  max_words = 100, height = 860, margin = 2).generate(txt)
    wcloud.to_file("红楼梦cloud_star.png")      #  保存图片
    #显示词云图片 
    plt.imshow(wcloud) 
    plt.axis('off') 
    plt.show()

text = open('红楼梦.txt', "r",encoding='utf-8').read() 
words_clear = wordFreq('红楼梦.txt',text,10)

gen_cloudword(' '.join(words_clear)) 

txt文本文件
链接:https://pan.baidu.com/s/1ZPvhT0rJddGPS4YUoYzzjQ?pwd=1234
提取码:1234

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/786752.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号