Chinese Word cloud code debugging and Chinese Language debugging
Word cloud is a very interesting thing.
Use jieba to break words and text into "mori.txt". In the "stopword.txt" List of deprecated words, the results of the deprecated words are good or bad, and the deprecated words are very important and need to be adjusted and supplemented constantly.
From wordcloud import WordCloudimport jiebaf = open(u'mori.txt ', 'R '). read () # cuttext = "". join (jieba. cut (f) cuttext = jieba. cut (f) final = [] stopwords1_open(u'stopword.txt ', 'R '). read () for seg in cuttext: # seg = seg. encode ('utf-8') if seg [0] not in ['0', '1', '2', '3', '4', '5 ', '6', '7', '8', '9']: # ignore the number if seg not in stopwords: final. append (seg) # Add font = r to the list "c:/Windows/Fonts/simsun. ttc "# wordcloud = WordCloud (font_path = font, background_color =" white ", width = 1000, height = 860, margin = 2) must be added for Chinese display ). generate ("". join (final) import matplotlib. pyplot as pltplt. imshow (wordcloud) plt. axis ("off") plt. show ()
Wordcloud.to_file('test.png ')
:
The following code sorts Word Frequency Statistics and word lengths.
# Count Word Frequency freqD2 ={} for word2 in final: freqD2 [word2] = freqD2.get (word2, 0) + 1 # output counter_list = sorted (freqD2.items () by word frequency (), key = lambda x: x [1], reverse = True) _ 2000 = counter_list [0] [1] + 1 print (_ 2000) # fp = open('sort.txt ', "w +", encoding = 'utf-8') for d in counter_list: fp. write (d [0] + ':' + str (d [1]) fp. write ('\ n') fp. close () # Sort out counter_list = sorted (freqD2.items (), key = lambda x: len (x [0]) * _ 2000 + x [1], reverse = True) fp = open('sortlen.txt ', "w +", encoding = 'utf-8') for d in counter_list: fp. write (d [0] + ':' + str (d [1]) fp. write ('\ n') fp. close ()
The sorting code is very convenient and worth learning. Python is a good thing, powerful, and easy to reuse.