Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

词云代码

#-*- coding:utf-8 -*-
from scipy.misc import imread
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import jieba
from collections import Counter
import codecs
text = codecs.open('app.txt','r','utf-8').read()
text = """
如果那两个字没有颤抖
我不会发现我难受
怎么说出口
也不过是分手
如果对于明天没有要求
牵牵手就像旅游
成千上万个门口
总有一个人要先走
怀抱既然不能逗留
何不在离开的时候
一边享受 一边泪流
十年之前
我不认识你 你不属于我
我们还是一样
陪在一个陌生人左右
走过渐渐熟悉的街头
十年之后
我们是朋友 还可以问候
只是那种温柔
再也找不到拥抱的理由
情人最后难免沦为朋友
怀抱既然不能逗留
何不在离开的时候
一边享受 一边泪流
十年之前
我不认识你 你不属于我
我们还是一样
陪在一个陌生人左右
走过渐渐熟悉的街头
十年之后
我们是朋友 还可以问候
只是那种温柔
再也找不到拥抱的理由
情人最后难免沦为朋友
直到和你做了多年朋友
才明白我的眼泪
不是为你而流
也为别人而流
"""
# 使用 jieba 分词
text_jieba = list(jieba.cut(text))
# 使用 counter 做词频统计,选取出现频率前 100 的词汇
c = Counter(text_jieba)
common_c = c.most_common(100)
# 读入图片
bg_pic = imread('love.jpg')
# 配置词云参数
wc = WordCloud(
    # 设置字体
    font_path = r'C:windowsfontssimfang.ttf',
    # 设置背景色
    background_color='white',
    # 允许最大词汇
    max_words=200,
    # 词云形状
    mask=bg_pic,
    # 最大号字体
    max_font_size=100,
)
# 生成词云
#wc.generate_from_frequencies(dict(common_c))
wc.generate(text)
# 生成图片并显示
plt.figure()
plt.imshow(wc)
plt.axis('off')
plt.show()
# 保存图片
wc.to_file('test.jpg')

windows 全是坑 whl地址

$ pip install wordcloud-1.2.1-cp27-cp27m-win32.whl
wordcloud-1.2.1-cp27-cp27m-win32.whl is not a supported wheel on this platfor
m.
$mv wordcloud-1.2.1-cp27-cp27m-win32.whl wordcloud-1.2.1-cp27-none-win32.whl
$ pip install wordcloud-1.2.1-cp27-none-win32.whl
Unpacking .wordcloud-1.2.1-cp27-none-win32.whl
Installing collected packages: wordcloud
Successfully installed wordcloud
Cleaning up...
$ pip install scipy-0.19.0-cp27-none-win32.whl
Unpacking .scipy-0.19.0-cp27-none-win32.whl
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.8.2 in d:pyt
hon27libsite-packages (from scipy==0.19.0)
Installing collected packages: scipy
Successfully installed scipy
Cleaning up...
$ python mycloud.py
Traceback (most recent call last):
  File "mycloud.py", line 2, in <module>
    from scipy.misc import imread
  File "d:python27libsite-packagesscipy\__init__.py", line 61, in <module>
    from numpy._distributor_init import NUMPY_MKL  # requires numpy+mkl
ImportError: cannot import name NUMPY_MKL
$ pip install numpy-1.12.1+mkl-cp27-cp27m-win32.whl
numpy-1.12.1%2Bmkl-cp27-cp27m-win32.whl is not a supported wheel on this platfor
m.
 
$ pip install numpy-1.12.1+mkl-cp27-none-win32.whl
Unpacking .
umpy-1.12.1+mkl-cp27-none-win32.whl
Installing collected packages: numpy
Cleaning up...
Exception:
Traceback (most recent call last):
  File "d:python27libsite-packagespip-1.5.6-py2.7.eggpipasecommand.py", l
ine 122, in main
    status = self.run(options, args)
  File "d:python27libsite-packagespip-1.5.6-py2.7.eggpipcommandsinstall.p
y", line 283, in run
    requirement_set.install(install_options, global_options, root=options.root_p
ath)
  File "d:python27libsite-packagespip-1.5.6-py2.7.eggpip
eq.py", line 1435
, in install
    requirement.install(install_options, global_options, *args, **kwargs)
  File "d:python27libsite-packagespip-1.5.6-py2.7.eggpip
eq.py", line 671,
 in install
    self.move_wheel_files(self.source_dir, root=root)
  File "d:python27libsite-packagespip-1.5.6-py2.7.eggpip
eq.py", line 901,
 in move_wheel_files
    pycompile=self.pycompile,
  File "d:python27libsite-packagespip-1.5.6-py2.7.eggpipwheel.py", line 21
5, in move_wheel_files
    clobber(source, lib_dir, True)
  File "d:python27libsite-packagespip-1.5.6-py2.7.eggpipwheel.py", line 20
9, in clobber
    shutil.copy2(srcfile, destfile)
  File "d:python27libshutil.py", line 130, in copy2
    copyfile(src, dst)
  File "d:python27libshutil.py", line 83, in copyfile
    with open(dst, 'wb') as fdst:
IOError: [Errno 13] Permission denied: 'd:\python27\Lib\site-packages\numpy
core\multiarray.pyd'
$ python mycloud.py
Building prefix dict from the default dictionary ...
Loading model from cache c:usersxxxappdatalocalempjieba.cache
Loading model cost 0.393 seconds.
Prefix dict has been built succesfully.
Traceback (most recent call last):
  File "mycloud.py", line 75, in <module>
    wc.generate(text)
  File "d:python27libsite-packageswordcloudwordcloud.py", line 448, in gene
rate
    return self.generate_from_text(text)
  File "d:python27libsite-packageswordcloudwordcloud.py", line 434, in gene
rate_from_text
    self.generate_from_frequencies(words)
  File "d:python27libsite-packageswordcloudwordcloud.py", line 317, in gene
rate_from_frequencies
    font = ImageFont.truetype(self.font_path, font_size)
  File "d:python27libsite-packagesPILImageFont.py", line 218, in truetype
    return FreeTypeFont(filename, size, index, encoding)
  File "d:python27libsite-packagesPILImageFont.py", line 134, in __init__
    self.font = core.getfont(file, size, index, encoding)
  File "d:python27libsite-packagesPILImageFont.py", line 34, in __getattr__

    raise ImportError("The _imagingft C module is not installed")
ImportError: The _imagingft C module is not installed

崩溃


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
161 views
Welcome To Ask or Share your Answers For Others

1 Answer

等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...