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