首页 > 编程 > Python > 正文

python 词云 wordcloud

2019-11-08 18:42:14
字体:
来源:转载
供稿:网友
Word_cloud 生成词云有两个方法。from text 和 from frequencies 。如果我们处理英文文档的话,就可以直接使用第一个方法,而不需要提前预处理文本,由于我们要做的是中文文本,所以我们必须自己分词,并处理为数组,使用第二种方法。
# -*- coding: utf-8 -*-from os import pathfrom wordcloud import WordCloudd = path.dirname(__file__)# Read the whole text.text = open(path.join(d, 'hua.txt')).read()#frequencies = [(u'知乎',5),(u'小段同学',4),(u'曲小花',3),(u'中文分词',2),(u'样例',1)]# Generate a word cloud image 此处原为 text 方法,我们改用 frequencies wordcloud = WordCloud().generate(text)#wordcloud = WordCloud().fit_words(frequencies)# Display the generated image:# the matplotlib way:import matplotlib.pyplot as pltplt.imshow(wordcloud)plt.axis("off")# take relative word frequencies into account, lower max_font_sizewordcloud = WordCloud(max_font_size=40, relative_scaling=.5).generate(text)#wordcloud = WordCloud(max_font_size=40, relative_scaling=.5).fit_words(frequencies)plt.figure()plt.imshow(wordcloud)plt.axis("off")plt.show()# The pil way (if you don't have matplotlib)#image = wordcloud.to_image()#image.show()
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表