爬虫专题机器学习与数据挖掘

《安娜卡列妮娜》词云制作

2017-11-27  本文已影响89人  羽恒

知识点普及

使用第三方库介绍

词云实现

#!/usr/bin/env python
# coding=utf-8
import os
import jieba
import codecs
import pandas as pd
import numpy  as np

from wordcloud import WordCloud,ImageColorGenerator
import matplotlib.pyplot as plt
#导入所用库文件
basefile = data存储路径

# 语料库加载
f_in = codecs.open(basefile+'an.txt','r','utf-8') 
content = f_in.read()
#分词,生成segments列表
segments = []
segs = jieba.cut(content)
for seg in segs:
    if len(seg)>1:
        segments.append(seg)

#生成DataFrame对象
segmentDF =  pd.DataFrame({'segment':segments})
#分组统计
segStat =  segmentDF.groupby(
                by = ['segment']
    )['segment'].agg({ '计数':np.size}).reset_index().sort_values(by = ['计数'],
    ascending = False
    )
#加载停用词
  stopwords = pd.read_csv(
    "./StopwordsCN.txt", 
    encoding='utf8', 
    index_col=False
)
#移除停用词,并做去反操作
fSegStat = segStat[
    ~segStat.segment.isin(stopwords.stopword)
]
#构建词云文件
wordcloud = WordCloud(
    font_path='./simhei.ttf', #词云展示字体
    background_color="black",#词云展示背景颜色
)
words = fSegStat.set_index('segment').to_dict()
wordcloud.fit_words(words['计数'])
plt.imshow(wordcloud)
plt.show()

效果展示

AnnaKarenina

词云美化

from scipy.misc import imread

#读取图片背景
bimg = imread(basefile+'An.png')

wordcloud = WordCloud(
    background_color="white", 
    mask=bimg, font_path='./simhei.ttf'
)

wordcloud = wordcloud.fit_words(words['计数'])

#设置图片大小
plt.figure(
    num=None, 
    figsize=(8, 6), dpi=80, 
    facecolor='w', edgecolor='k'
)
#获取图片颜色
bimgColors = ImageColorGenerator(bimg)

plt.axis("off")
#重置词云颜色
plt.imshow(wordcloud.recolor(color_func=bimgColors))
plt.show()

效果展示

AnnaKarenina
上一篇下一篇

猜你喜欢

热点阅读