#deepin os 测试通过
from wordcloud import WordCloud
import os
cur_path = os.path.dirname(__file__)
with open(os.path.join(cur_path,'love_en.txt')) as fp:
txt = fp.read()
# print(txt)
wordcloud = WordCloud().generate(txt)
image = wordcloud.to_image()
image.show()
#中文词云——————————-
# -*- coding:utf-8 -*-
from wordcloud import WordCloud
import jieba
import os
cur_path = os.path.dirname(__file__)
print(cur_path)
def chinese_jieba(txt):
wordlist_jieba = jieba.cut(txt) #将文本分割,返回列表
#将列表拼接为以空格为间断的字符串
txt_jieba =' '.join(wordlist_jieba)
return txt_jieba
with open(os.path.join(cur_path,'love_en.txt'),'rb') as fp:
txt = fp.read()
txt = chinese_jieba(txt)
#print(txt)
#wordcloud = WordCloud().generate(txt)
#wordcloud = WordCloud(font_path = 'FZLTXIHK.TTF').generate(txt) #增加字体变化
#wordcloud = WordCloud('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', # 字体
#wordcloud = WordCloud('c:/Windows/Fonts/arial.ttf', # 字体
#wordcloud = WordCloud('c:/Windows/Fonts/STXINGKA.TTF', # 字体
wordcloud = WordCloud('c:/Windows/Fonts/STKAITI.TTF', # 字体
background_color = 'black', # 背景色
max_words = 30, # 最大显示单词数
max_font_size = 60 # 频率最大单词字体大小
).generate(txt)
image = wordcloud.to_image()
image.show()
#win10 调试通过
#调试这段代码,1、注意open()函数读取方式采用'rb'方式,2、系统字体的名称看上米娜的示例
#运行需要的库有:pip install wordcloud jieba matplotlib,如果不制作中文的词云,不需要jieba库