赞
踩
目录
“词云”这个概念由美国西北大学新闻学副教授、新媒体专业主任里奇·戈登(Rich Gordon)于提出,词云是一种可视化描绘单词或词语出现在文本数据中频率的方式,它主要是由随机分布在词云图的单词或词语构成,出现频率较高的单词或词语则会以较大的形式呈现出来,而频率越低的单词或词语则会以较小的形式呈现。词云主要提供了一种观察社交媒体网站上的热门话题或搜索关键字的一种方式,它可以对网络文本中出现频率较高的“关键词”予以视觉上的突出,形成“关键词云层”或“关键词渲染”,从而过滤掉大量的文本信息,使浏览网页者只要一眼扫过文本就可以领略文本的主旨。
我们需要安装一些基本的库:(因为wordcloud库,jieba库不是python的内置库)
windows+R,打开cmd,在命令行输入:
pip install wordcloud
等待安装完成即可。同样的方法安装jieba库;
直接再pycharm软件中安装:
打开pycharm,找到pythong软件包,在搜索框中搜索要下载的库,点击安装即可。
(如果第一次安装失败的话,直接再次尝试安装,基本上第二次是可以成功的)。
- file = open(r"test.txt", mode="r",encoding="utf-8")
- txt1 = file.read()
-
- txt2 = re.sub(r"[^\u4e00-\u9fa5]","",txt1)
这里re.sub()函数可以看:re.sub()用法的详细介绍_jackandsnow的博客-CSDN博客_re sub
我们平常看到的词云:
如果整个文本直接生成词云肯定是不行的,接下来我们就需要对文本进行分词操作;
- txt3 = jieba.cut(txt2) # 可迭代对象
-
- #for i in txt3:
- # print(i)
这里我们就用到了准备工作中的jieba库:
jieba.cut(s) 精确模式:把文本精确的切分开,不存在冗余单词;
这样txt3中的保存的就是一个个的词语:
- txt4 = {}
- for i in txt3:
- if i not in txt4:
- txt4[i]=1
- else:
- txt4[i]+=1
-
- txt5 = sorted(txt4.items(),key=lambda x :x[1],reverse=True)
-
-
- txt6={}
- for word,count in txt5:
- txt6[word]=count
这里我们把结果存放到字典里面,因为{key:value},可以存放值和出现次数;
存放好后,我们进行排序;
- sorted函数是默认升序排序,当需要降序排序时,需要使用reverse = Ture;
- 这里的items是将txt4字典转换为一个列表;
我们排好序后,还需要将这个列表转换为一个字典;
统计并排好词后我们打印一下:
如果里面有不想出现的词语,可以把它删去:
- list1={'的','和','我','我们','会','可以','是','我会','例如'} #去除不想要的词语
- for i in list1:
- del txt6[i]
- img =Image.open("R-C.jpg")//我们想要的词云模板导入
-
- img_array = np.array(img)
-
- wordcloud = WordCloud(
- mask=img_array, # 设置词云模板
- background_color='white', #背景颜色
- font_path='simsun.ttc', #字体路径
- max_words=500, #最大显示的单词数
- max_font_size=100, #单词最大字号
- width = 500, #宽度
- height= 500, #高度
- ).generate_from_frequencies(txt6)
- plt.Figure(figsize=(8,8)) #画布大小
- plt.imshow(wordcloud,interpolation='bilinear')
- plt.axis('off') #关闭坐标轴
- plt.show()
wordcloud.to_file('词云图片.jpg') # 保存图片
- import re
- import matplotlib.pyplot as plt
- from wordcloud import WordCloud
- import jieba
- import numpy as np
- from PIL import Image
-
- file = open(r"test.txt", mode="r",encoding="utf-8")
- txt1 = file.read()
- # print(txt1) #原始文本
- txt2 = re.sub(r"[^\u4e00-\u9fa5]","",txt1)
- # print(txt2) #进化后的文本
-
- txt3 = jieba.cut(txt2) # 可迭代对象
- # for i in txt3:
- # print(i)
-
- txt4 = {}
- for i in txt3:
- if i not in txt4:
- txt4[i]=1
- else:
- txt4[i]+=1
- txt5 = sorted(txt4.items(),key=lambda x :x[1],reverse=True)
- # print(txt5)
-
- txt6={}
- for word,count in txt5:
- txt6[word]=count
- print(txt6)
-
-
- list1={'的','和','我','我们','会','可以','是','我会','例如'} #去除不想要的词语
- for i in list1:
- del txt6[i]
- # print(txt6)
-
-
- img =Image.open("R-C.jpg")
-
- img_array = np.array(img)
-
-
- wordcloud = WordCloud(
- mask=img_array, # 设置词云模板
- background_color='white', #背景颜色
- font_path='simsun.ttc', #字体路径
- max_words=500, #最大显示的单词数
- max_font_size=100, #单词最大字号
- width = 500, #宽度
- height= 500, #高度
- ).generate_from_frequencies(txt6)//使用给定的词频数据txt6生成词云图。
- plt.Figure(figsize=(8,8)) #画布大小
- plt.imshow(wordcloud,interpolation='bilinear')//将生成的词云图像绘制在 Matplotlib 图形对象
- plt.axis('off') #关闭坐标轴
- plt.show()//显示生成词云图
- wordcloud.to_file('词云图片.jpg') # 保存图片
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
完结!!
对于第3和第4步可以简化一下:
- 分词:文本数据处理好后,使用分词工具进行分词,也就是将词分成一个个词语;
- 统计词频:也就是统计每个词语出现的次数;
可以直接用python内置的Counter()来统计:
- t3=list(jieba.cut(t2))
- t4=Counter(t3)
这样t4 就是一个字典,其中键是单词,值是该单词在文本中出现的次数。
简化后的代码(主要是文本处理的简化,生成词云的步骤基本上没有变):
-
- import re
- import jieba
- from collections import Counter
- from wordcloud import WordCloud
- import matplotlib.pyplot as plt
- import numpy as np
- from PIL import Image
-
- f=open('test.txt','r',encoding='utf-8')
- t1=f.read()
- t2=re.sub(r"[^\u4e00-\u9fa5]","",t1)
- t3=list(jieba.cut(t2))
- t4=Counter(t3)
-
-
- l=['的','和','我们','我','是','他们','那','在','什么']
- for i in l:
- del t4[i]
-
- img=Image.open('R-C.jpg')
- img_array=np.array(img)
- # img_array 变量将包含图像的像素数据
-
- wordcloud=WordCloud(
- mask=img_array,
- background_color='white',
- font_path='simsun.ttc',
- max_words=500,
- max_font_size=100,
- width=500,
- height=500
- ).generate_from_frequencies(t4)
- plt.Figure(figsize=(8,8)) #画布大小
- plt.imshow(wordcloud,interpolation='bilinear')
- plt.axis('off') #关闭坐标轴
- plt.show()
- wordcloud.to_file('词云图片.jpg') # 保存图片
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
完结!!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。