今日学习了python的词云技术
from os import path
from wordcloud import WordCloud
import matplotlib.pyplot as plt
d=path.dirname(__file__)
text=open(path.join(d,"data//constitution.txt")).read()
# 步骤3-2:设置一张词云图对象
wordcloud = WordCloud(background_color="white", max_font_size=40).generate(text)
# 步骤4-1:创建一个图表画布
plt.figure()
# 步骤4-2:设置图片
plt.imshow(wordcloud, interpolation="bilinear")
# 步骤4-3:取消图表x、y轴
plt.axis("off")
# 显示图片
plt.show()
结果如下:这是没有背景图的词云
接下来这个是爱丽丝漫游小说的词云
from os import path
from PIL import Image
import numpy as np
from wordcloud import WordCloud
import matplotlib.pyplot as plt
d=path.dirname(__file__)
text=open(path.join(d,"data//alice.txt")).read()
alice_mask = np.array(Image.open(path.join(d, "data/alice_mask.png")))
wordcloud=WordCloud(background_color="white",max_words=2000,mask=alice_mask)
wordcloud.generate(text)
wordcloud.to_file(path.join(d,"images//alice_word.png"))
用英文做词云很简单,不需要很麻烦的分词技术,用wordclo