Python generates word clouds online

Source: Internet
Author: User

Gross steps

1 receives the text in the request, processing the text through the stutter participle.

Seg_generator = jieba.cut (text)#use stuttering participle, or you can not useStopwords = pd.read_csv (path +"/stop_words_zh_utf-8.txt", Index_col=false, quoting=3, sep="\ t", names=['Stopword'], encoding='Utf-8')#quoting=3 all not quotedSeg_list = [i forIinchSeg_generatorifI not inchStopwords] Seg_list= [I forIinchSeg_listifI! = u' '] Seg_list= R' '. Join (seg_list)Printseg_listreturnSeg_list

2 Create a picture and return

WC = Wordcloud (font_path= path +'/simhei.ttf',#Set FontBackground_color="Black",#Background Colormax_words=2000,#Word Cloud Maximum number of words displayed                #max_font_size=100, #字体最大值Random_state=42,                )    #Generate Word cloud, you can enter all the text with generate (Chinese bad word), we can also calculate the frequency after the use of generate_from_frequencies functionwc.generate (SEG_LISG)#wc.generate_from_frequencies (txt_freq)    #Txt_freq Example for [(' Word a ', 100), (' Word b ', 90), (' Word c ', ')]    #generate a color value from a background image    #wc.to_file ("B.png")IMG =wc.to_image ()returnImg

Python's web schema is configured with the Django URL:

  URL (r'^wordcloud$', Word.get),

Entry Code:

defget (Request): Text=""    Try: Text1= Request. post['text'] Text=Text1exceptexception,e:Print "Not POST"    Try: Text2= Request. get['text'] Text=Text2exceptexception,e:Print "Not GET"    Printtext img=Word.getwordcloud (text)#image_data = Img.read ()Rand = str (time.strftime ("%y-%m-%d-%h%m%s", Time.localtime (Time.time ()))) FileName= Path +"/temp/pic"+ Rand +". PNG"img.save (filename) image_data= open (filename,"RB"). Read ()returnHttpResponse (image_data,content_type="Image/png")

Demo

Python generates word clouds online

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.