用户:
朱晓宇1查看:0 回复:0 评论:0 创建时间:2022-02-27T12:10:51
【作品展示】

【作品介绍】
做一个词云
【作品源代码】
import jieba
from PIL import Image
import wordcloud
while True:
a = input("请输入要读取的文件名:")
b = len(a)
b -= 4
if a[b:] != ".txt":
print("请输入后缀为.txt的文件。")
else:
break
while True:
WIDTH = int(input('请输入词云高度:(500-2000)'))
if WIDTH >= 500 and WIDTH <= 2000:
break
else:
print('高度错误')
while True:
HEIGHT = int(input('请输入词云宽度:(500-2000)'))
if HEIGHT >= 500 and HEIGHT <= 2000:
break
else:
print('宽度错误')
while True:
bgc = input('请输入背景颜色(black,white,browm,blue,pink,gold)')
if bgc != 'black' and bgc != 'white' and bgc != 'brown' and bgc != 'blue' and bgc != 'pink' and bgc != 'gold':
print('背景颜色错误。')
else:
break
while True:
cm = input('请输入字体色系:(gnuplot,pla喵a,pri喵,hsv,rainbow,Blues)')
if cm != 'gnuplot' and cm != 'pla喵a' and cm != 'pri喵' and cm != 'hsv' and cm != 'rainbow' and cm != 'Blues':
print('字体色系错误。')
else:
break
a = open(a, 'r', encoding='utf-8')
txt = a.read()
a.close()
while True:
language = input('请输入文本语言(中文,英文)')
if language == '中文':
words = []
words = jieba.lcut(txt)
m = ' '.join(words)
while True:
front = input("请输入要使用的字体:(windows用simhei.ttf,Mac用PingFang.ttc)")
if front != 'simhei.ttf' and front != 'PingFang.ttc':
print('字体错误')
else:
break
s = {'你', '我', '她', '他', '的', '了', '是', '一', '在', '不', '有', '说', '就', '也', '这', '人', '着', '去', '和', '上', '到', '来', '个', '地', '一个', '好', '得', '很', '又', '看', '要', '里', '还', '没有', '把', '什么', '对', '那', '多', '大', '想', '给', '自己', '过', '吗', ' 会', '二', '三', '四', '五', '六', '七', '八', '九', '十', '从', '中', '您', '几',
'出', '它', '下', '而', '知道', '明白', '可以', '用', '没', '时候', '最', '叫', '做', '天', '只', '老师', '听', '为', '再', '但', '起来', '时', '才', '这个', '太', '像', '让', '买', '现在', '却', '这样', '们', '点', '真', '已经', '事', '使', '次', '问', '等', '做', '被', '可', '种', '家', '啊', '应为', '怎么', '位', '钱', '跟', '与', '请', '他们', '便', '晓得', '问'}
w = wordcloud.WordCloud(
width=WIDTH,
height=HEIGHT,
font_path=front,
stopwords=s,
background_color=bgc,
colormap=cm
)
w.generate(m)
w.to_file('词云1.png')
p=Image.open('词云1.png')
p.show()
elif language == '英文':
s = wordcloud.STOPWORDS
s.add('another')
w = wordcloud.WordCloud(
width=WIDTH,
height=HEIGHT,
stopwords=s,
background_color=bgc,
colormap=cm
)
else:
print('语言错误')
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn