First Python word cloud attempt

Source: Internet
Author: User

1 Analysis of English text
1  fromWordcloudImportWordcloud2 ImportOS3 4Cur_path = Os.path.dirname (__file__)5 6With open (Os.path.join (Cur_path,'Love_en.txt') ) as FP:7TXT =Fp.read ()8     #print (TXT)9Wordcloud =Wordcloud (). Generate (TXT)TenImage =wordcloud.to_image () OneImage.show ()

Error occurred, error type: Oserror:cannot Open resource

Solution: Add fonts

Corrected post code:

1  fromWordcloudImportWordcloud2 ImportOS3 4Cur_path = Os.path.dirname (__file__)5 6With open (Os.path.join (Cur_path,'Love_en.txt') ) as FP:7TXT =Fp.read ()8     #print (TXT)9Wordcloud = Wordcloud (Font_path = ' fzltxihk. TTF '). Generate (TXT)TenImage =wordcloud.to_image () OneImage.show ()

Further optimizing the code:

1  fromWordcloudImportWordcloud2 ImportOS3 4Cur_path = Os.path.dirname (__file__)5 6With open (Os.path.join (Cur_path,'Love_en.txt') ) as FP:7TXT =Fp.read ()8     #print (TXT)9Wordcloud = Wordcloud (Font_path ='FZLTXIHK. TTF',#FontTenBackground_color ='Black',#Background Color OneMax_words = 30,#maximum number of displayed words AMax_font_size = 60#frequency maximum word font size - ). Generate (TXT) -Image =wordcloud.to_image () theImage.show ()

2 analyzing Chinese text
1 ImportJieba2  fromWordcloudImportWordcloud3 ImportOS4 5Cur_path = Os.path.dirname (__file__)6 7 defChinese_jieba (TXT):8Wordlist_jieba = jieba.cut (TXT)#split text, return to list9Txt_jieba =" ". Join (Wordlist_jieba)#stitching a list into a string that breaks with spacesTen     returnTxt_jieba One  AStopwords = {'these'70A'those who'70A'because'70A'so': 0}#noise word -  -With open (Os.path.join (Cur_path,'Select the day note. txt') ) as FP: theTXT =Fp.read () -TXT =Chinese_jieba (TXT) -     #print (TXT) -Wordcloud = Wordcloud (Font_path ='FZLTXIHK. TTF',#Font +Background_color ='Black',#Background Color -Max_words = 30,#maximum number of displayed words +Max_font_size = 60,#frequency maximum word font size AStopwords = Stopwords#filter Noise words at ). Generate (TXT) -Image =wordcloud.to_image () -Image.show ()

3 Further optimize the display effect
1 ImportJieba2  fromWordcloudImportWordcloud3 ImportOS4 ImportNumPy5 ImportPIL. Image as Image6 7Cur_path = Os.path.dirname (__file__)8 9 defChinese_jieba (TXT):TenWordlist_jieba = jieba.cut (TXT)#split text, return to list OneTxt_jieba =" ". Join (Wordlist_jieba)#stitching a list into a string that breaks with spaces A     returnTxt_jieba -  -Stopwords = {'these'70A'those who'70A'because'70A'so': 0}#noise word theMask_pic = Numpy.array (Image.open (Os.path.join (Cur_path,'love.jpg'))) -  -With open (Os.path.join (Cur_path,'Select the day note. txt') ) as FP: -TXT =Fp.read () +TXT =Chinese_jieba (TXT) -     #print (TXT) +Wordcloud = Wordcloud (Font_path ='FZLTXIHK. TTF',#Font ABackground_color =' White',#Background Color atMax_words = 100,#maximum number of displayed words -Max_font_size = 60,#frequency maximum word font size -Stopwords = Stopwords,#filter Noise words -Mask = Mask_pic#Customize the displayed - ). Generate (TXT) -Image =wordcloud.to_image () inImage.show ()

First Python word cloud attempt

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.