本文主要是介绍Python制作词云--(中文和英文都可),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.英文
词语:
First, I want to tell you how proud we are. Getting into Columbia is a real testament of what a great well-rounded student you are. Your academic, artistic, and social skills have truly blossomed in the last few years. Whether it is getting the highest grade in Calculus, completing your elegant fashion design, successfully selling your painted running shoes, or becoming one of the top orators in Model United Nations, you have become a talented and accomplished young woman. You should be as proud of yourself as we are.I will always remember the first moment I held you in my arms. I felt a tingling sensation that directly touched my heart. It was an intoxicating feeling I will always have. It must be that "father-daughter connection" which will bind us for life. I will always remember singing you lullaby while I rocked you to sleep. When I put you down, it was always with both relief (she finally fell asleep!) and regret (wishing I could hold you longer). And I will always remember taking you to the playground, and watching you having so much fun. You were so cute and adorable, and that is why everybody loved you so.You have been a great kid ever since you were born, always quiet, empathetic, attentive, and well-mannered. You were three when we built our house. I remember you quietly followed us every weekend for more than ten hours a day to get building supplies. You put up with that boring period without a fuss, happily ate hamburgers every meal in the car, sang with Barney until you fell asleep. When you went to Sunday Chinese school, you studied hard even though it was no fun for you. I cannot believe how lucky we are as parents to have a daughter like you.You have been an excellent elder sister. Even though you two had your share of fights, the last few years you have become best friends. Your sister loves you so much, and she loves to make you laugh. She looks up to you, and sees you as her role model. As you saw when we departed, she misses you so much. And I know that you miss her just as much. There is nothing like family, and other than your parents, your sister is the one person who you can trust and confide in. She will be the one to take care of you, and the one you must take care of. There is nothing we wish more than that your sisterhood will continue to bond as you grow older, and that you will take care of each other throughout your lives. For the next four years, do have a short video chat with her every few days, and do email her when you have a chance.
代码
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 25 17:55:21 2020@author: Dell
"""
import matplotlib.pyplot as plt
import jieba
from wordcloud import WordCloudtext = open(r'ciyun1.txt', "r").read()
cut_text = jieba.cut(text)
result = " ".join(cut_text)
colormap='pink'
wc = WordCloud(background_color='white',# 设置背景宽width=1600,# 设置背景高height=1550,# 最大字体max_font_size=750,# 最小字体min_font_size=30,mode='RGBA'#colormap='pink')
# 产生词云
wc.generate(result)
# 保存图片
wc.to_file(r"ciyun1.png")
plt.imshow(wc)
plt.axis("off")
plt.show()
结果:
2.中文
词语:
首先,我想告诉你我们为你感到特别骄傲。进入哥伦比亚大学证明你是一个全面发展的优秀学生,你的学业、艺术和社交技能最近都有卓越的表现,无论是你在微积分上得了最高分,完成自己典雅的时尚的设计,成功卖出绘制的跑鞋,还是在“模拟联合国”演说中成为表现最突出的人之一,你毫无疑问已经是一个多才多艺的女孩。你的父母为你感到骄傲,你也应该像我们一样为自己感到自豪。
我会永远记得第一次将你抱在臂弯的那一刻,一种新鲜激动的感觉瞬间触动了我的心,那是一种永远让我陶醉的感觉,就是那种将我们的一生都联结在一起的“父女情结”。我也常常想起我唱着催眠曲轻摇你入睡,当我把你放下的时候,常常觉得既解脱又惋惜,一方面我想,她终于睡着了!另一方面,我又多么希望自己可以多抱你一会儿。我还记得带你到运动场,看着你玩得那么开心,你是那样可爱,所有人都非常爱你。
不但长得可爱,而且是个特别乖巧的孩子。你从不吵闹、为人着想,既听话又有礼貌。当你三岁我们建房子的时候,每个周末十多个小时你都静静地跟着我们去运建筑材料,三餐在车上吃着汉堡,唱着儿歌,唱累了就睡觉,一点都不娇气不抱怨。你去上周日的中文学习班时,尽管一点也不觉得有趣,却依然很努力。我们做父母的能有像你这样的女儿真的感到非常幸运。
代码:
import wordcloud #导入词云库
import numpy as np
import matplotlib.pyplot as plt
import PIL
import jieba
import re
with open(r'ciyun2.txt',encoding='utf8') as f:text1 = f.readlines()
#导入图片
image1 = PIL.Image.open(r's.jpg')
MASK = np.array(image1)
WC = WordCloud(scale=4,font_path='msyh.ttf',mask=MASK,background_color='white',max_words =300,max_font_size =160,random_state=20).generate(wl_space_split)
st1 = re.sub('[,。、“”‘ ’]','',str(text1)) #使用正则表达式将符号替换掉。
conten = ' '.join(jieba.lcut(st1)) #此处分词之间要有空格隔开,联想到英文书写方式,每个单词之间都有一个空格。
con = WC.generate(conten)
con.to_file(r"ciyun2.png")
plt.imshow(con)
plt.axis("off")
结果:
这篇关于Python制作词云--(中文和英文都可)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!