WordCloud词云图去除停用词的正确方法

前言

之前我们已经学习了如何使用wordcloud制作英文和中文词云,今天我们接着讲解,在实际制作词云中,有很多词是没有展示出的意义的,例如我,他等主语,那如何不显示这些词了,这就涉及到停用词。

wordcloud自带停用词

wordcloud自带一个停用词表,是一个集合的数据类型。

from wordcloud import STOPWORDS

print(STOPWORDS)

如果我们需要添入一些其他的词的话,也很简单,直接用add或者update方法即可(因为这是集合数据)。

from matplotlib import pyplot as plt
from wordcloud import WordCloud,STOPWORDS

text = 'my is luopan. he is zhangshan'
stopwords = STOPWORDS
stopwords.add('luopan')

wc = WordCloud(stopwords=stopwords)
wc.generate(text)

plt.imshow(wc)

中文停用词使用

用wordcloud库制作中文词云图,必须要分词,所以总结下来,中文中需要设置停用词的话可以有三种方法。

  • 在分词前,将中文文本的停用词先过滤掉。

  • 分词的时候,过滤掉停用词。

  • 在wordcloud中设置stopwords。

在这里我们只讲解第三种方法,设置stopwords,我们需要先有一个中文停用词表,在网上下载即可,然后将停用词表清洗为集合数据格式。

首先我们读取停用词表的内容,设置为集合数据结构。

stopwords = set()
content = [line.strip() for line in open('hit_stopwords.txt','r').readlines()]
stopwords.update(content)
stopwords

接着,我们就对文本进行分词,制作词云图即可。

from matplotlib import pyplot as plt
from wordcloud import WordCloud
import jieba

text = '我叫罗攀,他叫关羽,我叫罗攀,他叫刘备'
cut_word = " ".join(jieba.cut(text))

stopwords = set()
content = [line.strip() for line in open('hit_stopwords.txt','r').readlines()]
stopwords.update(content)

wc = WordCloud(font_path = r'/System/Library/Fonts/Supplemental/Songti.ttc',
              stopwords = stopwords)
wc.generate(cut_word)

plt.imshow(wc)

最后,如何美化词云图,我们下期再见~

  • 24
    点赞
  • 101
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值