Python generates word cloud for QQ chat record

Source: Internet
Author: User

First of all, in the QQ message manager will be a person or all of the chat records exported into TXT format, put in F disk;

Running the following Python code requires the installation of Jieba and Wordcloud two libraries.

Import Matplotlib.pyplot as Plt from Wordcloud import wordcloud import Jieba from Wordcloud import Wordcloud, Imagecolorge Nerator import OS import numpy as NP import pil.
    Image as Image newtext = [] #打开F盘下的聊天记录文件e. txt for word in open (' F:\\e.txt ', ' R ', encoding= ' Utf-8 '): tmp = Word[0:4] #print (TMP) if (tmp = = "2017" or tmp = = "= = = = = = = = =" = = Word[0:2] #print (tm P) if (tmp[0] = = ' [' or tmp[0] = = '): #过滤掉图片和表情, for example [picture],/cute continue newtext.append (word) #将过滤掉图片和表情和时间信息 
and QQ name the remainder of the text is rewritten in the Ab.txt file under F disk to go with open (' F:\\a2.txt ', ' W ', encoding= ' Utf-8 ') as F:for i in Newtext:f.write (i) #打开新生成的聊天记录文件 Text = open (' F:\\a2.txt ', ' r ', encoding = ' utf-8 '). Read () Word_jieba = jieba.cut (text, cut_all=true) word_sp Lit = "". Join (Word_jieba) #找一张小黄人logo图来生成配色方案, small yellow person logo map small yellow person. jpg path under f:\\ disk alice_coloring = Np.array (Image.open ( Os.path.join (' f:\\ ', ' Little yellow man. jpg ')) My_wordcloud = Wordcloud (background_color= "White", max_words=2000, Mask=alice_Coloring, max_font_size=40, random_state=42, font_path= ' c:/windows/fonts /simhei.ttf ') \ Generate (word_split) image_colors = Imagecolorgenerator (alice_coloring) plt.imshow (my_ Wordcloud.recolor (color_func=image_colors)) plt.imshow (My_wordcloud) plt.axis ("Off") Plt.show ()
The generated word cloud is as follows:


Filter writing is not good, the results of the chat recorded in the QQ expression did not filter out, still need to improve.

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.