Chinese Word cloud code debugging and Chinese Language debugging

Source: Internet
Author: User

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.

 

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.