-------------词云图集合-------------
WordCloud去掉停用词(fit_words+generate)的2种用法
通过词频来绘制词云图(jieba+WordCloud)
Python教程95:去掉停用词+词频统计+jieba.tokenize示例用法
将进酒—李白process_text词频统计,及词频可视化分析
使用wordcloud模块,绘制一个自定义的词云图形状
使用WordCloud模块中repeat参数,做一个关键字重复的词云图
关于词云图显示异常,出现乱码的解决办法
盘点WordCloud模块,词云图的相关知识点
Python源码05:使用Pyecharts画词云图图
在WordCloud中去掉停用词,可以通过设置stopwords参数来实现,也可以通过下面字典数据的过滤方法来去掉不想要的词语。停用词是指在文本中频繁出现但对文本含义贡献很小的词语,如虚词、助词、介词、连词等。下面写的2个例子,可以给大家参考一下。
1.W.generate通常结合stopwords过滤停用词的方法,运行代码就会生成如下图。可以看到原文本(杨过是欧阳锋的义子,他的黯然销魂掌,可以和郭靖的降龙十八掌媲美),文本里面的 是,和,的,它等需要都被去掉了。
# -*- coding: utf-8 -*-
# @Author : 小红牛
# 微信公众号:WdPython
import jieba
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
# 1.读取文本
text = '杨过是欧阳锋的义子,他的黯然销魂掌,可以和郭靖的降龙十八掌媲美。'
print('1.原文本:'.center(50, '-'))
print(text)
# 2.分词
cut_word = jieba.cut(text, cut_all=False)
cut_word = ' '.join(cut_word)
print('2.jieba分词后的内容:'.center(50, '-'))
print(cut_word)
# 3.准备停用词表
stopwords = set(STOPWORDS)
print('3.WordCloud自带的英文停用词表:'.center(50, '-'))
print(stopwords)
# 自定义添加停用词
stopwords.add('是')
stopwords.add('的')
stopwords.add('和')
stopwords.add('他')
stopwords.add('可以')
# 4.创建WordCloud对象并生成词云,stopwords=stopwords,
w = WordCloud(background_color='WHITE', stopwords=stopwords, height=400,
width=700, font_path='simkai.ttf')
w.generate(cut_word)
# 5.保存词云图图片
w.to_file('cloud.png')
# 显示词云图
plt.imshow(w)
plt.axis('off')
plt.show()
print('词云图生成完成。')
输出内容:
----------------------1.原文本:----------------------
杨过是欧阳锋的义子,他的黯然销魂掌,可以和郭靖的降龙十八掌媲美。
------------------2.jieba分词后的内容:------------------
杨过 是 欧阳锋 的 义子 , 他 的 黯然销魂 掌 , 可以 和 郭靖 的 降龙十八掌 媲美 。
--------------3.WordCloud自带的英文停用词表:---------------
{"you'll", "doesn't", 'yourself', 'after', 'by', "we're", 'other', 'and', 'just', 'this', "when's", 'hence', 'own', 'ours', 'myself', "won't", 'otherwise', 'what', 'on', 'can', 'else', "we'll", 'k', 'him', "haven't", "they'll", "she's", "i'd", 'there', 'itself', 'of', "you've", "there's", 'herself', 'off', 'his', 'once', "that's", 'very', 'was', 'ever', 'would', 'until', 'why', 'me', 'had', "where's", 'does', 'over', 'shall', 'if', 'since', "what's", 'a', 'some', 'the', "shouldn't", 'we', 'where', 'any', 'an', 'she', 'them', "hadn't", "he'll", "he's", 'who', "it's", 'he', 'ourselves', 'also', 'you', 'most', 'were', "i'll", 'r', 'with', 'at', 'yourselves', 'have', 'when', 'www', "mustn't", 'about', 'up', 'did', "let's", 'are', 'few', 'be', 'being', 'too', 'their', 'cannot', 'more', 'to', "here's", "why's", 'having', "didn't", 'hers', 'no', 'in', 'under', 'how', 'again', "he'd", 'her', 'so', "they're", "she'll", 'against', 'however', 'my', "couldn't", "i'm", "wouldn't", "she'd", "aren't", 'been', 'above', 'before', 'those', "we'd", 'such', "hasn't", "isn't", 'not', 'only', 'http', "they'd", 'nor', 'because', 'i', "you'd", 'which', 'they', "who's", 'it', 'could', 'into', "can't", 'out', 'while', 'down', "how's", 'ought', 'am', "don't", 'each', 'himself', 'through', "i've", 'whom', 'from', 'is', 'therefore', 'themselves', 'our', 'all', 'but', 'same', "shan't", "you're", 'as', 'between', 'has', 'then', "we've", 'both', 'its', 'these', 'should', 'like', 'or', "they've", 'do', 'theirs', "weren't", 'below', 'yours', 'com', 'for', 'than', 'here', 'get', 'that', "wasn't", 'your', 'further', 'doing', 'during'}
2.通过w.fit_words(参数为字典类型)+Counter(参数可以是字符串,也可以是可迭代的对象,返回字典类型)+jieba(参数是字符串,返回是generator类型)。下面代码中会按照关键字+词频的阈值去自定义过滤,不符合条件的词频(和字典数据知识点的用法一样),以上2种处理过滤的结果是一样。个人觉得使用w.fit_words比W.generate生成词云图,要更直观一点,因为它可以根据词频的大小,在词云图上反应出字体的大小。简单说某个词频非常高,那么它的字号在词云图上显示的就越大。
# -*- coding: utf-8 -*-
# @Author : 小红牛
# 微信公众号:WdPython
import re
import jieba
from collections import Counter
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
# 1.读取文本
text = '杨过是欧阳锋的义子,他的黯然销魂掌,可以和郭靖的降龙十八掌媲美。'
# 使用正则,只取中文的字符,过滤英文数字,各种标点符号等等
text = re.findall('[\u4e00-\u9fff]+', text)
text = ''.join(text)
print('1.只取中文文本:'.center(50, '-'))
print(text)
# 2.分词
cut_word = jieba.cut(text, cut_all=False)
word_freq = Counter(cut_word)
print('2.过滤前的分词词频:'.center(50, '-'))
print(word_freq)
# 自定义定义过滤条件
min_freq = 0 # 最小词频阈值
exclude_words = {'是', '的', '和', '可以', '他'} # 要排除的词列表
# 过滤词频字典
filtered_word_freq = {word: freq for word, freq in word_freq.items()
if freq >= min_freq and word not in exclude_words}
print('3.过滤后词频:'.center(50, '-'))
print(filtered_word_freq)
# 4.创建WordCloud对象并生成词云
w = WordCloud(background_color='WHITE', height=400, width=700,
font_path='simkai.ttf')
w.fit_words(filtered_word_freq)
# 5.保存词云图图片
w.to_file('cloud.png')
# 显示词云图
plt.imshow(w)
plt.axis('off')
plt.show()
print('词云图生成完成。')
输出内容:
--------------------1.只取中文文本:---------------------
杨过是欧阳锋的义子他的黯然销魂掌可以和郭靖的降龙十八掌媲美
Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\Ms-xiao\AppData\Local\Temp\jieba.cache
Loading model cost 1.151 seconds.
Prefix dict has been built successfully.
--------------------2.过滤前的词频:---------------------
Counter({'的': 3, '杨过': 1, '是': 1, '欧阳锋': 1, '义子': 1, '他': 1, '黯然销魂': 1, '掌': 1, '可以': 1, '和': 1, '郭靖': 1, '降龙十八掌': 1, '媲美': 1})
---------------------3.过滤后词频:---------------------
{'杨过': 1, '欧阳锋': 1, '义子': 1, '黯然销魂': 1, '掌': 1, '郭靖': 1, '降龙十八掌': 1, '媲美': 1}
词云图生成完成。
完毕!!感谢您的收看
----------★★历史博文集合★★----------