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